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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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, calls gst_pad_activate_mode() with the pad's current mode
913 * and a FALSE argument.
915 * Returns: #TRUE if the operation was successful.
920 gst_pad_set_active (GstPad * pad, gboolean active)
924 gboolean ret = FALSE;
926 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
928 GST_OBJECT_LOCK (pad);
929 old = GST_PAD_MODE (pad);
930 ACQUIRE_PARENT (pad, parent, no_parent);
931 GST_OBJECT_UNLOCK (pad);
934 if (old == GST_PAD_MODE_NONE) {
935 GST_DEBUG_OBJECT (pad, "activating pad from none");
936 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
938 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
939 gst_pad_mode_get_name (old));
943 if (old == GST_PAD_MODE_NONE) {
944 GST_DEBUG_OBJECT (pad, "pad was inactive");
947 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
948 gst_pad_mode_get_name (old));
949 ret = gst_pad_activate_mode (pad, old, FALSE);
953 RELEASE_PARENT (parent);
955 if (G_UNLIKELY (!ret))
963 GST_DEBUG_OBJECT (pad, "no parent");
964 GST_OBJECT_UNLOCK (pad);
969 GST_OBJECT_LOCK (pad);
971 g_critical ("Failed to deactivate pad %s:%s, very bad",
972 GST_DEBUG_PAD_NAME (pad));
974 GST_WARNING_OBJECT (pad, "Failed to activate pad");
976 GST_OBJECT_UNLOCK (pad);
982 * gst_pad_activate_mode:
983 * @pad: the #GstPad to activate or deactivate.
984 * @mode: the requested activation mode
985 * @active: whether or not the pad should be active.
987 * Activates or deactivates the given pad in @mode via dispatching to the
988 * pad's activatemodefunc. For use from within pad activation functions only.
990 * If you don't know what this is, you probably don't want to call it.
992 * Returns: TRUE if the operation was successful.
997 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
999 gboolean res = FALSE;
1001 GstPadMode old, new;
1002 GstPadDirection dir;
1005 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1007 GST_OBJECT_LOCK (pad);
1008 old = GST_PAD_MODE (pad);
1009 dir = GST_PAD_DIRECTION (pad);
1010 ACQUIRE_PARENT (pad, parent, no_parent);
1011 GST_OBJECT_UNLOCK (pad);
1013 new = active ? mode : GST_PAD_MODE_NONE;
1018 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1019 /* pad was activate in the wrong direction, deactivate it
1020 * and reactivate it in the requested mode */
1021 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1022 gst_pad_mode_get_name (old));
1024 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
1025 goto deactivate_failed;
1029 case GST_PAD_MODE_PULL:
1031 if (dir == GST_PAD_SINK) {
1032 if ((peer = gst_pad_get_peer (pad))) {
1033 GST_DEBUG_OBJECT (pad, "calling peer");
1034 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1036 gst_object_unref (peer);
1038 /* there is no peer, this is only fatal when we activate. When we
1039 * deactivate, we must assume the application has unlinked the peer and
1040 * will deactivate it eventually. */
1044 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1047 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1048 goto failure; /* Can't activate pull on a src without a
1049 getrange function */
1057 pre_activate (pad, new);
1059 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1060 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1064 /* can happen for sinks of passthrough elements */
1067 post_activate (pad, new);
1069 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1070 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1075 /* Clear sticky flags on deactivation */
1077 GST_OBJECT_LOCK (pad);
1078 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1079 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1080 GST_OBJECT_UNLOCK (pad);
1084 RELEASE_PARENT (parent);
1090 GST_DEBUG_OBJECT (pad, "no parent");
1091 GST_OBJECT_UNLOCK (pad);
1096 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1097 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1102 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1103 "failed to %s in switch to %s mode from %s mode",
1104 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1105 gst_pad_mode_get_name (old));
1110 GST_OBJECT_LOCK (peer);
1111 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1112 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1113 GST_OBJECT_UNLOCK (peer);
1114 gst_object_unref (peer);
1119 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1120 "pad in pull mode");
1125 GST_OBJECT_LOCK (pad);
1126 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1127 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1128 GST_PAD_SET_FLUSHING (pad);
1129 GST_PAD_MODE (pad) = old;
1130 GST_OBJECT_UNLOCK (pad);
1136 * gst_pad_is_active:
1137 * @pad: the #GstPad to query
1139 * Query if a pad is active
1141 * Returns: TRUE if the pad is active.
1146 gst_pad_is_active (GstPad * pad)
1148 gboolean result = FALSE;
1150 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1152 GST_OBJECT_LOCK (pad);
1153 result = GST_PAD_IS_ACTIVE (pad);
1154 GST_OBJECT_UNLOCK (pad);
1160 * gst_pad_add_probe:
1161 * @pad: the #GstPad to add the probe to
1162 * @mask: the probe mask
1163 * @callback: #GstPadProbeCallback that will be called with notifications of
1165 * @user_data: (closure): user data passed to the callback
1166 * @destroy_data: #GDestroyNotify for user_data
1168 * Be notified of different states of pads. The provided callback is called for
1169 * every state that matches @mask.
1171 * Returns: an id or 0 on error. The id can be used to remove the probe with
1172 * gst_pad_remove_probe().
1177 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1178 GstPadProbeCallback callback, gpointer user_data,
1179 GDestroyNotify destroy_data)
1184 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1185 g_return_val_if_fail (mask != 0, 0);
1187 GST_OBJECT_LOCK (pad);
1189 /* make a new probe */
1190 hook = g_hook_alloc (&pad->probes);
1192 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1195 /* when no contraints are given for the types, assume all types are
1197 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1198 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1199 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1200 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1202 /* store our flags and other fields */
1203 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1204 hook->func = callback;
1205 hook->data = user_data;
1206 hook->destroy = destroy_data;
1207 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1210 g_hook_prepend (&pad->probes, hook);
1212 /* incremenent cookie so that the new hook get's called */
1213 pad->priv->probe_list_cookie++;
1215 /* get the id of the hook, we return this and it can be used to remove the
1217 res = hook->hook_id;
1219 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1221 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1222 /* we have a block probe */
1224 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1225 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1226 "now %d blocking probes", pad->num_blocked);
1229 /* call the callback if we need to be called for idle callbacks */
1230 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1231 if (pad->priv->using > 0) {
1232 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1233 * flag above, the last thread to leave the push will do the callback. New
1234 * threads going into the push will block. */
1235 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1236 "pad is in use, delay idle callback");
1237 GST_OBJECT_UNLOCK (pad);
1239 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1241 /* the pad is idle now, we can signal the idle callback now */
1242 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1243 "pad is idle, trigger idle callback");
1244 GST_OBJECT_UNLOCK (pad);
1246 callback (pad, &info, user_data);
1249 GST_OBJECT_UNLOCK (pad);
1255 cleanup_hook (GstPad * pad, GHook * hook)
1257 GstPadProbeType type;
1259 if (!G_HOOK_IS_VALID (hook))
1262 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1264 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1265 /* unblock when we remove the last blocking probe */
1267 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1269 if (pad->num_blocked == 0) {
1270 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1271 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1272 GST_PAD_BLOCK_BROADCAST (pad);
1275 g_hook_destroy_link (&pad->probes, hook);
1280 * gst_pad_remove_probe:
1281 * @pad: the #GstPad with the probe
1282 * @id: the probe id to remove
1284 * Remove the probe with @id from @pad.
1289 gst_pad_remove_probe (GstPad * pad, gulong id)
1293 g_return_if_fail (GST_IS_PAD (pad));
1295 GST_OBJECT_LOCK (pad);
1297 hook = g_hook_get (&pad->probes, id);
1301 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1303 cleanup_hook (pad, hook);
1304 GST_OBJECT_UNLOCK (pad);
1310 GST_OBJECT_UNLOCK (pad);
1311 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1317 * gst_pad_is_blocked:
1318 * @pad: the #GstPad to query
1320 * Checks if the pad is blocked or not. This function returns the
1321 * last requested state of the pad. It is not certain that the pad
1322 * is actually blocking at this point (see gst_pad_is_blocking()).
1324 * Returns: TRUE if the pad is blocked.
1329 gst_pad_is_blocked (GstPad * pad)
1331 gboolean result = FALSE;
1333 g_return_val_if_fail (GST_IS_PAD (pad), result);
1335 GST_OBJECT_LOCK (pad);
1336 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1337 GST_OBJECT_UNLOCK (pad);
1343 * gst_pad_is_blocking:
1344 * @pad: the #GstPad to query
1346 * Checks if the pad is blocking or not. This is a guaranteed state
1347 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1349 * Returns: TRUE if the pad is blocking.
1354 gst_pad_is_blocking (GstPad * pad)
1356 gboolean result = FALSE;
1358 g_return_val_if_fail (GST_IS_PAD (pad), result);
1360 GST_OBJECT_LOCK (pad);
1361 /* the blocking flag is only valid if the pad is not flushing */
1362 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1363 GST_OBJECT_UNLOCK (pad);
1369 * gst_pad_needs_reconfigure:
1370 * @pad: the #GstPad to check
1372 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1373 * if the flag was set.
1375 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1378 gst_pad_needs_reconfigure (GstPad * pad)
1380 gboolean reconfigure;
1382 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1384 GST_OBJECT_LOCK (pad);
1385 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1386 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1387 GST_OBJECT_UNLOCK (pad);
1393 * gst_pad_check_reconfigure:
1394 * @pad: the #GstPad to check
1396 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1397 * if the flag was set.
1399 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1402 gst_pad_check_reconfigure (GstPad * pad)
1404 gboolean reconfigure;
1406 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1408 GST_OBJECT_LOCK (pad);
1409 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1411 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1412 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1414 GST_OBJECT_UNLOCK (pad);
1420 * gst_pad_mark_reconfigure:
1421 * @pad: the #GstPad to mark
1423 * Mark a pad for needing reconfiguration. The next call to
1424 * gst_pad_check_reconfigure() will return %TRUE after this call.
1427 gst_pad_mark_reconfigure (GstPad * pad)
1429 g_return_if_fail (GST_IS_PAD (pad));
1431 GST_OBJECT_LOCK (pad);
1432 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1433 GST_OBJECT_UNLOCK (pad);
1437 * gst_pad_set_activate_function:
1439 * @f: the #GstPadActivateFunction to set.
1441 * Calls gst_pad_set_activate_function_full() with NULL for the user_data and
1445 * gst_pad_set_activate_function_full:
1447 * @activate: the #GstPadActivateFunction to set.
1448 * @user_data: user_data passed to @notify
1449 * @notify: notify called when @activate will not be used anymore.
1451 * Sets the given activate function for @pad. The activate function will
1452 * dispatch to gst_pad_activate_mode() to perform the actual activation.
1453 * Only makes sense to set on sink pads.
1455 * Call this function if your sink pad can start a pull-based task.
1458 gst_pad_set_activate_function_full (GstPad * pad,
1459 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1461 g_return_if_fail (GST_IS_PAD (pad));
1463 if (pad->activatenotify)
1464 pad->activatenotify (pad->activatedata);
1465 GST_PAD_ACTIVATEFUNC (pad) = activate;
1466 pad->activatedata = user_data;
1467 pad->activatenotify = notify;
1469 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1470 GST_DEBUG_FUNCPTR_NAME (activate));
1474 * gst_pad_set_activatemode_function:
1476 * @f: the #GstPadActivateModeFunction to set.
1478 * Calls gst_pad_set_activatemode_function_full() with NULL for the user_data and
1482 * gst_pad_set_activatemode_function_full:
1484 * @activatemode: the #GstPadActivateModeFunction to set.
1485 * @user_data: user_data passed to @notify
1486 * @notify: notify called when @activatemode will not be used anymore.
1488 * Sets the given activate_mode function for the pad. An activate_mode function
1489 * prepares the element for data passing.
1492 gst_pad_set_activatemode_function_full (GstPad * pad,
1493 GstPadActivateModeFunction activatemode, gpointer user_data,
1494 GDestroyNotify notify)
1496 g_return_if_fail (GST_IS_PAD (pad));
1498 if (pad->activatemodenotify)
1499 pad->activatemodenotify (pad->activatemodedata);
1500 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1501 pad->activatemodedata = user_data;
1502 pad->activatemodenotify = notify;
1504 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1505 GST_DEBUG_FUNCPTR_NAME (activatemode));
1509 * gst_pad_set_chain_function:
1510 * @p: a sink #GstPad.
1511 * @f: the #GstPadChainFunction to set.
1513 * Calls gst_pad_set_chain_function_full() with NULL for the user_data and
1517 * gst_pad_set_chain_function_full:
1518 * @pad: a sink #GstPad.
1519 * @chain: the #GstPadChainFunction to set.
1520 * @user_data: user_data passed to @notify
1521 * @notify: notify called when @chain will not be used anymore.
1523 * Sets the given chain function for the pad. The chain function is called to
1524 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1527 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1528 gpointer user_data, GDestroyNotify notify)
1530 g_return_if_fail (GST_IS_PAD (pad));
1531 g_return_if_fail (GST_PAD_IS_SINK (pad));
1533 if (pad->chainnotify)
1534 pad->chainnotify (pad->chaindata);
1535 GST_PAD_CHAINFUNC (pad) = chain;
1536 pad->chaindata = user_data;
1537 pad->chainnotify = notify;
1539 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1540 GST_DEBUG_FUNCPTR_NAME (chain));
1544 * gst_pad_set_chain_list_function:
1545 * @p: a sink #GstPad.
1546 * @f: the #GstPadChainListFunction to set.
1548 * Calls gst_pad_set_chain_list_function_full() with NULL for the user_data and
1552 * gst_pad_set_chain_list_function_full:
1553 * @pad: a sink #GstPad.
1554 * @chainlist: the #GstPadChainListFunction to set.
1555 * @user_data: user_data passed to @notify
1556 * @notify: notify called when @chainlist will not be used anymore.
1558 * Sets the given chain list function for the pad. The chainlist function is
1559 * called to process a #GstBufferList input buffer list. See
1560 * #GstPadChainListFunction for more details.
1563 gst_pad_set_chain_list_function_full (GstPad * pad,
1564 GstPadChainListFunction chainlist, gpointer user_data,
1565 GDestroyNotify notify)
1567 g_return_if_fail (GST_IS_PAD (pad));
1568 g_return_if_fail (GST_PAD_IS_SINK (pad));
1570 if (pad->chainlistnotify)
1571 pad->chainlistnotify (pad->chainlistdata);
1572 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1573 pad->chainlistdata = user_data;
1574 pad->chainlistnotify = notify;
1576 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1577 GST_DEBUG_FUNCPTR_NAME (chainlist));
1581 * gst_pad_set_getrange_function:
1582 * @p: a source #GstPad.
1583 * @f: the #GstPadGetRangeFunction to set.
1585 * Calls gst_pad_set_getrange_function_full() with NULL for the user_data and
1589 * gst_pad_set_getrange_function_full:
1590 * @pad: a source #GstPad.
1591 * @get: the #GstPadGetRangeFunction to set.
1592 * @user_data: user_data passed to @notify
1593 * @notify: notify called when @get will not be used anymore.
1595 * Sets the given getrange function for the pad. The getrange function is
1596 * called to produce a new #GstBuffer to start the processing pipeline. see
1597 * #GstPadGetRangeFunction for a description of the getrange function.
1600 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1601 gpointer user_data, GDestroyNotify notify)
1603 g_return_if_fail (GST_IS_PAD (pad));
1604 g_return_if_fail (GST_PAD_IS_SRC (pad));
1606 if (pad->getrangenotify)
1607 pad->getrangenotify (pad->getrangedata);
1608 GST_PAD_GETRANGEFUNC (pad) = get;
1609 pad->getrangedata = user_data;
1610 pad->getrangenotify = notify;
1612 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1613 GST_DEBUG_FUNCPTR_NAME (get));
1617 * gst_pad_set_event_function:
1618 * @p: a #GstPad of either direction.
1619 * @f: the #GstPadEventFunction to set.
1621 * Calls gst_pad_set_event_function_full() with NULL for the user_data and
1625 * gst_pad_set_event_function_full:
1626 * @pad: a #GstPad of either direction.
1627 * @event: the #GstPadEventFunction to set.
1628 * @user_data: user_data passed to @notify
1629 * @notify: notify called when @event will not be used anymore.
1631 * Sets the given event handler for the pad.
1634 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1635 gpointer user_data, GDestroyNotify notify)
1637 g_return_if_fail (GST_IS_PAD (pad));
1639 if (pad->eventnotify)
1640 pad->eventnotify (pad->eventdata);
1641 GST_PAD_EVENTFUNC (pad) = event;
1642 pad->eventdata = user_data;
1643 pad->eventnotify = notify;
1645 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1646 GST_DEBUG_FUNCPTR_NAME (event));
1650 * gst_pad_set_query_function:
1651 * @p: a #GstPad of either direction.
1652 * @f: the #GstPadQueryFunction to set.
1654 * Calls gst_pad_set_query_function_full() with NULL for the user_data and
1658 * gst_pad_set_query_function_full:
1659 * @pad: a #GstPad of either direction.
1660 * @query: the #GstPadQueryFunction to set.
1661 * @user_data: user_data passed to @notify
1662 * @notify: notify called when @query will not be used anymore.
1664 * Set the given query function for the pad.
1667 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1668 gpointer user_data, GDestroyNotify notify)
1670 g_return_if_fail (GST_IS_PAD (pad));
1672 if (pad->querynotify)
1673 pad->querynotify (pad->querydata);
1674 GST_PAD_QUERYFUNC (pad) = query;
1675 pad->querydata = user_data;
1676 pad->querynotify = notify;
1678 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1679 GST_DEBUG_FUNCPTR_NAME (query));
1683 * gst_pad_set_iterate_internal_links_function:
1684 * @p: a #GstPad of either direction.
1685 * @f: the #GstPadIterIntLinkFunction to set.
1687 * Calls gst_pad_set_iterate_internal_links_function_full() with NULL
1688 * for the user_data and notify.
1691 * gst_pad_set_iterate_internal_links_function_full:
1692 * @pad: a #GstPad of either direction.
1693 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1694 * @user_data: user_data passed to @notify
1695 * @notify: notify called when @iterintlink will not be used anymore.
1697 * Sets the given internal link iterator function for the pad.
1700 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1701 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1702 GDestroyNotify notify)
1704 g_return_if_fail (GST_IS_PAD (pad));
1706 if (pad->iterintlinknotify)
1707 pad->iterintlinknotify (pad->iterintlinkdata);
1708 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1709 pad->iterintlinkdata = user_data;
1710 pad->iterintlinknotify = notify;
1712 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1713 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1717 * gst_pad_set_link_function:
1719 * @f: the #GstPadLinkFunction to set.
1721 * Calls gst_pad_set_link_function_full() with NULL
1722 * for the user_data and notify.
1725 * gst_pad_set_link_function_full:
1727 * @link: the #GstPadLinkFunction to set.
1728 * @user_data: user_data passed to @notify
1729 * @notify: notify called when @link will not be used anymore.
1731 * Sets the given link function for the pad. It will be called when
1732 * the pad is linked with another pad.
1734 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1737 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1738 * cannot be made for some reason.
1740 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1741 * of the peer sink pad, if present.
1744 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1745 gpointer user_data, GDestroyNotify notify)
1747 g_return_if_fail (GST_IS_PAD (pad));
1749 if (pad->linknotify)
1750 pad->linknotify (pad->linkdata);
1751 GST_PAD_LINKFUNC (pad) = link;
1752 pad->linkdata = user_data;
1753 pad->linknotify = notify;
1755 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1756 GST_DEBUG_FUNCPTR_NAME (link));
1760 * gst_pad_set_unlink_function:
1762 * @f: the #GstPadUnlinkFunction to set.
1764 * Calls gst_pad_set_unlink_function_full() with NULL
1765 * for the user_data and notify.
1768 * gst_pad_set_unlink_function_full:
1770 * @unlink: the #GstPadUnlinkFunction to set.
1771 * @user_data: user_data passed to @notify
1772 * @notify: notify called when @unlink will not be used anymore.
1774 * Sets the given unlink function for the pad. It will be called
1775 * when the pad is unlinked.
1778 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1779 gpointer user_data, GDestroyNotify notify)
1781 g_return_if_fail (GST_IS_PAD (pad));
1783 if (pad->unlinknotify)
1784 pad->unlinknotify (pad->unlinkdata);
1785 GST_PAD_UNLINKFUNC (pad) = unlink;
1786 pad->unlinkdata = user_data;
1787 pad->unlinknotify = notify;
1789 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1790 GST_DEBUG_FUNCPTR_NAME (unlink));
1795 * @srcpad: the source #GstPad to unlink.
1796 * @sinkpad: the sink #GstPad to unlink.
1798 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1799 * signal on both pads.
1801 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1802 * the pads were not linked together.
1807 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1809 gboolean result = FALSE;
1810 GstElement *parent = NULL;
1812 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1813 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1814 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1815 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1817 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1818 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1819 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1821 /* We need to notify the parent before taking any pad locks as the bin in
1822 * question might be waiting for a lock on the pad while holding its lock
1823 * that our message will try to take. */
1824 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1825 if (GST_IS_ELEMENT (parent)) {
1826 gst_element_post_message (parent,
1827 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1828 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1830 gst_object_unref (parent);
1835 GST_OBJECT_LOCK (srcpad);
1836 GST_OBJECT_LOCK (sinkpad);
1838 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1839 goto not_linked_together;
1841 if (GST_PAD_UNLINKFUNC (srcpad)) {
1842 GstObject *tmpparent;
1844 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
1846 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
1847 RELEASE_PARENT (tmpparent);
1850 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1851 GstObject *tmpparent;
1853 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
1855 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
1856 RELEASE_PARENT (tmpparent);
1860 /* first clear peers */
1861 GST_PAD_PEER (srcpad) = NULL;
1862 GST_PAD_PEER (sinkpad) = NULL;
1864 GST_OBJECT_UNLOCK (sinkpad);
1865 GST_OBJECT_UNLOCK (srcpad);
1867 /* fire off a signal to each of the pads telling them
1868 * that they've been unlinked */
1869 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1870 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1872 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1873 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1878 if (parent != NULL) {
1879 gst_element_post_message (parent,
1880 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1881 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1882 gst_object_unref (parent);
1887 not_linked_together:
1889 /* we do not emit a warning in this case because unlinking cannot
1890 * be made MT safe.*/
1891 GST_OBJECT_UNLOCK (sinkpad);
1892 GST_OBJECT_UNLOCK (srcpad);
1898 * gst_pad_is_linked:
1899 * @pad: pad to check
1901 * Checks if a @pad is linked to another pad or not.
1903 * Returns: TRUE if the pad is linked, FALSE otherwise.
1908 gst_pad_is_linked (GstPad * pad)
1912 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1914 GST_OBJECT_LOCK (pad);
1915 result = (GST_PAD_PEER (pad) != NULL);
1916 GST_OBJECT_UNLOCK (pad);
1921 /* get the caps from both pads and see if the intersection
1924 * This function should be called with the pad LOCK on both
1928 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1929 GstPadLinkCheck flags)
1931 GstCaps *srccaps = NULL;
1932 GstCaps *sinkcaps = NULL;
1933 gboolean compatible = FALSE;
1935 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1938 /* Doing the expensive caps checking takes priority over only checking the template caps */
1939 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1940 GST_OBJECT_UNLOCK (sink);
1941 GST_OBJECT_UNLOCK (src);
1943 srccaps = gst_pad_query_caps (src, NULL);
1944 sinkcaps = gst_pad_query_caps (sink, NULL);
1946 GST_OBJECT_LOCK (src);
1947 GST_OBJECT_LOCK (sink);
1949 /* If one of the two pads doesn't have a template, consider the intersection
1951 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1952 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1956 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1958 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1961 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1963 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1966 /* if we have caps on both pads we can check the intersection. If one
1967 * of the caps is NULL, we return TRUE. */
1968 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1970 gst_caps_unref (srccaps);
1972 gst_caps_unref (sinkcaps);
1976 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1977 gst_caps_unref (srccaps);
1978 gst_caps_unref (sinkcaps);
1981 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1982 (compatible ? "" : "not"));
1987 /* check if the grandparents of both pads are the same.
1988 * This check is required so that we don't try to link
1989 * pads from elements in different bins without ghostpads.
1991 * The LOCK should be held on both pads
1994 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1996 GstObject *psrc, *psink;
1998 psrc = GST_OBJECT_PARENT (src);
1999 psink = GST_OBJECT_PARENT (sink);
2001 /* if one of the pads has no parent, we allow the link */
2002 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2005 /* only care about parents that are elements */
2006 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2007 goto no_element_parent;
2009 /* if the parents are the same, we have a loop */
2010 if (G_UNLIKELY (psrc == psink))
2013 /* if they both have a parent, we check the grandparents. We can not lock
2014 * the parent because we hold on the child (pad) and the locking order is
2015 * parent >> child. */
2016 psrc = GST_OBJECT_PARENT (psrc);
2017 psink = GST_OBJECT_PARENT (psink);
2019 /* if they have grandparents but they are not the same */
2020 if (G_UNLIKELY (psrc != psink))
2021 goto wrong_grandparents;
2028 GST_CAT_DEBUG (GST_CAT_CAPS,
2029 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2030 GST_PTR_FORMAT, psrc, psink);
2035 GST_CAT_DEBUG (GST_CAT_CAPS,
2036 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2037 GST_PTR_FORMAT, psrc, psink);
2042 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2048 GST_CAT_DEBUG (GST_CAT_CAPS,
2049 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2050 GST_PTR_FORMAT, psrc, psink);
2055 /* FIXME leftover from an attempt at refactoring... */
2056 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2057 * the two pads will be locked in the srcpad, sinkpad order. */
2058 static GstPadLinkReturn
2059 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2061 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2062 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2064 GST_OBJECT_LOCK (srcpad);
2066 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2067 goto src_was_linked;
2069 GST_OBJECT_LOCK (sinkpad);
2071 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2072 goto sink_was_linked;
2074 /* check hierarchy, pads can only be linked if the grandparents
2076 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2077 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2078 goto wrong_hierarchy;
2080 /* check pad caps for non-empty intersection */
2081 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2084 /* FIXME check pad scheduling for non-empty intersection */
2086 return GST_PAD_LINK_OK;
2090 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2091 GST_DEBUG_PAD_NAME (srcpad),
2092 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2093 /* we do not emit a warning in this case because unlinking cannot
2094 * be made MT safe.*/
2095 GST_OBJECT_UNLOCK (srcpad);
2096 return GST_PAD_LINK_WAS_LINKED;
2100 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2101 GST_DEBUG_PAD_NAME (sinkpad),
2102 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2103 /* we do not emit a warning in this case because unlinking cannot
2104 * be made MT safe.*/
2105 GST_OBJECT_UNLOCK (sinkpad);
2106 GST_OBJECT_UNLOCK (srcpad);
2107 return GST_PAD_LINK_WAS_LINKED;
2111 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2112 GST_OBJECT_UNLOCK (sinkpad);
2113 GST_OBJECT_UNLOCK (srcpad);
2114 return GST_PAD_LINK_WRONG_HIERARCHY;
2118 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2119 GST_OBJECT_UNLOCK (sinkpad);
2120 GST_OBJECT_UNLOCK (srcpad);
2121 return GST_PAD_LINK_NOFORMAT;
2127 * @srcpad: the source #GstPad.
2128 * @sinkpad: the sink #GstPad.
2130 * Checks if the source pad and the sink pad are compatible so they can be
2133 * Returns: TRUE if the pads can be linked.
2136 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2138 GstPadLinkReturn result;
2140 /* generic checks */
2141 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2142 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2144 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2145 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2147 /* gst_pad_link_prepare does everything for us, we only release the locks
2148 * on the pads that it gets us. If this function returns !OK the locks are not
2150 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2151 if (result != GST_PAD_LINK_OK)
2154 GST_OBJECT_UNLOCK (srcpad);
2155 GST_OBJECT_UNLOCK (sinkpad);
2158 return result == GST_PAD_LINK_OK;
2162 * gst_pad_link_full:
2163 * @srcpad: the source #GstPad to link.
2164 * @sinkpad: the sink #GstPad to link.
2165 * @flags: the checks to validate when linking
2167 * Links the source pad and the sink pad.
2169 * This variant of #gst_pad_link provides a more granular control on the
2170 * checks being done when linking. While providing some considerable speedups
2171 * the caller of this method must be aware that wrong usage of those flags
2172 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2173 * for more information.
2177 * Returns: A result code indicating if the connection worked or
2181 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2183 GstPadLinkReturn result;
2185 GstPadLinkFunction srcfunc, sinkfunc;
2187 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2188 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2189 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2190 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2191 GST_PAD_LINK_WRONG_DIRECTION);
2193 /* Notify the parent early. See gst_pad_unlink for details. */
2194 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2195 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2196 gst_element_post_message (parent,
2197 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2198 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2200 gst_object_unref (parent);
2205 /* prepare will also lock the two pads */
2206 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2208 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2211 /* must set peers before calling the link function */
2212 GST_PAD_PEER (srcpad) = sinkpad;
2213 GST_PAD_PEER (sinkpad) = srcpad;
2215 /* check events, when something is different, mark pending */
2216 schedule_events (srcpad, sinkpad);
2218 /* get the link functions */
2219 srcfunc = GST_PAD_LINKFUNC (srcpad);
2220 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2222 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2223 /* custom link functions, execute them */
2224 GST_OBJECT_UNLOCK (sinkpad);
2225 GST_OBJECT_UNLOCK (srcpad);
2228 GstObject *tmpparent;
2230 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2231 /* this one will call the peer link function */
2232 result = srcfunc (srcpad, tmpparent, sinkpad);
2233 RELEASE_PARENT (tmpparent);
2234 } else if (sinkfunc) {
2235 GstObject *tmpparent;
2237 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2238 /* if no source link function, we need to call the sink link
2239 * function ourselves. */
2240 result = sinkfunc (sinkpad, tmpparent, srcpad);
2241 RELEASE_PARENT (tmpparent);
2245 GST_OBJECT_LOCK (srcpad);
2246 GST_OBJECT_LOCK (sinkpad);
2248 /* we released the lock, check if the same pads are linked still */
2249 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2250 goto concurrent_link;
2252 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2255 GST_OBJECT_UNLOCK (sinkpad);
2256 GST_OBJECT_UNLOCK (srcpad);
2258 /* fire off a signal to each of the pads telling them
2259 * that they've been linked */
2260 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2261 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2263 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2264 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2266 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2269 if (G_LIKELY (parent)) {
2270 gst_element_post_message (parent,
2271 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2272 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2273 gst_object_unref (parent);
2281 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2282 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2283 GST_OBJECT_UNLOCK (sinkpad);
2284 GST_OBJECT_UNLOCK (srcpad);
2286 /* The other link operation succeeded first */
2287 result = GST_PAD_LINK_WAS_LINKED;
2292 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2293 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2295 GST_PAD_PEER (srcpad) = NULL;
2296 GST_PAD_PEER (sinkpad) = NULL;
2298 GST_OBJECT_UNLOCK (sinkpad);
2299 GST_OBJECT_UNLOCK (srcpad);
2307 * @srcpad: the source #GstPad to link.
2308 * @sinkpad: the sink #GstPad to link.
2310 * Links the source pad and the sink pad.
2312 * Returns: A result code indicating if the connection worked or
2318 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2320 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2324 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2326 GstPadTemplate **template_p;
2328 /* this function would need checks if it weren't static */
2330 GST_OBJECT_LOCK (pad);
2331 template_p = &pad->padtemplate;
2332 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2333 GST_OBJECT_UNLOCK (pad);
2336 gst_pad_template_pad_created (templ, pad);
2340 * gst_pad_get_pad_template:
2343 * Gets the template for @pad.
2345 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2346 * instantiated, or %NULL if this pad has no template. Unref after
2350 gst_pad_get_pad_template (GstPad * pad)
2352 GstPadTemplate *templ;
2354 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2356 templ = GST_PAD_PAD_TEMPLATE (pad);
2358 return (templ ? gst_object_ref (templ) : NULL);
2362 * gst_pad_has_current_caps:
2363 * @pad: a #GstPad to check
2365 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2367 * Returns: TRUE when @pad has caps associated with it.
2370 gst_pad_has_current_caps (GstPad * pad)
2375 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2377 GST_OBJECT_LOCK (pad);
2378 caps = get_pad_caps (pad);
2379 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2380 "check current pad caps %" GST_PTR_FORMAT, caps);
2381 result = (caps != NULL);
2382 GST_OBJECT_UNLOCK (pad);
2388 * gst_pad_get_current_caps:
2389 * @pad: a #GstPad to get the current capabilities of.
2391 * Gets the capabilities currently configured on @pad with the last
2392 * #GST_EVENT_CAPS event.
2394 * Returns: the current caps of the pad with incremented ref-count.
2397 gst_pad_get_current_caps (GstPad * pad)
2401 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2403 GST_OBJECT_LOCK (pad);
2404 if ((result = get_pad_caps (pad)))
2405 gst_caps_ref (result);
2406 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2407 "get current pad caps %" GST_PTR_FORMAT, result);
2408 GST_OBJECT_UNLOCK (pad);
2414 * gst_pad_get_pad_template_caps:
2415 * @pad: a #GstPad to get the template capabilities from.
2417 * Gets the capabilities for @pad's template.
2419 * Returns: (transfer full): the #GstCaps of this pad template.
2420 * Unref after usage.
2423 gst_pad_get_pad_template_caps (GstPad * pad)
2425 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2427 if (GST_PAD_PAD_TEMPLATE (pad))
2428 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2430 return gst_caps_ref (GST_CAPS_ANY);
2435 * @pad: a #GstPad to get the peer of.
2437 * Gets the peer of @pad. This function refs the peer pad so
2438 * you need to unref it after use.
2440 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2445 gst_pad_get_peer (GstPad * pad)
2449 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2451 GST_OBJECT_LOCK (pad);
2452 result = GST_PAD_PEER (pad);
2454 gst_object_ref (result);
2455 GST_OBJECT_UNLOCK (pad);
2461 * gst_pad_get_allowed_caps:
2464 * Gets the capabilities of the allowed media types that can flow through
2465 * @pad and its peer.
2467 * The allowed capabilities is calculated as the intersection of the results of
2468 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2469 * on the resulting caps.
2471 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2472 * caps when you no longer need it. This function returns NULL when @pad
2478 gst_pad_get_allowed_caps (GstPad * pad)
2484 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2486 GST_OBJECT_LOCK (pad);
2487 peer = GST_PAD_PEER (pad);
2488 if (G_UNLIKELY (peer == NULL))
2491 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2493 gst_object_ref (peer);
2494 GST_OBJECT_UNLOCK (pad);
2495 mycaps = gst_pad_query_caps (pad, NULL);
2497 caps = gst_pad_query_caps (peer, mycaps);
2498 gst_object_unref (peer);
2500 gst_caps_unref (mycaps);
2502 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2509 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2510 GST_OBJECT_UNLOCK (pad);
2517 * gst_pad_iterate_internal_links_default:
2518 * @pad: the #GstPad to get the internal links of.
2519 * @parent: the parent of @pad or NULL
2521 * Iterate the list of pads to which the given pad is linked to inside of
2522 * the parent element.
2523 * This is the default handler, and thus returns an iterator of all of the
2524 * pads inside the parent element with opposite direction.
2526 * The caller must free this iterator after use with gst_iterator_free().
2528 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2529 * returned pad with gst_object_unref().
2532 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2539 GstElement *eparent;
2541 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2543 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2544 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2546 GST_OBJECT_LOCK (pad);
2547 eparent = GST_PAD_PARENT (pad);
2548 if (!eparent || !GST_IS_ELEMENT (eparent))
2551 gst_object_ref (eparent);
2552 GST_OBJECT_UNLOCK (pad);
2555 if (pad->direction == GST_PAD_SRC)
2556 padlist = &eparent->sinkpads;
2558 padlist = &eparent->srcpads;
2560 GST_DEBUG_OBJECT (pad, "Making iterator");
2562 cookie = &eparent->pads_cookie;
2564 lock = GST_OBJECT_GET_LOCK (eparent);
2566 res = gst_iterator_new_list (GST_TYPE_PAD,
2567 lock, cookie, padlist, (GObject *) owner, NULL);
2569 gst_object_unref (owner);
2576 GST_OBJECT_UNLOCK (pad);
2577 GST_DEBUG_OBJECT (pad, "no parent element");
2583 * gst_pad_iterate_internal_links:
2584 * @pad: the GstPad to get the internal links of.
2586 * Gets an iterator for the pads to which the given pad is linked to inside
2587 * of the parent element.
2589 * Each #GstPad element yielded by the iterator will have its refcount increased,
2590 * so unref after use.
2592 * Free-function: gst_iterator_free
2594 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2595 * pad does not have an iterator function configured. Use
2596 * gst_iterator_free() after usage.
2599 gst_pad_iterate_internal_links (GstPad * pad)
2601 GstIterator *res = NULL;
2604 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2606 GST_OBJECT_LOCK (pad);
2607 ACQUIRE_PARENT (pad, parent, no_parent);
2608 GST_OBJECT_UNLOCK (pad);
2610 if (GST_PAD_ITERINTLINKFUNC (pad))
2611 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2613 RELEASE_PARENT (parent);
2620 GST_DEBUG_OBJECT (pad, "no parent");
2621 GST_OBJECT_UNLOCK (pad);
2629 * @forward: (scope call): a #GstPadForwardFunction
2630 * @user_data: user data passed to @forward
2632 * Calls @forward for all internally linked pads of @pad. This function deals with
2633 * dynamically changing internal pads and will make sure that the @forward
2634 * function is only called once for each pad.
2636 * When @forward returns TRUE, no further pads will be processed.
2638 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2641 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2644 gboolean result = FALSE;
2646 gboolean done = FALSE;
2647 GValue item = { 0, };
2648 GList *pushed_pads = NULL;
2650 iter = gst_pad_iterate_internal_links (pad);
2656 switch (gst_iterator_next (iter, &item)) {
2657 case GST_ITERATOR_OK:
2661 intpad = g_value_get_object (&item);
2663 /* if already pushed, skip. FIXME, find something faster to tag pads */
2664 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2665 g_value_reset (&item);
2669 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2670 GST_DEBUG_PAD_NAME (intpad));
2671 done = result = forward (intpad, user_data);
2673 pushed_pads = g_list_prepend (pushed_pads, intpad);
2675 g_value_reset (&item);
2678 case GST_ITERATOR_RESYNC:
2679 /* We don't reset the result here because we don't push the event
2680 * again on pads that got the event already and because we need
2681 * to consider the result of the previous pushes */
2682 gst_iterator_resync (iter);
2684 case GST_ITERATOR_ERROR:
2685 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2688 case GST_ITERATOR_DONE:
2693 g_value_unset (&item);
2694 gst_iterator_free (iter);
2696 g_list_free (pushed_pads);
2706 gboolean dispatched;
2710 event_forward_func (GstPad * pad, EventData * data)
2712 /* for each pad we send to, we should ref the event; it's up
2713 * to downstream to unref again when handled. */
2714 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2715 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2717 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2719 data->dispatched = TRUE;
2726 * gst_pad_event_default:
2727 * @pad: a #GstPad to call the default event handler on.
2728 * @parent: the parent of @pad or NULL
2729 * @event: (transfer full): the #GstEvent to handle.
2731 * Invokes the default event handler for the given pad.
2733 * The EOS event will pause the task associated with @pad before it is forwarded
2734 * to all internally linked pads,
2736 * The the event is sent to all pads internally linked to @pad. This function
2737 * takes ownership of @event.
2739 * Returns: TRUE if the event was sent successfully.
2742 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2744 gboolean result, forward = TRUE;
2746 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2747 g_return_val_if_fail (event != NULL, FALSE);
2749 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2752 switch (GST_EVENT_TYPE (event)) {
2753 case GST_EVENT_CAPS:
2754 forward = GST_PAD_IS_PROXY_CAPS (pad);
2765 data.dispatched = FALSE;
2766 data.result = FALSE;
2768 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2770 /* for sinkpads without a parent element or without internal links, nothing
2771 * will be dispatched but we still want to return TRUE. */
2772 if (data.dispatched)
2773 result = data.result;
2778 gst_event_unref (event);
2783 /* Default accept caps implementation just checks against
2784 * the allowed caps for the pad */
2786 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2788 /* get the caps and see if it intersects to something not empty */
2789 GstCaps *caps, *allowed;
2792 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2794 /* first forward the query to internally linked pads when we are dealing with
2796 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2797 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2802 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2803 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2805 allowed = gst_pad_query_caps (pad, NULL);
2806 gst_query_parse_accept_caps (query, &caps);
2809 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2810 result = gst_caps_is_subset (caps, allowed);
2811 gst_caps_unref (allowed);
2813 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2816 gst_query_set_accept_caps_result (query, result);
2822 /* Default caps implementation */
2824 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2826 GstCaps *result = NULL, *filter;
2827 GstPadTemplate *templ;
2828 gboolean fixed_caps;
2830 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
2833 /* first try to proxy if we must */
2834 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2835 if ((gst_pad_proxy_query_caps (pad, query))) {
2840 gst_query_parse_caps (query, &filter);
2842 /* no proxy or it failed, do default handling */
2843 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2845 GST_OBJECT_LOCK (pad);
2847 /* fixed caps, try the negotiated caps first */
2848 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2849 if ((result = get_pad_caps (pad)))
2850 goto filter_done_unlock;
2853 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2854 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2855 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2856 goto filter_done_unlock;
2860 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2861 "non-fixed pad caps: trying pad caps");
2862 /* non fixed caps, try the negotiated caps */
2863 if ((result = get_pad_caps (pad)))
2864 goto filter_done_unlock;
2867 /* this almost never happens */
2868 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2869 result = GST_CAPS_ANY;
2872 GST_OBJECT_UNLOCK (pad);
2874 /* run the filter on the result */
2876 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2877 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2878 GST_PTR_FORMAT, result, result, filter, filter);
2879 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2880 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2883 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2884 "using caps %p %" GST_PTR_FORMAT, result, result);
2885 result = gst_caps_ref (result);
2887 gst_query_set_caps_result (query, result);
2888 gst_caps_unref (result);
2898 gboolean dispatched;
2902 query_forward_func (GstPad * pad, QueryData * data)
2904 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
2905 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
2907 data->result |= gst_pad_peer_query (pad, data->query);
2909 data->dispatched = TRUE;
2911 /* stop on first successful reply */
2912 return data->result;
2916 * gst_pad_query_default:
2917 * @pad: a #GstPad to call the default query handler on.
2918 * @parent: the parent of @pad or NULL
2919 * @query: (transfer none): the #GstQuery to handle.
2921 * Invokes the default query handler for the given pad.
2922 * The query is sent to all pads internally linked to @pad. Note that
2923 * if there are many possible sink pads that are internally linked to
2924 * @pad, only one will be sent the query.
2925 * Multi-sinkpad elements should implement custom query handlers.
2927 * Returns: TRUE if the query was performed successfully.
2930 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2932 gboolean forward, ret = FALSE;
2934 switch (GST_QUERY_TYPE (query)) {
2935 case GST_QUERY_SCHEDULING:
2936 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
2938 case GST_QUERY_ALLOCATION:
2939 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2941 case GST_QUERY_ACCEPT_CAPS:
2942 ret = gst_pad_query_accept_caps_default (pad, query);
2945 case GST_QUERY_CAPS:
2946 ret = gst_pad_query_caps_default (pad, query);
2949 case GST_QUERY_POSITION:
2950 case GST_QUERY_SEEKING:
2951 case GST_QUERY_FORMATS:
2952 case GST_QUERY_LATENCY:
2953 case GST_QUERY_JITTER:
2954 case GST_QUERY_RATE:
2955 case GST_QUERY_CONVERT:
2961 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
2962 query, GST_QUERY_TYPE_NAME (query));
2968 data.dispatched = FALSE;
2969 data.result = FALSE;
2971 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
2973 if (data.dispatched) {
2976 /* nothing dispatched, assume drained */
2977 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
2987 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2989 GstPad *pad = data->pad;
2990 GstPadProbeInfo *info = data->info;
2991 GstPadProbeType type, flags;
2992 GstPadProbeCallback callback;
2993 GstPadProbeReturn ret;
2995 /* if we have called this callback, do nothing */
2996 if (PROBE_COOKIE (hook) == data->cookie) {
2997 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2998 "hook %lu, cookie %u already called", hook->hook_id,
2999 PROBE_COOKIE (hook));
3003 PROBE_COOKIE (hook) = data->cookie;
3005 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3008 /* one of the data types */
3009 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3011 /* one of the scheduling types */
3012 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3014 /* one of the blocking types must match */
3015 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3016 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3018 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3019 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3020 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3023 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3024 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3025 PROBE_COOKIE (hook), flags);
3027 data->marshalled = TRUE;
3029 callback = (GstPadProbeCallback) hook->func;
3030 if (callback == NULL)
3033 info->id = hook->hook_id;
3035 GST_OBJECT_UNLOCK (pad);
3037 ret = callback (pad, info, hook->data);
3039 GST_OBJECT_LOCK (pad);
3042 case GST_PAD_PROBE_REMOVE:
3043 /* remove the probe */
3044 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3045 cleanup_hook (pad, hook);
3047 case GST_PAD_PROBE_DROP:
3048 /* need to drop the data, make sure other probes don't get called
3050 GST_DEBUG_OBJECT (pad, "asked to drop item");
3051 info->type = GST_PAD_PROBE_TYPE_INVALID;
3052 data->dropped = TRUE;
3054 case GST_PAD_PROBE_PASS:
3055 /* inform the pad block to let things pass */
3056 GST_DEBUG_OBJECT (pad, "asked to pass item");
3060 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3067 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3068 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3069 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3074 /* a probe that does not take or return any data */
3075 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3077 if (G_UNLIKELY (pad->num_probes)) { \
3078 /* pass NULL as the data item */ \
3079 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3080 ret = do_probe_callbacks (pad, &info, defaultval); \
3081 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3086 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3088 if (G_UNLIKELY (pad->num_probes)) { \
3089 /* pass the data item */ \
3090 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3091 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3092 /* store the possibly updated data item */ \
3093 data = GST_PAD_PROBE_INFO_DATA (&info); \
3094 /* if something went wrong, exit */ \
3095 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3100 #define PROBE_PUSH(pad,mask,data,label) \
3101 PROBE_FULL(pad, mask, data, -1, -1, label);
3102 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3103 PROBE_FULL(pad, mask, data, offs, size, label);
3105 static GstFlowReturn
3106 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3107 GstFlowReturn defaultval)
3116 data.marshalled = FALSE;
3117 data.dropped = FALSE;
3118 data.cookie = ++pad->priv->probe_cookie;
3121 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3124 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3125 "do probes cookie %u", data.cookie);
3126 cookie = pad->priv->probe_list_cookie;
3128 g_hook_list_marshal (&pad->probes, TRUE,
3129 (GHookMarshaller) probe_hook_marshal, &data);
3131 /* if the list changed, call the new callbacks (they will not have their
3132 * cookie set to data.cookie */
3133 if (cookie != pad->priv->probe_list_cookie) {
3134 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3135 "probe list changed, restarting");
3139 /* the first item that dropped will stop the hooks and then we drop here */
3143 /* if no handler matched and we are blocking, let the item pass */
3144 if (!data.marshalled && is_block)
3147 /* At this point, all handlers returned either OK or PASS. If one handler
3148 * returned PASS, let the item pass */
3153 while (GST_PAD_IS_BLOCKED (pad)) {
3154 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3155 "we are blocked %d times", pad->num_blocked);
3157 /* we might have released the lock */
3158 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3161 /* now we block the streaming thread. It can be unlocked when we
3162 * deactivate the pad (which will also set the FLUSHING flag) or
3163 * when the pad is unblocked. A flushing event will also unblock
3164 * the pad after setting the FLUSHING flag. */
3165 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3166 "Waiting to be unblocked or set flushing");
3167 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3168 GST_PAD_BLOCK_WAIT (pad);
3169 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3170 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3172 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3182 GST_DEBUG_OBJECT (pad, "pad is flushing");
3183 return GST_FLOW_FLUSHING;
3187 GST_DEBUG_OBJECT (pad, "data is dropped");
3188 return GST_FLOW_CUSTOM_SUCCESS;
3192 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3193 GST_DEBUG_OBJECT (pad, "data is passed");
3201 * gst_pad_get_offset:
3204 * Get the offset applied to the running time of @pad. @pad has to be a source
3207 * Returns: the offset.
3210 gst_pad_get_offset (GstPad * pad)
3214 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3216 GST_OBJECT_LOCK (pad);
3217 result = pad->offset;
3218 GST_OBJECT_UNLOCK (pad);
3224 * gst_pad_set_offset:
3226 * @offset: the offset
3228 * Set the offset that will be applied to the running time of @pad.
3231 gst_pad_set_offset (GstPad * pad, gint64 offset)
3235 g_return_if_fail (GST_IS_PAD (pad));
3237 GST_OBJECT_LOCK (pad);
3238 /* if nothing changed, do nothing */
3239 if (pad->offset == offset)
3242 pad->offset = offset;
3243 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3245 /* sinkpads will apply their offset the next time a segment
3246 * event is received. */
3247 if (GST_PAD_IS_SINK (pad))
3250 /* resend the last segment event on next buffer push */
3251 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3252 ev->received = FALSE;
3253 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3257 GST_OBJECT_UNLOCK (pad);
3264 /* If TRUE and ret is not OK this means
3265 * that pushing the EOS event failed
3270 /* should be called with pad LOCK */
3272 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3274 PushStickyData *data = user_data;
3275 GstEvent *event = ev->event;
3278 GST_DEBUG_OBJECT (pad, "event %s was already received",
3279 GST_EVENT_TYPE_NAME (event));
3283 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3284 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3286 switch (data->ret) {
3288 ev->received = TRUE;
3289 GST_DEBUG_OBJECT (pad, "event %s marked received",
3290 GST_EVENT_TYPE_NAME (event));
3292 case GST_FLOW_CUSTOM_SUCCESS:
3293 /* we can't assume the event is received when it was dropped */
3294 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3295 GST_EVENT_TYPE_NAME (event));
3296 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3297 data->ret = GST_FLOW_OK;
3299 case GST_FLOW_NOT_LINKED:
3300 /* not linked is not a problem, we are sticky so the event will be
3301 * sent later but only for non-EOS events */
3302 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3303 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3304 data->ret = GST_FLOW_OK;
3305 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3308 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3309 gst_flow_get_name (data->ret));
3310 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3314 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3315 data->was_eos = TRUE;
3317 return data->ret == GST_FLOW_OK;
3320 /* check sticky events and push them when needed. should be called
3322 static inline GstFlowReturn
3323 check_sticky (GstPad * pad)
3325 PushStickyData data = { GST_FLOW_OK, FALSE };
3327 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3328 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3330 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3331 events_foreach (pad, push_sticky, &data);
3333 /* If there's an EOS event we must push it downstream
3334 * even if sending a previous sticky event failed.
3335 * Otherwise the pipeline might wait forever for EOS.
3337 * Only do this if pushing another event than the EOS
3340 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3341 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3343 if (ev && !ev->received) {
3344 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3345 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3346 /* the event could have been dropped. Because this can only
3347 * happen if the user asked for it, it's not an error */
3348 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3349 data.ret = GST_FLOW_OK;
3359 * @pad: a #GstPad to invoke the default query on.
3360 * @query: (transfer none): the #GstQuery to perform.
3362 * Dispatches a query to a pad. The query should have been allocated by the
3363 * caller via one of the type-specific allocation functions. The element that
3364 * the pad belongs to is responsible for filling the query with an appropriate
3365 * response, which should then be parsed with a type-specific query parsing
3368 * Again, the caller is responsible for both the allocation and deallocation of
3369 * the query structure.
3371 * Please also note that some queries might need a running pipeline to work.
3373 * Returns: TRUE if the query could be performed.
3376 gst_pad_query (GstPad * pad, GstQuery * query)
3379 gboolean res, serialized;
3380 GstPadQueryFunction func;
3381 GstPadProbeType type;
3384 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3385 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3387 if (GST_PAD_IS_SRC (pad)) {
3388 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3389 goto wrong_direction;
3390 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3391 } else if (GST_PAD_IS_SINK (pad)) {
3392 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3393 goto wrong_direction;
3394 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3396 goto unknown_direction;
3398 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3399 GST_QUERY_TYPE_NAME (query));
3401 serialized = GST_QUERY_IS_SERIALIZED (query);
3402 if (G_UNLIKELY (serialized))
3403 GST_PAD_STREAM_LOCK (pad);
3405 GST_OBJECT_LOCK (pad);
3406 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3407 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3408 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3410 ACQUIRE_PARENT (pad, parent, no_parent);
3411 GST_OBJECT_UNLOCK (pad);
3413 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3416 res = func (pad, parent, query);
3418 RELEASE_PARENT (parent);
3420 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3421 GST_QUERY_TYPE_NAME (query), res);
3426 GST_OBJECT_LOCK (pad);
3427 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3428 GST_OBJECT_UNLOCK (pad);
3430 if (G_UNLIKELY (serialized))
3431 GST_PAD_STREAM_UNLOCK (pad);
3438 g_warning ("pad %s:%s query %s in wrong direction",
3439 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3444 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3449 GST_DEBUG_OBJECT (pad, "had no parent");
3450 GST_OBJECT_UNLOCK (pad);
3451 if (G_UNLIKELY (serialized))
3452 GST_PAD_STREAM_UNLOCK (pad);
3457 GST_DEBUG_OBJECT (pad, "had no query function");
3458 RELEASE_PARENT (parent);
3459 if (G_UNLIKELY (serialized))
3460 GST_PAD_STREAM_UNLOCK (pad);
3465 GST_DEBUG_OBJECT (pad, "query failed");
3466 if (G_UNLIKELY (serialized))
3467 GST_PAD_STREAM_UNLOCK (pad);
3472 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3473 GST_OBJECT_UNLOCK (pad);
3474 if (G_UNLIKELY (serialized))
3475 GST_PAD_STREAM_UNLOCK (pad);
3477 /* if a probe dropped, we don't sent it further but assume that the probe
3478 * answered the query and return TRUE */
3479 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3489 * gst_pad_peer_query:
3490 * @pad: a #GstPad to invoke the peer query on.
3491 * @query: (transfer none): the #GstQuery to perform.
3493 * Performs gst_pad_query() on the peer of @pad.
3495 * The caller is responsible for both the allocation and deallocation of
3496 * the query structure.
3498 * Returns: TRUE if the query could be performed. This function returns %FALSE
3499 * if @pad has no peer.
3502 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3505 GstPadProbeType type;
3506 gboolean res, serialized;
3509 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3510 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3512 if (GST_PAD_IS_SRC (pad)) {
3513 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3514 goto wrong_direction;
3515 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3516 } else if (GST_PAD_IS_SINK (pad)) {
3517 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3518 goto wrong_direction;
3519 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3521 goto unknown_direction;
3523 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3524 GST_QUERY_TYPE_NAME (query));
3526 serialized = GST_QUERY_IS_SERIALIZED (query);
3528 GST_OBJECT_LOCK (pad);
3529 if (GST_PAD_IS_SRC (pad) && serialized) {
3530 /* all serialized queries on the srcpad trigger push of
3532 if (!check_sticky (pad) == GST_FLOW_OK)
3536 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3537 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3538 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3540 peerpad = GST_PAD_PEER (pad);
3541 if (G_UNLIKELY (peerpad == NULL))
3544 gst_object_ref (peerpad);
3545 GST_OBJECT_UNLOCK (pad);
3547 res = gst_pad_query (peerpad, query);
3549 gst_object_unref (peerpad);
3554 GST_OBJECT_LOCK (pad);
3555 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3556 GST_OBJECT_UNLOCK (pad);
3563 g_warning ("pad %s:%s query %s in wrong direction",
3564 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3569 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3574 GST_WARNING_OBJECT (pad, "could not send sticky events");
3575 GST_OBJECT_UNLOCK (pad);
3580 GST_INFO_OBJECT (pad, "pad has no peer");
3581 GST_OBJECT_UNLOCK (pad);
3586 GST_DEBUG_OBJECT (pad, "query failed");
3591 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3592 GST_OBJECT_UNLOCK (pad);
3594 /* if a probe dropped, we don't sent it further but assume that the probe
3595 * answered the query and return TRUE */
3596 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3605 /**********************************************************************
3606 * Data passing functions
3609 /* this is the chain function that does not perform the additional argument
3610 * checking for that little extra speed.
3612 static inline GstFlowReturn
3613 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3618 GST_PAD_STREAM_LOCK (pad);
3620 GST_OBJECT_LOCK (pad);
3621 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3624 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3627 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3630 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3632 PROBE_PUSH (pad, type, data, probe_stopped);
3634 parent = GST_OBJECT_PARENT (pad);
3635 GST_OBJECT_UNLOCK (pad);
3637 /* NOTE: we read the chainfunc unlocked.
3638 * we cannot hold the lock for the pad so we might send
3639 * the data to the wrong function. This is not really a
3640 * problem since functions are assigned at creation time
3641 * and don't change that often... */
3642 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3643 GstPadChainFunction chainfunc;
3645 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3648 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3649 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3650 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3652 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3654 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3655 "called chainfunction &%s with buffer %p, returned %s",
3656 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3658 GstPadChainListFunction chainlistfunc;
3660 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3663 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3664 "calling chainlistfunction &%s",
3665 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3667 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3669 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3670 "called chainlistfunction &%s, returned %s",
3671 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3674 GST_PAD_STREAM_UNLOCK (pad);
3681 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3682 "chaining, but pad was flushing");
3683 GST_OBJECT_UNLOCK (pad);
3684 GST_PAD_STREAM_UNLOCK (pad);
3685 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3686 return GST_FLOW_FLUSHING;
3690 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
3691 GST_OBJECT_UNLOCK (pad);
3692 GST_PAD_STREAM_UNLOCK (pad);
3693 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3694 return GST_FLOW_EOS;
3698 g_critical ("chain on pad %s:%s but it was not in push mode",
3699 GST_DEBUG_PAD_NAME (pad));
3700 GST_OBJECT_UNLOCK (pad);
3701 GST_PAD_STREAM_UNLOCK (pad);
3702 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3703 return GST_FLOW_ERROR;
3707 GST_OBJECT_UNLOCK (pad);
3708 GST_PAD_STREAM_UNLOCK (pad);
3709 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3712 case GST_FLOW_CUSTOM_SUCCESS:
3713 GST_DEBUG_OBJECT (pad, "dropped buffer");
3717 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3724 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3725 g_critical ("chain on pad %s:%s but it has no chainfunction",
3726 GST_DEBUG_PAD_NAME (pad));
3727 GST_PAD_STREAM_UNLOCK (pad);
3728 return GST_FLOW_NOT_SUPPORTED;
3734 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3735 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3738 * Chain a buffer to @pad.
3740 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3742 * If the buffer type is not acceptable for @pad (as negotiated with a
3743 * preceeding GST_EVENT_CAPS event), this function returns
3744 * #GST_FLOW_NOT_NEGOTIATED.
3746 * The function proceeds calling the chain function installed on @pad (see
3747 * gst_pad_set_chain_function()) and the return value of that function is
3748 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3751 * In all cases, success or failure, the caller loses its reference to @buffer
3752 * after calling this function.
3754 * Returns: a #GstFlowReturn from the pad.
3759 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3761 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3762 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3763 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3765 return gst_pad_chain_data_unchecked (pad,
3766 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3769 static GstFlowReturn
3770 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3771 GstBufferList * list)
3777 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3779 len = gst_buffer_list_length (list);
3782 for (i = 0; i < len; i++) {
3783 buffer = gst_buffer_list_get (list, i);
3785 gst_pad_chain_data_unchecked (pad,
3786 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3787 gst_buffer_ref (buffer));
3788 if (ret != GST_FLOW_OK)
3791 gst_buffer_list_unref (list);
3797 * gst_pad_chain_list:
3798 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3799 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3802 * Chain a bufferlist to @pad.
3804 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3806 * If @pad was not negotiated properly with a CAPS event, this function
3807 * returns #GST_FLOW_NOT_NEGOTIATED.
3809 * The function proceeds calling the chainlist function installed on @pad (see
3810 * gst_pad_set_chain_list_function()) and the return value of that function is
3811 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3812 * chainlist function.
3814 * In all cases, success or failure, the caller loses its reference to @list
3815 * after calling this function.
3819 * Returns: a #GstFlowReturn from the pad.
3822 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3824 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3825 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3826 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3828 return gst_pad_chain_data_unchecked (pad,
3829 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3832 static GstFlowReturn
3833 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3838 GST_OBJECT_LOCK (pad);
3839 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3842 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3845 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3848 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3851 /* do block probes */
3852 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3854 /* recheck sticky events because the probe might have cause a relink */
3855 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3858 /* do post-blocking probes */
3859 PROBE_PUSH (pad, type, data, probe_stopped);
3861 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3864 /* take ref to peer pad before releasing the lock */
3865 gst_object_ref (peer);
3867 GST_OBJECT_UNLOCK (pad);
3869 ret = gst_pad_chain_data_unchecked (peer, type, data);
3871 gst_object_unref (peer);
3873 GST_OBJECT_LOCK (pad);
3875 if (pad->priv->using == 0) {
3876 /* pad is not active anymore, trigger idle callbacks */
3877 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3878 probe_stopped, ret);
3880 GST_OBJECT_UNLOCK (pad);
3884 /* ERROR recovery here */
3888 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3889 "pushing, but pad was flushing");
3890 GST_OBJECT_UNLOCK (pad);
3891 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3892 return GST_FLOW_FLUSHING;
3896 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
3897 GST_OBJECT_UNLOCK (pad);
3898 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3899 return GST_FLOW_EOS;
3903 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3904 GST_DEBUG_PAD_NAME (pad));
3905 GST_OBJECT_UNLOCK (pad);
3906 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3907 return GST_FLOW_ERROR;
3911 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3912 "error pushing events, return %s", gst_flow_get_name (ret));
3913 GST_OBJECT_UNLOCK (pad);
3914 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3919 GST_OBJECT_UNLOCK (pad);
3920 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3923 case GST_FLOW_CUSTOM_SUCCESS:
3924 GST_DEBUG_OBJECT (pad, "dropped buffer");
3928 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3935 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3936 "pushing, but it was not linked");
3937 GST_OBJECT_UNLOCK (pad);
3938 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3939 return GST_FLOW_NOT_LINKED;
3945 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3946 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3949 * Pushes a buffer to the peer of @pad.
3951 * This function will call installed block probes before triggering any
3952 * installed data probes.
3954 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3955 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3958 * In all cases, success or failure, the caller loses its reference to @buffer
3959 * after calling this function.
3961 * Returns: a #GstFlowReturn from the peer pad.
3966 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3968 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3969 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3970 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3972 return gst_pad_push_data (pad,
3973 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3977 * gst_pad_push_list:
3978 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3979 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3982 * Pushes a buffer list to the peer of @pad.
3984 * This function will call installed block probes before triggering any
3985 * installed data probes.
3987 * The function proceeds calling the chain function on the peer pad and returns
3988 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3989 * be returned. If the peer pad does not have any installed chainlist function
3990 * every group buffer of the list will be merged into a normal #GstBuffer and
3991 * chained via gst_pad_chain().
3993 * In all cases, success or failure, the caller loses its reference to @list
3994 * after calling this function.
3996 * Returns: a #GstFlowReturn from the peer pad.
4001 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4003 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4004 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4005 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4007 return gst_pad_push_data (pad,
4008 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4011 static GstFlowReturn
4012 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4013 GstBuffer ** buffer)
4016 GstPadGetRangeFunction getrangefunc;
4020 GST_PAD_STREAM_LOCK (pad);
4022 GST_OBJECT_LOCK (pad);
4023 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4026 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4029 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4034 /* when one of the probes returns DROPPED, probe_stopped will be called
4035 * and we skip calling the getrange function, res_buf should then contain a
4036 * valid result buffer */
4037 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4038 res_buf, offset, size, probe_stopped);
4040 /* recheck sticky events because the probe might have cause a relink */
4041 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4044 ACQUIRE_PARENT (pad, parent, no_parent);
4045 GST_OBJECT_UNLOCK (pad);
4047 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4050 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4051 "calling getrangefunc %s, offset %"
4052 G_GUINT64_FORMAT ", size %u",
4053 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4055 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4057 RELEASE_PARENT (parent);
4059 if (G_UNLIKELY (ret != GST_FLOW_OK))
4060 goto get_range_failed;
4062 /* can only fire the signal if we have a valid buffer */
4063 GST_OBJECT_LOCK (pad);
4065 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4066 res_buf, offset, size, probe_stopped_unref);
4067 GST_OBJECT_UNLOCK (pad);
4069 GST_PAD_STREAM_UNLOCK (pad);
4078 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4079 "getrange, but pad was flushing");
4080 GST_OBJECT_UNLOCK (pad);
4081 GST_PAD_STREAM_UNLOCK (pad);
4082 return GST_FLOW_FLUSHING;
4086 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4087 GST_DEBUG_PAD_NAME (pad));
4088 GST_OBJECT_UNLOCK (pad);
4089 GST_PAD_STREAM_UNLOCK (pad);
4090 return GST_FLOW_ERROR;
4094 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4095 GST_OBJECT_UNLOCK (pad);
4096 GST_PAD_STREAM_UNLOCK (pad);
4101 GST_DEBUG_OBJECT (pad, "no parent");
4102 GST_OBJECT_UNLOCK (pad);
4103 GST_PAD_STREAM_UNLOCK (pad);
4104 return GST_FLOW_FLUSHING;
4108 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4109 GST_DEBUG_PAD_NAME (pad));
4110 RELEASE_PARENT (parent);
4111 GST_PAD_STREAM_UNLOCK (pad);
4112 return GST_FLOW_NOT_SUPPORTED;
4116 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4117 "probe returned %s", gst_flow_get_name (ret));
4118 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4120 /* the probe filled the buffer and asks us to not call the getrange
4121 * anymore, we continue with the post probes then. */
4122 GST_DEBUG_OBJECT (pad, "handled buffer");
4126 /* no buffer, we are EOS */
4127 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4131 GST_OBJECT_UNLOCK (pad);
4132 GST_PAD_STREAM_UNLOCK (pad);
4136 probe_stopped_unref:
4138 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4139 "probe returned %s", gst_flow_get_name (ret));
4140 /* if we drop here, it signals EOS */
4141 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4143 GST_OBJECT_UNLOCK (pad);
4144 GST_PAD_STREAM_UNLOCK (pad);
4145 if (*buffer == NULL)
4146 gst_buffer_unref (res_buf);
4151 GST_PAD_STREAM_UNLOCK (pad);
4152 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4153 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4154 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4160 * gst_pad_get_range:
4161 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4162 * @offset: The start offset of the buffer
4163 * @size: The length of the buffer
4164 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4165 * returns #GST_FLOW_ERROR if %NULL.
4167 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4168 * immediately and @buffer is %NULL.
4170 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4171 * description of a getrange function. If @pad has no getrange function
4172 * installed (see gst_pad_set_getrange_function()) this function returns
4173 * #GST_FLOW_NOT_SUPPORTED.
4175 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4176 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4177 * must be freed with gst_buffer_unref() after usage.
4179 * When @buffer points to a variable that points to a valid #GstBuffer, the
4180 * buffer will be filled with the result data when this function returns
4181 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4182 * @size bytes will be filled in the result buffer and its size will be updated
4185 * Note that less than @size bytes can be returned in @buffer when, for example,
4186 * an EOS condition is near or when @buffer is not large enough to hold @size
4187 * bytes. The caller should check the result buffer size to get the result size.
4189 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4190 * will be unchanged.
4192 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4194 * Returns: a #GstFlowReturn from the pad.
4199 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4200 GstBuffer ** buffer)
4202 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4203 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4204 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4205 g_return_val_if_fail (*buffer == NULL
4206 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4208 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4212 * gst_pad_pull_range:
4213 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4214 * @offset: The start offset of the buffer
4215 * @size: The length of the buffer
4216 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4217 * GST_FLOW_ERROR if %NULL.
4219 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4221 * This function will first trigger the pad block signal if it was
4224 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4225 * function returns the result of gst_pad_get_range() on the peer pad.
4226 * See gst_pad_get_range() for a list of return values and for the
4227 * semantics of the arguments of this function.
4229 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4230 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4231 * must be freed with gst_buffer_unref() after usage. When this function
4232 * returns any other result value, @buffer will still point to NULL.
4234 * When @buffer points to a variable that points to a valid #GstBuffer, the
4235 * buffer will be filled with the result data when this function returns
4236 * #GST_FLOW_OK. When this function returns any other result value,
4237 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4238 * @size bytes will be filled in the result buffer and its size will be updated
4241 * Note that less than @size bytes can be returned in @buffer when, for example,
4242 * an EOS condition is near or when @buffer is not large enough to hold @size
4243 * bytes. The caller should check the result buffer size to get the result size.
4245 * Returns: a #GstFlowReturn from the peer pad.
4250 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4251 GstBuffer ** buffer)
4257 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4258 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4259 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4260 g_return_val_if_fail (*buffer == NULL
4261 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4263 GST_OBJECT_LOCK (pad);
4264 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4267 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4272 /* when one of the probes returns DROPPED, probe_stopped will be
4273 * called and we skip calling the peer getrange function. *buffer should then
4274 * contain a valid buffer */
4275 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4276 res_buf, offset, size, probe_stopped);
4278 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4281 gst_object_ref (peer);
4283 GST_OBJECT_UNLOCK (pad);
4285 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4287 gst_object_unref (peer);
4289 GST_OBJECT_LOCK (pad);
4291 if (pad->priv->using == 0) {
4292 /* pad is not active anymore, trigger idle callbacks */
4293 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4294 probe_stopped_unref, ret);
4297 if (G_UNLIKELY (ret != GST_FLOW_OK))
4298 goto pull_range_failed;
4301 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4302 res_buf, offset, size, probe_stopped_unref);
4304 GST_OBJECT_UNLOCK (pad);
4310 /* ERROR recovery here */
4313 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4314 "pullrange, but pad was flushing");
4315 GST_OBJECT_UNLOCK (pad);
4316 return GST_FLOW_FLUSHING;
4320 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4321 GST_DEBUG_PAD_NAME (pad));
4322 GST_OBJECT_UNLOCK (pad);
4323 return GST_FLOW_ERROR;
4327 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4328 gst_flow_get_name (ret));
4329 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4331 /* the probe filled the buffer and asks us to not forward to the peer
4332 * anymore, we continue with the post probes then */
4333 GST_DEBUG_OBJECT (pad, "handled buffer");
4337 /* no buffer, we are EOS then */
4338 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4342 GST_OBJECT_UNLOCK (pad);
4347 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4348 "pulling range, but it was not linked");
4349 GST_OBJECT_UNLOCK (pad);
4350 return GST_FLOW_NOT_LINKED;
4354 GST_OBJECT_UNLOCK (pad);
4355 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4356 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4357 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4360 probe_stopped_unref:
4362 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4363 "post probe returned %s", gst_flow_get_name (ret));
4364 GST_OBJECT_UNLOCK (pad);
4365 /* if we drop here, it signals EOS */
4366 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4368 if (*buffer == NULL)
4369 gst_buffer_unref (res_buf);
4374 /* must be called with pad object lock */
4376 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4381 gboolean res = FALSE;
4382 const gchar *name = NULL;
4384 type = GST_EVENT_TYPE (event);
4385 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4386 name = gst_structure_get_name (gst_event_get_structure (event));
4388 events = pad->priv->events;
4391 for (i = 0; i < len; i++) {
4392 PadEvent *ev = &g_array_index (events, PadEvent, i);
4394 if (ev->event == NULL)
4397 if (type == GST_EVENT_TYPE (ev->event)) {
4398 /* matching types, check matching name if needed */
4399 if (name && !gst_event_has_name (ev->event, name))
4403 if ((res = gst_event_replace (&ev->event, event)))
4404 ev->received = FALSE;
4410 ev.event = gst_event_ref (event);
4411 ev.received = FALSE;
4412 g_array_append_val (events, ev);
4417 pad->priv->events_cookie++;
4418 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4420 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4422 switch (GST_EVENT_TYPE (event)) {
4423 case GST_EVENT_CAPS:
4424 GST_OBJECT_UNLOCK (pad);
4426 GST_DEBUG_OBJECT (pad, "notify caps");
4427 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4429 GST_OBJECT_LOCK (pad);
4438 /* should be called with pad LOCK */
4439 static GstFlowReturn
4440 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4441 GstPadProbeType type)
4445 GstEventType event_type;
4447 /* Two checks to be made:
4448 * . (un)set the FLUSHING flag for flushing events,
4449 * . handle pad blocking */
4450 event_type = GST_EVENT_TYPE (event);
4451 switch (event_type) {
4452 case GST_EVENT_FLUSH_START:
4453 GST_PAD_SET_FLUSHING (pad);
4455 GST_PAD_BLOCK_BROADCAST (pad);
4456 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4458 case GST_EVENT_FLUSH_STOP:
4459 GST_PAD_UNSET_FLUSHING (pad);
4461 /* Remove sticky EOS events */
4462 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4463 remove_event_by_type (pad, GST_EVENT_EOS);
4464 remove_event_by_type (pad, GST_EVENT_SEGMENT);
4465 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4467 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4471 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4474 /* No need to check for EOS here as either the caller (gst_pad_push_event())
4475 * checked already or this is called as part of pushing sticky events,
4476 * in which case we still want to forward the EOS event downstream.
4479 switch (GST_EVENT_TYPE (event)) {
4480 case GST_EVENT_SEGMENT:
4481 /* pass the adjusted segment event on. We need to do this even if
4482 * there is no peer pad because of the probes. */
4483 event = apply_pad_offset (pad, event);
4485 case GST_EVENT_RECONFIGURE:
4486 if (GST_PAD_IS_SINK (pad))
4487 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4492 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4493 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4498 /* send probes after modifying the events above */
4499 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4501 /* now check the peer pad */
4502 peerpad = GST_PAD_PEER (pad);
4503 if (peerpad == NULL)
4506 gst_object_ref (peerpad);
4508 GST_OBJECT_UNLOCK (pad);
4510 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4511 event, GST_EVENT_TYPE_NAME (event), peerpad);
4513 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4515 /* Note: we gave away ownership of the event at this point but we can still
4516 * print the old pointer */
4517 GST_LOG_OBJECT (pad, "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s",
4518 event, peerpad, gst_flow_get_name (ret));
4520 gst_object_unref (peerpad);
4522 GST_OBJECT_LOCK (pad);
4524 if (pad->priv->using == 0) {
4525 /* pad is not active anymore, trigger idle callbacks */
4526 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4527 idle_probe_stopped, ret);
4531 /* ERROR handling */
4534 GST_DEBUG_OBJECT (pad, "We're flushing");
4535 gst_event_unref (event);
4536 return GST_FLOW_FLUSHING;
4540 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4541 gst_event_unref (event);
4544 case GST_FLOW_CUSTOM_SUCCESS:
4545 GST_DEBUG_OBJECT (pad, "dropped event");
4548 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4555 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4556 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4557 gst_event_unref (event);
4559 /* unlinked pads should not influence latency configuration */
4560 if (event_type == GST_EVENT_LATENCY)
4563 return GST_FLOW_NOT_LINKED;
4567 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4573 * gst_pad_push_event:
4574 * @pad: a #GstPad to push the event to.
4575 * @event: (transfer full): the #GstEvent to send to the pad.
4577 * Sends the event to the peer of the given pad. This function is
4578 * mainly used by elements to send events to their peer
4581 * This function takes owership of the provided event so you should
4582 * gst_event_ref() it if you want to reuse the event after this call.
4584 * Returns: TRUE if the event was handled.
4589 gst_pad_push_event (GstPad * pad, GstEvent * event)
4591 gboolean res = FALSE;
4592 GstPadProbeType type;
4593 gboolean sticky, serialized;
4595 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4596 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4598 if (GST_PAD_IS_SRC (pad)) {
4599 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4600 goto wrong_direction;
4601 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4602 } else if (GST_PAD_IS_SINK (pad)) {
4603 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4604 goto wrong_direction;
4605 /* events pushed on sinkpad never are sticky */
4606 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4608 goto unknown_direction;
4610 GST_OBJECT_LOCK (pad);
4611 sticky = GST_EVENT_IS_STICKY (event);
4612 serialized = GST_EVENT_IS_SERIALIZED (event);
4615 /* can't store on flushing pads */
4616 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4619 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4622 /* srcpad sticky events are stored immediately, the received flag is set
4623 * to FALSE and will be set to TRUE when we can successfully push the
4624 * event to the peer pad */
4625 if (gst_pad_store_sticky_event (pad, event)) {
4626 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4628 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
4629 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4631 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4632 /* all serialized or sticky events on the srcpad trigger push of
4634 res = (check_sticky (pad) == GST_FLOW_OK);
4639 /* other events are pushed right away */
4640 ret = gst_pad_push_event_unchecked (pad, event, type);
4641 /* dropped events by a probe are not an error */
4642 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
4644 /* Errors in sticky event pushing are no problem and ignored here
4645 * as they will cause more meaningful errors during data flow.
4646 * For EOS events, that are not followed by data flow, we still
4647 * return FALSE here though.
4649 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
4651 gst_event_unref (event);
4653 GST_OBJECT_UNLOCK (pad);
4657 /* ERROR handling */
4660 g_warning ("pad %s:%s pushing %s event in wrong direction",
4661 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4662 gst_event_unref (event);
4667 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4668 gst_event_unref (event);
4673 GST_DEBUG_OBJECT (pad, "We're flushing");
4674 GST_OBJECT_UNLOCK (pad);
4675 gst_event_unref (event);
4680 GST_DEBUG_OBJECT (pad, "We're EOS");
4681 GST_OBJECT_UNLOCK (pad);
4682 gst_event_unref (event);
4687 /* Check if we can call the event function with the given event */
4688 static GstFlowReturn
4689 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4693 switch (GST_EVENT_TYPE (event)) {
4694 case GST_EVENT_CAPS:
4696 /* backwards compatibility mode for caps */
4697 gst_event_parse_caps (event, &caps);
4699 if (!gst_pad_query_accept_caps (pad, caps))
4711 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4712 "caps %" GST_PTR_FORMAT " not accepted", caps);
4713 return GST_FLOW_NOT_NEGOTIATED;
4717 static GstFlowReturn
4718 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4719 GstPadProbeType type)
4722 GstEventType event_type;
4723 gboolean serialized, need_unlock = FALSE, sticky;
4724 GstPadEventFunction eventfunc;
4727 GST_OBJECT_LOCK (pad);
4728 if (GST_PAD_IS_SINK (pad))
4729 serialized = GST_EVENT_IS_SERIALIZED (event);
4732 sticky = GST_EVENT_IS_STICKY (event);
4733 event_type = GST_EVENT_TYPE (event);
4734 switch (event_type) {
4735 case GST_EVENT_FLUSH_START:
4736 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4737 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4739 /* can't even accept a flush begin event when flushing */
4740 if (GST_PAD_IS_FLUSHING (pad))
4743 GST_PAD_SET_FLUSHING (pad);
4744 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4746 case GST_EVENT_FLUSH_STOP:
4747 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4748 GST_PAD_UNSET_FLUSHING (pad);
4749 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4751 /* Remove pending EOS events */
4752 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
4753 remove_event_by_type (pad, GST_EVENT_EOS);
4754 remove_event_by_type (pad, GST_EVENT_SEGMENT);
4755 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4757 GST_OBJECT_UNLOCK (pad);
4758 /* grab stream lock */
4759 GST_PAD_STREAM_LOCK (pad);
4761 GST_OBJECT_LOCK (pad);
4762 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4765 case GST_EVENT_RECONFIGURE:
4766 if (GST_PAD_IS_SRC (pad))
4767 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4769 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4770 "have event type %" GST_PTR_FORMAT, event);
4772 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4776 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4779 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4780 GST_OBJECT_UNLOCK (pad);
4781 GST_PAD_STREAM_LOCK (pad);
4783 GST_OBJECT_LOCK (pad);
4784 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4787 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4791 switch (GST_EVENT_TYPE (event)) {
4792 case GST_EVENT_SEGMENT:
4793 event = apply_pad_offset (pad, event);
4799 /* now do the probe */
4801 type | GST_PAD_PROBE_TYPE_PUSH |
4802 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4804 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4808 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4811 ACQUIRE_PARENT (pad, parent, no_parent);
4812 GST_OBJECT_UNLOCK (pad);
4814 ret = pre_eventfunc_check (pad, event);
4815 if (G_UNLIKELY (ret != GST_FLOW_OK))
4816 goto precheck_failed;
4819 gst_event_ref (event);
4821 if (eventfunc (pad, parent, event)) {
4824 /* something went wrong */
4825 switch (event_type) {
4826 case GST_EVENT_CAPS:
4827 ret = GST_FLOW_NOT_NEGOTIATED;
4830 ret = GST_FLOW_ERROR;
4834 RELEASE_PARENT (parent);
4836 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4839 if (ret == GST_FLOW_OK) {
4840 GST_OBJECT_LOCK (pad);
4841 /* can't store on flushing pads */
4842 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4845 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4848 /* after the event function accepted the event, we can store the sticky
4849 * event on the pad */
4850 if (gst_pad_store_sticky_event (pad, event)) {
4851 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4853 if (event_type == GST_EVENT_EOS)
4854 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4856 GST_OBJECT_UNLOCK (pad);
4858 gst_event_unref (event);
4862 GST_PAD_STREAM_UNLOCK (pad);
4866 /* ERROR handling */
4869 GST_OBJECT_UNLOCK (pad);
4871 GST_PAD_STREAM_UNLOCK (pad);
4872 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4873 "Received event on flushing pad. Discarding");
4874 gst_event_unref (event);
4875 return GST_FLOW_FLUSHING;
4879 GST_OBJECT_UNLOCK (pad);
4881 GST_PAD_STREAM_UNLOCK (pad);
4882 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4883 "Received event on EOS pad. Discarding");
4884 gst_event_unref (event);
4885 return GST_FLOW_EOS;
4889 GST_OBJECT_UNLOCK (pad);
4891 GST_PAD_STREAM_UNLOCK (pad);
4892 gst_event_unref (event);
4895 case GST_FLOW_CUSTOM_SUCCESS:
4896 GST_DEBUG_OBJECT (pad, "dropped event");
4900 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4907 g_warning ("pad %s:%s has no event handler, file a bug.",
4908 GST_DEBUG_PAD_NAME (pad));
4909 GST_OBJECT_UNLOCK (pad);
4911 GST_PAD_STREAM_UNLOCK (pad);
4912 gst_event_unref (event);
4913 return GST_FLOW_NOT_SUPPORTED;
4917 GST_DEBUG_OBJECT (pad, "no parent");
4918 GST_OBJECT_UNLOCK (pad);
4920 GST_PAD_STREAM_UNLOCK (pad);
4921 gst_event_unref (event);
4922 return GST_FLOW_FLUSHING;
4926 GST_DEBUG_OBJECT (pad, "pre event check failed");
4927 RELEASE_PARENT (parent);
4929 GST_PAD_STREAM_UNLOCK (pad);
4930 gst_event_unref (event);
4936 * gst_pad_send_event:
4937 * @pad: a #GstPad to send the event to.
4938 * @event: (transfer full): the #GstEvent to send to the pad.
4940 * Sends the event to the pad. This function can be used
4941 * by applications to send events in the pipeline.
4943 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4944 * sink pad, @event should be a downstream event. For example, you would not
4945 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4946 * Furthermore, some downstream events have to be serialized with data flow,
4947 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4948 * the event needs to be serialized with data flow, this function will take the
4949 * pad's stream lock while calling its event function.
4951 * To find out whether an event type is upstream, downstream, or downstream and
4952 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4953 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4954 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4955 * plugin doesn't need to bother itself with this information; the core handles
4956 * all necessary locks and checks.
4958 * This function takes owership of the provided event so you should
4959 * gst_event_ref() it if you want to reuse the event after this call.
4961 * Returns: TRUE if the event was handled.
4964 gst_pad_send_event (GstPad * pad, GstEvent * event)
4967 GstPadProbeType type;
4969 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4970 g_return_val_if_fail (event != NULL, FALSE);
4972 if (GST_PAD_IS_SINK (pad)) {
4973 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4974 goto wrong_direction;
4975 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4976 } else if (GST_PAD_IS_SRC (pad)) {
4977 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4978 goto wrong_direction;
4979 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4981 goto unknown_direction;
4983 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4990 /* ERROR handling */
4993 g_warning ("pad %s:%s sending %s event in wrong direction",
4994 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4995 gst_event_unref (event);
5000 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5001 gst_event_unref (event);
5007 * gst_pad_set_element_private:
5008 * @pad: the #GstPad to set the private data of.
5009 * @priv: The private data to attach to the pad.
5011 * Set the given private data gpointer on the pad.
5012 * This function can only be used by the element that owns the pad.
5013 * No locking is performed in this function.
5016 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5018 pad->element_private = priv;
5022 * gst_pad_get_element_private:
5023 * @pad: the #GstPad to get the private data of.
5025 * Gets the private data of a pad.
5026 * No locking is performed in this function.
5028 * Returns: (transfer none): a #gpointer to the private data.
5031 gst_pad_get_element_private (GstPad * pad)
5033 return pad->element_private;
5037 * gst_pad_get_sticky_event:
5038 * @pad: the #GstPad to get the event from.
5039 * @event_type: the #GstEventType that should be retrieved.
5040 * @idx: the index of the event
5042 * Returns a new reference of the sticky event of type @event_type
5045 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
5046 * event of @event_type was on @pad. Unref after usage.
5049 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5051 GstEvent *event = NULL;
5054 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5055 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5057 GST_OBJECT_LOCK (pad);
5058 ev = find_event_by_type (pad, event_type, idx);
5059 if (ev && (event = ev->event))
5060 gst_event_ref (event);
5061 GST_OBJECT_UNLOCK (pad);
5068 GstPadStickyEventsForeachFunction func;
5073 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5075 ForeachDispatch *data = user_data;
5078 GST_OBJECT_UNLOCK (pad);
5080 ret = data->func (pad, &ev->event, data->user_data);
5082 GST_OBJECT_LOCK (pad);
5088 * gst_pad_sticky_events_foreach:
5089 * @pad: the #GstPad that should be used for iteration.
5090 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5091 * should be called for every event.
5092 * @user_data: (closure): the optional user data.
5094 * Iterates all sticky events on @pad and calls @foreach_func for every
5095 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5098 gst_pad_sticky_events_foreach (GstPad * pad,
5099 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5101 ForeachDispatch data;
5103 g_return_if_fail (GST_IS_PAD (pad));
5104 g_return_if_fail (foreach_func != NULL);
5106 data.func = foreach_func;
5107 data.user_data = user_data;
5109 GST_OBJECT_LOCK (pad);
5110 events_foreach (pad, foreach_dispatch_function, &data);
5111 GST_OBJECT_UNLOCK (pad);
5115 do_stream_status (GstPad * pad, GstStreamStatusType type,
5116 GThread * thread, GstTask * task)
5120 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5122 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5123 if (GST_IS_ELEMENT (parent)) {
5124 GstMessage *message;
5125 GValue value = { 0 };
5127 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5128 gchar *tname, *ename, *pname;
5130 /* create a good task name */
5131 ename = gst_element_get_name (parent);
5132 pname = gst_pad_get_name (pad);
5133 tname = g_strdup_printf ("%s:%s", ename, pname);
5137 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5141 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5144 g_value_init (&value, GST_TYPE_TASK);
5145 g_value_set_object (&value, task);
5146 gst_message_set_stream_status_object (message, &value);
5147 g_value_unset (&value);
5149 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5150 gst_element_post_message (parent, message);
5152 gst_object_unref (parent);
5157 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5159 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5164 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5166 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5171 * gst_pad_start_task:
5172 * @pad: the #GstPad to start the task of
5173 * @func: the task function to call
5174 * @user_data: user data passed to the task function
5175 * @notify: called when @user_data is no longer referenced
5177 * Starts a task that repeatedly calls @func with @user_data. This function
5178 * is mostly used in pad activation functions to start the dataflow.
5179 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5180 * before @func is called.
5182 * Returns: a %TRUE if the task could be started.
5185 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5186 GDestroyNotify notify)
5191 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5192 g_return_val_if_fail (func != NULL, FALSE);
5194 GST_DEBUG_OBJECT (pad, "start task");
5196 GST_OBJECT_LOCK (pad);
5197 task = GST_PAD_TASK (pad);
5199 task = gst_task_new (func, user_data, notify);
5200 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5201 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5202 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5203 GST_INFO_OBJECT (pad, "created task %p", task);
5204 GST_PAD_TASK (pad) = task;
5205 gst_object_ref (task);
5206 /* release lock to post the message */
5207 GST_OBJECT_UNLOCK (pad);
5209 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5211 gst_object_unref (task);
5213 GST_OBJECT_LOCK (pad);
5214 /* nobody else is supposed to have changed the pad now */
5215 if (GST_PAD_TASK (pad) != task)
5216 goto concurrent_stop;
5218 res = gst_task_set_state (task, GST_TASK_STARTED);
5219 GST_OBJECT_UNLOCK (pad);
5226 GST_OBJECT_UNLOCK (pad);
5232 * gst_pad_pause_task:
5233 * @pad: the #GstPad to pause the task of
5235 * Pause the task of @pad. This function will also wait until the
5236 * function executed by the task is finished if this function is not
5237 * called from the task function.
5239 * Returns: a TRUE if the task could be paused or FALSE when the pad
5243 gst_pad_pause_task (GstPad * pad)
5248 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5250 GST_DEBUG_OBJECT (pad, "pause task");
5252 GST_OBJECT_LOCK (pad);
5253 task = GST_PAD_TASK (pad);
5256 res = gst_task_set_state (task, GST_TASK_PAUSED);
5257 GST_OBJECT_UNLOCK (pad);
5259 /* wait for task function to finish, this lock is recursive so it does nothing
5260 * when the pause is called from the task itself */
5261 GST_PAD_STREAM_LOCK (pad);
5262 GST_PAD_STREAM_UNLOCK (pad);
5268 GST_DEBUG_OBJECT (pad, "pad has no task");
5269 GST_OBJECT_UNLOCK (pad);
5275 * gst_pad_stop_task:
5276 * @pad: the #GstPad to stop the task of
5278 * Stop the task of @pad. This function will also make sure that the
5279 * function executed by the task will effectively stop if not called
5280 * from the GstTaskFunction.
5282 * This function will deadlock if called from the GstTaskFunction of
5283 * the task. Use gst_task_pause() instead.
5285 * Regardless of whether the pad has a task, the stream lock is acquired and
5286 * released so as to ensure that streaming through this pad has finished.
5288 * Returns: a TRUE if the task could be stopped or FALSE on error.
5291 gst_pad_stop_task (GstPad * pad)
5296 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5298 GST_DEBUG_OBJECT (pad, "stop task");
5300 GST_OBJECT_LOCK (pad);
5301 task = GST_PAD_TASK (pad);
5304 GST_PAD_TASK (pad) = NULL;
5305 res = gst_task_set_state (task, GST_TASK_STOPPED);
5306 GST_OBJECT_UNLOCK (pad);
5308 GST_PAD_STREAM_LOCK (pad);
5309 GST_PAD_STREAM_UNLOCK (pad);
5311 if (!gst_task_join (task))
5314 gst_object_unref (task);
5320 GST_DEBUG_OBJECT (pad, "no task");
5321 GST_OBJECT_UNLOCK (pad);
5323 GST_PAD_STREAM_LOCK (pad);
5324 GST_PAD_STREAM_UNLOCK (pad);
5326 /* this is not an error */
5331 /* this is bad, possibly the application tried to join the task from
5332 * the task's thread. We install the task again so that it will be stopped
5333 * again from the right thread next time hopefully. */
5334 GST_OBJECT_LOCK (pad);
5335 GST_DEBUG_OBJECT (pad, "join failed");
5336 /* we can only install this task if there was no other task */
5337 if (GST_PAD_TASK (pad) == NULL)
5338 GST_PAD_TASK (pad) = task;
5339 GST_OBJECT_UNLOCK (pad);
5346 * gst_pad_probe_info_get_event:
5347 * @info: a #GstPadProbeInfo
5349 * Returns: (transfer none): The #GstEvent from the probe
5353 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5355 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5356 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5358 return GST_PAD_PROBE_INFO_EVENT (info);
5363 * gst_pad_probe_info_get_query:
5364 * @info: a #GstPadProbeInfo
5366 * Returns: (transfer none): The #GstQuery from the probe
5370 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5372 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5373 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5375 return GST_PAD_PROBE_INFO_QUERY (info);
5379 * gst_pad_probe_info_get_buffer:
5380 * @info: a #GstPadProbeInfo
5382 * Returns: (transfer none): The #GstBuffer from the probe
5386 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5388 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5390 return GST_PAD_PROBE_INFO_BUFFER (info);
5394 * gst_pad_probe_info_get_bufferlist:
5395 * @info: a #GstPadProbeInfo
5397 * Returns: (transfer none): The #GstBufferlist from the probe
5401 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5403 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5405 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);