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.
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_IS_ACTIVE (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_check_reconfigure:
1244 * @pad: the #GstPad to check
1246 * Check and clear the #GST_PAD_NEED_RECONFIGURE flag on @pad and return %TRUE
1247 * if the flag was set.
1249 * Returns: %TRUE is the GST_PAD_NEED_RECONFIGURE flag was set on @pad.
1252 gst_pad_check_reconfigure (GstPad * pad)
1254 gboolean reconfigure;
1256 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1258 GST_OBJECT_LOCK (pad);
1259 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1260 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
1261 GST_OBJECT_UNLOCK (pad);
1267 * gst_pad_mark_reconfigure:
1268 * @pad: the #GstPad to mark
1270 * Mark a pad for needing reconfiguration. The next call to
1271 * gst_pad_check_reconfigure() will return %TRUE after this call.
1274 gst_pad_mark_reconfigure (GstPad * pad)
1276 g_return_if_fail (GST_IS_PAD (pad));
1278 GST_OBJECT_LOCK (pad);
1279 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
1280 GST_OBJECT_UNLOCK (pad);
1284 * gst_pad_set_activate_function:
1286 * @activate: the #GstPadActivateFunction to set.
1288 * Sets the given activate function for @pad. The activate function will
1289 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1290 * the actual activation. Only makes sense to set on sink pads.
1292 * Call this function if your sink pad can start a pull-based task.
1295 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1297 g_return_if_fail (GST_IS_PAD (pad));
1299 GST_PAD_ACTIVATEFUNC (pad) = activate;
1300 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1301 GST_DEBUG_FUNCPTR_NAME (activate));
1305 * gst_pad_set_activatepull_function:
1307 * @activatepull: the #GstPadActivateModeFunction to set.
1309 * Sets the given activate_pull function for the pad. An activate_pull function
1310 * prepares the element and any upstream connections for pulling. See XXX
1311 * part-activation.txt for details.
1314 gst_pad_set_activatepull_function (GstPad * pad,
1315 GstPadActivateModeFunction activatepull)
1317 g_return_if_fail (GST_IS_PAD (pad));
1319 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1320 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1321 GST_DEBUG_FUNCPTR_NAME (activatepull));
1325 * gst_pad_set_activatepush_function:
1327 * @activatepush: the #GstPadActivateModeFunction to set.
1329 * Sets the given activate_push function for the pad. An activate_push function
1330 * prepares the element for pushing. See XXX part-activation.txt for details.
1333 gst_pad_set_activatepush_function (GstPad * pad,
1334 GstPadActivateModeFunction activatepush)
1336 g_return_if_fail (GST_IS_PAD (pad));
1338 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1339 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1340 GST_DEBUG_FUNCPTR_NAME (activatepush));
1344 * gst_pad_set_chain_function:
1345 * @pad: a sink #GstPad.
1346 * @chain: the #GstPadChainFunction to set.
1348 * Sets the given chain function for the pad. The chain function is called to
1349 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1352 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1354 g_return_if_fail (GST_IS_PAD (pad));
1355 g_return_if_fail (GST_PAD_IS_SINK (pad));
1357 GST_PAD_CHAINFUNC (pad) = chain;
1358 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1359 GST_DEBUG_FUNCPTR_NAME (chain));
1363 * gst_pad_set_chain_list_function:
1364 * @pad: a sink #GstPad.
1365 * @chainlist: the #GstPadChainListFunction to set.
1367 * Sets the given chain list function for the pad. The chainlist function is
1368 * called to process a #GstBufferList input buffer list. See
1369 * #GstPadChainListFunction for more details.
1374 gst_pad_set_chain_list_function (GstPad * pad,
1375 GstPadChainListFunction chainlist)
1377 g_return_if_fail (GST_IS_PAD (pad));
1378 g_return_if_fail (GST_PAD_IS_SINK (pad));
1380 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1381 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1382 GST_DEBUG_FUNCPTR_NAME (chainlist));
1386 * gst_pad_set_getrange_function:
1387 * @pad: a source #GstPad.
1388 * @get: the #GstPadGetRangeFunction to set.
1390 * Sets the given getrange function for the pad. The getrange function is
1391 * called to produce a new #GstBuffer to start the processing pipeline. see
1392 * #GstPadGetRangeFunction for a description of the getrange function.
1395 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1397 g_return_if_fail (GST_IS_PAD (pad));
1398 g_return_if_fail (GST_PAD_IS_SRC (pad));
1400 GST_PAD_GETRANGEFUNC (pad) = get;
1402 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1403 GST_DEBUG_FUNCPTR_NAME (get));
1407 * gst_pad_set_event_function:
1408 * @pad: a #GstPad of either direction.
1409 * @event: the #GstPadEventFunction to set.
1411 * Sets the given event handler for the pad.
1414 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1416 g_return_if_fail (GST_IS_PAD (pad));
1418 GST_PAD_EVENTFUNC (pad) = event;
1420 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1421 GST_DEBUG_FUNCPTR_NAME (event));
1425 * gst_pad_set_query_function:
1426 * @pad: a #GstPad of either direction.
1427 * @query: the #GstPadQueryFunction to set.
1429 * Set the given query function for the pad.
1432 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1434 g_return_if_fail (GST_IS_PAD (pad));
1436 GST_PAD_QUERYFUNC (pad) = query;
1438 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1439 GST_DEBUG_FUNCPTR_NAME (query));
1443 * gst_pad_set_query_type_function:
1444 * @pad: a #GstPad of either direction.
1445 * @type_func: the #GstPadQueryTypeFunction to set.
1447 * Set the given query type function for the pad.
1450 gst_pad_set_query_type_function (GstPad * pad,
1451 GstPadQueryTypeFunction type_func)
1453 g_return_if_fail (GST_IS_PAD (pad));
1455 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1457 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1458 GST_DEBUG_FUNCPTR_NAME (type_func));
1462 * gst_pad_get_query_types:
1465 * Get an array of supported queries that can be performed
1468 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1471 const GstQueryType *
1472 gst_pad_get_query_types (GstPad * pad)
1474 GstPadQueryTypeFunction func;
1476 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1478 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1490 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1494 if ((peer = gst_pad_get_peer (pad))) {
1495 *data = gst_pad_get_query_types (peer);
1496 gst_object_unref (peer);
1502 * gst_pad_get_query_types_default:
1505 * Invoke the default query types function on the pad. This function will get
1506 * the supported query type from the peer of an internally linked pad of @pad.
1508 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1509 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1510 * query types function.
1512 const GstQueryType *
1513 gst_pad_get_query_types_default (GstPad * pad)
1515 GstQueryType *result = NULL;
1517 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1519 gst_pad_forward (pad, (GstPadForwardFunction)
1520 gst_pad_get_query_types_dispatcher, &result);
1526 * gst_pad_set_iterate_internal_links_function:
1527 * @pad: a #GstPad of either direction.
1528 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1530 * Sets the given internal link iterator function for the pad.
1535 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1536 GstPadIterIntLinkFunction iterintlink)
1538 g_return_if_fail (GST_IS_PAD (pad));
1540 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1541 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1542 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1546 * gst_pad_set_link_function:
1548 * @link: the #GstPadLinkFunction to set.
1550 * Sets the given link function for the pad. It will be called when
1551 * the pad is linked with another pad.
1553 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1556 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1557 * cannot be made for some reason.
1559 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1560 * of the peer sink pad, if present.
1563 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1565 g_return_if_fail (GST_IS_PAD (pad));
1567 GST_PAD_LINKFUNC (pad) = link;
1568 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1569 GST_DEBUG_FUNCPTR_NAME (link));
1573 * gst_pad_set_unlink_function:
1575 * @unlink: the #GstPadUnlinkFunction to set.
1577 * Sets the given unlink function for the pad. It will be called
1578 * when the pad is unlinked.
1581 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1583 g_return_if_fail (GST_IS_PAD (pad));
1585 GST_PAD_UNLINKFUNC (pad) = unlink;
1586 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1587 GST_DEBUG_FUNCPTR_NAME (unlink));
1591 * gst_pad_set_getcaps_function:
1593 * @getcaps: the #GstPadGetCapsFunction to set.
1595 * Sets the given getcaps function for the pad. @getcaps should return the
1596 * allowable caps for a pad in the context of the element's state, its link to
1597 * other elements, and the devices or files it has opened. These caps must be a
1598 * subset of the pad template caps. In the NULL state with no links, @getcaps
1599 * should ideally return the same caps as the pad template. In rare
1600 * circumstances, an object property can affect the caps returned by @getcaps,
1601 * but this is discouraged.
1603 * You do not need to call this function if @pad's allowed caps are always the
1604 * same as the pad template caps. This can only be true if the padtemplate
1605 * has fixed simple caps.
1607 * For most filters, the caps returned by @getcaps is directly affected by the
1608 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1609 * the srcpad's getcaps function is directly related to the stream data. Again,
1610 * @getcaps should return the most specific caps it reasonably can, since this
1611 * helps with autoplugging.
1613 * Note that the return value from @getcaps is owned by the caller, so the
1614 * caller should unref the caps after usage.
1617 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1619 g_return_if_fail (GST_IS_PAD (pad));
1621 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1622 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1623 GST_DEBUG_FUNCPTR_NAME (getcaps));
1627 * gst_pad_set_acceptcaps_function:
1629 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1631 * Sets the given acceptcaps function for the pad. The acceptcaps function
1632 * will be called to check if the pad can accept the given caps. Setting the
1633 * acceptcaps function to NULL restores the default behaviour of allowing
1634 * any caps that matches the caps from gst_pad_get_caps().
1637 gst_pad_set_acceptcaps_function (GstPad * pad,
1638 GstPadAcceptCapsFunction acceptcaps)
1640 g_return_if_fail (GST_IS_PAD (pad));
1642 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1643 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1644 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1648 * gst_pad_set_fixatecaps_function:
1650 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1652 * Sets the given fixatecaps function for the pad. The fixatecaps function
1653 * will be called whenever the default values for a GstCaps needs to be
1657 gst_pad_set_fixatecaps_function (GstPad * pad,
1658 GstPadFixateCapsFunction fixatecaps)
1660 g_return_if_fail (GST_IS_PAD (pad));
1662 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1663 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1664 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1669 * @srcpad: the source #GstPad to unlink.
1670 * @sinkpad: the sink #GstPad to unlink.
1672 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1673 * signal on both pads.
1675 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1676 * the pads were not linked together.
1681 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1683 gboolean result = FALSE;
1684 GstElement *parent = NULL;
1687 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1688 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1689 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1690 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1692 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1693 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1694 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1696 /* We need to notify the parent before taking any pad locks as the bin in
1697 * question might be waiting for a lock on the pad while holding its lock
1698 * that our message will try to take. */
1699 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1700 if (GST_IS_ELEMENT (parent)) {
1701 gst_element_post_message (parent,
1702 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1703 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1705 gst_object_unref (parent);
1710 GST_OBJECT_LOCK (srcpad);
1711 GST_OBJECT_LOCK (sinkpad);
1713 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1714 goto not_linked_together;
1716 if (GST_PAD_UNLINKFUNC (srcpad)) {
1717 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1719 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1720 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1723 /* first clear peers */
1724 GST_PAD_PEER (srcpad) = NULL;
1725 GST_PAD_PEER (sinkpad) = NULL;
1727 /* clear pending caps if any */
1728 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1729 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1731 GST_OBJECT_UNLOCK (sinkpad);
1732 GST_OBJECT_UNLOCK (srcpad);
1734 /* fire off a signal to each of the pads telling them
1735 * that they've been unlinked */
1736 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1737 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1739 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1740 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1745 if (parent != NULL) {
1746 gst_element_post_message (parent,
1747 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1748 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1749 gst_object_unref (parent);
1754 not_linked_together:
1756 /* we do not emit a warning in this case because unlinking cannot
1757 * be made MT safe.*/
1758 GST_OBJECT_UNLOCK (sinkpad);
1759 GST_OBJECT_UNLOCK (srcpad);
1765 * gst_pad_is_linked:
1766 * @pad: pad to check
1768 * Checks if a @pad is linked to another pad or not.
1770 * Returns: TRUE if the pad is linked, FALSE otherwise.
1775 gst_pad_is_linked (GstPad * pad)
1779 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1781 GST_OBJECT_LOCK (pad);
1782 result = (GST_PAD_PEER (pad) != NULL);
1783 GST_OBJECT_UNLOCK (pad);
1788 /* get the caps from both pads and see if the intersection
1791 * This function should be called with the pad LOCK on both
1795 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1796 GstPadLinkCheck flags)
1798 GstCaps *srccaps = NULL;
1799 GstCaps *sinkcaps = NULL;
1800 gboolean compatible = FALSE;
1802 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1805 /* Doing the expensive caps checking takes priority over only checking the template caps */
1806 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1807 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1808 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1810 /* If one of the two pads doesn't have a template, consider the intersection
1812 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1813 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1817 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1819 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1822 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1824 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1827 /* if we have caps on both pads we can check the intersection. If one
1828 * of the caps is NULL, we return TRUE. */
1829 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1831 gst_caps_unref (srccaps);
1833 gst_caps_unref (sinkcaps);
1837 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1838 gst_caps_unref (srccaps);
1839 gst_caps_unref (sinkcaps);
1842 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1843 (compatible ? "" : "not"));
1848 /* check if the grandparents of both pads are the same.
1849 * This check is required so that we don't try to link
1850 * pads from elements in different bins without ghostpads.
1852 * The LOCK should be held on both pads
1855 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1857 GstObject *psrc, *psink;
1859 psrc = GST_OBJECT_PARENT (src);
1860 psink = GST_OBJECT_PARENT (sink);
1862 /* if one of the pads has no parent, we allow the link */
1863 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1866 /* only care about parents that are elements */
1867 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1868 goto no_element_parent;
1870 /* if the parents are the same, we have a loop */
1871 if (G_UNLIKELY (psrc == psink))
1874 /* if they both have a parent, we check the grandparents. We can not lock
1875 * the parent because we hold on the child (pad) and the locking order is
1876 * parent >> child. */
1877 psrc = GST_OBJECT_PARENT (psrc);
1878 psink = GST_OBJECT_PARENT (psink);
1880 /* if they have grandparents but they are not the same */
1881 if (G_UNLIKELY (psrc != psink))
1882 goto wrong_grandparents;
1889 GST_CAT_DEBUG (GST_CAT_CAPS,
1890 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1891 GST_PTR_FORMAT, psrc, psink);
1896 GST_CAT_DEBUG (GST_CAT_CAPS,
1897 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1898 GST_PTR_FORMAT, psrc, psink);
1903 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1909 GST_CAT_DEBUG (GST_CAT_CAPS,
1910 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1911 GST_PTR_FORMAT, psrc, psink);
1916 /* FIXME leftover from an attempt at refactoring... */
1917 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1918 * the two pads will be locked in the srcpad, sinkpad order. */
1919 static GstPadLinkReturn
1920 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1922 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1923 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1925 GST_OBJECT_LOCK (srcpad);
1927 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1928 goto src_was_linked;
1930 GST_OBJECT_LOCK (sinkpad);
1932 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1933 goto sink_was_linked;
1935 /* check hierarchy, pads can only be linked if the grandparents
1937 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1938 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1939 goto wrong_hierarchy;
1941 /* check pad caps for non-empty intersection */
1942 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1945 /* FIXME check pad scheduling for non-empty intersection */
1947 return GST_PAD_LINK_OK;
1951 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1952 GST_DEBUG_PAD_NAME (srcpad),
1953 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1954 /* we do not emit a warning in this case because unlinking cannot
1955 * be made MT safe.*/
1956 GST_OBJECT_UNLOCK (srcpad);
1957 return GST_PAD_LINK_WAS_LINKED;
1961 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1962 GST_DEBUG_PAD_NAME (sinkpad),
1963 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1964 /* we do not emit a warning in this case because unlinking cannot
1965 * be made MT safe.*/
1966 GST_OBJECT_UNLOCK (sinkpad);
1967 GST_OBJECT_UNLOCK (srcpad);
1968 return GST_PAD_LINK_WAS_LINKED;
1972 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1973 GST_OBJECT_UNLOCK (sinkpad);
1974 GST_OBJECT_UNLOCK (srcpad);
1975 return GST_PAD_LINK_WRONG_HIERARCHY;
1979 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1980 GST_OBJECT_UNLOCK (sinkpad);
1981 GST_OBJECT_UNLOCK (srcpad);
1982 return GST_PAD_LINK_NOFORMAT;
1988 * @srcpad: the source #GstPad.
1989 * @sinkpad: the sink #GstPad.
1991 * Checks if the source pad and the sink pad are compatible so they can be
1994 * Returns: TRUE if the pads can be linked.
1997 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1999 GstPadLinkReturn result;
2001 /* generic checks */
2002 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2003 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2005 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2006 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2008 /* gst_pad_link_prepare does everything for us, we only release the locks
2009 * on the pads that it gets us. If this function returns !OK the locks are not
2011 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2012 if (result != GST_PAD_LINK_OK)
2015 GST_OBJECT_UNLOCK (srcpad);
2016 GST_OBJECT_UNLOCK (sinkpad);
2019 return result == GST_PAD_LINK_OK;
2023 * gst_pad_link_full:
2024 * @srcpad: the source #GstPad to link.
2025 * @sinkpad: the sink #GstPad to link.
2026 * @flags: the checks to validate when linking
2028 * Links the source pad and the sink pad.
2030 * This variant of #gst_pad_link provides a more granular control on the
2031 * checks being done when linking. While providing some considerable speedups
2032 * the caller of this method must be aware that wrong usage of those flags
2033 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2034 * for more information.
2038 * Returns: A result code indicating if the connection worked or
2044 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2046 GstPadLinkReturn result;
2048 GstPadLinkFunction srcfunc, sinkfunc;
2050 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2051 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2052 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2053 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2054 GST_PAD_LINK_WRONG_DIRECTION);
2056 /* Notify the parent early. See gst_pad_unlink for details. */
2057 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2058 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2059 gst_element_post_message (parent,
2060 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2061 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2063 gst_object_unref (parent);
2068 /* prepare will also lock the two pads */
2069 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2071 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2074 /* must set peers before calling the link function */
2075 GST_PAD_PEER (srcpad) = sinkpad;
2076 GST_PAD_PEER (sinkpad) = srcpad;
2078 /* make sure we update events */
2079 prepare_event_update (srcpad, sinkpad);
2081 /* get the link functions */
2082 srcfunc = GST_PAD_LINKFUNC (srcpad);
2083 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2085 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2086 /* custom link functions, execute them */
2087 GST_OBJECT_UNLOCK (sinkpad);
2088 GST_OBJECT_UNLOCK (srcpad);
2091 /* this one will call the peer link function */
2092 result = srcfunc (srcpad, sinkpad);
2093 } else if (sinkfunc) {
2094 /* if no source link function, we need to call the sink link
2095 * function ourselves. */
2096 result = sinkfunc (sinkpad, srcpad);
2099 GST_OBJECT_LOCK (srcpad);
2100 GST_OBJECT_LOCK (sinkpad);
2102 /* we released the lock, check if the same pads are linked still */
2103 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2104 goto concurrent_link;
2106 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2109 GST_OBJECT_UNLOCK (sinkpad);
2110 GST_OBJECT_UNLOCK (srcpad);
2112 /* fire off a signal to each of the pads telling them
2113 * that they've been linked */
2114 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2115 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2117 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2118 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2120 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2123 if (G_LIKELY (parent)) {
2124 gst_element_post_message (parent,
2125 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2126 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2127 gst_object_unref (parent);
2135 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2136 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2137 GST_OBJECT_UNLOCK (sinkpad);
2138 GST_OBJECT_UNLOCK (srcpad);
2140 /* The other link operation succeeded first */
2141 result = GST_PAD_LINK_WAS_LINKED;
2146 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2147 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2149 GST_PAD_PEER (srcpad) = NULL;
2150 GST_PAD_PEER (sinkpad) = NULL;
2152 GST_OBJECT_UNLOCK (sinkpad);
2153 GST_OBJECT_UNLOCK (srcpad);
2161 * @srcpad: the source #GstPad to link.
2162 * @sinkpad: the sink #GstPad to link.
2164 * Links the source pad and the sink pad.
2166 * Returns: A result code indicating if the connection worked or
2172 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2174 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2178 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2180 GstPadTemplate **template_p;
2182 /* this function would need checks if it weren't static */
2184 GST_OBJECT_LOCK (pad);
2185 template_p = &pad->padtemplate;
2186 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2187 GST_OBJECT_UNLOCK (pad);
2190 gst_pad_template_pad_created (templ, pad);
2194 * gst_pad_get_pad_template:
2197 * Gets the template for @pad.
2199 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2200 * instantiated, or %NULL if this pad has no template. Unref after
2204 gst_pad_get_pad_template (GstPad * pad)
2206 GstPadTemplate *templ;
2208 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2210 templ = GST_PAD_PAD_TEMPLATE (pad);
2212 return (templ ? gst_object_ref (templ) : NULL);
2216 caps_with_getcaps (GstPad * pad, GstCaps * filter)
2220 if (GST_PAD_GETCAPSFUNC (pad) == NULL)
2223 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2224 "dispatching to pad getcaps function with "
2225 "filter %" GST_PTR_FORMAT, filter);
2227 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2228 GST_OBJECT_UNLOCK (pad);
2229 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2230 GST_OBJECT_LOCK (pad);
2231 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2233 if (G_UNLIKELY (result == NULL))
2236 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2237 "pad getcaps returned %" GST_PTR_FORMAT, result);
2239 #ifndef G_DISABLE_ASSERT
2240 /* check that the returned caps are a real subset of the template caps */
2241 if (GST_PAD_PAD_TEMPLATE (pad)) {
2242 const GstCaps *templ_caps =
2243 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2244 if (!gst_caps_is_subset (result, templ_caps)) {
2247 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2248 "pad returned caps %" GST_PTR_FORMAT
2249 " which are not a real subset of its template caps %"
2250 GST_PTR_FORMAT, result, templ_caps);
2252 ("pad %s:%s returned caps which are not a real "
2253 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2254 temp = gst_caps_intersect (templ_caps, result);
2255 gst_caps_unref (result);
2260 if (!gst_caps_is_subset (result, filter)) {
2263 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2264 "pad returned caps %" GST_PTR_FORMAT
2265 " which are not a real subset of the filter caps %"
2266 GST_PTR_FORMAT, result, filter);
2267 g_warning ("pad %s:%s returned caps which are not a real "
2268 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2269 /* FIXME: Order? But shouldn't happen anyway... */
2270 temp = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2271 gst_caps_unref (result);
2282 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2283 GST_DEBUG_PAD_NAME (pad));
2288 /* should be called with the pad LOCK held */
2289 /* refs the caps, so caller is responsible for getting it unreffed */
2291 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2293 GstCaps *result = NULL;
2294 GstPadTemplate *templ;
2295 gboolean fixed_caps;
2297 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2299 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2302 /* fixed caps, try the negotiated caps first */
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2304 if ((result = get_pad_caps (pad)))
2308 /* try the getcaps function next */
2309 if ((result = caps_with_getcaps (pad, filter)))
2312 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2313 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2314 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2319 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2320 "non-fixed pad caps: trying pad caps");
2321 /* non fixed caps, try the negotiated caps */
2322 if ((result = get_pad_caps (pad)))
2326 /* this almost never happens */
2327 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2328 result = gst_caps_new_empty ();
2332 /* run the filter on the result */
2334 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2335 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2336 GST_PTR_FORMAT, result, result, filter, filter);
2337 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2338 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2341 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2342 "using caps %p %" GST_PTR_FORMAT, result, result);
2343 result = gst_caps_ref (result);
2350 * gst_pad_has_current_caps:
2351 * @pad: a #GstPad to check
2353 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2355 * Returns: TRUE when @pad has caps associated with it.
2358 gst_pad_has_current_caps (GstPad * pad)
2362 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2364 GST_OBJECT_LOCK (pad);
2365 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2366 result = (get_pad_caps (pad) != NULL);
2367 GST_OBJECT_UNLOCK (pad);
2373 * gst_pad_get_current_caps:
2374 * @pad: a #GstPad to get the current capabilities of.
2376 * Gets the capabilities currently configured on @pad with the last
2377 * #GST_EVENT_CAPS event.
2379 * Returns: the current caps of the pad with incremented ref-count.
2382 gst_pad_get_current_caps (GstPad * pad)
2386 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2388 GST_OBJECT_LOCK (pad);
2389 if ((result = get_pad_caps (pad)))
2390 gst_caps_ref (result);
2391 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2392 "get current pad caps %" GST_PTR_FORMAT, result);
2393 GST_OBJECT_UNLOCK (pad);
2400 * @pad: a #GstPad to get the capabilities of.
2401 * @filter: suggested #GstCaps.
2403 * Gets the capabilities this pad can produce or consume.
2404 * Note that this method doesn't necessarily return the caps set by
2405 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2406 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2407 * the pad's get_caps function;
2408 * this returns the pad template caps if not explicitly set.
2410 * When called on sinkpads @filter contains the caps that
2411 * upstream could produce in the order preferred by upstream. When
2412 * called on srcpads @filter contains the caps accepted by
2413 * downstream in the preffered order. @filter might be %NULL but
2414 * if it is not %NULL the returned caps will be a subset of @filter.
2416 * Note that this function does not return writable #GstCaps, use
2417 * gst_caps_make_writable() before modifying the caps.
2419 * Returns: the caps of the pad with incremented ref-count.
2422 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2424 GstCaps *result = NULL;
2426 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2427 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2429 GST_OBJECT_LOCK (pad);
2431 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2433 result = gst_pad_get_caps_unlocked (pad, filter);
2435 GST_OBJECT_UNLOCK (pad);
2442 * gst_pad_peer_get_caps:
2443 * @pad: a #GstPad to get the capabilities of.
2444 * @filter: a #GstCaps filter.
2446 * Gets the capabilities of the peer connected to this pad. Similar to
2447 * gst_pad_get_caps().
2449 * When called on srcpads @filter contains the caps that
2450 * upstream could produce in the order preferred by upstream. When
2451 * called on sinkpads @filter contains the caps accepted by
2452 * downstream in the preffered order. @filter might be %NULL but
2453 * if it is not %NULL the returned caps will be a subset of @filter.
2455 * Returns: the caps of the peer pad with incremented ref-count. This function
2456 * returns %NULL when there is no peer pad.
2459 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2462 GstCaps *result = NULL;
2464 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2465 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2467 GST_OBJECT_LOCK (pad);
2469 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2471 peerpad = GST_PAD_PEER (pad);
2472 if (G_UNLIKELY (peerpad == NULL))
2475 gst_object_ref (peerpad);
2476 GST_OBJECT_UNLOCK (pad);
2478 result = gst_pad_get_caps (peerpad, filter);
2480 gst_object_unref (peerpad);
2486 GST_OBJECT_UNLOCK (pad);
2492 gst_pad_default_fixate (GstPad * pad, GstCaps * caps)
2496 /* default fixation */
2497 gst_caps_truncate (caps);
2498 s = gst_caps_get_structure (caps, 0);
2499 gst_structure_fixate (s);
2503 * gst_pad_fixate_caps:
2504 * @pad: a #GstPad to fixate
2505 * @caps: the #GstCaps to fixate
2507 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2508 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2511 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2513 GstPadFixateCapsFunction fixatefunc;
2515 g_return_if_fail (GST_IS_PAD (pad));
2516 g_return_if_fail (caps != NULL);
2517 g_return_if_fail (!gst_caps_is_empty (caps));
2518 g_return_if_fail (!gst_caps_is_any (caps));
2520 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2523 g_return_if_fail (gst_caps_is_writable (caps));
2525 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2527 fixatefunc (pad, caps);
2529 gst_pad_default_fixate (pad, caps);
2532 /* Default accept caps implementation just checks against
2533 * against the allowed caps for the pad */
2535 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2537 /* get the caps and see if it intersects to something not empty */
2539 gboolean result = FALSE;
2541 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2543 allowed = gst_pad_get_caps (pad, NULL);
2545 goto nothing_allowed;
2547 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2549 result = gst_caps_can_intersect (allowed, caps);
2551 gst_caps_unref (allowed);
2558 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2564 * gst_pad_accept_caps:
2565 * @pad: a #GstPad to check
2566 * @caps: a #GstCaps to check on the pad
2568 * Check if the given pad accepts the caps.
2570 * Returns: TRUE if the pad can accept the caps.
2573 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2576 GstPadAcceptCapsFunction acceptfunc;
2578 GstCaps *existing = NULL;
2581 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2583 /* any pad can be unnegotiated */
2587 /* lock for checking the existing caps */
2588 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2590 GST_OBJECT_LOCK (pad);
2591 /* The current caps on a pad are trivially acceptable */
2592 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2593 if (caps == existing || gst_caps_is_equal (caps, existing))
2596 GST_OBJECT_UNLOCK (pad);
2598 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2600 /* Only null if the element explicitly unset it */
2601 if (G_UNLIKELY (acceptfunc == NULL))
2604 /* we can call the function */
2605 result = acceptfunc (pad, caps);
2606 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2613 GST_DEBUG_OBJECT (pad, "pad had same caps");
2614 GST_OBJECT_UNLOCK (pad);
2620 GST_DEBUG_OBJECT (pad, "no acceptcaps function");
2626 * gst_pad_peer_accept_caps:
2627 * @pad: a #GstPad to check the peer of
2628 * @caps: a #GstCaps to check on the pad
2630 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2633 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2636 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2641 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2643 GST_OBJECT_LOCK (pad);
2645 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2647 peerpad = GST_PAD_PEER (pad);
2648 if (G_UNLIKELY (peerpad == NULL))
2651 gst_object_ref (peerpad);
2652 /* release lock before calling external methods but keep ref to pad */
2653 GST_OBJECT_UNLOCK (pad);
2655 result = gst_pad_accept_caps (peerpad, caps);
2657 gst_object_unref (peerpad);
2663 GST_OBJECT_UNLOCK (pad);
2670 * @pad: a #GstPad to set the capabilities of.
2671 * @caps: (transfer none): a #GstCaps to set.
2673 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2674 * caps on the pad will be unreffed. This function refs the caps so you should
2675 * unref if as soon as you don't need it anymore.
2676 * It is possible to set NULL caps, which will make the pad unnegotiated
2679 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2680 * or bad parameters were provided to this function.
2685 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2688 gboolean res = TRUE;
2690 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2691 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2693 event = gst_event_new_caps (caps);
2695 if (GST_PAD_IS_SRC (pad))
2696 res = gst_pad_push_event (pad, event);
2698 res = gst_pad_send_event (pad, event);
2704 do_event_function (GstPad * pad, GstEvent * event,
2705 GstPadEventFunction eventfunc, gboolean * caps_notify)
2707 gboolean result = TRUE, call_event = TRUE;
2708 GstCaps *caps, *templ, *old;
2710 switch (GST_EVENT_TYPE (event)) {
2711 case GST_EVENT_CAPS:
2713 /* backwards compatibility mode for caps */
2714 gst_event_parse_caps (event, &caps);
2716 /* See if pad accepts the caps */
2717 templ = gst_pad_get_pad_template_caps (pad);
2718 if (!gst_caps_can_intersect (caps, templ))
2721 /* check if it changed */
2722 if ((old = gst_pad_get_current_caps (pad))) {
2723 call_event = !gst_caps_is_equal (caps, old);
2724 gst_caps_unref (old);
2727 *caps_notify = TRUE;
2729 gst_caps_unref (templ);
2737 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2738 result = eventfunc (pad, event);
2740 gst_event_unref (event);
2747 gst_caps_unref (templ);
2748 gst_event_unref (event);
2749 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2750 "caps %" GST_PTR_FORMAT " not accepted", caps);
2755 /* function to send all pending events on the sinkpad to the event
2756 * function and collect the results. This function should be called with
2757 * the object lock. The object lock might be released by this function.
2759 static GstFlowReturn
2760 gst_pad_update_events (GstPad * pad)
2762 GstFlowReturn ret = GST_FLOW_OK;
2764 GstPadEventFunction eventfunc;
2766 gboolean caps_notify = FALSE;
2768 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2771 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2775 ev = &pad->priv->events[i];
2777 /* skip without pending event */
2778 if ((event = gst_event_steal (&ev->pending)) == NULL)
2781 gst_event_ref (event);
2782 GST_OBJECT_UNLOCK (pad);
2784 res = do_event_function (pad, event, eventfunc, &caps_notify);
2786 /* things could have changed while we release the lock, check if we still
2787 * are handling the same event, if we don't something changed and we have
2788 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2789 * that lock eventually and then do the retry elsewhere. */
2792 /* make the event active */
2793 gst_event_take (&ev->event, event);
2795 /* notify caps change when needed */
2797 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2798 caps_notify = FALSE;
2801 gst_event_unref (event);
2802 ret = GST_FLOW_ERROR;
2804 GST_OBJECT_LOCK (pad);
2806 /* when we get here all events were successfully updated. */
2812 g_warning ("pad %s:%s has no event handler, file a bug.",
2813 GST_DEBUG_PAD_NAME (pad));
2814 return GST_FLOW_NOT_SUPPORTED;
2819 * gst_pad_get_pad_template_caps:
2820 * @pad: a #GstPad to get the template capabilities from.
2822 * Gets the capabilities for @pad's template.
2824 * Returns: (transfer full): the #GstCaps of this pad template.
2825 * Unref after usage.
2828 gst_pad_get_pad_template_caps (GstPad * pad)
2830 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2832 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2834 if (GST_PAD_PAD_TEMPLATE (pad))
2835 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2837 return gst_static_caps_get (&anycaps);
2842 * @pad: a #GstPad to get the peer of.
2844 * Gets the peer of @pad. This function refs the peer pad so
2845 * you need to unref it after use.
2847 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2852 gst_pad_get_peer (GstPad * pad)
2856 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2858 GST_OBJECT_LOCK (pad);
2859 result = GST_PAD_PEER (pad);
2861 gst_object_ref (result);
2862 GST_OBJECT_UNLOCK (pad);
2868 * gst_pad_get_allowed_caps:
2871 * Gets the capabilities of the allowed media types that can flow through
2872 * @pad and its peer.
2874 * The allowed capabilities is calculated as the intersection of the results of
2875 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2876 * on the resulting caps.
2878 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2879 * caps when you no longer need it. This function returns NULL when @pad
2885 gst_pad_get_allowed_caps (GstPad * pad)
2892 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2894 GST_OBJECT_LOCK (pad);
2895 peer = GST_PAD_PEER (pad);
2896 if (G_UNLIKELY (peer == NULL))
2899 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2901 gst_object_ref (peer);
2902 GST_OBJECT_UNLOCK (pad);
2903 mycaps = gst_pad_get_caps (pad, NULL);
2905 peercaps = gst_pad_get_caps (peer, NULL);
2906 gst_object_unref (peer);
2908 caps = gst_caps_intersect (mycaps, peercaps);
2909 gst_caps_unref (peercaps);
2910 gst_caps_unref (mycaps);
2912 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2919 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2920 GST_OBJECT_UNLOCK (pad);
2927 * gst_pad_iterate_internal_links_default:
2928 * @pad: the #GstPad to get the internal links of.
2930 * Iterate the list of pads to which the given pad is linked to inside of
2931 * the parent element.
2932 * This is the default handler, and thus returns an iterator of all of the
2933 * pads inside the parent element with opposite direction.
2935 * The caller must free this iterator after use with gst_iterator_free().
2937 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2938 * returned pad with gst_object_unref().
2943 gst_pad_iterate_internal_links_default (GstPad * pad)
2951 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2956 GST_OBJECT_LOCK (pad);
2957 parent = GST_PAD_PARENT (pad);
2958 if (!parent || !GST_IS_ELEMENT (parent))
2961 gst_object_ref (parent);
2962 GST_OBJECT_UNLOCK (pad);
2964 if (pad->direction == GST_PAD_SRC)
2965 padlist = &parent->sinkpads;
2967 padlist = &parent->srcpads;
2969 GST_DEBUG_OBJECT (pad, "Making iterator");
2971 cookie = &parent->pads_cookie;
2973 lock = GST_OBJECT_GET_LOCK (parent);
2976 res = gst_iterator_new_list (GST_TYPE_PAD,
2977 lock, cookie, padlist, (GObject *) owner, NULL);
2979 gst_object_unref (owner);
2986 GST_OBJECT_UNLOCK (pad);
2987 GST_DEBUG_OBJECT (pad, "no parent element");
2993 * gst_pad_iterate_internal_links:
2994 * @pad: the GstPad to get the internal links of.
2996 * Gets an iterator for the pads to which the given pad is linked to inside
2997 * of the parent element.
2999 * Each #GstPad element yielded by the iterator will have its refcount increased,
3000 * so unref after use.
3002 * Free-function: gst_iterator_free
3004 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3005 * pad does not have an iterator function configured. Use
3006 * gst_iterator_free() after usage.
3011 gst_pad_iterate_internal_links (GstPad * pad)
3013 GstIterator *res = NULL;
3015 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3017 if (GST_PAD_ITERINTLINKFUNC (pad))
3018 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3026 * @forward: a #GstPadForwardFunction
3027 * @user_data: user data passed to @forward
3029 * Calls @forward for all internally linked pads of @pad. This function deals with
3030 * dynamically changing internal pads and will make sure that the @forward
3031 * function is only called once for each pad.
3033 * When @forward returns TRUE, no further pads will be processed.
3035 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3038 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3041 gboolean result = FALSE;
3043 gboolean done = FALSE;
3044 GValue item = { 0, };
3045 GList *pushed_pads = NULL;
3047 iter = gst_pad_iterate_internal_links (pad);
3053 switch (gst_iterator_next (iter, &item)) {
3054 case GST_ITERATOR_OK:
3058 intpad = g_value_get_object (&item);
3060 /* if already pushed, skip. FIXME, find something faster to tag pads */
3061 if (g_list_find (pushed_pads, intpad)) {
3062 g_value_reset (&item);
3066 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3067 GST_DEBUG_PAD_NAME (intpad));
3068 done = result = forward (intpad, user_data);
3070 pushed_pads = g_list_prepend (pushed_pads, intpad);
3072 g_value_reset (&item);
3075 case GST_ITERATOR_RESYNC:
3076 /* We don't reset the result here because we don't push the event
3077 * again on pads that got the event already and because we need
3078 * to consider the result of the previous pushes */
3079 gst_iterator_resync (iter);
3081 case GST_ITERATOR_ERROR:
3082 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3085 case GST_ITERATOR_DONE:
3090 g_value_unset (&item);
3091 gst_iterator_free (iter);
3093 g_list_free (pushed_pads);
3103 gboolean dispatched;
3107 event_forward_func (GstPad * pad, EventData * data)
3109 /* for each pad we send to, we should ref the event; it's up
3110 * to downstream to unref again when handled. */
3111 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3112 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3114 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3116 data->dispatched = TRUE;
3123 * gst_pad_event_default:
3124 * @pad: a #GstPad to call the default event handler on.
3125 * @event: (transfer full): the #GstEvent to handle.
3127 * Invokes the default event handler for the given pad.
3129 * The EOS event will pause the task associated with @pad before it is forwarded
3130 * to all internally linked pads,
3132 * The CAPS event will never be forwarded.
3134 * The the event is sent to all pads internally linked to @pad. This function
3135 * takes ownership of @event.
3137 * Returns: TRUE if the event was sent succesfully.
3140 gst_pad_event_default (GstPad * pad, GstEvent * event)
3145 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3146 g_return_val_if_fail (event != NULL, FALSE);
3148 GST_LOG_OBJECT (pad, "default event handler");
3150 switch (GST_EVENT_TYPE (event)) {
3153 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3154 gst_pad_pause_task (pad);
3162 data.dispatched = FALSE;
3163 data.result = FALSE;
3165 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3167 /* for sinkpads without a parent element or without internal links, nothing
3168 * will be dispatched but we still want to return TRUE. */
3169 if (data.dispatched)
3170 result = data.result;
3174 gst_event_unref (event);
3181 * @pad: a #GstPad to invoke the default query on.
3182 * @query: (transfer none): the #GstQuery to perform.
3184 * Dispatches a query to a pad. The query should have been allocated by the
3185 * caller via one of the type-specific allocation functions. The element that
3186 * the pad belongs to is responsible for filling the query with an appropriate
3187 * response, which should then be parsed with a type-specific query parsing
3190 * Again, the caller is responsible for both the allocation and deallocation of
3191 * the query structure.
3193 * Please also note that some queries might need a running pipeline to work.
3195 * Returns: TRUE if the query could be performed.
3198 gst_pad_query (GstPad * pad, GstQuery * query)
3201 GstPadQueryFunction func;
3203 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3204 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3206 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3207 GST_QUERY_TYPE_NAME (query));
3209 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3212 res = func (pad, query);
3214 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3215 GST_QUERY_TYPE_NAME (query), res);
3221 GST_DEBUG_OBJECT (pad, "had no query function");
3227 * gst_pad_peer_query:
3228 * @pad: a #GstPad to invoke the peer query on.
3229 * @query: (transfer none): the #GstQuery to perform.
3231 * Performs gst_pad_query() on the peer of @pad.
3233 * The caller is responsible for both the allocation and deallocation of
3234 * the query structure.
3236 * Returns: TRUE if the query could be performed. This function returns %FALSE
3237 * if @pad has no peer.
3242 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3247 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3248 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3250 GST_OBJECT_LOCK (pad);
3252 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3253 GST_QUERY_TYPE_NAME (query));
3255 peerpad = GST_PAD_PEER (pad);
3256 if (G_UNLIKELY (peerpad == NULL))
3259 gst_object_ref (peerpad);
3260 GST_OBJECT_UNLOCK (pad);
3262 result = gst_pad_query (peerpad, query);
3264 gst_object_unref (peerpad);
3271 GST_WARNING_OBJECT (pad, "pad has no peer");
3272 GST_OBJECT_UNLOCK (pad);
3278 * gst_pad_query_default:
3279 * @pad: a #GstPad to call the default query handler on.
3280 * @query: (transfer none): the #GstQuery to handle.
3282 * Invokes the default query handler for the given pad.
3283 * The query is sent to all pads internally linked to @pad. Note that
3284 * if there are many possible sink pads that are internally linked to
3285 * @pad, only one will be sent the query.
3286 * Multi-sinkpad elements should implement custom query handlers.
3288 * Returns: TRUE if the query was performed succesfully.
3291 gst_pad_query_default (GstPad * pad, GstQuery * query)
3293 gboolean forward = TRUE, ret = FALSE;
3295 switch (GST_QUERY_TYPE (query)) {
3296 case GST_QUERY_SCHEDULING:
3299 case GST_QUERY_POSITION:
3300 case GST_QUERY_SEEKING:
3301 case GST_QUERY_FORMATS:
3302 case GST_QUERY_LATENCY:
3303 case GST_QUERY_JITTER:
3304 case GST_QUERY_RATE:
3305 case GST_QUERY_CONVERT:
3306 case GST_QUERY_ALLOCATION:
3312 ret = gst_pad_forward
3313 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3319 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3321 GstPad *pad = data->pad;
3323 GstPadProbeCallback callback;
3326 /* if we have called this callback, do nothing */
3327 if (PROBE_COOKIE (hook) == data->cookie)
3330 PROBE_COOKIE (hook) = data->cookie;
3332 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3334 /* one of the data types */
3335 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3337 /* one of the scheduling types */
3338 if ((flags & GST_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3340 /* all of the blocking types must match */
3341 if ((flags & GST_PROBE_TYPE_BLOCKING) !=
3342 (data->mask & GST_PROBE_TYPE_BLOCKING))
3345 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3346 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3348 callback = (GstPadProbeCallback) hook->func;
3349 if (callback == NULL)
3352 GST_OBJECT_UNLOCK (pad);
3354 ret = callback (pad, data->mask, data->type_data, hook->data);
3356 GST_OBJECT_LOCK (pad);
3359 case GST_PROBE_REMOVE:
3360 /* remove the probe */
3361 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3362 cleanup_hook (pad, hook);
3364 case GST_PROBE_DROP:
3365 /* need to drop the data, make sure other probes don't get called
3367 GST_DEBUG_OBJECT (pad, "asked to drop item");
3368 data->mask = GST_PROBE_TYPE_INVALID;
3369 data->ret = GST_PROBE_DROP;
3371 case GST_PROBE_PASS:
3372 /* inform the pad block to let things pass */
3373 GST_DEBUG_OBJECT (pad, "asked to pass item");
3377 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3382 #define PROBE(pad,mask,data,label) \
3384 if (G_UNLIKELY (pad->num_probes)) { \
3385 ret = do_probe_callbacks (pad, mask, data); \
3386 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3391 static GstFlowReturn
3392 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3399 data.type_data = type_data;
3400 data.ret = GST_PROBE_OK;
3402 data.cookie = pad->priv->probe_cookie++;
3405 cookie = pad->priv->probe_cookie;
3407 g_hook_list_marshal (&pad->probes, FALSE,
3408 (GHookMarshaller) probe_hook_marshal, &data);
3410 /* if the list changed, call the new callbacks (they will not have their
3411 * cookie set to data.cookie */
3412 if (cookie != pad->priv->probe_cookie) {
3413 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3414 "probe list changed, restarting");
3418 if (data.ret == GST_PROBE_DROP)
3424 if (mask & GST_PROBE_TYPE_BLOCK) {
3425 while (GST_PAD_IS_BLOCKED (pad)) {
3426 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3427 "we are blocked %d times", pad->num_blocked);
3429 /* we might have released the lock */
3430 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3433 /* now we block the streaming thread. It can be unlocked when we
3434 * deactivate the pad (which will also set the FLUSHING flag) or
3435 * when the pad is unblocked. A flushing event will also unblock
3436 * the pad after setting the FLUSHING flag. */
3437 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3438 "Waiting to be unblocked or set flushing");
3439 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3440 GST_PAD_BLOCK_WAIT (pad);
3441 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3442 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3444 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3454 GST_DEBUG_OBJECT (pad, "pad is flushing");
3455 return GST_FLOW_WRONG_STATE;
3459 GST_DEBUG_OBJECT (pad, "data is dropped");
3460 return GST_FLOW_CUSTOM_SUCCESS;
3464 GST_DEBUG_OBJECT (pad, "data is passed");
3472 * gst_pad_get_offset:
3475 * Get the offset applied to the running time of @pad. @pad has to be a source
3478 * Returns: the offset.
3481 gst_pad_get_offset (GstPad * pad)
3485 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3487 GST_OBJECT_LOCK (pad);
3488 result = pad->offset;
3489 GST_OBJECT_UNLOCK (pad);
3495 * gst_pad_set_offset:
3497 * @offset: the offset
3499 * Set the offset that will be applied to the running time of @pad.
3502 gst_pad_set_offset (GstPad * pad, gint64 offset)
3508 g_return_if_fail (GST_IS_PAD (pad));
3510 GST_OBJECT_LOCK (pad);
3511 /* if nothing changed, do nothing */
3512 if (pad->offset == offset)
3515 pad->offset = offset;
3517 /* if no peer, we just updated the offset */
3518 if ((peer = GST_PAD_PEER (pad)) == NULL)
3521 /* switch pads around when dealing with a sinkpad */
3522 if (GST_PAD_IS_SINK (pad)) {
3523 /* ref the peer so it doesn't go away when we release the lock */
3524 tmp = gst_object_ref (peer);
3525 /* make sure we get the peer (the srcpad) */
3526 GST_OBJECT_UNLOCK (pad);
3532 GST_OBJECT_LOCK (pad);
3533 /* check if the pad didn't get relinked */
3534 if (GST_PAD_PEER (pad) != peer)
3537 /* we can release the ref now */
3538 gst_object_unref (peer);
3541 /* the index of the segment event in the array */
3542 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3544 /* lock order is srcpad >> sinkpad */
3545 GST_OBJECT_LOCK (peer);
3546 /* take the current segment event, adjust it and then place
3547 * it on the sinkpad. events on the srcpad are always active. */
3548 if (replace_event (pad, peer, idx))
3549 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3551 GST_OBJECT_UNLOCK (peer);
3554 GST_OBJECT_UNLOCK (pad);
3558 /**********************************************************************
3559 * Data passing functions
3562 /* this is the chain function that does not perform the additional argument
3563 * checking for that little extra speed.
3565 static inline GstFlowReturn
3566 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3569 gboolean needs_events;
3571 GST_PAD_STREAM_LOCK (pad);
3573 GST_OBJECT_LOCK (pad);
3574 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3577 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3578 if (G_UNLIKELY (needs_events)) {
3579 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3581 GST_DEBUG_OBJECT (pad, "need to update all events");
3582 ret = gst_pad_update_events (pad);
3583 if (G_UNLIKELY (ret != GST_FLOW_OK))
3587 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3589 GST_OBJECT_UNLOCK (pad);
3591 /* NOTE: we read the chainfunc unlocked.
3592 * we cannot hold the lock for the pad so we might send
3593 * the data to the wrong function. This is not really a
3594 * problem since functions are assigned at creation time
3595 * and don't change that often... */
3596 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3597 GstPadChainFunction chainfunc;
3599 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3602 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3603 "calling chainfunction &%s with buffer %p",
3604 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3606 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3608 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3609 "called chainfunction &%s with buffer %p, returned %s",
3610 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3612 GstPadChainListFunction chainlistfunc;
3614 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3617 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3618 "calling chainlistfunction &%s",
3619 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3621 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3623 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3624 "called chainlistfunction &%s, returned %s",
3625 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3628 GST_PAD_STREAM_UNLOCK (pad);
3635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3636 "chaining, but pad was flushing");
3637 GST_OBJECT_UNLOCK (pad);
3638 GST_PAD_STREAM_UNLOCK (pad);
3639 return GST_FLOW_WRONG_STATE;
3643 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3644 GST_OBJECT_UNLOCK (pad);
3645 GST_PAD_STREAM_UNLOCK (pad);
3650 GST_OBJECT_UNLOCK (pad);
3651 GST_PAD_STREAM_UNLOCK (pad);
3652 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3655 case GST_FLOW_CUSTOM_SUCCESS:
3656 GST_DEBUG_OBJECT (pad, "dropped buffer");
3660 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3667 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3668 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3669 "pushing, but not chainhandler");
3670 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3671 ("push on pad %s:%s but it has no chainfunction",
3672 GST_DEBUG_PAD_NAME (pad)));
3673 GST_PAD_STREAM_UNLOCK (pad);
3674 return GST_FLOW_NOT_SUPPORTED;
3680 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3681 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3684 * Chain a buffer to @pad.
3686 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3688 * If the buffer type is not acceptable for @pad (as negotiated with a
3689 * preceeding GST_EVENT_CAPS event), this function returns
3690 * #GST_FLOW_NOT_NEGOTIATED.
3692 * The function proceeds calling the chain function installed on @pad (see
3693 * gst_pad_set_chain_function()) and the return value of that function is
3694 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3697 * In all cases, success or failure, the caller loses its reference to @buffer
3698 * after calling this function.
3700 * Returns: a #GstFlowReturn from the pad.
3705 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3707 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3708 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3709 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3711 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3714 static GstFlowReturn
3715 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3721 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3723 len = gst_buffer_list_len (list);
3726 for (i = 0; i < len; i++) {
3727 buffer = gst_buffer_list_get (list, i);
3729 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3730 gst_buffer_ref (buffer));
3731 if (ret != GST_FLOW_OK)
3734 gst_buffer_list_unref (list);
3740 * gst_pad_chain_list:
3741 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3742 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3745 * Chain a bufferlist to @pad.
3747 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3749 * If @pad was not negotiated properly with a CAPS event, this function
3750 * returns #GST_FLOW_NOT_NEGOTIATED.
3752 * The function proceeds calling the chainlist function installed on @pad (see
3753 * gst_pad_set_chain_list_function()) and the return value of that function is
3754 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3755 * chainlist function.
3757 * In all cases, success or failure, the caller loses its reference to @list
3758 * after calling this function.
3762 * Returns: a #GstFlowReturn from the pad.
3767 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3769 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3770 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3771 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3773 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3776 static GstFlowReturn
3777 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3782 GST_OBJECT_LOCK (pad);
3783 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3786 type |= GST_PROBE_TYPE_PUSH;
3788 /* do block probes */
3789 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3791 /* do post-blocking probes */
3792 PROBE (pad, type, data, probe_stopped);
3794 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3797 /* take ref to peer pad before releasing the lock */
3798 gst_object_ref (peer);
3800 GST_OBJECT_UNLOCK (pad);
3802 ret = gst_pad_chain_data_unchecked (peer, type, data);
3804 gst_object_unref (peer);
3806 GST_OBJECT_LOCK (pad);
3808 if (pad->priv->using == 0) {
3809 /* pad is not active anymore, trigger idle callbacks */
3810 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3812 GST_OBJECT_UNLOCK (pad);
3816 /* ERROR recovery here */
3820 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3821 "pushing, but pad was flushing");
3822 GST_OBJECT_UNLOCK (pad);
3823 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3824 return GST_FLOW_WRONG_STATE;
3828 GST_OBJECT_UNLOCK (pad);
3829 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3832 case GST_FLOW_CUSTOM_SUCCESS:
3833 GST_DEBUG_OBJECT (pad, "dropped buffer");
3837 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3844 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3845 "pushing, but it was not linked");
3846 GST_OBJECT_UNLOCK (pad);
3847 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3848 return GST_FLOW_NOT_LINKED;
3854 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3855 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3858 * Pushes a buffer to the peer of @pad.
3860 * This function will call installed block probes before triggering any
3861 * installed data probes.
3863 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3864 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3867 * In all cases, success or failure, the caller loses its reference to @buffer
3868 * after calling this function.
3870 * Returns: a #GstFlowReturn from the peer pad.
3875 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3877 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3878 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3879 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3881 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
3885 * gst_pad_push_list:
3886 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3887 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3890 * Pushes a buffer list to the peer of @pad.
3892 * This function will call installed block probes before triggering any
3893 * installed data probes.
3895 * The function proceeds calling the chain function on the peer pad and returns
3896 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3897 * be returned. If the peer pad does not have any installed chainlist function
3898 * every group buffer of the list will be merged into a normal #GstBuffer and
3899 * chained via gst_pad_chain().
3901 * In all cases, success or failure, the caller loses its reference to @list
3902 * after calling this function.
3904 * Returns: a #GstFlowReturn from the peer pad.
3911 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3913 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3914 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3915 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3917 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3920 static GstFlowReturn
3921 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3922 GstBuffer ** buffer)
3925 GstPadGetRangeFunction getrangefunc;
3927 GST_PAD_STREAM_LOCK (pad);
3929 GST_OBJECT_LOCK (pad);
3930 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3932 GST_OBJECT_UNLOCK (pad);
3934 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3937 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3938 "calling getrangefunc %s, offset %"
3939 G_GUINT64_FORMAT ", size %u",
3940 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3942 ret = getrangefunc (pad, offset, size, buffer);
3944 if (G_UNLIKELY (ret != GST_FLOW_OK))
3945 goto get_range_failed;
3947 /* can only fire the signal if we have a valid buffer */
3948 GST_OBJECT_LOCK (pad);
3949 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
3951 GST_OBJECT_UNLOCK (pad);
3953 GST_PAD_STREAM_UNLOCK (pad);
3960 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3961 "getrange, but pad was flushing");
3962 GST_OBJECT_UNLOCK (pad);
3963 GST_PAD_STREAM_UNLOCK (pad);
3964 return GST_FLOW_WRONG_STATE;
3968 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3969 ("getrange on pad %s:%s but it has no getrangefunction",
3970 GST_DEBUG_PAD_NAME (pad)));
3971 GST_PAD_STREAM_UNLOCK (pad);
3972 return GST_FLOW_NOT_SUPPORTED;
3976 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3977 "probe returned %s", gst_flow_get_name (ret));
3978 GST_OBJECT_UNLOCK (pad);
3979 GST_PAD_STREAM_UNLOCK (pad);
3980 gst_buffer_unref (*buffer);
3986 GST_PAD_STREAM_UNLOCK (pad);
3988 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3989 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3990 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3996 * gst_pad_get_range:
3997 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3998 * @offset: The start offset of the buffer
3999 * @size: The length of the buffer
4000 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4001 * returns #GST_FLOW_ERROR if %NULL.
4003 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4004 * immediatly and @buffer is %NULL.
4006 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4007 * description of a getrange function. If @pad has no getrange function
4008 * installed (see gst_pad_set_getrange_function()) this function returns
4009 * #GST_FLOW_NOT_SUPPORTED.
4011 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4013 * Returns: a #GstFlowReturn from the pad.
4018 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4019 GstBuffer ** buffer)
4021 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4022 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4023 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4025 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4029 * gst_pad_pull_range:
4030 * @pad: a sink #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, returns
4034 * GST_FLOW_ERROR if %NULL.
4036 * Pulls a @buffer from the peer pad.
4038 * This function will first trigger the pad block signal if it was
4041 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4042 * function returns the result of gst_pad_get_range() on the peer pad.
4043 * See gst_pad_get_range() for a list of return values and for the
4044 * semantics of the arguments of this function.
4046 * @buffer's caps must either be unset or the same as what is already
4047 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4050 * Returns: a #GstFlowReturn from the peer pad.
4051 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4052 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4053 * @buffer may not be used or freed when any other return value than
4054 * #GST_FLOW_OK is returned.
4059 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4060 GstBuffer ** buffer)
4064 gboolean needs_events;
4066 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4067 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4068 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4070 GST_OBJECT_LOCK (pad);
4071 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4074 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4077 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4080 gst_object_ref (peer);
4082 GST_OBJECT_UNLOCK (pad);
4084 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4086 gst_object_unref (peer);
4088 GST_OBJECT_LOCK (pad);
4090 if (pad->priv->using == 0) {
4091 /* pad is not active anymore, trigger idle callbacks */
4092 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4093 post_probe_stopped);
4096 if (G_UNLIKELY (ret != GST_FLOW_OK))
4097 goto pull_range_failed;
4099 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4100 post_probe_stopped);
4102 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4103 if (G_UNLIKELY (needs_events)) {
4104 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4106 GST_DEBUG_OBJECT (pad, "we need to update the events");
4107 ret = gst_pad_update_events (pad);
4108 if (G_UNLIKELY (ret != GST_FLOW_OK))
4111 GST_OBJECT_UNLOCK (pad);
4115 /* ERROR recovery here */
4118 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4119 "pullrange, but pad was flushing");
4120 GST_OBJECT_UNLOCK (pad);
4121 return GST_FLOW_WRONG_STATE;
4125 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4126 gst_flow_get_name (ret));
4127 GST_OBJECT_UNLOCK (pad);
4132 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4133 "pulling range, but it was not linked");
4134 GST_OBJECT_UNLOCK (pad);
4135 return GST_FLOW_NOT_LINKED;
4140 GST_OBJECT_UNLOCK (pad);
4141 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4142 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4143 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4148 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4149 "post probe returned %s", gst_flow_get_name (ret));
4150 GST_OBJECT_UNLOCK (pad);
4151 if (ret == GST_FLOW_OK)
4152 gst_buffer_unref (*buffer);
4158 GST_OBJECT_UNLOCK (pad);
4159 gst_buffer_unref (*buffer);
4161 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4162 "pullrange returned events that were not accepted");
4168 * gst_pad_push_event:
4169 * @pad: a #GstPad to push the event to.
4170 * @event: (transfer full): the #GstEvent to send to the pad.
4172 * Sends the event to the peer of the given pad. This function is
4173 * mainly used by elements to send events to their peer
4176 * This function takes owership of the provided event so you should
4177 * gst_event_ref() it if you want to reuse the event after this call.
4179 * Returns: TRUE if the event was handled.
4184 gst_pad_push_event (GstPad * pad, GstEvent * event)
4189 gboolean stored = FALSE;
4191 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4192 g_return_val_if_fail (event != NULL, FALSE);
4193 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4195 GST_OBJECT_LOCK (pad);
4197 peerpad = GST_PAD_PEER (pad);
4199 /* Two checks to be made:
4200 * . (un)set the FLUSHING flag for flushing events,
4201 * . handle pad blocking */
4202 switch (GST_EVENT_TYPE (event)) {
4203 case GST_EVENT_FLUSH_START:
4204 GST_PAD_SET_FLUSHING (pad);
4206 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4207 /* flush start will have set the FLUSHING flag and will then
4208 * unlock all threads doing a GCond wait on the blocking pad. This
4209 * will typically unblock the STREAMING thread blocked on a pad. */
4210 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4211 "doing block signal.");
4212 GST_PAD_BLOCK_BROADCAST (pad);
4216 case GST_EVENT_FLUSH_STOP:
4217 GST_PAD_UNSET_FLUSHING (pad);
4218 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4219 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4225 /* store the event on the pad, but only on srcpads */
4226 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4229 idx = GST_EVENT_STICKY_IDX (event);
4230 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4231 GST_EVENT_TYPE_NAME (event), idx);
4233 /* srcpad sticky events always become active immediately */
4234 gst_event_replace (&pad->priv->events[idx].event, event);
4239 /* backwards compatibility mode for caps */
4240 switch (GST_EVENT_TYPE (event)) {
4241 case GST_EVENT_CAPS:
4243 GST_OBJECT_UNLOCK (pad);
4245 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4247 GST_OBJECT_LOCK (pad);
4248 /* the peerpad might have changed. Things we checked above could not
4250 peerpad = GST_PAD_PEER (pad);
4253 case GST_EVENT_SEGMENT:
4257 offset = pad->offset;
4258 /* check if we need to adjust the segment */
4259 if (offset != 0 && (peerpad != NULL)) {
4262 /* copy segment values */
4263 gst_event_copy_segment (event, &segment);
4264 gst_event_unref (event);
4266 /* adjust and make a new event with the offset applied */
4267 segment.base += offset;
4268 event = gst_event_new_segment (&segment);
4272 case GST_EVENT_RECONFIGURE:
4273 if (GST_PAD_IS_SINK (pad))
4274 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4280 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4283 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4284 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4290 /* send probes after modifying the events above */
4291 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4293 /* now check the peer pad */
4294 if (peerpad == NULL)
4297 gst_object_ref (peerpad);
4299 GST_OBJECT_UNLOCK (pad);
4301 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4302 event, GST_EVENT_TYPE_NAME (event), peerpad);
4304 result = gst_pad_send_event (peerpad, event);
4306 /* Note: we gave away ownership of the event at this point but we can still
4307 * print the old pointer */
4308 GST_LOG_OBJECT (pad,
4309 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4312 gst_object_unref (peerpad);
4314 GST_OBJECT_LOCK (pad);
4316 if (pad->priv->using == 0) {
4317 /* pad is not active anymore, trigger idle callbacks */
4318 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4320 GST_OBJECT_UNLOCK (pad);
4322 return result | stored;
4324 /* ERROR handling */
4327 GST_DEBUG_OBJECT (pad, "We're flushing");
4328 GST_OBJECT_UNLOCK (pad);
4329 gst_event_unref (event);
4334 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4335 GST_OBJECT_UNLOCK (pad);
4336 gst_event_unref (event);
4341 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4342 GST_OBJECT_UNLOCK (pad);
4343 gst_event_unref (event);
4349 * gst_pad_send_event:
4350 * @pad: a #GstPad to send the event to.
4351 * @event: (transfer full): the #GstEvent to send to the pad.
4353 * Sends the event to the pad. This function can be used
4354 * by applications to send events in the pipeline.
4356 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4357 * sink pad, @event should be a downstream event. For example, you would not
4358 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4359 * Furthermore, some downstream events have to be serialized with data flow,
4360 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4361 * the event needs to be serialized with data flow, this function will take the
4362 * pad's stream lock while calling its event function.
4364 * To find out whether an event type is upstream, downstream, or downstream and
4365 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4366 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4367 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4368 * plugin doesn't need to bother itself with this information; the core handles
4369 * all necessary locks and checks.
4371 * This function takes owership of the provided event so you should
4372 * gst_event_ref() it if you want to reuse the event after this call.
4374 * Returns: TRUE if the event was handled.
4377 gst_pad_send_event (GstPad * pad, GstEvent * event)
4380 gboolean result = FALSE;
4381 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4383 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4384 g_return_val_if_fail (event != NULL, FALSE);
4386 GST_OBJECT_LOCK (pad);
4387 if (GST_PAD_IS_SINK (pad)) {
4388 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4389 goto wrong_direction;
4390 serialized = GST_EVENT_IS_SERIALIZED (event);
4391 sticky = GST_EVENT_IS_STICKY (event);
4392 } else if (GST_PAD_IS_SRC (pad)) {
4393 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4394 goto wrong_direction;
4395 /* events on srcpad never are serialized and sticky */
4396 serialized = sticky = FALSE;
4398 goto unknown_direction;
4400 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4402 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4404 switch (GST_EVENT_TYPE (event)) {
4405 case GST_EVENT_FLUSH_START:
4406 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4407 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4409 /* can't even accept a flush begin event when flushing */
4410 if (GST_PAD_IS_FLUSHING (pad))
4413 GST_PAD_SET_FLUSHING (pad);
4414 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4415 needs_events = FALSE;
4417 case GST_EVENT_FLUSH_STOP:
4418 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4419 GST_PAD_UNSET_FLUSHING (pad);
4420 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4422 GST_OBJECT_UNLOCK (pad);
4423 /* grab stream lock */
4424 GST_PAD_STREAM_LOCK (pad);
4426 GST_OBJECT_LOCK (pad);
4427 needs_events = FALSE;
4429 case GST_EVENT_RECONFIGURE:
4430 if (GST_PAD_IS_SRC (pad))
4431 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4433 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4434 GST_EVENT_TYPE_NAME (event));
4437 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4438 GST_OBJECT_UNLOCK (pad);
4439 GST_PAD_STREAM_LOCK (pad);
4441 GST_OBJECT_LOCK (pad);
4443 /* don't forward events on non-serialized events */
4444 needs_events = FALSE;
4447 /* store the event on the pad, but only on srcpads. We need to store the
4448 * event before checking the flushing flag. */
4453 switch (GST_EVENT_TYPE (event)) {
4454 case GST_EVENT_SEGMENT:
4455 if (pad->offset != 0) {
4458 /* copy segment values */
4459 gst_event_copy_segment (event, &segment);
4460 gst_event_unref (event);
4462 /* adjust and make a new event with the offset applied */
4463 segment.base += pad->offset;
4464 event = gst_event_new_segment (&segment);
4471 idx = GST_EVENT_STICKY_IDX (event);
4472 ev = &pad->priv->events[idx];
4474 if (ev->event != event) {
4475 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4476 GST_EVENT_TYPE_NAME (event), idx);
4477 gst_event_replace (&ev->pending, event);
4478 /* set the flag so that we update the events next time. We would
4479 * usually update below but we might be flushing too. */
4480 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4481 needs_events = TRUE;
4484 /* now do the probe */
4485 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4488 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4494 if (G_UNLIKELY (needs_events)) {
4497 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4499 GST_DEBUG_OBJECT (pad, "need to update all events");
4500 ret = gst_pad_update_events (pad);
4501 if (ret != GST_FLOW_OK)
4503 GST_OBJECT_UNLOCK (pad);
4505 gst_event_unref (event);
4510 /* ensure to pass on event;
4511 * note that a sticky event has already been updated above */
4512 if (G_LIKELY (!needs_events || !sticky)) {
4513 GstPadEventFunction eventfunc;
4515 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4518 GST_OBJECT_UNLOCK (pad);
4520 result = eventfunc (pad, event);
4524 GST_PAD_STREAM_UNLOCK (pad);
4526 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4530 /* ERROR handling */
4533 g_warning ("pad %s:%s sending %s event in wrong direction",
4534 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4535 GST_OBJECT_UNLOCK (pad);
4536 gst_event_unref (event);
4541 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4542 GST_OBJECT_UNLOCK (pad);
4543 gst_event_unref (event);
4548 g_warning ("pad %s:%s has no event handler, file a bug.",
4549 GST_DEBUG_PAD_NAME (pad));
4550 GST_OBJECT_UNLOCK (pad);
4552 GST_PAD_STREAM_UNLOCK (pad);
4553 gst_event_unref (event);
4558 GST_OBJECT_UNLOCK (pad);
4560 GST_PAD_STREAM_UNLOCK (pad);
4561 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4562 "Received event on flushing pad. Discarding");
4563 gst_event_unref (event);
4568 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4569 GST_OBJECT_UNLOCK (pad);
4571 GST_PAD_STREAM_UNLOCK (pad);
4572 gst_event_unref (event);
4577 GST_OBJECT_UNLOCK (pad);
4579 GST_PAD_STREAM_UNLOCK (pad);
4580 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4581 gst_event_unref (event);
4587 * gst_pad_set_element_private:
4588 * @pad: the #GstPad to set the private data of.
4589 * @priv: The private data to attach to the pad.
4591 * Set the given private data gpointer on the pad.
4592 * This function can only be used by the element that owns the pad.
4593 * No locking is performed in this function.
4596 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4598 pad->element_private = priv;
4602 * gst_pad_get_element_private:
4603 * @pad: the #GstPad to get the private data of.
4605 * Gets the private data of a pad.
4606 * No locking is performed in this function.
4608 * Returns: (transfer none): a #gpointer to the private data.
4611 gst_pad_get_element_private (GstPad * pad)
4613 return pad->element_private;
4617 * gst_pad_get_sticky_event:
4618 * @pad: the #GstPad to get the event from.
4619 * @event_type: the #GstEventType that should be retrieved.
4621 * Returns a new reference of the sticky event of type @event_type
4624 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4627 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4629 GstEvent *event = NULL;
4632 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4633 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4635 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4637 GST_OBJECT_LOCK (pad);
4638 if ((event = pad->priv->events[idx].event)) {
4639 gst_event_ref (event);
4641 GST_OBJECT_UNLOCK (pad);
4647 * gst_pad_sticky_events_foreach:
4648 * @pad: the #GstPad that should be used for iteration.
4649 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4650 * @user_data: (closure): the optional user data.
4652 * Iterates all active sticky events on @pad and calls @foreach_func for every
4653 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4654 * is immediately stopped.
4656 * Returns: GST_FLOW_OK if iteration was successful
4659 gst_pad_sticky_events_foreach (GstPad * pad,
4660 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4662 GstFlowReturn ret = GST_FLOW_OK;
4666 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4667 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4669 GST_OBJECT_LOCK (pad);
4672 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4676 ev = &pad->priv->events[i];
4678 /* skip without active event */
4679 if ((event = ev->event) == NULL)
4682 gst_event_ref (event);
4683 GST_OBJECT_UNLOCK (pad);
4685 res = foreach_func (pad, event, user_data);
4687 GST_OBJECT_LOCK (pad);
4688 gst_event_unref (event);
4690 if (res != GST_FLOW_OK) {
4695 /* things could have changed while we release the lock, check if we still
4696 * are handling the same event, if we don't something changed and we have
4697 * to try again. FIXME. we need a cookie here. */
4698 if (event != ev->event) {
4699 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4703 GST_OBJECT_UNLOCK (pad);
4709 do_stream_status (GstPad * pad, GstStreamStatusType type,
4710 GThread * thread, GstTask * task)
4714 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4716 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4717 if (GST_IS_ELEMENT (parent)) {
4718 GstMessage *message;
4719 GValue value = { 0 };
4721 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4722 gchar *tname, *ename, *pname;
4724 /* create a good task name */
4725 ename = gst_element_get_name (parent);
4726 pname = gst_pad_get_name (pad);
4727 tname = g_strdup_printf ("%s:%s", ename, pname);
4731 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4735 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4738 g_value_init (&value, GST_TYPE_TASK);
4739 g_value_set_object (&value, task);
4740 gst_message_set_stream_status_object (message, &value);
4741 g_value_unset (&value);
4743 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4744 gst_element_post_message (parent, message);
4746 gst_object_unref (parent);
4751 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4753 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4758 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4760 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4764 static GstTaskThreadCallbacks thr_callbacks = {
4770 * gst_pad_start_task:
4771 * @pad: the #GstPad to start the task of
4772 * @func: the task function to call
4773 * @data: data passed to the task function
4775 * Starts a task that repeatedly calls @func with @data. This function
4776 * is mostly used in pad activation functions to start the dataflow.
4777 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4778 * before @func is called.
4780 * Returns: a %TRUE if the task could be started.
4783 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4788 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4789 g_return_val_if_fail (func != NULL, FALSE);
4791 GST_DEBUG_OBJECT (pad, "start task");
4793 GST_OBJECT_LOCK (pad);
4794 task = GST_PAD_TASK (pad);
4796 task = gst_task_create (func, data);
4797 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4798 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4799 GST_DEBUG_OBJECT (pad, "created task");
4800 GST_PAD_TASK (pad) = task;
4801 gst_object_ref (task);
4802 /* release lock to post the message */
4803 GST_OBJECT_UNLOCK (pad);
4805 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4807 gst_object_unref (task);
4809 GST_OBJECT_LOCK (pad);
4810 /* nobody else is supposed to have changed the pad now */
4811 if (GST_PAD_TASK (pad) != task)
4812 goto concurrent_stop;
4814 res = gst_task_set_state (task, GST_TASK_STARTED);
4815 GST_OBJECT_UNLOCK (pad);
4822 GST_OBJECT_UNLOCK (pad);
4828 * gst_pad_pause_task:
4829 * @pad: the #GstPad to pause the task of
4831 * Pause the task of @pad. This function will also wait until the
4832 * function executed by the task is finished if this function is not
4833 * called from the task function.
4835 * Returns: a TRUE if the task could be paused or FALSE when the pad
4839 gst_pad_pause_task (GstPad * pad)
4844 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4846 GST_DEBUG_OBJECT (pad, "pause task");
4848 GST_OBJECT_LOCK (pad);
4849 task = GST_PAD_TASK (pad);
4852 res = gst_task_set_state (task, GST_TASK_PAUSED);
4853 GST_OBJECT_UNLOCK (pad);
4855 /* wait for task function to finish, this lock is recursive so it does nothing
4856 * when the pause is called from the task itself */
4857 GST_PAD_STREAM_LOCK (pad);
4858 GST_PAD_STREAM_UNLOCK (pad);
4864 GST_DEBUG_OBJECT (pad, "pad has no task");
4865 GST_OBJECT_UNLOCK (pad);
4871 * gst_pad_stop_task:
4872 * @pad: the #GstPad to stop the task of
4874 * Stop the task of @pad. This function will also make sure that the
4875 * function executed by the task will effectively stop if not called
4876 * from the GstTaskFunction.
4878 * This function will deadlock if called from the GstTaskFunction of
4879 * the task. Use gst_task_pause() instead.
4881 * Regardless of whether the pad has a task, the stream lock is acquired and
4882 * released so as to ensure that streaming through this pad has finished.
4884 * Returns: a TRUE if the task could be stopped or FALSE on error.
4887 gst_pad_stop_task (GstPad * pad)
4892 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4894 GST_DEBUG_OBJECT (pad, "stop task");
4896 GST_OBJECT_LOCK (pad);
4897 task = GST_PAD_TASK (pad);
4900 GST_PAD_TASK (pad) = NULL;
4901 res = gst_task_set_state (task, GST_TASK_STOPPED);
4902 GST_OBJECT_UNLOCK (pad);
4904 GST_PAD_STREAM_LOCK (pad);
4905 GST_PAD_STREAM_UNLOCK (pad);
4907 if (!gst_task_join (task))
4910 gst_object_unref (task);
4916 GST_DEBUG_OBJECT (pad, "no task");
4917 GST_OBJECT_UNLOCK (pad);
4919 GST_PAD_STREAM_LOCK (pad);
4920 GST_PAD_STREAM_UNLOCK (pad);
4922 /* this is not an error */
4927 /* this is bad, possibly the application tried to join the task from
4928 * the task's thread. We install the task again so that it will be stopped
4929 * again from the right thread next time hopefully. */
4930 GST_OBJECT_LOCK (pad);
4931 GST_DEBUG_OBJECT (pad, "join failed");
4932 /* we can only install this task if there was no other task */
4933 if (GST_PAD_TASK (pad) == NULL)
4934 GST_PAD_TASK (pad) = task;
4935 GST_OBJECT_UNLOCK (pad);