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_EOS, "eos", 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_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, srcpad,
393 "Putting event %p (%s) on pad %s:%s", event,
394 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (sinkpad));
395 gst_event_replace (&sinkev->pending, event);
404 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
409 /* make sure we push the events from the source to this new peer, for this we
410 * copy the events on the sinkpad and mark EVENTS_PENDING */
412 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
413 pending |= replace_event (srcpad, sinkpad, i);
415 /* we had some new pending events, set our flag */
417 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
420 /* should be called with the OBJECT_LOCK */
422 get_pad_caps (GstPad * pad)
424 GstCaps *caps = NULL;
428 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
429 /* we can only use the caps when we have successfully send the caps
430 * event to the event function and is thus in the active entry */
431 if ((event = pad->priv->events[idx].event))
432 gst_event_parse_caps (event, &caps);
438 gst_pad_dispose (GObject * object)
440 GstPad *pad = GST_PAD_CAST (object);
443 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
445 /* unlink the peer pad */
446 if ((peer = gst_pad_get_peer (pad))) {
447 /* window for MT unsafeness, someone else could unlink here
448 * and then we call unlink with wrong pads. The unlink
449 * function would catch this and safely return failed. */
450 if (GST_PAD_IS_SRC (pad))
451 gst_pad_unlink (pad, peer);
453 gst_pad_unlink (peer, pad);
455 gst_object_unref (peer);
458 gst_pad_set_pad_template (pad, NULL);
460 clear_events (pad->priv->events);
462 g_hook_list_clear (&pad->probes);
464 G_OBJECT_CLASS (parent_class)->dispose (object);
468 gst_pad_finalize (GObject * object)
470 GstPad *pad = GST_PAD_CAST (object);
473 /* in case the task is still around, clean it up */
474 if ((task = GST_PAD_TASK (pad))) {
475 gst_task_join (task);
476 GST_PAD_TASK (pad) = NULL;
477 gst_object_unref (task);
480 g_static_rec_mutex_free (&pad->stream_rec_lock);
481 g_cond_free (pad->block_cond);
483 G_OBJECT_CLASS (parent_class)->finalize (object);
487 gst_pad_set_property (GObject * object, guint prop_id,
488 const GValue * value, GParamSpec * pspec)
490 g_return_if_fail (GST_IS_PAD (object));
493 case PAD_PROP_DIRECTION:
494 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
496 case PAD_PROP_TEMPLATE:
497 gst_pad_set_pad_template (GST_PAD_CAST (object),
498 (GstPadTemplate *) g_value_get_object (value));
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 gst_pad_get_property (GObject * object, guint prop_id,
508 GValue * value, GParamSpec * pspec)
510 g_return_if_fail (GST_IS_PAD (object));
514 GST_OBJECT_LOCK (object);
515 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
516 GST_OBJECT_UNLOCK (object);
518 case PAD_PROP_DIRECTION:
519 g_value_set_enum (value, GST_PAD_DIRECTION (object));
521 case PAD_PROP_TEMPLATE:
522 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 * @name: the name of the new pad.
533 * @direction: the #GstPadDirection of the pad.
535 * Creates a new pad with the given name in the given direction.
536 * If name is NULL, a guaranteed unique name (across all pads)
538 * This function makes a copy of the name so you can safely free the name.
540 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
545 gst_pad_new (const gchar * name, GstPadDirection direction)
547 return g_object_new (GST_TYPE_PAD,
548 "name", name, "direction", direction, NULL);
552 * gst_pad_new_from_template:
553 * @templ: the pad template to use
554 * @name: the name of the element
556 * Creates a new pad with the given name from the given template.
557 * If name is NULL, a guaranteed unique name (across all pads)
559 * This function makes a copy of the name so you can safely free the name.
561 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
564 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
566 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
568 return g_object_new (GST_TYPE_PAD,
569 "name", name, "direction", templ->direction, "template", templ, NULL);
573 * gst_pad_new_from_static_template:
574 * @templ: the #GstStaticPadTemplate to use
575 * @name: the name of the element
577 * Creates a new pad with the given name from the given static template.
578 * If name is NULL, a guaranteed unique name (across all pads)
580 * This function makes a copy of the name so you can safely free the name.
582 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
585 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
589 GstPadTemplate *template;
591 template = gst_static_pad_template_get (templ);
592 pad = gst_pad_new_from_template (template, name);
593 gst_object_unref (template);
598 * gst_pad_get_direction:
599 * @pad: a #GstPad to get the direction of.
601 * Gets the direction of the pad. The direction of the pad is
602 * decided at construction time so this function does not take
605 * Returns: the #GstPadDirection of the pad.
610 gst_pad_get_direction (GstPad * pad)
612 GstPadDirection result;
614 /* PAD_UNKNOWN is a little silly but we need some sort of
615 * error return value */
616 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
618 result = GST_PAD_DIRECTION (pad);
624 gst_pad_activate_default (GstPad * pad)
626 return gst_pad_activate_push (pad, TRUE);
630 pre_activate (GstPad * pad, GstActivateMode new_mode)
633 case GST_ACTIVATE_PUSH:
634 case GST_ACTIVATE_PULL:
635 GST_OBJECT_LOCK (pad);
636 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
638 GST_PAD_UNSET_FLUSHING (pad);
639 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
640 GST_OBJECT_UNLOCK (pad);
642 case GST_ACTIVATE_NONE:
643 GST_OBJECT_LOCK (pad);
644 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
645 GST_PAD_SET_FLUSHING (pad);
646 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
647 /* unlock blocked pads so element can resume and stop */
648 GST_PAD_BLOCK_BROADCAST (pad);
649 GST_OBJECT_UNLOCK (pad);
655 post_activate (GstPad * pad, GstActivateMode new_mode)
658 case GST_ACTIVATE_PUSH:
659 case GST_ACTIVATE_PULL:
662 case GST_ACTIVATE_NONE:
663 /* ensures that streaming stops */
664 GST_PAD_STREAM_LOCK (pad);
665 GST_DEBUG_OBJECT (pad, "stopped streaming");
666 GST_OBJECT_LOCK (pad);
667 clear_events (pad->priv->events);
668 GST_OBJECT_UNLOCK (pad);
669 GST_PAD_STREAM_UNLOCK (pad);
675 * gst_pad_set_active:
676 * @pad: the #GstPad to activate or deactivate.
677 * @active: whether or not the pad should be active.
679 * Activates or deactivates the given pad.
680 * Normally called from within core state change functions.
682 * If @active, makes sure the pad is active. If it is already active, either in
683 * push or pull mode, just return. Otherwise dispatches to the pad's activate
684 * function to perform the actual activation.
686 * If not @active, checks the pad's current mode and calls
687 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
690 * Returns: #TRUE if the operation was successful.
695 gst_pad_set_active (GstPad * pad, gboolean active)
698 gboolean ret = FALSE;
700 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
702 GST_OBJECT_LOCK (pad);
703 old = GST_PAD_ACTIVATE_MODE (pad);
704 GST_OBJECT_UNLOCK (pad);
708 case GST_ACTIVATE_PUSH:
709 GST_DEBUG_OBJECT (pad, "activating pad from push");
712 case GST_ACTIVATE_PULL:
713 GST_DEBUG_OBJECT (pad, "activating pad from pull");
716 case GST_ACTIVATE_NONE:
717 GST_DEBUG_OBJECT (pad, "activating pad from none");
718 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
721 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
726 case GST_ACTIVATE_PUSH:
727 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
728 ret = gst_pad_activate_push (pad, FALSE);
730 case GST_ACTIVATE_PULL:
731 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
732 ret = gst_pad_activate_pull (pad, FALSE);
734 case GST_ACTIVATE_NONE:
735 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
739 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
745 GST_OBJECT_LOCK (pad);
747 g_critical ("Failed to deactivate pad %s:%s, very bad",
748 GST_DEBUG_PAD_NAME (pad));
750 GST_WARNING_OBJECT (pad, "Failed to activate pad");
752 GST_OBJECT_UNLOCK (pad);
755 GST_OBJECT_LOCK (pad);
756 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
757 GST_OBJECT_UNLOCK (pad);
765 * gst_pad_activate_pull:
766 * @pad: the #GstPad to activate or deactivate.
767 * @active: whether or not the pad should be active.
769 * Activates or deactivates the given pad in pull mode via dispatching to the
770 * pad's activatepullfunc. For use from within pad activation functions only.
771 * When called on sink pads, will first proxy the call to the peer pad, which
772 * is expected to activate its internally linked pads from within its
773 * activate_pull function.
775 * If you don't know what this is, you probably don't want to call it.
777 * Returns: TRUE if the operation was successful.
782 gst_pad_activate_pull (GstPad * pad, gboolean active)
784 GstActivateMode old, new;
787 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
789 GST_OBJECT_LOCK (pad);
790 old = GST_PAD_ACTIVATE_MODE (pad);
791 GST_OBJECT_UNLOCK (pad);
795 case GST_ACTIVATE_PULL:
796 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
798 case GST_ACTIVATE_PUSH:
799 GST_DEBUG_OBJECT (pad,
800 "activating pad from push, deactivate push first");
801 /* pad was activate in the wrong direction, deactivate it
802 * and reactivate it in pull mode */
803 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
804 goto deactivate_failed;
805 /* fallthrough, pad is deactivated now. */
806 case GST_ACTIVATE_NONE:
807 GST_DEBUG_OBJECT (pad, "activating pad from none");
812 case GST_ACTIVATE_NONE:
813 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
815 case GST_ACTIVATE_PUSH:
816 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
817 /* pad was activated in the other direction, deactivate it
818 * in push mode, this should not happen... */
819 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
820 goto deactivate_failed;
821 /* everything is fine now */
823 case GST_ACTIVATE_PULL:
824 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
829 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
830 if ((peer = gst_pad_get_peer (pad))) {
831 GST_DEBUG_OBJECT (pad, "calling peer");
832 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
834 gst_object_unref (peer);
836 /* there is no peer, this is only fatal when we activate. When we
837 * deactivate, we must assume the application has unlinked the peer and
838 * will deactivate it eventually. */
842 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
845 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
846 goto failure; /* Can't activate pull on a src without a
850 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
851 pre_activate (pad, new);
853 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
854 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
857 /* can happen for sinks of passthrough elements */
860 post_activate (pad, new);
862 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
863 active ? "activated" : "deactivated");
869 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
870 active ? "activated" : "deactivated");
875 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
876 "failed to %s in switch to pull from mode %d",
877 (active ? "activate" : "deactivate"), old);
882 GST_OBJECT_LOCK (peer);
883 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
884 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
885 GST_OBJECT_UNLOCK (peer);
886 gst_object_unref (peer);
891 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
897 GST_OBJECT_LOCK (pad);
898 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
899 active ? "activate" : "deactivate");
900 GST_PAD_SET_FLUSHING (pad);
901 GST_PAD_ACTIVATE_MODE (pad) = old;
902 GST_OBJECT_UNLOCK (pad);
908 * gst_pad_activate_push:
909 * @pad: the #GstPad to activate or deactivate.
910 * @active: whether the pad should be active or not.
912 * Activates or deactivates the given pad in push mode via dispatching to the
913 * pad's activatepushfunc. For use from within pad activation functions only.
915 * If you don't know what this is, you probably don't want to call it.
917 * Returns: %TRUE if the operation was successful.
922 gst_pad_activate_push (GstPad * pad, gboolean active)
924 GstActivateMode old, new;
926 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
927 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
928 active ? "activated" : "deactivated");
930 GST_OBJECT_LOCK (pad);
931 old = GST_PAD_ACTIVATE_MODE (pad);
932 GST_OBJECT_UNLOCK (pad);
936 case GST_ACTIVATE_PUSH:
937 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
939 case GST_ACTIVATE_PULL:
940 GST_DEBUG_OBJECT (pad,
941 "activating pad from push, deactivating pull first");
942 /* pad was activate in the wrong direction, deactivate it
943 * an reactivate it in push mode */
944 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
945 goto deactivate_failed;
946 /* fallthrough, pad is deactivated now. */
947 case GST_ACTIVATE_NONE:
948 GST_DEBUG_OBJECT (pad, "activating pad from none");
953 case GST_ACTIVATE_NONE:
954 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
956 case GST_ACTIVATE_PULL:
957 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
958 /* pad was activated in the other direction, deactivate it
959 * in pull mode, this should not happen... */
960 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
961 goto deactivate_failed;
962 /* everything is fine now */
964 case GST_ACTIVATE_PUSH:
965 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
970 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
971 pre_activate (pad, new);
973 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
974 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
978 /* quite ok, element relies on state change func to prepare itself */
981 post_activate (pad, new);
983 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
984 active ? "activated" : "deactivated");
989 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
990 active ? "activated" : "deactivated");
995 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
996 "failed to %s in switch to push from mode %d",
997 (active ? "activate" : "deactivate"), old);
1002 GST_OBJECT_LOCK (pad);
1003 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1004 active ? "activate" : "deactivate");
1005 GST_PAD_SET_FLUSHING (pad);
1006 GST_PAD_ACTIVATE_MODE (pad) = old;
1007 GST_OBJECT_UNLOCK (pad);
1013 * gst_pad_is_active:
1014 * @pad: the #GstPad to query
1016 * Query if a pad is active
1018 * Returns: TRUE if the pad is active.
1023 gst_pad_is_active (GstPad * pad)
1025 gboolean result = FALSE;
1027 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1029 GST_OBJECT_LOCK (pad);
1030 result = GST_PAD_IS_ACTIVE (pad);
1031 GST_OBJECT_UNLOCK (pad);
1037 * gst_pad_add_probe:
1038 * @pad: the #GstPad to add the probe to
1039 * @mask: the probe mask
1040 * @callback: #GstPadProbeCallback that will be called with notifications of
1042 * @user_data: (closure): user data passed to the callback
1043 * @destroy_data: #GDestroyNotify for user_data
1045 * Be notified of different states of pads. The provided callback is called for
1046 * every state that matches @mask.
1049 * Pad probe handlers are only called for source pads in push mode
1050 * and sink pads in pull mode.
1053 * Returns: an id or 0 on error. The id can be used to remove the probe with
1054 * gst_pad_remove_probe().
1059 gst_pad_add_probe (GstPad * pad, GstProbeType mask,
1060 GstPadProbeCallback callback, gpointer user_data,
1061 GDestroyNotify destroy_data)
1066 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1067 g_return_val_if_fail (mask != 0, 0);
1069 GST_OBJECT_LOCK (pad);
1070 /* make a new probe */
1071 hook = g_hook_alloc (&pad->probes);
1073 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1076 /* when no contraints are given for the types, assume all types are
1078 if ((mask & GST_PROBE_TYPE_DATA) == 0)
1079 mask |= GST_PROBE_TYPE_DATA;
1080 if ((mask & GST_PROBE_TYPE_SCHEDULING) == 0)
1081 mask |= GST_PROBE_TYPE_SCHEDULING;
1083 /* store our flags and other fields */
1084 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1085 hook->func = callback;
1086 hook->data = user_data;
1087 hook->destroy = destroy_data;
1088 PROBE_COOKIE (hook) = 0;
1090 /* incremenent cookie so that the new hook get's called */
1091 pad->priv->probe_cookie++;
1094 g_hook_prepend (&pad->probes, hook);
1097 /* get the id of the hook, we return this and it can be used to remove the
1099 res = hook->hook_id;
1101 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1103 if (mask & GST_PROBE_TYPE_BLOCKING) {
1104 /* we have a block probe */
1106 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1107 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1108 "now %d blocking probes", pad->num_blocked);
1111 /* call the callback if we need to be called for idle callbacks */
1112 if ((mask & GST_PROBE_TYPE_IDLE) && (callback != NULL)) {
1113 if (pad->priv->using > 0) {
1114 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1115 * flag above, the last thread to leave the push will do the callback. New
1116 * threads going into the push will block. */
1117 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1118 "pad is in use, delay idle callback");
1119 GST_OBJECT_UNLOCK (pad);
1121 /* the pad is idle now, we can signal the idle callback now */
1122 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1123 "pad is idle, trigger idle callback");
1124 GST_OBJECT_UNLOCK (pad);
1126 callback (pad, GST_PROBE_TYPE_IDLE, NULL, user_data);
1129 GST_OBJECT_UNLOCK (pad);
1135 cleanup_hook (GstPad * pad, GHook * hook)
1139 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1141 if (type & GST_PROBE_TYPE_BLOCKING) {
1142 /* unblock when we remove the last blocking probe */
1144 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1146 if (pad->num_blocked == 0) {
1147 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1148 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1149 GST_PAD_BLOCK_BROADCAST (pad);
1152 g_hook_destroy_link (&pad->probes, hook);
1157 * gst_pad_remove_probe:
1158 * @pad: the #GstPad with the probe
1159 * @id: the probe id to remove
1161 * Remove the probe with @id from @pad.
1166 gst_pad_remove_probe (GstPad * pad, gulong id)
1170 g_return_if_fail (GST_IS_PAD (pad));
1172 GST_OBJECT_LOCK (pad);
1174 hook = g_hook_get (&pad->probes, id);
1178 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1180 cleanup_hook (pad, hook);
1181 GST_OBJECT_UNLOCK (pad);
1187 GST_OBJECT_UNLOCK (pad);
1188 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1194 * gst_pad_is_blocked:
1195 * @pad: the #GstPad to query
1197 * Checks if the pad is blocked or not. This function returns the
1198 * last requested state of the pad. It is not certain that the pad
1199 * is actually blocking at this point (see gst_pad_is_blocking()).
1201 * Returns: TRUE if the pad is blocked.
1206 gst_pad_is_blocked (GstPad * pad)
1208 gboolean result = FALSE;
1210 g_return_val_if_fail (GST_IS_PAD (pad), result);
1212 GST_OBJECT_LOCK (pad);
1213 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1214 GST_OBJECT_UNLOCK (pad);
1220 * gst_pad_is_blocking:
1221 * @pad: the #GstPad to query
1223 * Checks if the pad is blocking or not. This is a guaranteed state
1224 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1226 * Returns: TRUE if the pad is blocking.
1233 gst_pad_is_blocking (GstPad * pad)
1235 gboolean result = FALSE;
1237 g_return_val_if_fail (GST_IS_PAD (pad), result);
1239 GST_OBJECT_LOCK (pad);
1240 /* the blocking flag is only valid if the pad is not flushing */
1241 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1242 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1243 GST_OBJECT_UNLOCK (pad);
1249 * gst_pad_check_reconfigure:
1250 * @pad: the #GstPad to check
1252 * Check and clear the #GST_PAD_NEED_RECONFIGURE flag on @pad and return %TRUE
1253 * if the flag was set.
1255 * Returns: %TRUE is the GST_PAD_NEED_RECONFIGURE flag was set on @pad.
1258 gst_pad_check_reconfigure (GstPad * pad)
1260 gboolean reconfigure;
1262 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1264 GST_OBJECT_LOCK (pad);
1265 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1266 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
1267 GST_OBJECT_UNLOCK (pad);
1273 * gst_pad_mark_reconfigure:
1274 * @pad: the #GstPad to mark
1276 * Mark a pad for needing reconfiguration. The next call to
1277 * gst_pad_check_reconfigure() will return %TRUE after this call.
1280 gst_pad_mark_reconfigure (GstPad * pad)
1282 g_return_if_fail (GST_IS_PAD (pad));
1284 GST_OBJECT_LOCK (pad);
1285 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
1286 GST_OBJECT_UNLOCK (pad);
1290 * gst_pad_set_activate_function:
1292 * @activate: the #GstPadActivateFunction to set.
1294 * Sets the given activate function for @pad. The activate function will
1295 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1296 * the actual activation. Only makes sense to set on sink pads.
1298 * Call this function if your sink pad can start a pull-based task.
1301 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1303 g_return_if_fail (GST_IS_PAD (pad));
1305 GST_PAD_ACTIVATEFUNC (pad) = activate;
1306 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1307 GST_DEBUG_FUNCPTR_NAME (activate));
1311 * gst_pad_set_activatepull_function:
1313 * @activatepull: the #GstPadActivateModeFunction to set.
1315 * Sets the given activate_pull function for the pad. An activate_pull function
1316 * prepares the element and any upstream connections for pulling. See XXX
1317 * part-activation.txt for details.
1320 gst_pad_set_activatepull_function (GstPad * pad,
1321 GstPadActivateModeFunction activatepull)
1323 g_return_if_fail (GST_IS_PAD (pad));
1325 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1326 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1327 GST_DEBUG_FUNCPTR_NAME (activatepull));
1331 * gst_pad_set_activatepush_function:
1333 * @activatepush: the #GstPadActivateModeFunction to set.
1335 * Sets the given activate_push function for the pad. An activate_push function
1336 * prepares the element for pushing. See XXX part-activation.txt for details.
1339 gst_pad_set_activatepush_function (GstPad * pad,
1340 GstPadActivateModeFunction activatepush)
1342 g_return_if_fail (GST_IS_PAD (pad));
1344 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1345 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1346 GST_DEBUG_FUNCPTR_NAME (activatepush));
1350 * gst_pad_set_chain_function:
1351 * @pad: a sink #GstPad.
1352 * @chain: the #GstPadChainFunction to set.
1354 * Sets the given chain function for the pad. The chain function is called to
1355 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1358 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1360 g_return_if_fail (GST_IS_PAD (pad));
1361 g_return_if_fail (GST_PAD_IS_SINK (pad));
1363 GST_PAD_CHAINFUNC (pad) = chain;
1364 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1365 GST_DEBUG_FUNCPTR_NAME (chain));
1369 * gst_pad_set_chain_list_function:
1370 * @pad: a sink #GstPad.
1371 * @chainlist: the #GstPadChainListFunction to set.
1373 * Sets the given chain list function for the pad. The chainlist function is
1374 * called to process a #GstBufferList input buffer list. See
1375 * #GstPadChainListFunction for more details.
1380 gst_pad_set_chain_list_function (GstPad * pad,
1381 GstPadChainListFunction chainlist)
1383 g_return_if_fail (GST_IS_PAD (pad));
1384 g_return_if_fail (GST_PAD_IS_SINK (pad));
1386 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1387 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1388 GST_DEBUG_FUNCPTR_NAME (chainlist));
1392 * gst_pad_set_getrange_function:
1393 * @pad: a source #GstPad.
1394 * @get: the #GstPadGetRangeFunction to set.
1396 * Sets the given getrange function for the pad. The getrange function is
1397 * called to produce a new #GstBuffer to start the processing pipeline. see
1398 * #GstPadGetRangeFunction for a description of the getrange function.
1401 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1403 g_return_if_fail (GST_IS_PAD (pad));
1404 g_return_if_fail (GST_PAD_IS_SRC (pad));
1406 GST_PAD_GETRANGEFUNC (pad) = get;
1408 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1409 GST_DEBUG_FUNCPTR_NAME (get));
1413 * gst_pad_set_event_function:
1414 * @pad: a #GstPad of either direction.
1415 * @event: the #GstPadEventFunction to set.
1417 * Sets the given event handler for the pad.
1420 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1422 g_return_if_fail (GST_IS_PAD (pad));
1424 GST_PAD_EVENTFUNC (pad) = event;
1426 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1427 GST_DEBUG_FUNCPTR_NAME (event));
1431 * gst_pad_set_query_function:
1432 * @pad: a #GstPad of either direction.
1433 * @query: the #GstPadQueryFunction to set.
1435 * Set the given query function for the pad.
1438 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1440 g_return_if_fail (GST_IS_PAD (pad));
1442 GST_PAD_QUERYFUNC (pad) = query;
1444 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1445 GST_DEBUG_FUNCPTR_NAME (query));
1449 * gst_pad_set_query_type_function:
1450 * @pad: a #GstPad of either direction.
1451 * @type_func: the #GstPadQueryTypeFunction to set.
1453 * Set the given query type function for the pad.
1456 gst_pad_set_query_type_function (GstPad * pad,
1457 GstPadQueryTypeFunction type_func)
1459 g_return_if_fail (GST_IS_PAD (pad));
1461 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1463 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1464 GST_DEBUG_FUNCPTR_NAME (type_func));
1468 * gst_pad_get_query_types:
1471 * Get an array of supported queries that can be performed
1474 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1477 const GstQueryType *
1478 gst_pad_get_query_types (GstPad * pad)
1480 GstPadQueryTypeFunction func;
1482 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1484 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1496 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1500 if ((peer = gst_pad_get_peer (pad))) {
1501 *data = gst_pad_get_query_types (peer);
1502 gst_object_unref (peer);
1508 * gst_pad_get_query_types_default:
1511 * Invoke the default query types function on the pad. This function will get
1512 * the supported query type from the peer of an internally linked pad of @pad.
1514 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1515 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1516 * query types function.
1518 const GstQueryType *
1519 gst_pad_get_query_types_default (GstPad * pad)
1521 GstQueryType *result = NULL;
1523 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1525 gst_pad_forward (pad, (GstPadForwardFunction)
1526 gst_pad_get_query_types_dispatcher, &result);
1532 * gst_pad_set_iterate_internal_links_function:
1533 * @pad: a #GstPad of either direction.
1534 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1536 * Sets the given internal link iterator function for the pad.
1541 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1542 GstPadIterIntLinkFunction iterintlink)
1544 g_return_if_fail (GST_IS_PAD (pad));
1546 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1547 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1548 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1552 * gst_pad_set_link_function:
1554 * @link: the #GstPadLinkFunction to set.
1556 * Sets the given link function for the pad. It will be called when
1557 * the pad is linked with another pad.
1559 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1562 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1563 * cannot be made for some reason.
1565 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1566 * of the peer sink pad, if present.
1569 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1571 g_return_if_fail (GST_IS_PAD (pad));
1573 GST_PAD_LINKFUNC (pad) = link;
1574 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1575 GST_DEBUG_FUNCPTR_NAME (link));
1579 * gst_pad_set_unlink_function:
1581 * @unlink: the #GstPadUnlinkFunction to set.
1583 * Sets the given unlink function for the pad. It will be called
1584 * when the pad is unlinked.
1587 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1589 g_return_if_fail (GST_IS_PAD (pad));
1591 GST_PAD_UNLINKFUNC (pad) = unlink;
1592 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1593 GST_DEBUG_FUNCPTR_NAME (unlink));
1597 * gst_pad_set_getcaps_function:
1599 * @getcaps: the #GstPadGetCapsFunction to set.
1601 * Sets the given getcaps function for the pad. @getcaps should return the
1602 * allowable caps for a pad in the context of the element's state, its link to
1603 * other elements, and the devices or files it has opened. These caps must be a
1604 * subset of the pad template caps. In the NULL state with no links, @getcaps
1605 * should ideally return the same caps as the pad template. In rare
1606 * circumstances, an object property can affect the caps returned by @getcaps,
1607 * but this is discouraged.
1609 * You do not need to call this function if @pad's allowed caps are always the
1610 * same as the pad template caps. This can only be true if the padtemplate
1611 * has fixed simple caps.
1613 * For most filters, the caps returned by @getcaps is directly affected by the
1614 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1615 * the srcpad's getcaps function is directly related to the stream data. Again,
1616 * @getcaps should return the most specific caps it reasonably can, since this
1617 * helps with autoplugging.
1619 * Note that the return value from @getcaps is owned by the caller, so the
1620 * caller should unref the caps after usage.
1623 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1625 g_return_if_fail (GST_IS_PAD (pad));
1627 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1628 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1629 GST_DEBUG_FUNCPTR_NAME (getcaps));
1633 * gst_pad_set_acceptcaps_function:
1635 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1637 * Sets the given acceptcaps function for the pad. The acceptcaps function
1638 * will be called to check if the pad can accept the given caps. Setting the
1639 * acceptcaps function to NULL restores the default behaviour of allowing
1640 * any caps that matches the caps from gst_pad_get_caps().
1643 gst_pad_set_acceptcaps_function (GstPad * pad,
1644 GstPadAcceptCapsFunction acceptcaps)
1646 g_return_if_fail (GST_IS_PAD (pad));
1648 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1649 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1650 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1654 * gst_pad_set_fixatecaps_function:
1656 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1658 * Sets the given fixatecaps function for the pad. The fixatecaps function
1659 * will be called whenever the default values for a GstCaps needs to be
1663 gst_pad_set_fixatecaps_function (GstPad * pad,
1664 GstPadFixateCapsFunction fixatecaps)
1666 g_return_if_fail (GST_IS_PAD (pad));
1668 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1669 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1670 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1675 * @srcpad: the source #GstPad to unlink.
1676 * @sinkpad: the sink #GstPad to unlink.
1678 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1679 * signal on both pads.
1681 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1682 * the pads were not linked together.
1687 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1689 gboolean result = FALSE;
1690 GstElement *parent = NULL;
1693 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1694 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1695 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1696 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1698 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1699 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1700 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1702 /* We need to notify the parent before taking any pad locks as the bin in
1703 * question might be waiting for a lock on the pad while holding its lock
1704 * that our message will try to take. */
1705 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1706 if (GST_IS_ELEMENT (parent)) {
1707 gst_element_post_message (parent,
1708 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1709 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1711 gst_object_unref (parent);
1716 GST_OBJECT_LOCK (srcpad);
1717 GST_OBJECT_LOCK (sinkpad);
1719 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1720 goto not_linked_together;
1722 if (GST_PAD_UNLINKFUNC (srcpad)) {
1723 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1725 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1726 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1729 /* first clear peers */
1730 GST_PAD_PEER (srcpad) = NULL;
1731 GST_PAD_PEER (sinkpad) = NULL;
1733 /* clear pending caps if any */
1734 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1735 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1737 GST_OBJECT_UNLOCK (sinkpad);
1738 GST_OBJECT_UNLOCK (srcpad);
1740 /* fire off a signal to each of the pads telling them
1741 * that they've been unlinked */
1742 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1743 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1745 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1746 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1751 if (parent != NULL) {
1752 gst_element_post_message (parent,
1753 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1754 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1755 gst_object_unref (parent);
1760 not_linked_together:
1762 /* we do not emit a warning in this case because unlinking cannot
1763 * be made MT safe.*/
1764 GST_OBJECT_UNLOCK (sinkpad);
1765 GST_OBJECT_UNLOCK (srcpad);
1771 * gst_pad_is_linked:
1772 * @pad: pad to check
1774 * Checks if a @pad is linked to another pad or not.
1776 * Returns: TRUE if the pad is linked, FALSE otherwise.
1781 gst_pad_is_linked (GstPad * pad)
1785 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1787 GST_OBJECT_LOCK (pad);
1788 result = (GST_PAD_PEER (pad) != NULL);
1789 GST_OBJECT_UNLOCK (pad);
1794 /* get the caps from both pads and see if the intersection
1797 * This function should be called with the pad LOCK on both
1801 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1802 GstPadLinkCheck flags)
1804 GstCaps *srccaps = NULL;
1805 GstCaps *sinkcaps = NULL;
1806 gboolean compatible = FALSE;
1808 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1811 /* Doing the expensive caps checking takes priority over only checking the template caps */
1812 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1813 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1814 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1816 /* If one of the two pads doesn't have a template, consider the intersection
1818 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1819 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1823 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1825 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1828 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1830 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1833 /* if we have caps on both pads we can check the intersection. If one
1834 * of the caps is NULL, we return TRUE. */
1835 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1837 gst_caps_unref (srccaps);
1839 gst_caps_unref (sinkcaps);
1843 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1844 gst_caps_unref (srccaps);
1845 gst_caps_unref (sinkcaps);
1848 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1849 (compatible ? "" : "not"));
1854 /* check if the grandparents of both pads are the same.
1855 * This check is required so that we don't try to link
1856 * pads from elements in different bins without ghostpads.
1858 * The LOCK should be held on both pads
1861 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1863 GstObject *psrc, *psink;
1865 psrc = GST_OBJECT_PARENT (src);
1866 psink = GST_OBJECT_PARENT (sink);
1868 /* if one of the pads has no parent, we allow the link */
1869 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1872 /* only care about parents that are elements */
1873 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1874 goto no_element_parent;
1876 /* if the parents are the same, we have a loop */
1877 if (G_UNLIKELY (psrc == psink))
1880 /* if they both have a parent, we check the grandparents. We can not lock
1881 * the parent because we hold on the child (pad) and the locking order is
1882 * parent >> child. */
1883 psrc = GST_OBJECT_PARENT (psrc);
1884 psink = GST_OBJECT_PARENT (psink);
1886 /* if they have grandparents but they are not the same */
1887 if (G_UNLIKELY (psrc != psink))
1888 goto wrong_grandparents;
1895 GST_CAT_DEBUG (GST_CAT_CAPS,
1896 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1897 GST_PTR_FORMAT, psrc, psink);
1902 GST_CAT_DEBUG (GST_CAT_CAPS,
1903 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1904 GST_PTR_FORMAT, psrc, psink);
1909 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1915 GST_CAT_DEBUG (GST_CAT_CAPS,
1916 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1917 GST_PTR_FORMAT, psrc, psink);
1922 /* FIXME leftover from an attempt at refactoring... */
1923 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1924 * the two pads will be locked in the srcpad, sinkpad order. */
1925 static GstPadLinkReturn
1926 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1928 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1929 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1931 GST_OBJECT_LOCK (srcpad);
1933 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1934 goto src_was_linked;
1936 GST_OBJECT_LOCK (sinkpad);
1938 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1939 goto sink_was_linked;
1941 /* check hierarchy, pads can only be linked if the grandparents
1943 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1944 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1945 goto wrong_hierarchy;
1947 /* check pad caps for non-empty intersection */
1948 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1951 /* FIXME check pad scheduling for non-empty intersection */
1953 return GST_PAD_LINK_OK;
1957 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1958 GST_DEBUG_PAD_NAME (srcpad),
1959 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1960 /* we do not emit a warning in this case because unlinking cannot
1961 * be made MT safe.*/
1962 GST_OBJECT_UNLOCK (srcpad);
1963 return GST_PAD_LINK_WAS_LINKED;
1967 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1968 GST_DEBUG_PAD_NAME (sinkpad),
1969 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1970 /* we do not emit a warning in this case because unlinking cannot
1971 * be made MT safe.*/
1972 GST_OBJECT_UNLOCK (sinkpad);
1973 GST_OBJECT_UNLOCK (srcpad);
1974 return GST_PAD_LINK_WAS_LINKED;
1978 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1979 GST_OBJECT_UNLOCK (sinkpad);
1980 GST_OBJECT_UNLOCK (srcpad);
1981 return GST_PAD_LINK_WRONG_HIERARCHY;
1985 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1986 GST_OBJECT_UNLOCK (sinkpad);
1987 GST_OBJECT_UNLOCK (srcpad);
1988 return GST_PAD_LINK_NOFORMAT;
1994 * @srcpad: the source #GstPad.
1995 * @sinkpad: the sink #GstPad.
1997 * Checks if the source pad and the sink pad are compatible so they can be
2000 * Returns: TRUE if the pads can be linked.
2003 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2005 GstPadLinkReturn result;
2007 /* generic checks */
2008 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2009 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2011 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2012 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2014 /* gst_pad_link_prepare does everything for us, we only release the locks
2015 * on the pads that it gets us. If this function returns !OK the locks are not
2017 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2018 if (result != GST_PAD_LINK_OK)
2021 GST_OBJECT_UNLOCK (srcpad);
2022 GST_OBJECT_UNLOCK (sinkpad);
2025 return result == GST_PAD_LINK_OK;
2029 * gst_pad_link_full:
2030 * @srcpad: the source #GstPad to link.
2031 * @sinkpad: the sink #GstPad to link.
2032 * @flags: the checks to validate when linking
2034 * Links the source pad and the sink pad.
2036 * This variant of #gst_pad_link provides a more granular control on the
2037 * checks being done when linking. While providing some considerable speedups
2038 * the caller of this method must be aware that wrong usage of those flags
2039 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2040 * for more information.
2044 * Returns: A result code indicating if the connection worked or
2050 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2052 GstPadLinkReturn result;
2054 GstPadLinkFunction srcfunc, sinkfunc;
2056 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2057 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2058 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2059 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2060 GST_PAD_LINK_WRONG_DIRECTION);
2062 /* Notify the parent early. See gst_pad_unlink for details. */
2063 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2064 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2065 gst_element_post_message (parent,
2066 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2067 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2069 gst_object_unref (parent);
2074 /* prepare will also lock the two pads */
2075 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2077 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2080 /* must set peers before calling the link function */
2081 GST_PAD_PEER (srcpad) = sinkpad;
2082 GST_PAD_PEER (sinkpad) = srcpad;
2084 /* make sure we update events */
2085 prepare_event_update (srcpad, sinkpad);
2087 /* get the link functions */
2088 srcfunc = GST_PAD_LINKFUNC (srcpad);
2089 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2091 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2092 /* custom link functions, execute them */
2093 GST_OBJECT_UNLOCK (sinkpad);
2094 GST_OBJECT_UNLOCK (srcpad);
2097 /* this one will call the peer link function */
2098 result = srcfunc (srcpad, sinkpad);
2099 } else if (sinkfunc) {
2100 /* if no source link function, we need to call the sink link
2101 * function ourselves. */
2102 result = sinkfunc (sinkpad, srcpad);
2105 GST_OBJECT_LOCK (srcpad);
2106 GST_OBJECT_LOCK (sinkpad);
2108 /* we released the lock, check if the same pads are linked still */
2109 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2110 goto concurrent_link;
2112 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2115 GST_OBJECT_UNLOCK (sinkpad);
2116 GST_OBJECT_UNLOCK (srcpad);
2118 /* fire off a signal to each of the pads telling them
2119 * that they've been linked */
2120 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2121 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2123 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2124 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2126 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2129 if (G_LIKELY (parent)) {
2130 gst_element_post_message (parent,
2131 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2132 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2133 gst_object_unref (parent);
2141 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2142 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2143 GST_OBJECT_UNLOCK (sinkpad);
2144 GST_OBJECT_UNLOCK (srcpad);
2146 /* The other link operation succeeded first */
2147 result = GST_PAD_LINK_WAS_LINKED;
2152 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2153 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2155 GST_PAD_PEER (srcpad) = NULL;
2156 GST_PAD_PEER (sinkpad) = NULL;
2158 GST_OBJECT_UNLOCK (sinkpad);
2159 GST_OBJECT_UNLOCK (srcpad);
2167 * @srcpad: the source #GstPad to link.
2168 * @sinkpad: the sink #GstPad to link.
2170 * Links the source pad and the sink pad.
2172 * Returns: A result code indicating if the connection worked or
2178 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2180 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2184 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2186 GstPadTemplate **template_p;
2188 /* this function would need checks if it weren't static */
2190 GST_OBJECT_LOCK (pad);
2191 template_p = &pad->padtemplate;
2192 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2193 GST_OBJECT_UNLOCK (pad);
2196 gst_pad_template_pad_created (templ, pad);
2200 * gst_pad_get_pad_template:
2203 * Gets the template for @pad.
2205 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2206 * instantiated, or %NULL if this pad has no template. Unref after
2210 gst_pad_get_pad_template (GstPad * pad)
2212 GstPadTemplate *templ;
2214 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2216 templ = GST_PAD_PAD_TEMPLATE (pad);
2218 return (templ ? gst_object_ref (templ) : NULL);
2222 caps_with_getcaps (GstPad * pad, GstCaps * filter)
2226 if (GST_PAD_GETCAPSFUNC (pad) == NULL)
2229 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2230 "dispatching to pad getcaps function with "
2231 "filter %" GST_PTR_FORMAT, filter);
2233 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2234 GST_OBJECT_UNLOCK (pad);
2235 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2236 GST_OBJECT_LOCK (pad);
2237 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2239 if (G_UNLIKELY (result == NULL))
2242 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2243 "pad getcaps returned %" GST_PTR_FORMAT, result);
2245 #ifndef G_DISABLE_ASSERT
2246 /* check that the returned caps are a real subset of the template caps */
2247 if (GST_PAD_PAD_TEMPLATE (pad)) {
2248 const GstCaps *templ_caps =
2249 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2250 if (!gst_caps_is_subset (result, templ_caps)) {
2253 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2254 "pad returned caps %" GST_PTR_FORMAT
2255 " which are not a real subset of its template caps %"
2256 GST_PTR_FORMAT, result, templ_caps);
2258 ("pad %s:%s returned caps which are not a real "
2259 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2260 temp = gst_caps_intersect (templ_caps, result);
2261 gst_caps_unref (result);
2266 if (!gst_caps_is_subset (result, filter)) {
2269 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2270 "pad returned caps %" GST_PTR_FORMAT
2271 " which are not a real subset of the filter caps %"
2272 GST_PTR_FORMAT, result, filter);
2273 g_warning ("pad %s:%s returned caps which are not a real "
2274 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2275 /* FIXME: Order? But shouldn't happen anyway... */
2276 temp = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2277 gst_caps_unref (result);
2288 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2289 GST_DEBUG_PAD_NAME (pad));
2294 /* should be called with the pad LOCK held */
2295 /* refs the caps, so caller is responsible for getting it unreffed */
2297 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2299 GstCaps *result = NULL;
2300 GstPadTemplate *templ;
2301 gboolean fixed_caps;
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2305 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2308 /* fixed caps, try the negotiated caps first */
2309 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2310 if ((result = get_pad_caps (pad)))
2314 /* try the getcaps function next */
2315 if ((result = caps_with_getcaps (pad, filter)))
2318 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2319 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2320 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2325 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2326 "non-fixed pad caps: trying pad caps");
2327 /* non fixed caps, try the negotiated caps */
2328 if ((result = get_pad_caps (pad)))
2332 /* this almost never happens */
2333 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2334 result = gst_caps_new_empty ();
2338 /* run the filter on the result */
2340 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2341 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2342 GST_PTR_FORMAT, result, result, filter, filter);
2343 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2344 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2347 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2348 "using caps %p %" GST_PTR_FORMAT, result, result);
2349 result = gst_caps_ref (result);
2356 * gst_pad_has_current_caps:
2357 * @pad: a #GstPad to check
2359 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2361 * Returns: TRUE when @pad has caps associated with it.
2364 gst_pad_has_current_caps (GstPad * pad)
2369 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2371 GST_OBJECT_LOCK (pad);
2372 caps = get_pad_caps (pad);
2373 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2374 "check current pad caps %" GST_PTR_FORMAT, caps);
2375 result = (caps != NULL);
2376 GST_OBJECT_UNLOCK (pad);
2382 * gst_pad_get_current_caps:
2383 * @pad: a #GstPad to get the current capabilities of.
2385 * Gets the capabilities currently configured on @pad with the last
2386 * #GST_EVENT_CAPS event.
2388 * Returns: the current caps of the pad with incremented ref-count.
2391 gst_pad_get_current_caps (GstPad * pad)
2395 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2397 GST_OBJECT_LOCK (pad);
2398 if ((result = get_pad_caps (pad)))
2399 gst_caps_ref (result);
2400 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2401 "get current pad caps %" GST_PTR_FORMAT, result);
2402 GST_OBJECT_UNLOCK (pad);
2409 * @pad: a #GstPad to get the capabilities of.
2410 * @filter: suggested #GstCaps.
2412 * Gets the capabilities this pad can produce or consume.
2413 * Note that this method doesn't necessarily return the caps set by
2414 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2415 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2416 * the pad's get_caps function;
2417 * this returns the pad template caps if not explicitly set.
2419 * When called on sinkpads @filter contains the caps that
2420 * upstream could produce in the order preferred by upstream. When
2421 * called on srcpads @filter contains the caps accepted by
2422 * downstream in the preffered order. @filter might be %NULL but
2423 * if it is not %NULL the returned caps will be a subset of @filter.
2425 * Note that this function does not return writable #GstCaps, use
2426 * gst_caps_make_writable() before modifying the caps.
2428 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2431 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2433 GstCaps *result = NULL;
2435 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2436 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2438 GST_OBJECT_LOCK (pad);
2440 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2442 result = gst_pad_get_caps_unlocked (pad, filter);
2444 GST_OBJECT_UNLOCK (pad);
2451 * gst_pad_peer_get_caps:
2452 * @pad: a #GstPad to get the capabilities of.
2453 * @filter: a #GstCaps filter.
2455 * Gets the capabilities of the peer connected to this pad. Similar to
2456 * gst_pad_get_caps().
2458 * When called on srcpads @filter contains the caps that
2459 * upstream could produce in the order preferred by upstream. When
2460 * called on sinkpads @filter contains the caps accepted by
2461 * downstream in the preffered order. @filter might be %NULL but
2462 * if it is not %NULL the returned caps will be a subset of @filter.
2464 * Returns: the caps of the peer pad with incremented ref-count. This function
2465 * returns %NULL when there is no peer pad.
2468 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2471 GstCaps *result = NULL;
2473 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2474 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2476 GST_OBJECT_LOCK (pad);
2478 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2480 peerpad = GST_PAD_PEER (pad);
2481 if (G_UNLIKELY (peerpad == NULL))
2484 gst_object_ref (peerpad);
2485 GST_OBJECT_UNLOCK (pad);
2487 result = gst_pad_get_caps (peerpad, filter);
2489 gst_object_unref (peerpad);
2495 GST_OBJECT_UNLOCK (pad);
2501 gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps)
2503 /* default fixation */
2504 gst_caps_fixate (caps);
2508 * gst_pad_fixate_caps:
2509 * @pad: a #GstPad to fixate
2510 * @caps: the #GstCaps to fixate
2512 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2513 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2516 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2518 GstPadFixateCapsFunction fixatefunc;
2520 g_return_if_fail (GST_IS_PAD (pad));
2521 g_return_if_fail (caps != NULL);
2522 g_return_if_fail (!gst_caps_is_empty (caps));
2523 g_return_if_fail (!gst_caps_is_any (caps));
2525 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2528 g_return_if_fail (gst_caps_is_writable (caps));
2530 if (G_LIKELY ((fixatefunc = GST_PAD_FIXATECAPSFUNC (pad))))
2531 fixatefunc (pad, caps);
2534 /* Default accept caps implementation just checks against
2535 * against the allowed caps for the pad */
2537 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2539 /* get the caps and see if it intersects to something not empty */
2541 gboolean result = FALSE;
2543 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2545 allowed = gst_pad_get_caps (pad, NULL);
2547 goto nothing_allowed;
2549 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2551 result = gst_caps_is_subset (caps, allowed);
2553 gst_caps_unref (allowed);
2560 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2566 * gst_pad_accept_caps:
2567 * @pad: a #GstPad to check
2568 * @caps: a #GstCaps to check on the pad
2570 * Check if the given pad accepts the caps.
2572 * Returns: TRUE if the pad can accept the caps.
2575 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2578 GstPadAcceptCapsFunction acceptfunc;
2580 GstCaps *existing = NULL;
2583 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2585 /* any pad can be unnegotiated */
2589 /* lock for checking the existing caps */
2590 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2592 GST_OBJECT_LOCK (pad);
2593 /* The current caps on a pad are trivially acceptable */
2594 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2595 if (caps == existing || gst_caps_is_equal (caps, existing))
2598 GST_OBJECT_UNLOCK (pad);
2600 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2602 /* Only null if the element explicitly unset it */
2603 if (G_UNLIKELY (acceptfunc == NULL))
2606 /* we can call the function */
2607 result = acceptfunc (pad, caps);
2608 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2615 GST_DEBUG_OBJECT (pad, "pad had same caps");
2616 GST_OBJECT_UNLOCK (pad);
2622 GST_DEBUG_OBJECT (pad, "no acceptcaps function");
2628 * gst_pad_peer_accept_caps:
2629 * @pad: a #GstPad to check the peer of
2630 * @caps: a #GstCaps to check on the pad
2632 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2635 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2638 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2643 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2645 GST_OBJECT_LOCK (pad);
2647 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2649 peerpad = GST_PAD_PEER (pad);
2650 if (G_UNLIKELY (peerpad == NULL))
2653 gst_object_ref (peerpad);
2654 /* release lock before calling external methods but keep ref to pad */
2655 GST_OBJECT_UNLOCK (pad);
2657 result = gst_pad_accept_caps (peerpad, caps);
2659 gst_object_unref (peerpad);
2665 GST_OBJECT_UNLOCK (pad);
2672 * @pad: a #GstPad to set the capabilities of.
2673 * @caps: (transfer none): a #GstCaps to set.
2675 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2676 * caps on the pad will be unreffed. This function refs the caps so you should
2677 * unref if as soon as you don't need it anymore.
2678 * It is possible to set NULL caps, which will make the pad unnegotiated
2681 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2682 * or bad parameters were provided to this function.
2687 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2690 gboolean res = TRUE;
2692 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2693 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2695 event = gst_event_new_caps (caps);
2697 if (GST_PAD_IS_SRC (pad))
2698 res = gst_pad_push_event (pad, event);
2700 res = gst_pad_send_event (pad, event);
2706 do_event_function (GstPad * pad, GstEvent * event,
2707 GstPadEventFunction eventfunc, gboolean * caps_notify)
2709 gboolean result = TRUE, call_event = TRUE;
2710 GstCaps *caps, *old, *templ;
2712 switch (GST_EVENT_TYPE (event)) {
2713 case GST_EVENT_CAPS:
2715 /* backwards compatibility mode for caps */
2716 gst_event_parse_caps (event, &caps);
2718 /* See if pad accepts the caps */
2719 templ = gst_pad_get_pad_template_caps (pad);
2720 if (!gst_caps_is_subset (caps, templ))
2723 /* check if it changed */
2724 if ((old = gst_pad_get_current_caps (pad))) {
2725 call_event = !gst_caps_is_equal (caps, old);
2726 gst_caps_unref (old);
2729 *caps_notify = TRUE;
2730 gst_caps_unref (templ);
2738 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2739 result = eventfunc (pad, event);
2741 gst_event_unref (event);
2748 gst_caps_unref (templ);
2749 gst_event_unref (event);
2750 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2751 "caps %" GST_PTR_FORMAT " not accepted", caps);
2756 /* function to send all pending events on the sinkpad to the event
2757 * function and collect the results. This function should be called with
2758 * the object lock. The object lock might be released by this function.
2760 static GstFlowReturn
2761 gst_pad_update_events (GstPad * pad)
2763 GstFlowReturn ret = GST_FLOW_OK;
2765 GstPadEventFunction eventfunc;
2767 gboolean caps_notify = FALSE;
2769 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2772 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2776 ev = &pad->priv->events[i];
2778 /* skip without pending event */
2779 if ((event = gst_event_steal (&ev->pending)) == NULL)
2782 gst_event_ref (event);
2783 GST_OBJECT_UNLOCK (pad);
2785 res = do_event_function (pad, event, eventfunc, &caps_notify);
2787 /* things could have changed while we release the lock, check if we still
2788 * are handling the same event, if we don't something changed and we have
2789 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2790 * that lock eventually and then do the retry elsewhere. */
2793 /* make the event active */
2794 gst_event_take (&ev->event, event);
2796 /* notify caps change when needed */
2798 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2799 caps_notify = FALSE;
2802 gst_event_unref (event);
2803 ret = GST_FLOW_ERROR;
2805 GST_OBJECT_LOCK (pad);
2807 /* when we get here all events were successfully updated. */
2813 g_warning ("pad %s:%s has no event handler, file a bug.",
2814 GST_DEBUG_PAD_NAME (pad));
2815 return GST_FLOW_NOT_SUPPORTED;
2820 * gst_pad_get_pad_template_caps:
2821 * @pad: a #GstPad to get the template capabilities from.
2823 * Gets the capabilities for @pad's template.
2825 * Returns: (transfer full): the #GstCaps of this pad template.
2826 * Unref after usage.
2829 gst_pad_get_pad_template_caps (GstPad * pad)
2831 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2833 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2835 if (GST_PAD_PAD_TEMPLATE (pad))
2836 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2838 return gst_static_caps_get (&anycaps);
2843 * @pad: a #GstPad to get the peer of.
2845 * Gets the peer of @pad. This function refs the peer pad so
2846 * you need to unref it after use.
2848 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2853 gst_pad_get_peer (GstPad * pad)
2857 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2859 GST_OBJECT_LOCK (pad);
2860 result = GST_PAD_PEER (pad);
2862 gst_object_ref (result);
2863 GST_OBJECT_UNLOCK (pad);
2869 * gst_pad_get_allowed_caps:
2872 * Gets the capabilities of the allowed media types that can flow through
2873 * @pad and its peer.
2875 * The allowed capabilities is calculated as the intersection of the results of
2876 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2877 * on the resulting caps.
2879 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2880 * caps when you no longer need it. This function returns NULL when @pad
2886 gst_pad_get_allowed_caps (GstPad * pad)
2893 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2895 GST_OBJECT_LOCK (pad);
2896 peer = GST_PAD_PEER (pad);
2897 if (G_UNLIKELY (peer == NULL))
2900 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2902 gst_object_ref (peer);
2903 GST_OBJECT_UNLOCK (pad);
2904 mycaps = gst_pad_get_caps (pad, NULL);
2906 peercaps = gst_pad_get_caps (peer, NULL);
2907 gst_object_unref (peer);
2909 caps = gst_caps_intersect (mycaps, peercaps);
2910 gst_caps_unref (peercaps);
2911 gst_caps_unref (mycaps);
2913 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2920 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2921 GST_OBJECT_UNLOCK (pad);
2928 * gst_pad_iterate_internal_links_default:
2929 * @pad: the #GstPad to get the internal links of.
2931 * Iterate the list of pads to which the given pad is linked to inside of
2932 * the parent element.
2933 * This is the default handler, and thus returns an iterator of all of the
2934 * pads inside the parent element with opposite direction.
2936 * The caller must free this iterator after use with gst_iterator_free().
2938 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2939 * returned pad with gst_object_unref().
2944 gst_pad_iterate_internal_links_default (GstPad * pad)
2952 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2957 GST_OBJECT_LOCK (pad);
2958 parent = GST_PAD_PARENT (pad);
2959 if (!parent || !GST_IS_ELEMENT (parent))
2962 gst_object_ref (parent);
2963 GST_OBJECT_UNLOCK (pad);
2965 if (pad->direction == GST_PAD_SRC)
2966 padlist = &parent->sinkpads;
2968 padlist = &parent->srcpads;
2970 GST_DEBUG_OBJECT (pad, "Making iterator");
2972 cookie = &parent->pads_cookie;
2974 lock = GST_OBJECT_GET_LOCK (parent);
2977 res = gst_iterator_new_list (GST_TYPE_PAD,
2978 lock, cookie, padlist, (GObject *) owner, NULL);
2980 gst_object_unref (owner);
2987 GST_OBJECT_UNLOCK (pad);
2988 GST_DEBUG_OBJECT (pad, "no parent element");
2994 * gst_pad_iterate_internal_links:
2995 * @pad: the GstPad to get the internal links of.
2997 * Gets an iterator for the pads to which the given pad is linked to inside
2998 * of the parent element.
3000 * Each #GstPad element yielded by the iterator will have its refcount increased,
3001 * so unref after use.
3003 * Free-function: gst_iterator_free
3005 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3006 * pad does not have an iterator function configured. Use
3007 * gst_iterator_free() after usage.
3012 gst_pad_iterate_internal_links (GstPad * pad)
3014 GstIterator *res = NULL;
3016 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3018 if (GST_PAD_ITERINTLINKFUNC (pad))
3019 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3027 * @forward: a #GstPadForwardFunction
3028 * @user_data: user data passed to @forward
3030 * Calls @forward for all internally linked pads of @pad. This function deals with
3031 * dynamically changing internal pads and will make sure that the @forward
3032 * function is only called once for each pad.
3034 * When @forward returns TRUE, no further pads will be processed.
3036 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3039 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3042 gboolean result = FALSE;
3044 gboolean done = FALSE;
3045 GValue item = { 0, };
3046 GList *pushed_pads = NULL;
3048 iter = gst_pad_iterate_internal_links (pad);
3054 switch (gst_iterator_next (iter, &item)) {
3055 case GST_ITERATOR_OK:
3059 intpad = g_value_get_object (&item);
3061 /* if already pushed, skip. FIXME, find something faster to tag pads */
3062 if (g_list_find (pushed_pads, intpad)) {
3063 g_value_reset (&item);
3067 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3068 GST_DEBUG_PAD_NAME (intpad));
3069 done = result = forward (intpad, user_data);
3071 pushed_pads = g_list_prepend (pushed_pads, intpad);
3073 g_value_reset (&item);
3076 case GST_ITERATOR_RESYNC:
3077 /* We don't reset the result here because we don't push the event
3078 * again on pads that got the event already and because we need
3079 * to consider the result of the previous pushes */
3080 gst_iterator_resync (iter);
3082 case GST_ITERATOR_ERROR:
3083 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3086 case GST_ITERATOR_DONE:
3091 g_value_unset (&item);
3092 gst_iterator_free (iter);
3094 g_list_free (pushed_pads);
3104 gboolean dispatched;
3108 event_forward_func (GstPad * pad, EventData * data)
3110 /* for each pad we send to, we should ref the event; it's up
3111 * to downstream to unref again when handled. */
3112 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3113 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3115 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3117 data->dispatched = TRUE;
3124 * gst_pad_event_default:
3125 * @pad: a #GstPad to call the default event handler on.
3126 * @event: (transfer full): the #GstEvent to handle.
3128 * Invokes the default event handler for the given pad.
3130 * The EOS event will pause the task associated with @pad before it is forwarded
3131 * to all internally linked pads,
3133 * The CAPS event will never be forwarded.
3135 * The the event is sent to all pads internally linked to @pad. This function
3136 * takes ownership of @event.
3138 * Returns: TRUE if the event was sent successfully.
3141 gst_pad_event_default (GstPad * pad, GstEvent * event)
3146 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3147 g_return_val_if_fail (event != NULL, FALSE);
3149 GST_LOG_OBJECT (pad, "default event handler");
3151 switch (GST_EVENT_TYPE (event)) {
3154 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3155 gst_pad_pause_task (pad);
3163 data.dispatched = FALSE;
3164 data.result = FALSE;
3166 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3168 /* for sinkpads without a parent element or without internal links, nothing
3169 * will be dispatched but we still want to return TRUE. */
3170 if (data.dispatched)
3171 result = data.result;
3175 gst_event_unref (event);
3182 * @pad: a #GstPad to invoke the default query on.
3183 * @query: (transfer none): the #GstQuery to perform.
3185 * Dispatches a query to a pad. The query should have been allocated by the
3186 * caller via one of the type-specific allocation functions. The element that
3187 * the pad belongs to is responsible for filling the query with an appropriate
3188 * response, which should then be parsed with a type-specific query parsing
3191 * Again, the caller is responsible for both the allocation and deallocation of
3192 * the query structure.
3194 * Please also note that some queries might need a running pipeline to work.
3196 * Returns: TRUE if the query could be performed.
3199 gst_pad_query (GstPad * pad, GstQuery * query)
3202 GstPadQueryFunction func;
3204 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3205 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3207 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3208 GST_QUERY_TYPE_NAME (query));
3210 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3213 res = func (pad, query);
3215 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3216 GST_QUERY_TYPE_NAME (query), res);
3222 GST_DEBUG_OBJECT (pad, "had no query function");
3228 * gst_pad_peer_query:
3229 * @pad: a #GstPad to invoke the peer query on.
3230 * @query: (transfer none): the #GstQuery to perform.
3232 * Performs gst_pad_query() on the peer of @pad.
3234 * The caller is responsible for both the allocation and deallocation of
3235 * the query structure.
3237 * Returns: TRUE if the query could be performed. This function returns %FALSE
3238 * if @pad has no peer.
3243 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3248 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3249 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3251 GST_OBJECT_LOCK (pad);
3253 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3254 GST_QUERY_TYPE_NAME (query));
3256 peerpad = GST_PAD_PEER (pad);
3257 if (G_UNLIKELY (peerpad == NULL))
3260 gst_object_ref (peerpad);
3261 GST_OBJECT_UNLOCK (pad);
3263 result = gst_pad_query (peerpad, query);
3265 gst_object_unref (peerpad);
3272 GST_WARNING_OBJECT (pad, "pad has no peer");
3273 GST_OBJECT_UNLOCK (pad);
3279 * gst_pad_query_default:
3280 * @pad: a #GstPad to call the default query handler on.
3281 * @query: (transfer none): the #GstQuery to handle.
3283 * Invokes the default query handler for the given pad.
3284 * The query is sent to all pads internally linked to @pad. Note that
3285 * if there are many possible sink pads that are internally linked to
3286 * @pad, only one will be sent the query.
3287 * Multi-sinkpad elements should implement custom query handlers.
3289 * Returns: TRUE if the query was performed successfully.
3292 gst_pad_query_default (GstPad * pad, GstQuery * query)
3294 gboolean forward = TRUE, ret = FALSE;
3296 switch (GST_QUERY_TYPE (query)) {
3297 case GST_QUERY_SCHEDULING:
3300 case GST_QUERY_POSITION:
3301 case GST_QUERY_SEEKING:
3302 case GST_QUERY_FORMATS:
3303 case GST_QUERY_LATENCY:
3304 case GST_QUERY_JITTER:
3305 case GST_QUERY_RATE:
3306 case GST_QUERY_CONVERT:
3307 case GST_QUERY_ALLOCATION:
3313 ret = gst_pad_forward
3314 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3320 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3322 GstPad *pad = data->pad;
3324 GstPadProbeCallback callback;
3327 /* if we have called this callback, do nothing */
3328 if (PROBE_COOKIE (hook) == data->cookie)
3331 PROBE_COOKIE (hook) = data->cookie;
3333 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3335 /* one of the data types */
3336 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3338 /* one of the scheduling types */
3339 if ((flags & GST_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3341 /* all of the blocking types must match */
3342 if ((flags & GST_PROBE_TYPE_BLOCKING) !=
3343 (data->mask & GST_PROBE_TYPE_BLOCKING))
3346 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3347 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3349 callback = (GstPadProbeCallback) hook->func;
3350 if (callback == NULL)
3353 GST_OBJECT_UNLOCK (pad);
3355 ret = callback (pad, data->mask, data->type_data, hook->data);
3357 GST_OBJECT_LOCK (pad);
3360 case GST_PROBE_REMOVE:
3361 /* remove the probe */
3362 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3363 cleanup_hook (pad, hook);
3365 case GST_PROBE_DROP:
3366 /* need to drop the data, make sure other probes don't get called
3368 GST_DEBUG_OBJECT (pad, "asked to drop item");
3369 data->mask = GST_PROBE_TYPE_INVALID;
3370 data->ret = GST_PROBE_DROP;
3372 case GST_PROBE_PASS:
3373 /* inform the pad block to let things pass */
3374 GST_DEBUG_OBJECT (pad, "asked to pass item");
3378 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3383 #define PROBE_FULL(pad,mask,data,label,defaultval) \
3385 if (G_UNLIKELY (pad->num_probes)) { \
3386 ret = do_probe_callbacks (pad, mask, data, defaultval); \
3387 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3392 #define PROBE(pad,mask,data,label) \
3393 PROBE_FULL(pad, mask, data, label, GST_FLOW_OK);
3395 static GstFlowReturn
3396 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data,
3397 GstFlowReturn defaultval)
3404 data.type_data = type_data;
3405 data.ret = GST_PROBE_OK;
3407 data.cookie = pad->priv->probe_cookie++;
3410 cookie = pad->priv->probe_cookie;
3412 g_hook_list_marshal (&pad->probes, FALSE,
3413 (GHookMarshaller) probe_hook_marshal, &data);
3415 /* if the list changed, call the new callbacks (they will not have their
3416 * cookie set to data.cookie */
3417 if (cookie != pad->priv->probe_cookie) {
3418 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3419 "probe list changed, restarting");
3423 if (data.ret == GST_PROBE_DROP)
3429 if (mask & GST_PROBE_TYPE_BLOCK) {
3430 while (GST_PAD_IS_BLOCKED (pad)) {
3431 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3432 "we are blocked %d times", pad->num_blocked);
3434 /* we might have released the lock */
3435 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3438 /* now we block the streaming thread. It can be unlocked when we
3439 * deactivate the pad (which will also set the FLUSHING flag) or
3440 * when the pad is unblocked. A flushing event will also unblock
3441 * the pad after setting the FLUSHING flag. */
3442 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3443 "Waiting to be unblocked or set flushing");
3444 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3445 GST_PAD_BLOCK_WAIT (pad);
3446 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3447 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3449 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3459 GST_DEBUG_OBJECT (pad, "pad is flushing");
3460 return GST_FLOW_WRONG_STATE;
3464 GST_DEBUG_OBJECT (pad, "data is dropped");
3465 return GST_FLOW_CUSTOM_SUCCESS;
3469 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3470 GST_DEBUG_OBJECT (pad, "data is passed");
3478 * gst_pad_get_offset:
3481 * Get the offset applied to the running time of @pad. @pad has to be a source
3484 * Returns: the offset.
3487 gst_pad_get_offset (GstPad * pad)
3491 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3493 GST_OBJECT_LOCK (pad);
3494 result = pad->offset;
3495 GST_OBJECT_UNLOCK (pad);
3501 * gst_pad_set_offset:
3503 * @offset: the offset
3505 * Set the offset that will be applied to the running time of @pad.
3508 gst_pad_set_offset (GstPad * pad, gint64 offset)
3514 g_return_if_fail (GST_IS_PAD (pad));
3516 GST_OBJECT_LOCK (pad);
3517 /* if nothing changed, do nothing */
3518 if (pad->offset == offset)
3521 pad->offset = offset;
3523 /* if no peer, we just updated the offset */
3524 if ((peer = GST_PAD_PEER (pad)) == NULL)
3527 /* switch pads around when dealing with a sinkpad */
3528 if (GST_PAD_IS_SINK (pad)) {
3529 /* ref the peer so it doesn't go away when we release the lock */
3530 tmp = gst_object_ref (peer);
3531 /* make sure we get the peer (the srcpad) */
3532 GST_OBJECT_UNLOCK (pad);
3538 GST_OBJECT_LOCK (pad);
3539 /* check if the pad didn't get relinked */
3540 if (GST_PAD_PEER (pad) != peer)
3543 /* we can release the ref now */
3544 gst_object_unref (peer);
3547 /* the index of the segment event in the array */
3548 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3550 /* lock order is srcpad >> sinkpad */
3551 GST_OBJECT_LOCK (peer);
3552 /* take the current segment event, adjust it and then place
3553 * it on the sinkpad. events on the srcpad are always active. */
3554 if (replace_event (pad, peer, idx))
3555 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3557 GST_OBJECT_UNLOCK (peer);
3560 GST_OBJECT_UNLOCK (pad);
3564 /**********************************************************************
3565 * Data passing functions
3568 /* this is the chain function that does not perform the additional argument
3569 * checking for that little extra speed.
3571 static inline GstFlowReturn
3572 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3575 gboolean needs_events;
3577 GST_PAD_STREAM_LOCK (pad);
3579 GST_OBJECT_LOCK (pad);
3580 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3583 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3584 if (G_UNLIKELY (needs_events)) {
3585 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3587 GST_DEBUG_OBJECT (pad, "need to update all events");
3588 ret = gst_pad_update_events (pad);
3589 if (G_UNLIKELY (ret != GST_FLOW_OK))
3593 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3595 GST_OBJECT_UNLOCK (pad);
3597 /* NOTE: we read the chainfunc unlocked.
3598 * we cannot hold the lock for the pad so we might send
3599 * the data to the wrong function. This is not really a
3600 * problem since functions are assigned at creation time
3601 * and don't change that often... */
3602 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3603 GstPadChainFunction chainfunc;
3605 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3608 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3609 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3610 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3612 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3614 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3615 "called chainfunction &%s with buffer %p, returned %s",
3616 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3618 GstPadChainListFunction chainlistfunc;
3620 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3623 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3624 "calling chainlistfunction &%s",
3625 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3627 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3629 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3630 "called chainlistfunction &%s, returned %s",
3631 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3634 GST_PAD_STREAM_UNLOCK (pad);
3641 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3642 "chaining, but pad was flushing");
3643 GST_OBJECT_UNLOCK (pad);
3644 GST_PAD_STREAM_UNLOCK (pad);
3645 return GST_FLOW_WRONG_STATE;
3649 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3650 GST_OBJECT_UNLOCK (pad);
3651 GST_PAD_STREAM_UNLOCK (pad);
3656 GST_OBJECT_UNLOCK (pad);
3657 GST_PAD_STREAM_UNLOCK (pad);
3658 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3661 case GST_FLOW_CUSTOM_SUCCESS:
3662 GST_DEBUG_OBJECT (pad, "dropped buffer");
3666 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3673 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3674 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3675 "pushing, but not chainhandler");
3676 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3677 ("push on pad %s:%s but it has no chainfunction",
3678 GST_DEBUG_PAD_NAME (pad)));
3679 GST_PAD_STREAM_UNLOCK (pad);
3680 return GST_FLOW_NOT_SUPPORTED;
3686 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3687 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3690 * Chain a buffer to @pad.
3692 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3694 * If the buffer type is not acceptable for @pad (as negotiated with a
3695 * preceeding GST_EVENT_CAPS event), this function returns
3696 * #GST_FLOW_NOT_NEGOTIATED.
3698 * The function proceeds calling the chain function installed on @pad (see
3699 * gst_pad_set_chain_function()) and the return value of that function is
3700 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3703 * In all cases, success or failure, the caller loses its reference to @buffer
3704 * after calling this function.
3706 * Returns: a #GstFlowReturn from the pad.
3711 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3713 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3714 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3715 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3717 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3720 static GstFlowReturn
3721 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3727 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3729 len = gst_buffer_list_len (list);
3732 for (i = 0; i < len; i++) {
3733 buffer = gst_buffer_list_get (list, i);
3735 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3736 gst_buffer_ref (buffer));
3737 if (ret != GST_FLOW_OK)
3740 gst_buffer_list_unref (list);
3746 * gst_pad_chain_list:
3747 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3748 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3751 * Chain a bufferlist to @pad.
3753 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3755 * If @pad was not negotiated properly with a CAPS event, this function
3756 * returns #GST_FLOW_NOT_NEGOTIATED.
3758 * The function proceeds calling the chainlist function installed on @pad (see
3759 * gst_pad_set_chain_list_function()) and the return value of that function is
3760 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3761 * chainlist function.
3763 * In all cases, success or failure, the caller loses its reference to @list
3764 * after calling this function.
3768 * Returns: a #GstFlowReturn from the pad.
3773 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3775 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3776 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3777 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3779 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3782 static GstFlowReturn
3783 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3788 GST_OBJECT_LOCK (pad);
3789 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3792 type |= GST_PROBE_TYPE_PUSH;
3794 /* do block probes */
3795 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3797 /* do post-blocking probes */
3798 PROBE (pad, type, data, probe_stopped);
3800 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3803 /* take ref to peer pad before releasing the lock */
3804 gst_object_ref (peer);
3806 GST_OBJECT_UNLOCK (pad);
3808 ret = gst_pad_chain_data_unchecked (peer, type, data);
3810 gst_object_unref (peer);
3812 GST_OBJECT_LOCK (pad);
3814 if (pad->priv->using == 0) {
3815 /* pad is not active anymore, trigger idle callbacks */
3816 PROBE_FULL (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL,
3817 probe_stopped, ret);
3819 GST_OBJECT_UNLOCK (pad);
3823 /* ERROR recovery here */
3827 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3828 "pushing, but pad was flushing");
3829 GST_OBJECT_UNLOCK (pad);
3830 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3831 return GST_FLOW_WRONG_STATE;
3835 GST_OBJECT_UNLOCK (pad);
3836 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3839 case GST_FLOW_CUSTOM_SUCCESS:
3840 GST_DEBUG_OBJECT (pad, "dropped buffer");
3844 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3851 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3852 "pushing, but it was not linked");
3853 GST_OBJECT_UNLOCK (pad);
3854 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3855 return GST_FLOW_NOT_LINKED;
3861 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3862 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3865 * Pushes a buffer to the peer of @pad.
3867 * This function will call installed block probes before triggering any
3868 * installed data probes.
3870 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3871 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3874 * In all cases, success or failure, the caller loses its reference to @buffer
3875 * after calling this function.
3877 * Returns: a #GstFlowReturn from the peer pad.
3882 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3884 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3885 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3886 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3888 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
3892 * gst_pad_push_list:
3893 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3894 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3897 * Pushes a buffer list to the peer of @pad.
3899 * This function will call installed block probes before triggering any
3900 * installed data probes.
3902 * The function proceeds calling the chain function on the peer pad and returns
3903 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3904 * be returned. If the peer pad does not have any installed chainlist function
3905 * every group buffer of the list will be merged into a normal #GstBuffer and
3906 * chained via gst_pad_chain().
3908 * In all cases, success or failure, the caller loses its reference to @list
3909 * after calling this function.
3911 * Returns: a #GstFlowReturn from the peer pad.
3918 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3920 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3921 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3922 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3924 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3927 static GstFlowReturn
3928 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3929 GstBuffer ** buffer)
3932 GstPadGetRangeFunction getrangefunc;
3934 GST_PAD_STREAM_LOCK (pad);
3936 GST_OBJECT_LOCK (pad);
3937 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3939 GST_OBJECT_UNLOCK (pad);
3941 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3944 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3945 "calling getrangefunc %s, offset %"
3946 G_GUINT64_FORMAT ", size %u",
3947 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3949 ret = getrangefunc (pad, offset, size, buffer);
3951 if (G_UNLIKELY (ret != GST_FLOW_OK))
3952 goto get_range_failed;
3954 /* can only fire the signal if we have a valid buffer */
3955 GST_OBJECT_LOCK (pad);
3956 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
3958 GST_OBJECT_UNLOCK (pad);
3960 GST_PAD_STREAM_UNLOCK (pad);
3967 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3968 "getrange, but pad was flushing");
3969 GST_OBJECT_UNLOCK (pad);
3970 GST_PAD_STREAM_UNLOCK (pad);
3971 return GST_FLOW_WRONG_STATE;
3975 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3976 ("getrange on pad %s:%s but it has no getrangefunction",
3977 GST_DEBUG_PAD_NAME (pad)));
3978 GST_PAD_STREAM_UNLOCK (pad);
3979 return GST_FLOW_NOT_SUPPORTED;
3983 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3984 "probe returned %s", gst_flow_get_name (ret));
3985 GST_OBJECT_UNLOCK (pad);
3986 GST_PAD_STREAM_UNLOCK (pad);
3987 gst_buffer_unref (*buffer);
3993 GST_PAD_STREAM_UNLOCK (pad);
3995 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3996 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3997 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4003 * gst_pad_get_range:
4004 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4005 * @offset: The start offset of the buffer
4006 * @size: The length of the buffer
4007 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4008 * returns #GST_FLOW_ERROR if %NULL.
4010 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4011 * immediately and @buffer is %NULL.
4013 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4014 * description of a getrange function. If @pad has no getrange function
4015 * installed (see gst_pad_set_getrange_function()) this function returns
4016 * #GST_FLOW_NOT_SUPPORTED.
4018 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4020 * Returns: a #GstFlowReturn from the pad.
4025 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4026 GstBuffer ** buffer)
4028 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4029 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4030 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4032 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4036 * gst_pad_pull_range:
4037 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4038 * @offset: The start offset of the buffer
4039 * @size: The length of the buffer
4040 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4041 * GST_FLOW_ERROR if %NULL.
4043 * Pulls a @buffer from the peer pad.
4045 * This function will first trigger the pad block signal if it was
4048 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4049 * function returns the result of gst_pad_get_range() on the peer pad.
4050 * See gst_pad_get_range() for a list of return values and for the
4051 * semantics of the arguments of this function.
4053 * Returns: a #GstFlowReturn from the peer pad.
4054 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4055 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4056 * @buffer may not be used or freed when any other return value than
4057 * #GST_FLOW_OK is returned.
4062 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4063 GstBuffer ** buffer)
4067 gboolean needs_events;
4069 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4070 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4071 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4073 GST_OBJECT_LOCK (pad);
4074 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4077 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4080 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4083 gst_object_ref (peer);
4085 GST_OBJECT_UNLOCK (pad);
4087 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4089 gst_object_unref (peer);
4091 GST_OBJECT_LOCK (pad);
4093 if (pad->priv->using == 0) {
4094 /* pad is not active anymore, trigger idle callbacks */
4095 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4096 post_probe_stopped);
4099 if (G_UNLIKELY (ret != GST_FLOW_OK))
4100 goto pull_range_failed;
4102 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4103 post_probe_stopped);
4105 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4106 if (G_UNLIKELY (needs_events)) {
4107 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4109 GST_DEBUG_OBJECT (pad, "we need to update the events");
4110 ret = gst_pad_update_events (pad);
4111 if (G_UNLIKELY (ret != GST_FLOW_OK))
4114 GST_OBJECT_UNLOCK (pad);
4118 /* ERROR recovery here */
4121 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4122 "pullrange, but pad was flushing");
4123 GST_OBJECT_UNLOCK (pad);
4124 return GST_FLOW_WRONG_STATE;
4128 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4129 gst_flow_get_name (ret));
4130 GST_OBJECT_UNLOCK (pad);
4135 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4136 "pulling range, but it was not linked");
4137 GST_OBJECT_UNLOCK (pad);
4138 return GST_FLOW_NOT_LINKED;
4143 GST_OBJECT_UNLOCK (pad);
4144 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4145 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4146 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4151 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4152 "post probe returned %s", gst_flow_get_name (ret));
4153 GST_OBJECT_UNLOCK (pad);
4154 if (ret == GST_FLOW_OK)
4155 gst_buffer_unref (*buffer);
4161 GST_OBJECT_UNLOCK (pad);
4162 gst_buffer_unref (*buffer);
4164 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4165 "pullrange returned events that were not accepted");
4171 * gst_pad_push_event:
4172 * @pad: a #GstPad to push the event to.
4173 * @event: (transfer full): the #GstEvent to send to the pad.
4175 * Sends the event to the peer of the given pad. This function is
4176 * mainly used by elements to send events to their peer
4179 * This function takes owership of the provided event so you should
4180 * gst_event_ref() it if you want to reuse the event after this call.
4182 * Returns: TRUE if the event was handled.
4187 gst_pad_push_event (GstPad * pad, GstEvent * event)
4192 gboolean stored = FALSE;
4194 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4195 g_return_val_if_fail (event != NULL, FALSE);
4196 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4198 GST_OBJECT_LOCK (pad);
4200 peerpad = GST_PAD_PEER (pad);
4202 /* Two checks to be made:
4203 * . (un)set the FLUSHING flag for flushing events,
4204 * . handle pad blocking */
4205 switch (GST_EVENT_TYPE (event)) {
4206 case GST_EVENT_FLUSH_START:
4207 GST_PAD_SET_FLUSHING (pad);
4209 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4210 /* flush start will have set the FLUSHING flag and will then
4211 * unlock all threads doing a GCond wait on the blocking pad. This
4212 * will typically unblock the STREAMING thread blocked on a pad. */
4213 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4214 "doing block signal.");
4215 GST_PAD_BLOCK_BROADCAST (pad);
4219 case GST_EVENT_FLUSH_STOP:
4220 GST_PAD_UNSET_FLUSHING (pad);
4222 /* Remove sticky EOS events */
4223 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4224 gst_event_replace (&pad->priv->
4225 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].pending, NULL);
4226 gst_event_replace (&pad->priv->
4227 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].event, NULL);
4229 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4230 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4236 /* store the event on the pad, but only on srcpads */
4237 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4240 idx = GST_EVENT_STICKY_IDX (event);
4241 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4242 GST_EVENT_TYPE_NAME (event), idx);
4244 /* srcpad sticky events always become active immediately */
4245 gst_event_replace (&pad->priv->events[idx].event, event);
4250 /* backwards compatibility mode for caps */
4251 switch (GST_EVENT_TYPE (event)) {
4252 case GST_EVENT_CAPS:
4254 GST_OBJECT_UNLOCK (pad);
4256 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4258 GST_OBJECT_LOCK (pad);
4259 /* the peerpad might have changed. Things we checked above could not
4261 peerpad = GST_PAD_PEER (pad);
4264 case GST_EVENT_SEGMENT:
4268 offset = pad->offset;
4269 /* check if we need to adjust the segment */
4270 if (offset != 0 && (peerpad != NULL)) {
4273 /* copy segment values */
4274 gst_event_copy_segment (event, &segment);
4275 gst_event_unref (event);
4277 /* adjust and make a new event with the offset applied */
4278 segment.base += offset;
4279 event = gst_event_new_segment (&segment);
4283 case GST_EVENT_RECONFIGURE:
4284 if (GST_PAD_IS_SINK (pad))
4285 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4291 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4294 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4295 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4301 /* send probes after modifying the events above */
4302 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4304 /* now check the peer pad */
4305 if (peerpad == NULL)
4308 gst_object_ref (peerpad);
4310 GST_OBJECT_UNLOCK (pad);
4312 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4313 event, GST_EVENT_TYPE_NAME (event), peerpad);
4315 result = gst_pad_send_event (peerpad, event);
4317 /* Note: we gave away ownership of the event at this point but we can still
4318 * print the old pointer */
4319 GST_LOG_OBJECT (pad,
4320 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4323 gst_object_unref (peerpad);
4325 GST_OBJECT_LOCK (pad);
4327 if (pad->priv->using == 0) {
4328 /* pad is not active anymore, trigger idle callbacks */
4329 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4331 GST_OBJECT_UNLOCK (pad);
4333 return result | stored;
4335 /* ERROR handling */
4338 GST_DEBUG_OBJECT (pad, "We're flushing");
4339 GST_OBJECT_UNLOCK (pad);
4340 gst_event_unref (event);
4345 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4346 GST_OBJECT_UNLOCK (pad);
4347 gst_event_unref (event);
4352 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4353 GST_OBJECT_UNLOCK (pad);
4354 gst_event_unref (event);
4360 * gst_pad_send_event:
4361 * @pad: a #GstPad to send the event to.
4362 * @event: (transfer full): the #GstEvent to send to the pad.
4364 * Sends the event to the pad. This function can be used
4365 * by applications to send events in the pipeline.
4367 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4368 * sink pad, @event should be a downstream event. For example, you would not
4369 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4370 * Furthermore, some downstream events have to be serialized with data flow,
4371 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4372 * the event needs to be serialized with data flow, this function will take the
4373 * pad's stream lock while calling its event function.
4375 * To find out whether an event type is upstream, downstream, or downstream and
4376 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4377 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4378 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4379 * plugin doesn't need to bother itself with this information; the core handles
4380 * all necessary locks and checks.
4382 * This function takes owership of the provided event so you should
4383 * gst_event_ref() it if you want to reuse the event after this call.
4385 * Returns: TRUE if the event was handled.
4388 gst_pad_send_event (GstPad * pad, GstEvent * event)
4391 gboolean result = FALSE;
4392 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4394 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4395 g_return_val_if_fail (event != NULL, FALSE);
4397 GST_OBJECT_LOCK (pad);
4398 if (GST_PAD_IS_SINK (pad)) {
4399 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4400 goto wrong_direction;
4401 serialized = GST_EVENT_IS_SERIALIZED (event);
4402 sticky = GST_EVENT_IS_STICKY (event);
4403 } else if (GST_PAD_IS_SRC (pad)) {
4404 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4405 goto wrong_direction;
4406 /* events on srcpad never are serialized and sticky */
4407 serialized = sticky = FALSE;
4409 goto unknown_direction;
4411 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4413 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4415 switch (GST_EVENT_TYPE (event)) {
4416 case GST_EVENT_FLUSH_START:
4417 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4418 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4420 /* can't even accept a flush begin event when flushing */
4421 if (GST_PAD_IS_FLUSHING (pad))
4424 GST_PAD_SET_FLUSHING (pad);
4425 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4426 needs_events = FALSE;
4428 case GST_EVENT_FLUSH_STOP:
4429 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4430 GST_PAD_UNSET_FLUSHING (pad);
4431 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4433 /* Remove pending EOS events */
4434 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4435 gst_event_replace (&pad->priv->
4436 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].pending, NULL);
4437 gst_event_replace (&pad->priv->
4438 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].event, NULL);
4440 GST_OBJECT_UNLOCK (pad);
4441 /* grab stream lock */
4442 GST_PAD_STREAM_LOCK (pad);
4444 GST_OBJECT_LOCK (pad);
4445 needs_events = FALSE;
4447 case GST_EVENT_RECONFIGURE:
4448 if (GST_PAD_IS_SRC (pad))
4449 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4451 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4452 GST_EVENT_TYPE_NAME (event));
4455 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4456 GST_OBJECT_UNLOCK (pad);
4457 GST_PAD_STREAM_LOCK (pad);
4459 GST_OBJECT_LOCK (pad);
4461 /* don't forward events on non-serialized events */
4462 needs_events = FALSE;
4465 /* store the event on the pad, but only on srcpads. We need to store the
4466 * event before checking the flushing flag. */
4471 switch (GST_EVENT_TYPE (event)) {
4472 case GST_EVENT_SEGMENT:
4473 if (pad->offset != 0) {
4476 /* copy segment values */
4477 gst_event_copy_segment (event, &segment);
4478 gst_event_unref (event);
4480 /* adjust and make a new event with the offset applied */
4481 segment.base += pad->offset;
4482 event = gst_event_new_segment (&segment);
4489 idx = GST_EVENT_STICKY_IDX (event);
4490 ev = &pad->priv->events[idx];
4492 if (ev->event != event) {
4493 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4494 GST_EVENT_TYPE_NAME (event), idx);
4495 gst_event_replace (&ev->pending, event);
4496 /* set the flag so that we update the events next time. We would
4497 * usually update below but we might be flushing too. */
4498 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4499 needs_events = TRUE;
4502 /* now do the probe */
4503 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4506 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4512 if (G_UNLIKELY (needs_events)) {
4515 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4517 GST_DEBUG_OBJECT (pad, "need to update all events");
4518 ret = gst_pad_update_events (pad);
4519 if (ret != GST_FLOW_OK)
4521 GST_OBJECT_UNLOCK (pad);
4523 gst_event_unref (event);
4528 /* ensure to pass on event;
4529 * note that a sticky event has already been updated above */
4530 if (G_LIKELY (!needs_events || !sticky)) {
4531 GstPadEventFunction eventfunc;
4533 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4536 GST_OBJECT_UNLOCK (pad);
4538 result = eventfunc (pad, event);
4542 GST_PAD_STREAM_UNLOCK (pad);
4544 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4548 /* ERROR handling */
4551 g_warning ("pad %s:%s sending %s event in wrong direction",
4552 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4553 GST_OBJECT_UNLOCK (pad);
4554 gst_event_unref (event);
4559 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4560 GST_OBJECT_UNLOCK (pad);
4561 gst_event_unref (event);
4566 g_warning ("pad %s:%s has no event handler, file a bug.",
4567 GST_DEBUG_PAD_NAME (pad));
4568 GST_OBJECT_UNLOCK (pad);
4570 GST_PAD_STREAM_UNLOCK (pad);
4571 gst_event_unref (event);
4576 GST_OBJECT_UNLOCK (pad);
4578 GST_PAD_STREAM_UNLOCK (pad);
4579 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4580 "Received event on flushing pad. Discarding");
4581 gst_event_unref (event);
4586 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4587 GST_OBJECT_UNLOCK (pad);
4589 GST_PAD_STREAM_UNLOCK (pad);
4590 gst_event_unref (event);
4595 GST_OBJECT_UNLOCK (pad);
4597 GST_PAD_STREAM_UNLOCK (pad);
4598 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4599 gst_event_unref (event);
4605 * gst_pad_set_element_private:
4606 * @pad: the #GstPad to set the private data of.
4607 * @priv: The private data to attach to the pad.
4609 * Set the given private data gpointer on the pad.
4610 * This function can only be used by the element that owns the pad.
4611 * No locking is performed in this function.
4614 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4616 pad->element_private = priv;
4620 * gst_pad_get_element_private:
4621 * @pad: the #GstPad to get the private data of.
4623 * Gets the private data of a pad.
4624 * No locking is performed in this function.
4626 * Returns: (transfer none): a #gpointer to the private data.
4629 gst_pad_get_element_private (GstPad * pad)
4631 return pad->element_private;
4635 * gst_pad_get_sticky_event:
4636 * @pad: the #GstPad to get the event from.
4637 * @event_type: the #GstEventType that should be retrieved.
4639 * Returns a new reference of the sticky event of type @event_type
4642 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4645 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4647 GstEvent *event = NULL;
4650 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4651 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4653 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4655 GST_OBJECT_LOCK (pad);
4656 if ((event = pad->priv->events[idx].event)) {
4657 gst_event_ref (event);
4659 GST_OBJECT_UNLOCK (pad);
4665 * gst_pad_sticky_events_foreach:
4666 * @pad: the #GstPad that should be used for iteration.
4667 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4668 * @user_data: (closure): the optional user data.
4670 * Iterates all active sticky events on @pad and calls @foreach_func for every
4671 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4672 * is immediately stopped.
4674 * Returns: GST_FLOW_OK if iteration was successful
4677 gst_pad_sticky_events_foreach (GstPad * pad,
4678 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4680 GstFlowReturn ret = GST_FLOW_OK;
4684 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4685 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4687 GST_OBJECT_LOCK (pad);
4690 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4694 ev = &pad->priv->events[i];
4696 /* skip without active event */
4697 if ((event = ev->event) == NULL)
4700 gst_event_ref (event);
4701 GST_OBJECT_UNLOCK (pad);
4703 res = foreach_func (pad, event, user_data);
4705 GST_OBJECT_LOCK (pad);
4706 gst_event_unref (event);
4708 if (res != GST_FLOW_OK) {
4713 /* things could have changed while we release the lock, check if we still
4714 * are handling the same event, if we don't something changed and we have
4715 * to try again. FIXME. we need a cookie here. */
4716 if (event != ev->event) {
4717 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4721 GST_OBJECT_UNLOCK (pad);
4727 do_stream_status (GstPad * pad, GstStreamStatusType type,
4728 GThread * thread, GstTask * task)
4732 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4734 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4735 if (GST_IS_ELEMENT (parent)) {
4736 GstMessage *message;
4737 GValue value = { 0 };
4739 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4740 gchar *tname, *ename, *pname;
4742 /* create a good task name */
4743 ename = gst_element_get_name (parent);
4744 pname = gst_pad_get_name (pad);
4745 tname = g_strdup_printf ("%s:%s", ename, pname);
4749 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4753 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4756 g_value_init (&value, GST_TYPE_TASK);
4757 g_value_set_object (&value, task);
4758 gst_message_set_stream_status_object (message, &value);
4759 g_value_unset (&value);
4761 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4762 gst_element_post_message (parent, message);
4764 gst_object_unref (parent);
4769 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4771 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4776 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4778 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4782 static GstTaskThreadCallbacks thr_callbacks = {
4788 * gst_pad_start_task:
4789 * @pad: the #GstPad to start the task of
4790 * @func: the task function to call
4791 * @data: data passed to the task function
4793 * Starts a task that repeatedly calls @func with @data. This function
4794 * is mostly used in pad activation functions to start the dataflow.
4795 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4796 * before @func is called.
4798 * Returns: a %TRUE if the task could be started.
4801 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4806 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4807 g_return_val_if_fail (func != NULL, FALSE);
4809 GST_DEBUG_OBJECT (pad, "start task");
4811 GST_OBJECT_LOCK (pad);
4812 task = GST_PAD_TASK (pad);
4814 task = gst_task_create (func, data);
4815 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4816 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4817 GST_DEBUG_OBJECT (pad, "created task");
4818 GST_PAD_TASK (pad) = task;
4819 gst_object_ref (task);
4820 /* release lock to post the message */
4821 GST_OBJECT_UNLOCK (pad);
4823 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4825 gst_object_unref (task);
4827 GST_OBJECT_LOCK (pad);
4828 /* nobody else is supposed to have changed the pad now */
4829 if (GST_PAD_TASK (pad) != task)
4830 goto concurrent_stop;
4832 res = gst_task_set_state (task, GST_TASK_STARTED);
4833 GST_OBJECT_UNLOCK (pad);
4840 GST_OBJECT_UNLOCK (pad);
4846 * gst_pad_pause_task:
4847 * @pad: the #GstPad to pause the task of
4849 * Pause the task of @pad. This function will also wait until the
4850 * function executed by the task is finished if this function is not
4851 * called from the task function.
4853 * Returns: a TRUE if the task could be paused or FALSE when the pad
4857 gst_pad_pause_task (GstPad * pad)
4862 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4864 GST_DEBUG_OBJECT (pad, "pause task");
4866 GST_OBJECT_LOCK (pad);
4867 task = GST_PAD_TASK (pad);
4870 res = gst_task_set_state (task, GST_TASK_PAUSED);
4871 GST_OBJECT_UNLOCK (pad);
4873 /* wait for task function to finish, this lock is recursive so it does nothing
4874 * when the pause is called from the task itself */
4875 GST_PAD_STREAM_LOCK (pad);
4876 GST_PAD_STREAM_UNLOCK (pad);
4882 GST_DEBUG_OBJECT (pad, "pad has no task");
4883 GST_OBJECT_UNLOCK (pad);
4889 * gst_pad_stop_task:
4890 * @pad: the #GstPad to stop the task of
4892 * Stop the task of @pad. This function will also make sure that the
4893 * function executed by the task will effectively stop if not called
4894 * from the GstTaskFunction.
4896 * This function will deadlock if called from the GstTaskFunction of
4897 * the task. Use gst_task_pause() instead.
4899 * Regardless of whether the pad has a task, the stream lock is acquired and
4900 * released so as to ensure that streaming through this pad has finished.
4902 * Returns: a TRUE if the task could be stopped or FALSE on error.
4905 gst_pad_stop_task (GstPad * pad)
4910 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4912 GST_DEBUG_OBJECT (pad, "stop task");
4914 GST_OBJECT_LOCK (pad);
4915 task = GST_PAD_TASK (pad);
4918 GST_PAD_TASK (pad) = NULL;
4919 res = gst_task_set_state (task, GST_TASK_STOPPED);
4920 GST_OBJECT_UNLOCK (pad);
4922 GST_PAD_STREAM_LOCK (pad);
4923 GST_PAD_STREAM_UNLOCK (pad);
4925 if (!gst_task_join (task))
4928 gst_object_unref (task);
4934 GST_DEBUG_OBJECT (pad, "no task");
4935 GST_OBJECT_UNLOCK (pad);
4937 GST_PAD_STREAM_LOCK (pad);
4938 GST_PAD_STREAM_UNLOCK (pad);
4940 /* this is not an error */
4945 /* this is bad, possibly the application tried to join the task from
4946 * the task's thread. We install the task again so that it will be stopped
4947 * again from the right thread next time hopefully. */
4948 GST_OBJECT_LOCK (pad);
4949 GST_DEBUG_OBJECT (pad, "join failed");
4950 /* we can only install this task if there was no other task */
4951 if (GST_PAD_TASK (pad) == NULL)
4952 GST_PAD_TASK (pad) = task;
4953 GST_OBJECT_UNLOCK (pad);