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 void gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps);
146 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
147 GstBufferList * list);
149 static GstObjectClass *parent_class = NULL;
150 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
152 static GParamSpec *pspec_caps = NULL;
154 /* quarks for probe signals */
155 static GQuark buffer_quark;
156 static GQuark buffer_list_quark;
157 static GQuark event_quark;
166 static GstFlowQuarks flow_quarks[] = {
167 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
168 {GST_FLOW_RESEND, "resend", 0},
169 {GST_FLOW_OK, "ok", 0},
170 {GST_FLOW_NOT_LINKED, "not-linked", 0},
171 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
172 {GST_FLOW_UNEXPECTED, "unexpected", 0},
173 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
174 {GST_FLOW_ERROR, "error", 0},
175 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
176 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
181 * @ret: a #GstFlowReturn to get the name of.
183 * Gets a string representing the given flow return.
185 * Returns: a static string with the name of the flow return.
188 gst_flow_get_name (GstFlowReturn ret)
192 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
194 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
195 if (ret == flow_quarks[i].ret)
196 return flow_quarks[i].name;
203 * @ret: a #GstFlowReturn to get the quark of.
205 * Get the unique quark for the given GstFlowReturn.
207 * Returns: the quark associated with the flow return or 0 if an
208 * invalid return was specified.
211 gst_flow_to_quark (GstFlowReturn ret)
215 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
217 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
218 if (ret == flow_quarks[i].ret)
219 return flow_quarks[i].quark;
228 buffer_quark = g_quark_from_static_string ("buffer"); \
229 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
230 event_quark = g_quark_from_static_string ("event"); \
232 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
233 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
236 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
237 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
240 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
243 gst_pad_class_init (GstPadClass * klass)
245 GObjectClass *gobject_class;
246 GstObjectClass *gstobject_class;
248 gobject_class = G_OBJECT_CLASS (klass);
249 gstobject_class = GST_OBJECT_CLASS (klass);
251 g_type_class_add_private (klass, sizeof (GstPadPrivate));
253 parent_class = g_type_class_peek_parent (klass);
255 gobject_class->dispose = gst_pad_dispose;
256 gobject_class->finalize = gst_pad_finalize;
257 gobject_class->set_property = gst_pad_set_property;
258 gobject_class->get_property = gst_pad_get_property;
262 * @pad: the pad that emitted the signal
263 * @peer: the peer pad that has been connected
265 * Signals that a pad has been linked to the peer pad.
267 gst_pad_signals[PAD_LINKED] =
268 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
270 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
273 * @pad: the pad that emitted the signal
274 * @peer: the peer pad that has been disconnected
276 * Signals that a pad has been unlinked from the peer pad.
278 gst_pad_signals[PAD_UNLINKED] =
279 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
281 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
283 pspec_caps = g_param_spec_boxed ("caps", "Caps",
284 "The capabilities of the pad", GST_TYPE_CAPS,
285 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
286 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
288 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
289 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
290 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
293 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
294 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
295 g_param_spec_object ("template", "Template",
296 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 gstobject_class->path_string_separator = ".";
301 /* Register common function pointer descriptions */
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
308 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
309 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_fixate_caps_default);
313 gst_pad_init (GstPad * pad)
315 pad->priv = GST_PAD_GET_PRIVATE (pad);
317 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
319 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
320 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
321 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
322 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
323 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
324 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
325 GST_PAD_FIXATECAPSFUNC (pad) = gst_pad_fixate_caps_default;
326 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
328 GST_PAD_SET_FLUSHING (pad);
330 g_static_rec_mutex_init (&pad->stream_rec_lock);
332 pad->block_cond = g_cond_new ();
334 g_hook_list_init (&pad->probes, sizeof (GstProbe));
337 /* called when setting the pad inactive. It removes all sticky events from
340 clear_events (PadEvent events[])
344 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
345 gst_event_replace (&events[i].event, NULL);
346 gst_event_replace (&events[i].pending, NULL);
350 /* The sticky event with @idx from the srcpad is copied to the
351 * pending event on the sinkpad (when different).
352 * This function applies the pad offsets in case of segment events.
353 * This will make sure that we send the event to the sinkpad event
354 * function when the next buffer of event arrives.
355 * Should be called with the OBJECT lock of both pads.
356 * This function returns TRUE when there is a pending event on the
359 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
361 PadEvent *srcev, *sinkev;
363 gboolean pending = FALSE;
365 srcev = &srcpad->priv->events[idx];
367 if ((event = srcev->event)) {
368 sinkev = &sinkpad->priv->events[idx];
370 switch (GST_EVENT_TYPE (event)) {
371 case GST_EVENT_SEGMENT:
376 offset = srcpad->offset + sinkpad->offset;
378 gst_event_copy_segment (event, &segment);
379 /* adjust the base time. FIXME, check negative times, try to tweak the
380 * start to do clipping on negative times */
381 segment.base += offset;
382 /* make a new event from the updated segment */
383 event = gst_event_new_segment (&segment);
390 if (sinkev->event != event) {
391 /* put in the pending entry when different */
392 gst_event_replace (&sinkev->pending, event);
401 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
406 /* make sure we push the events from the source to this new peer, for this we
407 * copy the events on the sinkpad and mark EVENTS_PENDING */
409 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
410 pending |= replace_event (srcpad, sinkpad, i);
412 /* we had some new pending events, set our flag */
414 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
417 /* should be called with the OBJECT_LOCK */
419 get_pad_caps (GstPad * pad)
421 GstCaps *caps = NULL;
425 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
426 /* we can only use the caps when we have successfully send the caps
427 * event to the event function and is thus in the active entry */
428 if ((event = pad->priv->events[idx].event))
429 gst_event_parse_caps (event, &caps);
435 gst_pad_dispose (GObject * object)
437 GstPad *pad = GST_PAD_CAST (object);
440 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
442 /* unlink the peer pad */
443 if ((peer = gst_pad_get_peer (pad))) {
444 /* window for MT unsafeness, someone else could unlink here
445 * and then we call unlink with wrong pads. The unlink
446 * function would catch this and safely return failed. */
447 if (GST_PAD_IS_SRC (pad))
448 gst_pad_unlink (pad, peer);
450 gst_pad_unlink (peer, pad);
452 gst_object_unref (peer);
455 gst_pad_set_pad_template (pad, NULL);
457 clear_events (pad->priv->events);
459 g_hook_list_clear (&pad->probes);
461 G_OBJECT_CLASS (parent_class)->dispose (object);
465 gst_pad_finalize (GObject * object)
467 GstPad *pad = GST_PAD_CAST (object);
470 /* in case the task is still around, clean it up */
471 if ((task = GST_PAD_TASK (pad))) {
472 gst_task_join (task);
473 GST_PAD_TASK (pad) = NULL;
474 gst_object_unref (task);
477 g_static_rec_mutex_free (&pad->stream_rec_lock);
478 g_cond_free (pad->block_cond);
480 G_OBJECT_CLASS (parent_class)->finalize (object);
484 gst_pad_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec)
487 g_return_if_fail (GST_IS_PAD (object));
490 case PAD_PROP_DIRECTION:
491 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
493 case PAD_PROP_TEMPLATE:
494 gst_pad_set_pad_template (GST_PAD_CAST (object),
495 (GstPadTemplate *) g_value_get_object (value));
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 gst_pad_get_property (GObject * object, guint prop_id,
505 GValue * value, GParamSpec * pspec)
507 g_return_if_fail (GST_IS_PAD (object));
511 GST_OBJECT_LOCK (object);
512 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
513 GST_OBJECT_UNLOCK (object);
515 case PAD_PROP_DIRECTION:
516 g_value_set_enum (value, GST_PAD_DIRECTION (object));
518 case PAD_PROP_TEMPLATE:
519 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529 * @name: the name of the new pad.
530 * @direction: the #GstPadDirection of the pad.
532 * Creates a new pad with the given name in the given direction.
533 * If name is NULL, a guaranteed unique name (across all pads)
535 * This function makes a copy of the name so you can safely free the name.
537 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
542 gst_pad_new (const gchar * name, GstPadDirection direction)
544 return g_object_new (GST_TYPE_PAD,
545 "name", name, "direction", direction, NULL);
549 * gst_pad_new_from_template:
550 * @templ: the pad template to use
551 * @name: the name of the element
553 * Creates a new pad with the given name from the given template.
554 * If name is NULL, a guaranteed unique name (across all pads)
556 * This function makes a copy of the name so you can safely free the name.
558 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
561 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
563 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
565 return g_object_new (GST_TYPE_PAD,
566 "name", name, "direction", templ->direction, "template", templ, NULL);
570 * gst_pad_new_from_static_template:
571 * @templ: the #GstStaticPadTemplate to use
572 * @name: the name of the element
574 * Creates a new pad with the given name from the given static template.
575 * If name is NULL, a guaranteed unique name (across all pads)
577 * This function makes a copy of the name so you can safely free the name.
579 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
582 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
586 GstPadTemplate *template;
588 template = gst_static_pad_template_get (templ);
589 pad = gst_pad_new_from_template (template, name);
590 gst_object_unref (template);
595 * gst_pad_get_direction:
596 * @pad: a #GstPad to get the direction of.
598 * Gets the direction of the pad. The direction of the pad is
599 * decided at construction time so this function does not take
602 * Returns: the #GstPadDirection of the pad.
607 gst_pad_get_direction (GstPad * pad)
609 GstPadDirection result;
611 /* PAD_UNKNOWN is a little silly but we need some sort of
612 * error return value */
613 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
615 result = GST_PAD_DIRECTION (pad);
621 gst_pad_activate_default (GstPad * pad)
623 return gst_pad_activate_push (pad, TRUE);
627 pre_activate (GstPad * pad, GstActivateMode new_mode)
630 case GST_ACTIVATE_PUSH:
631 case GST_ACTIVATE_PULL:
632 GST_OBJECT_LOCK (pad);
633 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
635 GST_PAD_UNSET_FLUSHING (pad);
636 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
637 GST_OBJECT_UNLOCK (pad);
639 case GST_ACTIVATE_NONE:
640 GST_OBJECT_LOCK (pad);
641 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
642 GST_PAD_SET_FLUSHING (pad);
643 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
644 /* unlock blocked pads so element can resume and stop */
645 GST_PAD_BLOCK_BROADCAST (pad);
646 GST_OBJECT_UNLOCK (pad);
652 post_activate (GstPad * pad, GstActivateMode new_mode)
655 case GST_ACTIVATE_PUSH:
656 case GST_ACTIVATE_PULL:
659 case GST_ACTIVATE_NONE:
660 /* ensures that streaming stops */
661 GST_PAD_STREAM_LOCK (pad);
662 GST_DEBUG_OBJECT (pad, "stopped streaming");
663 GST_OBJECT_LOCK (pad);
664 clear_events (pad->priv->events);
665 GST_OBJECT_UNLOCK (pad);
666 GST_PAD_STREAM_UNLOCK (pad);
672 * gst_pad_set_active:
673 * @pad: the #GstPad to activate or deactivate.
674 * @active: whether or not the pad should be active.
676 * Activates or deactivates the given pad.
677 * Normally called from within core state change functions.
679 * If @active, makes sure the pad is active. If it is already active, either in
680 * push or pull mode, just return. Otherwise dispatches to the pad's activate
681 * function to perform the actual activation.
683 * If not @active, checks the pad's current mode and calls
684 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
687 * Returns: #TRUE if the operation was successful.
692 gst_pad_set_active (GstPad * pad, gboolean active)
695 gboolean ret = FALSE;
697 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
699 GST_OBJECT_LOCK (pad);
700 old = GST_PAD_ACTIVATE_MODE (pad);
701 GST_OBJECT_UNLOCK (pad);
705 case GST_ACTIVATE_PUSH:
706 GST_DEBUG_OBJECT (pad, "activating pad from push");
709 case GST_ACTIVATE_PULL:
710 GST_DEBUG_OBJECT (pad, "activating pad from pull");
713 case GST_ACTIVATE_NONE:
714 GST_DEBUG_OBJECT (pad, "activating pad from none");
715 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
718 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
723 case GST_ACTIVATE_PUSH:
724 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
725 ret = gst_pad_activate_push (pad, FALSE);
727 case GST_ACTIVATE_PULL:
728 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
729 ret = gst_pad_activate_pull (pad, FALSE);
731 case GST_ACTIVATE_NONE:
732 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
736 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
742 GST_OBJECT_LOCK (pad);
744 g_critical ("Failed to deactivate pad %s:%s, very bad",
745 GST_DEBUG_PAD_NAME (pad));
747 GST_WARNING_OBJECT (pad, "Failed to activate pad");
749 GST_OBJECT_UNLOCK (pad);
752 GST_OBJECT_LOCK (pad);
753 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
754 GST_OBJECT_UNLOCK (pad);
762 * gst_pad_activate_pull:
763 * @pad: the #GstPad to activate or deactivate.
764 * @active: whether or not the pad should be active.
766 * Activates or deactivates the given pad in pull mode via dispatching to the
767 * pad's activatepullfunc. For use from within pad activation functions only.
768 * When called on sink pads, will first proxy the call to the peer pad, which
769 * is expected to activate its internally linked pads from within its
770 * activate_pull function.
772 * If you don't know what this is, you probably don't want to call it.
774 * Returns: TRUE if the operation was successful.
779 gst_pad_activate_pull (GstPad * pad, gboolean active)
781 GstActivateMode old, new;
784 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
786 GST_OBJECT_LOCK (pad);
787 old = GST_PAD_ACTIVATE_MODE (pad);
788 GST_OBJECT_UNLOCK (pad);
792 case GST_ACTIVATE_PULL:
793 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
795 case GST_ACTIVATE_PUSH:
796 GST_DEBUG_OBJECT (pad,
797 "activating pad from push, deactivate push first");
798 /* pad was activate in the wrong direction, deactivate it
799 * and reactivate it in pull mode */
800 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
801 goto deactivate_failed;
802 /* fallthrough, pad is deactivated now. */
803 case GST_ACTIVATE_NONE:
804 GST_DEBUG_OBJECT (pad, "activating pad from none");
809 case GST_ACTIVATE_NONE:
810 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
812 case GST_ACTIVATE_PUSH:
813 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
814 /* pad was activated in the other direction, deactivate it
815 * in push mode, this should not happen... */
816 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
817 goto deactivate_failed;
818 /* everything is fine now */
820 case GST_ACTIVATE_PULL:
821 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
826 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
827 if ((peer = gst_pad_get_peer (pad))) {
828 GST_DEBUG_OBJECT (pad, "calling peer");
829 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
831 gst_object_unref (peer);
833 /* there is no peer, this is only fatal when we activate. When we
834 * deactivate, we must assume the application has unlinked the peer and
835 * will deactivate it eventually. */
839 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
842 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
843 goto failure; /* Can't activate pull on a src without a
847 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
848 pre_activate (pad, new);
850 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
851 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
854 /* can happen for sinks of passthrough elements */
857 post_activate (pad, new);
859 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
860 active ? "activated" : "deactivated");
866 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
867 active ? "activated" : "deactivated");
872 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
873 "failed to %s in switch to pull from mode %d",
874 (active ? "activate" : "deactivate"), old);
879 GST_OBJECT_LOCK (peer);
880 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
881 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
882 GST_OBJECT_UNLOCK (peer);
883 gst_object_unref (peer);
888 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
894 GST_OBJECT_LOCK (pad);
895 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
896 active ? "activate" : "deactivate");
897 GST_PAD_SET_FLUSHING (pad);
898 GST_PAD_ACTIVATE_MODE (pad) = old;
899 GST_OBJECT_UNLOCK (pad);
905 * gst_pad_activate_push:
906 * @pad: the #GstPad to activate or deactivate.
907 * @active: whether the pad should be active or not.
909 * Activates or deactivates the given pad in push mode via dispatching to the
910 * pad's activatepushfunc. For use from within pad activation functions only.
912 * If you don't know what this is, you probably don't want to call it.
914 * Returns: %TRUE if the operation was successful.
919 gst_pad_activate_push (GstPad * pad, gboolean active)
921 GstActivateMode old, new;
923 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
924 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
925 active ? "activated" : "deactivated");
927 GST_OBJECT_LOCK (pad);
928 old = GST_PAD_ACTIVATE_MODE (pad);
929 GST_OBJECT_UNLOCK (pad);
933 case GST_ACTIVATE_PUSH:
934 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
936 case GST_ACTIVATE_PULL:
937 GST_DEBUG_OBJECT (pad,
938 "activating pad from push, deactivating pull first");
939 /* pad was activate in the wrong direction, deactivate it
940 * an reactivate it in push mode */
941 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
942 goto deactivate_failed;
943 /* fallthrough, pad is deactivated now. */
944 case GST_ACTIVATE_NONE:
945 GST_DEBUG_OBJECT (pad, "activating pad from none");
950 case GST_ACTIVATE_NONE:
951 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
953 case GST_ACTIVATE_PULL:
954 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
955 /* pad was activated in the other direction, deactivate it
956 * in pull mode, this should not happen... */
957 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
958 goto deactivate_failed;
959 /* everything is fine now */
961 case GST_ACTIVATE_PUSH:
962 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
967 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
968 pre_activate (pad, new);
970 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
971 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
975 /* quite ok, element relies on state change func to prepare itself */
978 post_activate (pad, new);
980 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
981 active ? "activated" : "deactivated");
986 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
987 active ? "activated" : "deactivated");
992 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
993 "failed to %s in switch to push from mode %d",
994 (active ? "activate" : "deactivate"), old);
999 GST_OBJECT_LOCK (pad);
1000 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1001 active ? "activate" : "deactivate");
1002 GST_PAD_SET_FLUSHING (pad);
1003 GST_PAD_ACTIVATE_MODE (pad) = old;
1004 GST_OBJECT_UNLOCK (pad);
1010 * gst_pad_is_active:
1011 * @pad: the #GstPad to query
1013 * Query if a pad is active
1015 * Returns: TRUE if the pad is active.
1020 gst_pad_is_active (GstPad * pad)
1022 gboolean result = FALSE;
1024 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1026 GST_OBJECT_LOCK (pad);
1027 result = GST_PAD_IS_ACTIVE (pad);
1028 GST_OBJECT_UNLOCK (pad);
1034 * gst_pad_add_probe:
1035 * @pad: the #GstPad to add the probe to
1036 * @mask: the probe mask
1037 * @callback: #GstPadProbeCallback that will be called with notifications of
1039 * @user_data: (closure): user data passed to the callback
1040 * @destroy_data: #GDestroyNotify for user_data
1042 * Be notified of different states of pads. The provided callback is called for
1043 * every state that matches @mask.
1046 * Pad probe handlers are only called for source pads in push mode
1047 * and sink pads in pull mode.
1050 * Returns: an id or 0 on error. The id can be used to remove the probe with
1051 * gst_pad_remove_probe().
1056 gst_pad_add_probe (GstPad * pad, GstProbeType mask,
1057 GstPadProbeCallback callback, gpointer user_data,
1058 GDestroyNotify destroy_data)
1063 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1064 g_return_val_if_fail (mask != 0, 0);
1066 GST_OBJECT_LOCK (pad);
1067 /* make a new probe */
1068 hook = g_hook_alloc (&pad->probes);
1070 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1073 /* when no contraints are given for the types, assume all types are
1075 if ((mask & GST_PROBE_TYPE_DATA) == 0)
1076 mask |= GST_PROBE_TYPE_DATA;
1077 if ((mask & GST_PROBE_TYPE_SCHEDULING) == 0)
1078 mask |= GST_PROBE_TYPE_SCHEDULING;
1080 /* store our flags and other fields */
1081 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1082 hook->func = callback;
1083 hook->data = user_data;
1084 hook->destroy = destroy_data;
1085 PROBE_COOKIE (hook) = 0;
1087 /* incremenent cookie so that the new hook get's called */
1088 pad->priv->probe_cookie++;
1091 g_hook_prepend (&pad->probes, hook);
1094 /* get the id of the hook, we return this and it can be used to remove the
1096 res = hook->hook_id;
1098 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1100 if (mask & GST_PROBE_TYPE_BLOCKING) {
1101 /* we have a block probe */
1103 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1104 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1105 "now %d blocking probes", pad->num_blocked);
1108 /* call the callback if we need to be called for idle callbacks */
1109 if ((mask & GST_PROBE_TYPE_IDLE) && (callback != NULL)) {
1110 if (pad->priv->using > 0) {
1111 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1112 * flag above, the last thread to leave the push will do the callback. New
1113 * threads going into the push will block. */
1114 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1115 "pad is in use, delay idle callback");
1116 GST_OBJECT_UNLOCK (pad);
1118 /* the pad is idle now, we can signal the idle callback now */
1119 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1120 "pad is idle, trigger idle callback");
1121 GST_OBJECT_UNLOCK (pad);
1123 callback (pad, GST_PROBE_TYPE_IDLE, NULL, user_data);
1126 GST_OBJECT_UNLOCK (pad);
1132 cleanup_hook (GstPad * pad, GHook * hook)
1136 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1138 if (type & GST_PROBE_TYPE_BLOCKING) {
1139 /* unblock when we remove the last blocking probe */
1141 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1143 if (pad->num_blocked == 0) {
1144 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1145 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1146 GST_PAD_BLOCK_BROADCAST (pad);
1149 g_hook_destroy_link (&pad->probes, hook);
1154 * gst_pad_remove_probe:
1155 * @pad: the #GstPad with the probe
1156 * @id: the probe id to remove
1158 * Remove the probe with @id from @pad.
1163 gst_pad_remove_probe (GstPad * pad, gulong id)
1167 g_return_if_fail (GST_IS_PAD (pad));
1169 GST_OBJECT_LOCK (pad);
1171 hook = g_hook_get (&pad->probes, id);
1175 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1177 cleanup_hook (pad, hook);
1178 GST_OBJECT_UNLOCK (pad);
1184 GST_OBJECT_UNLOCK (pad);
1185 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1191 * gst_pad_is_blocked:
1192 * @pad: the #GstPad to query
1194 * Checks if the pad is blocked or not. This function returns the
1195 * last requested state of the pad. It is not certain that the pad
1196 * is actually blocking at this point (see gst_pad_is_blocking()).
1198 * Returns: TRUE if the pad is blocked.
1203 gst_pad_is_blocked (GstPad * pad)
1205 gboolean result = FALSE;
1207 g_return_val_if_fail (GST_IS_PAD (pad), result);
1209 GST_OBJECT_LOCK (pad);
1210 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1211 GST_OBJECT_UNLOCK (pad);
1217 * gst_pad_is_blocking:
1218 * @pad: the #GstPad to query
1220 * Checks if the pad is blocking or not. This is a guaranteed state
1221 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1223 * Returns: TRUE if the pad is blocking.
1230 gst_pad_is_blocking (GstPad * pad)
1232 gboolean result = FALSE;
1234 g_return_val_if_fail (GST_IS_PAD (pad), result);
1236 GST_OBJECT_LOCK (pad);
1237 /* the blocking flag is only valid if the pad is not flushing */
1238 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1239 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1240 GST_OBJECT_UNLOCK (pad);
1246 * gst_pad_check_reconfigure:
1247 * @pad: the #GstPad to check
1249 * Check and clear the #GST_PAD_NEED_RECONFIGURE flag on @pad and return %TRUE
1250 * if the flag was set.
1252 * Returns: %TRUE is the GST_PAD_NEED_RECONFIGURE flag was set on @pad.
1255 gst_pad_check_reconfigure (GstPad * pad)
1257 gboolean reconfigure;
1259 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1261 GST_OBJECT_LOCK (pad);
1262 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1263 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
1264 GST_OBJECT_UNLOCK (pad);
1270 * gst_pad_mark_reconfigure:
1271 * @pad: the #GstPad to mark
1273 * Mark a pad for needing reconfiguration. The next call to
1274 * gst_pad_check_reconfigure() will return %TRUE after this call.
1277 gst_pad_mark_reconfigure (GstPad * pad)
1279 g_return_if_fail (GST_IS_PAD (pad));
1281 GST_OBJECT_LOCK (pad);
1282 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
1283 GST_OBJECT_UNLOCK (pad);
1287 * gst_pad_set_activate_function:
1289 * @activate: the #GstPadActivateFunction to set.
1291 * Sets the given activate function for @pad. The activate function will
1292 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1293 * the actual activation. Only makes sense to set on sink pads.
1295 * Call this function if your sink pad can start a pull-based task.
1298 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1300 g_return_if_fail (GST_IS_PAD (pad));
1302 GST_PAD_ACTIVATEFUNC (pad) = activate;
1303 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1304 GST_DEBUG_FUNCPTR_NAME (activate));
1308 * gst_pad_set_activatepull_function:
1310 * @activatepull: the #GstPadActivateModeFunction to set.
1312 * Sets the given activate_pull function for the pad. An activate_pull function
1313 * prepares the element and any upstream connections for pulling. See XXX
1314 * part-activation.txt for details.
1317 gst_pad_set_activatepull_function (GstPad * pad,
1318 GstPadActivateModeFunction activatepull)
1320 g_return_if_fail (GST_IS_PAD (pad));
1322 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1323 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1324 GST_DEBUG_FUNCPTR_NAME (activatepull));
1328 * gst_pad_set_activatepush_function:
1330 * @activatepush: the #GstPadActivateModeFunction to set.
1332 * Sets the given activate_push function for the pad. An activate_push function
1333 * prepares the element for pushing. See XXX part-activation.txt for details.
1336 gst_pad_set_activatepush_function (GstPad * pad,
1337 GstPadActivateModeFunction activatepush)
1339 g_return_if_fail (GST_IS_PAD (pad));
1341 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1342 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1343 GST_DEBUG_FUNCPTR_NAME (activatepush));
1347 * gst_pad_set_chain_function:
1348 * @pad: a sink #GstPad.
1349 * @chain: the #GstPadChainFunction to set.
1351 * Sets the given chain function for the pad. The chain function is called to
1352 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1355 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1357 g_return_if_fail (GST_IS_PAD (pad));
1358 g_return_if_fail (GST_PAD_IS_SINK (pad));
1360 GST_PAD_CHAINFUNC (pad) = chain;
1361 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1362 GST_DEBUG_FUNCPTR_NAME (chain));
1366 * gst_pad_set_chain_list_function:
1367 * @pad: a sink #GstPad.
1368 * @chainlist: the #GstPadChainListFunction to set.
1370 * Sets the given chain list function for the pad. The chainlist function is
1371 * called to process a #GstBufferList input buffer list. See
1372 * #GstPadChainListFunction for more details.
1377 gst_pad_set_chain_list_function (GstPad * pad,
1378 GstPadChainListFunction chainlist)
1380 g_return_if_fail (GST_IS_PAD (pad));
1381 g_return_if_fail (GST_PAD_IS_SINK (pad));
1383 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1384 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1385 GST_DEBUG_FUNCPTR_NAME (chainlist));
1389 * gst_pad_set_getrange_function:
1390 * @pad: a source #GstPad.
1391 * @get: the #GstPadGetRangeFunction to set.
1393 * Sets the given getrange function for the pad. The getrange function is
1394 * called to produce a new #GstBuffer to start the processing pipeline. see
1395 * #GstPadGetRangeFunction for a description of the getrange function.
1398 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1400 g_return_if_fail (GST_IS_PAD (pad));
1401 g_return_if_fail (GST_PAD_IS_SRC (pad));
1403 GST_PAD_GETRANGEFUNC (pad) = get;
1405 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1406 GST_DEBUG_FUNCPTR_NAME (get));
1410 * gst_pad_set_event_function:
1411 * @pad: a #GstPad of either direction.
1412 * @event: the #GstPadEventFunction to set.
1414 * Sets the given event handler for the pad.
1417 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1419 g_return_if_fail (GST_IS_PAD (pad));
1421 GST_PAD_EVENTFUNC (pad) = event;
1423 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1424 GST_DEBUG_FUNCPTR_NAME (event));
1428 * gst_pad_set_query_function:
1429 * @pad: a #GstPad of either direction.
1430 * @query: the #GstPadQueryFunction to set.
1432 * Set the given query function for the pad.
1435 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1437 g_return_if_fail (GST_IS_PAD (pad));
1439 GST_PAD_QUERYFUNC (pad) = query;
1441 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1442 GST_DEBUG_FUNCPTR_NAME (query));
1446 * gst_pad_set_query_type_function:
1447 * @pad: a #GstPad of either direction.
1448 * @type_func: the #GstPadQueryTypeFunction to set.
1450 * Set the given query type function for the pad.
1453 gst_pad_set_query_type_function (GstPad * pad,
1454 GstPadQueryTypeFunction type_func)
1456 g_return_if_fail (GST_IS_PAD (pad));
1458 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1460 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1461 GST_DEBUG_FUNCPTR_NAME (type_func));
1465 * gst_pad_get_query_types:
1468 * Get an array of supported queries that can be performed
1471 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1474 const GstQueryType *
1475 gst_pad_get_query_types (GstPad * pad)
1477 GstPadQueryTypeFunction func;
1479 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1481 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1493 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1497 if ((peer = gst_pad_get_peer (pad))) {
1498 *data = gst_pad_get_query_types (peer);
1499 gst_object_unref (peer);
1505 * gst_pad_get_query_types_default:
1508 * Invoke the default query types function on the pad. This function will get
1509 * the supported query type from the peer of an internally linked pad of @pad.
1511 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1512 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1513 * query types function.
1515 const GstQueryType *
1516 gst_pad_get_query_types_default (GstPad * pad)
1518 GstQueryType *result = NULL;
1520 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1522 gst_pad_forward (pad, (GstPadForwardFunction)
1523 gst_pad_get_query_types_dispatcher, &result);
1529 * gst_pad_set_iterate_internal_links_function:
1530 * @pad: a #GstPad of either direction.
1531 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1533 * Sets the given internal link iterator function for the pad.
1538 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1539 GstPadIterIntLinkFunction iterintlink)
1541 g_return_if_fail (GST_IS_PAD (pad));
1543 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1544 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1545 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1549 * gst_pad_set_link_function:
1551 * @link: the #GstPadLinkFunction to set.
1553 * Sets the given link function for the pad. It will be called when
1554 * the pad is linked with another pad.
1556 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1559 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1560 * cannot be made for some reason.
1562 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1563 * of the peer sink pad, if present.
1566 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1568 g_return_if_fail (GST_IS_PAD (pad));
1570 GST_PAD_LINKFUNC (pad) = link;
1571 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1572 GST_DEBUG_FUNCPTR_NAME (link));
1576 * gst_pad_set_unlink_function:
1578 * @unlink: the #GstPadUnlinkFunction to set.
1580 * Sets the given unlink function for the pad. It will be called
1581 * when the pad is unlinked.
1584 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1586 g_return_if_fail (GST_IS_PAD (pad));
1588 GST_PAD_UNLINKFUNC (pad) = unlink;
1589 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1590 GST_DEBUG_FUNCPTR_NAME (unlink));
1594 * gst_pad_set_getcaps_function:
1596 * @getcaps: the #GstPadGetCapsFunction to set.
1598 * Sets the given getcaps function for the pad. @getcaps should return the
1599 * allowable caps for a pad in the context of the element's state, its link to
1600 * other elements, and the devices or files it has opened. These caps must be a
1601 * subset of the pad template caps. In the NULL state with no links, @getcaps
1602 * should ideally return the same caps as the pad template. In rare
1603 * circumstances, an object property can affect the caps returned by @getcaps,
1604 * but this is discouraged.
1606 * You do not need to call this function if @pad's allowed caps are always the
1607 * same as the pad template caps. This can only be true if the padtemplate
1608 * has fixed simple caps.
1610 * For most filters, the caps returned by @getcaps is directly affected by the
1611 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1612 * the srcpad's getcaps function is directly related to the stream data. Again,
1613 * @getcaps should return the most specific caps it reasonably can, since this
1614 * helps with autoplugging.
1616 * Note that the return value from @getcaps is owned by the caller, so the
1617 * caller should unref the caps after usage.
1620 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1622 g_return_if_fail (GST_IS_PAD (pad));
1624 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1625 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1626 GST_DEBUG_FUNCPTR_NAME (getcaps));
1630 * gst_pad_set_acceptcaps_function:
1632 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1634 * Sets the given acceptcaps function for the pad. The acceptcaps function
1635 * will be called to check if the pad can accept the given caps. Setting the
1636 * acceptcaps function to NULL restores the default behaviour of allowing
1637 * any caps that matches the caps from gst_pad_get_caps().
1640 gst_pad_set_acceptcaps_function (GstPad * pad,
1641 GstPadAcceptCapsFunction acceptcaps)
1643 g_return_if_fail (GST_IS_PAD (pad));
1645 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1646 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1647 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1651 * gst_pad_set_fixatecaps_function:
1653 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1655 * Sets the given fixatecaps function for the pad. The fixatecaps function
1656 * will be called whenever the default values for a GstCaps needs to be
1660 gst_pad_set_fixatecaps_function (GstPad * pad,
1661 GstPadFixateCapsFunction fixatecaps)
1663 g_return_if_fail (GST_IS_PAD (pad));
1665 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1666 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1667 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1672 * @srcpad: the source #GstPad to unlink.
1673 * @sinkpad: the sink #GstPad to unlink.
1675 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1676 * signal on both pads.
1678 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1679 * the pads were not linked together.
1684 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1686 gboolean result = FALSE;
1687 GstElement *parent = NULL;
1690 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1691 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1692 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1693 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1695 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1696 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1697 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1699 /* We need to notify the parent before taking any pad locks as the bin in
1700 * question might be waiting for a lock on the pad while holding its lock
1701 * that our message will try to take. */
1702 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1703 if (GST_IS_ELEMENT (parent)) {
1704 gst_element_post_message (parent,
1705 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1706 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1708 gst_object_unref (parent);
1713 GST_OBJECT_LOCK (srcpad);
1714 GST_OBJECT_LOCK (sinkpad);
1716 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1717 goto not_linked_together;
1719 if (GST_PAD_UNLINKFUNC (srcpad)) {
1720 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1722 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1723 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1726 /* first clear peers */
1727 GST_PAD_PEER (srcpad) = NULL;
1728 GST_PAD_PEER (sinkpad) = NULL;
1730 /* clear pending caps if any */
1731 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1732 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1734 GST_OBJECT_UNLOCK (sinkpad);
1735 GST_OBJECT_UNLOCK (srcpad);
1737 /* fire off a signal to each of the pads telling them
1738 * that they've been unlinked */
1739 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1740 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1742 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1743 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1748 if (parent != NULL) {
1749 gst_element_post_message (parent,
1750 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1751 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1752 gst_object_unref (parent);
1757 not_linked_together:
1759 /* we do not emit a warning in this case because unlinking cannot
1760 * be made MT safe.*/
1761 GST_OBJECT_UNLOCK (sinkpad);
1762 GST_OBJECT_UNLOCK (srcpad);
1768 * gst_pad_is_linked:
1769 * @pad: pad to check
1771 * Checks if a @pad is linked to another pad or not.
1773 * Returns: TRUE if the pad is linked, FALSE otherwise.
1778 gst_pad_is_linked (GstPad * pad)
1782 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1784 GST_OBJECT_LOCK (pad);
1785 result = (GST_PAD_PEER (pad) != NULL);
1786 GST_OBJECT_UNLOCK (pad);
1791 /* get the caps from both pads and see if the intersection
1794 * This function should be called with the pad LOCK on both
1798 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1799 GstPadLinkCheck flags)
1801 GstCaps *srccaps = NULL;
1802 GstCaps *sinkcaps = NULL;
1803 gboolean compatible = FALSE;
1805 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1808 /* Doing the expensive caps checking takes priority over only checking the template caps */
1809 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1810 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1811 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1813 /* If one of the two pads doesn't have a template, consider the intersection
1815 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1816 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1820 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1822 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1825 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1827 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1830 /* if we have caps on both pads we can check the intersection. If one
1831 * of the caps is NULL, we return TRUE. */
1832 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1834 gst_caps_unref (srccaps);
1836 gst_caps_unref (sinkcaps);
1840 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1841 gst_caps_unref (srccaps);
1842 gst_caps_unref (sinkcaps);
1845 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1846 (compatible ? "" : "not"));
1851 /* check if the grandparents of both pads are the same.
1852 * This check is required so that we don't try to link
1853 * pads from elements in different bins without ghostpads.
1855 * The LOCK should be held on both pads
1858 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1860 GstObject *psrc, *psink;
1862 psrc = GST_OBJECT_PARENT (src);
1863 psink = GST_OBJECT_PARENT (sink);
1865 /* if one of the pads has no parent, we allow the link */
1866 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1869 /* only care about parents that are elements */
1870 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1871 goto no_element_parent;
1873 /* if the parents are the same, we have a loop */
1874 if (G_UNLIKELY (psrc == psink))
1877 /* if they both have a parent, we check the grandparents. We can not lock
1878 * the parent because we hold on the child (pad) and the locking order is
1879 * parent >> child. */
1880 psrc = GST_OBJECT_PARENT (psrc);
1881 psink = GST_OBJECT_PARENT (psink);
1883 /* if they have grandparents but they are not the same */
1884 if (G_UNLIKELY (psrc != psink))
1885 goto wrong_grandparents;
1892 GST_CAT_DEBUG (GST_CAT_CAPS,
1893 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1894 GST_PTR_FORMAT, psrc, psink);
1899 GST_CAT_DEBUG (GST_CAT_CAPS,
1900 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1901 GST_PTR_FORMAT, psrc, psink);
1906 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1912 GST_CAT_DEBUG (GST_CAT_CAPS,
1913 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1914 GST_PTR_FORMAT, psrc, psink);
1919 /* FIXME leftover from an attempt at refactoring... */
1920 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1921 * the two pads will be locked in the srcpad, sinkpad order. */
1922 static GstPadLinkReturn
1923 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1925 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1926 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1928 GST_OBJECT_LOCK (srcpad);
1930 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1931 goto src_was_linked;
1933 GST_OBJECT_LOCK (sinkpad);
1935 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1936 goto sink_was_linked;
1938 /* check hierarchy, pads can only be linked if the grandparents
1940 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1941 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1942 goto wrong_hierarchy;
1944 /* check pad caps for non-empty intersection */
1945 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1948 /* FIXME check pad scheduling for non-empty intersection */
1950 return GST_PAD_LINK_OK;
1954 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1955 GST_DEBUG_PAD_NAME (srcpad),
1956 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1957 /* we do not emit a warning in this case because unlinking cannot
1958 * be made MT safe.*/
1959 GST_OBJECT_UNLOCK (srcpad);
1960 return GST_PAD_LINK_WAS_LINKED;
1964 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1965 GST_DEBUG_PAD_NAME (sinkpad),
1966 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1967 /* we do not emit a warning in this case because unlinking cannot
1968 * be made MT safe.*/
1969 GST_OBJECT_UNLOCK (sinkpad);
1970 GST_OBJECT_UNLOCK (srcpad);
1971 return GST_PAD_LINK_WAS_LINKED;
1975 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1976 GST_OBJECT_UNLOCK (sinkpad);
1977 GST_OBJECT_UNLOCK (srcpad);
1978 return GST_PAD_LINK_WRONG_HIERARCHY;
1982 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1983 GST_OBJECT_UNLOCK (sinkpad);
1984 GST_OBJECT_UNLOCK (srcpad);
1985 return GST_PAD_LINK_NOFORMAT;
1991 * @srcpad: the source #GstPad.
1992 * @sinkpad: the sink #GstPad.
1994 * Checks if the source pad and the sink pad are compatible so they can be
1997 * Returns: TRUE if the pads can be linked.
2000 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2002 GstPadLinkReturn result;
2004 /* generic checks */
2005 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2006 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2008 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2009 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2011 /* gst_pad_link_prepare does everything for us, we only release the locks
2012 * on the pads that it gets us. If this function returns !OK the locks are not
2014 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2015 if (result != GST_PAD_LINK_OK)
2018 GST_OBJECT_UNLOCK (srcpad);
2019 GST_OBJECT_UNLOCK (sinkpad);
2022 return result == GST_PAD_LINK_OK;
2026 * gst_pad_link_full:
2027 * @srcpad: the source #GstPad to link.
2028 * @sinkpad: the sink #GstPad to link.
2029 * @flags: the checks to validate when linking
2031 * Links the source pad and the sink pad.
2033 * This variant of #gst_pad_link provides a more granular control on the
2034 * checks being done when linking. While providing some considerable speedups
2035 * the caller of this method must be aware that wrong usage of those flags
2036 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2037 * for more information.
2041 * Returns: A result code indicating if the connection worked or
2047 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2049 GstPadLinkReturn result;
2051 GstPadLinkFunction srcfunc, sinkfunc;
2053 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2054 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2055 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2056 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2057 GST_PAD_LINK_WRONG_DIRECTION);
2059 /* Notify the parent early. See gst_pad_unlink for details. */
2060 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2061 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2062 gst_element_post_message (parent,
2063 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2064 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2066 gst_object_unref (parent);
2071 /* prepare will also lock the two pads */
2072 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2074 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2077 /* must set peers before calling the link function */
2078 GST_PAD_PEER (srcpad) = sinkpad;
2079 GST_PAD_PEER (sinkpad) = srcpad;
2081 /* make sure we update events */
2082 prepare_event_update (srcpad, sinkpad);
2084 /* get the link functions */
2085 srcfunc = GST_PAD_LINKFUNC (srcpad);
2086 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2088 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2089 /* custom link functions, execute them */
2090 GST_OBJECT_UNLOCK (sinkpad);
2091 GST_OBJECT_UNLOCK (srcpad);
2094 /* this one will call the peer link function */
2095 result = srcfunc (srcpad, sinkpad);
2096 } else if (sinkfunc) {
2097 /* if no source link function, we need to call the sink link
2098 * function ourselves. */
2099 result = sinkfunc (sinkpad, srcpad);
2102 GST_OBJECT_LOCK (srcpad);
2103 GST_OBJECT_LOCK (sinkpad);
2105 /* we released the lock, check if the same pads are linked still */
2106 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2107 goto concurrent_link;
2109 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2112 GST_OBJECT_UNLOCK (sinkpad);
2113 GST_OBJECT_UNLOCK (srcpad);
2115 /* fire off a signal to each of the pads telling them
2116 * that they've been linked */
2117 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2118 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2120 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2121 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2123 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2126 if (G_LIKELY (parent)) {
2127 gst_element_post_message (parent,
2128 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2129 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2130 gst_object_unref (parent);
2138 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2139 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2140 GST_OBJECT_UNLOCK (sinkpad);
2141 GST_OBJECT_UNLOCK (srcpad);
2143 /* The other link operation succeeded first */
2144 result = GST_PAD_LINK_WAS_LINKED;
2149 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2150 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2152 GST_PAD_PEER (srcpad) = NULL;
2153 GST_PAD_PEER (sinkpad) = NULL;
2155 GST_OBJECT_UNLOCK (sinkpad);
2156 GST_OBJECT_UNLOCK (srcpad);
2164 * @srcpad: the source #GstPad to link.
2165 * @sinkpad: the sink #GstPad to link.
2167 * Links the source pad and the sink pad.
2169 * Returns: A result code indicating if the connection worked or
2175 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2177 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2181 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2183 GstPadTemplate **template_p;
2185 /* this function would need checks if it weren't static */
2187 GST_OBJECT_LOCK (pad);
2188 template_p = &pad->padtemplate;
2189 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2190 GST_OBJECT_UNLOCK (pad);
2193 gst_pad_template_pad_created (templ, pad);
2197 * gst_pad_get_pad_template:
2200 * Gets the template for @pad.
2202 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2203 * instantiated, or %NULL if this pad has no template. Unref after
2207 gst_pad_get_pad_template (GstPad * pad)
2209 GstPadTemplate *templ;
2211 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2213 templ = GST_PAD_PAD_TEMPLATE (pad);
2215 return (templ ? gst_object_ref (templ) : NULL);
2219 caps_with_getcaps (GstPad * pad, GstCaps * filter)
2223 if (GST_PAD_GETCAPSFUNC (pad) == NULL)
2226 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2227 "dispatching to pad getcaps function with "
2228 "filter %" GST_PTR_FORMAT, filter);
2230 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2231 GST_OBJECT_UNLOCK (pad);
2232 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2233 GST_OBJECT_LOCK (pad);
2234 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2236 if (G_UNLIKELY (result == NULL))
2239 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2240 "pad getcaps returned %" GST_PTR_FORMAT, result);
2242 #ifndef G_DISABLE_ASSERT
2243 /* check that the returned caps are a real subset of the template caps */
2244 if (GST_PAD_PAD_TEMPLATE (pad)) {
2245 const GstCaps *templ_caps =
2246 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2247 if (!gst_caps_is_subset (result, templ_caps)) {
2250 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2251 "pad returned caps %" GST_PTR_FORMAT
2252 " which are not a real subset of its template caps %"
2253 GST_PTR_FORMAT, result, templ_caps);
2255 ("pad %s:%s returned caps which are not a real "
2256 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2257 temp = gst_caps_intersect (templ_caps, result);
2258 gst_caps_unref (result);
2263 if (!gst_caps_is_subset (result, filter)) {
2266 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2267 "pad returned caps %" GST_PTR_FORMAT
2268 " which are not a real subset of the filter caps %"
2269 GST_PTR_FORMAT, result, filter);
2270 g_warning ("pad %s:%s returned caps which are not a real "
2271 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2272 /* FIXME: Order? But shouldn't happen anyway... */
2273 temp = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2274 gst_caps_unref (result);
2285 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2286 GST_DEBUG_PAD_NAME (pad));
2291 /* should be called with the pad LOCK held */
2292 /* refs the caps, so caller is responsible for getting it unreffed */
2294 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2296 GstCaps *result = NULL;
2297 GstPadTemplate *templ;
2298 gboolean fixed_caps;
2300 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2302 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2305 /* fixed caps, try the negotiated caps first */
2306 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2307 if ((result = get_pad_caps (pad)))
2311 /* try the getcaps function next */
2312 if ((result = caps_with_getcaps (pad, filter)))
2315 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2316 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2317 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2322 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2323 "non-fixed pad caps: trying pad caps");
2324 /* non fixed caps, try the negotiated caps */
2325 if ((result = get_pad_caps (pad)))
2329 /* this almost never happens */
2330 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2331 result = gst_caps_new_empty ();
2335 /* run the filter on the result */
2337 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2338 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2339 GST_PTR_FORMAT, result, result, filter, filter);
2340 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2341 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2344 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2345 "using caps %p %" GST_PTR_FORMAT, result, result);
2346 result = gst_caps_ref (result);
2353 * gst_pad_has_current_caps:
2354 * @pad: a #GstPad to check
2356 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2358 * Returns: TRUE when @pad has caps associated with it.
2361 gst_pad_has_current_caps (GstPad * pad)
2365 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2367 GST_OBJECT_LOCK (pad);
2368 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2369 result = (get_pad_caps (pad) != NULL);
2370 GST_OBJECT_UNLOCK (pad);
2376 * gst_pad_get_current_caps:
2377 * @pad: a #GstPad to get the current capabilities of.
2379 * Gets the capabilities currently configured on @pad with the last
2380 * #GST_EVENT_CAPS event.
2382 * Returns: the current caps of the pad with incremented ref-count.
2385 gst_pad_get_current_caps (GstPad * pad)
2389 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2391 GST_OBJECT_LOCK (pad);
2392 if ((result = get_pad_caps (pad)))
2393 gst_caps_ref (result);
2394 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2395 "get current pad caps %" GST_PTR_FORMAT, result);
2396 GST_OBJECT_UNLOCK (pad);
2403 * @pad: a #GstPad to get the capabilities of.
2404 * @filter: suggested #GstCaps.
2406 * Gets the capabilities this pad can produce or consume.
2407 * Note that this method doesn't necessarily return the caps set by
2408 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2409 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2410 * the pad's get_caps function;
2411 * this returns the pad template caps if not explicitly set.
2413 * When called on sinkpads @filter contains the caps that
2414 * upstream could produce in the order preferred by upstream. When
2415 * called on srcpads @filter contains the caps accepted by
2416 * downstream in the preffered order. @filter might be %NULL but
2417 * if it is not %NULL the returned caps will be a subset of @filter.
2419 * Note that this function does not return writable #GstCaps, use
2420 * gst_caps_make_writable() before modifying the caps.
2422 * Returns: the caps of the pad with incremented ref-count.
2425 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2427 GstCaps *result = NULL;
2429 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2430 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2432 GST_OBJECT_LOCK (pad);
2434 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2436 result = gst_pad_get_caps_unlocked (pad, filter);
2438 GST_OBJECT_UNLOCK (pad);
2445 * gst_pad_peer_get_caps:
2446 * @pad: a #GstPad to get the capabilities of.
2447 * @filter: a #GstCaps filter.
2449 * Gets the capabilities of the peer connected to this pad. Similar to
2450 * gst_pad_get_caps().
2452 * When called on srcpads @filter contains the caps that
2453 * upstream could produce in the order preferred by upstream. When
2454 * called on sinkpads @filter contains the caps accepted by
2455 * downstream in the preffered order. @filter might be %NULL but
2456 * if it is not %NULL the returned caps will be a subset of @filter.
2458 * Returns: the caps of the peer pad with incremented ref-count. This function
2459 * returns %NULL when there is no peer pad.
2462 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2465 GstCaps *result = NULL;
2467 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2468 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2470 GST_OBJECT_LOCK (pad);
2472 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2474 peerpad = GST_PAD_PEER (pad);
2475 if (G_UNLIKELY (peerpad == NULL))
2478 gst_object_ref (peerpad);
2479 GST_OBJECT_UNLOCK (pad);
2481 result = gst_pad_get_caps (peerpad, filter);
2483 gst_object_unref (peerpad);
2489 GST_OBJECT_UNLOCK (pad);
2495 gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps)
2497 /* default fixation */
2498 gst_caps_fixate (caps);
2502 * gst_pad_fixate_caps:
2503 * @pad: a #GstPad to fixate
2504 * @caps: the #GstCaps to fixate
2506 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2507 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2510 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2512 GstPadFixateCapsFunction fixatefunc;
2514 g_return_if_fail (GST_IS_PAD (pad));
2515 g_return_if_fail (caps != NULL);
2516 g_return_if_fail (!gst_caps_is_empty (caps));
2517 g_return_if_fail (!gst_caps_is_any (caps));
2519 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2522 g_return_if_fail (gst_caps_is_writable (caps));
2524 if (G_LIKELY ((fixatefunc = GST_PAD_FIXATECAPSFUNC (pad))))
2525 fixatefunc (pad, caps);
2528 /* Default accept caps implementation just checks against
2529 * against the allowed caps for the pad */
2531 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2533 /* get the caps and see if it intersects to something not empty */
2535 gboolean result = FALSE;
2537 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2539 allowed = gst_pad_get_caps (pad, NULL);
2541 goto nothing_allowed;
2543 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2545 result = gst_caps_is_subset (caps, allowed);
2547 gst_caps_unref (allowed);
2554 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2560 * gst_pad_accept_caps:
2561 * @pad: a #GstPad to check
2562 * @caps: a #GstCaps to check on the pad
2564 * Check if the given pad accepts the caps.
2566 * Returns: TRUE if the pad can accept the caps.
2569 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2572 GstPadAcceptCapsFunction acceptfunc;
2574 GstCaps *existing = NULL;
2577 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2579 /* any pad can be unnegotiated */
2583 /* lock for checking the existing caps */
2584 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2586 GST_OBJECT_LOCK (pad);
2587 /* The current caps on a pad are trivially acceptable */
2588 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2589 if (caps == existing || gst_caps_is_equal (caps, existing))
2592 GST_OBJECT_UNLOCK (pad);
2594 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2596 /* Only null if the element explicitly unset it */
2597 if (G_UNLIKELY (acceptfunc == NULL))
2600 /* we can call the function */
2601 result = acceptfunc (pad, caps);
2602 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2609 GST_DEBUG_OBJECT (pad, "pad had same caps");
2610 GST_OBJECT_UNLOCK (pad);
2616 GST_DEBUG_OBJECT (pad, "no acceptcaps function");
2622 * gst_pad_peer_accept_caps:
2623 * @pad: a #GstPad to check the peer of
2624 * @caps: a #GstCaps to check on the pad
2626 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2629 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2632 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2637 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2639 GST_OBJECT_LOCK (pad);
2641 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2643 peerpad = GST_PAD_PEER (pad);
2644 if (G_UNLIKELY (peerpad == NULL))
2647 gst_object_ref (peerpad);
2648 /* release lock before calling external methods but keep ref to pad */
2649 GST_OBJECT_UNLOCK (pad);
2651 result = gst_pad_accept_caps (peerpad, caps);
2653 gst_object_unref (peerpad);
2659 GST_OBJECT_UNLOCK (pad);
2666 * @pad: a #GstPad to set the capabilities of.
2667 * @caps: (transfer none): a #GstCaps to set.
2669 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2670 * caps on the pad will be unreffed. This function refs the caps so you should
2671 * unref if as soon as you don't need it anymore.
2672 * It is possible to set NULL caps, which will make the pad unnegotiated
2675 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2676 * or bad parameters were provided to this function.
2681 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2684 gboolean res = TRUE;
2686 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2687 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2689 event = gst_event_new_caps (caps);
2691 if (GST_PAD_IS_SRC (pad))
2692 res = gst_pad_push_event (pad, event);
2694 res = gst_pad_send_event (pad, event);
2700 do_event_function (GstPad * pad, GstEvent * event,
2701 GstPadEventFunction eventfunc, gboolean * caps_notify)
2703 gboolean result = TRUE, call_event = TRUE;
2704 GstCaps *caps, *old, *templ;
2706 switch (GST_EVENT_TYPE (event)) {
2707 case GST_EVENT_CAPS:
2709 /* backwards compatibility mode for caps */
2710 gst_event_parse_caps (event, &caps);
2712 /* See if pad accepts the caps */
2713 templ = gst_pad_get_pad_template_caps (pad);
2714 if (!gst_caps_is_subset (caps, templ))
2717 /* check if it changed */
2718 if ((old = gst_pad_get_current_caps (pad))) {
2719 call_event = !gst_caps_is_equal (caps, old);
2720 gst_caps_unref (old);
2723 *caps_notify = TRUE;
2724 gst_caps_unref (templ);
2732 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2733 result = eventfunc (pad, event);
2735 gst_event_unref (event);
2742 gst_caps_unref (templ);
2743 gst_event_unref (event);
2744 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2745 "caps %" GST_PTR_FORMAT " not accepted", caps);
2750 /* function to send all pending events on the sinkpad to the event
2751 * function and collect the results. This function should be called with
2752 * the object lock. The object lock might be released by this function.
2754 static GstFlowReturn
2755 gst_pad_update_events (GstPad * pad)
2757 GstFlowReturn ret = GST_FLOW_OK;
2759 GstPadEventFunction eventfunc;
2761 gboolean caps_notify = FALSE;
2763 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2766 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2770 ev = &pad->priv->events[i];
2772 /* skip without pending event */
2773 if ((event = gst_event_steal (&ev->pending)) == NULL)
2776 gst_event_ref (event);
2777 GST_OBJECT_UNLOCK (pad);
2779 res = do_event_function (pad, event, eventfunc, &caps_notify);
2781 /* things could have changed while we release the lock, check if we still
2782 * are handling the same event, if we don't something changed and we have
2783 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2784 * that lock eventually and then do the retry elsewhere. */
2787 /* make the event active */
2788 gst_event_take (&ev->event, event);
2790 /* notify caps change when needed */
2792 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2793 caps_notify = FALSE;
2796 gst_event_unref (event);
2797 ret = GST_FLOW_ERROR;
2799 GST_OBJECT_LOCK (pad);
2801 /* when we get here all events were successfully updated. */
2807 g_warning ("pad %s:%s has no event handler, file a bug.",
2808 GST_DEBUG_PAD_NAME (pad));
2809 return GST_FLOW_NOT_SUPPORTED;
2814 * gst_pad_get_pad_template_caps:
2815 * @pad: a #GstPad to get the template capabilities from.
2817 * Gets the capabilities for @pad's template.
2819 * Returns: (transfer full): the #GstCaps of this pad template.
2820 * Unref after usage.
2823 gst_pad_get_pad_template_caps (GstPad * pad)
2825 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2827 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2829 if (GST_PAD_PAD_TEMPLATE (pad))
2830 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2832 return gst_static_caps_get (&anycaps);
2837 * @pad: a #GstPad to get the peer of.
2839 * Gets the peer of @pad. This function refs the peer pad so
2840 * you need to unref it after use.
2842 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2847 gst_pad_get_peer (GstPad * pad)
2851 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2853 GST_OBJECT_LOCK (pad);
2854 result = GST_PAD_PEER (pad);
2856 gst_object_ref (result);
2857 GST_OBJECT_UNLOCK (pad);
2863 * gst_pad_get_allowed_caps:
2866 * Gets the capabilities of the allowed media types that can flow through
2867 * @pad and its peer.
2869 * The allowed capabilities is calculated as the intersection of the results of
2870 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2871 * on the resulting caps.
2873 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2874 * caps when you no longer need it. This function returns NULL when @pad
2880 gst_pad_get_allowed_caps (GstPad * pad)
2887 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2889 GST_OBJECT_LOCK (pad);
2890 peer = GST_PAD_PEER (pad);
2891 if (G_UNLIKELY (peer == NULL))
2894 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2896 gst_object_ref (peer);
2897 GST_OBJECT_UNLOCK (pad);
2898 mycaps = gst_pad_get_caps (pad, NULL);
2900 peercaps = gst_pad_get_caps (peer, NULL);
2901 gst_object_unref (peer);
2903 caps = gst_caps_intersect (mycaps, peercaps);
2904 gst_caps_unref (peercaps);
2905 gst_caps_unref (mycaps);
2907 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2914 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2915 GST_OBJECT_UNLOCK (pad);
2922 * gst_pad_iterate_internal_links_default:
2923 * @pad: the #GstPad to get the internal links of.
2925 * Iterate the list of pads to which the given pad is linked to inside of
2926 * the parent element.
2927 * This is the default handler, and thus returns an iterator of all of the
2928 * pads inside the parent element with opposite direction.
2930 * The caller must free this iterator after use with gst_iterator_free().
2932 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2933 * returned pad with gst_object_unref().
2938 gst_pad_iterate_internal_links_default (GstPad * pad)
2946 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2951 GST_OBJECT_LOCK (pad);
2952 parent = GST_PAD_PARENT (pad);
2953 if (!parent || !GST_IS_ELEMENT (parent))
2956 gst_object_ref (parent);
2957 GST_OBJECT_UNLOCK (pad);
2959 if (pad->direction == GST_PAD_SRC)
2960 padlist = &parent->sinkpads;
2962 padlist = &parent->srcpads;
2964 GST_DEBUG_OBJECT (pad, "Making iterator");
2966 cookie = &parent->pads_cookie;
2968 lock = GST_OBJECT_GET_LOCK (parent);
2971 res = gst_iterator_new_list (GST_TYPE_PAD,
2972 lock, cookie, padlist, (GObject *) owner, NULL);
2974 gst_object_unref (owner);
2981 GST_OBJECT_UNLOCK (pad);
2982 GST_DEBUG_OBJECT (pad, "no parent element");
2988 * gst_pad_iterate_internal_links:
2989 * @pad: the GstPad to get the internal links of.
2991 * Gets an iterator for the pads to which the given pad is linked to inside
2992 * of the parent element.
2994 * Each #GstPad element yielded by the iterator will have its refcount increased,
2995 * so unref after use.
2997 * Free-function: gst_iterator_free
2999 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3000 * pad does not have an iterator function configured. Use
3001 * gst_iterator_free() after usage.
3006 gst_pad_iterate_internal_links (GstPad * pad)
3008 GstIterator *res = NULL;
3010 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3012 if (GST_PAD_ITERINTLINKFUNC (pad))
3013 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3021 * @forward: a #GstPadForwardFunction
3022 * @user_data: user data passed to @forward
3024 * Calls @forward for all internally linked pads of @pad. This function deals with
3025 * dynamically changing internal pads and will make sure that the @forward
3026 * function is only called once for each pad.
3028 * When @forward returns TRUE, no further pads will be processed.
3030 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3033 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3036 gboolean result = FALSE;
3038 gboolean done = FALSE;
3039 GValue item = { 0, };
3040 GList *pushed_pads = NULL;
3042 iter = gst_pad_iterate_internal_links (pad);
3048 switch (gst_iterator_next (iter, &item)) {
3049 case GST_ITERATOR_OK:
3053 intpad = g_value_get_object (&item);
3055 /* if already pushed, skip. FIXME, find something faster to tag pads */
3056 if (g_list_find (pushed_pads, intpad)) {
3057 g_value_reset (&item);
3061 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3062 GST_DEBUG_PAD_NAME (intpad));
3063 done = result = forward (intpad, user_data);
3065 pushed_pads = g_list_prepend (pushed_pads, intpad);
3067 g_value_reset (&item);
3070 case GST_ITERATOR_RESYNC:
3071 /* We don't reset the result here because we don't push the event
3072 * again on pads that got the event already and because we need
3073 * to consider the result of the previous pushes */
3074 gst_iterator_resync (iter);
3076 case GST_ITERATOR_ERROR:
3077 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3080 case GST_ITERATOR_DONE:
3085 g_value_unset (&item);
3086 gst_iterator_free (iter);
3088 g_list_free (pushed_pads);
3098 gboolean dispatched;
3102 event_forward_func (GstPad * pad, EventData * data)
3104 /* for each pad we send to, we should ref the event; it's up
3105 * to downstream to unref again when handled. */
3106 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3107 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3109 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3111 data->dispatched = TRUE;
3118 * gst_pad_event_default:
3119 * @pad: a #GstPad to call the default event handler on.
3120 * @event: (transfer full): the #GstEvent to handle.
3122 * Invokes the default event handler for the given pad.
3124 * The EOS event will pause the task associated with @pad before it is forwarded
3125 * to all internally linked pads,
3127 * The CAPS event will never be forwarded.
3129 * The the event is sent to all pads internally linked to @pad. This function
3130 * takes ownership of @event.
3132 * Returns: TRUE if the event was sent successfully.
3135 gst_pad_event_default (GstPad * pad, GstEvent * event)
3140 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3141 g_return_val_if_fail (event != NULL, FALSE);
3143 GST_LOG_OBJECT (pad, "default event handler");
3145 switch (GST_EVENT_TYPE (event)) {
3148 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3149 gst_pad_pause_task (pad);
3157 data.dispatched = FALSE;
3158 data.result = FALSE;
3160 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3162 /* for sinkpads without a parent element or without internal links, nothing
3163 * will be dispatched but we still want to return TRUE. */
3164 if (data.dispatched)
3165 result = data.result;
3169 gst_event_unref (event);
3176 * @pad: a #GstPad to invoke the default query on.
3177 * @query: (transfer none): the #GstQuery to perform.
3179 * Dispatches a query to a pad. The query should have been allocated by the
3180 * caller via one of the type-specific allocation functions. The element that
3181 * the pad belongs to is responsible for filling the query with an appropriate
3182 * response, which should then be parsed with a type-specific query parsing
3185 * Again, the caller is responsible for both the allocation and deallocation of
3186 * the query structure.
3188 * Please also note that some queries might need a running pipeline to work.
3190 * Returns: TRUE if the query could be performed.
3193 gst_pad_query (GstPad * pad, GstQuery * query)
3196 GstPadQueryFunction func;
3198 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3199 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3201 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3202 GST_QUERY_TYPE_NAME (query));
3204 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3207 res = func (pad, query);
3209 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3210 GST_QUERY_TYPE_NAME (query), res);
3216 GST_DEBUG_OBJECT (pad, "had no query function");
3222 * gst_pad_peer_query:
3223 * @pad: a #GstPad to invoke the peer query on.
3224 * @query: (transfer none): the #GstQuery to perform.
3226 * Performs gst_pad_query() on the peer of @pad.
3228 * The caller is responsible for both the allocation and deallocation of
3229 * the query structure.
3231 * Returns: TRUE if the query could be performed. This function returns %FALSE
3232 * if @pad has no peer.
3237 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3242 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3243 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3245 GST_OBJECT_LOCK (pad);
3247 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3248 GST_QUERY_TYPE_NAME (query));
3250 peerpad = GST_PAD_PEER (pad);
3251 if (G_UNLIKELY (peerpad == NULL))
3254 gst_object_ref (peerpad);
3255 GST_OBJECT_UNLOCK (pad);
3257 result = gst_pad_query (peerpad, query);
3259 gst_object_unref (peerpad);
3266 GST_WARNING_OBJECT (pad, "pad has no peer");
3267 GST_OBJECT_UNLOCK (pad);
3273 * gst_pad_query_default:
3274 * @pad: a #GstPad to call the default query handler on.
3275 * @query: (transfer none): the #GstQuery to handle.
3277 * Invokes the default query handler for the given pad.
3278 * The query is sent to all pads internally linked to @pad. Note that
3279 * if there are many possible sink pads that are internally linked to
3280 * @pad, only one will be sent the query.
3281 * Multi-sinkpad elements should implement custom query handlers.
3283 * Returns: TRUE if the query was performed successfully.
3286 gst_pad_query_default (GstPad * pad, GstQuery * query)
3288 gboolean forward = TRUE, ret = FALSE;
3290 switch (GST_QUERY_TYPE (query)) {
3291 case GST_QUERY_SCHEDULING:
3294 case GST_QUERY_POSITION:
3295 case GST_QUERY_SEEKING:
3296 case GST_QUERY_FORMATS:
3297 case GST_QUERY_LATENCY:
3298 case GST_QUERY_JITTER:
3299 case GST_QUERY_RATE:
3300 case GST_QUERY_CONVERT:
3301 case GST_QUERY_ALLOCATION:
3307 ret = gst_pad_forward
3308 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3314 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3316 GstPad *pad = data->pad;
3318 GstPadProbeCallback callback;
3321 /* if we have called this callback, do nothing */
3322 if (PROBE_COOKIE (hook) == data->cookie)
3325 PROBE_COOKIE (hook) = data->cookie;
3327 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3329 /* one of the data types */
3330 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3332 /* one of the scheduling types */
3333 if ((flags & GST_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3335 /* all of the blocking types must match */
3336 if ((flags & GST_PROBE_TYPE_BLOCKING) !=
3337 (data->mask & GST_PROBE_TYPE_BLOCKING))
3340 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3341 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3343 callback = (GstPadProbeCallback) hook->func;
3344 if (callback == NULL)
3347 GST_OBJECT_UNLOCK (pad);
3349 ret = callback (pad, data->mask, data->type_data, hook->data);
3351 GST_OBJECT_LOCK (pad);
3354 case GST_PROBE_REMOVE:
3355 /* remove the probe */
3356 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3357 cleanup_hook (pad, hook);
3359 case GST_PROBE_DROP:
3360 /* need to drop the data, make sure other probes don't get called
3362 GST_DEBUG_OBJECT (pad, "asked to drop item");
3363 data->mask = GST_PROBE_TYPE_INVALID;
3364 data->ret = GST_PROBE_DROP;
3366 case GST_PROBE_PASS:
3367 /* inform the pad block to let things pass */
3368 GST_DEBUG_OBJECT (pad, "asked to pass item");
3372 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3377 #define PROBE(pad,mask,data,label) \
3379 if (G_UNLIKELY (pad->num_probes)) { \
3380 ret = do_probe_callbacks (pad, mask, data); \
3381 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3386 static GstFlowReturn
3387 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3394 data.type_data = type_data;
3395 data.ret = GST_PROBE_OK;
3397 data.cookie = pad->priv->probe_cookie++;
3400 cookie = pad->priv->probe_cookie;
3402 g_hook_list_marshal (&pad->probes, FALSE,
3403 (GHookMarshaller) probe_hook_marshal, &data);
3405 /* if the list changed, call the new callbacks (they will not have their
3406 * cookie set to data.cookie */
3407 if (cookie != pad->priv->probe_cookie) {
3408 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3409 "probe list changed, restarting");
3413 if (data.ret == GST_PROBE_DROP)
3419 if (mask & GST_PROBE_TYPE_BLOCK) {
3420 while (GST_PAD_IS_BLOCKED (pad)) {
3421 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3422 "we are blocked %d times", pad->num_blocked);
3424 /* we might have released the lock */
3425 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3428 /* now we block the streaming thread. It can be unlocked when we
3429 * deactivate the pad (which will also set the FLUSHING flag) or
3430 * when the pad is unblocked. A flushing event will also unblock
3431 * the pad after setting the FLUSHING flag. */
3432 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3433 "Waiting to be unblocked or set flushing");
3434 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3435 GST_PAD_BLOCK_WAIT (pad);
3436 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3437 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3439 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3449 GST_DEBUG_OBJECT (pad, "pad is flushing");
3450 return GST_FLOW_WRONG_STATE;
3454 GST_DEBUG_OBJECT (pad, "data is dropped");
3455 return GST_FLOW_CUSTOM_SUCCESS;
3459 GST_DEBUG_OBJECT (pad, "data is passed");
3467 * gst_pad_get_offset:
3470 * Get the offset applied to the running time of @pad. @pad has to be a source
3473 * Returns: the offset.
3476 gst_pad_get_offset (GstPad * pad)
3480 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3482 GST_OBJECT_LOCK (pad);
3483 result = pad->offset;
3484 GST_OBJECT_UNLOCK (pad);
3490 * gst_pad_set_offset:
3492 * @offset: the offset
3494 * Set the offset that will be applied to the running time of @pad.
3497 gst_pad_set_offset (GstPad * pad, gint64 offset)
3503 g_return_if_fail (GST_IS_PAD (pad));
3505 GST_OBJECT_LOCK (pad);
3506 /* if nothing changed, do nothing */
3507 if (pad->offset == offset)
3510 pad->offset = offset;
3512 /* if no peer, we just updated the offset */
3513 if ((peer = GST_PAD_PEER (pad)) == NULL)
3516 /* switch pads around when dealing with a sinkpad */
3517 if (GST_PAD_IS_SINK (pad)) {
3518 /* ref the peer so it doesn't go away when we release the lock */
3519 tmp = gst_object_ref (peer);
3520 /* make sure we get the peer (the srcpad) */
3521 GST_OBJECT_UNLOCK (pad);
3527 GST_OBJECT_LOCK (pad);
3528 /* check if the pad didn't get relinked */
3529 if (GST_PAD_PEER (pad) != peer)
3532 /* we can release the ref now */
3533 gst_object_unref (peer);
3536 /* the index of the segment event in the array */
3537 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3539 /* lock order is srcpad >> sinkpad */
3540 GST_OBJECT_LOCK (peer);
3541 /* take the current segment event, adjust it and then place
3542 * it on the sinkpad. events on the srcpad are always active. */
3543 if (replace_event (pad, peer, idx))
3544 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3546 GST_OBJECT_UNLOCK (peer);
3549 GST_OBJECT_UNLOCK (pad);
3553 /**********************************************************************
3554 * Data passing functions
3557 /* this is the chain function that does not perform the additional argument
3558 * checking for that little extra speed.
3560 static inline GstFlowReturn
3561 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3564 gboolean needs_events;
3566 GST_PAD_STREAM_LOCK (pad);
3568 GST_OBJECT_LOCK (pad);
3569 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3572 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3573 if (G_UNLIKELY (needs_events)) {
3574 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3576 GST_DEBUG_OBJECT (pad, "need to update all events");
3577 ret = gst_pad_update_events (pad);
3578 if (G_UNLIKELY (ret != GST_FLOW_OK))
3582 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3584 GST_OBJECT_UNLOCK (pad);
3586 /* NOTE: we read the chainfunc unlocked.
3587 * we cannot hold the lock for the pad so we might send
3588 * the data to the wrong function. This is not really a
3589 * problem since functions are assigned at creation time
3590 * and don't change that often... */
3591 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3592 GstPadChainFunction chainfunc;
3594 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3597 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3598 "calling chainfunction &%s with buffer %p, ts %" GST_TIME_FORMAT,
3599 GST_DEBUG_FUNCPTR_NAME (chainfunc),
3600 GST_BUFFER (data), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (data)));
3602 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3604 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3605 "called chainfunction &%s with buffer %p, returned %s",
3606 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3608 GstPadChainListFunction chainlistfunc;
3610 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3613 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3614 "calling chainlistfunction &%s",
3615 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3617 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3619 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3620 "called chainlistfunction &%s, returned %s",
3621 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3624 GST_PAD_STREAM_UNLOCK (pad);
3631 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3632 "chaining, but pad was flushing");
3633 GST_OBJECT_UNLOCK (pad);
3634 GST_PAD_STREAM_UNLOCK (pad);
3635 return GST_FLOW_WRONG_STATE;
3639 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3640 GST_OBJECT_UNLOCK (pad);
3641 GST_PAD_STREAM_UNLOCK (pad);
3646 GST_OBJECT_UNLOCK (pad);
3647 GST_PAD_STREAM_UNLOCK (pad);
3648 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3651 case GST_FLOW_CUSTOM_SUCCESS:
3652 GST_DEBUG_OBJECT (pad, "dropped buffer");
3656 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3663 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3664 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3665 "pushing, but not chainhandler");
3666 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3667 ("push on pad %s:%s but it has no chainfunction",
3668 GST_DEBUG_PAD_NAME (pad)));
3669 GST_PAD_STREAM_UNLOCK (pad);
3670 return GST_FLOW_NOT_SUPPORTED;
3676 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3677 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3680 * Chain a buffer to @pad.
3682 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3684 * If the buffer type is not acceptable for @pad (as negotiated with a
3685 * preceeding GST_EVENT_CAPS event), this function returns
3686 * #GST_FLOW_NOT_NEGOTIATED.
3688 * The function proceeds calling the chain function installed on @pad (see
3689 * gst_pad_set_chain_function()) and the return value of that function is
3690 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3693 * In all cases, success or failure, the caller loses its reference to @buffer
3694 * after calling this function.
3696 * Returns: a #GstFlowReturn from the pad.
3701 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3703 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3704 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3705 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3707 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3710 static GstFlowReturn
3711 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3717 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3719 len = gst_buffer_list_len (list);
3722 for (i = 0; i < len; i++) {
3723 buffer = gst_buffer_list_get (list, i);
3725 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3726 gst_buffer_ref (buffer));
3727 if (ret != GST_FLOW_OK)
3730 gst_buffer_list_unref (list);
3736 * gst_pad_chain_list:
3737 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3738 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3741 * Chain a bufferlist to @pad.
3743 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3745 * If @pad was not negotiated properly with a CAPS event, this function
3746 * returns #GST_FLOW_NOT_NEGOTIATED.
3748 * The function proceeds calling the chainlist function installed on @pad (see
3749 * gst_pad_set_chain_list_function()) and the return value of that function is
3750 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3751 * chainlist function.
3753 * In all cases, success or failure, the caller loses its reference to @list
3754 * after calling this function.
3758 * Returns: a #GstFlowReturn from the pad.
3763 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3765 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3766 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3767 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3769 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3772 static GstFlowReturn
3773 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3778 GST_OBJECT_LOCK (pad);
3779 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3782 type |= GST_PROBE_TYPE_PUSH;
3784 /* do block probes */
3785 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3787 /* do post-blocking probes */
3788 PROBE (pad, type, data, probe_stopped);
3790 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3793 /* take ref to peer pad before releasing the lock */
3794 gst_object_ref (peer);
3796 GST_OBJECT_UNLOCK (pad);
3798 ret = gst_pad_chain_data_unchecked (peer, type, data);
3800 gst_object_unref (peer);
3802 GST_OBJECT_LOCK (pad);
3804 if (pad->priv->using == 0) {
3805 /* pad is not active anymore, trigger idle callbacks */
3806 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3808 GST_OBJECT_UNLOCK (pad);
3812 /* ERROR recovery here */
3816 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3817 "pushing, but pad was flushing");
3818 GST_OBJECT_UNLOCK (pad);
3819 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3820 return GST_FLOW_WRONG_STATE;
3824 GST_OBJECT_UNLOCK (pad);
3825 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3828 case GST_FLOW_CUSTOM_SUCCESS:
3829 GST_DEBUG_OBJECT (pad, "dropped buffer");
3833 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3840 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3841 "pushing, but it was not linked");
3842 GST_OBJECT_UNLOCK (pad);
3843 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3844 return GST_FLOW_NOT_LINKED;
3850 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3851 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3854 * Pushes a buffer to the peer of @pad.
3856 * This function will call installed block probes before triggering any
3857 * installed data probes.
3859 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3860 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3863 * In all cases, success or failure, the caller loses its reference to @buffer
3864 * after calling this function.
3866 * Returns: a #GstFlowReturn from the peer pad.
3871 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3873 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3874 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3875 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3877 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
3881 * gst_pad_push_list:
3882 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3883 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3886 * Pushes a buffer list to the peer of @pad.
3888 * This function will call installed block probes before triggering any
3889 * installed data probes.
3891 * The function proceeds calling the chain function on the peer pad and returns
3892 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3893 * be returned. If the peer pad does not have any installed chainlist function
3894 * every group buffer of the list will be merged into a normal #GstBuffer and
3895 * chained via gst_pad_chain().
3897 * In all cases, success or failure, the caller loses its reference to @list
3898 * after calling this function.
3900 * Returns: a #GstFlowReturn from the peer pad.
3907 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3909 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3910 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3911 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3913 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3916 static GstFlowReturn
3917 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3918 GstBuffer ** buffer)
3921 GstPadGetRangeFunction getrangefunc;
3923 GST_PAD_STREAM_LOCK (pad);
3925 GST_OBJECT_LOCK (pad);
3926 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3928 GST_OBJECT_UNLOCK (pad);
3930 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3933 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3934 "calling getrangefunc %s, offset %"
3935 G_GUINT64_FORMAT ", size %u",
3936 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3938 ret = getrangefunc (pad, offset, size, buffer);
3940 if (G_UNLIKELY (ret != GST_FLOW_OK))
3941 goto get_range_failed;
3943 /* can only fire the signal if we have a valid buffer */
3944 GST_OBJECT_LOCK (pad);
3945 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
3947 GST_OBJECT_UNLOCK (pad);
3949 GST_PAD_STREAM_UNLOCK (pad);
3956 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3957 "getrange, but pad was flushing");
3958 GST_OBJECT_UNLOCK (pad);
3959 GST_PAD_STREAM_UNLOCK (pad);
3960 return GST_FLOW_WRONG_STATE;
3964 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3965 ("getrange on pad %s:%s but it has no getrangefunction",
3966 GST_DEBUG_PAD_NAME (pad)));
3967 GST_PAD_STREAM_UNLOCK (pad);
3968 return GST_FLOW_NOT_SUPPORTED;
3972 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3973 "probe returned %s", gst_flow_get_name (ret));
3974 GST_OBJECT_UNLOCK (pad);
3975 GST_PAD_STREAM_UNLOCK (pad);
3976 gst_buffer_unref (*buffer);
3982 GST_PAD_STREAM_UNLOCK (pad);
3984 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3985 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3986 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3992 * gst_pad_get_range:
3993 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3994 * @offset: The start offset of the buffer
3995 * @size: The length of the buffer
3996 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3997 * returns #GST_FLOW_ERROR if %NULL.
3999 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4000 * immediately and @buffer is %NULL.
4002 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4003 * description of a getrange function. If @pad has no getrange function
4004 * installed (see gst_pad_set_getrange_function()) this function returns
4005 * #GST_FLOW_NOT_SUPPORTED.
4007 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4009 * Returns: a #GstFlowReturn from the pad.
4014 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4015 GstBuffer ** buffer)
4017 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4018 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4019 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4021 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4025 * gst_pad_pull_range:
4026 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4027 * @offset: The start offset of the buffer
4028 * @size: The length of the buffer
4029 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4030 * GST_FLOW_ERROR if %NULL.
4032 * Pulls a @buffer from the peer pad.
4034 * This function will first trigger the pad block signal if it was
4037 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4038 * function returns the result of gst_pad_get_range() on the peer pad.
4039 * See gst_pad_get_range() for a list of return values and for the
4040 * semantics of the arguments of this function.
4042 * Returns: a #GstFlowReturn from the peer pad.
4043 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4044 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4045 * @buffer may not be used or freed when any other return value than
4046 * #GST_FLOW_OK is returned.
4051 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4052 GstBuffer ** buffer)
4056 gboolean needs_events;
4058 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4059 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4060 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4062 GST_OBJECT_LOCK (pad);
4063 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4066 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4069 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4072 gst_object_ref (peer);
4074 GST_OBJECT_UNLOCK (pad);
4076 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4078 gst_object_unref (peer);
4080 GST_OBJECT_LOCK (pad);
4082 if (pad->priv->using == 0) {
4083 /* pad is not active anymore, trigger idle callbacks */
4084 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4085 post_probe_stopped);
4088 if (G_UNLIKELY (ret != GST_FLOW_OK))
4089 goto pull_range_failed;
4091 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4092 post_probe_stopped);
4094 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4095 if (G_UNLIKELY (needs_events)) {
4096 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4098 GST_DEBUG_OBJECT (pad, "we need to update the events");
4099 ret = gst_pad_update_events (pad);
4100 if (G_UNLIKELY (ret != GST_FLOW_OK))
4103 GST_OBJECT_UNLOCK (pad);
4107 /* ERROR recovery here */
4110 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4111 "pullrange, but pad was flushing");
4112 GST_OBJECT_UNLOCK (pad);
4113 return GST_FLOW_WRONG_STATE;
4117 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4118 gst_flow_get_name (ret));
4119 GST_OBJECT_UNLOCK (pad);
4124 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4125 "pulling range, but it was not linked");
4126 GST_OBJECT_UNLOCK (pad);
4127 return GST_FLOW_NOT_LINKED;
4132 GST_OBJECT_UNLOCK (pad);
4133 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4134 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4135 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4140 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4141 "post probe returned %s", gst_flow_get_name (ret));
4142 GST_OBJECT_UNLOCK (pad);
4143 if (ret == GST_FLOW_OK)
4144 gst_buffer_unref (*buffer);
4150 GST_OBJECT_UNLOCK (pad);
4151 gst_buffer_unref (*buffer);
4153 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4154 "pullrange returned events that were not accepted");
4160 * gst_pad_push_event:
4161 * @pad: a #GstPad to push the event to.
4162 * @event: (transfer full): the #GstEvent to send to the pad.
4164 * Sends the event to the peer of the given pad. This function is
4165 * mainly used by elements to send events to their peer
4168 * This function takes owership of the provided event so you should
4169 * gst_event_ref() it if you want to reuse the event after this call.
4171 * Returns: TRUE if the event was handled.
4176 gst_pad_push_event (GstPad * pad, GstEvent * event)
4181 gboolean stored = FALSE;
4183 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4184 g_return_val_if_fail (event != NULL, FALSE);
4185 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4187 GST_OBJECT_LOCK (pad);
4189 peerpad = GST_PAD_PEER (pad);
4191 /* Two checks to be made:
4192 * . (un)set the FLUSHING flag for flushing events,
4193 * . handle pad blocking */
4194 switch (GST_EVENT_TYPE (event)) {
4195 case GST_EVENT_FLUSH_START:
4196 GST_PAD_SET_FLUSHING (pad);
4198 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4199 /* flush start will have set the FLUSHING flag and will then
4200 * unlock all threads doing a GCond wait on the blocking pad. This
4201 * will typically unblock the STREAMING thread blocked on a pad. */
4202 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4203 "doing block signal.");
4204 GST_PAD_BLOCK_BROADCAST (pad);
4208 case GST_EVENT_FLUSH_STOP:
4209 GST_PAD_UNSET_FLUSHING (pad);
4210 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4211 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4217 /* store the event on the pad, but only on srcpads */
4218 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4221 idx = GST_EVENT_STICKY_IDX (event);
4222 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4223 GST_EVENT_TYPE_NAME (event), idx);
4225 /* srcpad sticky events always become active immediately */
4226 gst_event_replace (&pad->priv->events[idx].event, event);
4231 /* backwards compatibility mode for caps */
4232 switch (GST_EVENT_TYPE (event)) {
4233 case GST_EVENT_CAPS:
4235 GST_OBJECT_UNLOCK (pad);
4237 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4239 GST_OBJECT_LOCK (pad);
4240 /* the peerpad might have changed. Things we checked above could not
4242 peerpad = GST_PAD_PEER (pad);
4245 case GST_EVENT_SEGMENT:
4249 offset = pad->offset;
4250 /* check if we need to adjust the segment */
4251 if (offset != 0 && (peerpad != NULL)) {
4254 /* copy segment values */
4255 gst_event_copy_segment (event, &segment);
4256 gst_event_unref (event);
4258 /* adjust and make a new event with the offset applied */
4259 segment.base += offset;
4260 event = gst_event_new_segment (&segment);
4264 case GST_EVENT_RECONFIGURE:
4265 if (GST_PAD_IS_SINK (pad))
4266 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4272 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4275 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4276 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4282 /* send probes after modifying the events above */
4283 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4285 /* now check the peer pad */
4286 if (peerpad == NULL)
4289 gst_object_ref (peerpad);
4291 GST_OBJECT_UNLOCK (pad);
4293 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4294 event, GST_EVENT_TYPE_NAME (event), peerpad);
4296 result = gst_pad_send_event (peerpad, event);
4298 /* Note: we gave away ownership of the event at this point but we can still
4299 * print the old pointer */
4300 GST_LOG_OBJECT (pad,
4301 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4304 gst_object_unref (peerpad);
4306 GST_OBJECT_LOCK (pad);
4308 if (pad->priv->using == 0) {
4309 /* pad is not active anymore, trigger idle callbacks */
4310 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4312 GST_OBJECT_UNLOCK (pad);
4314 return result | stored;
4316 /* ERROR handling */
4319 GST_DEBUG_OBJECT (pad, "We're flushing");
4320 GST_OBJECT_UNLOCK (pad);
4321 gst_event_unref (event);
4326 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4327 GST_OBJECT_UNLOCK (pad);
4328 gst_event_unref (event);
4333 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4334 GST_OBJECT_UNLOCK (pad);
4335 gst_event_unref (event);
4341 * gst_pad_send_event:
4342 * @pad: a #GstPad to send the event to.
4343 * @event: (transfer full): the #GstEvent to send to the pad.
4345 * Sends the event to the pad. This function can be used
4346 * by applications to send events in the pipeline.
4348 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4349 * sink pad, @event should be a downstream event. For example, you would not
4350 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4351 * Furthermore, some downstream events have to be serialized with data flow,
4352 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4353 * the event needs to be serialized with data flow, this function will take the
4354 * pad's stream lock while calling its event function.
4356 * To find out whether an event type is upstream, downstream, or downstream and
4357 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4358 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4359 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4360 * plugin doesn't need to bother itself with this information; the core handles
4361 * all necessary locks and checks.
4363 * This function takes owership of the provided event so you should
4364 * gst_event_ref() it if you want to reuse the event after this call.
4366 * Returns: TRUE if the event was handled.
4369 gst_pad_send_event (GstPad * pad, GstEvent * event)
4372 gboolean result = FALSE;
4373 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4375 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4376 g_return_val_if_fail (event != NULL, FALSE);
4378 GST_OBJECT_LOCK (pad);
4379 if (GST_PAD_IS_SINK (pad)) {
4380 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4381 goto wrong_direction;
4382 serialized = GST_EVENT_IS_SERIALIZED (event);
4383 sticky = GST_EVENT_IS_STICKY (event);
4384 } else if (GST_PAD_IS_SRC (pad)) {
4385 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4386 goto wrong_direction;
4387 /* events on srcpad never are serialized and sticky */
4388 serialized = sticky = FALSE;
4390 goto unknown_direction;
4392 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4394 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4396 switch (GST_EVENT_TYPE (event)) {
4397 case GST_EVENT_FLUSH_START:
4398 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4399 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4401 /* can't even accept a flush begin event when flushing */
4402 if (GST_PAD_IS_FLUSHING (pad))
4405 GST_PAD_SET_FLUSHING (pad);
4406 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4407 needs_events = FALSE;
4409 case GST_EVENT_FLUSH_STOP:
4410 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4411 GST_PAD_UNSET_FLUSHING (pad);
4412 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4414 GST_OBJECT_UNLOCK (pad);
4415 /* grab stream lock */
4416 GST_PAD_STREAM_LOCK (pad);
4418 GST_OBJECT_LOCK (pad);
4419 needs_events = FALSE;
4421 case GST_EVENT_RECONFIGURE:
4422 if (GST_PAD_IS_SRC (pad))
4423 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4425 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4426 GST_EVENT_TYPE_NAME (event));
4429 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4430 GST_OBJECT_UNLOCK (pad);
4431 GST_PAD_STREAM_LOCK (pad);
4433 GST_OBJECT_LOCK (pad);
4435 /* don't forward events on non-serialized events */
4436 needs_events = FALSE;
4439 /* store the event on the pad, but only on srcpads. We need to store the
4440 * event before checking the flushing flag. */
4445 switch (GST_EVENT_TYPE (event)) {
4446 case GST_EVENT_SEGMENT:
4447 if (pad->offset != 0) {
4450 /* copy segment values */
4451 gst_event_copy_segment (event, &segment);
4452 gst_event_unref (event);
4454 /* adjust and make a new event with the offset applied */
4455 segment.base += pad->offset;
4456 event = gst_event_new_segment (&segment);
4463 idx = GST_EVENT_STICKY_IDX (event);
4464 ev = &pad->priv->events[idx];
4466 if (ev->event != event) {
4467 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4468 GST_EVENT_TYPE_NAME (event), idx);
4469 gst_event_replace (&ev->pending, event);
4470 /* set the flag so that we update the events next time. We would
4471 * usually update below but we might be flushing too. */
4472 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4473 needs_events = TRUE;
4476 /* now do the probe */
4477 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4480 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4486 if (G_UNLIKELY (needs_events)) {
4489 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4491 GST_DEBUG_OBJECT (pad, "need to update all events");
4492 ret = gst_pad_update_events (pad);
4493 if (ret != GST_FLOW_OK)
4495 GST_OBJECT_UNLOCK (pad);
4497 gst_event_unref (event);
4502 /* ensure to pass on event;
4503 * note that a sticky event has already been updated above */
4504 if (G_LIKELY (!needs_events || !sticky)) {
4505 GstPadEventFunction eventfunc;
4507 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4510 GST_OBJECT_UNLOCK (pad);
4512 result = eventfunc (pad, event);
4516 GST_PAD_STREAM_UNLOCK (pad);
4518 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4522 /* ERROR handling */
4525 g_warning ("pad %s:%s sending %s event in wrong direction",
4526 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4527 GST_OBJECT_UNLOCK (pad);
4528 gst_event_unref (event);
4533 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4534 GST_OBJECT_UNLOCK (pad);
4535 gst_event_unref (event);
4540 g_warning ("pad %s:%s has no event handler, file a bug.",
4541 GST_DEBUG_PAD_NAME (pad));
4542 GST_OBJECT_UNLOCK (pad);
4544 GST_PAD_STREAM_UNLOCK (pad);
4545 gst_event_unref (event);
4550 GST_OBJECT_UNLOCK (pad);
4552 GST_PAD_STREAM_UNLOCK (pad);
4553 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4554 "Received event on flushing pad. Discarding");
4555 gst_event_unref (event);
4560 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4561 GST_OBJECT_UNLOCK (pad);
4563 GST_PAD_STREAM_UNLOCK (pad);
4564 gst_event_unref (event);
4569 GST_OBJECT_UNLOCK (pad);
4571 GST_PAD_STREAM_UNLOCK (pad);
4572 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4573 gst_event_unref (event);
4579 * gst_pad_set_element_private:
4580 * @pad: the #GstPad to set the private data of.
4581 * @priv: The private data to attach to the pad.
4583 * Set the given private data gpointer on the pad.
4584 * This function can only be used by the element that owns the pad.
4585 * No locking is performed in this function.
4588 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4590 pad->element_private = priv;
4594 * gst_pad_get_element_private:
4595 * @pad: the #GstPad to get the private data of.
4597 * Gets the private data of a pad.
4598 * No locking is performed in this function.
4600 * Returns: (transfer none): a #gpointer to the private data.
4603 gst_pad_get_element_private (GstPad * pad)
4605 return pad->element_private;
4609 * gst_pad_get_sticky_event:
4610 * @pad: the #GstPad to get the event from.
4611 * @event_type: the #GstEventType that should be retrieved.
4613 * Returns a new reference of the sticky event of type @event_type
4616 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4619 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4621 GstEvent *event = NULL;
4624 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4625 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4627 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4629 GST_OBJECT_LOCK (pad);
4630 if ((event = pad->priv->events[idx].event)) {
4631 gst_event_ref (event);
4633 GST_OBJECT_UNLOCK (pad);
4639 * gst_pad_sticky_events_foreach:
4640 * @pad: the #GstPad that should be used for iteration.
4641 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4642 * @user_data: (closure): the optional user data.
4644 * Iterates all active sticky events on @pad and calls @foreach_func for every
4645 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4646 * is immediately stopped.
4648 * Returns: GST_FLOW_OK if iteration was successful
4651 gst_pad_sticky_events_foreach (GstPad * pad,
4652 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4654 GstFlowReturn ret = GST_FLOW_OK;
4658 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4659 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4661 GST_OBJECT_LOCK (pad);
4664 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4668 ev = &pad->priv->events[i];
4670 /* skip without active event */
4671 if ((event = ev->event) == NULL)
4674 gst_event_ref (event);
4675 GST_OBJECT_UNLOCK (pad);
4677 res = foreach_func (pad, event, user_data);
4679 GST_OBJECT_LOCK (pad);
4680 gst_event_unref (event);
4682 if (res != GST_FLOW_OK) {
4687 /* things could have changed while we release the lock, check if we still
4688 * are handling the same event, if we don't something changed and we have
4689 * to try again. FIXME. we need a cookie here. */
4690 if (event != ev->event) {
4691 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4695 GST_OBJECT_UNLOCK (pad);
4701 do_stream_status (GstPad * pad, GstStreamStatusType type,
4702 GThread * thread, GstTask * task)
4706 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4708 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4709 if (GST_IS_ELEMENT (parent)) {
4710 GstMessage *message;
4711 GValue value = { 0 };
4713 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4714 gchar *tname, *ename, *pname;
4716 /* create a good task name */
4717 ename = gst_element_get_name (parent);
4718 pname = gst_pad_get_name (pad);
4719 tname = g_strdup_printf ("%s:%s", ename, pname);
4723 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4727 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4730 g_value_init (&value, GST_TYPE_TASK);
4731 g_value_set_object (&value, task);
4732 gst_message_set_stream_status_object (message, &value);
4733 g_value_unset (&value);
4735 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4736 gst_element_post_message (parent, message);
4738 gst_object_unref (parent);
4743 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4745 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4750 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4752 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4756 static GstTaskThreadCallbacks thr_callbacks = {
4762 * gst_pad_start_task:
4763 * @pad: the #GstPad to start the task of
4764 * @func: the task function to call
4765 * @data: data passed to the task function
4767 * Starts a task that repeatedly calls @func with @data. This function
4768 * is mostly used in pad activation functions to start the dataflow.
4769 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4770 * before @func is called.
4772 * Returns: a %TRUE if the task could be started.
4775 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4780 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4781 g_return_val_if_fail (func != NULL, FALSE);
4783 GST_DEBUG_OBJECT (pad, "start task");
4785 GST_OBJECT_LOCK (pad);
4786 task = GST_PAD_TASK (pad);
4788 task = gst_task_create (func, data);
4789 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4790 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4791 GST_DEBUG_OBJECT (pad, "created task");
4792 GST_PAD_TASK (pad) = task;
4793 gst_object_ref (task);
4794 /* release lock to post the message */
4795 GST_OBJECT_UNLOCK (pad);
4797 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4799 gst_object_unref (task);
4801 GST_OBJECT_LOCK (pad);
4802 /* nobody else is supposed to have changed the pad now */
4803 if (GST_PAD_TASK (pad) != task)
4804 goto concurrent_stop;
4806 res = gst_task_set_state (task, GST_TASK_STARTED);
4807 GST_OBJECT_UNLOCK (pad);
4814 GST_OBJECT_UNLOCK (pad);
4820 * gst_pad_pause_task:
4821 * @pad: the #GstPad to pause the task of
4823 * Pause the task of @pad. This function will also wait until the
4824 * function executed by the task is finished if this function is not
4825 * called from the task function.
4827 * Returns: a TRUE if the task could be paused or FALSE when the pad
4831 gst_pad_pause_task (GstPad * pad)
4836 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4838 GST_DEBUG_OBJECT (pad, "pause task");
4840 GST_OBJECT_LOCK (pad);
4841 task = GST_PAD_TASK (pad);
4844 res = gst_task_set_state (task, GST_TASK_PAUSED);
4845 GST_OBJECT_UNLOCK (pad);
4847 /* wait for task function to finish, this lock is recursive so it does nothing
4848 * when the pause is called from the task itself */
4849 GST_PAD_STREAM_LOCK (pad);
4850 GST_PAD_STREAM_UNLOCK (pad);
4856 GST_DEBUG_OBJECT (pad, "pad has no task");
4857 GST_OBJECT_UNLOCK (pad);
4863 * gst_pad_stop_task:
4864 * @pad: the #GstPad to stop the task of
4866 * Stop the task of @pad. This function will also make sure that the
4867 * function executed by the task will effectively stop if not called
4868 * from the GstTaskFunction.
4870 * This function will deadlock if called from the GstTaskFunction of
4871 * the task. Use gst_task_pause() instead.
4873 * Regardless of whether the pad has a task, the stream lock is acquired and
4874 * released so as to ensure that streaming through this pad has finished.
4876 * Returns: a TRUE if the task could be stopped or FALSE on error.
4879 gst_pad_stop_task (GstPad * pad)
4884 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4886 GST_DEBUG_OBJECT (pad, "stop task");
4888 GST_OBJECT_LOCK (pad);
4889 task = GST_PAD_TASK (pad);
4892 GST_PAD_TASK (pad) = NULL;
4893 res = gst_task_set_state (task, GST_TASK_STOPPED);
4894 GST_OBJECT_UNLOCK (pad);
4896 GST_PAD_STREAM_LOCK (pad);
4897 GST_PAD_STREAM_UNLOCK (pad);
4899 if (!gst_task_join (task))
4902 gst_object_unref (task);
4908 GST_DEBUG_OBJECT (pad, "no task");
4909 GST_OBJECT_UNLOCK (pad);
4911 GST_PAD_STREAM_LOCK (pad);
4912 GST_PAD_STREAM_UNLOCK (pad);
4914 /* this is not an error */
4919 /* this is bad, possibly the application tried to join the task from
4920 * the task's thread. We install the task again so that it will be stopped
4921 * again from the right thread next time hopefully. */
4922 GST_OBJECT_LOCK (pad);
4923 GST_DEBUG_OBJECT (pad, "join failed");
4924 /* we can only install this task if there was no other task */
4925 if (GST_PAD_TASK (pad) == NULL)
4926 GST_PAD_TASK (pad) = task;
4927 GST_OBJECT_UNLOCK (pad);