2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
30 * After two pads are retrieved from an element with gst_element_get_pad(),
31 * the pads can be link with gst_pad_link(). (For quick links,
32 * you can also use gst_element_link(), which will make the obvious
33 * link for you if it's straightforward.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
38 * Pads have #GstCaps attached to it to describe the media type they are
39 * capable of dealing with. gst_pad_query_caps() and gst_pad_set_caps() are
40 * used to manipulate the caps of the pads.
41 * Pads created from a pad template cannot set capabilities that are
42 * incompatible with the pad template capabilities.
44 * Pads without pad templates can be created with gst_pad_new(),
45 * which takes a direction and a name as an argument. If the name is NULL,
46 * then a guaranteed unique name will be assigned to it.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
73 #include "glib-compat-private.h"
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
78 /* Pad signals and args */
96 #define GST_PAD_GET_PRIVATE(obj) \
97 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
99 /* we have a pending and an active event on the pad. On source pads only the
100 * active event is used. On sinkpads, events are copied to the pending entry and
101 * moved to the active event when the eventfunc returned TRUE. */
108 struct _GstPadPrivate
110 PadEvent events[GST_EVENT_MAX_STICKY];
113 guint probe_list_cookie;
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
128 GstPadProbeInfo *info;
135 static void gst_pad_dispose (GObject * object);
136 static void gst_pad_finalize (GObject * object);
137 static void gst_pad_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec);
139 static void gst_pad_get_property (GObject * object, guint prop_id,
140 GValue * value, GParamSpec * pspec);
142 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
143 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
144 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
145 GstObject * parent, GstBufferList * list);
147 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
149 static GParamSpec *pspec_caps = NULL;
151 /* quarks for probe signals */
152 static GQuark buffer_quark;
153 static GQuark buffer_list_quark;
154 static GQuark event_quark;
163 static GstFlowQuarks flow_quarks[] = {
164 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
165 {GST_FLOW_RESEND, "resend", 0},
166 {GST_FLOW_OK, "ok", 0},
167 {GST_FLOW_NOT_LINKED, "not-linked", 0},
168 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
169 {GST_FLOW_EOS, "eos", 0},
170 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
171 {GST_FLOW_ERROR, "error", 0},
172 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
173 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
178 * @ret: a #GstFlowReturn to get the name of.
180 * Gets a string representing the given flow return.
182 * Returns: a static string with the name of the flow return.
185 gst_flow_get_name (GstFlowReturn ret)
189 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
191 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
192 if (ret == flow_quarks[i].ret)
193 return flow_quarks[i].name;
200 * @ret: a #GstFlowReturn to get the quark of.
202 * Get the unique quark for the given GstFlowReturn.
204 * Returns: the quark associated with the flow return or 0 if an
205 * invalid return was specified.
208 gst_flow_to_quark (GstFlowReturn ret)
212 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
214 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
215 if (ret == flow_quarks[i].ret)
216 return flow_quarks[i].quark;
225 buffer_quark = g_quark_from_static_string ("buffer"); \
226 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
227 event_quark = g_quark_from_static_string ("event"); \
229 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
230 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
233 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
234 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
237 #define gst_pad_parent_class parent_class
238 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
241 gst_pad_class_init (GstPadClass * klass)
243 GObjectClass *gobject_class;
244 GstObjectClass *gstobject_class;
246 gobject_class = G_OBJECT_CLASS (klass);
247 gstobject_class = GST_OBJECT_CLASS (klass);
249 g_type_class_add_private (klass, sizeof (GstPadPrivate));
251 gobject_class->dispose = gst_pad_dispose;
252 gobject_class->finalize = gst_pad_finalize;
253 gobject_class->set_property = gst_pad_set_property;
254 gobject_class->get_property = gst_pad_get_property;
258 * @pad: the pad that emitted the signal
259 * @peer: the peer pad that has been connected
261 * Signals that a pad has been linked to the peer pad.
263 gst_pad_signals[PAD_LINKED] =
264 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
265 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
266 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
269 * @pad: the pad that emitted the signal
270 * @peer: the peer pad that has been disconnected
272 * Signals that a pad has been unlinked from the peer pad.
274 gst_pad_signals[PAD_UNLINKED] =
275 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
276 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
277 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
279 pspec_caps = g_param_spec_boxed ("caps", "Caps",
280 "The capabilities of the pad", GST_TYPE_CAPS,
281 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
282 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
284 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
285 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
286 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
287 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
289 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
290 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
291 g_param_spec_object ("template", "Template",
292 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
293 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 gstobject_class->path_string_separator = ".";
297 /* Register common function pointer descriptions */
298 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
299 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
300 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
301 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
306 gst_pad_init (GstPad * pad)
308 pad->priv = GST_PAD_GET_PRIVATE (pad);
310 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
312 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
313 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
314 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
315 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
316 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
318 GST_PAD_SET_FLUSHING (pad);
320 g_static_rec_mutex_init (&pad->stream_rec_lock);
322 pad->block_cond = g_cond_new ();
324 g_hook_list_init (&pad->probes, sizeof (GstProbe));
328 clear_event (GstPad * pad, guint idx)
330 gst_event_replace (&pad->priv->events[idx].event, NULL);
331 pad->priv->events[idx].received = FALSE;
334 /* called when setting the pad inactive. It removes all sticky events from
337 clear_events (GstPad * pad)
341 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
342 clear_event (pad, i);
344 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
348 reschedule_event (GstPad * pad, guint idx)
350 if (pad->priv->events[idx].event) {
351 pad->priv->events[idx].received = FALSE;
358 reschedule_events (GstPad * pad)
361 gboolean pending = FALSE;
363 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
364 pending |= reschedule_event (pad, i);
367 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
371 /* should be called with LOCK */
373 apply_pad_offset (GstPad * pad, GstEvent * event)
375 /* check if we need to adjust the segment */
376 if (pad->offset != 0) {
379 /* copy segment values */
380 gst_event_copy_segment (event, &segment);
381 gst_event_unref (event);
383 /* adjust and make a new event with the offset applied */
384 segment.base += pad->offset;
385 event = gst_event_new_segment (&segment);
390 /* should be called with the OBJECT_LOCK */
392 get_pad_caps (GstPad * pad)
394 GstCaps *caps = NULL;
398 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
399 /* we can only use the caps when we have successfully send the caps
400 * event to the event function and is thus in the active entry */
401 if ((event = pad->priv->events[idx].event))
402 gst_event_parse_caps (event, &caps);
408 gst_pad_dispose (GObject * object)
410 GstPad *pad = GST_PAD_CAST (object);
413 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
415 /* unlink the peer pad */
416 if ((peer = gst_pad_get_peer (pad))) {
417 /* window for MT unsafeness, someone else could unlink here
418 * and then we call unlink with wrong pads. The unlink
419 * function would catch this and safely return failed. */
420 if (GST_PAD_IS_SRC (pad))
421 gst_pad_unlink (pad, peer);
423 gst_pad_unlink (peer, pad);
425 gst_object_unref (peer);
428 gst_pad_set_pad_template (pad, NULL);
432 g_hook_list_clear (&pad->probes);
434 G_OBJECT_CLASS (parent_class)->dispose (object);
438 gst_pad_finalize (GObject * object)
440 GstPad *pad = GST_PAD_CAST (object);
443 /* in case the task is still around, clean it up */
444 if ((task = GST_PAD_TASK (pad))) {
445 gst_task_join (task);
446 GST_PAD_TASK (pad) = NULL;
447 gst_object_unref (task);
450 if (pad->activatenotify)
451 pad->activatenotify (pad);
452 if (pad->activatemodenotify)
453 pad->activatemodenotify (pad);
455 pad->linknotify (pad);
456 if (pad->unlinknotify)
457 pad->unlinknotify (pad);
458 if (pad->chainnotify)
459 pad->chainnotify (pad);
460 if (pad->chainlistnotify)
461 pad->chainlistnotify (pad);
462 if (pad->getrangenotify)
463 pad->getrangenotify (pad);
464 if (pad->eventnotify)
465 pad->eventnotify (pad);
466 if (pad->querynotify)
467 pad->querynotify (pad);
468 if (pad->iterintlinknotify)
469 pad->iterintlinknotify (pad);
471 g_static_rec_mutex_free (&pad->stream_rec_lock);
472 g_cond_free (pad->block_cond);
474 G_OBJECT_CLASS (parent_class)->finalize (object);
478 gst_pad_set_property (GObject * object, guint prop_id,
479 const GValue * value, GParamSpec * pspec)
481 g_return_if_fail (GST_IS_PAD (object));
484 case PAD_PROP_DIRECTION:
485 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
487 case PAD_PROP_TEMPLATE:
488 gst_pad_set_pad_template (GST_PAD_CAST (object),
489 (GstPadTemplate *) g_value_get_object (value));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gst_pad_get_property (GObject * object, guint prop_id,
499 GValue * value, GParamSpec * pspec)
501 g_return_if_fail (GST_IS_PAD (object));
505 GST_OBJECT_LOCK (object);
506 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
507 GST_OBJECT_UNLOCK (object);
509 case PAD_PROP_DIRECTION:
510 g_value_set_enum (value, GST_PAD_DIRECTION (object));
512 case PAD_PROP_TEMPLATE:
513 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 * @name: the name of the new pad.
524 * @direction: the #GstPadDirection of the pad.
526 * Creates a new pad with the given name in the given direction.
527 * If name is NULL, a guaranteed unique name (across all pads)
529 * This function makes a copy of the name so you can safely free the name.
531 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
536 gst_pad_new (const gchar * name, GstPadDirection direction)
538 return g_object_new (GST_TYPE_PAD,
539 "name", name, "direction", direction, NULL);
543 * gst_pad_new_from_template:
544 * @templ: the pad template to use
545 * @name: the name of the element
547 * Creates a new pad with the given name from the given template.
548 * If name is NULL, a guaranteed unique name (across all pads)
550 * This function makes a copy of the name so you can safely free the name.
552 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
555 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
557 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
559 return g_object_new (GST_TYPE_PAD,
560 "name", name, "direction", templ->direction, "template", templ, NULL);
564 * gst_pad_new_from_static_template:
565 * @templ: the #GstStaticPadTemplate to use
566 * @name: the name of the element
568 * Creates a new pad with the given name from the given static template.
569 * If name is NULL, a guaranteed unique name (across all pads)
571 * This function makes a copy of the name so you can safely free the name.
573 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
576 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
580 GstPadTemplate *template;
582 template = gst_static_pad_template_get (templ);
583 pad = gst_pad_new_from_template (template, name);
584 gst_object_unref (template);
588 #define ACQUIRE_PARENT(pad, parent, label) \
590 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
591 gst_object_ref (parent); \
592 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
596 #define RELEASE_PARENT(parent) \
598 if (G_LIKELY (parent)) \
599 gst_object_unref (parent); \
603 * gst_pad_get_direction:
604 * @pad: a #GstPad to get the direction of.
606 * Gets the direction of the pad. The direction of the pad is
607 * decided at construction time so this function does not take
610 * Returns: the #GstPadDirection of the pad.
615 gst_pad_get_direction (GstPad * pad)
617 GstPadDirection result;
619 /* PAD_UNKNOWN is a little silly but we need some sort of
620 * error return value */
621 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
623 result = GST_PAD_DIRECTION (pad);
629 gst_pad_activate_default (GstPad * pad, GstObject * parent)
631 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
635 pre_activate (GstPad * pad, GstPadMode new_mode)
638 case GST_PAD_MODE_NONE:
639 GST_OBJECT_LOCK (pad);
640 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
641 GST_PAD_SET_FLUSHING (pad);
642 GST_PAD_MODE (pad) = new_mode;
643 /* unlock blocked pads so element can resume and stop */
644 GST_PAD_BLOCK_BROADCAST (pad);
645 GST_OBJECT_UNLOCK (pad);
647 case GST_PAD_MODE_PUSH:
648 case GST_PAD_MODE_PULL:
649 GST_OBJECT_LOCK (pad);
650 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
651 GST_PAD_UNSET_FLUSHING (pad);
652 GST_PAD_MODE (pad) = new_mode;
653 if (GST_PAD_IS_SINK (pad)) {
655 /* make sure the peer src pad sends us all events */
656 if ((peer = GST_PAD_PEER (pad))) {
657 gst_object_ref (peer);
658 GST_OBJECT_UNLOCK (pad);
660 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
661 GST_DEBUG_PAD_NAME (peer));
663 GST_OBJECT_LOCK (peer);
664 reschedule_events (peer);
665 GST_OBJECT_UNLOCK (peer);
667 gst_object_unref (peer);
669 GST_OBJECT_UNLOCK (pad);
672 GST_OBJECT_UNLOCK (pad);
679 post_activate (GstPad * pad, GstPadMode new_mode)
682 case GST_PAD_MODE_NONE:
683 /* ensures that streaming stops */
684 GST_PAD_STREAM_LOCK (pad);
685 GST_DEBUG_OBJECT (pad, "stopped streaming");
686 GST_OBJECT_LOCK (pad);
688 GST_OBJECT_UNLOCK (pad);
689 GST_PAD_STREAM_UNLOCK (pad);
691 case GST_PAD_MODE_PUSH:
692 case GST_PAD_MODE_PULL:
699 * gst_pad_set_active:
700 * @pad: the #GstPad to activate or deactivate.
701 * @active: whether or not the pad should be active.
703 * Activates or deactivates the given pad.
704 * Normally called from within core state change functions.
706 * If @active, makes sure the pad is active. If it is already active, either in
707 * push or pull mode, just return. Otherwise dispatches to the pad's activate
708 * function to perform the actual activation.
710 * If not @active, checks the pad's current mode and calls
711 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
714 * Returns: #TRUE if the operation was successful.
719 gst_pad_set_active (GstPad * pad, gboolean active)
723 gboolean ret = FALSE;
725 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
727 GST_OBJECT_LOCK (pad);
728 old = GST_PAD_MODE (pad);
729 ACQUIRE_PARENT (pad, parent, no_parent);
730 GST_OBJECT_UNLOCK (pad);
733 if (old == GST_PAD_MODE_NONE) {
734 GST_DEBUG_OBJECT (pad, "activating pad from none");
735 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
737 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
741 if (old == GST_PAD_MODE_NONE) {
742 GST_DEBUG_OBJECT (pad, "pad was inactive");
745 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
746 ret = gst_pad_activate_mode (pad, old, FALSE);
750 RELEASE_PARENT (parent);
752 if (G_UNLIKELY (!ret))
756 GST_OBJECT_LOCK (pad);
757 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
758 GST_OBJECT_UNLOCK (pad);
765 GST_DEBUG_OBJECT (pad, "no parent");
766 GST_OBJECT_UNLOCK (pad);
771 GST_OBJECT_LOCK (pad);
773 g_critical ("Failed to deactivate pad %s:%s, very bad",
774 GST_DEBUG_PAD_NAME (pad));
776 GST_WARNING_OBJECT (pad, "Failed to activate pad");
778 GST_OBJECT_UNLOCK (pad);
784 * gst_pad_activate_mode:
785 * @pad: the #GstPad to activate or deactivate.
786 * @mode: the requested activation mode
787 * @active: whether or not the pad should be active.
789 * Activates or deactivates the given pad in @mode via dispatching to the
790 * pad's activatemodefunc. For use from within pad activation functions only.
792 * If you don't know what this is, you probably don't want to call it.
794 * Returns: TRUE if the operation was successful.
799 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
801 gboolean res = FALSE;
807 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
809 GST_OBJECT_LOCK (pad);
810 old = GST_PAD_MODE (pad);
811 dir = GST_PAD_DIRECTION (pad);
812 ACQUIRE_PARENT (pad, parent, no_parent);
813 GST_OBJECT_UNLOCK (pad);
815 new = active ? mode : GST_PAD_MODE_NONE;
820 if (active && old != mode) {
821 /* pad was activate in the wrong direction, deactivate it
822 * and reactivate it in the requested mode */
823 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
824 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
825 goto deactivate_failed;
829 case GST_PAD_MODE_PULL:
831 if (dir == GST_PAD_SINK) {
832 if ((peer = gst_pad_get_peer (pad))) {
833 GST_DEBUG_OBJECT (pad, "calling peer");
834 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
836 gst_object_unref (peer);
838 /* there is no peer, this is only fatal when we activate. When we
839 * deactivate, we must assume the application has unlinked the peer and
840 * will deactivate it eventually. */
844 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
847 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
848 goto failure; /* Can't activate pull on a src without a
857 pre_activate (pad, new);
859 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
860 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
864 /* can happen for sinks of passthrough elements */
867 post_activate (pad, new);
869 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
870 active ? "activated" : "deactivated", mode);
875 RELEASE_PARENT (parent);
881 GST_DEBUG_OBJECT (pad, "no parent");
882 GST_OBJECT_UNLOCK (pad);
887 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
888 active ? "activated" : "deactivated", mode);
893 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
894 "failed to %s in switch to mode %d from mode %d",
895 (active ? "activate" : "deactivate"), mode, old);
900 GST_OBJECT_LOCK (peer);
901 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
902 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
903 GST_OBJECT_UNLOCK (peer);
904 gst_object_unref (peer);
909 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
915 GST_OBJECT_LOCK (pad);
916 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
917 active ? "activate" : "deactivate", mode);
918 GST_PAD_SET_FLUSHING (pad);
919 GST_PAD_MODE (pad) = old;
920 GST_OBJECT_UNLOCK (pad);
927 * @pad: the #GstPad to query
929 * Query if a pad is active
931 * Returns: TRUE if the pad is active.
936 gst_pad_is_active (GstPad * pad)
938 gboolean result = FALSE;
940 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
942 GST_OBJECT_LOCK (pad);
943 result = GST_PAD_IS_ACTIVE (pad);
944 GST_OBJECT_UNLOCK (pad);
951 * @pad: the #GstPad to add the probe to
952 * @mask: the probe mask
953 * @callback: #GstPadProbeCallback that will be called with notifications of
955 * @user_data: (closure): user data passed to the callback
956 * @destroy_data: #GDestroyNotify for user_data
958 * Be notified of different states of pads. The provided callback is called for
959 * every state that matches @mask.
961 * Returns: an id or 0 on error. The id can be used to remove the probe with
962 * gst_pad_remove_probe().
967 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
968 GstPadProbeCallback callback, gpointer user_data,
969 GDestroyNotify destroy_data)
974 g_return_val_if_fail (GST_IS_PAD (pad), 0);
975 g_return_val_if_fail (mask != 0, 0);
977 GST_OBJECT_LOCK (pad);
979 /* make a new probe */
980 hook = g_hook_alloc (&pad->probes);
982 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
985 /* when no contraints are given for the types, assume all types are
987 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
988 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
989 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
990 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
992 /* store our flags and other fields */
993 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
994 hook->func = callback;
995 hook->data = user_data;
996 hook->destroy = destroy_data;
997 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1000 g_hook_prepend (&pad->probes, hook);
1002 /* incremenent cookie so that the new hook get's called */
1003 pad->priv->probe_list_cookie++;
1005 /* get the id of the hook, we return this and it can be used to remove the
1007 res = hook->hook_id;
1009 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1011 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1012 /* we have a block probe */
1014 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1015 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1016 "now %d blocking probes", pad->num_blocked);
1019 /* call the callback if we need to be called for idle callbacks */
1020 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1021 if (pad->priv->using > 0) {
1022 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1023 * flag above, the last thread to leave the push will do the callback. New
1024 * threads going into the push will block. */
1025 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1026 "pad is in use, delay idle callback");
1027 GST_OBJECT_UNLOCK (pad);
1029 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, };
1031 /* the pad is idle now, we can signal the idle callback now */
1032 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1033 "pad is idle, trigger idle callback");
1034 GST_OBJECT_UNLOCK (pad);
1036 callback (pad, &info, user_data);
1039 GST_OBJECT_UNLOCK (pad);
1045 cleanup_hook (GstPad * pad, GHook * hook)
1047 GstPadProbeType type;
1049 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1051 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1052 /* unblock when we remove the last blocking probe */
1054 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1056 if (pad->num_blocked == 0) {
1057 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1058 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1059 GST_PAD_BLOCK_BROADCAST (pad);
1062 g_hook_destroy_link (&pad->probes, hook);
1067 * gst_pad_remove_probe:
1068 * @pad: the #GstPad with the probe
1069 * @id: the probe id to remove
1071 * Remove the probe with @id from @pad.
1076 gst_pad_remove_probe (GstPad * pad, gulong id)
1080 g_return_if_fail (GST_IS_PAD (pad));
1082 GST_OBJECT_LOCK (pad);
1084 hook = g_hook_get (&pad->probes, id);
1088 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1090 cleanup_hook (pad, hook);
1091 GST_OBJECT_UNLOCK (pad);
1097 GST_OBJECT_UNLOCK (pad);
1098 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1104 * gst_pad_is_blocked:
1105 * @pad: the #GstPad to query
1107 * Checks if the pad is blocked or not. This function returns the
1108 * last requested state of the pad. It is not certain that the pad
1109 * is actually blocking at this point (see gst_pad_is_blocking()).
1111 * Returns: TRUE if the pad is blocked.
1116 gst_pad_is_blocked (GstPad * pad)
1118 gboolean result = FALSE;
1120 g_return_val_if_fail (GST_IS_PAD (pad), result);
1122 GST_OBJECT_LOCK (pad);
1123 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1124 GST_OBJECT_UNLOCK (pad);
1130 * gst_pad_is_blocking:
1131 * @pad: the #GstPad to query
1133 * Checks if the pad is blocking or not. This is a guaranteed state
1134 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1136 * Returns: TRUE if the pad is blocking.
1143 gst_pad_is_blocking (GstPad * pad)
1145 gboolean result = FALSE;
1147 g_return_val_if_fail (GST_IS_PAD (pad), result);
1149 GST_OBJECT_LOCK (pad);
1150 /* the blocking flag is only valid if the pad is not flushing */
1151 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1152 GST_OBJECT_UNLOCK (pad);
1158 * gst_pad_check_reconfigure:
1159 * @pad: the #GstPad to check
1161 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1162 * if the flag was set.
1164 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1167 gst_pad_check_reconfigure (GstPad * pad)
1169 gboolean reconfigure;
1171 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1173 GST_OBJECT_LOCK (pad);
1174 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1175 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1176 GST_OBJECT_UNLOCK (pad);
1182 * gst_pad_mark_reconfigure:
1183 * @pad: the #GstPad to mark
1185 * Mark a pad for needing reconfiguration. The next call to
1186 * gst_pad_check_reconfigure() will return %TRUE after this call.
1189 gst_pad_mark_reconfigure (GstPad * pad)
1191 g_return_if_fail (GST_IS_PAD (pad));
1193 GST_OBJECT_LOCK (pad);
1194 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1195 GST_OBJECT_UNLOCK (pad);
1199 * gst_pad_set_activate_function_full:
1201 * @activate: the #GstPadActivateFunction to set.
1202 * @notify: notify called when @activate will not be used anymore.
1204 * Sets the given activate function for @pad. The activate function will
1205 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1206 * the actual activation. Only makes sense to set on sink pads.
1208 * Call this function if your sink pad can start a pull-based task.
1211 gst_pad_set_activate_function_full (GstPad * pad,
1212 GstPadActivateFunction activate, GDestroyNotify notify)
1214 g_return_if_fail (GST_IS_PAD (pad));
1216 if (pad->activatenotify)
1217 pad->activatenotify (pad);
1218 GST_PAD_ACTIVATEFUNC (pad) = activate;
1219 pad->activatenotify = notify;
1221 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1222 GST_DEBUG_FUNCPTR_NAME (activate));
1226 * gst_pad_set_activatemode_function_full:
1228 * @activatemode: the #GstPadActivateModeFunction to set.
1229 * @notify: notify called when @activatemode will not be used anymore.
1231 * Sets the given activate_mode function for the pad. An activate_mode function
1232 * prepares the element for data passing.
1235 gst_pad_set_activatemode_function_full (GstPad * pad,
1236 GstPadActivateModeFunction activatemode, GDestroyNotify notify)
1238 g_return_if_fail (GST_IS_PAD (pad));
1240 if (pad->activatemodenotify)
1241 pad->activatemodenotify (pad);
1242 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1243 pad->activatemodenotify = notify;
1245 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1246 GST_DEBUG_FUNCPTR_NAME (activatemode));
1250 * gst_pad_set_chain_function_full:
1251 * @pad: a sink #GstPad.
1252 * @chain: the #GstPadChainFunction to set.
1253 * @notify: notify called when @chain will not be used anymore.
1255 * Sets the given chain function for the pad. The chain function is called to
1256 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1259 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1260 GDestroyNotify notify)
1262 g_return_if_fail (GST_IS_PAD (pad));
1263 g_return_if_fail (GST_PAD_IS_SINK (pad));
1265 if (pad->chainnotify)
1266 pad->chainnotify (pad);
1267 GST_PAD_CHAINFUNC (pad) = chain;
1268 pad->chainnotify = notify;
1270 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1271 GST_DEBUG_FUNCPTR_NAME (chain));
1275 * gst_pad_set_chain_list_function_full:
1276 * @pad: a sink #GstPad.
1277 * @chainlist: the #GstPadChainListFunction to set.
1278 * @notify: notify called when @chainlist will not be used anymore.
1280 * Sets the given chain list function for the pad. The chainlist function is
1281 * called to process a #GstBufferList input buffer list. See
1282 * #GstPadChainListFunction for more details.
1287 gst_pad_set_chain_list_function_full (GstPad * pad,
1288 GstPadChainListFunction chainlist, GDestroyNotify notify)
1290 g_return_if_fail (GST_IS_PAD (pad));
1291 g_return_if_fail (GST_PAD_IS_SINK (pad));
1293 if (pad->chainlistnotify)
1294 pad->chainlistnotify (pad);
1295 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1296 pad->chainlistnotify = notify;
1298 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1299 GST_DEBUG_FUNCPTR_NAME (chainlist));
1303 * gst_pad_set_getrange_function_full:
1304 * @pad: a source #GstPad.
1305 * @get: the #GstPadGetRangeFunction to set.
1306 * @notify: notify called when @get will not be used anymore.
1308 * Sets the given getrange function for the pad. The getrange function is
1309 * called to produce a new #GstBuffer to start the processing pipeline. see
1310 * #GstPadGetRangeFunction for a description of the getrange function.
1313 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1314 GDestroyNotify notify)
1316 g_return_if_fail (GST_IS_PAD (pad));
1317 g_return_if_fail (GST_PAD_IS_SRC (pad));
1319 if (pad->getrangenotify)
1320 pad->getrangenotify (pad);
1321 GST_PAD_GETRANGEFUNC (pad) = get;
1322 pad->getrangenotify = notify;
1324 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1325 GST_DEBUG_FUNCPTR_NAME (get));
1329 * gst_pad_set_event_function_full:
1330 * @pad: a #GstPad of either direction.
1331 * @event: the #GstPadEventFunction to set.
1332 * @notify: notify called when @event will not be used anymore.
1334 * Sets the given event handler for the pad.
1337 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1338 GDestroyNotify notify)
1340 g_return_if_fail (GST_IS_PAD (pad));
1342 if (pad->eventnotify)
1343 pad->eventnotify (pad);
1344 GST_PAD_EVENTFUNC (pad) = event;
1345 pad->eventnotify = notify;
1347 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1348 GST_DEBUG_FUNCPTR_NAME (event));
1352 * gst_pad_set_query_function_full:
1353 * @pad: a #GstPad of either direction.
1354 * @query: the #GstPadQueryFunction to set.
1355 * @notify: notify called when @query will not be used anymore.
1357 * Set the given query function for the pad.
1360 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1361 GDestroyNotify notify)
1363 g_return_if_fail (GST_IS_PAD (pad));
1365 if (pad->querynotify)
1366 pad->querynotify (pad);
1367 GST_PAD_QUERYFUNC (pad) = query;
1368 pad->querynotify = notify;
1370 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1371 GST_DEBUG_FUNCPTR_NAME (query));
1375 * gst_pad_set_iterate_internal_links_function_full:
1376 * @pad: a #GstPad of either direction.
1377 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1378 * @notify: notify called when @iterintlink will not be used anymore.
1380 * Sets the given internal link iterator function for the pad.
1385 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1386 GstPadIterIntLinkFunction iterintlink, GDestroyNotify notify)
1388 g_return_if_fail (GST_IS_PAD (pad));
1390 if (pad->iterintlinknotify)
1391 pad->iterintlinknotify (pad);
1392 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1393 pad->iterintlinknotify = notify;
1395 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1396 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1400 * gst_pad_set_link_function_full:
1402 * @link: the #GstPadLinkFunction to set.
1403 * @notify: notify called when @link will not be used anymore.
1405 * Sets the given link function for the pad. It will be called when
1406 * the pad is linked with another pad.
1408 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1411 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1412 * cannot be made for some reason.
1414 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1415 * of the peer sink pad, if present.
1418 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1419 GDestroyNotify notify)
1421 g_return_if_fail (GST_IS_PAD (pad));
1423 if (pad->linknotify)
1424 pad->linknotify (pad);
1425 GST_PAD_LINKFUNC (pad) = link;
1426 pad->linknotify = notify;
1428 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1429 GST_DEBUG_FUNCPTR_NAME (link));
1433 * gst_pad_set_unlink_function_full:
1435 * @unlink: the #GstPadUnlinkFunction to set.
1436 * @notify: notify called when @unlink will not be used anymore.
1438 * Sets the given unlink function for the pad. It will be called
1439 * when the pad is unlinked.
1442 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1443 GDestroyNotify notify)
1445 g_return_if_fail (GST_IS_PAD (pad));
1447 if (pad->unlinknotify)
1448 pad->unlinknotify (pad);
1449 GST_PAD_UNLINKFUNC (pad) = unlink;
1450 pad->unlinknotify = notify;
1452 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1453 GST_DEBUG_FUNCPTR_NAME (unlink));
1458 * @srcpad: the source #GstPad to unlink.
1459 * @sinkpad: the sink #GstPad to unlink.
1461 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1462 * signal on both pads.
1464 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1465 * the pads were not linked together.
1470 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1472 gboolean result = FALSE;
1473 GstElement *parent = NULL;
1475 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1476 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1477 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1478 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1480 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1481 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1482 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1484 /* We need to notify the parent before taking any pad locks as the bin in
1485 * question might be waiting for a lock on the pad while holding its lock
1486 * that our message will try to take. */
1487 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1488 if (GST_IS_ELEMENT (parent)) {
1489 gst_element_post_message (parent,
1490 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1491 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1493 gst_object_unref (parent);
1498 GST_OBJECT_LOCK (srcpad);
1499 GST_OBJECT_LOCK (sinkpad);
1501 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1502 goto not_linked_together;
1504 if (GST_PAD_UNLINKFUNC (srcpad)) {
1505 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1507 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1508 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1511 /* first clear peers */
1512 GST_PAD_PEER (srcpad) = NULL;
1513 GST_PAD_PEER (sinkpad) = NULL;
1515 GST_OBJECT_UNLOCK (sinkpad);
1516 GST_OBJECT_UNLOCK (srcpad);
1518 /* fire off a signal to each of the pads telling them
1519 * that they've been unlinked */
1520 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1521 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1523 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1524 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1529 if (parent != NULL) {
1530 gst_element_post_message (parent,
1531 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1532 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1533 gst_object_unref (parent);
1538 not_linked_together:
1540 /* we do not emit a warning in this case because unlinking cannot
1541 * be made MT safe.*/
1542 GST_OBJECT_UNLOCK (sinkpad);
1543 GST_OBJECT_UNLOCK (srcpad);
1549 * gst_pad_is_linked:
1550 * @pad: pad to check
1552 * Checks if a @pad is linked to another pad or not.
1554 * Returns: TRUE if the pad is linked, FALSE otherwise.
1559 gst_pad_is_linked (GstPad * pad)
1563 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1565 GST_OBJECT_LOCK (pad);
1566 result = (GST_PAD_PEER (pad) != NULL);
1567 GST_OBJECT_UNLOCK (pad);
1572 /* get the caps from both pads and see if the intersection
1575 * This function should be called with the pad LOCK on both
1579 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1580 GstPadLinkCheck flags)
1582 GstCaps *srccaps = NULL;
1583 GstCaps *sinkcaps = NULL;
1584 gboolean compatible = FALSE;
1586 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1589 /* Doing the expensive caps checking takes priority over only checking the template caps */
1590 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1591 GST_OBJECT_UNLOCK (sink);
1592 GST_OBJECT_UNLOCK (src);
1594 srccaps = gst_pad_query_caps (src, NULL);
1595 sinkcaps = gst_pad_query_caps (sink, NULL);
1597 GST_OBJECT_LOCK (src);
1598 GST_OBJECT_LOCK (sink);
1600 /* If one of the two pads doesn't have a template, consider the intersection
1602 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1603 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1607 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1609 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1612 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1614 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1617 /* if we have caps on both pads we can check the intersection. If one
1618 * of the caps is NULL, we return TRUE. */
1619 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1621 gst_caps_unref (srccaps);
1623 gst_caps_unref (sinkcaps);
1627 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1628 gst_caps_unref (srccaps);
1629 gst_caps_unref (sinkcaps);
1632 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1633 (compatible ? "" : "not"));
1638 /* check if the grandparents of both pads are the same.
1639 * This check is required so that we don't try to link
1640 * pads from elements in different bins without ghostpads.
1642 * The LOCK should be held on both pads
1645 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1647 GstObject *psrc, *psink;
1649 psrc = GST_OBJECT_PARENT (src);
1650 psink = GST_OBJECT_PARENT (sink);
1652 /* if one of the pads has no parent, we allow the link */
1653 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1656 /* only care about parents that are elements */
1657 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1658 goto no_element_parent;
1660 /* if the parents are the same, we have a loop */
1661 if (G_UNLIKELY (psrc == psink))
1664 /* if they both have a parent, we check the grandparents. We can not lock
1665 * the parent because we hold on the child (pad) and the locking order is
1666 * parent >> child. */
1667 psrc = GST_OBJECT_PARENT (psrc);
1668 psink = GST_OBJECT_PARENT (psink);
1670 /* if they have grandparents but they are not the same */
1671 if (G_UNLIKELY (psrc != psink))
1672 goto wrong_grandparents;
1679 GST_CAT_DEBUG (GST_CAT_CAPS,
1680 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1681 GST_PTR_FORMAT, psrc, psink);
1686 GST_CAT_DEBUG (GST_CAT_CAPS,
1687 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1688 GST_PTR_FORMAT, psrc, psink);
1693 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1699 GST_CAT_DEBUG (GST_CAT_CAPS,
1700 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1701 GST_PTR_FORMAT, psrc, psink);
1706 /* FIXME leftover from an attempt at refactoring... */
1707 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1708 * the two pads will be locked in the srcpad, sinkpad order. */
1709 static GstPadLinkReturn
1710 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1712 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1713 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1715 GST_OBJECT_LOCK (srcpad);
1717 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1718 goto src_was_linked;
1720 GST_OBJECT_LOCK (sinkpad);
1722 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1723 goto sink_was_linked;
1725 /* check hierarchy, pads can only be linked if the grandparents
1727 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1728 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1729 goto wrong_hierarchy;
1731 /* check pad caps for non-empty intersection */
1732 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1735 /* FIXME check pad scheduling for non-empty intersection */
1737 return GST_PAD_LINK_OK;
1741 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1742 GST_DEBUG_PAD_NAME (srcpad),
1743 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1744 /* we do not emit a warning in this case because unlinking cannot
1745 * be made MT safe.*/
1746 GST_OBJECT_UNLOCK (srcpad);
1747 return GST_PAD_LINK_WAS_LINKED;
1751 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1752 GST_DEBUG_PAD_NAME (sinkpad),
1753 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1754 /* we do not emit a warning in this case because unlinking cannot
1755 * be made MT safe.*/
1756 GST_OBJECT_UNLOCK (sinkpad);
1757 GST_OBJECT_UNLOCK (srcpad);
1758 return GST_PAD_LINK_WAS_LINKED;
1762 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1763 GST_OBJECT_UNLOCK (sinkpad);
1764 GST_OBJECT_UNLOCK (srcpad);
1765 return GST_PAD_LINK_WRONG_HIERARCHY;
1769 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1770 GST_OBJECT_UNLOCK (sinkpad);
1771 GST_OBJECT_UNLOCK (srcpad);
1772 return GST_PAD_LINK_NOFORMAT;
1778 * @srcpad: the source #GstPad.
1779 * @sinkpad: the sink #GstPad.
1781 * Checks if the source pad and the sink pad are compatible so they can be
1784 * Returns: TRUE if the pads can be linked.
1787 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1789 GstPadLinkReturn result;
1791 /* generic checks */
1792 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1793 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1795 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1796 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1798 /* gst_pad_link_prepare does everything for us, we only release the locks
1799 * on the pads that it gets us. If this function returns !OK the locks are not
1801 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1802 if (result != GST_PAD_LINK_OK)
1805 GST_OBJECT_UNLOCK (srcpad);
1806 GST_OBJECT_UNLOCK (sinkpad);
1809 return result == GST_PAD_LINK_OK;
1813 * gst_pad_link_full:
1814 * @srcpad: the source #GstPad to link.
1815 * @sinkpad: the sink #GstPad to link.
1816 * @flags: the checks to validate when linking
1818 * Links the source pad and the sink pad.
1820 * This variant of #gst_pad_link provides a more granular control on the
1821 * checks being done when linking. While providing some considerable speedups
1822 * the caller of this method must be aware that wrong usage of those flags
1823 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1824 * for more information.
1828 * Returns: A result code indicating if the connection worked or
1834 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1836 GstPadLinkReturn result;
1838 GstPadLinkFunction srcfunc, sinkfunc;
1841 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1842 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1843 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1844 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1845 GST_PAD_LINK_WRONG_DIRECTION);
1847 /* Notify the parent early. See gst_pad_unlink for details. */
1848 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
1849 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
1850 gst_element_post_message (parent,
1851 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1852 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
1854 gst_object_unref (parent);
1859 /* prepare will also lock the two pads */
1860 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
1862 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
1865 /* must set peers before calling the link function */
1866 GST_PAD_PEER (srcpad) = sinkpad;
1867 GST_PAD_PEER (sinkpad) = srcpad;
1869 /* check events, when something is different, mark pending */
1870 for (idx = 0; idx < GST_EVENT_MAX_STICKY; idx++) {
1871 PadEvent *srcev, *sinkev;
1873 srcev = &srcpad->priv->events[idx];
1874 sinkev = &sinkpad->priv->events[idx];
1876 if (srcev->event != sinkev->event) {
1877 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
1882 /* get the link functions */
1883 srcfunc = GST_PAD_LINKFUNC (srcpad);
1884 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
1886 if (G_UNLIKELY (srcfunc || sinkfunc)) {
1887 /* custom link functions, execute them */
1888 GST_OBJECT_UNLOCK (sinkpad);
1889 GST_OBJECT_UNLOCK (srcpad);
1892 /* this one will call the peer link function */
1893 result = srcfunc (srcpad, sinkpad);
1894 } else if (sinkfunc) {
1895 /* if no source link function, we need to call the sink link
1896 * function ourselves. */
1897 result = sinkfunc (sinkpad, srcpad);
1900 GST_OBJECT_LOCK (srcpad);
1901 GST_OBJECT_LOCK (sinkpad);
1903 /* we released the lock, check if the same pads are linked still */
1904 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
1905 goto concurrent_link;
1907 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
1910 GST_OBJECT_UNLOCK (sinkpad);
1911 GST_OBJECT_UNLOCK (srcpad);
1913 /* fire off a signal to each of the pads telling them
1914 * that they've been linked */
1915 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
1916 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
1918 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1919 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1921 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
1924 if (G_LIKELY (parent)) {
1925 gst_element_post_message (parent,
1926 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1927 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
1928 gst_object_unref (parent);
1936 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
1937 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1938 GST_OBJECT_UNLOCK (sinkpad);
1939 GST_OBJECT_UNLOCK (srcpad);
1941 /* The other link operation succeeded first */
1942 result = GST_PAD_LINK_WAS_LINKED;
1947 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1948 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1950 GST_PAD_PEER (srcpad) = NULL;
1951 GST_PAD_PEER (sinkpad) = NULL;
1953 GST_OBJECT_UNLOCK (sinkpad);
1954 GST_OBJECT_UNLOCK (srcpad);
1962 * @srcpad: the source #GstPad to link.
1963 * @sinkpad: the sink #GstPad to link.
1965 * Links the source pad and the sink pad.
1967 * Returns: A result code indicating if the connection worked or
1973 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1975 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1979 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1981 GstPadTemplate **template_p;
1983 /* this function would need checks if it weren't static */
1985 GST_OBJECT_LOCK (pad);
1986 template_p = &pad->padtemplate;
1987 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
1988 GST_OBJECT_UNLOCK (pad);
1991 gst_pad_template_pad_created (templ, pad);
1995 * gst_pad_get_pad_template:
1998 * Gets the template for @pad.
2000 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2001 * instantiated, or %NULL if this pad has no template. Unref after
2005 gst_pad_get_pad_template (GstPad * pad)
2007 GstPadTemplate *templ;
2009 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2011 templ = GST_PAD_PAD_TEMPLATE (pad);
2013 return (templ ? gst_object_ref (templ) : NULL);
2017 * gst_pad_has_current_caps:
2018 * @pad: a #GstPad to check
2020 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2022 * Returns: TRUE when @pad has caps associated with it.
2025 gst_pad_has_current_caps (GstPad * pad)
2030 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2032 GST_OBJECT_LOCK (pad);
2033 caps = get_pad_caps (pad);
2034 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2035 "check current pad caps %" GST_PTR_FORMAT, caps);
2036 result = (caps != NULL);
2037 GST_OBJECT_UNLOCK (pad);
2043 * gst_pad_get_current_caps:
2044 * @pad: a #GstPad to get the current capabilities of.
2046 * Gets the capabilities currently configured on @pad with the last
2047 * #GST_EVENT_CAPS event.
2049 * Returns: the current caps of the pad with incremented ref-count.
2052 gst_pad_get_current_caps (GstPad * pad)
2056 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2058 GST_OBJECT_LOCK (pad);
2059 if ((result = get_pad_caps (pad)))
2060 gst_caps_ref (result);
2061 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2062 "get current pad caps %" GST_PTR_FORMAT, result);
2063 GST_OBJECT_UNLOCK (pad);
2070 * @pad: a #GstPad to set the capabilities of.
2071 * @caps: (transfer none): a #GstCaps to set.
2073 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2074 * caps on the pad will be unreffed. This function refs the caps so you should
2075 * unref if as soon as you don't need it anymore.
2076 * It is possible to set NULL caps, which will make the pad unnegotiated
2079 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2080 * or bad parameters were provided to this function.
2085 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2088 gboolean res = TRUE;
2090 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2091 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2093 event = gst_event_new_caps (caps);
2095 if (GST_PAD_IS_SRC (pad))
2096 res = gst_pad_push_event (pad, event);
2098 res = gst_pad_send_event (pad, event);
2104 * gst_pad_get_pad_template_caps:
2105 * @pad: a #GstPad to get the template capabilities from.
2107 * Gets the capabilities for @pad's template.
2109 * Returns: (transfer full): the #GstCaps of this pad template.
2110 * Unref after usage.
2113 gst_pad_get_pad_template_caps (GstPad * pad)
2115 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2117 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2119 if (GST_PAD_PAD_TEMPLATE (pad))
2120 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2122 return gst_static_caps_get (&anycaps);
2127 * @pad: a #GstPad to get the peer of.
2129 * Gets the peer of @pad. This function refs the peer pad so
2130 * you need to unref it after use.
2132 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2137 gst_pad_get_peer (GstPad * pad)
2141 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2143 GST_OBJECT_LOCK (pad);
2144 result = GST_PAD_PEER (pad);
2146 gst_object_ref (result);
2147 GST_OBJECT_UNLOCK (pad);
2153 * gst_pad_get_allowed_caps:
2156 * Gets the capabilities of the allowed media types that can flow through
2157 * @pad and its peer.
2159 * The allowed capabilities is calculated as the intersection of the results of
2160 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2161 * on the resulting caps.
2163 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2164 * caps when you no longer need it. This function returns NULL when @pad
2170 gst_pad_get_allowed_caps (GstPad * pad)
2177 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2179 GST_OBJECT_LOCK (pad);
2180 peer = GST_PAD_PEER (pad);
2181 if (G_UNLIKELY (peer == NULL))
2184 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2186 gst_object_ref (peer);
2187 GST_OBJECT_UNLOCK (pad);
2188 mycaps = gst_pad_query_caps (pad, NULL);
2190 peercaps = gst_pad_query_caps (peer, NULL);
2191 gst_object_unref (peer);
2193 caps = gst_caps_intersect (mycaps, peercaps);
2194 gst_caps_unref (peercaps);
2195 gst_caps_unref (mycaps);
2197 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2204 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2205 GST_OBJECT_UNLOCK (pad);
2212 * gst_pad_iterate_internal_links_default:
2213 * @pad: the #GstPad to get the internal links of.
2214 * @parent: the parent of @pad or NULL
2216 * Iterate the list of pads to which the given pad is linked to inside of
2217 * the parent element.
2218 * This is the default handler, and thus returns an iterator of all of the
2219 * pads inside the parent element with opposite direction.
2221 * The caller must free this iterator after use with gst_iterator_free().
2223 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2224 * returned pad with gst_object_unref().
2229 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2236 GstElement *eparent;
2238 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2240 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2241 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2243 GST_OBJECT_LOCK (pad);
2244 eparent = GST_PAD_PARENT (pad);
2245 if (!eparent || !GST_IS_ELEMENT (eparent))
2248 gst_object_ref (eparent);
2249 GST_OBJECT_UNLOCK (pad);
2252 if (pad->direction == GST_PAD_SRC)
2253 padlist = &eparent->sinkpads;
2255 padlist = &eparent->srcpads;
2257 GST_DEBUG_OBJECT (pad, "Making iterator");
2259 cookie = &eparent->pads_cookie;
2261 lock = GST_OBJECT_GET_LOCK (eparent);
2263 res = gst_iterator_new_list (GST_TYPE_PAD,
2264 lock, cookie, padlist, (GObject *) owner, NULL);
2266 gst_object_unref (owner);
2273 GST_OBJECT_UNLOCK (pad);
2274 GST_DEBUG_OBJECT (pad, "no parent element");
2280 * gst_pad_iterate_internal_links:
2281 * @pad: the GstPad to get the internal links of.
2283 * Gets an iterator for the pads to which the given pad is linked to inside
2284 * of the parent element.
2286 * Each #GstPad element yielded by the iterator will have its refcount increased,
2287 * so unref after use.
2289 * Free-function: gst_iterator_free
2291 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2292 * pad does not have an iterator function configured. Use
2293 * gst_iterator_free() after usage.
2298 gst_pad_iterate_internal_links (GstPad * pad)
2300 GstIterator *res = NULL;
2303 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2305 GST_OBJECT_LOCK (pad);
2306 ACQUIRE_PARENT (pad, parent, no_parent);
2307 GST_OBJECT_UNLOCK (pad);
2309 if (GST_PAD_ITERINTLINKFUNC (pad))
2310 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2312 RELEASE_PARENT (parent);
2319 GST_DEBUG_OBJECT (pad, "no parent");
2320 GST_OBJECT_UNLOCK (pad);
2328 * @forward: a #GstPadForwardFunction
2329 * @user_data: user data passed to @forward
2331 * Calls @forward for all internally linked pads of @pad. This function deals with
2332 * dynamically changing internal pads and will make sure that the @forward
2333 * function is only called once for each pad.
2335 * When @forward returns TRUE, no further pads will be processed.
2337 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2340 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2343 gboolean result = FALSE;
2345 gboolean done = FALSE;
2346 GValue item = { 0, };
2347 GList *pushed_pads = NULL;
2349 iter = gst_pad_iterate_internal_links (pad);
2355 switch (gst_iterator_next (iter, &item)) {
2356 case GST_ITERATOR_OK:
2360 intpad = g_value_get_object (&item);
2362 /* if already pushed, skip. FIXME, find something faster to tag pads */
2363 if (g_list_find (pushed_pads, intpad)) {
2364 g_value_reset (&item);
2368 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2369 GST_DEBUG_PAD_NAME (intpad));
2370 done = result = forward (intpad, user_data);
2372 pushed_pads = g_list_prepend (pushed_pads, intpad);
2374 g_value_reset (&item);
2377 case GST_ITERATOR_RESYNC:
2378 /* We don't reset the result here because we don't push the event
2379 * again on pads that got the event already and because we need
2380 * to consider the result of the previous pushes */
2381 gst_iterator_resync (iter);
2383 case GST_ITERATOR_ERROR:
2384 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2387 case GST_ITERATOR_DONE:
2392 g_value_unset (&item);
2393 gst_iterator_free (iter);
2395 g_list_free (pushed_pads);
2405 gboolean dispatched;
2409 event_forward_func (GstPad * pad, EventData * data)
2411 /* for each pad we send to, we should ref the event; it's up
2412 * to downstream to unref again when handled. */
2413 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2414 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2416 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2418 data->dispatched = TRUE;
2425 * gst_pad_event_default:
2426 * @pad: a #GstPad to call the default event handler on.
2427 * @parent: the parent of @pad or NULL
2428 * @event: (transfer full): the #GstEvent to handle.
2430 * Invokes the default event handler for the given pad.
2432 * The EOS event will pause the task associated with @pad before it is forwarded
2433 * to all internally linked pads,
2435 * The the event is sent to all pads internally linked to @pad. This function
2436 * takes ownership of @event.
2438 * Returns: TRUE if the event was sent successfully.
2441 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2443 gboolean result, forward = TRUE;
2445 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2446 g_return_val_if_fail (event != NULL, FALSE);
2448 GST_LOG_OBJECT (pad, "default event handler");
2450 switch (GST_EVENT_TYPE (event)) {
2453 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2454 gst_pad_pause_task (pad);
2457 case GST_EVENT_CAPS:
2458 forward = GST_PAD_IS_PROXY_CAPS (pad);
2469 data.dispatched = FALSE;
2470 data.result = FALSE;
2472 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2474 /* for sinkpads without a parent element or without internal links, nothing
2475 * will be dispatched but we still want to return TRUE. */
2476 if (data.dispatched)
2477 result = data.result;
2482 gst_event_unref (event);
2487 /* Default accept caps implementation just checks against
2488 * the allowed caps for the pad */
2490 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2492 /* get the caps and see if it intersects to something not empty */
2493 GstCaps *caps, *allowed;
2496 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2498 /* first forward the query to internally linked pads when we are dealing with
2500 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2501 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2506 allowed = gst_pad_query_caps (pad, NULL);
2507 gst_query_parse_accept_caps (query, &caps);
2510 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2511 result = gst_caps_is_subset (caps, allowed);
2512 gst_caps_unref (allowed);
2514 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2517 gst_query_set_accept_caps_result (query, result);
2523 /* Default caps implementation */
2525 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2527 GstCaps *result = NULL, *filter;
2528 GstPadTemplate *templ;
2529 gboolean fixed_caps;
2531 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2533 gst_query_parse_caps (query, &filter);
2535 /* first try to proxy if we must */
2536 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2537 if ((gst_pad_proxy_query_caps (pad, query))) {
2538 gst_query_parse_caps_result (query, &result);
2543 /* no proxy or it failed, do default handling */
2544 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2546 GST_OBJECT_LOCK (pad);
2548 /* fixed caps, try the negotiated caps first */
2549 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2550 if ((result = get_pad_caps (pad)))
2551 goto filter_done_unlock;
2554 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2555 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2556 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2557 goto filter_done_unlock;
2561 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2562 "non-fixed pad caps: trying pad caps");
2563 /* non fixed caps, try the negotiated caps */
2564 if ((result = get_pad_caps (pad)))
2565 goto filter_done_unlock;
2567 GST_OBJECT_UNLOCK (pad);
2569 /* this almost never happens */
2570 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2571 result = gst_caps_new_empty ();
2576 GST_OBJECT_UNLOCK (pad);
2579 /* run the filter on the result */
2581 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2582 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2583 GST_PTR_FORMAT, result, result, filter, filter);
2584 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2585 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2588 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2589 "using caps %p %" GST_PTR_FORMAT, result, result);
2590 result = gst_caps_ref (result);
2594 gst_query_set_caps_result (query, result);
2595 gst_caps_unref (result);
2601 * gst_pad_query_default:
2602 * @pad: a #GstPad to call the default query handler on.
2603 * @parent: the parent of @pad or NULL
2604 * @query: (transfer none): the #GstQuery to handle.
2606 * Invokes the default query handler for the given pad.
2607 * The query is sent to all pads internally linked to @pad. Note that
2608 * if there are many possible sink pads that are internally linked to
2609 * @pad, only one will be sent the query.
2610 * Multi-sinkpad elements should implement custom query handlers.
2612 * Returns: TRUE if the query was performed successfully.
2615 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2617 gboolean forward = TRUE, ret = FALSE;
2619 switch (GST_QUERY_TYPE (query)) {
2620 case GST_QUERY_SCHEDULING:
2623 case GST_QUERY_ACCEPT_CAPS:
2624 ret = gst_pad_query_accept_caps_default (pad, query);
2627 case GST_QUERY_CAPS:
2628 ret = gst_pad_query_caps_default (pad, query);
2631 case GST_QUERY_POSITION:
2632 case GST_QUERY_SEEKING:
2633 case GST_QUERY_FORMATS:
2634 case GST_QUERY_LATENCY:
2635 case GST_QUERY_JITTER:
2636 case GST_QUERY_RATE:
2637 case GST_QUERY_CONVERT:
2638 case GST_QUERY_ALLOCATION:
2644 ret = gst_pad_forward
2645 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2651 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2653 GstPad *pad = data->pad;
2654 GstPadProbeInfo *info = data->info;
2655 GstPadProbeType type, flags;
2656 GstPadProbeCallback callback;
2657 GstPadProbeReturn ret;
2659 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2660 "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
2662 /* if we have called this callback, do nothing */
2663 if (PROBE_COOKIE (hook) == data->cookie) {
2664 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2665 "hook %lu, cookie %u already called", hook->hook_id,
2666 PROBE_COOKIE (hook));
2670 PROBE_COOKIE (hook) = data->cookie;
2672 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2675 /* one of the data types */
2676 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2678 /* one of the scheduling types */
2679 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2681 /* all of the blocking types must match */
2682 if ((flags & GST_PAD_PROBE_TYPE_BLOCKING) !=
2683 (type & GST_PAD_PROBE_TYPE_BLOCKING))
2686 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2687 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
2689 callback = (GstPadProbeCallback) hook->func;
2690 if (callback == NULL)
2693 GST_OBJECT_UNLOCK (pad);
2695 ret = callback (pad, info, hook->data);
2697 GST_OBJECT_LOCK (pad);
2698 data->marshalled = TRUE;
2701 case GST_PAD_PROBE_REMOVE:
2702 /* remove the probe */
2703 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2704 cleanup_hook (pad, hook);
2706 case GST_PAD_PROBE_DROP:
2707 /* need to drop the data, make sure other probes don't get called
2709 GST_DEBUG_OBJECT (pad, "asked to drop item");
2710 info->type = GST_PAD_PROBE_TYPE_INVALID;
2711 data->dropped = TRUE;
2713 case GST_PAD_PROBE_PASS:
2714 /* inform the pad block to let things pass */
2715 GST_DEBUG_OBJECT (pad, "asked to pass item");
2719 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2726 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2727 "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
2733 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2735 if (G_UNLIKELY (pad->num_probes)) { \
2736 /* we start with passing NULL as the data item */ \
2737 GstPadProbeInfo info = { mask, NULL, offs, size }; \
2738 ret = do_probe_callbacks (pad, &info, defaultval); \
2739 /* store the possibly updated data item */ \
2740 data = GST_PAD_PROBE_INFO_DATA (&info); \
2741 /* if something went wrong, exit */ \
2742 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2744 /* otherwise check if the probe retured data */ \
2745 if (G_UNLIKELY (data != NULL)) \
2751 /* a probe that does not take or return any data */
2752 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2754 if (G_UNLIKELY (pad->num_probes)) { \
2755 /* pass NULL as the data item */ \
2756 GstPadProbeInfo info = { mask, NULL, 0, 0 }; \
2757 ret = do_probe_callbacks (pad, &info, defaultval); \
2758 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2763 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2765 if (G_UNLIKELY (pad->num_probes)) { \
2766 GstPadProbeInfo info = { mask, data, offs, size }; \
2767 ret = do_probe_callbacks (pad, &info, defaultval); \
2768 data = GST_PAD_PROBE_INFO_DATA (&info); \
2769 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2774 #define PROBE_PUSH(pad,mask,data,label) \
2775 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2776 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2777 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2779 static GstFlowReturn
2780 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2781 GstFlowReturn defaultval)
2790 data.marshalled = FALSE;
2791 data.dropped = FALSE;
2792 data.cookie = ++pad->priv->probe_cookie;
2795 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2798 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2799 "do probes cookie %u", data.cookie);
2800 cookie = pad->priv->probe_list_cookie;
2802 g_hook_list_marshal (&pad->probes, TRUE,
2803 (GHookMarshaller) probe_hook_marshal, &data);
2805 /* if the list changed, call the new callbacks (they will not have their
2806 * cookie set to data.cookie */
2807 if (cookie != pad->priv->probe_list_cookie) {
2808 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2809 "probe list changed, restarting");
2813 /* the first item that dropped will stop the hooks and then we drop here */
2817 /* if no handler matched and we are blocking, let the item pass */
2818 if (!data.marshalled && is_block)
2821 /* At this point, all handlers returned either OK or PASS. If one handler
2822 * returned PASS, let the item pass */
2827 while (GST_PAD_IS_BLOCKED (pad)) {
2828 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2829 "we are blocked %d times", pad->num_blocked);
2831 /* we might have released the lock */
2832 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2835 /* now we block the streaming thread. It can be unlocked when we
2836 * deactivate the pad (which will also set the FLUSHING flag) or
2837 * when the pad is unblocked. A flushing event will also unblock
2838 * the pad after setting the FLUSHING flag. */
2839 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2840 "Waiting to be unblocked or set flushing");
2841 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
2842 GST_PAD_BLOCK_WAIT (pad);
2843 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
2844 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
2846 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2856 GST_DEBUG_OBJECT (pad, "pad is flushing");
2857 return GST_FLOW_WRONG_STATE;
2861 GST_DEBUG_OBJECT (pad, "data is dropped");
2862 return GST_FLOW_CUSTOM_SUCCESS;
2866 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
2867 GST_DEBUG_OBJECT (pad, "data is passed");
2875 * gst_pad_get_offset:
2878 * Get the offset applied to the running time of @pad. @pad has to be a source
2881 * Returns: the offset.
2884 gst_pad_get_offset (GstPad * pad)
2888 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2890 GST_OBJECT_LOCK (pad);
2891 result = pad->offset;
2892 GST_OBJECT_UNLOCK (pad);
2898 * gst_pad_set_offset:
2900 * @offset: the offset
2902 * Set the offset that will be applied to the running time of @pad.
2905 gst_pad_set_offset (GstPad * pad, gint64 offset)
2909 g_return_if_fail (GST_IS_PAD (pad));
2911 GST_OBJECT_LOCK (pad);
2912 /* if nothing changed, do nothing */
2913 if (pad->offset == offset)
2916 pad->offset = offset;
2917 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
2919 /* sinkpads will apply their offset the next time a segment
2920 * event is received. */
2921 if (GST_PAD_IS_SINK (pad))
2924 /* the index of the segment event in the array */
2925 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
2927 /* resend the last segment event on next buffer push */
2928 if (pad->priv->events[idx].event) {
2929 pad->priv->events[idx].received = FALSE;
2930 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
2934 GST_OBJECT_UNLOCK (pad);
2940 * @pad: a #GstPad to invoke the default query on.
2941 * @query: (transfer none): the #GstQuery to perform.
2943 * Dispatches a query to a pad. The query should have been allocated by the
2944 * caller via one of the type-specific allocation functions. The element that
2945 * the pad belongs to is responsible for filling the query with an appropriate
2946 * response, which should then be parsed with a type-specific query parsing
2949 * Again, the caller is responsible for both the allocation and deallocation of
2950 * the query structure.
2952 * Please also note that some queries might need a running pipeline to work.
2954 * Returns: TRUE if the query could be performed.
2957 gst_pad_query (GstPad * pad, GstQuery * query)
2961 GstPadQueryFunction func;
2962 GstPadProbeType type;
2965 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2966 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2968 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
2969 GST_QUERY_TYPE_NAME (query));
2971 if (GST_PAD_IS_SRC (pad))
2972 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
2974 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
2976 GST_OBJECT_LOCK (pad);
2977 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
2978 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
2979 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
2981 ACQUIRE_PARENT (pad, parent, no_parent);
2982 GST_OBJECT_UNLOCK (pad);
2984 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
2987 res = func (pad, parent, query);
2989 RELEASE_PARENT (parent);
2991 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
2992 GST_QUERY_TYPE_NAME (query), res);
2997 GST_OBJECT_LOCK (pad);
2998 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
2999 GST_OBJECT_UNLOCK (pad);
3006 GST_DEBUG_OBJECT (pad, "had no parent");
3007 GST_OBJECT_UNLOCK (pad);
3012 GST_DEBUG_OBJECT (pad, "had no query function");
3013 RELEASE_PARENT (parent);
3018 GST_DEBUG_OBJECT (pad, "query failed");
3023 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3024 GST_OBJECT_UNLOCK (pad);
3030 * gst_pad_peer_query:
3031 * @pad: a #GstPad to invoke the peer query on.
3032 * @query: (transfer none): the #GstQuery to perform.
3034 * Performs gst_pad_query() on the peer of @pad.
3036 * The caller is responsible for both the allocation and deallocation of
3037 * the query structure.
3039 * Returns: TRUE if the query could be performed. This function returns %FALSE
3040 * if @pad has no peer.
3045 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3048 GstPadProbeType type;
3052 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3053 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3055 if (GST_PAD_IS_SRC (pad))
3056 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3058 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3060 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3061 GST_QUERY_TYPE_NAME (query));
3063 GST_OBJECT_LOCK (pad);
3064 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3065 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3066 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3068 peerpad = GST_PAD_PEER (pad);
3069 if (G_UNLIKELY (peerpad == NULL))
3072 gst_object_ref (peerpad);
3073 GST_OBJECT_UNLOCK (pad);
3075 res = gst_pad_query (peerpad, query);
3077 gst_object_unref (peerpad);
3082 GST_OBJECT_LOCK (pad);
3083 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3084 GST_OBJECT_UNLOCK (pad);
3091 GST_WARNING_OBJECT (pad, "pad has no peer");
3092 GST_OBJECT_UNLOCK (pad);
3097 GST_DEBUG_OBJECT (pad, "query failed");
3102 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3103 GST_OBJECT_UNLOCK (pad);
3108 /**********************************************************************
3109 * Data passing functions
3112 /* should be called with the LOCK */
3113 static inline GstFlowReturn
3114 gst_pad_push_sticky_events (GstPad * pad)
3122 GST_DEBUG_OBJECT (pad, "pushing out all sticky events");
3123 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
3128 ev = &pad->priv->events[i];
3130 /* skip without active event */
3131 if ((event = ev->event) == NULL)
3135 GST_DEBUG_OBJECT (pad, "event %s was already received",
3136 GST_EVENT_TYPE_NAME (event));
3140 gst_event_ref (event);
3141 GST_OBJECT_UNLOCK (pad);
3143 res = gst_pad_push_event (pad, event);
3145 GST_OBJECT_LOCK (pad);
3147 ret = GST_FLOW_ERROR;
3150 /* things could have changed while we release the lock, check if we still
3151 * are handling the same event, if we don't something changed and we have
3152 * to try again. FIXME. we need a cookie here. */
3153 if (event != ev->event) {
3154 GST_DEBUG_OBJECT (pad, "events changed, restarting");
3162 /* this is the chain function that does not perform the additional argument
3163 * checking for that little extra speed.
3165 static inline GstFlowReturn
3166 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3171 GST_PAD_STREAM_LOCK (pad);
3173 GST_OBJECT_LOCK (pad);
3174 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3177 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3179 PROBE_PUSH (pad, type, data, probe_stopped);
3181 parent = GST_OBJECT_PARENT (pad);
3182 GST_OBJECT_UNLOCK (pad);
3184 /* NOTE: we read the chainfunc unlocked.
3185 * we cannot hold the lock for the pad so we might send
3186 * the data to the wrong function. This is not really a
3187 * problem since functions are assigned at creation time
3188 * and don't change that often... */
3189 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3190 GstPadChainFunction chainfunc;
3192 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3195 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3196 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3197 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3199 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3201 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3202 "called chainfunction &%s with buffer %p, returned %s",
3203 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3205 GstPadChainListFunction chainlistfunc;
3207 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3210 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3211 "calling chainlistfunction &%s",
3212 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3214 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3216 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3217 "called chainlistfunction &%s, returned %s",
3218 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3221 GST_PAD_STREAM_UNLOCK (pad);
3228 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3229 "chaining, but pad was flushing");
3230 GST_OBJECT_UNLOCK (pad);
3231 GST_PAD_STREAM_UNLOCK (pad);
3232 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3233 return GST_FLOW_WRONG_STATE;
3237 GST_OBJECT_UNLOCK (pad);
3238 GST_PAD_STREAM_UNLOCK (pad);
3239 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3242 case GST_FLOW_CUSTOM_SUCCESS:
3243 GST_DEBUG_OBJECT (pad, "dropped buffer");
3247 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3254 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3255 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3256 "pushing, but not chainhandler");
3257 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3258 ("push on pad %s:%s but it has no chainfunction",
3259 GST_DEBUG_PAD_NAME (pad)));
3260 GST_PAD_STREAM_UNLOCK (pad);
3261 return GST_FLOW_NOT_SUPPORTED;
3267 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3268 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3271 * Chain a buffer to @pad.
3273 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3275 * If the buffer type is not acceptable for @pad (as negotiated with a
3276 * preceeding GST_EVENT_CAPS event), this function returns
3277 * #GST_FLOW_NOT_NEGOTIATED.
3279 * The function proceeds calling the chain function installed on @pad (see
3280 * gst_pad_set_chain_function()) and the return value of that function is
3281 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3284 * In all cases, success or failure, the caller loses its reference to @buffer
3285 * after calling this function.
3287 * Returns: a #GstFlowReturn from the pad.
3292 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3294 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3295 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3296 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3298 return gst_pad_chain_data_unchecked (pad,
3299 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3302 static GstFlowReturn
3303 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3304 GstBufferList * list)
3310 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3312 len = gst_buffer_list_length (list);
3315 for (i = 0; i < len; i++) {
3316 buffer = gst_buffer_list_get (list, i);
3318 gst_pad_chain_data_unchecked (pad,
3319 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3320 gst_buffer_ref (buffer));
3321 if (ret != GST_FLOW_OK)
3324 gst_buffer_list_unref (list);
3330 * gst_pad_chain_list:
3331 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3332 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3335 * Chain a bufferlist to @pad.
3337 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3339 * If @pad was not negotiated properly with a CAPS event, this function
3340 * returns #GST_FLOW_NOT_NEGOTIATED.
3342 * The function proceeds calling the chainlist function installed on @pad (see
3343 * gst_pad_set_chain_list_function()) and the return value of that function is
3344 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3345 * chainlist function.
3347 * In all cases, success or failure, the caller loses its reference to @list
3348 * after calling this function.
3352 * Returns: a #GstFlowReturn from the pad.
3357 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3359 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3360 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3361 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3363 return gst_pad_chain_data_unchecked (pad,
3364 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3367 static GstFlowReturn
3368 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3373 GST_OBJECT_LOCK (pad);
3374 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3377 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3378 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3380 if ((ret = gst_pad_push_sticky_events (pad)))
3384 /* do block probes */
3385 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3387 /* do post-blocking probes */
3388 PROBE_PUSH (pad, type, data, probe_stopped);
3390 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3393 /* take ref to peer pad before releasing the lock */
3394 gst_object_ref (peer);
3396 GST_OBJECT_UNLOCK (pad);
3398 ret = gst_pad_chain_data_unchecked (peer, type, data);
3400 gst_object_unref (peer);
3402 GST_OBJECT_LOCK (pad);
3404 if (pad->priv->using == 0) {
3405 /* pad is not active anymore, trigger idle callbacks */
3406 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3407 probe_stopped, ret);
3409 GST_OBJECT_UNLOCK (pad);
3413 /* ERROR recovery here */
3417 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3418 "pushing, but pad was flushing");
3419 GST_OBJECT_UNLOCK (pad);
3420 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3421 return GST_FLOW_WRONG_STATE;
3425 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3426 GST_OBJECT_UNLOCK (pad);
3427 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3432 GST_OBJECT_UNLOCK (pad);
3433 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3436 case GST_FLOW_CUSTOM_SUCCESS:
3437 GST_DEBUG_OBJECT (pad, "dropped buffer");
3441 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3448 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3449 "pushing, but it was not linked");
3450 GST_OBJECT_UNLOCK (pad);
3451 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3452 return GST_FLOW_NOT_LINKED;
3458 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3459 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3462 * Pushes a buffer to the peer of @pad.
3464 * This function will call installed block probes before triggering any
3465 * installed data probes.
3467 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3468 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3471 * In all cases, success or failure, the caller loses its reference to @buffer
3472 * after calling this function.
3474 * Returns: a #GstFlowReturn from the peer pad.
3479 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3481 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3482 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3483 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3485 return gst_pad_push_data (pad,
3486 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3490 * gst_pad_push_list:
3491 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3492 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3495 * Pushes a buffer list to the peer of @pad.
3497 * This function will call installed block probes before triggering any
3498 * installed data probes.
3500 * The function proceeds calling the chain function on the peer pad and returns
3501 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3502 * be returned. If the peer pad does not have any installed chainlist function
3503 * every group buffer of the list will be merged into a normal #GstBuffer and
3504 * chained via gst_pad_chain().
3506 * In all cases, success or failure, the caller loses its reference to @list
3507 * after calling this function.
3509 * Returns: a #GstFlowReturn from the peer pad.
3516 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3518 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3519 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3520 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3522 return gst_pad_push_data (pad,
3523 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3526 static GstFlowReturn
3527 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3528 GstBuffer ** buffer)
3531 GstPadGetRangeFunction getrangefunc;
3534 GST_PAD_STREAM_LOCK (pad);
3536 GST_OBJECT_LOCK (pad);
3537 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3540 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3541 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3543 if ((ret = gst_pad_push_sticky_events (pad)))
3547 /* when one of the probes returns a buffer, probed_data will be called and we
3548 * skip calling the getrange function */
3549 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3550 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3552 ACQUIRE_PARENT (pad, parent, no_parent);
3553 GST_OBJECT_UNLOCK (pad);
3555 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3558 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3559 "calling getrangefunc %s, offset %"
3560 G_GUINT64_FORMAT ", size %u",
3561 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3563 ret = getrangefunc (pad, parent, offset, size, buffer);
3565 RELEASE_PARENT (parent);
3567 if (G_UNLIKELY (ret != GST_FLOW_OK))
3568 goto get_range_failed;
3570 /* can only fire the signal if we have a valid buffer */
3571 GST_OBJECT_LOCK (pad);
3573 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3574 *buffer, offset, size, probe_stopped_unref);
3575 GST_OBJECT_UNLOCK (pad);
3577 GST_PAD_STREAM_UNLOCK (pad);
3584 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3585 "getrange, but pad was flushing");
3586 GST_OBJECT_UNLOCK (pad);
3587 GST_PAD_STREAM_UNLOCK (pad);
3588 return GST_FLOW_WRONG_STATE;
3592 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3593 GST_OBJECT_UNLOCK (pad);
3594 GST_PAD_STREAM_UNLOCK (pad);
3599 GST_DEBUG_OBJECT (pad, "no parent");
3600 GST_OBJECT_UNLOCK (pad);
3601 GST_PAD_STREAM_UNLOCK (pad);
3602 return GST_FLOW_WRONG_STATE;
3606 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3607 ("getrange on pad %s:%s but it has no getrangefunction",
3608 GST_DEBUG_PAD_NAME (pad)));
3609 RELEASE_PARENT (parent);
3610 GST_PAD_STREAM_UNLOCK (pad);
3611 return GST_FLOW_NOT_SUPPORTED;
3615 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3616 "probe returned %s", gst_flow_get_name (ret));
3617 GST_OBJECT_UNLOCK (pad);
3618 GST_PAD_STREAM_UNLOCK (pad);
3621 probe_stopped_unref:
3623 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3624 "probe returned %s", gst_flow_get_name (ret));
3625 GST_OBJECT_UNLOCK (pad);
3626 GST_PAD_STREAM_UNLOCK (pad);
3627 gst_buffer_unref (*buffer);
3633 GST_PAD_STREAM_UNLOCK (pad);
3635 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3636 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3637 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3643 * gst_pad_get_range:
3644 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3645 * @offset: The start offset of the buffer
3646 * @size: The length of the buffer
3647 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3648 * returns #GST_FLOW_ERROR if %NULL.
3650 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
3651 * immediately and @buffer is %NULL.
3653 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3654 * description of a getrange function. If @pad has no getrange function
3655 * installed (see gst_pad_set_getrange_function()) this function returns
3656 * #GST_FLOW_NOT_SUPPORTED.
3658 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3660 * Returns: a #GstFlowReturn from the pad.
3665 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3666 GstBuffer ** buffer)
3668 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3669 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3670 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3672 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3676 * gst_pad_pull_range:
3677 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3678 * @offset: The start offset of the buffer
3679 * @size: The length of the buffer
3680 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3681 * GST_FLOW_ERROR if %NULL.
3683 * Pulls a @buffer from the peer pad.
3685 * This function will first trigger the pad block signal if it was
3688 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3689 * function returns the result of gst_pad_get_range() on the peer pad.
3690 * See gst_pad_get_range() for a list of return values and for the
3691 * semantics of the arguments of this function.
3693 * Returns: a #GstFlowReturn from the peer pad.
3694 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3695 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3696 * @buffer may not be used or freed when any other return value than
3697 * #GST_FLOW_OK is returned.
3702 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3703 GstBuffer ** buffer)
3708 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3709 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3710 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3712 GST_OBJECT_LOCK (pad);
3713 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3716 /* when one of the probes returns a buffer, probed_data will be called and we
3717 * skip calling the peer getrange function */
3718 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3719 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3721 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3724 gst_object_ref (peer);
3726 GST_OBJECT_UNLOCK (pad);
3728 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3730 gst_object_unref (peer);
3732 GST_OBJECT_LOCK (pad);
3734 if (pad->priv->using == 0) {
3735 /* pad is not active anymore, trigger idle callbacks */
3736 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3737 post_probe_stopped, ret);
3740 if (G_UNLIKELY (ret != GST_FLOW_OK))
3741 goto pull_range_failed;
3744 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3745 *buffer, offset, size, post_probe_stopped);
3747 GST_OBJECT_UNLOCK (pad);
3751 /* ERROR recovery here */
3754 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3755 "pullrange, but pad was flushing");
3756 GST_OBJECT_UNLOCK (pad);
3757 return GST_FLOW_WRONG_STATE;
3761 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3762 gst_flow_get_name (ret));
3763 GST_OBJECT_UNLOCK (pad);
3768 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3769 "pulling range, but it was not linked");
3770 GST_OBJECT_UNLOCK (pad);
3771 return GST_FLOW_NOT_LINKED;
3776 GST_OBJECT_UNLOCK (pad);
3777 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3778 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3779 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3784 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3785 "post probe returned %s", gst_flow_get_name (ret));
3786 GST_OBJECT_UNLOCK (pad);
3787 if (ret == GST_FLOW_OK)
3788 gst_buffer_unref (*buffer);
3795 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
3800 idx = GST_EVENT_STICKY_IDX (event);
3802 ret = gst_event_replace (&pad->priv->events[idx].event, event);
3805 GST_LOG_OBJECT (pad, "stored sticky event %s at index %u",
3806 GST_EVENT_TYPE_NAME (event), idx);
3807 switch (GST_EVENT_TYPE (event)) {
3808 case GST_EVENT_CAPS:
3810 GST_OBJECT_UNLOCK (pad);
3812 GST_DEBUG_OBJECT (pad, "notify caps");
3813 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
3816 GST_OBJECT_LOCK (pad);
3826 * gst_pad_push_event:
3827 * @pad: a #GstPad to push the event to.
3828 * @event: (transfer full): the #GstEvent to send to the pad.
3830 * Sends the event to the peer of the given pad. This function is
3831 * mainly used by elements to send events to their peer
3834 * This function takes owership of the provided event so you should
3835 * gst_event_ref() it if you want to reuse the event after this call.
3837 * Returns: TRUE if the event was handled.
3842 gst_pad_push_event (GstPad * pad, GstEvent * event)
3847 gboolean stored = FALSE;
3848 GstPadProbeType type;
3852 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3853 g_return_val_if_fail (event != NULL, FALSE);
3854 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
3856 if (GST_PAD_IS_SRC (pad)) {
3857 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
3858 goto wrong_direction;
3859 if ((sticky = GST_EVENT_IS_STICKY (event)))
3860 idx = GST_EVENT_STICKY_IDX (event);
3861 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
3862 } else if (GST_PAD_IS_SINK (pad)) {
3863 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
3864 goto wrong_direction;
3865 /* events pushed on sinkpad never are sticky */
3867 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
3869 goto unknown_direction;
3871 GST_OBJECT_LOCK (pad);
3873 /* Two checks to be made:
3874 * . (un)set the FLUSHING flag for flushing events,
3875 * . handle pad blocking */
3876 switch (GST_EVENT_TYPE (event)) {
3877 case GST_EVENT_FLUSH_START:
3878 GST_PAD_SET_FLUSHING (pad);
3880 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3881 /* flush start will have set the FLUSHING flag and will then
3882 * unlock all threads doing a GCond wait on the blocking pad. This
3883 * will typically unblock the STREAMING thread blocked on a pad. */
3884 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
3885 "doing block signal.");
3886 GST_PAD_BLOCK_BROADCAST (pad);
3890 case GST_EVENT_FLUSH_STOP:
3891 GST_PAD_UNSET_FLUSHING (pad);
3893 /* Remove sticky EOS events */
3894 GST_LOG_OBJECT (pad, "Removing pending EOS events");
3895 clear_event (pad, GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
3897 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3898 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
3904 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3907 /* store the event on the pad, but only on srcpads. We always store the
3908 * event exactly how it was sent */
3910 /* srcpad sticky events are store immediately, we set the pending flag
3911 * to TRUE, it will be set to FALSE when we can successfully push the
3912 * event to the peer pad */
3913 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
3914 GST_DEBUG_OBJECT (pad, "event %s updated",
3915 GST_EVENT_TYPE_NAME (event));
3916 pad->priv->events[idx].received = FALSE;
3921 switch (GST_EVENT_TYPE (event)) {
3922 case GST_EVENT_SEGMENT:
3923 /* pass the adjusted segment event on. We need to do this even if
3924 * there is no peer pad because of the probes. */
3925 event = apply_pad_offset (pad, event);
3927 case GST_EVENT_RECONFIGURE:
3928 if (GST_PAD_IS_SINK (pad))
3929 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
3934 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3935 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
3940 /* send probes after modifying the events above */
3941 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
3943 /* now check the peer pad */
3944 peerpad = GST_PAD_PEER (pad);
3945 if (peerpad == NULL)
3948 gst_object_ref (peerpad);
3950 GST_OBJECT_UNLOCK (pad);
3952 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
3953 event, GST_EVENT_TYPE_NAME (event), peerpad);
3955 result = gst_pad_send_event (peerpad, event);
3957 /* Note: we gave away ownership of the event at this point but we can still
3958 * print the old pointer */
3959 GST_LOG_OBJECT (pad,
3960 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
3963 gst_object_unref (peerpad);
3965 GST_OBJECT_LOCK (pad);
3968 pad->priv->events[idx].received = TRUE;
3969 GST_DEBUG_OBJECT (pad, "event cleared pending");
3971 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3972 GST_DEBUG_OBJECT (pad, "mark pending events");
3976 if (pad->priv->using == 0) {
3977 /* pad is not active anymore, trigger idle callbacks */
3978 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3979 idle_probe_stopped, GST_FLOW_OK);
3981 GST_OBJECT_UNLOCK (pad);
3983 return result | stored;
3985 /* ERROR handling */
3988 g_warning ("pad %s:%s pushing %s event in wrong direction",
3989 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
3990 gst_event_unref (event);
3995 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3996 gst_event_unref (event);
4001 GST_DEBUG_OBJECT (pad, "We're flushing");
4002 GST_OBJECT_UNLOCK (pad);
4003 gst_event_unref (event);
4008 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4009 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4010 GST_OBJECT_UNLOCK (pad);
4011 gst_event_unref (event);
4016 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4017 GST_OBJECT_UNLOCK (pad);
4018 gst_event_unref (event);
4023 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4024 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4025 GST_OBJECT_UNLOCK (pad);
4026 gst_event_unref (event);
4031 /* Check if we can call the event function with the given event */
4032 static GstFlowReturn
4033 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4035 GstCaps *caps, *templ;
4037 switch (GST_EVENT_TYPE (event)) {
4038 case GST_EVENT_CAPS:
4040 /* backwards compatibility mode for caps */
4041 gst_event_parse_caps (event, &caps);
4043 /* See if pad accepts the caps */
4044 templ = gst_pad_get_pad_template_caps (pad);
4045 if (!gst_caps_is_subset (caps, templ))
4048 gst_caps_unref (templ);
4059 gst_caps_unref (templ);
4060 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4061 "caps %" GST_PTR_FORMAT " not accepted", caps);
4062 return GST_FLOW_NOT_NEGOTIATED;
4067 * gst_pad_send_event:
4068 * @pad: a #GstPad to send the event to.
4069 * @event: (transfer full): the #GstEvent to send to the pad.
4071 * Sends the event to the pad. This function can be used
4072 * by applications to send events in the pipeline.
4074 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4075 * sink pad, @event should be a downstream event. For example, you would not
4076 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4077 * Furthermore, some downstream events have to be serialized with data flow,
4078 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4079 * the event needs to be serialized with data flow, this function will take the
4080 * pad's stream lock while calling its event function.
4082 * To find out whether an event type is upstream, downstream, or downstream and
4083 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4084 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4085 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4086 * plugin doesn't need to bother itself with this information; the core handles
4087 * all necessary locks and checks.
4089 * This function takes owership of the provided event so you should
4090 * gst_event_ref() it if you want to reuse the event after this call.
4092 * Returns: TRUE if the event was handled.
4095 gst_pad_send_event (GstPad * pad, GstEvent * event)
4098 gboolean result = FALSE;
4099 gboolean serialized, need_unlock = FALSE, sticky;
4100 GstPadEventFunction eventfunc;
4102 GstPadProbeType type;
4104 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4105 g_return_val_if_fail (event != NULL, FALSE);
4107 if (GST_PAD_IS_SINK (pad)) {
4108 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4109 goto wrong_direction;
4110 serialized = GST_EVENT_IS_SERIALIZED (event);
4111 sticky = GST_EVENT_IS_STICKY (event);
4112 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4113 } else if (GST_PAD_IS_SRC (pad)) {
4114 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4115 goto wrong_direction;
4116 /* events on srcpad never are serialized and sticky */
4117 serialized = sticky = FALSE;
4118 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4120 goto unknown_direction;
4122 GST_OBJECT_LOCK (pad);
4123 switch (GST_EVENT_TYPE (event)) {
4124 case GST_EVENT_FLUSH_START:
4125 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4126 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4128 /* can't even accept a flush begin event when flushing */
4129 if (GST_PAD_IS_FLUSHING (pad))
4132 GST_PAD_SET_FLUSHING (pad);
4133 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4135 case GST_EVENT_FLUSH_STOP:
4136 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4137 GST_PAD_UNSET_FLUSHING (pad);
4138 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4140 /* Remove pending EOS events */
4141 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4142 clear_event (pad, GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4144 GST_OBJECT_UNLOCK (pad);
4145 /* grab stream lock */
4146 GST_PAD_STREAM_LOCK (pad);
4148 GST_OBJECT_LOCK (pad);
4149 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4152 case GST_EVENT_RECONFIGURE:
4153 if (GST_PAD_IS_SRC (pad))
4154 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4156 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4157 GST_EVENT_TYPE_NAME (event));
4159 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4163 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4164 GST_OBJECT_UNLOCK (pad);
4165 GST_PAD_STREAM_LOCK (pad);
4167 GST_OBJECT_LOCK (pad);
4168 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4172 switch (GST_EVENT_TYPE (event)) {
4173 case GST_EVENT_SEGMENT:
4174 event = apply_pad_offset (pad, event);
4180 /* now do the probe */
4182 type | GST_PAD_PROBE_TYPE_PUSH |
4183 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4185 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4189 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4192 ACQUIRE_PARENT (pad, parent, no_parent);
4193 GST_OBJECT_UNLOCK (pad);
4195 if (G_UNLIKELY (pre_eventfunc_check (pad, event) != GST_FLOW_OK))
4196 goto precheck_failed;
4199 gst_event_ref (event);
4201 result = eventfunc (pad, parent, event);
4203 RELEASE_PARENT (parent);
4207 /* after the event function accepted the event, we can store the sticky
4208 * event on the pad */
4209 gst_pad_store_sticky_event (pad, event, FALSE);
4211 gst_event_unref (event);
4215 GST_PAD_STREAM_UNLOCK (pad);
4217 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4221 /* ERROR handling */
4224 g_warning ("pad %s:%s sending %s event in wrong direction",
4225 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4226 gst_event_unref (event);
4231 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4232 gst_event_unref (event);
4237 g_warning ("pad %s:%s has no event handler, file a bug.",
4238 GST_DEBUG_PAD_NAME (pad));
4239 GST_OBJECT_UNLOCK (pad);
4241 GST_PAD_STREAM_UNLOCK (pad);
4242 gst_event_unref (event);
4247 GST_DEBUG_OBJECT (pad, "pre event check failed");
4248 RELEASE_PARENT (parent);
4250 GST_PAD_STREAM_UNLOCK (pad);
4251 gst_event_unref (event);
4256 GST_DEBUG_OBJECT (pad, "no parent");
4257 GST_OBJECT_UNLOCK (pad);
4259 GST_PAD_STREAM_UNLOCK (pad);
4260 gst_event_unref (event);
4265 GST_OBJECT_UNLOCK (pad);
4267 GST_PAD_STREAM_UNLOCK (pad);
4268 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4269 "Received event on flushing pad. Discarding");
4270 gst_event_unref (event);
4275 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4276 GST_OBJECT_UNLOCK (pad);
4278 GST_PAD_STREAM_UNLOCK (pad);
4279 gst_event_unref (event);
4285 * gst_pad_set_element_private:
4286 * @pad: the #GstPad to set the private data of.
4287 * @priv: The private data to attach to the pad.
4289 * Set the given private data gpointer on the pad.
4290 * This function can only be used by the element that owns the pad.
4291 * No locking is performed in this function.
4294 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4296 pad->element_private = priv;
4300 * gst_pad_get_element_private:
4301 * @pad: the #GstPad to get the private data of.
4303 * Gets the private data of a pad.
4304 * No locking is performed in this function.
4306 * Returns: (transfer none): a #gpointer to the private data.
4309 gst_pad_get_element_private (GstPad * pad)
4311 return pad->element_private;
4315 * gst_pad_get_sticky_event:
4316 * @pad: the #GstPad to get the event from.
4317 * @event_type: the #GstEventType that should be retrieved.
4319 * Returns a new reference of the sticky event of type @event_type
4322 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4325 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4327 GstEvent *event = NULL;
4330 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4331 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4333 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4335 GST_OBJECT_LOCK (pad);
4336 if ((event = pad->priv->events[idx].event)) {
4337 gst_event_ref (event);
4339 GST_OBJECT_UNLOCK (pad);
4345 * gst_pad_sticky_events_foreach:
4346 * @pad: the #GstPad that should be used for iteration.
4347 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4348 * @user_data: (closure): the optional user data.
4350 * Iterates all active sticky events on @pad and calls @foreach_func for every
4351 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4352 * is immediately stopped.
4354 * Returns: GST_FLOW_OK if iteration was successful
4357 gst_pad_sticky_events_foreach (GstPad * pad,
4358 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4360 GstFlowReturn ret = GST_FLOW_OK;
4364 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4365 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4367 GST_OBJECT_LOCK (pad);
4370 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4374 ev = &pad->priv->events[i];
4376 /* skip without active event */
4377 if ((event = ev->event) == NULL)
4380 gst_event_ref (event);
4381 GST_OBJECT_UNLOCK (pad);
4383 res = foreach_func (pad, event, user_data);
4385 GST_OBJECT_LOCK (pad);
4386 gst_event_unref (event);
4388 if (res != GST_FLOW_OK) {
4393 /* things could have changed while we release the lock, check if we still
4394 * are handling the same event, if we don't something changed and we have
4395 * to try again. FIXME. we need a cookie here. */
4396 if (event != ev->event) {
4397 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4401 GST_OBJECT_UNLOCK (pad);
4407 do_stream_status (GstPad * pad, GstStreamStatusType type,
4408 GThread * thread, GstTask * task)
4412 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4414 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4415 if (GST_IS_ELEMENT (parent)) {
4416 GstMessage *message;
4417 GValue value = { 0 };
4419 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4420 gchar *tname, *ename, *pname;
4422 /* create a good task name */
4423 ename = gst_element_get_name (parent);
4424 pname = gst_pad_get_name (pad);
4425 tname = g_strdup_printf ("%s:%s", ename, pname);
4429 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4433 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4436 g_value_init (&value, GST_TYPE_TASK);
4437 g_value_set_object (&value, task);
4438 gst_message_set_stream_status_object (message, &value);
4439 g_value_unset (&value);
4441 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4442 gst_element_post_message (parent, message);
4444 gst_object_unref (parent);
4449 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4451 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4456 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4458 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4462 static GstTaskThreadCallbacks thr_callbacks = {
4468 * gst_pad_start_task:
4469 * @pad: the #GstPad to start the task of
4470 * @func: the task function to call
4471 * @data: data passed to the task function
4473 * Starts a task that repeatedly calls @func with @data. This function
4474 * is mostly used in pad activation functions to start the dataflow.
4475 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4476 * before @func is called.
4478 * Returns: a %TRUE if the task could be started.
4481 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4486 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4487 g_return_val_if_fail (func != NULL, FALSE);
4489 GST_DEBUG_OBJECT (pad, "start task");
4491 GST_OBJECT_LOCK (pad);
4492 task = GST_PAD_TASK (pad);
4494 task = gst_task_new (func, data);
4495 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4496 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4497 GST_DEBUG_OBJECT (pad, "created task");
4498 GST_PAD_TASK (pad) = task;
4499 gst_object_ref (task);
4500 /* release lock to post the message */
4501 GST_OBJECT_UNLOCK (pad);
4503 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4505 gst_object_unref (task);
4507 GST_OBJECT_LOCK (pad);
4508 /* nobody else is supposed to have changed the pad now */
4509 if (GST_PAD_TASK (pad) != task)
4510 goto concurrent_stop;
4512 res = gst_task_set_state (task, GST_TASK_STARTED);
4513 GST_OBJECT_UNLOCK (pad);
4520 GST_OBJECT_UNLOCK (pad);
4526 * gst_pad_pause_task:
4527 * @pad: the #GstPad to pause the task of
4529 * Pause the task of @pad. This function will also wait until the
4530 * function executed by the task is finished if this function is not
4531 * called from the task function.
4533 * Returns: a TRUE if the task could be paused or FALSE when the pad
4537 gst_pad_pause_task (GstPad * pad)
4542 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4544 GST_DEBUG_OBJECT (pad, "pause task");
4546 GST_OBJECT_LOCK (pad);
4547 task = GST_PAD_TASK (pad);
4550 res = gst_task_set_state (task, GST_TASK_PAUSED);
4551 GST_OBJECT_UNLOCK (pad);
4553 /* wait for task function to finish, this lock is recursive so it does nothing
4554 * when the pause is called from the task itself */
4555 GST_PAD_STREAM_LOCK (pad);
4556 GST_PAD_STREAM_UNLOCK (pad);
4562 GST_DEBUG_OBJECT (pad, "pad has no task");
4563 GST_OBJECT_UNLOCK (pad);
4569 * gst_pad_stop_task:
4570 * @pad: the #GstPad to stop the task of
4572 * Stop the task of @pad. This function will also make sure that the
4573 * function executed by the task will effectively stop if not called
4574 * from the GstTaskFunction.
4576 * This function will deadlock if called from the GstTaskFunction of
4577 * the task. Use gst_task_pause() instead.
4579 * Regardless of whether the pad has a task, the stream lock is acquired and
4580 * released so as to ensure that streaming through this pad has finished.
4582 * Returns: a TRUE if the task could be stopped or FALSE on error.
4585 gst_pad_stop_task (GstPad * pad)
4590 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4592 GST_DEBUG_OBJECT (pad, "stop task");
4594 GST_OBJECT_LOCK (pad);
4595 task = GST_PAD_TASK (pad);
4598 GST_PAD_TASK (pad) = NULL;
4599 res = gst_task_set_state (task, GST_TASK_STOPPED);
4600 GST_OBJECT_UNLOCK (pad);
4602 GST_PAD_STREAM_LOCK (pad);
4603 GST_PAD_STREAM_UNLOCK (pad);
4605 if (!gst_task_join (task))
4608 gst_object_unref (task);
4614 GST_DEBUG_OBJECT (pad, "no task");
4615 GST_OBJECT_UNLOCK (pad);
4617 GST_PAD_STREAM_LOCK (pad);
4618 GST_PAD_STREAM_UNLOCK (pad);
4620 /* this is not an error */
4625 /* this is bad, possibly the application tried to join the task from
4626 * the task's thread. We install the task again so that it will be stopped
4627 * again from the right thread next time hopefully. */
4628 GST_OBJECT_LOCK (pad);
4629 GST_DEBUG_OBJECT (pad, "join failed");
4630 /* we can only install this task if there was no other task */
4631 if (GST_PAD_TASK (pad) == NULL)
4632 GST_PAD_TASK (pad) = task;
4633 GST_OBJECT_UNLOCK (pad);