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_get_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];
122 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
134 static void gst_pad_dispose (GObject * object);
135 static void gst_pad_finalize (GObject * object);
136 static void gst_pad_set_property (GObject * object, guint prop_id,
137 const GValue * value, GParamSpec * pspec);
138 static void gst_pad_get_property (GObject * object, guint prop_id,
139 GValue * value, GParamSpec * pspec);
141 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
142 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
143 static gboolean gst_pad_activate_default (GstPad * pad);
144 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
145 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
146 GstBufferList * list);
148 static GstObjectClass *parent_class = NULL;
149 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
151 static GParamSpec *pspec_caps = NULL;
153 /* quarks for probe signals */
154 static GQuark buffer_quark;
155 static GQuark buffer_list_quark;
156 static GQuark event_quark;
165 static GstFlowQuarks flow_quarks[] = {
166 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
167 {GST_FLOW_RESEND, "resend", 0},
168 {GST_FLOW_OK, "ok", 0},
169 {GST_FLOW_NOT_LINKED, "not-linked", 0},
170 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
171 {GST_FLOW_UNEXPECTED, "unexpected", 0},
172 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
173 {GST_FLOW_ERROR, "error", 0},
174 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
175 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
180 * @ret: a #GstFlowReturn to get the name of.
182 * Gets a string representing the given flow return.
184 * Returns: a static string with the name of the flow return.
186 G_CONST_RETURN gchar *
187 gst_flow_get_name (GstFlowReturn ret)
191 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
193 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
194 if (ret == flow_quarks[i].ret)
195 return flow_quarks[i].name;
202 * @ret: a #GstFlowReturn to get the quark of.
204 * Get the unique quark for the given GstFlowReturn.
206 * Returns: the quark associated with the flow return or 0 if an
207 * invalid return was specified.
210 gst_flow_to_quark (GstFlowReturn ret)
214 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
216 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
217 if (ret == flow_quarks[i].ret)
218 return flow_quarks[i].quark;
227 buffer_quark = g_quark_from_static_string ("buffer"); \
228 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
229 event_quark = g_quark_from_static_string ("event"); \
231 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
232 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
235 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
236 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
239 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
242 gst_pad_class_init (GstPadClass * klass)
244 GObjectClass *gobject_class;
245 GstObjectClass *gstobject_class;
247 gobject_class = G_OBJECT_CLASS (klass);
248 gstobject_class = GST_OBJECT_CLASS (klass);
250 g_type_class_add_private (klass, sizeof (GstPadPrivate));
252 parent_class = g_type_class_peek_parent (klass);
254 gobject_class->dispose = gst_pad_dispose;
255 gobject_class->finalize = gst_pad_finalize;
256 gobject_class->set_property = gst_pad_set_property;
257 gobject_class->get_property = gst_pad_get_property;
261 * @pad: the pad that emitted the signal
262 * @peer: the peer pad that has been connected
264 * Signals that a pad has been linked to the peer pad.
266 gst_pad_signals[PAD_LINKED] =
267 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
268 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
269 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
272 * @pad: the pad that emitted the signal
273 * @peer: the peer pad that has been disconnected
275 * Signals that a pad has been unlinked from the peer pad.
277 gst_pad_signals[PAD_UNLINKED] =
278 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
279 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
280 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
282 pspec_caps = g_param_spec_boxed ("caps", "Caps",
283 "The capabilities of the pad", GST_TYPE_CAPS,
284 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
285 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
287 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
288 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
289 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
290 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
292 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
293 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
294 g_param_spec_object ("template", "Template",
295 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
296 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 gstobject_class->path_string_separator = ".";
300 /* Register common function pointer descriptions */
301 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
311 gst_pad_init (GstPad * pad)
313 pad->priv = GST_PAD_GET_PRIVATE (pad);
315 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
317 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
318 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
319 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
320 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
321 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
322 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
323 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
325 GST_PAD_SET_FLUSHING (pad);
327 g_static_rec_mutex_init (&pad->stream_rec_lock);
329 pad->block_cond = g_cond_new ();
331 g_hook_list_init (&pad->probes, sizeof (GstProbe));
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 gst_event_replace (&events[i].event, NULL);
343 gst_event_replace (&events[i].pending, NULL);
347 /* The sticky event with @idx from the srcpad is copied to the
348 * pending event on the sinkpad (when different).
349 * This function applies the pad offsets in case of segment events.
350 * This will make sure that we send the event to the sinkpad event
351 * function when the next buffer of event arrives.
352 * Should be called with the OBJECT lock of both pads.
353 * This function returns TRUE when there is a pending event on the
356 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
358 PadEvent *srcev, *sinkev;
360 gboolean pending = FALSE;
362 srcev = &srcpad->priv->events[idx];
364 if ((event = srcev->event)) {
365 sinkev = &sinkpad->priv->events[idx];
367 switch (GST_EVENT_TYPE (event)) {
368 case GST_EVENT_SEGMENT:
373 offset = srcpad->offset + sinkpad->offset;
375 gst_event_copy_segment (event, &segment);
376 /* adjust the base time. FIXME, check negative times, try to tweak the
377 * start to do clipping on negative times */
378 segment.base += offset;
379 /* make a new event from the updated segment */
380 event = gst_event_new_segment (&segment);
387 if (sinkev->event != event) {
388 /* put in the pending entry when different */
389 gst_event_replace (&sinkev->pending, event);
398 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
403 /* make sure we push the events from the source to this new peer, for this we
404 * copy the events on the sinkpad and mark EVENTS_PENDING */
406 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
407 pending |= replace_event (srcpad, sinkpad, i);
409 /* we had some new pending events, set our flag */
411 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
414 /* should be called with the OBJECT_LOCK */
416 get_pad_caps (GstPad * pad)
418 GstCaps *caps = NULL;
422 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
423 /* we can only use the caps when we have successfully send the caps
424 * event to the event function and is thus in the active entry */
425 if ((event = pad->priv->events[idx].event))
426 gst_event_parse_caps (event, &caps);
432 gst_pad_dispose (GObject * object)
434 GstPad *pad = GST_PAD_CAST (object);
437 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
439 /* unlink the peer pad */
440 if ((peer = gst_pad_get_peer (pad))) {
441 /* window for MT unsafeness, someone else could unlink here
442 * and then we call unlink with wrong pads. The unlink
443 * function would catch this and safely return failed. */
444 if (GST_PAD_IS_SRC (pad))
445 gst_pad_unlink (pad, peer);
447 gst_pad_unlink (peer, pad);
449 gst_object_unref (peer);
452 gst_pad_set_pad_template (pad, NULL);
454 clear_events (pad->priv->events);
456 g_hook_list_clear (&pad->probes);
458 G_OBJECT_CLASS (parent_class)->dispose (object);
462 gst_pad_finalize (GObject * object)
464 GstPad *pad = GST_PAD_CAST (object);
467 /* in case the task is still around, clean it up */
468 if ((task = GST_PAD_TASK (pad))) {
469 gst_task_join (task);
470 GST_PAD_TASK (pad) = NULL;
471 gst_object_unref (task);
474 g_static_rec_mutex_free (&pad->stream_rec_lock);
475 g_cond_free (pad->block_cond);
477 G_OBJECT_CLASS (parent_class)->finalize (object);
481 gst_pad_set_property (GObject * object, guint prop_id,
482 const GValue * value, GParamSpec * pspec)
484 g_return_if_fail (GST_IS_PAD (object));
487 case PAD_PROP_DIRECTION:
488 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
490 case PAD_PROP_TEMPLATE:
491 gst_pad_set_pad_template (GST_PAD_CAST (object),
492 (GstPadTemplate *) g_value_get_object (value));
495 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501 gst_pad_get_property (GObject * object, guint prop_id,
502 GValue * value, GParamSpec * pspec)
504 g_return_if_fail (GST_IS_PAD (object));
508 GST_OBJECT_LOCK (object);
509 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
510 GST_OBJECT_UNLOCK (object);
512 case PAD_PROP_DIRECTION:
513 g_value_set_enum (value, GST_PAD_DIRECTION (object));
515 case PAD_PROP_TEMPLATE:
516 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
526 * @name: the name of the new pad.
527 * @direction: the #GstPadDirection of the pad.
529 * Creates a new pad with the given name in the given direction.
530 * If name is NULL, a guaranteed unique name (across all pads)
532 * This function makes a copy of the name so you can safely free the name.
534 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
539 gst_pad_new (const gchar * name, GstPadDirection direction)
541 return g_object_new (GST_TYPE_PAD,
542 "name", name, "direction", direction, NULL);
546 * gst_pad_new_from_template:
547 * @templ: the pad template to use
548 * @name: the name of the element
550 * Creates a new pad with the given name from the given template.
551 * If name is NULL, a guaranteed unique name (across all pads)
553 * This function makes a copy of the name so you can safely free the name.
555 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
558 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
560 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
562 return g_object_new (GST_TYPE_PAD,
563 "name", name, "direction", templ->direction, "template", templ, NULL);
567 * gst_pad_new_from_static_template:
568 * @templ: the #GstStaticPadTemplate to use
569 * @name: the name of the element
571 * Creates a new pad with the given name from the given static template.
572 * If name is NULL, a guaranteed unique name (across all pads)
574 * This function makes a copy of the name so you can safely free the name.
576 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
579 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
583 GstPadTemplate *template;
585 template = gst_static_pad_template_get (templ);
586 pad = gst_pad_new_from_template (template, name);
587 gst_object_unref (template);
592 * gst_pad_get_direction:
593 * @pad: a #GstPad to get the direction of.
595 * Gets the direction of the pad. The direction of the pad is
596 * decided at construction time so this function does not take
599 * Returns: the #GstPadDirection of the pad.
604 gst_pad_get_direction (GstPad * pad)
606 GstPadDirection result;
608 /* PAD_UNKNOWN is a little silly but we need some sort of
609 * error return value */
610 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
612 result = GST_PAD_DIRECTION (pad);
618 gst_pad_activate_default (GstPad * pad)
620 return gst_pad_activate_push (pad, TRUE);
624 pre_activate (GstPad * pad, GstActivateMode new_mode)
627 case GST_ACTIVATE_PUSH:
628 case GST_ACTIVATE_PULL:
629 GST_OBJECT_LOCK (pad);
630 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
632 GST_PAD_UNSET_FLUSHING (pad);
633 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
634 GST_OBJECT_UNLOCK (pad);
636 case GST_ACTIVATE_NONE:
637 GST_OBJECT_LOCK (pad);
638 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
639 GST_PAD_SET_FLUSHING (pad);
640 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
641 /* unlock blocked pads so element can resume and stop */
642 GST_PAD_BLOCK_BROADCAST (pad);
643 GST_OBJECT_UNLOCK (pad);
649 post_activate (GstPad * pad, GstActivateMode new_mode)
652 case GST_ACTIVATE_PUSH:
653 case GST_ACTIVATE_PULL:
656 case GST_ACTIVATE_NONE:
657 /* ensures that streaming stops */
658 GST_PAD_STREAM_LOCK (pad);
659 GST_DEBUG_OBJECT (pad, "stopped streaming");
660 GST_OBJECT_LOCK (pad);
661 clear_events (pad->priv->events);
662 GST_OBJECT_UNLOCK (pad);
663 GST_PAD_STREAM_UNLOCK (pad);
669 * gst_pad_set_active:
670 * @pad: the #GstPad to activate or deactivate.
671 * @active: whether or not the pad should be active.
673 * Activates or deactivates the given pad.
674 * Normally called from within core state change functions.
676 * If @active, makes sure the pad is active. If it is already active, either in
677 * push or pull mode, just return. Otherwise dispatches to the pad's activate
678 * function to perform the actual activation.
680 * If not @active, checks the pad's current mode and calls
681 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
684 * Returns: #TRUE if the operation was successful.
689 gst_pad_set_active (GstPad * pad, gboolean active)
692 gboolean ret = FALSE;
694 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
696 GST_OBJECT_LOCK (pad);
697 old = GST_PAD_ACTIVATE_MODE (pad);
698 GST_OBJECT_UNLOCK (pad);
702 case GST_ACTIVATE_PUSH:
703 GST_DEBUG_OBJECT (pad, "activating pad from push");
706 case GST_ACTIVATE_PULL:
707 GST_DEBUG_OBJECT (pad, "activating pad from pull");
710 case GST_ACTIVATE_NONE:
711 GST_DEBUG_OBJECT (pad, "activating pad from none");
712 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
715 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
720 case GST_ACTIVATE_PUSH:
721 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
722 ret = gst_pad_activate_push (pad, FALSE);
724 case GST_ACTIVATE_PULL:
725 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
726 ret = gst_pad_activate_pull (pad, FALSE);
728 case GST_ACTIVATE_NONE:
729 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
733 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
739 GST_OBJECT_LOCK (pad);
741 g_critical ("Failed to deactivate pad %s:%s, very bad",
742 GST_DEBUG_PAD_NAME (pad));
744 GST_WARNING_OBJECT (pad, "Failed to activate pad");
746 GST_OBJECT_UNLOCK (pad);
749 GST_OBJECT_LOCK (pad);
750 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
751 GST_OBJECT_UNLOCK (pad);
759 * gst_pad_activate_pull:
760 * @pad: the #GstPad to activate or deactivate.
761 * @active: whether or not the pad should be active.
763 * Activates or deactivates the given pad in pull mode via dispatching to the
764 * pad's activatepullfunc. For use from within pad activation functions only.
765 * When called on sink pads, will first proxy the call to the peer pad, which
766 * is expected to activate its internally linked pads from within its
767 * activate_pull function.
769 * If you don't know what this is, you probably don't want to call it.
771 * Returns: TRUE if the operation was successful.
776 gst_pad_activate_pull (GstPad * pad, gboolean active)
778 GstActivateMode old, new;
781 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
783 GST_OBJECT_LOCK (pad);
784 old = GST_PAD_ACTIVATE_MODE (pad);
785 GST_OBJECT_UNLOCK (pad);
789 case GST_ACTIVATE_PULL:
790 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
792 case GST_ACTIVATE_PUSH:
793 GST_DEBUG_OBJECT (pad,
794 "activating pad from push, deactivate push first");
795 /* pad was activate in the wrong direction, deactivate it
796 * and reactivate it in pull mode */
797 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
798 goto deactivate_failed;
799 /* fallthrough, pad is deactivated now. */
800 case GST_ACTIVATE_NONE:
801 GST_DEBUG_OBJECT (pad, "activating pad from none");
806 case GST_ACTIVATE_NONE:
807 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
809 case GST_ACTIVATE_PUSH:
810 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
811 /* pad was activated in the other direction, deactivate it
812 * in push mode, this should not happen... */
813 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
814 goto deactivate_failed;
815 /* everything is fine now */
817 case GST_ACTIVATE_PULL:
818 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
823 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
824 if ((peer = gst_pad_get_peer (pad))) {
825 GST_DEBUG_OBJECT (pad, "calling peer");
826 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
828 gst_object_unref (peer);
830 /* there is no peer, this is only fatal when we activate. When we
831 * deactivate, we must assume the application has unlinked the peer and
832 * will deactivate it eventually. */
836 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
839 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
840 goto failure; /* Can't activate pull on a src without a
844 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
845 pre_activate (pad, new);
847 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
848 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
851 /* can happen for sinks of passthrough elements */
854 post_activate (pad, new);
856 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
857 active ? "activated" : "deactivated");
863 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
864 active ? "activated" : "deactivated");
869 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
870 "failed to %s in switch to pull from mode %d",
871 (active ? "activate" : "deactivate"), old);
876 GST_OBJECT_LOCK (peer);
877 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
878 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
879 GST_OBJECT_UNLOCK (peer);
880 gst_object_unref (peer);
885 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
891 GST_OBJECT_LOCK (pad);
892 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
893 active ? "activate" : "deactivate");
894 GST_PAD_SET_FLUSHING (pad);
895 GST_PAD_ACTIVATE_MODE (pad) = old;
896 GST_OBJECT_UNLOCK (pad);
902 * gst_pad_activate_push:
903 * @pad: the #GstPad to activate or deactivate.
904 * @active: whether the pad should be active or not.
906 * Activates or deactivates the given pad in push mode via dispatching to the
907 * pad's activatepushfunc. For use from within pad activation functions only.
909 * If you don't know what this is, you probably don't want to call it.
911 * Returns: %TRUE if the operation was successful.
916 gst_pad_activate_push (GstPad * pad, gboolean active)
918 GstActivateMode old, new;
920 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
921 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
922 active ? "activated" : "deactivated");
924 GST_OBJECT_LOCK (pad);
925 old = GST_PAD_ACTIVATE_MODE (pad);
926 GST_OBJECT_UNLOCK (pad);
930 case GST_ACTIVATE_PUSH:
931 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
933 case GST_ACTIVATE_PULL:
934 GST_DEBUG_OBJECT (pad,
935 "activating pad from push, deactivating pull first");
936 /* pad was activate in the wrong direction, deactivate it
937 * an reactivate it in push mode */
938 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
939 goto deactivate_failed;
940 /* fallthrough, pad is deactivated now. */
941 case GST_ACTIVATE_NONE:
942 GST_DEBUG_OBJECT (pad, "activating pad from none");
947 case GST_ACTIVATE_NONE:
948 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
950 case GST_ACTIVATE_PULL:
951 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
952 /* pad was activated in the other direction, deactivate it
953 * in pull mode, this should not happen... */
954 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
955 goto deactivate_failed;
956 /* everything is fine now */
958 case GST_ACTIVATE_PUSH:
959 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
964 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
965 pre_activate (pad, new);
967 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
968 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
972 /* quite ok, element relies on state change func to prepare itself */
975 post_activate (pad, new);
977 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
978 active ? "activated" : "deactivated");
983 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
984 active ? "activated" : "deactivated");
989 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
990 "failed to %s in switch to push from mode %d",
991 (active ? "activate" : "deactivate"), old);
996 GST_OBJECT_LOCK (pad);
997 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
998 active ? "activate" : "deactivate");
999 GST_PAD_SET_FLUSHING (pad);
1000 GST_PAD_ACTIVATE_MODE (pad) = old;
1001 GST_OBJECT_UNLOCK (pad);
1007 * gst_pad_is_active:
1008 * @pad: the #GstPad to query
1010 * Query if a pad is active
1012 * Returns: TRUE if the pad is active.
1017 gst_pad_is_active (GstPad * pad)
1019 gboolean result = FALSE;
1021 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1023 GST_OBJECT_LOCK (pad);
1024 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1025 GST_OBJECT_UNLOCK (pad);
1031 * gst_pad_add_probe:
1032 * @pad: the #GstPad to add the probe to
1033 * @mask: the probe mask
1034 * @callback: #GstPadProbeCallback that will be called with notifications of
1036 * @user_data: (closure): user data passed to the callback
1037 * @destroy_data: #GDestroyNotify for user_data
1039 * Be notified of different states of pads. The provided callback is called for
1040 * every state that matches @mask.
1043 * Pad probe handlers are only called for source pads in push mode
1044 * and sink pads in pull mode.
1047 * Returns: an id or 0 on error. The id can be used to remove the probe with
1048 * gst_pad_remove_probe().
1053 gst_pad_add_probe (GstPad * pad, GstProbeType mask,
1054 GstPadProbeCallback callback, gpointer user_data,
1055 GDestroyNotify destroy_data)
1060 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1061 g_return_val_if_fail (mask != 0, 0);
1063 GST_OBJECT_LOCK (pad);
1064 /* make a new probe */
1065 hook = g_hook_alloc (&pad->probes);
1067 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1070 /* when no contraints are given for the types, assume all types are
1072 if ((mask & GST_PROBE_TYPE_DATA) == 0)
1073 mask |= GST_PROBE_TYPE_DATA;
1074 if ((mask & GST_PROBE_TYPE_SCHEDULING) == 0)
1075 mask |= GST_PROBE_TYPE_SCHEDULING;
1077 /* store our flags and other fields */
1078 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1079 hook->func = callback;
1080 hook->data = user_data;
1081 hook->destroy = destroy_data;
1082 PROBE_COOKIE (hook) = 0;
1084 /* incremenent cookie so that the new hook get's called */
1085 pad->priv->probe_cookie++;
1088 g_hook_prepend (&pad->probes, hook);
1091 /* get the id of the hook, we return this and it can be used to remove the
1093 res = hook->hook_id;
1095 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1097 if (mask & GST_PROBE_TYPE_BLOCKING) {
1098 /* we have a block probe */
1100 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1101 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1102 "now %d blocking probes", pad->num_blocked);
1105 /* call the callback if we need to be called for idle callbacks */
1106 if ((mask & GST_PROBE_TYPE_IDLE) && (callback != NULL)) {
1107 if (pad->priv->using > 0) {
1108 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1109 * flag above, the last thread to leave the push will do the callback. New
1110 * threads going into the push will block. */
1111 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1112 "pad is in use, delay idle callback");
1113 GST_OBJECT_UNLOCK (pad);
1115 /* the pad is idle now, we can signal the idle callback now */
1116 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1117 "pad is idle, trigger idle callback");
1118 GST_OBJECT_UNLOCK (pad);
1120 callback (pad, GST_PROBE_TYPE_IDLE, NULL, user_data);
1123 GST_OBJECT_UNLOCK (pad);
1129 cleanup_hook (GstPad * pad, GHook * hook)
1133 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1135 if (type & GST_PROBE_TYPE_BLOCKING) {
1136 /* unblock when we remove the last blocking probe */
1138 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1140 if (pad->num_blocked == 0) {
1141 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1142 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1143 GST_PAD_BLOCK_BROADCAST (pad);
1146 g_hook_destroy_link (&pad->probes, hook);
1151 * gst_pad_remove_probe:
1152 * @pad: the #GstPad with the probe
1153 * @id: the probe id to remove
1155 * Remove the probe with @id from @pad.
1160 gst_pad_remove_probe (GstPad * pad, gulong id)
1164 g_return_if_fail (GST_IS_PAD (pad));
1166 GST_OBJECT_LOCK (pad);
1168 hook = g_hook_get (&pad->probes, id);
1172 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1174 cleanup_hook (pad, hook);
1175 GST_OBJECT_UNLOCK (pad);
1181 GST_OBJECT_UNLOCK (pad);
1182 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1188 * gst_pad_is_blocked:
1189 * @pad: the #GstPad to query
1191 * Checks if the pad is blocked or not. This function returns the
1192 * last requested state of the pad. It is not certain that the pad
1193 * is actually blocking at this point (see gst_pad_is_blocking()).
1195 * Returns: TRUE if the pad is blocked.
1200 gst_pad_is_blocked (GstPad * pad)
1202 gboolean result = FALSE;
1204 g_return_val_if_fail (GST_IS_PAD (pad), result);
1206 GST_OBJECT_LOCK (pad);
1207 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1208 GST_OBJECT_UNLOCK (pad);
1214 * gst_pad_is_blocking:
1215 * @pad: the #GstPad to query
1217 * Checks if the pad is blocking or not. This is a guaranteed state
1218 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1220 * Returns: TRUE if the pad is blocking.
1227 gst_pad_is_blocking (GstPad * pad)
1229 gboolean result = FALSE;
1231 g_return_val_if_fail (GST_IS_PAD (pad), result);
1233 GST_OBJECT_LOCK (pad);
1234 /* the blocking flag is only valid if the pad is not flushing */
1235 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1236 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1237 GST_OBJECT_UNLOCK (pad);
1243 * gst_pad_set_activate_function:
1245 * @activate: the #GstPadActivateFunction to set.
1247 * Sets the given activate function for @pad. The activate function will
1248 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1249 * the actual activation. Only makes sense to set on sink pads.
1251 * Call this function if your sink pad can start a pull-based task.
1254 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1256 g_return_if_fail (GST_IS_PAD (pad));
1258 GST_PAD_ACTIVATEFUNC (pad) = activate;
1259 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1260 GST_DEBUG_FUNCPTR_NAME (activate));
1264 * gst_pad_set_activatepull_function:
1266 * @activatepull: the #GstPadActivateModeFunction to set.
1268 * Sets the given activate_pull function for the pad. An activate_pull function
1269 * prepares the element and any upstream connections for pulling. See XXX
1270 * part-activation.txt for details.
1273 gst_pad_set_activatepull_function (GstPad * pad,
1274 GstPadActivateModeFunction activatepull)
1276 g_return_if_fail (GST_IS_PAD (pad));
1278 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1279 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1280 GST_DEBUG_FUNCPTR_NAME (activatepull));
1284 * gst_pad_set_activatepush_function:
1286 * @activatepush: the #GstPadActivateModeFunction to set.
1288 * Sets the given activate_push function for the pad. An activate_push function
1289 * prepares the element for pushing. See XXX part-activation.txt for details.
1292 gst_pad_set_activatepush_function (GstPad * pad,
1293 GstPadActivateModeFunction activatepush)
1295 g_return_if_fail (GST_IS_PAD (pad));
1297 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1298 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1299 GST_DEBUG_FUNCPTR_NAME (activatepush));
1303 * gst_pad_set_chain_function:
1304 * @pad: a sink #GstPad.
1305 * @chain: the #GstPadChainFunction to set.
1307 * Sets the given chain function for the pad. The chain function is called to
1308 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1311 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1313 g_return_if_fail (GST_IS_PAD (pad));
1314 g_return_if_fail (GST_PAD_IS_SINK (pad));
1316 GST_PAD_CHAINFUNC (pad) = chain;
1317 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1318 GST_DEBUG_FUNCPTR_NAME (chain));
1322 * gst_pad_set_chain_list_function:
1323 * @pad: a sink #GstPad.
1324 * @chainlist: the #GstPadChainListFunction to set.
1326 * Sets the given chain list function for the pad. The chainlist function is
1327 * called to process a #GstBufferList input buffer list. See
1328 * #GstPadChainListFunction for more details.
1333 gst_pad_set_chain_list_function (GstPad * pad,
1334 GstPadChainListFunction chainlist)
1336 g_return_if_fail (GST_IS_PAD (pad));
1337 g_return_if_fail (GST_PAD_IS_SINK (pad));
1339 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1340 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1341 GST_DEBUG_FUNCPTR_NAME (chainlist));
1345 * gst_pad_set_getrange_function:
1346 * @pad: a source #GstPad.
1347 * @get: the #GstPadGetRangeFunction to set.
1349 * Sets the given getrange function for the pad. The getrange function is
1350 * called to produce a new #GstBuffer to start the processing pipeline. see
1351 * #GstPadGetRangeFunction for a description of the getrange function.
1354 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1356 g_return_if_fail (GST_IS_PAD (pad));
1357 g_return_if_fail (GST_PAD_IS_SRC (pad));
1359 GST_PAD_GETRANGEFUNC (pad) = get;
1361 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1362 GST_DEBUG_FUNCPTR_NAME (get));
1366 * gst_pad_set_event_function:
1367 * @pad: a #GstPad of either direction.
1368 * @event: the #GstPadEventFunction to set.
1370 * Sets the given event handler for the pad.
1373 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1375 g_return_if_fail (GST_IS_PAD (pad));
1377 GST_PAD_EVENTFUNC (pad) = event;
1379 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1380 GST_DEBUG_FUNCPTR_NAME (event));
1384 * gst_pad_set_query_function:
1385 * @pad: a #GstPad of either direction.
1386 * @query: the #GstPadQueryFunction to set.
1388 * Set the given query function for the pad.
1391 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1393 g_return_if_fail (GST_IS_PAD (pad));
1395 GST_PAD_QUERYFUNC (pad) = query;
1397 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1398 GST_DEBUG_FUNCPTR_NAME (query));
1402 * gst_pad_set_query_type_function:
1403 * @pad: a #GstPad of either direction.
1404 * @type_func: the #GstPadQueryTypeFunction to set.
1406 * Set the given query type function for the pad.
1409 gst_pad_set_query_type_function (GstPad * pad,
1410 GstPadQueryTypeFunction type_func)
1412 g_return_if_fail (GST_IS_PAD (pad));
1414 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1416 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1417 GST_DEBUG_FUNCPTR_NAME (type_func));
1421 * gst_pad_get_query_types:
1424 * Get an array of supported queries that can be performed
1427 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1430 const GstQueryType *
1431 gst_pad_get_query_types (GstPad * pad)
1433 GstPadQueryTypeFunction func;
1435 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1437 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1449 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1453 if ((peer = gst_pad_get_peer (pad))) {
1454 *data = gst_pad_get_query_types (peer);
1455 gst_object_unref (peer);
1461 * gst_pad_get_query_types_default:
1464 * Invoke the default query types function on the pad. This function will get
1465 * the supported query type from the peer of an internally linked pad of @pad.
1467 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1468 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1469 * query types function.
1471 const GstQueryType *
1472 gst_pad_get_query_types_default (GstPad * pad)
1474 GstQueryType *result = NULL;
1476 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1478 gst_pad_forward (pad, (GstPadForwardFunction)
1479 gst_pad_get_query_types_dispatcher, &result);
1485 * gst_pad_set_iterate_internal_links_function:
1486 * @pad: a #GstPad of either direction.
1487 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1489 * Sets the given internal link iterator function for the pad.
1494 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1495 GstPadIterIntLinkFunction iterintlink)
1497 g_return_if_fail (GST_IS_PAD (pad));
1499 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1500 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1501 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1505 * gst_pad_set_link_function:
1507 * @link: the #GstPadLinkFunction to set.
1509 * Sets the given link function for the pad. It will be called when
1510 * the pad is linked with another pad.
1512 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1515 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1516 * cannot be made for some reason.
1518 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1519 * of the peer sink pad, if present.
1522 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1524 g_return_if_fail (GST_IS_PAD (pad));
1526 GST_PAD_LINKFUNC (pad) = link;
1527 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1528 GST_DEBUG_FUNCPTR_NAME (link));
1532 * gst_pad_set_unlink_function:
1534 * @unlink: the #GstPadUnlinkFunction to set.
1536 * Sets the given unlink function for the pad. It will be called
1537 * when the pad is unlinked.
1540 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1542 g_return_if_fail (GST_IS_PAD (pad));
1544 GST_PAD_UNLINKFUNC (pad) = unlink;
1545 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1546 GST_DEBUG_FUNCPTR_NAME (unlink));
1550 * gst_pad_set_getcaps_function:
1552 * @getcaps: the #GstPadGetCapsFunction to set.
1554 * Sets the given getcaps function for the pad. @getcaps should return the
1555 * allowable caps for a pad in the context of the element's state, its link to
1556 * other elements, and the devices or files it has opened. These caps must be a
1557 * subset of the pad template caps. In the NULL state with no links, @getcaps
1558 * should ideally return the same caps as the pad template. In rare
1559 * circumstances, an object property can affect the caps returned by @getcaps,
1560 * but this is discouraged.
1562 * You do not need to call this function if @pad's allowed caps are always the
1563 * same as the pad template caps. This can only be true if the padtemplate
1564 * has fixed simple caps.
1566 * For most filters, the caps returned by @getcaps is directly affected by the
1567 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1568 * the srcpad's getcaps function is directly related to the stream data. Again,
1569 * @getcaps should return the most specific caps it reasonably can, since this
1570 * helps with autoplugging.
1572 * Note that the return value from @getcaps is owned by the caller, so the
1573 * caller should unref the caps after usage.
1576 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1578 g_return_if_fail (GST_IS_PAD (pad));
1580 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1581 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1582 GST_DEBUG_FUNCPTR_NAME (getcaps));
1586 * gst_pad_set_acceptcaps_function:
1588 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1590 * Sets the given acceptcaps function for the pad. The acceptcaps function
1591 * will be called to check if the pad can accept the given caps. Setting the
1592 * acceptcaps function to NULL restores the default behaviour of allowing
1593 * any caps that matches the caps from gst_pad_get_caps().
1596 gst_pad_set_acceptcaps_function (GstPad * pad,
1597 GstPadAcceptCapsFunction acceptcaps)
1599 g_return_if_fail (GST_IS_PAD (pad));
1601 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1602 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1603 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1607 * gst_pad_set_fixatecaps_function:
1609 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1611 * Sets the given fixatecaps function for the pad. The fixatecaps function
1612 * will be called whenever the default values for a GstCaps needs to be
1616 gst_pad_set_fixatecaps_function (GstPad * pad,
1617 GstPadFixateCapsFunction fixatecaps)
1619 g_return_if_fail (GST_IS_PAD (pad));
1621 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1622 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1623 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1628 * @srcpad: the source #GstPad to unlink.
1629 * @sinkpad: the sink #GstPad to unlink.
1631 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1632 * signal on both pads.
1634 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1635 * the pads were not linked together.
1640 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1642 gboolean result = FALSE;
1643 GstElement *parent = NULL;
1646 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1647 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1648 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1649 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1651 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1652 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1653 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1655 /* We need to notify the parent before taking any pad locks as the bin in
1656 * question might be waiting for a lock on the pad while holding its lock
1657 * that our message will try to take. */
1658 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1659 if (GST_IS_ELEMENT (parent)) {
1660 gst_element_post_message (parent,
1661 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1662 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1664 gst_object_unref (parent);
1669 GST_OBJECT_LOCK (srcpad);
1670 GST_OBJECT_LOCK (sinkpad);
1672 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1673 goto not_linked_together;
1675 if (GST_PAD_UNLINKFUNC (srcpad)) {
1676 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1678 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1679 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1682 /* first clear peers */
1683 GST_PAD_PEER (srcpad) = NULL;
1684 GST_PAD_PEER (sinkpad) = NULL;
1686 /* clear pending caps if any */
1687 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1688 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1690 GST_OBJECT_UNLOCK (sinkpad);
1691 GST_OBJECT_UNLOCK (srcpad);
1693 /* fire off a signal to each of the pads telling them
1694 * that they've been unlinked */
1695 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1696 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1698 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1699 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1704 if (parent != NULL) {
1705 gst_element_post_message (parent,
1706 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1707 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1708 gst_object_unref (parent);
1713 not_linked_together:
1715 /* we do not emit a warning in this case because unlinking cannot
1716 * be made MT safe.*/
1717 GST_OBJECT_UNLOCK (sinkpad);
1718 GST_OBJECT_UNLOCK (srcpad);
1724 * gst_pad_is_linked:
1725 * @pad: pad to check
1727 * Checks if a @pad is linked to another pad or not.
1729 * Returns: TRUE if the pad is linked, FALSE otherwise.
1734 gst_pad_is_linked (GstPad * pad)
1738 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1740 GST_OBJECT_LOCK (pad);
1741 result = (GST_PAD_PEER (pad) != NULL);
1742 GST_OBJECT_UNLOCK (pad);
1747 /* get the caps from both pads and see if the intersection
1750 * This function should be called with the pad LOCK on both
1754 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1755 GstPadLinkCheck flags)
1757 GstCaps *srccaps = NULL;
1758 GstCaps *sinkcaps = NULL;
1759 gboolean compatible = FALSE;
1761 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1764 /* Doing the expensive caps checking takes priority over only checking the template caps */
1765 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1766 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1767 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1769 /* If one of the two pads doesn't have a template, consider the intersection
1771 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1772 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1776 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1778 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1781 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1783 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1786 /* if we have caps on both pads we can check the intersection. If one
1787 * of the caps is NULL, we return TRUE. */
1788 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1790 gst_caps_unref (srccaps);
1792 gst_caps_unref (sinkcaps);
1796 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1797 gst_caps_unref (srccaps);
1798 gst_caps_unref (sinkcaps);
1801 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1802 (compatible ? "" : "not"));
1807 /* check if the grandparents of both pads are the same.
1808 * This check is required so that we don't try to link
1809 * pads from elements in different bins without ghostpads.
1811 * The LOCK should be held on both pads
1814 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1816 GstObject *psrc, *psink;
1818 psrc = GST_OBJECT_PARENT (src);
1819 psink = GST_OBJECT_PARENT (sink);
1821 /* if one of the pads has no parent, we allow the link */
1822 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1825 /* only care about parents that are elements */
1826 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1827 goto no_element_parent;
1829 /* if the parents are the same, we have a loop */
1830 if (G_UNLIKELY (psrc == psink))
1833 /* if they both have a parent, we check the grandparents. We can not lock
1834 * the parent because we hold on the child (pad) and the locking order is
1835 * parent >> child. */
1836 psrc = GST_OBJECT_PARENT (psrc);
1837 psink = GST_OBJECT_PARENT (psink);
1839 /* if they have grandparents but they are not the same */
1840 if (G_UNLIKELY (psrc != psink))
1841 goto wrong_grandparents;
1848 GST_CAT_DEBUG (GST_CAT_CAPS,
1849 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1850 GST_PTR_FORMAT, psrc, psink);
1855 GST_CAT_DEBUG (GST_CAT_CAPS,
1856 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1857 GST_PTR_FORMAT, psrc, psink);
1862 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1868 GST_CAT_DEBUG (GST_CAT_CAPS,
1869 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1870 GST_PTR_FORMAT, psrc, psink);
1875 /* FIXME leftover from an attempt at refactoring... */
1876 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1877 * the two pads will be locked in the srcpad, sinkpad order. */
1878 static GstPadLinkReturn
1879 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1881 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1882 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1884 GST_OBJECT_LOCK (srcpad);
1886 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1887 goto src_was_linked;
1889 GST_OBJECT_LOCK (sinkpad);
1891 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1892 goto sink_was_linked;
1894 /* check hierarchy, pads can only be linked if the grandparents
1896 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1897 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1898 goto wrong_hierarchy;
1900 /* check pad caps for non-empty intersection */
1901 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1904 /* FIXME check pad scheduling for non-empty intersection */
1906 return GST_PAD_LINK_OK;
1910 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1911 GST_DEBUG_PAD_NAME (srcpad),
1912 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1913 /* we do not emit a warning in this case because unlinking cannot
1914 * be made MT safe.*/
1915 GST_OBJECT_UNLOCK (srcpad);
1916 return GST_PAD_LINK_WAS_LINKED;
1920 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1921 GST_DEBUG_PAD_NAME (sinkpad),
1922 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1923 /* we do not emit a warning in this case because unlinking cannot
1924 * be made MT safe.*/
1925 GST_OBJECT_UNLOCK (sinkpad);
1926 GST_OBJECT_UNLOCK (srcpad);
1927 return GST_PAD_LINK_WAS_LINKED;
1931 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1932 GST_OBJECT_UNLOCK (sinkpad);
1933 GST_OBJECT_UNLOCK (srcpad);
1934 return GST_PAD_LINK_WRONG_HIERARCHY;
1938 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1939 GST_OBJECT_UNLOCK (sinkpad);
1940 GST_OBJECT_UNLOCK (srcpad);
1941 return GST_PAD_LINK_NOFORMAT;
1947 * @srcpad: the source #GstPad.
1948 * @sinkpad: the sink #GstPad.
1950 * Checks if the source pad and the sink pad are compatible so they can be
1953 * Returns: TRUE if the pads can be linked.
1956 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1958 GstPadLinkReturn result;
1960 /* generic checks */
1961 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1962 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1964 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1965 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1967 /* gst_pad_link_prepare does everything for us, we only release the locks
1968 * on the pads that it gets us. If this function returns !OK the locks are not
1970 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1971 if (result != GST_PAD_LINK_OK)
1974 GST_OBJECT_UNLOCK (srcpad);
1975 GST_OBJECT_UNLOCK (sinkpad);
1978 return result == GST_PAD_LINK_OK;
1982 * gst_pad_link_full:
1983 * @srcpad: the source #GstPad to link.
1984 * @sinkpad: the sink #GstPad to link.
1985 * @flags: the checks to validate when linking
1987 * Links the source pad and the sink pad.
1989 * This variant of #gst_pad_link provides a more granular control on the
1990 * checks being done when linking. While providing some considerable speedups
1991 * the caller of this method must be aware that wrong usage of those flags
1992 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1993 * for more information.
1997 * Returns: A result code indicating if the connection worked or
2003 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2005 GstPadLinkReturn result;
2007 GstPadLinkFunction srcfunc, sinkfunc;
2009 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2010 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2011 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2012 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2013 GST_PAD_LINK_WRONG_DIRECTION);
2015 /* Notify the parent early. See gst_pad_unlink for details. */
2016 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2017 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2018 gst_element_post_message (parent,
2019 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2020 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2022 gst_object_unref (parent);
2027 /* prepare will also lock the two pads */
2028 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2030 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2033 /* must set peers before calling the link function */
2034 GST_PAD_PEER (srcpad) = sinkpad;
2035 GST_PAD_PEER (sinkpad) = srcpad;
2037 /* make sure we update events */
2038 prepare_event_update (srcpad, sinkpad);
2040 /* get the link functions */
2041 srcfunc = GST_PAD_LINKFUNC (srcpad);
2042 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2044 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2045 /* custom link functions, execute them */
2046 GST_OBJECT_UNLOCK (sinkpad);
2047 GST_OBJECT_UNLOCK (srcpad);
2050 /* this one will call the peer link function */
2051 result = srcfunc (srcpad, sinkpad);
2052 } else if (sinkfunc) {
2053 /* if no source link function, we need to call the sink link
2054 * function ourselves. */
2055 result = sinkfunc (sinkpad, srcpad);
2058 GST_OBJECT_LOCK (srcpad);
2059 GST_OBJECT_LOCK (sinkpad);
2061 /* we released the lock, check if the same pads are linked still */
2062 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2063 goto concurrent_link;
2065 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2068 GST_OBJECT_UNLOCK (sinkpad);
2069 GST_OBJECT_UNLOCK (srcpad);
2071 /* fire off a signal to each of the pads telling them
2072 * that they've been linked */
2073 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2074 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2076 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2077 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2079 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2082 if (G_LIKELY (parent)) {
2083 gst_element_post_message (parent,
2084 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2085 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2086 gst_object_unref (parent);
2094 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2095 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2096 GST_OBJECT_UNLOCK (sinkpad);
2097 GST_OBJECT_UNLOCK (srcpad);
2099 /* The other link operation succeeded first */
2100 result = GST_PAD_LINK_WAS_LINKED;
2105 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2106 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2108 GST_PAD_PEER (srcpad) = NULL;
2109 GST_PAD_PEER (sinkpad) = NULL;
2111 GST_OBJECT_UNLOCK (sinkpad);
2112 GST_OBJECT_UNLOCK (srcpad);
2120 * @srcpad: the source #GstPad to link.
2121 * @sinkpad: the sink #GstPad to link.
2123 * Links the source pad and the sink pad.
2125 * Returns: A result code indicating if the connection worked or
2131 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2133 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2137 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2139 GstPadTemplate **template_p;
2141 /* this function would need checks if it weren't static */
2143 GST_OBJECT_LOCK (pad);
2144 template_p = &pad->padtemplate;
2145 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2146 GST_OBJECT_UNLOCK (pad);
2149 gst_pad_template_pad_created (templ, pad);
2153 * gst_pad_get_pad_template:
2156 * Gets the template for @pad.
2158 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2159 * instantiated, or %NULL if this pad has no template. Unref after
2163 gst_pad_get_pad_template (GstPad * pad)
2165 GstPadTemplate *templ;
2167 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2169 templ = GST_PAD_PAD_TEMPLATE (pad);
2171 return (templ ? gst_object_ref (templ) : NULL);
2174 /* should be called with the pad LOCK held */
2175 /* refs the caps, so caller is responsible for getting it unreffed */
2177 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2179 GstCaps *result = NULL;
2180 GstPadTemplate *templ;
2181 gboolean fixed_caps;
2183 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2185 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2187 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2188 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2189 "dispatching to pad getcaps function with "
2190 "filter %" GST_PTR_FORMAT, filter);
2192 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2193 GST_OBJECT_UNLOCK (pad);
2194 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2195 GST_OBJECT_LOCK (pad);
2196 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2198 if (result == NULL) {
2199 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2200 GST_DEBUG_PAD_NAME (pad));
2202 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2203 "pad getcaps returned %" GST_PTR_FORMAT, result);
2204 #ifndef G_DISABLE_ASSERT
2205 /* check that the returned caps are a real subset of the template caps */
2206 if (GST_PAD_PAD_TEMPLATE (pad)) {
2207 const GstCaps *templ_caps =
2208 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2209 if (!gst_caps_is_subset (result, templ_caps)) {
2212 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2213 "pad returned caps %" GST_PTR_FORMAT
2214 " which are not a real subset of its template caps %"
2215 GST_PTR_FORMAT, result, templ_caps);
2217 ("pad %s:%s returned caps which are not a real "
2218 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2219 temp = gst_caps_intersect (templ_caps, result);
2220 gst_caps_unref (result);
2225 if (!gst_caps_is_subset (result, filter)) {
2228 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2229 "pad returned caps %" GST_PTR_FORMAT
2230 " which are not a real subset of the filter caps %"
2231 GST_PTR_FORMAT, result, filter);
2232 g_warning ("pad %s:%s returned caps which are not a real "
2233 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2234 /* FIXME: Order? But shouldn't happen anyway... */
2236 gst_caps_intersect_full (filter, result,
2237 GST_CAPS_INTERSECT_FIRST);
2238 gst_caps_unref (result);
2246 if (fixed_caps && (result = get_pad_caps (pad))) {
2248 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2249 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2250 GST_PTR_FORMAT, result, result, filter, filter);
2252 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2253 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2256 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2257 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2258 result = gst_caps_ref (result);
2262 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2263 result = GST_PAD_TEMPLATE_CAPS (templ);
2266 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2267 "using pad template %p with caps %p %" GST_PTR_FORMAT
2268 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2271 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2272 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2275 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2276 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2278 result = gst_caps_ref (result);
2283 if (!fixed_caps && (result = get_pad_caps (pad))) {
2285 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2286 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2287 GST_PTR_FORMAT, result, result, filter, filter);
2289 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2290 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2293 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2294 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2295 result = gst_caps_ref (result);
2301 /* this almost never happens */
2302 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2303 result = gst_caps_new_empty ();
2310 * gst_pad_has_current_caps:
2311 * @pad: a #GstPad to check
2313 * Check if @pad has caps set on it with gst_pad_set_caps().
2315 * Returns: TRUE when @pad has caps associated with it.
2318 gst_pad_has_current_caps (GstPad * pad)
2322 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2324 GST_OBJECT_LOCK (pad);
2325 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2326 result = (get_pad_caps (pad) != NULL);
2327 GST_OBJECT_UNLOCK (pad);
2333 * gst_pad_get_current_caps:
2334 * @pad: a #GstPad to get the current capabilities of.
2336 * Gets the capabilities currently configured on @pad with the last call to
2337 * gst_pad_set_caps().
2339 * Returns: the current caps of the pad with incremented ref-count.
2342 gst_pad_get_current_caps (GstPad * pad)
2346 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2348 GST_OBJECT_LOCK (pad);
2349 if ((result = get_pad_caps (pad)))
2350 gst_caps_ref (result);
2351 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2352 "get current pad caps %" GST_PTR_FORMAT, result);
2353 GST_OBJECT_UNLOCK (pad);
2360 * @pad: a #GstPad to get the capabilities of.
2361 * @filter: suggested #GstCaps.
2363 * Gets the capabilities this pad can produce or consume.
2364 * Note that this method doesn't necessarily return the caps set by
2365 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2366 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2367 * the pad's get_caps function;
2368 * this returns the pad template caps if not explicitly set.
2370 * When called on sinkpads @filter contains the caps that
2371 * upstream could produce in the order preferred by upstream. When
2372 * called on srcpads @filter contains the caps accepted by
2373 * downstream in the preffered order. @filter might be %NULL but
2374 * if it is not %NULL the returned caps will be a subset of @filter.
2376 * Note that this function does not return writable #GstCaps, use
2377 * gst_caps_make_writable() before modifying the caps.
2379 * Returns: the caps of the pad with incremented ref-count.
2382 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2384 GstCaps *result = NULL;
2386 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2387 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2389 GST_OBJECT_LOCK (pad);
2391 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2393 result = gst_pad_get_caps_unlocked (pad, filter);
2395 GST_OBJECT_UNLOCK (pad);
2402 * gst_pad_peer_get_caps:
2403 * @pad: a #GstPad to get the capabilities of.
2404 * @filter: a #GstCaps filter.
2406 * Gets the capabilities of the peer connected to this pad. Similar to
2407 * gst_pad_get_caps().
2409 * When called on srcpads @filter contains the caps that
2410 * upstream could produce in the order preferred by upstream. When
2411 * called on sinkpads @filter contains the caps accepted by
2412 * downstream in the preffered order. @filter might be %NULL but
2413 * if it is not %NULL the returned caps will be a subset of @filter.
2415 * Returns: the caps of the peer pad with incremented ref-count. This function
2416 * returns %NULL when there is no peer pad.
2419 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2422 GstCaps *result = NULL;
2424 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2425 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2427 GST_OBJECT_LOCK (pad);
2429 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2431 peerpad = GST_PAD_PEER (pad);
2432 if (G_UNLIKELY (peerpad == NULL))
2435 gst_object_ref (peerpad);
2436 GST_OBJECT_UNLOCK (pad);
2438 result = gst_pad_get_caps (peerpad, filter);
2440 gst_object_unref (peerpad);
2446 GST_OBJECT_UNLOCK (pad);
2452 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2454 GstStructure *s = data;
2457 if (gst_value_fixate (&v, value)) {
2458 gst_structure_id_set_value (s, field_id, &v);
2465 gst_pad_default_fixate (GstPad * pad, GstCaps * caps)
2469 /* default fixation */
2470 gst_caps_truncate (caps);
2471 s = gst_caps_get_structure (caps, 0);
2472 gst_structure_foreach (s, default_fixate, s);
2476 * gst_pad_fixate_caps:
2477 * @pad: a #GstPad to fixate
2478 * @caps: the #GstCaps to fixate
2480 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2481 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2484 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2486 GstPadFixateCapsFunction fixatefunc;
2488 g_return_if_fail (GST_IS_PAD (pad));
2489 g_return_if_fail (caps != NULL);
2490 g_return_if_fail (!gst_caps_is_empty (caps));
2491 g_return_if_fail (!gst_caps_is_any (caps));
2493 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2496 g_return_if_fail (gst_caps_is_writable (caps));
2498 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2500 fixatefunc (pad, caps);
2502 gst_pad_default_fixate (pad, caps);
2505 /* Default accept caps implementation just checks against
2506 * against the allowed caps for the pad */
2508 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2510 /* get the caps and see if it intersects to something not empty */
2512 gboolean result = FALSE;
2514 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2516 allowed = gst_pad_get_caps (pad, NULL);
2518 goto nothing_allowed;
2520 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2522 result = gst_caps_can_intersect (allowed, caps);
2524 gst_caps_unref (allowed);
2531 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2537 * gst_pad_accept_caps:
2538 * @pad: a #GstPad to check
2539 * @caps: a #GstCaps to check on the pad
2541 * Check if the given pad accepts the caps.
2543 * Returns: TRUE if the pad can accept the caps.
2546 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2549 GstPadAcceptCapsFunction acceptfunc;
2551 GstCaps *existing = NULL;
2554 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2556 /* any pad can be unnegotiated */
2560 /* lock for checking the existing caps */
2561 GST_OBJECT_LOCK (pad);
2562 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2564 /* The current caps on a pad are trivially acceptable */
2565 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2566 if (caps == existing || gst_caps_is_equal (caps, existing))
2570 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2571 GST_OBJECT_UNLOCK (pad);
2573 if (G_LIKELY (acceptfunc)) {
2574 /* we can call the function */
2575 result = acceptfunc (pad, caps);
2576 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2578 /* Only null if the element explicitly unset it */
2579 result = gst_pad_acceptcaps_default (pad, caps);
2580 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2587 GST_DEBUG_OBJECT (pad, "pad had same caps");
2588 GST_OBJECT_UNLOCK (pad);
2595 * gst_pad_peer_accept_caps:
2596 * @pad: a #GstPad to check the peer of
2597 * @caps: a #GstCaps to check on the pad
2599 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2602 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2605 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2610 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2612 GST_OBJECT_LOCK (pad);
2614 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2616 peerpad = GST_PAD_PEER (pad);
2617 if (G_UNLIKELY (peerpad == NULL))
2620 gst_object_ref (peerpad);
2621 /* release lock before calling external methods but keep ref to pad */
2622 GST_OBJECT_UNLOCK (pad);
2624 result = gst_pad_accept_caps (peerpad, caps);
2626 gst_object_unref (peerpad);
2632 GST_OBJECT_UNLOCK (pad);
2639 * @pad: a #GstPad to set the capabilities of.
2640 * @caps: (transfer none): a #GstCaps to set.
2642 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2643 * caps on the pad will be unreffed. This function refs the caps so you should
2644 * unref if as soon as you don't need it anymore.
2645 * It is possible to set NULL caps, which will make the pad unnegotiated
2648 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2649 * or bad parameters were provided to this function.
2654 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2657 gboolean res = TRUE;
2659 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2660 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2662 event = gst_event_new_caps (caps);
2664 if (GST_PAD_IS_SRC (pad))
2665 res = gst_pad_push_event (pad, event);
2667 res = gst_pad_send_event (pad, event);
2673 do_event_function (GstPad * pad, GstEvent * event,
2674 GstPadEventFunction eventfunc)
2676 gboolean result = TRUE;
2677 GstCaps *caps, *templ;
2679 switch (GST_EVENT_TYPE (event)) {
2680 case GST_EVENT_CAPS:
2682 /* backwards compatibility mode for caps */
2683 gst_event_parse_caps (event, &caps);
2685 /* See if pad accepts the caps */
2686 templ = gst_pad_get_pad_template_caps (pad);
2687 if (!gst_caps_can_intersect (caps, templ))
2690 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2692 gst_caps_unref (templ);
2699 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2700 result = eventfunc (pad, event);
2707 gst_caps_unref (templ);
2708 gst_event_unref (event);
2709 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2710 "caps %" GST_PTR_FORMAT " not accepted", caps);
2715 /* function to send all pending events on the sinkpad to the event
2716 * function and collect the results. This function should be called with
2717 * the object lock. The object lock might be released by this function.
2719 static GstFlowReturn
2720 gst_pad_update_events (GstPad * pad)
2722 GstFlowReturn ret = GST_FLOW_OK;
2724 GstPadEventFunction eventfunc;
2727 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2731 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2735 ev = &pad->priv->events[i];
2737 /* skip without pending event */
2738 if ((event = ev->pending) == NULL)
2741 gst_event_ref (event);
2742 GST_OBJECT_UNLOCK (pad);
2744 res = do_event_function (pad, event, eventfunc);
2746 GST_OBJECT_LOCK (pad);
2747 /* things could have changed while we release the lock, check if we still
2748 * are handling the same event, if we don't something changed and we have
2749 * to try again. FIXME. we need a cookie here. */
2750 if (event != ev->pending) {
2751 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2755 /* remove the event from the pending entry in all cases */
2759 /* make the event active */
2761 gst_event_unref (ev->event);
2764 gst_event_unref (event);
2765 ret = GST_FLOW_ERROR;
2768 /* when we get here all events were successfully updated. */
2775 g_warning ("pad %s:%s has no event handler, file a bug.",
2776 GST_DEBUG_PAD_NAME (pad));
2777 return GST_FLOW_NOT_SUPPORTED;
2782 * gst_pad_get_pad_template_caps:
2783 * @pad: a #GstPad to get the template capabilities from.
2785 * Gets the capabilities for @pad's template.
2787 * Returns: (transfer full): the #GstCaps of this pad template.
2788 * Unref after usage.
2791 gst_pad_get_pad_template_caps (GstPad * pad)
2793 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2795 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2797 if (GST_PAD_PAD_TEMPLATE (pad))
2798 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2800 return gst_static_caps_get (&anycaps);
2805 * @pad: a #GstPad to get the peer of.
2807 * Gets the peer of @pad. This function refs the peer pad so
2808 * you need to unref it after use.
2810 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2815 gst_pad_get_peer (GstPad * pad)
2819 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2821 GST_OBJECT_LOCK (pad);
2822 result = GST_PAD_PEER (pad);
2824 gst_object_ref (result);
2825 GST_OBJECT_UNLOCK (pad);
2831 * gst_pad_get_allowed_caps:
2834 * Gets the capabilities of the allowed media types that can flow through
2835 * @pad and its peer.
2837 * The allowed capabilities is calculated as the intersection of the results of
2838 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2839 * on the resulting caps.
2841 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2842 * caps when you no longer need it. This function returns NULL when @pad
2848 gst_pad_get_allowed_caps (GstPad * pad)
2855 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2857 GST_OBJECT_LOCK (pad);
2858 peer = GST_PAD_PEER (pad);
2859 if (G_UNLIKELY (peer == NULL))
2862 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2864 gst_object_ref (peer);
2865 GST_OBJECT_UNLOCK (pad);
2866 mycaps = gst_pad_get_caps (pad, NULL);
2868 peercaps = gst_pad_get_caps (peer, NULL);
2869 gst_object_unref (peer);
2871 caps = gst_caps_intersect (mycaps, peercaps);
2872 gst_caps_unref (peercaps);
2873 gst_caps_unref (mycaps);
2875 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2882 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2883 GST_OBJECT_UNLOCK (pad);
2890 * gst_pad_get_negotiated_caps:
2893 * Gets the capabilities of the media type that currently flows through @pad
2896 * This function can be used on both src and sinkpads. Note that srcpads are
2897 * always negotiated before sinkpads so it is possible that the negotiated caps
2898 * on the srcpad do not match the negotiated caps of the peer.
2900 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2901 * the caps when you no longer need it. This function returns NULL when
2902 * the @pad has no peer or is not negotiated yet.
2907 gst_pad_get_negotiated_caps (GstPad * pad)
2909 GstCaps *caps = NULL;
2912 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2914 GST_OBJECT_LOCK (pad);
2916 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2919 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2921 if ((caps = get_pad_caps (pad)))
2922 gst_caps_ref (caps);
2924 GST_OBJECT_UNLOCK (pad);
2926 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2933 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2934 GST_OBJECT_UNLOCK (pad);
2940 * gst_pad_iterate_internal_links_default:
2941 * @pad: the #GstPad to get the internal links of.
2943 * Iterate the list of pads to which the given pad is linked to inside of
2944 * the parent element.
2945 * This is the default handler, and thus returns an iterator of all of the
2946 * pads inside the parent element with opposite direction.
2948 * The caller must free this iterator after use with gst_iterator_free().
2950 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2951 * returned pad with gst_object_unref().
2956 gst_pad_iterate_internal_links_default (GstPad * pad)
2964 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2969 GST_OBJECT_LOCK (pad);
2970 parent = GST_PAD_PARENT (pad);
2971 if (!parent || !GST_IS_ELEMENT (parent))
2974 gst_object_ref (parent);
2975 GST_OBJECT_UNLOCK (pad);
2977 if (pad->direction == GST_PAD_SRC)
2978 padlist = &parent->sinkpads;
2980 padlist = &parent->srcpads;
2982 GST_DEBUG_OBJECT (pad, "Making iterator");
2984 cookie = &parent->pads_cookie;
2986 lock = GST_OBJECT_GET_LOCK (parent);
2989 res = gst_iterator_new_list (GST_TYPE_PAD,
2990 lock, cookie, padlist, (GObject *) owner, NULL);
2992 gst_object_unref (owner);
2999 GST_OBJECT_UNLOCK (pad);
3000 GST_DEBUG_OBJECT (pad, "no parent element");
3006 * gst_pad_iterate_internal_links:
3007 * @pad: the GstPad to get the internal links of.
3009 * Gets an iterator for the pads to which the given pad is linked to inside
3010 * of the parent element.
3012 * Each #GstPad element yielded by the iterator will have its refcount increased,
3013 * so unref after use.
3015 * Free-function: gst_iterator_free
3017 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3018 * pad does not have an iterator function configured. Use
3019 * gst_iterator_free() after usage.
3024 gst_pad_iterate_internal_links (GstPad * pad)
3026 GstIterator *res = NULL;
3028 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3030 if (GST_PAD_ITERINTLINKFUNC (pad))
3031 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3039 * @forward: a #GstPadForwardFunction
3040 * @user_data: user data passed to @forward
3042 * Calls @forward for all internally linked pads of @pad. This function deals with
3043 * dynamically changing internal pads and will make sure that the @forward
3044 * function is only called once for each pad.
3046 * When @forward returns TRUE, no further pads will be processed.
3048 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3051 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3054 gboolean result = FALSE;
3056 gboolean done = FALSE;
3057 GValue item = { 0, };
3058 GList *pushed_pads = NULL;
3060 iter = gst_pad_iterate_internal_links (pad);
3066 switch (gst_iterator_next (iter, &item)) {
3067 case GST_ITERATOR_OK:
3071 intpad = g_value_get_object (&item);
3073 /* if already pushed, skip. FIXME, find something faster to tag pads */
3074 if (g_list_find (pushed_pads, intpad)) {
3075 g_value_reset (&item);
3079 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3080 GST_DEBUG_PAD_NAME (intpad));
3081 done = result = forward (intpad, user_data);
3083 pushed_pads = g_list_prepend (pushed_pads, intpad);
3085 g_value_reset (&item);
3088 case GST_ITERATOR_RESYNC:
3089 /* We don't reset the result here because we don't push the event
3090 * again on pads that got the event already and because we need
3091 * to consider the result of the previous pushes */
3092 gst_iterator_resync (iter);
3094 case GST_ITERATOR_ERROR:
3095 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3098 case GST_ITERATOR_DONE:
3103 g_value_unset (&item);
3104 gst_iterator_free (iter);
3106 g_list_free (pushed_pads);
3116 gboolean dispatched;
3120 event_forward_func (GstPad * pad, EventData * data)
3122 /* for each pad we send to, we should ref the event; it's up
3123 * to downstream to unref again when handled. */
3124 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3125 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3127 data->res |= gst_pad_push_event (pad, gst_event_ref (data->event));
3129 data->dispatched = TRUE;
3136 * gst_pad_event_forward:
3138 * @event: (transfer full): the #GstEvent to handle.
3140 * Forward @event to all internally linked pads of @pad. This function takes
3141 * ownership of @event.
3143 * Returns: TRUE if the event was sent succesfully.
3146 gst_pad_event_forward (GstPad * pad, GstEvent * event)
3151 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3152 g_return_val_if_fail (event != NULL, FALSE);
3157 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3159 gst_event_unref (event);
3161 /* for sinkpads without a parent element or without internal links, nothing
3162 * will be dispatched but we still want to return TRUE. */
3163 if (data.dispatched)
3172 * gst_pad_event_default:
3173 * @pad: a #GstPad to call the default event handler on.
3174 * @event: (transfer full): the #GstEvent to handle.
3176 * Invokes the default event handler for the given pad.
3178 * The EOS event will pause the task associated with @pad before it is forwarded
3179 * to all internally linked pads,
3181 * The CAPS event will never be forwarded.
3183 * The the event is sent to all pads internally linked to @pad. This function
3184 * takes ownership of @event.
3186 * Returns: TRUE if the event was sent succesfully.
3189 gst_pad_event_default (GstPad * pad, GstEvent * event)
3191 gboolean result = TRUE, forward = TRUE;
3193 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3194 g_return_val_if_fail (event != NULL, FALSE);
3196 GST_LOG_OBJECT (pad, "default event handler");
3198 switch (GST_EVENT_TYPE (event)) {
3201 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3202 gst_pad_pause_task (pad);
3205 case GST_EVENT_CAPS:
3207 /* don't forward by default */
3216 result = gst_pad_event_forward (pad, event);
3218 gst_event_unref (event);
3225 * @pad: a #GstPad to invoke the default query on.
3226 * @query: (transfer none): the #GstQuery to perform.
3228 * Dispatches a query to a pad. The query should have been allocated by the
3229 * caller via one of the type-specific allocation functions. The element that
3230 * the pad belongs to is responsible for filling the query with an appropriate
3231 * response, which should then be parsed with a type-specific query parsing
3234 * Again, the caller is responsible for both the allocation and deallocation of
3235 * the query structure.
3237 * Please also note that some queries might need a running pipeline to work.
3239 * Returns: TRUE if the query could be performed.
3242 gst_pad_query (GstPad * pad, GstQuery * query)
3245 GstPadQueryFunction func;
3247 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3248 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3250 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3251 GST_QUERY_TYPE_NAME (query));
3253 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3256 res = func (pad, query);
3258 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3259 GST_QUERY_TYPE_NAME (query), res);
3265 GST_DEBUG_OBJECT (pad, "had no query function");
3271 * gst_pad_peer_query:
3272 * @pad: a #GstPad to invoke the peer query on.
3273 * @query: (transfer none): the #GstQuery to perform.
3275 * Performs gst_pad_query() on the peer of @pad.
3277 * The caller is responsible for both the allocation and deallocation of
3278 * the query structure.
3280 * Returns: TRUE if the query could be performed. This function returns %FALSE
3281 * if @pad has no peer.
3286 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3291 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3292 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3294 GST_OBJECT_LOCK (pad);
3296 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3297 GST_QUERY_TYPE_NAME (query));
3299 peerpad = GST_PAD_PEER (pad);
3300 if (G_UNLIKELY (peerpad == NULL))
3303 gst_object_ref (peerpad);
3304 GST_OBJECT_UNLOCK (pad);
3306 result = gst_pad_query (peerpad, query);
3308 gst_object_unref (peerpad);
3315 GST_WARNING_OBJECT (pad, "pad has no peer");
3316 GST_OBJECT_UNLOCK (pad);
3322 * gst_pad_query_default:
3323 * @pad: a #GstPad to call the default query handler on.
3324 * @query: (transfer none): the #GstQuery to handle.
3326 * Invokes the default query handler for the given pad.
3327 * The query is sent to all pads internally linked to @pad. Note that
3328 * if there are many possible sink pads that are internally linked to
3329 * @pad, only one will be sent the query.
3330 * Multi-sinkpad elements should implement custom query handlers.
3332 * Returns: TRUE if the query was performed succesfully.
3335 gst_pad_query_default (GstPad * pad, GstQuery * query)
3337 switch (GST_QUERY_TYPE (query)) {
3338 case GST_QUERY_POSITION:
3339 case GST_QUERY_SEEKING:
3340 case GST_QUERY_FORMATS:
3341 case GST_QUERY_LATENCY:
3342 case GST_QUERY_JITTER:
3343 case GST_QUERY_RATE:
3344 case GST_QUERY_CONVERT:
3346 return gst_pad_forward
3347 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3352 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3354 GstPad *pad = data->pad;
3356 GstPadProbeCallback callback;
3359 /* if we have called this callback, do nothing */
3360 if (PROBE_COOKIE (hook) == data->cookie)
3363 PROBE_COOKIE (hook) = data->cookie;
3365 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3367 /* one of the data types */
3368 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3370 /* one of the scheduling types */
3371 if ((flags & GST_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3373 /* all of the blocking types must match */
3374 if ((flags & GST_PROBE_TYPE_BLOCKING) !=
3375 (data->mask & GST_PROBE_TYPE_BLOCKING))
3378 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3379 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3381 callback = (GstPadProbeCallback) hook->func;
3382 if (callback == NULL)
3385 GST_OBJECT_UNLOCK (pad);
3387 ret = callback (pad, data->mask, data->type_data, hook->data);
3389 GST_OBJECT_LOCK (pad);
3392 case GST_PROBE_REMOVE:
3393 /* remove the probe */
3394 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3395 cleanup_hook (pad, hook);
3397 case GST_PROBE_DROP:
3398 /* need to drop the data, make sure other probes don't get called
3400 GST_DEBUG_OBJECT (pad, "asked to drop item");
3401 data->mask = GST_PROBE_TYPE_INVALID;
3402 data->ret = GST_PROBE_DROP;
3404 case GST_PROBE_PASS:
3405 /* inform the pad block to let things pass */
3406 GST_DEBUG_OBJECT (pad, "asked to pass item");
3410 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3415 #define PROBE(pad,mask,data,label) \
3417 if (G_UNLIKELY (pad->num_probes)) { \
3418 ret = do_probe_callbacks (pad, mask, data); \
3419 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3424 static GstFlowReturn
3425 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3432 data.type_data = type_data;
3433 data.ret = GST_PROBE_OK;
3435 data.cookie = pad->priv->probe_cookie++;
3438 cookie = pad->priv->probe_cookie;
3440 g_hook_list_marshal (&pad->probes, FALSE,
3441 (GHookMarshaller) probe_hook_marshal, &data);
3443 /* if the list changed, call the new callbacks (they will not have their
3444 * cookie set to data.cookie */
3445 if (cookie != pad->priv->probe_cookie) {
3446 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3447 "probe list changed, restarting");
3451 if (data.ret == GST_PROBE_DROP)
3457 if (mask & GST_PROBE_TYPE_BLOCK) {
3458 while (GST_PAD_IS_BLOCKED (pad)) {
3459 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3460 "we are blocked %d times", pad->num_blocked);
3462 /* we might have released the lock */
3463 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3466 /* now we block the streaming thread. It can be unlocked when we
3467 * deactivate the pad (which will also set the FLUSHING flag) or
3468 * when the pad is unblocked. A flushing event will also unblock
3469 * the pad after setting the FLUSHING flag. */
3470 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3471 "Waiting to be unblocked or set flushing");
3472 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3473 GST_PAD_BLOCK_WAIT (pad);
3474 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3475 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3477 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3487 GST_DEBUG_OBJECT (pad, "pad is flushing");
3488 return GST_FLOW_WRONG_STATE;
3492 GST_DEBUG_OBJECT (pad, "data is dropped");
3493 return GST_FLOW_CUSTOM_SUCCESS;
3497 GST_DEBUG_OBJECT (pad, "data is passed");
3505 * gst_pad_get_offset:
3508 * Get the offset applied to the running time of @pad. @pad has to be a source
3511 * Returns: the offset.
3514 gst_pad_get_offset (GstPad * pad)
3518 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3520 GST_OBJECT_LOCK (pad);
3521 result = pad->offset;
3522 GST_OBJECT_UNLOCK (pad);
3528 * gst_pad_set_offset:
3530 * @offset: the offset
3532 * Set the offset that will be applied to the running time of @pad.
3535 gst_pad_set_offset (GstPad * pad, gint64 offset)
3541 g_return_if_fail (GST_IS_PAD (pad));
3543 GST_OBJECT_LOCK (pad);
3544 /* if nothing changed, do nothing */
3545 if (pad->offset == offset)
3548 pad->offset = offset;
3550 /* if no peer, we just updated the offset */
3551 if ((peer = GST_PAD_PEER (pad)) == NULL)
3554 /* switch pads around when dealing with a sinkpad */
3555 if (GST_PAD_IS_SINK (pad)) {
3556 /* ref the peer so it doesn't go away when we release the lock */
3557 tmp = gst_object_ref (peer);
3558 /* make sure we get the peer (the srcpad) */
3559 GST_OBJECT_UNLOCK (pad);
3565 GST_OBJECT_LOCK (pad);
3566 /* check if the pad didn't get relinked */
3567 if (GST_PAD_PEER (pad) != peer)
3570 /* we can release the ref now */
3571 gst_object_unref (peer);
3574 /* the index of the segment event in the array */
3575 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3577 /* lock order is srcpad >> sinkpad */
3578 GST_OBJECT_LOCK (peer);
3579 /* take the current segment event, adjust it and then place
3580 * it on the sinkpad. events on the srcpad are always active. */
3581 if (replace_event (pad, peer, idx))
3582 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3584 GST_OBJECT_UNLOCK (peer);
3587 GST_OBJECT_UNLOCK (pad);
3591 /**********************************************************************
3592 * Data passing functions
3595 /* this is the chain function that does not perform the additional argument
3596 * checking for that little extra speed.
3598 static inline GstFlowReturn
3599 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3602 gboolean needs_events;
3604 GST_PAD_STREAM_LOCK (pad);
3606 GST_OBJECT_LOCK (pad);
3607 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3610 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3611 if (G_UNLIKELY (needs_events)) {
3612 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3614 GST_DEBUG_OBJECT (pad, "need to update all events");
3615 ret = gst_pad_update_events (pad);
3616 if (G_UNLIKELY (ret != GST_FLOW_OK))
3620 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3622 GST_OBJECT_UNLOCK (pad);
3624 /* NOTE: we read the chainfunc unlocked.
3625 * we cannot hold the lock for the pad so we might send
3626 * the data to the wrong function. This is not really a
3627 * problem since functions are assigned at creation time
3628 * and don't change that often... */
3629 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3630 GstPadChainFunction chainfunc;
3632 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3636 "calling chainfunction &%s with buffer %p",
3637 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3639 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3641 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3642 "called chainfunction &%s with buffer %p, returned %s",
3643 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3645 GstPadChainListFunction chainlistfunc;
3647 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3650 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3651 "calling chainlistfunction &%s",
3652 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3654 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3656 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3657 "called chainlistfunction &%s, returned %s",
3658 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3661 GST_PAD_STREAM_UNLOCK (pad);
3668 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3669 "chaining, but pad was flushing");
3670 GST_OBJECT_UNLOCK (pad);
3671 GST_PAD_STREAM_UNLOCK (pad);
3672 return GST_FLOW_WRONG_STATE;
3676 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3677 GST_OBJECT_UNLOCK (pad);
3678 GST_PAD_STREAM_UNLOCK (pad);
3683 GST_OBJECT_UNLOCK (pad);
3684 GST_PAD_STREAM_UNLOCK (pad);
3685 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3688 case GST_FLOW_CUSTOM_SUCCESS:
3689 GST_DEBUG_OBJECT (pad, "dropped buffer");
3693 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3700 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3701 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3702 "pushing, but not chainhandler");
3703 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3704 ("push on pad %s:%s but it has no chainfunction",
3705 GST_DEBUG_PAD_NAME (pad)));
3706 GST_PAD_STREAM_UNLOCK (pad);
3707 return GST_FLOW_NOT_SUPPORTED;
3713 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3714 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3717 * Chain a buffer to @pad.
3719 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3721 * If the buffer type is not acceptable for @pad (as negotiated with a
3722 * preceeding GST_EVENT_CAPS event), this function returns
3723 * #GST_FLOW_NOT_NEGOTIATED.
3725 * The function proceeds calling the chain function installed on @pad (see
3726 * gst_pad_set_chain_function()) and the return value of that function is
3727 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3730 * In all cases, success or failure, the caller loses its reference to @buffer
3731 * after calling this function.
3733 * Returns: a #GstFlowReturn from the pad.
3738 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3740 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3741 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3742 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3744 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3747 static GstFlowReturn
3748 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3754 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3756 len = gst_buffer_list_len (list);
3759 for (i = 0; i < len; i++) {
3760 buffer = gst_buffer_list_get (list, i);
3762 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3763 gst_buffer_ref (buffer));
3764 if (ret != GST_FLOW_OK)
3767 gst_buffer_list_unref (list);
3773 * gst_pad_chain_list:
3774 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3775 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3778 * Chain a bufferlist to @pad.
3780 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3782 * If @pad was not negotiated properly with a CAPS event, this function
3783 * returns #GST_FLOW_NOT_NEGOTIATED.
3785 * The function proceeds calling the chainlist function installed on @pad (see
3786 * gst_pad_set_chain_list_function()) and the return value of that function is
3787 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3788 * chainlist function.
3790 * In all cases, success or failure, the caller loses its reference to @list
3791 * after calling this function.
3795 * Returns: a #GstFlowReturn from the pad.
3800 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3802 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3803 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3804 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3806 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3809 static GstFlowReturn
3810 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3815 GST_OBJECT_LOCK (pad);
3816 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3819 type |= GST_PROBE_TYPE_PUSH;
3821 /* do block probes */
3822 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3824 /* do post-blocking probes */
3825 PROBE (pad, type, data, probe_stopped);
3827 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3830 /* take ref to peer pad before releasing the lock */
3831 gst_object_ref (peer);
3833 GST_OBJECT_UNLOCK (pad);
3835 ret = gst_pad_chain_data_unchecked (peer, type, data);
3837 gst_object_unref (peer);
3839 GST_OBJECT_LOCK (pad);
3841 if (pad->priv->using == 0) {
3842 /* pad is not active anymore, trigger idle callbacks */
3843 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3845 GST_OBJECT_UNLOCK (pad);
3849 /* ERROR recovery here */
3853 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3854 "pushing, but pad was flushing");
3855 GST_OBJECT_UNLOCK (pad);
3856 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3857 return GST_FLOW_WRONG_STATE;
3861 GST_OBJECT_UNLOCK (pad);
3862 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3865 case GST_FLOW_CUSTOM_SUCCESS:
3866 GST_DEBUG_OBJECT (pad, "dropped buffer");
3870 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3877 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3878 "pushing, but it was not linked");
3879 GST_OBJECT_UNLOCK (pad);
3880 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3881 return GST_FLOW_NOT_LINKED;
3887 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3888 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3891 * Pushes a buffer to the peer of @pad.
3893 * This function will call installed block probes before triggering any
3894 * installed data probes.
3896 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3897 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3900 * In all cases, success or failure, the caller loses its reference to @buffer
3901 * after calling this function.
3903 * Returns: a #GstFlowReturn from the peer pad.
3908 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3910 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3911 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3912 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3914 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
3918 * gst_pad_push_list:
3919 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3920 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3923 * Pushes a buffer list to the peer of @pad.
3925 * This function will call installed block probes before triggering any
3926 * installed data probes.
3928 * The function proceeds calling the chain function on the peer pad and returns
3929 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3930 * be returned. If the peer pad does not have any installed chainlist function
3931 * every group buffer of the list will be merged into a normal #GstBuffer and
3932 * chained via gst_pad_chain().
3934 * In all cases, success or failure, the caller loses its reference to @list
3935 * after calling this function.
3937 * Returns: a #GstFlowReturn from the peer pad.
3944 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3946 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3947 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3948 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3950 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3953 static GstFlowReturn
3954 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3955 GstBuffer ** buffer)
3958 GstPadGetRangeFunction getrangefunc;
3960 GST_PAD_STREAM_LOCK (pad);
3962 GST_OBJECT_LOCK (pad);
3963 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3965 GST_OBJECT_UNLOCK (pad);
3967 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3970 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3971 "calling getrangefunc %s, offset %"
3972 G_GUINT64_FORMAT ", size %u",
3973 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3975 ret = getrangefunc (pad, offset, size, buffer);
3977 if (G_UNLIKELY (ret != GST_FLOW_OK))
3978 goto get_range_failed;
3980 /* can only fire the signal if we have a valid buffer */
3981 GST_OBJECT_LOCK (pad);
3982 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
3984 GST_OBJECT_UNLOCK (pad);
3986 GST_PAD_STREAM_UNLOCK (pad);
3993 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3994 "getrange, but pad was flushing");
3995 GST_OBJECT_UNLOCK (pad);
3996 GST_PAD_STREAM_UNLOCK (pad);
3997 return GST_FLOW_WRONG_STATE;
4001 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4002 ("getrange on pad %s:%s but it has no getrangefunction",
4003 GST_DEBUG_PAD_NAME (pad)));
4004 GST_PAD_STREAM_UNLOCK (pad);
4005 return GST_FLOW_NOT_SUPPORTED;
4009 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4010 "probe returned %s", gst_flow_get_name (ret));
4011 GST_OBJECT_UNLOCK (pad);
4012 GST_PAD_STREAM_UNLOCK (pad);
4013 gst_buffer_unref (*buffer);
4019 GST_PAD_STREAM_UNLOCK (pad);
4021 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4022 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4023 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4029 * gst_pad_get_range:
4030 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4031 * @offset: The start offset of the buffer
4032 * @size: The length of the buffer
4033 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4034 * returns #GST_FLOW_ERROR if %NULL.
4036 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4037 * immediatly and @buffer is %NULL.
4039 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4040 * description of a getrange function. If @pad has no getrange function
4041 * installed (see gst_pad_set_getrange_function()) this function returns
4042 * #GST_FLOW_NOT_SUPPORTED.
4044 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4046 * Returns: a #GstFlowReturn from the pad.
4051 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4052 GstBuffer ** buffer)
4054 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4055 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4056 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4058 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4062 * gst_pad_pull_range:
4063 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4064 * @offset: The start offset of the buffer
4065 * @size: The length of the buffer
4066 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4067 * GST_FLOW_ERROR if %NULL.
4069 * Pulls a @buffer from the peer pad.
4071 * This function will first trigger the pad block signal if it was
4074 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4075 * function returns the result of gst_pad_get_range() on the peer pad.
4076 * See gst_pad_get_range() for a list of return values and for the
4077 * semantics of the arguments of this function.
4079 * @buffer's caps must either be unset or the same as what is already
4080 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4083 * Returns: a #GstFlowReturn from the peer pad.
4084 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4085 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4086 * @buffer may not be used or freed when any other return value than
4087 * #GST_FLOW_OK is returned.
4092 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4093 GstBuffer ** buffer)
4097 gboolean needs_events;
4099 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4100 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4101 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4103 GST_OBJECT_LOCK (pad);
4104 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4107 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4110 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4113 gst_object_ref (peer);
4115 GST_OBJECT_UNLOCK (pad);
4117 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4119 gst_object_unref (peer);
4121 GST_OBJECT_LOCK (pad);
4123 if (pad->priv->using == 0) {
4124 /* pad is not active anymore, trigger idle callbacks */
4125 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4126 post_probe_stopped);
4129 if (G_UNLIKELY (ret != GST_FLOW_OK))
4130 goto pull_range_failed;
4132 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4133 post_probe_stopped);
4135 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4136 if (G_UNLIKELY (needs_events)) {
4137 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4139 GST_DEBUG_OBJECT (pad, "we need to update the events");
4140 ret = gst_pad_update_events (pad);
4141 if (G_UNLIKELY (ret != GST_FLOW_OK))
4144 GST_OBJECT_UNLOCK (pad);
4148 /* ERROR recovery here */
4151 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4152 "pullrange, but pad was flushing");
4153 GST_OBJECT_UNLOCK (pad);
4154 return GST_FLOW_WRONG_STATE;
4158 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4159 gst_flow_get_name (ret));
4160 GST_OBJECT_UNLOCK (pad);
4165 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4166 "pulling range, but it was not linked");
4167 GST_OBJECT_UNLOCK (pad);
4168 return GST_FLOW_NOT_LINKED;
4173 GST_OBJECT_UNLOCK (pad);
4174 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4175 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4176 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4181 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4182 "post probe returned %s", gst_flow_get_name (ret));
4183 GST_OBJECT_UNLOCK (pad);
4184 if (ret == GST_FLOW_OK)
4185 gst_buffer_unref (*buffer);
4191 GST_OBJECT_UNLOCK (pad);
4192 gst_buffer_unref (*buffer);
4194 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4195 "pullrange returned events that were not accepted");
4201 * gst_pad_push_event:
4202 * @pad: a #GstPad to push the event to.
4203 * @event: (transfer full): the #GstEvent to send to the pad.
4205 * Sends the event to the peer of the given pad. This function is
4206 * mainly used by elements to send events to their peer
4209 * This function takes owership of the provided event so you should
4210 * gst_event_ref() it if you want to reuse the event after this call.
4212 * Returns: TRUE if the event was handled.
4217 gst_pad_push_event (GstPad * pad, GstEvent * event)
4221 gboolean result, do_event_actions = TRUE;
4222 gboolean stored = FALSE;
4224 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4225 g_return_val_if_fail (event != NULL, FALSE);
4226 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4228 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4230 GST_OBJECT_LOCK (pad);
4232 peerpad = GST_PAD_PEER (pad);
4234 /* Two checks to be made:
4235 * . (un)set the FLUSHING flag for flushing events,
4236 * . handle pad blocking */
4237 switch (GST_EVENT_TYPE (event)) {
4238 case GST_EVENT_FLUSH_START:
4239 GST_PAD_SET_FLUSHING (pad);
4241 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4242 /* flush start will have set the FLUSHING flag and will then
4243 * unlock all threads doing a GCond wait on the blocking pad. This
4244 * will typically unblock the STREAMING thread blocked on a pad. */
4245 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4246 "doing block signal.");
4247 GST_PAD_BLOCK_BROADCAST (pad);
4251 case GST_EVENT_FLUSH_STOP:
4252 GST_PAD_UNSET_FLUSHING (pad);
4253 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4254 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4260 /* store the event on the pad, but only on srcpads */
4261 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4264 idx = GST_EVENT_STICKY_IDX (event);
4265 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4266 GST_EVENT_TYPE_NAME (event), idx);
4268 /* srcpad sticky events always become active immediately */
4269 gst_event_replace (&pad->priv->events[idx].event, event);
4274 /* backwards compatibility mode for caps */
4275 if (do_event_actions) {
4276 do_event_actions = FALSE;
4278 switch (GST_EVENT_TYPE (event)) {
4279 case GST_EVENT_CAPS:
4281 GST_OBJECT_UNLOCK (pad);
4283 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4285 GST_OBJECT_LOCK (pad);
4286 /* the peerpad might have changed. Things we checked above could not
4288 peerpad = GST_PAD_PEER (pad);
4291 case GST_EVENT_SEGMENT:
4295 offset = pad->offset;
4296 /* check if we need to adjust the segment */
4297 if (offset != 0 && (peerpad != NULL)) {
4300 /* copy segment values */
4301 gst_event_copy_segment (event, &segment);
4302 gst_event_unref (event);
4304 /* adjust and make a new event with the offset applied */
4305 segment.base += offset;
4306 event = gst_event_new_segment (&segment);
4310 case GST_EVENT_RECONFIGURE:
4311 if (GST_PAD_IS_SINK (pad))
4312 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4318 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4321 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4322 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4328 /* send probes after modifying the events above */
4329 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4331 /* now check the peer pad */
4332 if (peerpad == NULL)
4335 gst_object_ref (peerpad);
4337 GST_OBJECT_UNLOCK (pad);
4339 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4340 GST_EVENT_TYPE_NAME (event), peerpad);
4342 result = gst_pad_send_event (peerpad, event);
4344 /* Note: we gave away ownership of the event at this point */
4345 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4348 gst_object_unref (peerpad);
4350 GST_OBJECT_LOCK (pad);
4352 if (pad->priv->using == 0) {
4353 /* pad is not active anymore, trigger idle callbacks */
4354 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4356 GST_OBJECT_UNLOCK (pad);
4358 return result | stored;
4360 /* ERROR handling */
4363 GST_DEBUG_OBJECT (pad, "We're flushing");
4364 GST_OBJECT_UNLOCK (pad);
4365 gst_event_unref (event);
4370 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4371 GST_OBJECT_UNLOCK (pad);
4372 gst_event_unref (event);
4377 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4378 GST_OBJECT_UNLOCK (pad);
4379 gst_event_unref (event);
4385 * gst_pad_send_event:
4386 * @pad: a #GstPad to send the event to.
4387 * @event: (transfer full): the #GstEvent to send to the pad.
4389 * Sends the event to the pad. This function can be used
4390 * by applications to send events in the pipeline.
4392 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4393 * sink pad, @event should be a downstream event. For example, you would not
4394 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4395 * Furthermore, some downstream events have to be serialized with data flow,
4396 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4397 * the event needs to be serialized with data flow, this function will take the
4398 * pad's stream lock while calling its event function.
4400 * To find out whether an event type is upstream, downstream, or downstream and
4401 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4402 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4403 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4404 * plugin doesn't need to bother itself with this information; the core handles
4405 * all necessary locks and checks.
4407 * This function takes owership of the provided event so you should
4408 * gst_event_ref() it if you want to reuse the event after this call.
4410 * Returns: TRUE if the event was handled.
4413 gst_pad_send_event (GstPad * pad, GstEvent * event)
4416 gboolean result = FALSE;
4417 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4419 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4420 g_return_val_if_fail (event != NULL, FALSE);
4422 GST_OBJECT_LOCK (pad);
4423 if (GST_PAD_IS_SINK (pad)) {
4424 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4425 goto wrong_direction;
4426 serialized = GST_EVENT_IS_SERIALIZED (event);
4427 sticky = GST_EVENT_IS_STICKY (event);
4428 } else if (GST_PAD_IS_SRC (pad)) {
4429 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4430 goto wrong_direction;
4431 /* events on srcpad never are serialized and sticky */
4432 serialized = sticky = FALSE;
4434 goto unknown_direction;
4436 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4438 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4440 switch (GST_EVENT_TYPE (event)) {
4441 case GST_EVENT_FLUSH_START:
4442 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4443 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4445 /* can't even accept a flush begin event when flushing */
4446 if (GST_PAD_IS_FLUSHING (pad))
4449 GST_PAD_SET_FLUSHING (pad);
4450 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4451 needs_events = FALSE;
4453 case GST_EVENT_FLUSH_STOP:
4454 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4455 GST_PAD_UNSET_FLUSHING (pad);
4456 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4458 GST_OBJECT_UNLOCK (pad);
4459 /* grab stream lock */
4460 GST_PAD_STREAM_LOCK (pad);
4462 GST_OBJECT_LOCK (pad);
4463 needs_events = FALSE;
4465 case GST_EVENT_RECONFIGURE:
4466 if (GST_PAD_IS_SRC (pad))
4467 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4469 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4470 GST_EVENT_TYPE_NAME (event));
4473 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4474 GST_OBJECT_UNLOCK (pad);
4475 GST_PAD_STREAM_LOCK (pad);
4477 GST_OBJECT_LOCK (pad);
4479 /* don't forward events on non-serialized events */
4480 needs_events = FALSE;
4483 /* store the event on the pad, but only on srcpads. We need to store the
4484 * event before checking the flushing flag. */
4489 switch (GST_EVENT_TYPE (event)) {
4490 case GST_EVENT_SEGMENT:
4491 if (pad->offset != 0) {
4494 /* copy segment values */
4495 gst_event_copy_segment (event, &segment);
4496 gst_event_unref (event);
4498 /* adjust and make a new event with the offset applied */
4499 segment.base += pad->offset;
4500 event = gst_event_new_segment (&segment);
4507 idx = GST_EVENT_STICKY_IDX (event);
4508 ev = &pad->priv->events[idx];
4510 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4511 GST_EVENT_TYPE_NAME (event), idx);
4513 if (ev->event != event) {
4514 gst_event_replace (&ev->pending, event);
4515 /* set the flag so that we update the events next time. We would
4516 * usually update below but we might be flushing too. */
4517 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4518 needs_events = TRUE;
4521 /* now do the probe */
4522 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4525 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4531 if (G_UNLIKELY (needs_events)) {
4534 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4536 GST_DEBUG_OBJECT (pad, "need to update all events");
4537 ret = gst_pad_update_events (pad);
4538 if (ret != GST_FLOW_OK)
4540 GST_OBJECT_UNLOCK (pad);
4542 gst_event_unref (event);
4546 GstPadEventFunction eventfunc;
4548 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4551 GST_OBJECT_UNLOCK (pad);
4553 result = do_event_function (pad, event, eventfunc);
4557 GST_PAD_STREAM_UNLOCK (pad);
4559 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4563 /* ERROR handling */
4566 g_warning ("pad %s:%s sending %s event in wrong direction",
4567 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4568 GST_OBJECT_UNLOCK (pad);
4569 gst_event_unref (event);
4574 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4575 GST_OBJECT_UNLOCK (pad);
4576 gst_event_unref (event);
4581 g_warning ("pad %s:%s has no event handler, file a bug.",
4582 GST_DEBUG_PAD_NAME (pad));
4583 GST_OBJECT_UNLOCK (pad);
4585 GST_PAD_STREAM_UNLOCK (pad);
4586 gst_event_unref (event);
4591 GST_OBJECT_UNLOCK (pad);
4593 GST_PAD_STREAM_UNLOCK (pad);
4594 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4595 "Received event on flushing pad. Discarding");
4596 gst_event_unref (event);
4601 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4602 GST_OBJECT_UNLOCK (pad);
4604 GST_PAD_STREAM_UNLOCK (pad);
4605 gst_event_unref (event);
4610 GST_OBJECT_UNLOCK (pad);
4612 GST_PAD_STREAM_UNLOCK (pad);
4613 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4614 gst_event_unref (event);
4620 * gst_pad_set_element_private:
4621 * @pad: the #GstPad to set the private data of.
4622 * @priv: The private data to attach to the pad.
4624 * Set the given private data gpointer on the pad.
4625 * This function can only be used by the element that owns the pad.
4626 * No locking is performed in this function.
4629 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4631 pad->element_private = priv;
4635 * gst_pad_get_element_private:
4636 * @pad: the #GstPad to get the private data of.
4638 * Gets the private data of a pad.
4639 * No locking is performed in this function.
4641 * Returns: (transfer none): a #gpointer to the private data.
4644 gst_pad_get_element_private (GstPad * pad)
4646 return pad->element_private;
4650 * gst_pad_get_sticky_event:
4651 * @pad: the #GstPad to get the event from.
4652 * @event_type: the #GstEventType that should be retrieved.
4654 * Returns a new reference of the sticky event of type @event_type
4657 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4660 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4662 GstEvent *event = NULL;
4665 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4666 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4668 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4670 GST_OBJECT_LOCK (pad);
4671 if ((event = pad->priv->events[idx].event)) {
4672 gst_event_ref (event);
4674 GST_OBJECT_UNLOCK (pad);
4680 * gst_pad_sticky_events_foreach:
4681 * @pad: the #GstPad that should be used for iteration.
4682 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4683 * @user_data: (closure): the optional user data.
4685 * Iterates all active sticky events on @pad and calls @foreach_func for every
4686 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4687 * is immediately stopped.
4689 * Returns: GST_FLOW_OK if iteration was successful
4692 gst_pad_sticky_events_foreach (GstPad * pad,
4693 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4695 GstFlowReturn ret = GST_FLOW_OK;
4699 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4700 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4702 GST_OBJECT_LOCK (pad);
4705 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4709 ev = &pad->priv->events[i];
4711 /* skip without active event */
4712 if ((event = ev->event) == NULL)
4715 gst_event_ref (event);
4716 GST_OBJECT_UNLOCK (pad);
4718 res = foreach_func (pad, event, user_data);
4720 GST_OBJECT_LOCK (pad);
4721 gst_event_unref (event);
4723 if (res != GST_FLOW_OK) {
4728 /* things could have changed while we release the lock, check if we still
4729 * are handling the same event, if we don't something changed and we have
4730 * to try again. FIXME. we need a cookie here. */
4731 if (event != ev->event) {
4732 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4736 GST_OBJECT_UNLOCK (pad);
4742 do_stream_status (GstPad * pad, GstStreamStatusType type,
4743 GThread * thread, GstTask * task)
4747 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4749 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4750 if (GST_IS_ELEMENT (parent)) {
4751 GstMessage *message;
4752 GValue value = { 0 };
4754 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4755 gchar *tname, *ename, *pname;
4757 /* create a good task name */
4758 ename = gst_element_get_name (parent);
4759 pname = gst_pad_get_name (pad);
4760 tname = g_strdup_printf ("%s:%s", ename, pname);
4764 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4768 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4771 g_value_init (&value, GST_TYPE_TASK);
4772 g_value_set_object (&value, task);
4773 gst_message_set_stream_status_object (message, &value);
4774 g_value_unset (&value);
4776 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4777 gst_element_post_message (parent, message);
4779 gst_object_unref (parent);
4784 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4786 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4791 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4793 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4797 static GstTaskThreadCallbacks thr_callbacks = {
4803 * gst_pad_start_task:
4804 * @pad: the #GstPad to start the task of
4805 * @func: the task function to call
4806 * @data: data passed to the task function
4808 * Starts a task that repeatedly calls @func with @data. This function
4809 * is mostly used in pad activation functions to start the dataflow.
4810 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4811 * before @func is called.
4813 * Returns: a %TRUE if the task could be started.
4816 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4821 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4822 g_return_val_if_fail (func != NULL, FALSE);
4824 GST_DEBUG_OBJECT (pad, "start task");
4826 GST_OBJECT_LOCK (pad);
4827 task = GST_PAD_TASK (pad);
4829 task = gst_task_create (func, data);
4830 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4831 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4832 GST_DEBUG_OBJECT (pad, "created task");
4833 GST_PAD_TASK (pad) = task;
4834 gst_object_ref (task);
4835 /* release lock to post the message */
4836 GST_OBJECT_UNLOCK (pad);
4838 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4840 gst_object_unref (task);
4842 GST_OBJECT_LOCK (pad);
4843 /* nobody else is supposed to have changed the pad now */
4844 if (GST_PAD_TASK (pad) != task)
4845 goto concurrent_stop;
4847 res = gst_task_set_state (task, GST_TASK_STARTED);
4848 GST_OBJECT_UNLOCK (pad);
4855 GST_OBJECT_UNLOCK (pad);
4861 * gst_pad_pause_task:
4862 * @pad: the #GstPad to pause the task of
4864 * Pause the task of @pad. This function will also wait until the
4865 * function executed by the task is finished if this function is not
4866 * called from the task function.
4868 * Returns: a TRUE if the task could be paused or FALSE when the pad
4872 gst_pad_pause_task (GstPad * pad)
4877 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4879 GST_DEBUG_OBJECT (pad, "pause task");
4881 GST_OBJECT_LOCK (pad);
4882 task = GST_PAD_TASK (pad);
4885 res = gst_task_set_state (task, GST_TASK_PAUSED);
4886 GST_OBJECT_UNLOCK (pad);
4888 /* wait for task function to finish, this lock is recursive so it does nothing
4889 * when the pause is called from the task itself */
4890 GST_PAD_STREAM_LOCK (pad);
4891 GST_PAD_STREAM_UNLOCK (pad);
4897 GST_DEBUG_OBJECT (pad, "pad has no task");
4898 GST_OBJECT_UNLOCK (pad);
4904 * gst_pad_stop_task:
4905 * @pad: the #GstPad to stop the task of
4907 * Stop the task of @pad. This function will also make sure that the
4908 * function executed by the task will effectively stop if not called
4909 * from the GstTaskFunction.
4911 * This function will deadlock if called from the GstTaskFunction of
4912 * the task. Use gst_task_pause() instead.
4914 * Regardless of whether the pad has a task, the stream lock is acquired and
4915 * released so as to ensure that streaming through this pad has finished.
4917 * Returns: a TRUE if the task could be stopped or FALSE on error.
4920 gst_pad_stop_task (GstPad * pad)
4925 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4927 GST_DEBUG_OBJECT (pad, "stop task");
4929 GST_OBJECT_LOCK (pad);
4930 task = GST_PAD_TASK (pad);
4933 GST_PAD_TASK (pad) = NULL;
4934 res = gst_task_set_state (task, GST_TASK_STOPPED);
4935 GST_OBJECT_UNLOCK (pad);
4937 GST_PAD_STREAM_LOCK (pad);
4938 GST_PAD_STREAM_UNLOCK (pad);
4940 if (!gst_task_join (task))
4943 gst_object_unref (task);
4949 GST_DEBUG_OBJECT (pad, "no task");
4950 GST_OBJECT_UNLOCK (pad);
4952 GST_PAD_STREAM_LOCK (pad);
4953 GST_PAD_STREAM_UNLOCK (pad);
4955 /* this is not an error */
4960 /* this is bad, possibly the application tried to join the task from
4961 * the task's thread. We install the task again so that it will be stopped
4962 * again from the right thread next time hopefully. */
4963 GST_OBJECT_LOCK (pad);
4964 GST_DEBUG_OBJECT (pad, "join failed");
4965 /* we can only install this task if there was no other task */
4966 if (GST_PAD_TASK (pad) == NULL)
4967 GST_PAD_TASK (pad) = task;
4968 GST_OBJECT_UNLOCK (pad);