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 (PadEvent events[], guint idx)
330 gst_event_replace (&events[idx].event, NULL);
331 gst_event_replace (&events[idx].pending, NULL);
334 /* called when setting the pad inactive. It removes all sticky events from
337 clear_events (PadEvent events[])
341 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
342 clear_event (events, i);
345 /* The sticky event with @idx from the srcpad is copied to the
346 * pending event on the sinkpad (when different).
347 * This function applies the pad offsets in case of segment events.
348 * This will make sure that we send the event to the sinkpad event
349 * function when the next buffer of event arrives.
350 * Should be called with the OBJECT lock of both pads.
351 * This function returns TRUE when there is a pending event on the
354 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
356 PadEvent *srcev, *sinkev;
358 gboolean pending = FALSE;
360 srcev = &srcpad->priv->events[idx];
362 if ((event = srcev->event)) {
363 sinkev = &sinkpad->priv->events[idx];
365 switch (GST_EVENT_TYPE (event)) {
366 case GST_EVENT_SEGMENT:
371 offset = srcpad->offset + sinkpad->offset;
373 gst_event_copy_segment (event, &segment);
374 /* adjust the base time. FIXME, check negative times, try to tweak the
375 * start to do clipping on negative times */
376 segment.base += offset;
377 /* make a new event from the updated segment */
378 event = gst_event_new_segment (&segment);
385 if (sinkev->event != event) {
386 /* put in the pending entry when different */
387 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, srcpad,
388 "Putting event %p (%s) on pad %s:%s", event,
389 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (sinkpad));
390 gst_event_replace (&sinkev->pending, event);
399 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
404 /* make sure we push the events from the source to this new peer, for this we
405 * copy the events on the sinkpad and mark EVENTS_PENDING */
407 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
408 pending |= replace_event (srcpad, sinkpad, i);
410 /* we had some new pending events, set our flag */
412 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_NEED_EVENTS);
415 /* should be called with the OBJECT_LOCK */
417 get_pad_caps (GstPad * pad)
419 GstCaps *caps = NULL;
423 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
424 /* we can only use the caps when we have successfully send the caps
425 * event to the event function and is thus in the active entry */
426 if ((event = pad->priv->events[idx].event))
427 gst_event_parse_caps (event, &caps);
433 gst_pad_dispose (GObject * object)
435 GstPad *pad = GST_PAD_CAST (object);
438 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
440 /* unlink the peer pad */
441 if ((peer = gst_pad_get_peer (pad))) {
442 /* window for MT unsafeness, someone else could unlink here
443 * and then we call unlink with wrong pads. The unlink
444 * function would catch this and safely return failed. */
445 if (GST_PAD_IS_SRC (pad))
446 gst_pad_unlink (pad, peer);
448 gst_pad_unlink (peer, pad);
450 gst_object_unref (peer);
453 gst_pad_set_pad_template (pad, NULL);
455 clear_events (pad->priv->events);
457 g_hook_list_clear (&pad->probes);
459 G_OBJECT_CLASS (parent_class)->dispose (object);
463 gst_pad_finalize (GObject * object)
465 GstPad *pad = GST_PAD_CAST (object);
468 /* in case the task is still around, clean it up */
469 if ((task = GST_PAD_TASK (pad))) {
470 gst_task_join (task);
471 GST_PAD_TASK (pad) = NULL;
472 gst_object_unref (task);
475 g_static_rec_mutex_free (&pad->stream_rec_lock);
476 g_cond_free (pad->block_cond);
478 G_OBJECT_CLASS (parent_class)->finalize (object);
482 gst_pad_set_property (GObject * object, guint prop_id,
483 const GValue * value, GParamSpec * pspec)
485 g_return_if_fail (GST_IS_PAD (object));
488 case PAD_PROP_DIRECTION:
489 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
491 case PAD_PROP_TEMPLATE:
492 gst_pad_set_pad_template (GST_PAD_CAST (object),
493 (GstPadTemplate *) g_value_get_object (value));
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gst_pad_get_property (GObject * object, guint prop_id,
503 GValue * value, GParamSpec * pspec)
505 g_return_if_fail (GST_IS_PAD (object));
509 GST_OBJECT_LOCK (object);
510 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
511 GST_OBJECT_UNLOCK (object);
513 case PAD_PROP_DIRECTION:
514 g_value_set_enum (value, GST_PAD_DIRECTION (object));
516 case PAD_PROP_TEMPLATE:
517 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
527 * @name: the name of the new pad.
528 * @direction: the #GstPadDirection of the pad.
530 * Creates a new pad with the given name in the given direction.
531 * If name is NULL, a guaranteed unique name (across all pads)
533 * This function makes a copy of the name so you can safely free the name.
535 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
540 gst_pad_new (const gchar * name, GstPadDirection direction)
542 return g_object_new (GST_TYPE_PAD,
543 "name", name, "direction", direction, NULL);
547 * gst_pad_new_from_template:
548 * @templ: the pad template to use
549 * @name: the name of the element
551 * Creates a new pad with the given name from the given template.
552 * If name is NULL, a guaranteed unique name (across all pads)
554 * This function makes a copy of the name so you can safely free the name.
556 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
559 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
561 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
563 return g_object_new (GST_TYPE_PAD,
564 "name", name, "direction", templ->direction, "template", templ, NULL);
568 * gst_pad_new_from_static_template:
569 * @templ: the #GstStaticPadTemplate to use
570 * @name: the name of the element
572 * Creates a new pad with the given name from the given static template.
573 * If name is NULL, a guaranteed unique name (across all pads)
575 * This function makes a copy of the name so you can safely free the name.
577 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
580 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
584 GstPadTemplate *template;
586 template = gst_static_pad_template_get (templ);
587 pad = gst_pad_new_from_template (template, name);
588 gst_object_unref (template);
592 #define ACQUIRE_PARENT(pad, parent, label) \
594 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
595 gst_object_ref (parent); \
596 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
600 #define RELEASE_PARENT(parent) \
602 if (G_LIKELY (parent)) \
603 gst_object_unref (parent); \
607 * gst_pad_get_direction:
608 * @pad: a #GstPad to get the direction of.
610 * Gets the direction of the pad. The direction of the pad is
611 * decided at construction time so this function does not take
614 * Returns: the #GstPadDirection of the pad.
619 gst_pad_get_direction (GstPad * pad)
621 GstPadDirection result;
623 /* PAD_UNKNOWN is a little silly but we need some sort of
624 * error return value */
625 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
627 result = GST_PAD_DIRECTION (pad);
633 gst_pad_activate_default (GstPad * pad, GstObject * parent)
635 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
639 pre_activate (GstPad * pad, GstPadMode new_mode)
642 case GST_PAD_MODE_NONE:
643 GST_OBJECT_LOCK (pad);
644 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
645 GST_PAD_SET_FLUSHING (pad);
646 GST_PAD_MODE (pad) = new_mode;
647 /* unlock blocked pads so element can resume and stop */
648 GST_PAD_BLOCK_BROADCAST (pad);
649 GST_OBJECT_UNLOCK (pad);
651 case GST_PAD_MODE_PUSH:
652 case GST_PAD_MODE_PULL:
653 GST_OBJECT_LOCK (pad);
654 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
655 GST_PAD_UNSET_FLUSHING (pad);
656 GST_PAD_MODE (pad) = new_mode;
657 GST_OBJECT_UNLOCK (pad);
663 post_activate (GstPad * pad, GstPadMode new_mode)
666 case GST_PAD_MODE_NONE:
667 /* ensures that streaming stops */
668 GST_PAD_STREAM_LOCK (pad);
669 GST_DEBUG_OBJECT (pad, "stopped streaming");
670 GST_OBJECT_LOCK (pad);
671 clear_events (pad->priv->events);
672 GST_OBJECT_UNLOCK (pad);
673 GST_PAD_STREAM_UNLOCK (pad);
675 case GST_PAD_MODE_PUSH:
676 case GST_PAD_MODE_PULL:
683 * gst_pad_set_active:
684 * @pad: the #GstPad to activate or deactivate.
685 * @active: whether or not the pad should be active.
687 * Activates or deactivates the given pad.
688 * Normally called from within core state change functions.
690 * If @active, makes sure the pad is active. If it is already active, either in
691 * push or pull mode, just return. Otherwise dispatches to the pad's activate
692 * function to perform the actual activation.
694 * If not @active, checks the pad's current mode and calls
695 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
698 * Returns: #TRUE if the operation was successful.
703 gst_pad_set_active (GstPad * pad, gboolean active)
707 gboolean ret = FALSE;
709 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
711 GST_OBJECT_LOCK (pad);
712 old = GST_PAD_MODE (pad);
713 ACQUIRE_PARENT (pad, parent, no_parent);
714 GST_OBJECT_UNLOCK (pad);
717 if (old == GST_PAD_MODE_NONE) {
718 GST_DEBUG_OBJECT (pad, "activating pad from none");
719 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
721 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
725 if (old == GST_PAD_MODE_NONE) {
726 GST_DEBUG_OBJECT (pad, "pad was inactive");
729 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
730 ret = gst_pad_activate_mode (pad, old, FALSE);
734 RELEASE_PARENT (parent);
736 if (G_UNLIKELY (!ret))
740 GST_OBJECT_LOCK (pad);
741 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
742 GST_OBJECT_UNLOCK (pad);
749 GST_DEBUG_OBJECT (pad, "no parent");
750 GST_OBJECT_UNLOCK (pad);
755 GST_OBJECT_LOCK (pad);
757 g_critical ("Failed to deactivate pad %s:%s, very bad",
758 GST_DEBUG_PAD_NAME (pad));
760 GST_WARNING_OBJECT (pad, "Failed to activate pad");
762 GST_OBJECT_UNLOCK (pad);
768 * gst_pad_activate_mode:
769 * @pad: the #GstPad to activate or deactivate.
770 * @mode: the requested activation mode
771 * @active: whether or not the pad should be active.
773 * Activates or deactivates the given pad in @mode via dispatching to the
774 * pad's activatemodefunc. For use from within pad activation functions only.
776 * If you don't know what this is, you probably don't want to call it.
778 * Returns: TRUE if the operation was successful.
783 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
785 gboolean res = FALSE;
791 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
793 GST_OBJECT_LOCK (pad);
794 old = GST_PAD_MODE (pad);
795 dir = GST_PAD_DIRECTION (pad);
796 ACQUIRE_PARENT (pad, parent, no_parent);
797 GST_OBJECT_UNLOCK (pad);
799 new = active ? mode : GST_PAD_MODE_NONE;
804 if (active && old != mode) {
805 /* pad was activate in the wrong direction, deactivate it
806 * and reactivate it in the requested mode */
807 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
808 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
809 goto deactivate_failed;
813 case GST_PAD_MODE_PULL:
815 if (dir == GST_PAD_SINK) {
816 if ((peer = gst_pad_get_peer (pad))) {
817 GST_DEBUG_OBJECT (pad, "calling peer");
818 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
820 gst_object_unref (peer);
822 /* there is no peer, this is only fatal when we activate. When we
823 * deactivate, we must assume the application has unlinked the peer and
824 * will deactivate it eventually. */
828 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
831 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
832 goto failure; /* Can't activate pull on a src without a
841 pre_activate (pad, new);
843 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
844 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
848 /* can happen for sinks of passthrough elements */
851 post_activate (pad, new);
853 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
854 active ? "activated" : "deactivated", mode);
859 RELEASE_PARENT (parent);
865 GST_DEBUG_OBJECT (pad, "no parent");
866 GST_OBJECT_UNLOCK (pad);
871 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
872 active ? "activated" : "deactivated", mode);
877 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
878 "failed to %s in switch to mode %d from mode %d",
879 (active ? "activate" : "deactivate"), mode, old);
884 GST_OBJECT_LOCK (peer);
885 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
886 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
887 GST_OBJECT_UNLOCK (peer);
888 gst_object_unref (peer);
893 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
899 GST_OBJECT_LOCK (pad);
900 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
901 active ? "activate" : "deactivate", mode);
902 GST_PAD_SET_FLUSHING (pad);
903 GST_PAD_MODE (pad) = old;
904 GST_OBJECT_UNLOCK (pad);
911 * @pad: the #GstPad to query
913 * Query if a pad is active
915 * Returns: TRUE if the pad is active.
920 gst_pad_is_active (GstPad * pad)
922 gboolean result = FALSE;
924 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
926 GST_OBJECT_LOCK (pad);
927 result = GST_PAD_IS_ACTIVE (pad);
928 GST_OBJECT_UNLOCK (pad);
935 * @pad: the #GstPad to add the probe to
936 * @mask: the probe mask
937 * @callback: #GstPadProbeCallback that will be called with notifications of
939 * @user_data: (closure): user data passed to the callback
940 * @destroy_data: #GDestroyNotify for user_data
942 * Be notified of different states of pads. The provided callback is called for
943 * every state that matches @mask.
945 * Returns: an id or 0 on error. The id can be used to remove the probe with
946 * gst_pad_remove_probe().
951 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
952 GstPadProbeCallback callback, gpointer user_data,
953 GDestroyNotify destroy_data)
958 g_return_val_if_fail (GST_IS_PAD (pad), 0);
959 g_return_val_if_fail (mask != 0, 0);
961 GST_OBJECT_LOCK (pad);
963 /* make a new probe */
964 hook = g_hook_alloc (&pad->probes);
966 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
969 /* when no contraints are given for the types, assume all types are
971 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
972 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
973 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
974 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
976 /* store our flags and other fields */
977 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
978 hook->func = callback;
979 hook->data = user_data;
980 hook->destroy = destroy_data;
981 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
984 g_hook_prepend (&pad->probes, hook);
986 /* incremenent cookie so that the new hook get's called */
987 pad->priv->probe_list_cookie++;
989 /* get the id of the hook, we return this and it can be used to remove the
993 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
995 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
996 /* we have a block probe */
998 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
999 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1000 "now %d blocking probes", pad->num_blocked);
1003 /* call the callback if we need to be called for idle callbacks */
1004 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1005 if (pad->priv->using > 0) {
1006 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1007 * flag above, the last thread to leave the push will do the callback. New
1008 * threads going into the push will block. */
1009 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1010 "pad is in use, delay idle callback");
1011 GST_OBJECT_UNLOCK (pad);
1013 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, };
1015 /* the pad is idle now, we can signal the idle callback now */
1016 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1017 "pad is idle, trigger idle callback");
1018 GST_OBJECT_UNLOCK (pad);
1020 callback (pad, &info, user_data);
1023 GST_OBJECT_UNLOCK (pad);
1029 cleanup_hook (GstPad * pad, GHook * hook)
1031 GstPadProbeType type;
1033 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1035 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1036 /* unblock when we remove the last blocking probe */
1038 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1040 if (pad->num_blocked == 0) {
1041 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1042 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1043 GST_PAD_BLOCK_BROADCAST (pad);
1046 g_hook_destroy_link (&pad->probes, hook);
1051 * gst_pad_remove_probe:
1052 * @pad: the #GstPad with the probe
1053 * @id: the probe id to remove
1055 * Remove the probe with @id from @pad.
1060 gst_pad_remove_probe (GstPad * pad, gulong id)
1064 g_return_if_fail (GST_IS_PAD (pad));
1066 GST_OBJECT_LOCK (pad);
1068 hook = g_hook_get (&pad->probes, id);
1072 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1074 cleanup_hook (pad, hook);
1075 GST_OBJECT_UNLOCK (pad);
1081 GST_OBJECT_UNLOCK (pad);
1082 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1088 * gst_pad_is_blocked:
1089 * @pad: the #GstPad to query
1091 * Checks if the pad is blocked or not. This function returns the
1092 * last requested state of the pad. It is not certain that the pad
1093 * is actually blocking at this point (see gst_pad_is_blocking()).
1095 * Returns: TRUE if the pad is blocked.
1100 gst_pad_is_blocked (GstPad * pad)
1102 gboolean result = FALSE;
1104 g_return_val_if_fail (GST_IS_PAD (pad), result);
1106 GST_OBJECT_LOCK (pad);
1107 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1108 GST_OBJECT_UNLOCK (pad);
1114 * gst_pad_is_blocking:
1115 * @pad: the #GstPad to query
1117 * Checks if the pad is blocking or not. This is a guaranteed state
1118 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1120 * Returns: TRUE if the pad is blocking.
1127 gst_pad_is_blocking (GstPad * pad)
1129 gboolean result = FALSE;
1131 g_return_val_if_fail (GST_IS_PAD (pad), result);
1133 GST_OBJECT_LOCK (pad);
1134 /* the blocking flag is only valid if the pad is not flushing */
1135 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1136 GST_OBJECT_UNLOCK (pad);
1142 * gst_pad_check_reconfigure:
1143 * @pad: the #GstPad to check
1145 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1146 * if the flag was set.
1148 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1151 gst_pad_check_reconfigure (GstPad * pad)
1153 gboolean reconfigure;
1155 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1157 GST_OBJECT_LOCK (pad);
1158 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1159 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1160 GST_OBJECT_UNLOCK (pad);
1166 * gst_pad_mark_reconfigure:
1167 * @pad: the #GstPad to mark
1169 * Mark a pad for needing reconfiguration. The next call to
1170 * gst_pad_check_reconfigure() will return %TRUE after this call.
1173 gst_pad_mark_reconfigure (GstPad * pad)
1175 g_return_if_fail (GST_IS_PAD (pad));
1177 GST_OBJECT_LOCK (pad);
1178 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1179 GST_OBJECT_UNLOCK (pad);
1183 * gst_pad_set_activate_function:
1185 * @activate: the #GstPadActivateFunction to set.
1187 * Sets the given activate function for @pad. The activate function will
1188 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1189 * the actual activation. Only makes sense to set on sink pads.
1191 * Call this function if your sink pad can start a pull-based task.
1194 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1196 g_return_if_fail (GST_IS_PAD (pad));
1198 GST_PAD_ACTIVATEFUNC (pad) = activate;
1199 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1200 GST_DEBUG_FUNCPTR_NAME (activate));
1204 * gst_pad_set_activatemode_function:
1206 * @activatemode: the #GstPadActivateModeFunction to set.
1208 * Sets the given activate_mode function for the pad. An activate_mode function
1209 * prepares the element for data passing.
1212 gst_pad_set_activatemode_function (GstPad * pad,
1213 GstPadActivateModeFunction activatemode)
1215 g_return_if_fail (GST_IS_PAD (pad));
1217 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1218 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1219 GST_DEBUG_FUNCPTR_NAME (activatemode));
1223 * gst_pad_set_chain_function:
1224 * @pad: a sink #GstPad.
1225 * @chain: the #GstPadChainFunction to set.
1227 * Sets the given chain function for the pad. The chain function is called to
1228 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1231 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1233 g_return_if_fail (GST_IS_PAD (pad));
1234 g_return_if_fail (GST_PAD_IS_SINK (pad));
1236 GST_PAD_CHAINFUNC (pad) = chain;
1237 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1238 GST_DEBUG_FUNCPTR_NAME (chain));
1242 * gst_pad_set_chain_list_function:
1243 * @pad: a sink #GstPad.
1244 * @chainlist: the #GstPadChainListFunction to set.
1246 * Sets the given chain list function for the pad. The chainlist function is
1247 * called to process a #GstBufferList input buffer list. See
1248 * #GstPadChainListFunction for more details.
1253 gst_pad_set_chain_list_function (GstPad * pad,
1254 GstPadChainListFunction chainlist)
1256 g_return_if_fail (GST_IS_PAD (pad));
1257 g_return_if_fail (GST_PAD_IS_SINK (pad));
1259 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1260 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1261 GST_DEBUG_FUNCPTR_NAME (chainlist));
1265 * gst_pad_set_getrange_function:
1266 * @pad: a source #GstPad.
1267 * @get: the #GstPadGetRangeFunction to set.
1269 * Sets the given getrange function for the pad. The getrange function is
1270 * called to produce a new #GstBuffer to start the processing pipeline. see
1271 * #GstPadGetRangeFunction for a description of the getrange function.
1274 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1276 g_return_if_fail (GST_IS_PAD (pad));
1277 g_return_if_fail (GST_PAD_IS_SRC (pad));
1279 GST_PAD_GETRANGEFUNC (pad) = get;
1281 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1282 GST_DEBUG_FUNCPTR_NAME (get));
1286 * gst_pad_set_event_function:
1287 * @pad: a #GstPad of either direction.
1288 * @event: the #GstPadEventFunction to set.
1290 * Sets the given event handler for the pad.
1293 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1295 g_return_if_fail (GST_IS_PAD (pad));
1297 GST_PAD_EVENTFUNC (pad) = event;
1299 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1300 GST_DEBUG_FUNCPTR_NAME (event));
1304 * gst_pad_set_query_function:
1305 * @pad: a #GstPad of either direction.
1306 * @query: the #GstPadQueryFunction to set.
1308 * Set the given query function for the pad.
1311 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1313 g_return_if_fail (GST_IS_PAD (pad));
1315 GST_PAD_QUERYFUNC (pad) = query;
1317 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1318 GST_DEBUG_FUNCPTR_NAME (query));
1322 * gst_pad_set_iterate_internal_links_function:
1323 * @pad: a #GstPad of either direction.
1324 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1326 * Sets the given internal link iterator function for the pad.
1331 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1332 GstPadIterIntLinkFunction iterintlink)
1334 g_return_if_fail (GST_IS_PAD (pad));
1336 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1337 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1338 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1342 * gst_pad_set_link_function:
1344 * @link: the #GstPadLinkFunction to set.
1346 * Sets the given link function for the pad. It will be called when
1347 * the pad is linked with another pad.
1349 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1352 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1353 * cannot be made for some reason.
1355 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1356 * of the peer sink pad, if present.
1359 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1361 g_return_if_fail (GST_IS_PAD (pad));
1363 GST_PAD_LINKFUNC (pad) = link;
1364 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1365 GST_DEBUG_FUNCPTR_NAME (link));
1369 * gst_pad_set_unlink_function:
1371 * @unlink: the #GstPadUnlinkFunction to set.
1373 * Sets the given unlink function for the pad. It will be called
1374 * when the pad is unlinked.
1377 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1379 g_return_if_fail (GST_IS_PAD (pad));
1381 GST_PAD_UNLINKFUNC (pad) = unlink;
1382 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1383 GST_DEBUG_FUNCPTR_NAME (unlink));
1388 * @srcpad: the source #GstPad to unlink.
1389 * @sinkpad: the sink #GstPad to unlink.
1391 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1392 * signal on both pads.
1394 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1395 * the pads were not linked together.
1400 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1402 gboolean result = FALSE;
1403 GstElement *parent = NULL;
1406 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1407 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1408 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1409 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1411 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1412 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1413 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1415 /* We need to notify the parent before taking any pad locks as the bin in
1416 * question might be waiting for a lock on the pad while holding its lock
1417 * that our message will try to take. */
1418 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1419 if (GST_IS_ELEMENT (parent)) {
1420 gst_element_post_message (parent,
1421 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1422 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1424 gst_object_unref (parent);
1429 GST_OBJECT_LOCK (srcpad);
1430 GST_OBJECT_LOCK (sinkpad);
1432 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1433 goto not_linked_together;
1435 if (GST_PAD_UNLINKFUNC (srcpad)) {
1436 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1438 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1439 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1442 /* first clear peers */
1443 GST_PAD_PEER (srcpad) = NULL;
1444 GST_PAD_PEER (sinkpad) = NULL;
1446 /* clear pending caps if any */
1447 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1448 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1450 GST_OBJECT_UNLOCK (sinkpad);
1451 GST_OBJECT_UNLOCK (srcpad);
1453 /* fire off a signal to each of the pads telling them
1454 * that they've been unlinked */
1455 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1456 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1458 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1459 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1464 if (parent != NULL) {
1465 gst_element_post_message (parent,
1466 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1467 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1468 gst_object_unref (parent);
1473 not_linked_together:
1475 /* we do not emit a warning in this case because unlinking cannot
1476 * be made MT safe.*/
1477 GST_OBJECT_UNLOCK (sinkpad);
1478 GST_OBJECT_UNLOCK (srcpad);
1484 * gst_pad_is_linked:
1485 * @pad: pad to check
1487 * Checks if a @pad is linked to another pad or not.
1489 * Returns: TRUE if the pad is linked, FALSE otherwise.
1494 gst_pad_is_linked (GstPad * pad)
1498 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1500 GST_OBJECT_LOCK (pad);
1501 result = (GST_PAD_PEER (pad) != NULL);
1502 GST_OBJECT_UNLOCK (pad);
1507 /* get the caps from both pads and see if the intersection
1510 * This function should be called with the pad LOCK on both
1514 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1515 GstPadLinkCheck flags)
1517 GstCaps *srccaps = NULL;
1518 GstCaps *sinkcaps = NULL;
1519 gboolean compatible = FALSE;
1521 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1524 /* Doing the expensive caps checking takes priority over only checking the template caps */
1525 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1526 GST_OBJECT_UNLOCK (src);
1527 srccaps = gst_pad_query_caps (src, NULL);
1528 GST_OBJECT_LOCK (src);
1529 GST_OBJECT_UNLOCK (sink);
1530 sinkcaps = gst_pad_query_caps (sink, NULL);
1531 GST_OBJECT_LOCK (sink);
1533 /* If one of the two pads doesn't have a template, consider the intersection
1535 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1536 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1540 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1542 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1545 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1547 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1550 /* if we have caps on both pads we can check the intersection. If one
1551 * of the caps is NULL, we return TRUE. */
1552 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1554 gst_caps_unref (srccaps);
1556 gst_caps_unref (sinkcaps);
1560 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1561 gst_caps_unref (srccaps);
1562 gst_caps_unref (sinkcaps);
1565 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1566 (compatible ? "" : "not"));
1571 /* check if the grandparents of both pads are the same.
1572 * This check is required so that we don't try to link
1573 * pads from elements in different bins without ghostpads.
1575 * The LOCK should be held on both pads
1578 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1580 GstObject *psrc, *psink;
1582 psrc = GST_OBJECT_PARENT (src);
1583 psink = GST_OBJECT_PARENT (sink);
1585 /* if one of the pads has no parent, we allow the link */
1586 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1589 /* only care about parents that are elements */
1590 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1591 goto no_element_parent;
1593 /* if the parents are the same, we have a loop */
1594 if (G_UNLIKELY (psrc == psink))
1597 /* if they both have a parent, we check the grandparents. We can not lock
1598 * the parent because we hold on the child (pad) and the locking order is
1599 * parent >> child. */
1600 psrc = GST_OBJECT_PARENT (psrc);
1601 psink = GST_OBJECT_PARENT (psink);
1603 /* if they have grandparents but they are not the same */
1604 if (G_UNLIKELY (psrc != psink))
1605 goto wrong_grandparents;
1612 GST_CAT_DEBUG (GST_CAT_CAPS,
1613 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1614 GST_PTR_FORMAT, psrc, psink);
1619 GST_CAT_DEBUG (GST_CAT_CAPS,
1620 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1621 GST_PTR_FORMAT, psrc, psink);
1626 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1632 GST_CAT_DEBUG (GST_CAT_CAPS,
1633 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1634 GST_PTR_FORMAT, psrc, psink);
1639 /* FIXME leftover from an attempt at refactoring... */
1640 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1641 * the two pads will be locked in the srcpad, sinkpad order. */
1642 static GstPadLinkReturn
1643 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1645 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1646 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1648 GST_OBJECT_LOCK (srcpad);
1650 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1651 goto src_was_linked;
1653 GST_OBJECT_LOCK (sinkpad);
1655 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1656 goto sink_was_linked;
1658 /* check hierarchy, pads can only be linked if the grandparents
1660 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1661 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1662 goto wrong_hierarchy;
1664 /* check pad caps for non-empty intersection */
1665 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1668 /* FIXME check pad scheduling for non-empty intersection */
1670 return GST_PAD_LINK_OK;
1674 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1675 GST_DEBUG_PAD_NAME (srcpad),
1676 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1677 /* we do not emit a warning in this case because unlinking cannot
1678 * be made MT safe.*/
1679 GST_OBJECT_UNLOCK (srcpad);
1680 return GST_PAD_LINK_WAS_LINKED;
1684 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1685 GST_DEBUG_PAD_NAME (sinkpad),
1686 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1687 /* we do not emit a warning in this case because unlinking cannot
1688 * be made MT safe.*/
1689 GST_OBJECT_UNLOCK (sinkpad);
1690 GST_OBJECT_UNLOCK (srcpad);
1691 return GST_PAD_LINK_WAS_LINKED;
1695 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1696 GST_OBJECT_UNLOCK (sinkpad);
1697 GST_OBJECT_UNLOCK (srcpad);
1698 return GST_PAD_LINK_WRONG_HIERARCHY;
1702 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1703 GST_OBJECT_UNLOCK (sinkpad);
1704 GST_OBJECT_UNLOCK (srcpad);
1705 return GST_PAD_LINK_NOFORMAT;
1711 * @srcpad: the source #GstPad.
1712 * @sinkpad: the sink #GstPad.
1714 * Checks if the source pad and the sink pad are compatible so they can be
1717 * Returns: TRUE if the pads can be linked.
1720 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1722 GstPadLinkReturn result;
1724 /* generic checks */
1725 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1726 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1728 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1729 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1731 /* gst_pad_link_prepare does everything for us, we only release the locks
1732 * on the pads that it gets us. If this function returns !OK the locks are not
1734 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1735 if (result != GST_PAD_LINK_OK)
1738 GST_OBJECT_UNLOCK (srcpad);
1739 GST_OBJECT_UNLOCK (sinkpad);
1742 return result == GST_PAD_LINK_OK;
1746 * gst_pad_link_full:
1747 * @srcpad: the source #GstPad to link.
1748 * @sinkpad: the sink #GstPad to link.
1749 * @flags: the checks to validate when linking
1751 * Links the source pad and the sink pad.
1753 * This variant of #gst_pad_link provides a more granular control on the
1754 * checks being done when linking. While providing some considerable speedups
1755 * the caller of this method must be aware that wrong usage of those flags
1756 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1757 * for more information.
1761 * Returns: A result code indicating if the connection worked or
1767 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1769 GstPadLinkReturn result;
1771 GstPadLinkFunction srcfunc, sinkfunc;
1773 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1774 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1775 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1776 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1777 GST_PAD_LINK_WRONG_DIRECTION);
1779 /* Notify the parent early. See gst_pad_unlink for details. */
1780 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
1781 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
1782 gst_element_post_message (parent,
1783 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1784 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
1786 gst_object_unref (parent);
1791 /* prepare will also lock the two pads */
1792 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
1794 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
1797 /* must set peers before calling the link function */
1798 GST_PAD_PEER (srcpad) = sinkpad;
1799 GST_PAD_PEER (sinkpad) = srcpad;
1801 /* make sure we update events */
1802 prepare_event_update (srcpad, sinkpad);
1804 /* get the link functions */
1805 srcfunc = GST_PAD_LINKFUNC (srcpad);
1806 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
1808 if (G_UNLIKELY (srcfunc || sinkfunc)) {
1809 /* custom link functions, execute them */
1810 GST_OBJECT_UNLOCK (sinkpad);
1811 GST_OBJECT_UNLOCK (srcpad);
1814 /* this one will call the peer link function */
1815 result = srcfunc (srcpad, sinkpad);
1816 } else if (sinkfunc) {
1817 /* if no source link function, we need to call the sink link
1818 * function ourselves. */
1819 result = sinkfunc (sinkpad, srcpad);
1822 GST_OBJECT_LOCK (srcpad);
1823 GST_OBJECT_LOCK (sinkpad);
1825 /* we released the lock, check if the same pads are linked still */
1826 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
1827 goto concurrent_link;
1829 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
1832 GST_OBJECT_UNLOCK (sinkpad);
1833 GST_OBJECT_UNLOCK (srcpad);
1835 /* fire off a signal to each of the pads telling them
1836 * that they've been linked */
1837 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
1838 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
1840 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1841 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1843 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
1846 if (G_LIKELY (parent)) {
1847 gst_element_post_message (parent,
1848 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1849 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
1850 gst_object_unref (parent);
1858 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
1859 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1860 GST_OBJECT_UNLOCK (sinkpad);
1861 GST_OBJECT_UNLOCK (srcpad);
1863 /* The other link operation succeeded first */
1864 result = GST_PAD_LINK_WAS_LINKED;
1869 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1870 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1872 GST_PAD_PEER (srcpad) = NULL;
1873 GST_PAD_PEER (sinkpad) = NULL;
1875 GST_OBJECT_UNLOCK (sinkpad);
1876 GST_OBJECT_UNLOCK (srcpad);
1884 * @srcpad: the source #GstPad to link.
1885 * @sinkpad: the sink #GstPad to link.
1887 * Links the source pad and the sink pad.
1889 * Returns: A result code indicating if the connection worked or
1895 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1897 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1901 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1903 GstPadTemplate **template_p;
1905 /* this function would need checks if it weren't static */
1907 GST_OBJECT_LOCK (pad);
1908 template_p = &pad->padtemplate;
1909 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
1910 GST_OBJECT_UNLOCK (pad);
1913 gst_pad_template_pad_created (templ, pad);
1917 * gst_pad_get_pad_template:
1920 * Gets the template for @pad.
1922 * Returns: (transfer full): the #GstPadTemplate from which this pad was
1923 * instantiated, or %NULL if this pad has no template. Unref after
1927 gst_pad_get_pad_template (GstPad * pad)
1929 GstPadTemplate *templ;
1931 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1933 templ = GST_PAD_PAD_TEMPLATE (pad);
1935 return (templ ? gst_object_ref (templ) : NULL);
1939 * gst_pad_has_current_caps:
1940 * @pad: a #GstPad to check
1942 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
1944 * Returns: TRUE when @pad has caps associated with it.
1947 gst_pad_has_current_caps (GstPad * pad)
1952 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1954 GST_OBJECT_LOCK (pad);
1955 caps = get_pad_caps (pad);
1956 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
1957 "check current pad caps %" GST_PTR_FORMAT, caps);
1958 result = (caps != NULL);
1959 GST_OBJECT_UNLOCK (pad);
1965 * gst_pad_get_current_caps:
1966 * @pad: a #GstPad to get the current capabilities of.
1968 * Gets the capabilities currently configured on @pad with the last
1969 * #GST_EVENT_CAPS event.
1971 * Returns: the current caps of the pad with incremented ref-count.
1974 gst_pad_get_current_caps (GstPad * pad)
1978 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1980 GST_OBJECT_LOCK (pad);
1981 if ((result = get_pad_caps (pad)))
1982 gst_caps_ref (result);
1983 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
1984 "get current pad caps %" GST_PTR_FORMAT, result);
1985 GST_OBJECT_UNLOCK (pad);
1992 * @pad: a #GstPad to set the capabilities of.
1993 * @caps: (transfer none): a #GstCaps to set.
1995 * Sets the capabilities of this pad. The caps must be fixed. Any previous
1996 * caps on the pad will be unreffed. This function refs the caps so you should
1997 * unref if as soon as you don't need it anymore.
1998 * It is possible to set NULL caps, which will make the pad unnegotiated
2001 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2002 * or bad parameters were provided to this function.
2007 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2010 gboolean res = TRUE;
2012 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2013 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2015 event = gst_event_new_caps (caps);
2017 if (GST_PAD_IS_SRC (pad))
2018 res = gst_pad_push_event (pad, event);
2020 res = gst_pad_send_event (pad, event);
2026 do_event_function (GstPad * pad, GstObject * parent, GstEvent * event,
2027 GstPadEventFunction eventfunc, gboolean * caps_notify)
2029 gboolean result = TRUE, call_event = TRUE;
2030 GstCaps *caps, *old, *templ;
2032 switch (GST_EVENT_TYPE (event)) {
2033 case GST_EVENT_CAPS:
2035 /* backwards compatibility mode for caps */
2036 gst_event_parse_caps (event, &caps);
2038 /* See if pad accepts the caps */
2039 templ = gst_pad_get_pad_template_caps (pad);
2040 if (!gst_caps_is_subset (caps, templ))
2043 /* check if it changed */
2044 if ((old = gst_pad_get_current_caps (pad))) {
2045 call_event = !gst_caps_is_equal (caps, old);
2046 gst_caps_unref (old);
2049 *caps_notify = TRUE;
2050 gst_caps_unref (templ);
2058 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2059 result = eventfunc (pad, parent, event);
2061 gst_event_unref (event);
2068 gst_caps_unref (templ);
2069 gst_event_unref (event);
2070 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2071 "caps %" GST_PTR_FORMAT " not accepted", caps);
2076 /* function to send all pending events on the sinkpad to the event
2077 * function and collect the results. This function should be called with
2078 * the object lock. The object lock might be released by this function.
2080 static GstFlowReturn
2081 gst_pad_update_events (GstPad * pad)
2084 GstFlowReturn ret = GST_FLOW_OK;
2086 GstPadEventFunction eventfunc;
2088 gboolean caps_notify = FALSE;
2091 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2094 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2097 ev = &pad->priv->events[i];
2099 /* skip without pending event */
2100 if ((event = gst_event_steal (&ev->pending)) == NULL)
2103 ACQUIRE_PARENT (pad, parent, no_parent);
2105 gst_event_ref (event);
2106 GST_OBJECT_UNLOCK (pad);
2108 res = do_event_function (pad, parent, event, eventfunc, &caps_notify);
2110 RELEASE_PARENT (parent);
2112 /* things could have changed while we release the lock, check if we still
2113 * are handling the same event, if we don't something changed and we have
2114 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2115 * that lock eventually and then do the retry elsewhere. */
2118 /* make the event active */
2119 gst_event_take (&ev->event, event);
2121 /* notify caps change when needed */
2123 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2124 caps_notify = FALSE;
2127 gst_event_unref (event);
2128 ret = GST_FLOW_ERROR;
2130 GST_OBJECT_LOCK (pad);
2132 /* when we get here all events were successfully updated. */
2138 g_warning ("pad %s:%s has no event handler, file a bug.",
2139 GST_DEBUG_PAD_NAME (pad));
2140 return GST_FLOW_NOT_SUPPORTED;
2144 GST_DEBUG_OBJECT (pad, "pad has no parent");
2145 gst_event_take (&ev->pending, event);
2146 return GST_FLOW_WRONG_STATE;
2151 * gst_pad_get_pad_template_caps:
2152 * @pad: a #GstPad to get the template capabilities from.
2154 * Gets the capabilities for @pad's template.
2156 * Returns: (transfer full): the #GstCaps of this pad template.
2157 * Unref after usage.
2160 gst_pad_get_pad_template_caps (GstPad * pad)
2162 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2164 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2166 if (GST_PAD_PAD_TEMPLATE (pad))
2167 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2169 return gst_static_caps_get (&anycaps);
2174 * @pad: a #GstPad to get the peer of.
2176 * Gets the peer of @pad. This function refs the peer pad so
2177 * you need to unref it after use.
2179 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2184 gst_pad_get_peer (GstPad * pad)
2188 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2190 GST_OBJECT_LOCK (pad);
2191 result = GST_PAD_PEER (pad);
2193 gst_object_ref (result);
2194 GST_OBJECT_UNLOCK (pad);
2200 * gst_pad_get_allowed_caps:
2203 * Gets the capabilities of the allowed media types that can flow through
2204 * @pad and its peer.
2206 * The allowed capabilities is calculated as the intersection of the results of
2207 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2208 * on the resulting caps.
2210 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2211 * caps when you no longer need it. This function returns NULL when @pad
2217 gst_pad_get_allowed_caps (GstPad * pad)
2224 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2226 GST_OBJECT_LOCK (pad);
2227 peer = GST_PAD_PEER (pad);
2228 if (G_UNLIKELY (peer == NULL))
2231 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2233 gst_object_ref (peer);
2234 GST_OBJECT_UNLOCK (pad);
2235 mycaps = gst_pad_query_caps (pad, NULL);
2237 peercaps = gst_pad_query_caps (peer, NULL);
2238 gst_object_unref (peer);
2240 caps = gst_caps_intersect (mycaps, peercaps);
2241 gst_caps_unref (peercaps);
2242 gst_caps_unref (mycaps);
2244 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2251 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2252 GST_OBJECT_UNLOCK (pad);
2259 * gst_pad_iterate_internal_links_default:
2260 * @pad: the #GstPad to get the internal links of.
2261 * @parent: the parent of @pad or NULL
2263 * Iterate the list of pads to which the given pad is linked to inside of
2264 * the parent element.
2265 * This is the default handler, and thus returns an iterator of all of the
2266 * pads inside the parent element with opposite direction.
2268 * The caller must free this iterator after use with gst_iterator_free().
2270 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2271 * returned pad with gst_object_unref().
2276 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2283 GstElement *eparent;
2285 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2287 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2288 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2290 GST_OBJECT_LOCK (pad);
2291 eparent = GST_PAD_PARENT (pad);
2292 if (!eparent || !GST_IS_ELEMENT (eparent))
2295 gst_object_ref (eparent);
2296 GST_OBJECT_UNLOCK (pad);
2299 if (pad->direction == GST_PAD_SRC)
2300 padlist = &eparent->sinkpads;
2302 padlist = &eparent->srcpads;
2304 GST_DEBUG_OBJECT (pad, "Making iterator");
2306 cookie = &eparent->pads_cookie;
2308 lock = GST_OBJECT_GET_LOCK (eparent);
2310 res = gst_iterator_new_list (GST_TYPE_PAD,
2311 lock, cookie, padlist, (GObject *) owner, NULL);
2313 gst_object_unref (owner);
2320 GST_OBJECT_UNLOCK (pad);
2321 GST_DEBUG_OBJECT (pad, "no parent element");
2327 * gst_pad_iterate_internal_links:
2328 * @pad: the GstPad to get the internal links of.
2330 * Gets an iterator for the pads to which the given pad is linked to inside
2331 * of the parent element.
2333 * Each #GstPad element yielded by the iterator will have its refcount increased,
2334 * so unref after use.
2336 * Free-function: gst_iterator_free
2338 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2339 * pad does not have an iterator function configured. Use
2340 * gst_iterator_free() after usage.
2345 gst_pad_iterate_internal_links (GstPad * pad)
2347 GstIterator *res = NULL;
2350 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2352 GST_OBJECT_LOCK (pad);
2353 ACQUIRE_PARENT (pad, parent, no_parent);
2354 GST_OBJECT_UNLOCK (pad);
2356 if (GST_PAD_ITERINTLINKFUNC (pad))
2357 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2359 RELEASE_PARENT (parent);
2366 GST_DEBUG_OBJECT (pad, "no parent");
2367 GST_OBJECT_UNLOCK (pad);
2375 * @forward: a #GstPadForwardFunction
2376 * @user_data: user data passed to @forward
2378 * Calls @forward for all internally linked pads of @pad. This function deals with
2379 * dynamically changing internal pads and will make sure that the @forward
2380 * function is only called once for each pad.
2382 * When @forward returns TRUE, no further pads will be processed.
2384 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2387 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2390 gboolean result = FALSE;
2392 gboolean done = FALSE;
2393 GValue item = { 0, };
2394 GList *pushed_pads = NULL;
2396 iter = gst_pad_iterate_internal_links (pad);
2402 switch (gst_iterator_next (iter, &item)) {
2403 case GST_ITERATOR_OK:
2407 intpad = g_value_get_object (&item);
2409 /* if already pushed, skip. FIXME, find something faster to tag pads */
2410 if (g_list_find (pushed_pads, intpad)) {
2411 g_value_reset (&item);
2415 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2416 GST_DEBUG_PAD_NAME (intpad));
2417 done = result = forward (intpad, user_data);
2419 pushed_pads = g_list_prepend (pushed_pads, intpad);
2421 g_value_reset (&item);
2424 case GST_ITERATOR_RESYNC:
2425 /* We don't reset the result here because we don't push the event
2426 * again on pads that got the event already and because we need
2427 * to consider the result of the previous pushes */
2428 gst_iterator_resync (iter);
2430 case GST_ITERATOR_ERROR:
2431 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2434 case GST_ITERATOR_DONE:
2439 g_value_unset (&item);
2440 gst_iterator_free (iter);
2442 g_list_free (pushed_pads);
2452 gboolean dispatched;
2456 event_forward_func (GstPad * pad, EventData * data)
2458 /* for each pad we send to, we should ref the event; it's up
2459 * to downstream to unref again when handled. */
2460 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2461 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2463 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2465 data->dispatched = TRUE;
2472 * gst_pad_event_default:
2473 * @pad: a #GstPad to call the default event handler on.
2474 * @parent: the parent of @pad or NULL
2475 * @event: (transfer full): the #GstEvent to handle.
2477 * Invokes the default event handler for the given pad.
2479 * The EOS event will pause the task associated with @pad before it is forwarded
2480 * to all internally linked pads,
2482 * The the event is sent to all pads internally linked to @pad. This function
2483 * takes ownership of @event.
2485 * Returns: TRUE if the event was sent successfully.
2488 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2490 gboolean result, forward = TRUE;
2492 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2493 g_return_val_if_fail (event != NULL, FALSE);
2495 GST_LOG_OBJECT (pad, "default event handler");
2497 switch (GST_EVENT_TYPE (event)) {
2500 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2501 gst_pad_pause_task (pad);
2504 case GST_EVENT_CAPS:
2505 forward = GST_PAD_IS_PROXY_CAPS (pad);
2516 data.dispatched = FALSE;
2517 data.result = FALSE;
2519 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2521 /* for sinkpads without a parent element or without internal links, nothing
2522 * will be dispatched but we still want to return TRUE. */
2523 if (data.dispatched)
2524 result = data.result;
2529 gst_event_unref (event);
2534 /* Default accept caps implementation just checks against
2535 * the allowed caps for the pad */
2537 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2539 /* get the caps and see if it intersects to something not empty */
2540 GstCaps *caps, *allowed;
2543 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2545 /* first forward the query to internally linked pads when we are dealing with
2547 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2548 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2553 allowed = gst_pad_query_caps (pad, NULL);
2554 gst_query_parse_accept_caps (query, &caps);
2557 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2558 result = gst_caps_is_subset (caps, allowed);
2559 gst_caps_unref (allowed);
2561 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2564 gst_query_set_accept_caps_result (query, result);
2570 /* Default caps implementation */
2572 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2574 GstCaps *result = NULL, *filter;
2575 GstPadTemplate *templ;
2576 gboolean fixed_caps;
2578 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2580 gst_query_parse_caps (query, &filter);
2582 /* first try to proxy if we must */
2583 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2584 if ((gst_pad_proxy_query_caps (pad, query))) {
2585 gst_query_parse_caps_result (query, &result);
2590 /* no proxy or it failed, do default handling */
2591 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2593 GST_OBJECT_LOCK (pad);
2595 /* fixed caps, try the negotiated caps first */
2596 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2597 if ((result = get_pad_caps (pad)))
2598 goto filter_done_unlock;
2601 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2602 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2603 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2604 goto filter_done_unlock;
2608 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2609 "non-fixed pad caps: trying pad caps");
2610 /* non fixed caps, try the negotiated caps */
2611 if ((result = get_pad_caps (pad)))
2612 goto filter_done_unlock;
2614 GST_OBJECT_UNLOCK (pad);
2616 /* this almost never happens */
2617 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2618 result = gst_caps_new_empty ();
2623 GST_OBJECT_UNLOCK (pad);
2626 /* run the filter on the result */
2628 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2629 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2630 GST_PTR_FORMAT, result, result, filter, filter);
2631 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2632 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2635 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2636 "using caps %p %" GST_PTR_FORMAT, result, result);
2637 result = gst_caps_ref (result);
2641 gst_query_set_caps_result (query, result);
2642 gst_caps_unref (result);
2648 * gst_pad_query_default:
2649 * @pad: a #GstPad to call the default query handler on.
2650 * @parent: the parent of @pad or NULL
2651 * @query: (transfer none): the #GstQuery to handle.
2653 * Invokes the default query handler for the given pad.
2654 * The query is sent to all pads internally linked to @pad. Note that
2655 * if there are many possible sink pads that are internally linked to
2656 * @pad, only one will be sent the query.
2657 * Multi-sinkpad elements should implement custom query handlers.
2659 * Returns: TRUE if the query was performed successfully.
2662 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2664 gboolean forward = TRUE, ret = FALSE;
2666 switch (GST_QUERY_TYPE (query)) {
2667 case GST_QUERY_SCHEDULING:
2670 case GST_QUERY_ACCEPT_CAPS:
2671 ret = gst_pad_query_accept_caps_default (pad, query);
2674 case GST_QUERY_CAPS:
2675 ret = gst_pad_query_caps_default (pad, query);
2678 case GST_QUERY_POSITION:
2679 case GST_QUERY_SEEKING:
2680 case GST_QUERY_FORMATS:
2681 case GST_QUERY_LATENCY:
2682 case GST_QUERY_JITTER:
2683 case GST_QUERY_RATE:
2684 case GST_QUERY_CONVERT:
2685 case GST_QUERY_ALLOCATION:
2691 ret = gst_pad_forward
2692 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2698 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2700 GstPad *pad = data->pad;
2701 GstPadProbeInfo *info = data->info;
2702 GstPadProbeType type, flags;
2703 GstPadProbeCallback callback;
2704 GstPadProbeReturn ret;
2706 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2707 "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
2709 /* if we have called this callback, do nothing */
2710 if (PROBE_COOKIE (hook) == data->cookie) {
2711 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2712 "hook %lu, cookie %u already called", hook->hook_id,
2713 PROBE_COOKIE (hook));
2717 PROBE_COOKIE (hook) = data->cookie;
2719 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2722 /* one of the data types */
2723 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2725 /* one of the scheduling types */
2726 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2728 /* all of the blocking types must match */
2729 if ((flags & GST_PAD_PROBE_TYPE_BLOCKING) !=
2730 (type & GST_PAD_PROBE_TYPE_BLOCKING))
2733 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2734 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
2736 callback = (GstPadProbeCallback) hook->func;
2737 if (callback == NULL)
2740 GST_OBJECT_UNLOCK (pad);
2742 ret = callback (pad, info, hook->data);
2744 GST_OBJECT_LOCK (pad);
2745 data->marshalled = TRUE;
2748 case GST_PAD_PROBE_REMOVE:
2749 /* remove the probe */
2750 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2751 cleanup_hook (pad, hook);
2753 case GST_PAD_PROBE_DROP:
2754 /* need to drop the data, make sure other probes don't get called
2756 GST_DEBUG_OBJECT (pad, "asked to drop item");
2757 info->type = GST_PAD_PROBE_TYPE_INVALID;
2758 data->dropped = TRUE;
2760 case GST_PAD_PROBE_PASS:
2761 /* inform the pad block to let things pass */
2762 GST_DEBUG_OBJECT (pad, "asked to pass item");
2766 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2773 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2774 "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
2780 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2782 if (G_UNLIKELY (pad->num_probes)) { \
2783 /* we start with passing NULL as the data item */ \
2784 GstPadProbeInfo info = { mask, NULL, offs, size }; \
2785 ret = do_probe_callbacks (pad, &info, defaultval); \
2786 /* store the possibly updated data item */ \
2787 data = GST_PAD_PROBE_INFO_DATA (&info); \
2788 /* if something went wrong, exit */ \
2789 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2791 /* otherwise check if the probe retured data */ \
2792 if (G_UNLIKELY (data != NULL)) \
2798 /* a probe that does not take or return any data */
2799 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2801 if (G_UNLIKELY (pad->num_probes)) { \
2802 /* pass NULL as the data item */ \
2803 GstPadProbeInfo info = { mask, NULL, 0, 0 }; \
2804 ret = do_probe_callbacks (pad, &info, defaultval); \
2805 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2810 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2812 if (G_UNLIKELY (pad->num_probes)) { \
2813 GstPadProbeInfo info = { mask, data, offs, size }; \
2814 ret = do_probe_callbacks (pad, &info, defaultval); \
2815 data = GST_PAD_PROBE_INFO_DATA (&info); \
2816 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2821 #define PROBE_PUSH(pad,mask,data,label) \
2822 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2823 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2824 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2826 static GstFlowReturn
2827 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2828 GstFlowReturn defaultval)
2837 data.marshalled = FALSE;
2838 data.dropped = FALSE;
2839 data.cookie = ++pad->priv->probe_cookie;
2842 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2845 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2846 "do probes cookie %u", data.cookie);
2847 cookie = pad->priv->probe_list_cookie;
2849 g_hook_list_marshal (&pad->probes, TRUE,
2850 (GHookMarshaller) probe_hook_marshal, &data);
2852 /* if the list changed, call the new callbacks (they will not have their
2853 * cookie set to data.cookie */
2854 if (cookie != pad->priv->probe_list_cookie) {
2855 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2856 "probe list changed, restarting");
2860 /* the first item that dropped will stop the hooks and then we drop here */
2864 /* if no handler matched and we are blocking, let the item pass */
2865 if (!data.marshalled && is_block)
2868 /* At this point, all handlers returned either OK or PASS. If one handler
2869 * returned PASS, let the item pass */
2874 while (GST_PAD_IS_BLOCKED (pad)) {
2875 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2876 "we are blocked %d times", pad->num_blocked);
2878 /* we might have released the lock */
2879 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2882 /* now we block the streaming thread. It can be unlocked when we
2883 * deactivate the pad (which will also set the FLUSHING flag) or
2884 * when the pad is unblocked. A flushing event will also unblock
2885 * the pad after setting the FLUSHING flag. */
2886 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2887 "Waiting to be unblocked or set flushing");
2888 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
2889 GST_PAD_BLOCK_WAIT (pad);
2890 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
2891 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
2893 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2903 GST_DEBUG_OBJECT (pad, "pad is flushing");
2904 return GST_FLOW_WRONG_STATE;
2908 GST_DEBUG_OBJECT (pad, "data is dropped");
2909 return GST_FLOW_CUSTOM_SUCCESS;
2913 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
2914 GST_DEBUG_OBJECT (pad, "data is passed");
2922 * gst_pad_get_offset:
2925 * Get the offset applied to the running time of @pad. @pad has to be a source
2928 * Returns: the offset.
2931 gst_pad_get_offset (GstPad * pad)
2935 g_return_val_if_fail (GST_IS_PAD (pad), 0);
2937 GST_OBJECT_LOCK (pad);
2938 result = pad->offset;
2939 GST_OBJECT_UNLOCK (pad);
2945 * gst_pad_set_offset:
2947 * @offset: the offset
2949 * Set the offset that will be applied to the running time of @pad.
2952 gst_pad_set_offset (GstPad * pad, gint64 offset)
2958 g_return_if_fail (GST_IS_PAD (pad));
2960 GST_OBJECT_LOCK (pad);
2961 /* if nothing changed, do nothing */
2962 if (pad->offset == offset)
2965 pad->offset = offset;
2967 /* if no peer, we just updated the offset */
2968 if ((peer = GST_PAD_PEER (pad)) == NULL)
2971 /* switch pads around when dealing with a sinkpad */
2972 if (GST_PAD_IS_SINK (pad)) {
2973 /* ref the peer so it doesn't go away when we release the lock */
2974 tmp = gst_object_ref (peer);
2975 /* make sure we get the peer (the srcpad) */
2976 GST_OBJECT_UNLOCK (pad);
2982 GST_OBJECT_LOCK (pad);
2983 /* check if the pad didn't get relinked */
2984 if (GST_PAD_PEER (pad) != peer)
2987 /* we can release the ref now */
2988 gst_object_unref (peer);
2991 /* the index of the segment event in the array */
2992 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
2994 /* lock order is srcpad >> sinkpad */
2995 GST_OBJECT_LOCK (peer);
2996 /* take the current segment event, adjust it and then place
2997 * it on the sinkpad. events on the srcpad are always active. */
2998 if (replace_event (pad, peer, idx))
2999 GST_OBJECT_FLAG_SET (peer, GST_PAD_FLAG_NEED_EVENTS);
3001 GST_OBJECT_UNLOCK (peer);
3004 GST_OBJECT_UNLOCK (pad);
3010 * @pad: a #GstPad to invoke the default query on.
3011 * @query: (transfer none): the #GstQuery to perform.
3013 * Dispatches a query to a pad. The query should have been allocated by the
3014 * caller via one of the type-specific allocation functions. The element that
3015 * the pad belongs to is responsible for filling the query with an appropriate
3016 * response, which should then be parsed with a type-specific query parsing
3019 * Again, the caller is responsible for both the allocation and deallocation of
3020 * the query structure.
3022 * Please also note that some queries might need a running pipeline to work.
3024 * Returns: TRUE if the query could be performed.
3027 gst_pad_query (GstPad * pad, GstQuery * query)
3031 GstPadQueryFunction func;
3032 GstPadProbeType type;
3035 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3036 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3038 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3039 GST_QUERY_TYPE_NAME (query));
3041 if (GST_PAD_IS_SRC (pad))
3042 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3044 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3046 GST_OBJECT_LOCK (pad);
3047 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3048 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3049 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3051 ACQUIRE_PARENT (pad, parent, no_parent);
3052 GST_OBJECT_UNLOCK (pad);
3054 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3057 res = func (pad, parent, query);
3059 RELEASE_PARENT (parent);
3061 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3062 GST_QUERY_TYPE_NAME (query), res);
3067 GST_OBJECT_LOCK (pad);
3068 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3069 GST_OBJECT_UNLOCK (pad);
3076 GST_DEBUG_OBJECT (pad, "had no parent");
3077 GST_OBJECT_UNLOCK (pad);
3082 GST_DEBUG_OBJECT (pad, "had no query function");
3083 RELEASE_PARENT (parent);
3088 GST_DEBUG_OBJECT (pad, "query failed");
3093 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3094 GST_OBJECT_UNLOCK (pad);
3100 * gst_pad_peer_query:
3101 * @pad: a #GstPad to invoke the peer query on.
3102 * @query: (transfer none): the #GstQuery to perform.
3104 * Performs gst_pad_query() on the peer of @pad.
3106 * The caller is responsible for both the allocation and deallocation of
3107 * the query structure.
3109 * Returns: TRUE if the query could be performed. This function returns %FALSE
3110 * if @pad has no peer.
3115 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3118 GstPadProbeType type;
3122 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3123 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3125 if (GST_PAD_IS_SRC (pad))
3126 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3128 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3130 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3131 GST_QUERY_TYPE_NAME (query));
3133 GST_OBJECT_LOCK (pad);
3134 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3135 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3136 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3138 peerpad = GST_PAD_PEER (pad);
3139 if (G_UNLIKELY (peerpad == NULL))
3142 gst_object_ref (peerpad);
3143 GST_OBJECT_UNLOCK (pad);
3145 res = gst_pad_query (peerpad, query);
3147 gst_object_unref (peerpad);
3152 GST_OBJECT_LOCK (pad);
3153 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3154 GST_OBJECT_UNLOCK (pad);
3161 GST_WARNING_OBJECT (pad, "pad has no peer");
3162 GST_OBJECT_UNLOCK (pad);
3167 GST_DEBUG_OBJECT (pad, "query failed");
3172 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3173 GST_OBJECT_UNLOCK (pad);
3178 /**********************************************************************
3179 * Data passing functions
3182 /* this is the chain function that does not perform the additional argument
3183 * checking for that little extra speed.
3185 static inline GstFlowReturn
3186 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3190 gboolean needs_events;
3192 GST_PAD_STREAM_LOCK (pad);
3194 GST_OBJECT_LOCK (pad);
3195 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3198 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3199 if (G_UNLIKELY (needs_events)) {
3200 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_EVENTS);
3202 GST_DEBUG_OBJECT (pad, "need to update all events");
3203 ret = gst_pad_update_events (pad);
3204 if (G_UNLIKELY (ret != GST_FLOW_OK))
3208 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3210 PROBE_PUSH (pad, type, data, probe_stopped);
3212 parent = GST_OBJECT_PARENT (pad);
3213 GST_OBJECT_UNLOCK (pad);
3215 /* NOTE: we read the chainfunc unlocked.
3216 * we cannot hold the lock for the pad so we might send
3217 * the data to the wrong function. This is not really a
3218 * problem since functions are assigned at creation time
3219 * and don't change that often... */
3220 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3221 GstPadChainFunction chainfunc;
3223 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3226 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3227 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3228 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3230 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3232 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3233 "called chainfunction &%s with buffer %p, returned %s",
3234 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3236 GstPadChainListFunction chainlistfunc;
3238 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3241 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3242 "calling chainlistfunction &%s",
3243 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3245 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3247 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3248 "called chainlistfunction &%s, returned %s",
3249 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3252 GST_PAD_STREAM_UNLOCK (pad);
3259 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3260 "chaining, but pad was flushing");
3261 GST_OBJECT_UNLOCK (pad);
3262 GST_PAD_STREAM_UNLOCK (pad);
3263 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3264 return GST_FLOW_WRONG_STATE;
3268 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3269 GST_OBJECT_UNLOCK (pad);
3270 GST_PAD_STREAM_UNLOCK (pad);
3271 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3276 GST_OBJECT_UNLOCK (pad);
3277 GST_PAD_STREAM_UNLOCK (pad);
3278 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3281 case GST_FLOW_CUSTOM_SUCCESS:
3282 GST_DEBUG_OBJECT (pad, "dropped buffer");
3286 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3293 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3294 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3295 "pushing, but not chainhandler");
3296 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3297 ("push on pad %s:%s but it has no chainfunction",
3298 GST_DEBUG_PAD_NAME (pad)));
3299 GST_PAD_STREAM_UNLOCK (pad);
3300 return GST_FLOW_NOT_SUPPORTED;
3306 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3307 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3310 * Chain a buffer to @pad.
3312 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3314 * If the buffer type is not acceptable for @pad (as negotiated with a
3315 * preceeding GST_EVENT_CAPS event), this function returns
3316 * #GST_FLOW_NOT_NEGOTIATED.
3318 * The function proceeds calling the chain function installed on @pad (see
3319 * gst_pad_set_chain_function()) and the return value of that function is
3320 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3323 * In all cases, success or failure, the caller loses its reference to @buffer
3324 * after calling this function.
3326 * Returns: a #GstFlowReturn from the pad.
3331 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3333 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3334 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3335 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3337 return gst_pad_chain_data_unchecked (pad,
3338 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3341 static GstFlowReturn
3342 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3343 GstBufferList * list)
3349 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3351 len = gst_buffer_list_length (list);
3354 for (i = 0; i < len; i++) {
3355 buffer = gst_buffer_list_get (list, i);
3357 gst_pad_chain_data_unchecked (pad,
3358 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3359 gst_buffer_ref (buffer));
3360 if (ret != GST_FLOW_OK)
3363 gst_buffer_list_unref (list);
3369 * gst_pad_chain_list:
3370 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3371 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3374 * Chain a bufferlist to @pad.
3376 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3378 * If @pad was not negotiated properly with a CAPS event, this function
3379 * returns #GST_FLOW_NOT_NEGOTIATED.
3381 * The function proceeds calling the chainlist function installed on @pad (see
3382 * gst_pad_set_chain_list_function()) and the return value of that function is
3383 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3384 * chainlist function.
3386 * In all cases, success or failure, the caller loses its reference to @list
3387 * after calling this function.
3391 * Returns: a #GstFlowReturn from the pad.
3396 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3398 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3399 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3400 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3402 return gst_pad_chain_data_unchecked (pad,
3403 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3406 static GstFlowReturn
3407 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3412 GST_OBJECT_LOCK (pad);
3413 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3416 /* do block probes */
3417 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3419 /* do post-blocking probes */
3420 PROBE_PUSH (pad, type, data, probe_stopped);
3422 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3425 /* take ref to peer pad before releasing the lock */
3426 gst_object_ref (peer);
3428 GST_OBJECT_UNLOCK (pad);
3430 ret = gst_pad_chain_data_unchecked (peer, type, data);
3432 gst_object_unref (peer);
3434 GST_OBJECT_LOCK (pad);
3436 if (pad->priv->using == 0) {
3437 /* pad is not active anymore, trigger idle callbacks */
3438 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3439 probe_stopped, ret);
3441 GST_OBJECT_UNLOCK (pad);
3445 /* ERROR recovery here */
3449 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3450 "pushing, but pad was flushing");
3451 GST_OBJECT_UNLOCK (pad);
3452 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3453 return GST_FLOW_WRONG_STATE;
3457 GST_OBJECT_UNLOCK (pad);
3458 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3461 case GST_FLOW_CUSTOM_SUCCESS:
3462 GST_DEBUG_OBJECT (pad, "dropped buffer");
3466 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3473 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3474 "pushing, but it was not linked");
3475 GST_OBJECT_UNLOCK (pad);
3476 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3477 return GST_FLOW_NOT_LINKED;
3483 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3484 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3487 * Pushes a buffer to the peer of @pad.
3489 * This function will call installed block probes before triggering any
3490 * installed data probes.
3492 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3493 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3496 * In all cases, success or failure, the caller loses its reference to @buffer
3497 * after calling this function.
3499 * Returns: a #GstFlowReturn from the peer pad.
3504 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3506 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3507 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3508 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3510 return gst_pad_push_data (pad,
3511 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3515 * gst_pad_push_list:
3516 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3517 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3520 * Pushes a buffer list to the peer of @pad.
3522 * This function will call installed block probes before triggering any
3523 * installed data probes.
3525 * The function proceeds calling the chain function on the peer pad and returns
3526 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3527 * be returned. If the peer pad does not have any installed chainlist function
3528 * every group buffer of the list will be merged into a normal #GstBuffer and
3529 * chained via gst_pad_chain().
3531 * In all cases, success or failure, the caller loses its reference to @list
3532 * after calling this function.
3534 * Returns: a #GstFlowReturn from the peer pad.
3541 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3543 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3544 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3545 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3547 return gst_pad_push_data (pad,
3548 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3551 static GstFlowReturn
3552 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3553 GstBuffer ** buffer)
3556 GstPadGetRangeFunction getrangefunc;
3559 GST_PAD_STREAM_LOCK (pad);
3561 GST_OBJECT_LOCK (pad);
3562 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3565 /* when one of the probes returns a buffer, probed_data will be called and we
3566 * skip calling the getrange function */
3567 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3568 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3570 ACQUIRE_PARENT (pad, parent, no_parent);
3571 GST_OBJECT_UNLOCK (pad);
3573 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3576 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3577 "calling getrangefunc %s, offset %"
3578 G_GUINT64_FORMAT ", size %u",
3579 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3581 ret = getrangefunc (pad, parent, offset, size, buffer);
3583 RELEASE_PARENT (parent);
3585 if (G_UNLIKELY (ret != GST_FLOW_OK))
3586 goto get_range_failed;
3588 /* can only fire the signal if we have a valid buffer */
3589 GST_OBJECT_LOCK (pad);
3591 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3592 *buffer, offset, size, probe_stopped_unref);
3593 GST_OBJECT_UNLOCK (pad);
3595 GST_PAD_STREAM_UNLOCK (pad);
3602 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3603 "getrange, but pad was flushing");
3604 GST_OBJECT_UNLOCK (pad);
3605 GST_PAD_STREAM_UNLOCK (pad);
3606 return GST_FLOW_WRONG_STATE;
3610 GST_DEBUG_OBJECT (pad, "no parent");
3611 GST_OBJECT_UNLOCK (pad);
3612 GST_PAD_STREAM_UNLOCK (pad);
3613 return GST_FLOW_WRONG_STATE;
3617 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3618 ("getrange on pad %s:%s but it has no getrangefunction",
3619 GST_DEBUG_PAD_NAME (pad)));
3620 RELEASE_PARENT (parent);
3621 GST_PAD_STREAM_UNLOCK (pad);
3622 return GST_FLOW_NOT_SUPPORTED;
3626 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3627 "probe returned %s", gst_flow_get_name (ret));
3628 GST_OBJECT_UNLOCK (pad);
3629 GST_PAD_STREAM_UNLOCK (pad);
3632 probe_stopped_unref:
3634 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3635 "probe returned %s", gst_flow_get_name (ret));
3636 GST_OBJECT_UNLOCK (pad);
3637 GST_PAD_STREAM_UNLOCK (pad);
3638 gst_buffer_unref (*buffer);
3644 GST_PAD_STREAM_UNLOCK (pad);
3646 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3647 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3648 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3654 * gst_pad_get_range:
3655 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3656 * @offset: The start offset of the buffer
3657 * @size: The length of the buffer
3658 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3659 * returns #GST_FLOW_ERROR if %NULL.
3661 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
3662 * immediately and @buffer is %NULL.
3664 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3665 * description of a getrange function. If @pad has no getrange function
3666 * installed (see gst_pad_set_getrange_function()) this function returns
3667 * #GST_FLOW_NOT_SUPPORTED.
3669 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3671 * Returns: a #GstFlowReturn from the pad.
3676 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3677 GstBuffer ** buffer)
3679 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3680 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3681 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3683 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3687 * gst_pad_pull_range:
3688 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3689 * @offset: The start offset of the buffer
3690 * @size: The length of the buffer
3691 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3692 * GST_FLOW_ERROR if %NULL.
3694 * Pulls a @buffer from the peer pad.
3696 * This function will first trigger the pad block signal if it was
3699 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3700 * function returns the result of gst_pad_get_range() on the peer pad.
3701 * See gst_pad_get_range() for a list of return values and for the
3702 * semantics of the arguments of this function.
3704 * Returns: a #GstFlowReturn from the peer pad.
3705 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3706 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3707 * @buffer may not be used or freed when any other return value than
3708 * #GST_FLOW_OK is returned.
3713 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3714 GstBuffer ** buffer)
3718 gboolean needs_events;
3720 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3721 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3722 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3724 GST_OBJECT_LOCK (pad);
3725 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3728 /* when one of the probes returns a buffer, probed_data will be called and we
3729 * skip calling the peer getrange function */
3730 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3731 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3733 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3736 gst_object_ref (peer);
3738 GST_OBJECT_UNLOCK (pad);
3740 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3742 gst_object_unref (peer);
3744 GST_OBJECT_LOCK (pad);
3746 if (pad->priv->using == 0) {
3747 /* pad is not active anymore, trigger idle callbacks */
3748 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3749 post_probe_stopped, ret);
3752 if (G_UNLIKELY (ret != GST_FLOW_OK))
3753 goto pull_range_failed;
3756 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3757 *buffer, offset, size, post_probe_stopped);
3759 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3760 if (G_UNLIKELY (needs_events)) {
3761 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_EVENTS);
3763 GST_DEBUG_OBJECT (pad, "we need to update the events");
3764 ret = gst_pad_update_events (pad);
3765 if (G_UNLIKELY (ret != GST_FLOW_OK))
3768 GST_OBJECT_UNLOCK (pad);
3772 /* ERROR recovery here */
3775 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3776 "pullrange, but pad was flushing");
3777 GST_OBJECT_UNLOCK (pad);
3778 return GST_FLOW_WRONG_STATE;
3782 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3783 gst_flow_get_name (ret));
3784 GST_OBJECT_UNLOCK (pad);
3789 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3790 "pulling range, but it was not linked");
3791 GST_OBJECT_UNLOCK (pad);
3792 return GST_FLOW_NOT_LINKED;
3797 GST_OBJECT_UNLOCK (pad);
3798 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3799 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3800 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3805 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3806 "post probe returned %s", gst_flow_get_name (ret));
3807 GST_OBJECT_UNLOCK (pad);
3808 if (ret == GST_FLOW_OK)
3809 gst_buffer_unref (*buffer);
3815 GST_OBJECT_UNLOCK (pad);
3816 gst_buffer_unref (*buffer);
3818 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
3819 "pullrange returned events that were not accepted");
3825 * gst_pad_push_event:
3826 * @pad: a #GstPad to push the event to.
3827 * @event: (transfer full): the #GstEvent to send to the pad.
3829 * Sends the event to the peer of the given pad. This function is
3830 * mainly used by elements to send events to their peer
3833 * This function takes owership of the provided event so you should
3834 * gst_event_ref() it if you want to reuse the event after this call.
3836 * Returns: TRUE if the event was handled.
3841 gst_pad_push_event (GstPad * pad, GstEvent * event)
3846 gboolean stored = FALSE;
3847 GstPadProbeType type;
3849 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3850 g_return_val_if_fail (event != NULL, FALSE);
3851 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
3853 if (GST_EVENT_IS_DOWNSTREAM (event))
3854 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
3856 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
3858 GST_OBJECT_LOCK (pad);
3860 peerpad = GST_PAD_PEER (pad);
3862 /* Two checks to be made:
3863 * . (un)set the FLUSHING flag for flushing events,
3864 * . handle pad blocking */
3865 switch (GST_EVENT_TYPE (event)) {
3866 case GST_EVENT_FLUSH_START:
3867 GST_PAD_SET_FLUSHING (pad);
3869 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3870 /* flush start will have set the FLUSHING flag and will then
3871 * unlock all threads doing a GCond wait on the blocking pad. This
3872 * will typically unblock the STREAMING thread blocked on a pad. */
3873 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
3874 "doing block signal.");
3875 GST_PAD_BLOCK_BROADCAST (pad);
3879 case GST_EVENT_FLUSH_STOP:
3880 GST_PAD_UNSET_FLUSHING (pad);
3882 /* Remove sticky EOS events */
3883 GST_LOG_OBJECT (pad, "Removing pending EOS events");
3884 clear_event (pad->priv->events,
3885 GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
3887 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3888 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
3894 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3897 /* store the event on the pad, but only on srcpads */
3898 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
3901 idx = GST_EVENT_STICKY_IDX (event);
3902 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
3903 GST_EVENT_TYPE_NAME (event), idx);
3905 /* srcpad sticky events always become active immediately */
3906 gst_event_replace (&pad->priv->events[idx].event, event);
3911 /* backwards compatibility mode for caps */
3912 switch (GST_EVENT_TYPE (event)) {
3913 case GST_EVENT_CAPS:
3915 GST_OBJECT_UNLOCK (pad);
3917 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
3919 GST_OBJECT_LOCK (pad);
3920 /* the peerpad might have changed. Things we checked above could not
3922 peerpad = GST_PAD_PEER (pad);
3925 case GST_EVENT_SEGMENT:
3929 offset = pad->offset;
3930 /* check if we need to adjust the segment */
3931 if (offset != 0 && (peerpad != NULL)) {
3934 /* copy segment values */
3935 gst_event_copy_segment (event, &segment);
3936 gst_event_unref (event);
3938 /* adjust and make a new event with the offset applied */
3939 segment.base += offset;
3940 event = gst_event_new_segment (&segment);
3944 case GST_EVENT_RECONFIGURE:
3945 if (GST_PAD_IS_SINK (pad))
3946 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
3952 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3953 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
3959 /* send probes after modifying the events above */
3960 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
3962 /* now check the peer pad */
3963 if (peerpad == NULL)
3966 gst_object_ref (peerpad);
3968 GST_OBJECT_UNLOCK (pad);
3970 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
3971 event, GST_EVENT_TYPE_NAME (event), peerpad);
3973 result = gst_pad_send_event (peerpad, event);
3975 /* Note: we gave away ownership of the event at this point but we can still
3976 * print the old pointer */
3977 GST_LOG_OBJECT (pad,
3978 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
3981 gst_object_unref (peerpad);
3983 GST_OBJECT_LOCK (pad);
3985 if (pad->priv->using == 0) {
3986 /* pad is not active anymore, trigger idle callbacks */
3987 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3988 probe_stopped, GST_FLOW_OK);
3990 GST_OBJECT_UNLOCK (pad);
3992 return result | stored;
3994 /* ERROR handling */
3997 GST_DEBUG_OBJECT (pad, "We're flushing");
3998 GST_OBJECT_UNLOCK (pad);
3999 gst_event_unref (event);
4004 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4005 GST_OBJECT_UNLOCK (pad);
4006 gst_event_unref (event);
4011 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4012 GST_OBJECT_UNLOCK (pad);
4013 gst_event_unref (event);
4019 * gst_pad_send_event:
4020 * @pad: a #GstPad to send the event to.
4021 * @event: (transfer full): the #GstEvent to send to the pad.
4023 * Sends the event to the pad. This function can be used
4024 * by applications to send events in the pipeline.
4026 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4027 * sink pad, @event should be a downstream event. For example, you would not
4028 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4029 * Furthermore, some downstream events have to be serialized with data flow,
4030 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4031 * the event needs to be serialized with data flow, this function will take the
4032 * pad's stream lock while calling its event function.
4034 * To find out whether an event type is upstream, downstream, or downstream and
4035 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4036 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4037 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4038 * plugin doesn't need to bother itself with this information; the core handles
4039 * all necessary locks and checks.
4041 * This function takes owership of the provided event so you should
4042 * gst_event_ref() it if you want to reuse the event after this call.
4044 * Returns: TRUE if the event was handled.
4047 gst_pad_send_event (GstPad * pad, GstEvent * event)
4050 gboolean result = FALSE;
4051 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4052 GstPadProbeType type;
4054 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4055 g_return_val_if_fail (event != NULL, FALSE);
4057 GST_OBJECT_LOCK (pad);
4058 if (GST_PAD_IS_SINK (pad)) {
4059 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4060 goto wrong_direction;
4061 serialized = GST_EVENT_IS_SERIALIZED (event);
4062 sticky = GST_EVENT_IS_STICKY (event);
4063 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4064 } else if (GST_PAD_IS_SRC (pad)) {
4065 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4066 goto wrong_direction;
4067 /* events on srcpad never are serialized and sticky */
4068 serialized = sticky = FALSE;
4069 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4071 goto unknown_direction;
4073 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4075 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4077 switch (GST_EVENT_TYPE (event)) {
4078 case GST_EVENT_FLUSH_START:
4079 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4080 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4082 /* can't even accept a flush begin event when flushing */
4083 if (GST_PAD_IS_FLUSHING (pad))
4086 GST_PAD_SET_FLUSHING (pad);
4087 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4088 needs_events = FALSE;
4090 case GST_EVENT_FLUSH_STOP:
4091 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4092 GST_PAD_UNSET_FLUSHING (pad);
4093 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4095 /* Remove pending EOS events */
4096 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4097 clear_event (pad->priv->events,
4098 GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4100 GST_OBJECT_UNLOCK (pad);
4101 /* grab stream lock */
4102 GST_PAD_STREAM_LOCK (pad);
4104 GST_OBJECT_LOCK (pad);
4105 needs_events = FALSE;
4107 case GST_EVENT_RECONFIGURE:
4108 if (GST_PAD_IS_SRC (pad))
4109 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4111 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4112 GST_EVENT_TYPE_NAME (event));
4114 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4118 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4119 GST_OBJECT_UNLOCK (pad);
4120 GST_PAD_STREAM_LOCK (pad);
4122 GST_OBJECT_LOCK (pad);
4124 /* don't forward events on non-serialized events */
4125 needs_events = FALSE;
4128 /* store the event on the pad, but only on srcpads. We need to store the
4129 * event before checking the flushing flag. */
4134 switch (GST_EVENT_TYPE (event)) {
4135 case GST_EVENT_SEGMENT:
4136 if (pad->offset != 0) {
4139 /* copy segment values */
4140 gst_event_copy_segment (event, &segment);
4141 gst_event_unref (event);
4143 /* adjust and make a new event with the offset applied */
4144 segment.base += pad->offset;
4145 event = gst_event_new_segment (&segment);
4152 idx = GST_EVENT_STICKY_IDX (event);
4153 ev = &pad->priv->events[idx];
4155 if (ev->event != event) {
4156 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4157 GST_EVENT_TYPE_NAME (event), idx);
4158 gst_event_replace (&ev->pending, event);
4159 /* set the flag so that we update the events next time. We would
4160 * usually update below but we might be flushing too. */
4161 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_EVENTS);
4162 needs_events = TRUE;
4165 /* now do the probe */
4167 type | GST_PAD_PROBE_TYPE_PUSH |
4168 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4170 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4175 if (G_UNLIKELY (needs_events)) {
4178 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_EVENTS);
4180 GST_DEBUG_OBJECT (pad, "need to update all events");
4181 ret = gst_pad_update_events (pad);
4182 if (ret != GST_FLOW_OK)
4184 GST_OBJECT_UNLOCK (pad);
4186 gst_event_unref (event);
4191 /* ensure to pass on event;
4192 * note that a sticky event has already been updated above */
4193 if (G_LIKELY (!needs_events || !sticky)) {
4194 GstPadEventFunction eventfunc;
4197 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4200 ACQUIRE_PARENT (pad, parent, no_parent);
4201 GST_OBJECT_UNLOCK (pad);
4203 result = eventfunc (pad, parent, event);
4205 RELEASE_PARENT (parent);
4209 GST_PAD_STREAM_UNLOCK (pad);
4211 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4215 /* ERROR handling */
4218 g_warning ("pad %s:%s sending %s event in wrong direction",
4219 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4220 GST_OBJECT_UNLOCK (pad);
4221 gst_event_unref (event);
4226 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4227 GST_OBJECT_UNLOCK (pad);
4228 gst_event_unref (event);
4233 g_warning ("pad %s:%s has no event handler, file a bug.",
4234 GST_DEBUG_PAD_NAME (pad));
4235 GST_OBJECT_UNLOCK (pad);
4237 GST_PAD_STREAM_UNLOCK (pad);
4238 gst_event_unref (event);
4243 GST_DEBUG_OBJECT (pad, "no parent");
4244 GST_OBJECT_UNLOCK (pad);
4246 GST_PAD_STREAM_UNLOCK (pad);
4247 gst_event_unref (event);
4252 GST_OBJECT_UNLOCK (pad);
4254 GST_PAD_STREAM_UNLOCK (pad);
4255 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4256 "Received event on flushing pad. Discarding");
4257 gst_event_unref (event);
4262 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4263 GST_OBJECT_UNLOCK (pad);
4265 GST_PAD_STREAM_UNLOCK (pad);
4266 gst_event_unref (event);
4271 GST_OBJECT_UNLOCK (pad);
4273 GST_PAD_STREAM_UNLOCK (pad);
4274 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4275 gst_event_unref (event);
4283 * gst_pad_set_element_private:
4284 * @pad: the #GstPad to set the private data of.
4285 * @priv: The private data to attach to the pad.
4287 * Set the given private data gpointer on the pad.
4288 * This function can only be used by the element that owns the pad.
4289 * No locking is performed in this function.
4292 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4294 pad->element_private = priv;
4298 * gst_pad_get_element_private:
4299 * @pad: the #GstPad to get the private data of.
4301 * Gets the private data of a pad.
4302 * No locking is performed in this function.
4304 * Returns: (transfer none): a #gpointer to the private data.
4307 gst_pad_get_element_private (GstPad * pad)
4309 return pad->element_private;
4313 * gst_pad_get_sticky_event:
4314 * @pad: the #GstPad to get the event from.
4315 * @event_type: the #GstEventType that should be retrieved.
4317 * Returns a new reference of the sticky event of type @event_type
4320 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4323 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4325 GstEvent *event = NULL;
4328 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4329 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4331 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4333 GST_OBJECT_LOCK (pad);
4334 if ((event = pad->priv->events[idx].event)) {
4335 gst_event_ref (event);
4337 GST_OBJECT_UNLOCK (pad);
4343 * gst_pad_sticky_events_foreach:
4344 * @pad: the #GstPad that should be used for iteration.
4345 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4346 * @user_data: (closure): the optional user data.
4348 * Iterates all active sticky events on @pad and calls @foreach_func for every
4349 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4350 * is immediately stopped.
4352 * Returns: GST_FLOW_OK if iteration was successful
4355 gst_pad_sticky_events_foreach (GstPad * pad,
4356 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4358 GstFlowReturn ret = GST_FLOW_OK;
4362 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4363 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4365 GST_OBJECT_LOCK (pad);
4368 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4372 ev = &pad->priv->events[i];
4374 /* skip without active event */
4375 if ((event = ev->event) == NULL)
4378 gst_event_ref (event);
4379 GST_OBJECT_UNLOCK (pad);
4381 res = foreach_func (pad, event, user_data);
4383 GST_OBJECT_LOCK (pad);
4384 gst_event_unref (event);
4386 if (res != GST_FLOW_OK) {
4391 /* things could have changed while we release the lock, check if we still
4392 * are handling the same event, if we don't something changed and we have
4393 * to try again. FIXME. we need a cookie here. */
4394 if (event != ev->event) {
4395 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4399 GST_OBJECT_UNLOCK (pad);
4405 do_stream_status (GstPad * pad, GstStreamStatusType type,
4406 GThread * thread, GstTask * task)
4410 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4412 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4413 if (GST_IS_ELEMENT (parent)) {
4414 GstMessage *message;
4415 GValue value = { 0 };
4417 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4418 gchar *tname, *ename, *pname;
4420 /* create a good task name */
4421 ename = gst_element_get_name (parent);
4422 pname = gst_pad_get_name (pad);
4423 tname = g_strdup_printf ("%s:%s", ename, pname);
4427 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4431 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4434 g_value_init (&value, GST_TYPE_TASK);
4435 g_value_set_object (&value, task);
4436 gst_message_set_stream_status_object (message, &value);
4437 g_value_unset (&value);
4439 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4440 gst_element_post_message (parent, message);
4442 gst_object_unref (parent);
4447 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4449 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4454 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4456 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4460 static GstTaskThreadCallbacks thr_callbacks = {
4466 * gst_pad_start_task:
4467 * @pad: the #GstPad to start the task of
4468 * @func: the task function to call
4469 * @data: data passed to the task function
4471 * Starts a task that repeatedly calls @func with @data. This function
4472 * is mostly used in pad activation functions to start the dataflow.
4473 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4474 * before @func is called.
4476 * Returns: a %TRUE if the task could be started.
4479 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4484 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4485 g_return_val_if_fail (func != NULL, FALSE);
4487 GST_DEBUG_OBJECT (pad, "start task");
4489 GST_OBJECT_LOCK (pad);
4490 task = GST_PAD_TASK (pad);
4492 task = gst_task_new (func, data);
4493 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4494 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4495 GST_DEBUG_OBJECT (pad, "created task");
4496 GST_PAD_TASK (pad) = task;
4497 gst_object_ref (task);
4498 /* release lock to post the message */
4499 GST_OBJECT_UNLOCK (pad);
4501 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4503 gst_object_unref (task);
4505 GST_OBJECT_LOCK (pad);
4506 /* nobody else is supposed to have changed the pad now */
4507 if (GST_PAD_TASK (pad) != task)
4508 goto concurrent_stop;
4510 res = gst_task_set_state (task, GST_TASK_STARTED);
4511 GST_OBJECT_UNLOCK (pad);
4518 GST_OBJECT_UNLOCK (pad);
4524 * gst_pad_pause_task:
4525 * @pad: the #GstPad to pause the task of
4527 * Pause the task of @pad. This function will also wait until the
4528 * function executed by the task is finished if this function is not
4529 * called from the task function.
4531 * Returns: a TRUE if the task could be paused or FALSE when the pad
4535 gst_pad_pause_task (GstPad * pad)
4540 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4542 GST_DEBUG_OBJECT (pad, "pause task");
4544 GST_OBJECT_LOCK (pad);
4545 task = GST_PAD_TASK (pad);
4548 res = gst_task_set_state (task, GST_TASK_PAUSED);
4549 GST_OBJECT_UNLOCK (pad);
4551 /* wait for task function to finish, this lock is recursive so it does nothing
4552 * when the pause is called from the task itself */
4553 GST_PAD_STREAM_LOCK (pad);
4554 GST_PAD_STREAM_UNLOCK (pad);
4560 GST_DEBUG_OBJECT (pad, "pad has no task");
4561 GST_OBJECT_UNLOCK (pad);
4567 * gst_pad_stop_task:
4568 * @pad: the #GstPad to stop the task of
4570 * Stop the task of @pad. This function will also make sure that the
4571 * function executed by the task will effectively stop if not called
4572 * from the GstTaskFunction.
4574 * This function will deadlock if called from the GstTaskFunction of
4575 * the task. Use gst_task_pause() instead.
4577 * Regardless of whether the pad has a task, the stream lock is acquired and
4578 * released so as to ensure that streaming through this pad has finished.
4580 * Returns: a TRUE if the task could be stopped or FALSE on error.
4583 gst_pad_stop_task (GstPad * pad)
4588 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4590 GST_DEBUG_OBJECT (pad, "stop task");
4592 GST_OBJECT_LOCK (pad);
4593 task = GST_PAD_TASK (pad);
4596 GST_PAD_TASK (pad) = NULL;
4597 res = gst_task_set_state (task, GST_TASK_STOPPED);
4598 GST_OBJECT_UNLOCK (pad);
4600 GST_PAD_STREAM_LOCK (pad);
4601 GST_PAD_STREAM_UNLOCK (pad);
4603 if (!gst_task_join (task))
4606 gst_object_unref (task);
4612 GST_DEBUG_OBJECT (pad, "no task");
4613 GST_OBJECT_UNLOCK (pad);
4615 GST_PAD_STREAM_LOCK (pad);
4616 GST_PAD_STREAM_UNLOCK (pad);
4618 /* this is not an error */
4623 /* this is bad, possibly the application tried to join the task from
4624 * the task's thread. We install the task again so that it will be stopped
4625 * again from the right thread next time hopefully. */
4626 GST_OBJECT_LOCK (pad);
4627 GST_DEBUG_OBJECT (pad, "join failed");
4628 /* we can only install this task if there was no other task */
4629 if (GST_PAD_TASK (pad) == NULL)
4630 GST_PAD_TASK (pad) = task;
4631 GST_OBJECT_UNLOCK (pad);