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)
127 GstPadProbeType mask;
129 GstPadProbeReturn ret;
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 guint gst_pad_signals[LAST_SIGNAL] = { 0 };
151 static GParamSpec *pspec_caps = NULL;
153 /* quarks for probe signals */
154 static GQuark buffer_quark;
155 static GQuark buffer_list_quark;
156 static GQuark event_quark;
165 static GstFlowQuarks flow_quarks[] = {
166 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
167 {GST_FLOW_RESEND, "resend", 0},
168 {GST_FLOW_OK, "ok", 0},
169 {GST_FLOW_NOT_LINKED, "not-linked", 0},
170 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
171 {GST_FLOW_EOS, "eos", 0},
172 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
173 {GST_FLOW_ERROR, "error", 0},
174 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
175 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
180 * @ret: a #GstFlowReturn to get the name of.
182 * Gets a string representing the given flow return.
184 * Returns: a static string with the name of the flow return.
187 gst_flow_get_name (GstFlowReturn ret)
191 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
193 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
194 if (ret == flow_quarks[i].ret)
195 return flow_quarks[i].name;
202 * @ret: a #GstFlowReturn to get the quark of.
204 * Get the unique quark for the given GstFlowReturn.
206 * Returns: the quark associated with the flow return or 0 if an
207 * invalid return was specified.
210 gst_flow_to_quark (GstFlowReturn ret)
214 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
216 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
217 if (ret == flow_quarks[i].ret)
218 return flow_quarks[i].quark;
227 buffer_quark = g_quark_from_static_string ("buffer"); \
228 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
229 event_quark = g_quark_from_static_string ("event"); \
231 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
232 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
235 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
236 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
239 #define gst_pad_parent_class parent_class
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 gobject_class->dispose = gst_pad_dispose;
254 gobject_class->finalize = gst_pad_finalize;
255 gobject_class->set_property = gst_pad_set_property;
256 gobject_class->get_property = gst_pad_get_property;
260 * @pad: the pad that emitted the signal
261 * @peer: the peer pad that has been connected
263 * Signals that a pad has been linked to the peer pad.
265 gst_pad_signals[PAD_LINKED] =
266 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
267 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
268 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
271 * @pad: the pad that emitted the signal
272 * @peer: the peer pad that has been disconnected
274 * Signals that a pad has been unlinked from the peer pad.
276 gst_pad_signals[PAD_UNLINKED] =
277 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
278 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
279 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
281 pspec_caps = g_param_spec_boxed ("caps", "Caps",
282 "The capabilities of the pad", GST_TYPE_CAPS,
283 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
284 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
286 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
287 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
288 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
289 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
291 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
292 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
293 g_param_spec_object ("template", "Template",
294 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
295 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 gstobject_class->path_string_separator = ".";
299 /* Register common function pointer descriptions */
300 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
301 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_fixate_caps_default);
311 gst_pad_init (GstPad * pad)
313 pad->priv = GST_PAD_GET_PRIVATE (pad);
315 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
317 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
318 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
319 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
320 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
321 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
322 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
323 GST_PAD_FIXATECAPSFUNC (pad) = gst_pad_fixate_caps_default;
324 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
326 GST_PAD_SET_FLUSHING (pad);
328 g_static_rec_mutex_init (&pad->stream_rec_lock);
330 pad->block_cond = g_cond_new ();
332 g_hook_list_init (&pad->probes, sizeof (GstProbe));
335 /* called when setting the pad inactive. It removes all sticky events from
338 clear_events (PadEvent events[])
342 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
343 gst_event_replace (&events[i].event, NULL);
344 gst_event_replace (&events[i].pending, NULL);
348 /* The sticky event with @idx from the srcpad is copied to the
349 * pending event on the sinkpad (when different).
350 * This function applies the pad offsets in case of segment events.
351 * This will make sure that we send the event to the sinkpad event
352 * function when the next buffer of event arrives.
353 * Should be called with the OBJECT lock of both pads.
354 * This function returns TRUE when there is a pending event on the
357 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
359 PadEvent *srcev, *sinkev;
361 gboolean pending = FALSE;
363 srcev = &srcpad->priv->events[idx];
365 if ((event = srcev->event)) {
366 sinkev = &sinkpad->priv->events[idx];
368 switch (GST_EVENT_TYPE (event)) {
369 case GST_EVENT_SEGMENT:
374 offset = srcpad->offset + sinkpad->offset;
376 gst_event_copy_segment (event, &segment);
377 /* adjust the base time. FIXME, check negative times, try to tweak the
378 * start to do clipping on negative times */
379 segment.base += offset;
380 /* make a new event from the updated segment */
381 event = gst_event_new_segment (&segment);
388 if (sinkev->event != event) {
389 /* put in the pending entry when different */
390 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, srcpad,
391 "Putting event %p (%s) on pad %s:%s", event,
392 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (sinkpad));
393 gst_event_replace (&sinkev->pending, event);
402 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
407 /* make sure we push the events from the source to this new peer, for this we
408 * copy the events on the sinkpad and mark EVENTS_PENDING */
410 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
411 pending |= replace_event (srcpad, sinkpad, i);
413 /* we had some new pending events, set our flag */
415 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
418 /* should be called with the OBJECT_LOCK */
420 get_pad_caps (GstPad * pad)
422 GstCaps *caps = NULL;
426 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
427 /* we can only use the caps when we have successfully send the caps
428 * event to the event function and is thus in the active entry */
429 if ((event = pad->priv->events[idx].event))
430 gst_event_parse_caps (event, &caps);
436 gst_pad_dispose (GObject * object)
438 GstPad *pad = GST_PAD_CAST (object);
441 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
443 /* unlink the peer pad */
444 if ((peer = gst_pad_get_peer (pad))) {
445 /* window for MT unsafeness, someone else could unlink here
446 * and then we call unlink with wrong pads. The unlink
447 * function would catch this and safely return failed. */
448 if (GST_PAD_IS_SRC (pad))
449 gst_pad_unlink (pad, peer);
451 gst_pad_unlink (peer, pad);
453 gst_object_unref (peer);
456 gst_pad_set_pad_template (pad, NULL);
458 clear_events (pad->priv->events);
460 g_hook_list_clear (&pad->probes);
462 G_OBJECT_CLASS (parent_class)->dispose (object);
466 gst_pad_finalize (GObject * object)
468 GstPad *pad = GST_PAD_CAST (object);
471 /* in case the task is still around, clean it up */
472 if ((task = GST_PAD_TASK (pad))) {
473 gst_task_join (task);
474 GST_PAD_TASK (pad) = NULL;
475 gst_object_unref (task);
478 g_static_rec_mutex_free (&pad->stream_rec_lock);
479 g_cond_free (pad->block_cond);
481 G_OBJECT_CLASS (parent_class)->finalize (object);
485 gst_pad_set_property (GObject * object, guint prop_id,
486 const GValue * value, GParamSpec * pspec)
488 g_return_if_fail (GST_IS_PAD (object));
491 case PAD_PROP_DIRECTION:
492 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
494 case PAD_PROP_TEMPLATE:
495 gst_pad_set_pad_template (GST_PAD_CAST (object),
496 (GstPadTemplate *) g_value_get_object (value));
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 gst_pad_get_property (GObject * object, guint prop_id,
506 GValue * value, GParamSpec * pspec)
508 g_return_if_fail (GST_IS_PAD (object));
512 GST_OBJECT_LOCK (object);
513 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
514 GST_OBJECT_UNLOCK (object);
516 case PAD_PROP_DIRECTION:
517 g_value_set_enum (value, GST_PAD_DIRECTION (object));
519 case PAD_PROP_TEMPLATE:
520 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
523 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530 * @name: the name of the new pad.
531 * @direction: the #GstPadDirection of the pad.
533 * Creates a new pad with the given name in the given direction.
534 * If name is NULL, a guaranteed unique name (across all pads)
536 * This function makes a copy of the name so you can safely free the name.
538 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
543 gst_pad_new (const gchar * name, GstPadDirection direction)
545 return g_object_new (GST_TYPE_PAD,
546 "name", name, "direction", direction, NULL);
550 * gst_pad_new_from_template:
551 * @templ: the pad template to use
552 * @name: the name of the element
554 * Creates a new pad with the given name from the given template.
555 * If name is NULL, a guaranteed unique name (across all pads)
557 * This function makes a copy of the name so you can safely free the name.
559 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
562 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
564 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
566 return g_object_new (GST_TYPE_PAD,
567 "name", name, "direction", templ->direction, "template", templ, NULL);
571 * gst_pad_new_from_static_template:
572 * @templ: the #GstStaticPadTemplate to use
573 * @name: the name of the element
575 * Creates a new pad with the given name from the given static template.
576 * If name is NULL, a guaranteed unique name (across all pads)
578 * This function makes a copy of the name so you can safely free the name.
580 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
583 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
587 GstPadTemplate *template;
589 template = gst_static_pad_template_get (templ);
590 pad = gst_pad_new_from_template (template, name);
591 gst_object_unref (template);
596 * gst_pad_get_direction:
597 * @pad: a #GstPad to get the direction of.
599 * Gets the direction of the pad. The direction of the pad is
600 * decided at construction time so this function does not take
603 * Returns: the #GstPadDirection of the pad.
608 gst_pad_get_direction (GstPad * pad)
610 GstPadDirection result;
612 /* PAD_UNKNOWN is a little silly but we need some sort of
613 * error return value */
614 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
616 result = GST_PAD_DIRECTION (pad);
622 gst_pad_activate_default (GstPad * pad)
624 return gst_pad_activate_push (pad, TRUE);
628 pre_activate (GstPad * pad, GstPadActivateMode new_mode)
631 case GST_PAD_ACTIVATE_PUSH:
632 case GST_PAD_ACTIVATE_PULL:
633 GST_OBJECT_LOCK (pad);
634 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
636 GST_PAD_UNSET_FLUSHING (pad);
637 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
638 GST_OBJECT_UNLOCK (pad);
640 case GST_PAD_ACTIVATE_NONE:
641 GST_OBJECT_LOCK (pad);
642 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
643 GST_PAD_SET_FLUSHING (pad);
644 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
645 /* unlock blocked pads so element can resume and stop */
646 GST_PAD_BLOCK_BROADCAST (pad);
647 GST_OBJECT_UNLOCK (pad);
653 post_activate (GstPad * pad, GstPadActivateMode new_mode)
656 case GST_PAD_ACTIVATE_PUSH:
657 case GST_PAD_ACTIVATE_PULL:
660 case GST_PAD_ACTIVATE_NONE:
661 /* ensures that streaming stops */
662 GST_PAD_STREAM_LOCK (pad);
663 GST_DEBUG_OBJECT (pad, "stopped streaming");
664 GST_OBJECT_LOCK (pad);
665 clear_events (pad->priv->events);
666 GST_OBJECT_UNLOCK (pad);
667 GST_PAD_STREAM_UNLOCK (pad);
673 * gst_pad_set_active:
674 * @pad: the #GstPad to activate or deactivate.
675 * @active: whether or not the pad should be active.
677 * Activates or deactivates the given pad.
678 * Normally called from within core state change functions.
680 * If @active, makes sure the pad is active. If it is already active, either in
681 * push or pull mode, just return. Otherwise dispatches to the pad's activate
682 * function to perform the actual activation.
684 * If not @active, checks the pad's current mode and calls
685 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
688 * Returns: #TRUE if the operation was successful.
693 gst_pad_set_active (GstPad * pad, gboolean active)
695 GstPadActivateMode old;
696 gboolean ret = FALSE;
698 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
700 GST_OBJECT_LOCK (pad);
701 old = GST_PAD_ACTIVATE_MODE (pad);
702 GST_OBJECT_UNLOCK (pad);
706 case GST_PAD_ACTIVATE_PUSH:
707 GST_DEBUG_OBJECT (pad, "activating pad from push");
710 case GST_PAD_ACTIVATE_PULL:
711 GST_DEBUG_OBJECT (pad, "activating pad from pull");
714 case GST_PAD_ACTIVATE_NONE:
715 GST_DEBUG_OBJECT (pad, "activating pad from none");
716 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
719 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
724 case GST_PAD_ACTIVATE_PUSH:
725 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
726 ret = gst_pad_activate_push (pad, FALSE);
728 case GST_PAD_ACTIVATE_PULL:
729 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
730 ret = gst_pad_activate_pull (pad, FALSE);
732 case GST_PAD_ACTIVATE_NONE:
733 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
737 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
743 GST_OBJECT_LOCK (pad);
745 g_critical ("Failed to deactivate pad %s:%s, very bad",
746 GST_DEBUG_PAD_NAME (pad));
748 GST_WARNING_OBJECT (pad, "Failed to activate pad");
750 GST_OBJECT_UNLOCK (pad);
753 GST_OBJECT_LOCK (pad);
754 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
755 GST_OBJECT_UNLOCK (pad);
763 * gst_pad_activate_pull:
764 * @pad: the #GstPad to activate or deactivate.
765 * @active: whether or not the pad should be active.
767 * Activates or deactivates the given pad in pull mode via dispatching to the
768 * pad's activatepullfunc. For use from within pad activation functions only.
769 * When called on sink pads, will first proxy the call to the peer pad, which
770 * is expected to activate its internally linked pads from within its
771 * activate_pull function.
773 * If you don't know what this is, you probably don't want to call it.
775 * Returns: TRUE if the operation was successful.
780 gst_pad_activate_pull (GstPad * pad, gboolean active)
782 GstPadActivateMode old, new;
785 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
787 GST_OBJECT_LOCK (pad);
788 old = GST_PAD_ACTIVATE_MODE (pad);
789 GST_OBJECT_UNLOCK (pad);
793 case GST_PAD_ACTIVATE_PULL:
794 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
796 case GST_PAD_ACTIVATE_PUSH:
797 GST_DEBUG_OBJECT (pad,
798 "activating pad from push, deactivate push first");
799 /* pad was activate in the wrong direction, deactivate it
800 * and reactivate it in pull mode */
801 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
802 goto deactivate_failed;
803 /* fallthrough, pad is deactivated now. */
804 case GST_PAD_ACTIVATE_NONE:
805 GST_DEBUG_OBJECT (pad, "activating pad from none");
810 case GST_PAD_ACTIVATE_NONE:
811 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
813 case GST_PAD_ACTIVATE_PUSH:
814 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
815 /* pad was activated in the other direction, deactivate it
816 * in push mode, this should not happen... */
817 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
818 goto deactivate_failed;
819 /* everything is fine now */
821 case GST_PAD_ACTIVATE_PULL:
822 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
827 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
828 if ((peer = gst_pad_get_peer (pad))) {
829 GST_DEBUG_OBJECT (pad, "calling peer");
830 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
832 gst_object_unref (peer);
834 /* there is no peer, this is only fatal when we activate. When we
835 * deactivate, we must assume the application has unlinked the peer and
836 * will deactivate it eventually. */
840 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
843 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
844 goto failure; /* Can't activate pull on a src without a
848 new = active ? GST_PAD_ACTIVATE_PULL : GST_PAD_ACTIVATE_NONE;
849 pre_activate (pad, new);
851 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
852 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
855 /* can happen for sinks of passthrough elements */
858 post_activate (pad, new);
860 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
861 active ? "activated" : "deactivated");
867 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
868 active ? "activated" : "deactivated");
873 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
874 "failed to %s in switch to pull from mode %d",
875 (active ? "activate" : "deactivate"), old);
880 GST_OBJECT_LOCK (peer);
881 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
882 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
883 GST_OBJECT_UNLOCK (peer);
884 gst_object_unref (peer);
889 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
895 GST_OBJECT_LOCK (pad);
896 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
897 active ? "activate" : "deactivate");
898 GST_PAD_SET_FLUSHING (pad);
899 GST_PAD_ACTIVATE_MODE (pad) = old;
900 GST_OBJECT_UNLOCK (pad);
906 * gst_pad_activate_push:
907 * @pad: the #GstPad to activate or deactivate.
908 * @active: whether the pad should be active or not.
910 * Activates or deactivates the given pad in push mode via dispatching to the
911 * pad's activatepushfunc. For use from within pad activation functions only.
913 * If you don't know what this is, you probably don't want to call it.
915 * Returns: %TRUE if the operation was successful.
920 gst_pad_activate_push (GstPad * pad, gboolean active)
922 GstPadActivateMode old, new;
924 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
925 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
926 active ? "activated" : "deactivated");
928 GST_OBJECT_LOCK (pad);
929 old = GST_PAD_ACTIVATE_MODE (pad);
930 GST_OBJECT_UNLOCK (pad);
934 case GST_PAD_ACTIVATE_PUSH:
935 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
937 case GST_PAD_ACTIVATE_PULL:
938 GST_DEBUG_OBJECT (pad,
939 "activating pad from push, deactivating pull first");
940 /* pad was activate in the wrong direction, deactivate it
941 * an reactivate it in push mode */
942 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
943 goto deactivate_failed;
944 /* fallthrough, pad is deactivated now. */
945 case GST_PAD_ACTIVATE_NONE:
946 GST_DEBUG_OBJECT (pad, "activating pad from none");
951 case GST_PAD_ACTIVATE_NONE:
952 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
954 case GST_PAD_ACTIVATE_PULL:
955 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
956 /* pad was activated in the other direction, deactivate it
957 * in pull mode, this should not happen... */
958 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
959 goto deactivate_failed;
960 /* everything is fine now */
962 case GST_PAD_ACTIVATE_PUSH:
963 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
968 new = active ? GST_PAD_ACTIVATE_PUSH : GST_PAD_ACTIVATE_NONE;
969 pre_activate (pad, new);
971 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
972 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
976 /* quite ok, element relies on state change func to prepare itself */
979 post_activate (pad, new);
981 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
982 active ? "activated" : "deactivated");
987 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
988 active ? "activated" : "deactivated");
993 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
994 "failed to %s in switch to push from mode %d",
995 (active ? "activate" : "deactivate"), old);
1000 GST_OBJECT_LOCK (pad);
1001 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1002 active ? "activate" : "deactivate");
1003 GST_PAD_SET_FLUSHING (pad);
1004 GST_PAD_ACTIVATE_MODE (pad) = old;
1005 GST_OBJECT_UNLOCK (pad);
1011 * gst_pad_is_active:
1012 * @pad: the #GstPad to query
1014 * Query if a pad is active
1016 * Returns: TRUE if the pad is active.
1021 gst_pad_is_active (GstPad * pad)
1023 gboolean result = FALSE;
1025 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1027 GST_OBJECT_LOCK (pad);
1028 result = GST_PAD_IS_ACTIVE (pad);
1029 GST_OBJECT_UNLOCK (pad);
1035 * gst_pad_add_probe:
1036 * @pad: the #GstPad to add the probe to
1037 * @mask: the probe mask
1038 * @callback: #GstPadProbeCallback that will be called with notifications of
1040 * @user_data: (closure): user data passed to the callback
1041 * @destroy_data: #GDestroyNotify for user_data
1043 * Be notified of different states of pads. The provided callback is called for
1044 * every state that matches @mask.
1047 * Pad probe handlers are only called for source pads in push mode
1048 * and sink pads in pull mode.
1051 * Returns: an id or 0 on error. The id can be used to remove the probe with
1052 * gst_pad_remove_probe().
1057 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1058 GstPadProbeCallback callback, gpointer user_data,
1059 GDestroyNotify destroy_data)
1064 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1065 g_return_val_if_fail (mask != 0, 0);
1067 GST_OBJECT_LOCK (pad);
1069 /* FIXME : I'm not checking for != GST_PAD_ACTIVATE_correct_direction
1070 * because the pad might not be activated yet.
1071 * This means that _add_probe() might return a valid probeid ...
1072 * which will potentially never be called if the pad
1073 * is activated in the wrong direction */
1074 if (G_UNLIKELY ((mask & GST_PAD_PROBE_TYPE_PUSH) &&
1075 (GST_PAD_ACTIVATE_MODE (pad) == GST_PAD_ACTIVATE_PULL)))
1076 goto wrong_direction;
1077 if (G_UNLIKELY ((mask & GST_PAD_PROBE_TYPE_PULL) &&
1078 (GST_PAD_ACTIVATE_MODE (pad) == GST_PAD_ACTIVATE_PUSH)))
1079 goto wrong_direction;
1081 /* make a new probe */
1082 hook = g_hook_alloc (&pad->probes);
1084 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1087 /* when no contraints are given for the types, assume all types are
1089 if ((mask & GST_PAD_PROBE_TYPE_DATA) == 0)
1090 mask |= GST_PAD_PROBE_TYPE_DATA;
1091 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1092 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1094 /* store our flags and other fields */
1095 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1096 hook->func = callback;
1097 hook->data = user_data;
1098 hook->destroy = destroy_data;
1099 PROBE_COOKIE (hook) = 0;
1101 /* incremenent cookie so that the new hook get's called */
1102 pad->priv->probe_cookie++;
1105 g_hook_prepend (&pad->probes, hook);
1108 /* get the id of the hook, we return this and it can be used to remove the
1110 res = hook->hook_id;
1112 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1114 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1115 /* we have a block probe */
1117 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1118 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1119 "now %d blocking probes", pad->num_blocked);
1122 /* call the callback if we need to be called for idle callbacks */
1123 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1124 if (pad->priv->using > 0) {
1125 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1126 * flag above, the last thread to leave the push will do the callback. New
1127 * threads going into the push will block. */
1128 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1129 "pad is in use, delay idle callback");
1130 GST_OBJECT_UNLOCK (pad);
1132 /* the pad is idle now, we can signal the idle callback now */
1133 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1134 "pad is idle, trigger idle callback");
1135 GST_OBJECT_UNLOCK (pad);
1137 callback (pad, GST_PAD_PROBE_TYPE_IDLE, NULL, user_data);
1140 GST_OBJECT_UNLOCK (pad);
1146 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pad block on the wrong pad, "
1147 "block src pads in push mode and sink pads in pull mode.");
1148 GST_OBJECT_UNLOCK (pad);
1155 cleanup_hook (GstPad * pad, GHook * hook)
1157 GstPadProbeType type;
1159 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1161 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1162 /* unblock when we remove the last blocking probe */
1164 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1166 if (pad->num_blocked == 0) {
1167 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1168 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1169 GST_PAD_BLOCK_BROADCAST (pad);
1172 g_hook_destroy_link (&pad->probes, hook);
1177 * gst_pad_remove_probe:
1178 * @pad: the #GstPad with the probe
1179 * @id: the probe id to remove
1181 * Remove the probe with @id from @pad.
1186 gst_pad_remove_probe (GstPad * pad, gulong id)
1190 g_return_if_fail (GST_IS_PAD (pad));
1192 GST_OBJECT_LOCK (pad);
1194 hook = g_hook_get (&pad->probes, id);
1198 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1200 cleanup_hook (pad, hook);
1201 GST_OBJECT_UNLOCK (pad);
1207 GST_OBJECT_UNLOCK (pad);
1208 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1214 * gst_pad_is_blocked:
1215 * @pad: the #GstPad to query
1217 * Checks if the pad is blocked or not. This function returns the
1218 * last requested state of the pad. It is not certain that the pad
1219 * is actually blocking at this point (see gst_pad_is_blocking()).
1221 * Returns: TRUE if the pad is blocked.
1226 gst_pad_is_blocked (GstPad * pad)
1228 gboolean result = FALSE;
1230 g_return_val_if_fail (GST_IS_PAD (pad), result);
1232 GST_OBJECT_LOCK (pad);
1233 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1234 GST_OBJECT_UNLOCK (pad);
1240 * gst_pad_is_blocking:
1241 * @pad: the #GstPad to query
1243 * Checks if the pad is blocking or not. This is a guaranteed state
1244 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1246 * Returns: TRUE if the pad is blocking.
1253 gst_pad_is_blocking (GstPad * pad)
1255 gboolean result = FALSE;
1257 g_return_val_if_fail (GST_IS_PAD (pad), result);
1259 GST_OBJECT_LOCK (pad);
1260 /* the blocking flag is only valid if the pad is not flushing */
1261 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1262 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1263 GST_OBJECT_UNLOCK (pad);
1269 * gst_pad_check_reconfigure:
1270 * @pad: the #GstPad to check
1272 * Check and clear the #GST_PAD_NEED_RECONFIGURE flag on @pad and return %TRUE
1273 * if the flag was set.
1275 * Returns: %TRUE is the GST_PAD_NEED_RECONFIGURE flag was set on @pad.
1278 gst_pad_check_reconfigure (GstPad * pad)
1280 gboolean reconfigure;
1282 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1284 GST_OBJECT_LOCK (pad);
1285 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1286 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
1287 GST_OBJECT_UNLOCK (pad);
1293 * gst_pad_mark_reconfigure:
1294 * @pad: the #GstPad to mark
1296 * Mark a pad for needing reconfiguration. The next call to
1297 * gst_pad_check_reconfigure() will return %TRUE after this call.
1300 gst_pad_mark_reconfigure (GstPad * pad)
1302 g_return_if_fail (GST_IS_PAD (pad));
1304 GST_OBJECT_LOCK (pad);
1305 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
1306 GST_OBJECT_UNLOCK (pad);
1310 * gst_pad_set_activate_function:
1312 * @activate: the #GstPadActivateFunction to set.
1314 * Sets the given activate function for @pad. The activate function will
1315 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1316 * the actual activation. Only makes sense to set on sink pads.
1318 * Call this function if your sink pad can start a pull-based task.
1321 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1323 g_return_if_fail (GST_IS_PAD (pad));
1325 GST_PAD_ACTIVATEFUNC (pad) = activate;
1326 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1327 GST_DEBUG_FUNCPTR_NAME (activate));
1331 * gst_pad_set_activatepull_function:
1333 * @activatepull: the #GstPadActivateModeFunction to set.
1335 * Sets the given activate_pull function for the pad. An activate_pull function
1336 * prepares the element and any upstream connections for pulling. See XXX
1337 * part-activation.txt for details.
1340 gst_pad_set_activatepull_function (GstPad * pad,
1341 GstPadActivateModeFunction activatepull)
1343 g_return_if_fail (GST_IS_PAD (pad));
1345 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1346 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1347 GST_DEBUG_FUNCPTR_NAME (activatepull));
1351 * gst_pad_set_activatepush_function:
1353 * @activatepush: the #GstPadActivateModeFunction to set.
1355 * Sets the given activate_push function for the pad. An activate_push function
1356 * prepares the element for pushing. See XXX part-activation.txt for details.
1359 gst_pad_set_activatepush_function (GstPad * pad,
1360 GstPadActivateModeFunction activatepush)
1362 g_return_if_fail (GST_IS_PAD (pad));
1364 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1365 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1366 GST_DEBUG_FUNCPTR_NAME (activatepush));
1370 * gst_pad_set_chain_function:
1371 * @pad: a sink #GstPad.
1372 * @chain: the #GstPadChainFunction to set.
1374 * Sets the given chain function for the pad. The chain function is called to
1375 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1378 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1380 g_return_if_fail (GST_IS_PAD (pad));
1381 g_return_if_fail (GST_PAD_IS_SINK (pad));
1383 GST_PAD_CHAINFUNC (pad) = chain;
1384 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1385 GST_DEBUG_FUNCPTR_NAME (chain));
1389 * gst_pad_set_chain_list_function:
1390 * @pad: a sink #GstPad.
1391 * @chainlist: the #GstPadChainListFunction to set.
1393 * Sets the given chain list function for the pad. The chainlist function is
1394 * called to process a #GstBufferList input buffer list. See
1395 * #GstPadChainListFunction for more details.
1400 gst_pad_set_chain_list_function (GstPad * pad,
1401 GstPadChainListFunction chainlist)
1403 g_return_if_fail (GST_IS_PAD (pad));
1404 g_return_if_fail (GST_PAD_IS_SINK (pad));
1406 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1407 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1408 GST_DEBUG_FUNCPTR_NAME (chainlist));
1412 * gst_pad_set_getrange_function:
1413 * @pad: a source #GstPad.
1414 * @get: the #GstPadGetRangeFunction to set.
1416 * Sets the given getrange function for the pad. The getrange function is
1417 * called to produce a new #GstBuffer to start the processing pipeline. see
1418 * #GstPadGetRangeFunction for a description of the getrange function.
1421 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1423 g_return_if_fail (GST_IS_PAD (pad));
1424 g_return_if_fail (GST_PAD_IS_SRC (pad));
1426 GST_PAD_GETRANGEFUNC (pad) = get;
1428 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1429 GST_DEBUG_FUNCPTR_NAME (get));
1433 * gst_pad_set_event_function:
1434 * @pad: a #GstPad of either direction.
1435 * @event: the #GstPadEventFunction to set.
1437 * Sets the given event handler for the pad.
1440 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1442 g_return_if_fail (GST_IS_PAD (pad));
1444 GST_PAD_EVENTFUNC (pad) = event;
1446 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1447 GST_DEBUG_FUNCPTR_NAME (event));
1451 * gst_pad_set_query_function:
1452 * @pad: a #GstPad of either direction.
1453 * @query: the #GstPadQueryFunction to set.
1455 * Set the given query function for the pad.
1458 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1460 g_return_if_fail (GST_IS_PAD (pad));
1462 GST_PAD_QUERYFUNC (pad) = query;
1464 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1465 GST_DEBUG_FUNCPTR_NAME (query));
1469 * gst_pad_set_query_type_function:
1470 * @pad: a #GstPad of either direction.
1471 * @type_func: the #GstPadQueryTypeFunction to set.
1473 * Set the given query type function for the pad.
1476 gst_pad_set_query_type_function (GstPad * pad,
1477 GstPadQueryTypeFunction type_func)
1479 g_return_if_fail (GST_IS_PAD (pad));
1481 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1483 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1484 GST_DEBUG_FUNCPTR_NAME (type_func));
1488 * gst_pad_get_query_types:
1491 * Get an array of supported queries that can be performed
1494 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1497 const GstQueryType *
1498 gst_pad_get_query_types (GstPad * pad)
1500 GstPadQueryTypeFunction func;
1502 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1504 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1516 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1520 if ((peer = gst_pad_get_peer (pad))) {
1521 *data = gst_pad_get_query_types (peer);
1522 gst_object_unref (peer);
1528 * gst_pad_get_query_types_default:
1531 * Invoke the default query types function on the pad. This function will get
1532 * the supported query type from the peer of an internally linked pad of @pad.
1534 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1535 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1536 * query types function.
1538 const GstQueryType *
1539 gst_pad_get_query_types_default (GstPad * pad)
1541 GstQueryType *result = NULL;
1543 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1545 gst_pad_forward (pad, (GstPadForwardFunction)
1546 gst_pad_get_query_types_dispatcher, &result);
1552 * gst_pad_set_iterate_internal_links_function:
1553 * @pad: a #GstPad of either direction.
1554 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1556 * Sets the given internal link iterator function for the pad.
1561 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1562 GstPadIterIntLinkFunction iterintlink)
1564 g_return_if_fail (GST_IS_PAD (pad));
1566 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1567 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1568 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1572 * gst_pad_set_link_function:
1574 * @link: the #GstPadLinkFunction to set.
1576 * Sets the given link function for the pad. It will be called when
1577 * the pad is linked with another pad.
1579 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1582 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1583 * cannot be made for some reason.
1585 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1586 * of the peer sink pad, if present.
1589 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1591 g_return_if_fail (GST_IS_PAD (pad));
1593 GST_PAD_LINKFUNC (pad) = link;
1594 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1595 GST_DEBUG_FUNCPTR_NAME (link));
1599 * gst_pad_set_unlink_function:
1601 * @unlink: the #GstPadUnlinkFunction to set.
1603 * Sets the given unlink function for the pad. It will be called
1604 * when the pad is unlinked.
1607 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1609 g_return_if_fail (GST_IS_PAD (pad));
1611 GST_PAD_UNLINKFUNC (pad) = unlink;
1612 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1613 GST_DEBUG_FUNCPTR_NAME (unlink));
1617 * gst_pad_set_getcaps_function:
1619 * @getcaps: the #GstPadGetCapsFunction to set.
1621 * Sets the given getcaps function for the pad. @getcaps should return the
1622 * allowable caps for a pad in the context of the element's state, its link to
1623 * other elements, and the devices or files it has opened. These caps must be a
1624 * subset of the pad template caps. In the NULL state with no links, @getcaps
1625 * should ideally return the same caps as the pad template. In rare
1626 * circumstances, an object property can affect the caps returned by @getcaps,
1627 * but this is discouraged.
1629 * You do not need to call this function if @pad's allowed caps are always the
1630 * same as the pad template caps. This can only be true if the padtemplate
1631 * has fixed simple caps.
1633 * For most filters, the caps returned by @getcaps is directly affected by the
1634 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1635 * the srcpad's getcaps function is directly related to the stream data. Again,
1636 * @getcaps should return the most specific caps it reasonably can, since this
1637 * helps with autoplugging.
1639 * Note that the return value from @getcaps is owned by the caller, so the
1640 * caller should unref the caps after usage.
1643 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1645 g_return_if_fail (GST_IS_PAD (pad));
1647 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1648 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1649 GST_DEBUG_FUNCPTR_NAME (getcaps));
1653 * gst_pad_set_acceptcaps_function:
1655 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1657 * Sets the given acceptcaps function for the pad. The acceptcaps function
1658 * will be called to check if the pad can accept the given caps. Setting the
1659 * acceptcaps function to NULL restores the default behaviour of allowing
1660 * any caps that matches the caps from gst_pad_get_caps().
1663 gst_pad_set_acceptcaps_function (GstPad * pad,
1664 GstPadAcceptCapsFunction acceptcaps)
1666 g_return_if_fail (GST_IS_PAD (pad));
1668 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1669 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1670 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1674 * gst_pad_set_fixatecaps_function:
1676 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1678 * Sets the given fixatecaps function for the pad. The fixatecaps function
1679 * will be called whenever the default values for a GstCaps needs to be
1683 gst_pad_set_fixatecaps_function (GstPad * pad,
1684 GstPadFixateCapsFunction fixatecaps)
1686 g_return_if_fail (GST_IS_PAD (pad));
1688 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1689 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1690 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1695 * @srcpad: the source #GstPad to unlink.
1696 * @sinkpad: the sink #GstPad to unlink.
1698 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1699 * signal on both pads.
1701 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1702 * the pads were not linked together.
1707 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1709 gboolean result = FALSE;
1710 GstElement *parent = NULL;
1713 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1714 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1715 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1716 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1718 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1719 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1720 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1722 /* We need to notify the parent before taking any pad locks as the bin in
1723 * question might be waiting for a lock on the pad while holding its lock
1724 * that our message will try to take. */
1725 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1726 if (GST_IS_ELEMENT (parent)) {
1727 gst_element_post_message (parent,
1728 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1729 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1731 gst_object_unref (parent);
1736 GST_OBJECT_LOCK (srcpad);
1737 GST_OBJECT_LOCK (sinkpad);
1739 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1740 goto not_linked_together;
1742 if (GST_PAD_UNLINKFUNC (srcpad)) {
1743 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1745 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1746 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1749 /* first clear peers */
1750 GST_PAD_PEER (srcpad) = NULL;
1751 GST_PAD_PEER (sinkpad) = NULL;
1753 /* clear pending caps if any */
1754 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1755 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1757 GST_OBJECT_UNLOCK (sinkpad);
1758 GST_OBJECT_UNLOCK (srcpad);
1760 /* fire off a signal to each of the pads telling them
1761 * that they've been unlinked */
1762 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1763 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1765 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1766 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1771 if (parent != NULL) {
1772 gst_element_post_message (parent,
1773 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1774 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1775 gst_object_unref (parent);
1780 not_linked_together:
1782 /* we do not emit a warning in this case because unlinking cannot
1783 * be made MT safe.*/
1784 GST_OBJECT_UNLOCK (sinkpad);
1785 GST_OBJECT_UNLOCK (srcpad);
1791 * gst_pad_is_linked:
1792 * @pad: pad to check
1794 * Checks if a @pad is linked to another pad or not.
1796 * Returns: TRUE if the pad is linked, FALSE otherwise.
1801 gst_pad_is_linked (GstPad * pad)
1805 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1807 GST_OBJECT_LOCK (pad);
1808 result = (GST_PAD_PEER (pad) != NULL);
1809 GST_OBJECT_UNLOCK (pad);
1814 /* get the caps from both pads and see if the intersection
1817 * This function should be called with the pad LOCK on both
1821 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1822 GstPadLinkCheck flags)
1824 GstCaps *srccaps = NULL;
1825 GstCaps *sinkcaps = NULL;
1826 gboolean compatible = FALSE;
1828 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1831 /* Doing the expensive caps checking takes priority over only checking the template caps */
1832 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1833 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1834 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1836 /* If one of the two pads doesn't have a template, consider the intersection
1838 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1839 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1843 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1845 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1848 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1850 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1853 /* if we have caps on both pads we can check the intersection. If one
1854 * of the caps is NULL, we return TRUE. */
1855 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1857 gst_caps_unref (srccaps);
1859 gst_caps_unref (sinkcaps);
1863 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1864 gst_caps_unref (srccaps);
1865 gst_caps_unref (sinkcaps);
1868 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1869 (compatible ? "" : "not"));
1874 /* check if the grandparents of both pads are the same.
1875 * This check is required so that we don't try to link
1876 * pads from elements in different bins without ghostpads.
1878 * The LOCK should be held on both pads
1881 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1883 GstObject *psrc, *psink;
1885 psrc = GST_OBJECT_PARENT (src);
1886 psink = GST_OBJECT_PARENT (sink);
1888 /* if one of the pads has no parent, we allow the link */
1889 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1892 /* only care about parents that are elements */
1893 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1894 goto no_element_parent;
1896 /* if the parents are the same, we have a loop */
1897 if (G_UNLIKELY (psrc == psink))
1900 /* if they both have a parent, we check the grandparents. We can not lock
1901 * the parent because we hold on the child (pad) and the locking order is
1902 * parent >> child. */
1903 psrc = GST_OBJECT_PARENT (psrc);
1904 psink = GST_OBJECT_PARENT (psink);
1906 /* if they have grandparents but they are not the same */
1907 if (G_UNLIKELY (psrc != psink))
1908 goto wrong_grandparents;
1915 GST_CAT_DEBUG (GST_CAT_CAPS,
1916 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1917 GST_PTR_FORMAT, psrc, psink);
1922 GST_CAT_DEBUG (GST_CAT_CAPS,
1923 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1924 GST_PTR_FORMAT, psrc, psink);
1929 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1935 GST_CAT_DEBUG (GST_CAT_CAPS,
1936 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1937 GST_PTR_FORMAT, psrc, psink);
1942 /* FIXME leftover from an attempt at refactoring... */
1943 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1944 * the two pads will be locked in the srcpad, sinkpad order. */
1945 static GstPadLinkReturn
1946 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1948 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1949 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1951 GST_OBJECT_LOCK (srcpad);
1953 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1954 goto src_was_linked;
1956 GST_OBJECT_LOCK (sinkpad);
1958 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1959 goto sink_was_linked;
1961 /* check hierarchy, pads can only be linked if the grandparents
1963 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1964 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1965 goto wrong_hierarchy;
1967 /* check pad caps for non-empty intersection */
1968 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1971 /* FIXME check pad scheduling for non-empty intersection */
1973 return GST_PAD_LINK_OK;
1977 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1978 GST_DEBUG_PAD_NAME (srcpad),
1979 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1980 /* we do not emit a warning in this case because unlinking cannot
1981 * be made MT safe.*/
1982 GST_OBJECT_UNLOCK (srcpad);
1983 return GST_PAD_LINK_WAS_LINKED;
1987 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1988 GST_DEBUG_PAD_NAME (sinkpad),
1989 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1990 /* we do not emit a warning in this case because unlinking cannot
1991 * be made MT safe.*/
1992 GST_OBJECT_UNLOCK (sinkpad);
1993 GST_OBJECT_UNLOCK (srcpad);
1994 return GST_PAD_LINK_WAS_LINKED;
1998 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1999 GST_OBJECT_UNLOCK (sinkpad);
2000 GST_OBJECT_UNLOCK (srcpad);
2001 return GST_PAD_LINK_WRONG_HIERARCHY;
2005 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2006 GST_OBJECT_UNLOCK (sinkpad);
2007 GST_OBJECT_UNLOCK (srcpad);
2008 return GST_PAD_LINK_NOFORMAT;
2014 * @srcpad: the source #GstPad.
2015 * @sinkpad: the sink #GstPad.
2017 * Checks if the source pad and the sink pad are compatible so they can be
2020 * Returns: TRUE if the pads can be linked.
2023 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2025 GstPadLinkReturn result;
2027 /* generic checks */
2028 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2029 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2031 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2032 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2034 /* gst_pad_link_prepare does everything for us, we only release the locks
2035 * on the pads that it gets us. If this function returns !OK the locks are not
2037 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2038 if (result != GST_PAD_LINK_OK)
2041 GST_OBJECT_UNLOCK (srcpad);
2042 GST_OBJECT_UNLOCK (sinkpad);
2045 return result == GST_PAD_LINK_OK;
2049 * gst_pad_link_full:
2050 * @srcpad: the source #GstPad to link.
2051 * @sinkpad: the sink #GstPad to link.
2052 * @flags: the checks to validate when linking
2054 * Links the source pad and the sink pad.
2056 * This variant of #gst_pad_link provides a more granular control on the
2057 * checks being done when linking. While providing some considerable speedups
2058 * the caller of this method must be aware that wrong usage of those flags
2059 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2060 * for more information.
2064 * Returns: A result code indicating if the connection worked or
2070 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2072 GstPadLinkReturn result;
2074 GstPadLinkFunction srcfunc, sinkfunc;
2076 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2077 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2078 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2079 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2080 GST_PAD_LINK_WRONG_DIRECTION);
2082 /* Notify the parent early. See gst_pad_unlink for details. */
2083 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2084 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2085 gst_element_post_message (parent,
2086 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2087 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2089 gst_object_unref (parent);
2094 /* prepare will also lock the two pads */
2095 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2097 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2100 /* must set peers before calling the link function */
2101 GST_PAD_PEER (srcpad) = sinkpad;
2102 GST_PAD_PEER (sinkpad) = srcpad;
2104 /* make sure we update events */
2105 prepare_event_update (srcpad, sinkpad);
2107 /* get the link functions */
2108 srcfunc = GST_PAD_LINKFUNC (srcpad);
2109 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2111 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2112 /* custom link functions, execute them */
2113 GST_OBJECT_UNLOCK (sinkpad);
2114 GST_OBJECT_UNLOCK (srcpad);
2117 /* this one will call the peer link function */
2118 result = srcfunc (srcpad, sinkpad);
2119 } else if (sinkfunc) {
2120 /* if no source link function, we need to call the sink link
2121 * function ourselves. */
2122 result = sinkfunc (sinkpad, srcpad);
2125 GST_OBJECT_LOCK (srcpad);
2126 GST_OBJECT_LOCK (sinkpad);
2128 /* we released the lock, check if the same pads are linked still */
2129 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2130 goto concurrent_link;
2132 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2135 GST_OBJECT_UNLOCK (sinkpad);
2136 GST_OBJECT_UNLOCK (srcpad);
2138 /* fire off a signal to each of the pads telling them
2139 * that they've been linked */
2140 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2141 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2143 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2144 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2146 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2149 if (G_LIKELY (parent)) {
2150 gst_element_post_message (parent,
2151 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2152 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2153 gst_object_unref (parent);
2161 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2162 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2163 GST_OBJECT_UNLOCK (sinkpad);
2164 GST_OBJECT_UNLOCK (srcpad);
2166 /* The other link operation succeeded first */
2167 result = GST_PAD_LINK_WAS_LINKED;
2172 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2173 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2175 GST_PAD_PEER (srcpad) = NULL;
2176 GST_PAD_PEER (sinkpad) = NULL;
2178 GST_OBJECT_UNLOCK (sinkpad);
2179 GST_OBJECT_UNLOCK (srcpad);
2187 * @srcpad: the source #GstPad to link.
2188 * @sinkpad: the sink #GstPad to link.
2190 * Links the source pad and the sink pad.
2192 * Returns: A result code indicating if the connection worked or
2198 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2200 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2204 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2206 GstPadTemplate **template_p;
2208 /* this function would need checks if it weren't static */
2210 GST_OBJECT_LOCK (pad);
2211 template_p = &pad->padtemplate;
2212 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2213 GST_OBJECT_UNLOCK (pad);
2216 gst_pad_template_pad_created (templ, pad);
2220 * gst_pad_get_pad_template:
2223 * Gets the template for @pad.
2225 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2226 * instantiated, or %NULL if this pad has no template. Unref after
2230 gst_pad_get_pad_template (GstPad * pad)
2232 GstPadTemplate *templ;
2234 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2236 templ = GST_PAD_PAD_TEMPLATE (pad);
2238 return (templ ? gst_object_ref (templ) : NULL);
2242 caps_with_getcaps (GstPad * pad, GstCaps * filter)
2246 if (GST_PAD_GETCAPSFUNC (pad) == NULL)
2249 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2250 "dispatching to pad getcaps function with "
2251 "filter %" GST_PTR_FORMAT, filter);
2253 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2254 GST_OBJECT_UNLOCK (pad);
2255 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2256 GST_OBJECT_LOCK (pad);
2257 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2259 if (G_UNLIKELY (result == NULL))
2262 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2263 "pad getcaps returned %" GST_PTR_FORMAT, result);
2265 #ifndef G_DISABLE_ASSERT
2266 /* check that the returned caps are a real subset of the template caps */
2267 if (GST_PAD_PAD_TEMPLATE (pad)) {
2268 const GstCaps *templ_caps =
2269 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2270 if (!gst_caps_is_subset (result, templ_caps)) {
2273 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2274 "pad returned caps %" GST_PTR_FORMAT
2275 " which are not a real subset of its template caps %"
2276 GST_PTR_FORMAT, result, templ_caps);
2278 ("pad %s:%s returned caps which are not a real "
2279 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2280 temp = gst_caps_intersect (templ_caps, result);
2281 gst_caps_unref (result);
2286 if (!gst_caps_is_subset (result, filter)) {
2289 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2290 "pad returned caps %" GST_PTR_FORMAT
2291 " which are not a real subset of the filter caps %"
2292 GST_PTR_FORMAT, result, filter);
2293 g_warning ("pad %s:%s returned caps which are not a real "
2294 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2295 /* FIXME: Order? But shouldn't happen anyway... */
2296 temp = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2297 gst_caps_unref (result);
2308 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2309 GST_DEBUG_PAD_NAME (pad));
2314 /* should be called with the pad LOCK held */
2315 /* refs the caps, so caller is responsible for getting it unreffed */
2317 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2319 GstCaps *result = NULL;
2320 GstPadTemplate *templ;
2321 gboolean fixed_caps;
2323 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2325 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2328 /* fixed caps, try the negotiated caps first */
2329 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2330 if ((result = get_pad_caps (pad)))
2334 /* try the getcaps function next */
2335 if ((result = caps_with_getcaps (pad, filter)))
2338 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2339 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2340 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2345 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2346 "non-fixed pad caps: trying pad caps");
2347 /* non fixed caps, try the negotiated caps */
2348 if ((result = get_pad_caps (pad)))
2352 /* this almost never happens */
2353 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2354 result = gst_caps_new_empty ();
2358 /* run the filter on the result */
2360 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2361 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2362 GST_PTR_FORMAT, result, result, filter, filter);
2363 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2364 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2367 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2368 "using caps %p %" GST_PTR_FORMAT, result, result);
2369 result = gst_caps_ref (result);
2376 * gst_pad_has_current_caps:
2377 * @pad: a #GstPad to check
2379 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2381 * Returns: TRUE when @pad has caps associated with it.
2384 gst_pad_has_current_caps (GstPad * pad)
2389 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2391 GST_OBJECT_LOCK (pad);
2392 caps = get_pad_caps (pad);
2393 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2394 "check current pad caps %" GST_PTR_FORMAT, caps);
2395 result = (caps != NULL);
2396 GST_OBJECT_UNLOCK (pad);
2402 * gst_pad_get_current_caps:
2403 * @pad: a #GstPad to get the current capabilities of.
2405 * Gets the capabilities currently configured on @pad with the last
2406 * #GST_EVENT_CAPS event.
2408 * Returns: the current caps of the pad with incremented ref-count.
2411 gst_pad_get_current_caps (GstPad * pad)
2415 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2417 GST_OBJECT_LOCK (pad);
2418 if ((result = get_pad_caps (pad)))
2419 gst_caps_ref (result);
2420 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2421 "get current pad caps %" GST_PTR_FORMAT, result);
2422 GST_OBJECT_UNLOCK (pad);
2429 * @pad: a #GstPad to get the capabilities of.
2430 * @filter: suggested #GstCaps.
2432 * Gets the capabilities this pad can produce or consume.
2433 * Note that this method doesn't necessarily return the caps set by
2434 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2435 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2436 * the pad's get_caps function;
2437 * this returns the pad template caps if not explicitly set.
2439 * When called on sinkpads @filter contains the caps that
2440 * upstream could produce in the order preferred by upstream. When
2441 * called on srcpads @filter contains the caps accepted by
2442 * downstream in the preffered order. @filter might be %NULL but
2443 * if it is not %NULL the returned caps will be a subset of @filter.
2445 * Note that this function does not return writable #GstCaps, use
2446 * gst_caps_make_writable() before modifying the caps.
2448 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2451 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2453 GstCaps *result = NULL;
2455 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2456 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2458 GST_OBJECT_LOCK (pad);
2460 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2462 result = gst_pad_get_caps_unlocked (pad, filter);
2464 GST_OBJECT_UNLOCK (pad);
2471 * gst_pad_peer_get_caps:
2472 * @pad: a #GstPad to get the capabilities of.
2473 * @filter: a #GstCaps filter.
2475 * Gets the capabilities of the peer connected to this pad. Similar to
2476 * gst_pad_get_caps().
2478 * When called on srcpads @filter contains the caps that
2479 * upstream could produce in the order preferred by upstream. When
2480 * called on sinkpads @filter contains the caps accepted by
2481 * downstream in the preffered order. @filter might be %NULL but
2482 * if it is not %NULL the returned caps will be a subset of @filter.
2484 * Returns: the caps of the peer pad with incremented ref-count. This function
2485 * returns %NULL when there is no peer pad.
2488 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2491 GstCaps *result = NULL;
2493 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2494 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2496 GST_OBJECT_LOCK (pad);
2498 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2500 peerpad = GST_PAD_PEER (pad);
2501 if (G_UNLIKELY (peerpad == NULL))
2504 gst_object_ref (peerpad);
2505 GST_OBJECT_UNLOCK (pad);
2507 result = gst_pad_get_caps (peerpad, filter);
2509 gst_object_unref (peerpad);
2515 GST_OBJECT_UNLOCK (pad);
2521 gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps)
2523 /* default fixation */
2524 gst_caps_fixate (caps);
2528 * gst_pad_fixate_caps:
2529 * @pad: a #GstPad to fixate
2530 * @caps: the #GstCaps to fixate
2532 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2533 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2536 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2538 GstPadFixateCapsFunction fixatefunc;
2540 g_return_if_fail (GST_IS_PAD (pad));
2541 g_return_if_fail (caps != NULL);
2542 g_return_if_fail (!gst_caps_is_empty (caps));
2543 g_return_if_fail (!gst_caps_is_any (caps));
2545 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2548 g_return_if_fail (gst_caps_is_writable (caps));
2550 if (G_LIKELY ((fixatefunc = GST_PAD_FIXATECAPSFUNC (pad))))
2551 fixatefunc (pad, caps);
2554 /* Default accept caps implementation just checks against
2555 * against the allowed caps for the pad */
2557 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2559 /* get the caps and see if it intersects to something not empty */
2561 gboolean result = FALSE;
2563 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2565 allowed = gst_pad_get_caps (pad, NULL);
2567 goto nothing_allowed;
2569 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2571 result = gst_caps_is_subset (caps, allowed);
2573 gst_caps_unref (allowed);
2580 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2586 * gst_pad_accept_caps:
2587 * @pad: a #GstPad to check
2588 * @caps: a #GstCaps to check on the pad
2590 * Check if the given pad accepts the caps.
2592 * Returns: TRUE if the pad can accept the caps.
2595 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2598 GstPadAcceptCapsFunction acceptfunc;
2600 GstCaps *existing = NULL;
2603 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2605 /* any pad can be unnegotiated */
2609 /* lock for checking the existing caps */
2610 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2612 GST_OBJECT_LOCK (pad);
2613 /* The current caps on a pad are trivially acceptable */
2614 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2615 if (caps == existing || gst_caps_is_equal (caps, existing))
2618 GST_OBJECT_UNLOCK (pad);
2620 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2622 /* Only null if the element explicitly unset it */
2623 if (G_UNLIKELY (acceptfunc == NULL))
2626 /* we can call the function */
2627 result = acceptfunc (pad, caps);
2628 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2635 GST_DEBUG_OBJECT (pad, "pad had same caps");
2636 GST_OBJECT_UNLOCK (pad);
2642 GST_DEBUG_OBJECT (pad, "no acceptcaps function");
2648 * gst_pad_peer_accept_caps:
2649 * @pad: a #GstPad to check the peer of
2650 * @caps: a #GstCaps to check on the pad
2652 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2655 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2658 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2663 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2665 GST_OBJECT_LOCK (pad);
2667 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2669 peerpad = GST_PAD_PEER (pad);
2670 if (G_UNLIKELY (peerpad == NULL))
2673 gst_object_ref (peerpad);
2674 /* release lock before calling external methods but keep ref to pad */
2675 GST_OBJECT_UNLOCK (pad);
2677 result = gst_pad_accept_caps (peerpad, caps);
2679 gst_object_unref (peerpad);
2685 GST_OBJECT_UNLOCK (pad);
2692 * @pad: a #GstPad to set the capabilities of.
2693 * @caps: (transfer none): a #GstCaps to set.
2695 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2696 * caps on the pad will be unreffed. This function refs the caps so you should
2697 * unref if as soon as you don't need it anymore.
2698 * It is possible to set NULL caps, which will make the pad unnegotiated
2701 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2702 * or bad parameters were provided to this function.
2707 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2710 gboolean res = TRUE;
2712 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2713 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2715 event = gst_event_new_caps (caps);
2717 if (GST_PAD_IS_SRC (pad))
2718 res = gst_pad_push_event (pad, event);
2720 res = gst_pad_send_event (pad, event);
2726 do_event_function (GstPad * pad, GstEvent * event,
2727 GstPadEventFunction eventfunc, gboolean * caps_notify)
2729 gboolean result = TRUE, call_event = TRUE;
2730 GstCaps *caps, *old, *templ;
2732 switch (GST_EVENT_TYPE (event)) {
2733 case GST_EVENT_CAPS:
2735 /* backwards compatibility mode for caps */
2736 gst_event_parse_caps (event, &caps);
2738 /* See if pad accepts the caps */
2739 templ = gst_pad_get_pad_template_caps (pad);
2740 if (!gst_caps_is_subset (caps, templ))
2743 /* check if it changed */
2744 if ((old = gst_pad_get_current_caps (pad))) {
2745 call_event = !gst_caps_is_equal (caps, old);
2746 gst_caps_unref (old);
2749 *caps_notify = TRUE;
2750 gst_caps_unref (templ);
2758 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2759 result = eventfunc (pad, event);
2761 gst_event_unref (event);
2768 gst_caps_unref (templ);
2769 gst_event_unref (event);
2770 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2771 "caps %" GST_PTR_FORMAT " not accepted", caps);
2776 /* function to send all pending events on the sinkpad to the event
2777 * function and collect the results. This function should be called with
2778 * the object lock. The object lock might be released by this function.
2780 static GstFlowReturn
2781 gst_pad_update_events (GstPad * pad)
2783 GstFlowReturn ret = GST_FLOW_OK;
2785 GstPadEventFunction eventfunc;
2787 gboolean caps_notify = FALSE;
2789 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2792 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2796 ev = &pad->priv->events[i];
2798 /* skip without pending event */
2799 if ((event = gst_event_steal (&ev->pending)) == NULL)
2802 gst_event_ref (event);
2803 GST_OBJECT_UNLOCK (pad);
2805 res = do_event_function (pad, event, eventfunc, &caps_notify);
2807 /* things could have changed while we release the lock, check if we still
2808 * are handling the same event, if we don't something changed and we have
2809 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2810 * that lock eventually and then do the retry elsewhere. */
2813 /* make the event active */
2814 gst_event_take (&ev->event, event);
2816 /* notify caps change when needed */
2818 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2819 caps_notify = FALSE;
2822 gst_event_unref (event);
2823 ret = GST_FLOW_ERROR;
2825 GST_OBJECT_LOCK (pad);
2827 /* when we get here all events were successfully updated. */
2833 g_warning ("pad %s:%s has no event handler, file a bug.",
2834 GST_DEBUG_PAD_NAME (pad));
2835 return GST_FLOW_NOT_SUPPORTED;
2840 * gst_pad_get_pad_template_caps:
2841 * @pad: a #GstPad to get the template capabilities from.
2843 * Gets the capabilities for @pad's template.
2845 * Returns: (transfer full): the #GstCaps of this pad template.
2846 * Unref after usage.
2849 gst_pad_get_pad_template_caps (GstPad * pad)
2851 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2853 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2855 if (GST_PAD_PAD_TEMPLATE (pad))
2856 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2858 return gst_static_caps_get (&anycaps);
2863 * @pad: a #GstPad to get the peer of.
2865 * Gets the peer of @pad. This function refs the peer pad so
2866 * you need to unref it after use.
2868 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2873 gst_pad_get_peer (GstPad * pad)
2877 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2879 GST_OBJECT_LOCK (pad);
2880 result = GST_PAD_PEER (pad);
2882 gst_object_ref (result);
2883 GST_OBJECT_UNLOCK (pad);
2889 * gst_pad_get_allowed_caps:
2892 * Gets the capabilities of the allowed media types that can flow through
2893 * @pad and its peer.
2895 * The allowed capabilities is calculated as the intersection of the results of
2896 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2897 * on the resulting caps.
2899 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2900 * caps when you no longer need it. This function returns NULL when @pad
2906 gst_pad_get_allowed_caps (GstPad * pad)
2913 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2915 GST_OBJECT_LOCK (pad);
2916 peer = GST_PAD_PEER (pad);
2917 if (G_UNLIKELY (peer == NULL))
2920 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2922 gst_object_ref (peer);
2923 GST_OBJECT_UNLOCK (pad);
2924 mycaps = gst_pad_get_caps (pad, NULL);
2926 peercaps = gst_pad_get_caps (peer, NULL);
2927 gst_object_unref (peer);
2929 caps = gst_caps_intersect (mycaps, peercaps);
2930 gst_caps_unref (peercaps);
2931 gst_caps_unref (mycaps);
2933 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2940 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2941 GST_OBJECT_UNLOCK (pad);
2948 * gst_pad_iterate_internal_links_default:
2949 * @pad: the #GstPad to get the internal links of.
2951 * Iterate the list of pads to which the given pad is linked to inside of
2952 * the parent element.
2953 * This is the default handler, and thus returns an iterator of all of the
2954 * pads inside the parent element with opposite direction.
2956 * The caller must free this iterator after use with gst_iterator_free().
2958 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2959 * returned pad with gst_object_unref().
2964 gst_pad_iterate_internal_links_default (GstPad * pad)
2972 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2977 GST_OBJECT_LOCK (pad);
2978 parent = GST_PAD_PARENT (pad);
2979 if (!parent || !GST_IS_ELEMENT (parent))
2982 gst_object_ref (parent);
2983 GST_OBJECT_UNLOCK (pad);
2985 if (pad->direction == GST_PAD_SRC)
2986 padlist = &parent->sinkpads;
2988 padlist = &parent->srcpads;
2990 GST_DEBUG_OBJECT (pad, "Making iterator");
2992 cookie = &parent->pads_cookie;
2994 lock = GST_OBJECT_GET_LOCK (parent);
2997 res = gst_iterator_new_list (GST_TYPE_PAD,
2998 lock, cookie, padlist, (GObject *) owner, NULL);
3000 gst_object_unref (owner);
3007 GST_OBJECT_UNLOCK (pad);
3008 GST_DEBUG_OBJECT (pad, "no parent element");
3014 * gst_pad_iterate_internal_links:
3015 * @pad: the GstPad to get the internal links of.
3017 * Gets an iterator for the pads to which the given pad is linked to inside
3018 * of the parent element.
3020 * Each #GstPad element yielded by the iterator will have its refcount increased,
3021 * so unref after use.
3023 * Free-function: gst_iterator_free
3025 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3026 * pad does not have an iterator function configured. Use
3027 * gst_iterator_free() after usage.
3032 gst_pad_iterate_internal_links (GstPad * pad)
3034 GstIterator *res = NULL;
3036 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3038 if (GST_PAD_ITERINTLINKFUNC (pad))
3039 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3047 * @forward: a #GstPadForwardFunction
3048 * @user_data: user data passed to @forward
3050 * Calls @forward for all internally linked pads of @pad. This function deals with
3051 * dynamically changing internal pads and will make sure that the @forward
3052 * function is only called once for each pad.
3054 * When @forward returns TRUE, no further pads will be processed.
3056 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3059 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3062 gboolean result = FALSE;
3064 gboolean done = FALSE;
3065 GValue item = { 0, };
3066 GList *pushed_pads = NULL;
3068 iter = gst_pad_iterate_internal_links (pad);
3074 switch (gst_iterator_next (iter, &item)) {
3075 case GST_ITERATOR_OK:
3079 intpad = g_value_get_object (&item);
3081 /* if already pushed, skip. FIXME, find something faster to tag pads */
3082 if (g_list_find (pushed_pads, intpad)) {
3083 g_value_reset (&item);
3087 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3088 GST_DEBUG_PAD_NAME (intpad));
3089 done = result = forward (intpad, user_data);
3091 pushed_pads = g_list_prepend (pushed_pads, intpad);
3093 g_value_reset (&item);
3096 case GST_ITERATOR_RESYNC:
3097 /* We don't reset the result here because we don't push the event
3098 * again on pads that got the event already and because we need
3099 * to consider the result of the previous pushes */
3100 gst_iterator_resync (iter);
3102 case GST_ITERATOR_ERROR:
3103 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3106 case GST_ITERATOR_DONE:
3111 g_value_unset (&item);
3112 gst_iterator_free (iter);
3114 g_list_free (pushed_pads);
3124 gboolean dispatched;
3128 event_forward_func (GstPad * pad, EventData * data)
3130 /* for each pad we send to, we should ref the event; it's up
3131 * to downstream to unref again when handled. */
3132 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3133 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3135 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3137 data->dispatched = TRUE;
3144 * gst_pad_event_default:
3145 * @pad: a #GstPad to call the default event handler on.
3146 * @event: (transfer full): the #GstEvent to handle.
3148 * Invokes the default event handler for the given pad.
3150 * The EOS event will pause the task associated with @pad before it is forwarded
3151 * to all internally linked pads,
3153 * The CAPS event will never be forwarded.
3155 * The the event is sent to all pads internally linked to @pad. This function
3156 * takes ownership of @event.
3158 * Returns: TRUE if the event was sent successfully.
3161 gst_pad_event_default (GstPad * pad, GstEvent * event)
3166 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3167 g_return_val_if_fail (event != NULL, FALSE);
3169 GST_LOG_OBJECT (pad, "default event handler");
3171 switch (GST_EVENT_TYPE (event)) {
3174 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3175 gst_pad_pause_task (pad);
3183 data.dispatched = FALSE;
3184 data.result = FALSE;
3186 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3188 /* for sinkpads without a parent element or without internal links, nothing
3189 * will be dispatched but we still want to return TRUE. */
3190 if (data.dispatched)
3191 result = data.result;
3195 gst_event_unref (event);
3202 * @pad: a #GstPad to invoke the default query on.
3203 * @query: (transfer none): the #GstQuery to perform.
3205 * Dispatches a query to a pad. The query should have been allocated by the
3206 * caller via one of the type-specific allocation functions. The element that
3207 * the pad belongs to is responsible for filling the query with an appropriate
3208 * response, which should then be parsed with a type-specific query parsing
3211 * Again, the caller is responsible for both the allocation and deallocation of
3212 * the query structure.
3214 * Please also note that some queries might need a running pipeline to work.
3216 * Returns: TRUE if the query could be performed.
3219 gst_pad_query (GstPad * pad, GstQuery * query)
3222 GstPadQueryFunction func;
3224 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3225 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3227 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3228 GST_QUERY_TYPE_NAME (query));
3230 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3233 res = func (pad, query);
3235 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3236 GST_QUERY_TYPE_NAME (query), res);
3242 GST_DEBUG_OBJECT (pad, "had no query function");
3248 * gst_pad_peer_query:
3249 * @pad: a #GstPad to invoke the peer query on.
3250 * @query: (transfer none): the #GstQuery to perform.
3252 * Performs gst_pad_query() on the peer of @pad.
3254 * The caller is responsible for both the allocation and deallocation of
3255 * the query structure.
3257 * Returns: TRUE if the query could be performed. This function returns %FALSE
3258 * if @pad has no peer.
3263 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3268 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3269 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3271 GST_OBJECT_LOCK (pad);
3273 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3274 GST_QUERY_TYPE_NAME (query));
3276 peerpad = GST_PAD_PEER (pad);
3277 if (G_UNLIKELY (peerpad == NULL))
3280 gst_object_ref (peerpad);
3281 GST_OBJECT_UNLOCK (pad);
3283 result = gst_pad_query (peerpad, query);
3285 gst_object_unref (peerpad);
3292 GST_WARNING_OBJECT (pad, "pad has no peer");
3293 GST_OBJECT_UNLOCK (pad);
3299 * gst_pad_query_default:
3300 * @pad: a #GstPad to call the default query handler on.
3301 * @query: (transfer none): the #GstQuery to handle.
3303 * Invokes the default query handler for the given pad.
3304 * The query is sent to all pads internally linked to @pad. Note that
3305 * if there are many possible sink pads that are internally linked to
3306 * @pad, only one will be sent the query.
3307 * Multi-sinkpad elements should implement custom query handlers.
3309 * Returns: TRUE if the query was performed successfully.
3312 gst_pad_query_default (GstPad * pad, GstQuery * query)
3314 gboolean forward = TRUE, ret = FALSE;
3316 switch (GST_QUERY_TYPE (query)) {
3317 case GST_QUERY_SCHEDULING:
3320 case GST_QUERY_POSITION:
3321 case GST_QUERY_SEEKING:
3322 case GST_QUERY_FORMATS:
3323 case GST_QUERY_LATENCY:
3324 case GST_QUERY_JITTER:
3325 case GST_QUERY_RATE:
3326 case GST_QUERY_CONVERT:
3327 case GST_QUERY_ALLOCATION:
3333 ret = gst_pad_forward
3334 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3340 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3342 GstPad *pad = data->pad;
3343 GstPadProbeType flags;
3344 GstPadProbeCallback callback;
3345 GstPadProbeReturn ret;
3347 /* if we have called this callback, do nothing */
3348 if (PROBE_COOKIE (hook) == data->cookie)
3351 PROBE_COOKIE (hook) = data->cookie;
3353 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3355 /* one of the data types */
3356 if ((flags & GST_PAD_PROBE_TYPE_DATA & data->mask) == 0)
3358 /* one of the scheduling types */
3359 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3361 /* all of the blocking types must match */
3362 if ((flags & GST_PAD_PROBE_TYPE_BLOCKING) !=
3363 (data->mask & GST_PAD_PROBE_TYPE_BLOCKING))
3366 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3367 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3369 callback = (GstPadProbeCallback) hook->func;
3370 if (callback == NULL)
3373 GST_OBJECT_UNLOCK (pad);
3375 ret = callback (pad, data->mask, data->type_data, hook->data);
3377 GST_OBJECT_LOCK (pad);
3380 case GST_PAD_PROBE_REMOVE:
3381 /* remove the probe */
3382 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3383 cleanup_hook (pad, hook);
3385 case GST_PAD_PROBE_DROP:
3386 /* need to drop the data, make sure other probes don't get called
3388 GST_DEBUG_OBJECT (pad, "asked to drop item");
3389 data->mask = GST_PAD_PROBE_TYPE_INVALID;
3390 data->ret = GST_PAD_PROBE_DROP;
3392 case GST_PAD_PROBE_PASS:
3393 /* inform the pad block to let things pass */
3394 GST_DEBUG_OBJECT (pad, "asked to pass item");
3398 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3403 #define PROBE_FULL(pad,mask,data,label,defaultval) \
3405 if (G_UNLIKELY (pad->num_probes)) { \
3406 ret = do_probe_callbacks (pad, mask, data, defaultval); \
3407 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3412 #define PROBE(pad,mask,data,label) \
3413 PROBE_FULL(pad, mask, data, label, GST_FLOW_OK);
3415 static GstFlowReturn
3416 do_probe_callbacks (GstPad * pad, GstPadProbeType mask, gpointer type_data,
3417 GstFlowReturn defaultval)
3424 data.type_data = type_data;
3425 data.ret = GST_PAD_PROBE_OK;
3427 data.cookie = pad->priv->probe_cookie++;
3430 cookie = pad->priv->probe_cookie;
3432 g_hook_list_marshal (&pad->probes, FALSE,
3433 (GHookMarshaller) probe_hook_marshal, &data);
3435 /* if the list changed, call the new callbacks (they will not have their
3436 * cookie set to data.cookie */
3437 if (cookie != pad->priv->probe_cookie) {
3438 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3439 "probe list changed, restarting");
3443 if (data.ret == GST_PAD_PROBE_DROP)
3449 if (mask & GST_PAD_PROBE_TYPE_BLOCK) {
3450 while (GST_PAD_IS_BLOCKED (pad)) {
3451 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3452 "we are blocked %d times", pad->num_blocked);
3454 /* we might have released the lock */
3455 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3458 /* now we block the streaming thread. It can be unlocked when we
3459 * deactivate the pad (which will also set the FLUSHING flag) or
3460 * when the pad is unblocked. A flushing event will also unblock
3461 * the pad after setting the FLUSHING flag. */
3462 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3463 "Waiting to be unblocked or set flushing");
3464 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3465 GST_PAD_BLOCK_WAIT (pad);
3466 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3467 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3469 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3479 GST_DEBUG_OBJECT (pad, "pad is flushing");
3480 return GST_FLOW_WRONG_STATE;
3484 GST_DEBUG_OBJECT (pad, "data is dropped");
3485 return GST_FLOW_CUSTOM_SUCCESS;
3489 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3490 GST_DEBUG_OBJECT (pad, "data is passed");
3498 * gst_pad_get_offset:
3501 * Get the offset applied to the running time of @pad. @pad has to be a source
3504 * Returns: the offset.
3507 gst_pad_get_offset (GstPad * pad)
3511 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3513 GST_OBJECT_LOCK (pad);
3514 result = pad->offset;
3515 GST_OBJECT_UNLOCK (pad);
3521 * gst_pad_set_offset:
3523 * @offset: the offset
3525 * Set the offset that will be applied to the running time of @pad.
3528 gst_pad_set_offset (GstPad * pad, gint64 offset)
3534 g_return_if_fail (GST_IS_PAD (pad));
3536 GST_OBJECT_LOCK (pad);
3537 /* if nothing changed, do nothing */
3538 if (pad->offset == offset)
3541 pad->offset = offset;
3543 /* if no peer, we just updated the offset */
3544 if ((peer = GST_PAD_PEER (pad)) == NULL)
3547 /* switch pads around when dealing with a sinkpad */
3548 if (GST_PAD_IS_SINK (pad)) {
3549 /* ref the peer so it doesn't go away when we release the lock */
3550 tmp = gst_object_ref (peer);
3551 /* make sure we get the peer (the srcpad) */
3552 GST_OBJECT_UNLOCK (pad);
3558 GST_OBJECT_LOCK (pad);
3559 /* check if the pad didn't get relinked */
3560 if (GST_PAD_PEER (pad) != peer)
3563 /* we can release the ref now */
3564 gst_object_unref (peer);
3567 /* the index of the segment event in the array */
3568 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3570 /* lock order is srcpad >> sinkpad */
3571 GST_OBJECT_LOCK (peer);
3572 /* take the current segment event, adjust it and then place
3573 * it on the sinkpad. events on the srcpad are always active. */
3574 if (replace_event (pad, peer, idx))
3575 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3577 GST_OBJECT_UNLOCK (peer);
3580 GST_OBJECT_UNLOCK (pad);
3584 /**********************************************************************
3585 * Data passing functions
3588 /* this is the chain function that does not perform the additional argument
3589 * checking for that little extra speed.
3591 static inline GstFlowReturn
3592 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3595 gboolean needs_events;
3597 GST_PAD_STREAM_LOCK (pad);
3599 GST_OBJECT_LOCK (pad);
3600 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3603 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3604 if (G_UNLIKELY (needs_events)) {
3605 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3607 GST_DEBUG_OBJECT (pad, "need to update all events");
3608 ret = gst_pad_update_events (pad);
3609 if (G_UNLIKELY (ret != GST_FLOW_OK))
3613 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | type, data, probe_stopped);
3615 GST_OBJECT_UNLOCK (pad);
3617 /* NOTE: we read the chainfunc unlocked.
3618 * we cannot hold the lock for the pad so we might send
3619 * the data to the wrong function. This is not really a
3620 * problem since functions are assigned at creation time
3621 * and don't change that often... */
3622 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3623 GstPadChainFunction chainfunc;
3625 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3628 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3629 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3630 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3632 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3634 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3635 "called chainfunction &%s with buffer %p, returned %s",
3636 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3638 GstPadChainListFunction chainlistfunc;
3640 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3643 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3644 "calling chainlistfunction &%s",
3645 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3647 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3649 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3650 "called chainlistfunction &%s, returned %s",
3651 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3654 GST_PAD_STREAM_UNLOCK (pad);
3661 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3662 "chaining, but pad was flushing");
3663 GST_OBJECT_UNLOCK (pad);
3664 GST_PAD_STREAM_UNLOCK (pad);
3665 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3666 return GST_FLOW_WRONG_STATE;
3670 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3671 GST_OBJECT_UNLOCK (pad);
3672 GST_PAD_STREAM_UNLOCK (pad);
3673 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3678 GST_OBJECT_UNLOCK (pad);
3679 GST_PAD_STREAM_UNLOCK (pad);
3680 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3683 case GST_FLOW_CUSTOM_SUCCESS:
3684 GST_DEBUG_OBJECT (pad, "dropped buffer");
3688 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3695 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3696 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3697 "pushing, but not chainhandler");
3698 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3699 ("push on pad %s:%s but it has no chainfunction",
3700 GST_DEBUG_PAD_NAME (pad)));
3701 GST_PAD_STREAM_UNLOCK (pad);
3702 return GST_FLOW_NOT_SUPPORTED;
3708 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3709 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3712 * Chain a buffer to @pad.
3714 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3716 * If the buffer type is not acceptable for @pad (as negotiated with a
3717 * preceeding GST_EVENT_CAPS event), this function returns
3718 * #GST_FLOW_NOT_NEGOTIATED.
3720 * The function proceeds calling the chain function installed on @pad (see
3721 * gst_pad_set_chain_function()) and the return value of that function is
3722 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3725 * In all cases, success or failure, the caller loses its reference to @buffer
3726 * after calling this function.
3728 * Returns: a #GstFlowReturn from the pad.
3733 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3735 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3736 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3737 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3739 return gst_pad_chain_data_unchecked (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer);
3742 static GstFlowReturn
3743 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3749 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3751 len = gst_buffer_list_length (list);
3754 for (i = 0; i < len; i++) {
3755 buffer = gst_buffer_list_get (list, i);
3757 gst_pad_chain_data_unchecked (pad, GST_PAD_PROBE_TYPE_BUFFER,
3758 gst_buffer_ref (buffer));
3759 if (ret != GST_FLOW_OK)
3762 gst_buffer_list_unref (list);
3768 * gst_pad_chain_list:
3769 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3770 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3773 * Chain a bufferlist to @pad.
3775 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3777 * If @pad was not negotiated properly with a CAPS event, this function
3778 * returns #GST_FLOW_NOT_NEGOTIATED.
3780 * The function proceeds calling the chainlist function installed on @pad (see
3781 * gst_pad_set_chain_list_function()) and the return value of that function is
3782 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3783 * chainlist function.
3785 * In all cases, success or failure, the caller loses its reference to @list
3786 * after calling this function.
3790 * Returns: a #GstFlowReturn from the pad.
3795 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3797 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3798 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3799 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3801 return gst_pad_chain_data_unchecked (pad, GST_PAD_PROBE_TYPE_BUFFER_LIST,
3805 static GstFlowReturn
3806 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3811 GST_OBJECT_LOCK (pad);
3812 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3815 type |= GST_PAD_PROBE_TYPE_PUSH;
3817 /* do block probes */
3818 PROBE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3820 /* do post-blocking probes */
3821 PROBE (pad, type, data, probe_stopped);
3823 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3826 /* take ref to peer pad before releasing the lock */
3827 gst_object_ref (peer);
3829 GST_OBJECT_UNLOCK (pad);
3831 ret = gst_pad_chain_data_unchecked (peer, type, data);
3833 gst_object_unref (peer);
3835 GST_OBJECT_LOCK (pad);
3837 if (pad->priv->using == 0) {
3838 /* pad is not active anymore, trigger idle callbacks */
3839 PROBE_FULL (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
3840 probe_stopped, ret);
3842 GST_OBJECT_UNLOCK (pad);
3846 /* ERROR recovery here */
3850 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3851 "pushing, but pad was flushing");
3852 GST_OBJECT_UNLOCK (pad);
3853 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3854 return GST_FLOW_WRONG_STATE;
3858 GST_OBJECT_UNLOCK (pad);
3859 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3862 case GST_FLOW_CUSTOM_SUCCESS:
3863 GST_DEBUG_OBJECT (pad, "dropped buffer");
3867 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3874 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3875 "pushing, but it was not linked");
3876 GST_OBJECT_UNLOCK (pad);
3877 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3878 return GST_FLOW_NOT_LINKED;
3884 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3885 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3888 * Pushes a buffer to the peer of @pad.
3890 * This function will call installed block probes before triggering any
3891 * installed data probes.
3893 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3894 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3897 * In all cases, success or failure, the caller loses its reference to @buffer
3898 * after calling this function.
3900 * Returns: a #GstFlowReturn from the peer pad.
3905 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3907 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3908 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3909 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3911 return gst_pad_push_data (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer);
3915 * gst_pad_push_list:
3916 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3917 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3920 * Pushes a buffer list to the peer of @pad.
3922 * This function will call installed block probes before triggering any
3923 * installed data probes.
3925 * The function proceeds calling the chain function on the peer pad and returns
3926 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3927 * be returned. If the peer pad does not have any installed chainlist function
3928 * every group buffer of the list will be merged into a normal #GstBuffer and
3929 * chained via gst_pad_chain().
3931 * In all cases, success or failure, the caller loses its reference to @list
3932 * after calling this function.
3934 * Returns: a #GstFlowReturn from the peer pad.
3941 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3943 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3944 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3945 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3947 return gst_pad_push_data (pad, GST_PAD_PROBE_TYPE_BUFFER_LIST, list);
3950 static GstFlowReturn
3951 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3952 GstBuffer ** buffer)
3955 GstPadGetRangeFunction getrangefunc;
3957 GST_PAD_STREAM_LOCK (pad);
3959 GST_OBJECT_LOCK (pad);
3960 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3962 GST_OBJECT_UNLOCK (pad);
3964 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3967 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3968 "calling getrangefunc %s, offset %"
3969 G_GUINT64_FORMAT ", size %u",
3970 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3972 ret = getrangefunc (pad, offset, size, buffer);
3974 if (G_UNLIKELY (ret != GST_FLOW_OK))
3975 goto get_range_failed;
3977 /* can only fire the signal if we have a valid buffer */
3978 GST_OBJECT_LOCK (pad);
3979 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, *buffer,
3981 GST_OBJECT_UNLOCK (pad);
3983 GST_PAD_STREAM_UNLOCK (pad);
3990 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3991 "getrange, but pad was flushing");
3992 GST_OBJECT_UNLOCK (pad);
3993 GST_PAD_STREAM_UNLOCK (pad);
3994 return GST_FLOW_WRONG_STATE;
3998 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3999 ("getrange on pad %s:%s but it has no getrangefunction",
4000 GST_DEBUG_PAD_NAME (pad)));
4001 GST_PAD_STREAM_UNLOCK (pad);
4002 return GST_FLOW_NOT_SUPPORTED;
4006 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4007 "probe returned %s", gst_flow_get_name (ret));
4008 GST_OBJECT_UNLOCK (pad);
4009 GST_PAD_STREAM_UNLOCK (pad);
4010 gst_buffer_unref (*buffer);
4016 GST_PAD_STREAM_UNLOCK (pad);
4018 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4019 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4020 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4026 * gst_pad_get_range:
4027 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4028 * @offset: The start offset of the buffer
4029 * @size: The length of the buffer
4030 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4031 * returns #GST_FLOW_ERROR if %NULL.
4033 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4034 * immediately and @buffer is %NULL.
4036 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4037 * description of a getrange function. If @pad has no getrange function
4038 * installed (see gst_pad_set_getrange_function()) this function returns
4039 * #GST_FLOW_NOT_SUPPORTED.
4041 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4043 * Returns: a #GstFlowReturn from the pad.
4048 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4049 GstBuffer ** buffer)
4051 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4052 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4053 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4055 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4059 * gst_pad_pull_range:
4060 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4061 * @offset: The start offset of the buffer
4062 * @size: The length of the buffer
4063 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4064 * GST_FLOW_ERROR if %NULL.
4066 * Pulls a @buffer from the peer pad.
4068 * This function will first trigger the pad block signal if it was
4071 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4072 * function returns the result of gst_pad_get_range() on the peer pad.
4073 * See gst_pad_get_range() for a list of return values and for the
4074 * semantics of the arguments of this function.
4076 * Returns: a #GstFlowReturn from the peer pad.
4077 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4078 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4079 * @buffer may not be used or freed when any other return value than
4080 * #GST_FLOW_OK is returned.
4085 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4086 GstBuffer ** buffer)
4090 gboolean needs_events;
4092 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4093 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4094 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4096 GST_OBJECT_LOCK (pad);
4097 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4100 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK, NULL,
4103 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4106 gst_object_ref (peer);
4108 GST_OBJECT_UNLOCK (pad);
4110 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4112 gst_object_unref (peer);
4114 GST_OBJECT_LOCK (pad);
4116 if (pad->priv->using == 0) {
4117 /* pad is not active anymore, trigger idle callbacks */
4118 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE, NULL,
4119 post_probe_stopped);
4122 if (G_UNLIKELY (ret != GST_FLOW_OK))
4123 goto pull_range_failed;
4125 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, buffer,
4126 post_probe_stopped);
4128 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4129 if (G_UNLIKELY (needs_events)) {
4130 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4132 GST_DEBUG_OBJECT (pad, "we need to update the events");
4133 ret = gst_pad_update_events (pad);
4134 if (G_UNLIKELY (ret != GST_FLOW_OK))
4137 GST_OBJECT_UNLOCK (pad);
4141 /* ERROR recovery here */
4144 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4145 "pullrange, but pad was flushing");
4146 GST_OBJECT_UNLOCK (pad);
4147 return GST_FLOW_WRONG_STATE;
4151 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4152 gst_flow_get_name (ret));
4153 GST_OBJECT_UNLOCK (pad);
4158 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4159 "pulling range, but it was not linked");
4160 GST_OBJECT_UNLOCK (pad);
4161 return GST_FLOW_NOT_LINKED;
4166 GST_OBJECT_UNLOCK (pad);
4167 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4168 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4169 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4174 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4175 "post probe returned %s", gst_flow_get_name (ret));
4176 GST_OBJECT_UNLOCK (pad);
4177 if (ret == GST_FLOW_OK)
4178 gst_buffer_unref (*buffer);
4184 GST_OBJECT_UNLOCK (pad);
4185 gst_buffer_unref (*buffer);
4187 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4188 "pullrange returned events that were not accepted");
4194 * gst_pad_push_event:
4195 * @pad: a #GstPad to push the event to.
4196 * @event: (transfer full): the #GstEvent to send to the pad.
4198 * Sends the event to the peer of the given pad. This function is
4199 * mainly used by elements to send events to their peer
4202 * This function takes owership of the provided event so you should
4203 * gst_event_ref() it if you want to reuse the event after this call.
4205 * Returns: TRUE if the event was handled.
4210 gst_pad_push_event (GstPad * pad, GstEvent * event)
4215 gboolean stored = FALSE;
4217 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4218 g_return_val_if_fail (event != NULL, FALSE);
4219 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4221 GST_OBJECT_LOCK (pad);
4223 peerpad = GST_PAD_PEER (pad);
4225 /* Two checks to be made:
4226 * . (un)set the FLUSHING flag for flushing events,
4227 * . handle pad blocking */
4228 switch (GST_EVENT_TYPE (event)) {
4229 case GST_EVENT_FLUSH_START:
4230 GST_PAD_SET_FLUSHING (pad);
4232 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4233 /* flush start will have set the FLUSHING flag and will then
4234 * unlock all threads doing a GCond wait on the blocking pad. This
4235 * will typically unblock the STREAMING thread blocked on a pad. */
4236 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4237 "doing block signal.");
4238 GST_PAD_BLOCK_BROADCAST (pad);
4242 case GST_EVENT_FLUSH_STOP:
4243 GST_PAD_UNSET_FLUSHING (pad);
4245 /* Remove sticky EOS events */
4246 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4247 gst_event_replace (&pad->priv->
4248 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].pending, NULL);
4249 gst_event_replace (&pad->priv->
4250 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].event, NULL);
4252 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4253 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4259 /* store the event on the pad, but only on srcpads */
4260 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4263 idx = GST_EVENT_STICKY_IDX (event);
4264 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4265 GST_EVENT_TYPE_NAME (event), idx);
4267 /* srcpad sticky events always become active immediately */
4268 gst_event_replace (&pad->priv->events[idx].event, event);
4273 /* backwards compatibility mode for caps */
4274 switch (GST_EVENT_TYPE (event)) {
4275 case GST_EVENT_CAPS:
4277 GST_OBJECT_UNLOCK (pad);
4279 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4281 GST_OBJECT_LOCK (pad);
4282 /* the peerpad might have changed. Things we checked above could not
4284 peerpad = GST_PAD_PEER (pad);
4287 case GST_EVENT_SEGMENT:
4291 offset = pad->offset;
4292 /* check if we need to adjust the segment */
4293 if (offset != 0 && (peerpad != NULL)) {
4296 /* copy segment values */
4297 gst_event_copy_segment (event, &segment);
4298 gst_event_unref (event);
4300 /* adjust and make a new event with the offset applied */
4301 segment.base += offset;
4302 event = gst_event_new_segment (&segment);
4306 case GST_EVENT_RECONFIGURE:
4307 if (GST_PAD_IS_SINK (pad))
4308 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4314 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4317 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_EVENT
4318 | GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4324 /* send probes after modifying the events above */
4325 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_EVENT, event,
4328 /* now check the peer pad */
4329 if (peerpad == NULL)
4332 gst_object_ref (peerpad);
4334 GST_OBJECT_UNLOCK (pad);
4336 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4337 event, GST_EVENT_TYPE_NAME (event), peerpad);
4339 result = gst_pad_send_event (peerpad, event);
4341 /* Note: we gave away ownership of the event at this point but we can still
4342 * print the old pointer */
4343 GST_LOG_OBJECT (pad,
4344 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4347 gst_object_unref (peerpad);
4349 GST_OBJECT_LOCK (pad);
4351 if (pad->priv->using == 0) {
4352 /* pad is not active anymore, trigger idle callbacks */
4353 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
4356 GST_OBJECT_UNLOCK (pad);
4358 return result | stored;
4360 /* ERROR handling */
4363 GST_DEBUG_OBJECT (pad, "We're flushing");
4364 GST_OBJECT_UNLOCK (pad);
4365 gst_event_unref (event);
4370 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4371 GST_OBJECT_UNLOCK (pad);
4372 gst_event_unref (event);
4377 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4378 GST_OBJECT_UNLOCK (pad);
4379 gst_event_unref (event);
4385 * gst_pad_send_event:
4386 * @pad: a #GstPad to send the event to.
4387 * @event: (transfer full): the #GstEvent to send to the pad.
4389 * Sends the event to the pad. This function can be used
4390 * by applications to send events in the pipeline.
4392 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4393 * sink pad, @event should be a downstream event. For example, you would not
4394 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4395 * Furthermore, some downstream events have to be serialized with data flow,
4396 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4397 * the event needs to be serialized with data flow, this function will take the
4398 * pad's stream lock while calling its event function.
4400 * To find out whether an event type is upstream, downstream, or downstream and
4401 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4402 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4403 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4404 * plugin doesn't need to bother itself with this information; the core handles
4405 * all necessary locks and checks.
4407 * This function takes owership of the provided event so you should
4408 * gst_event_ref() it if you want to reuse the event after this call.
4410 * Returns: TRUE if the event was handled.
4413 gst_pad_send_event (GstPad * pad, GstEvent * event)
4416 gboolean result = FALSE;
4417 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4419 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4420 g_return_val_if_fail (event != NULL, FALSE);
4422 GST_OBJECT_LOCK (pad);
4423 if (GST_PAD_IS_SINK (pad)) {
4424 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4425 goto wrong_direction;
4426 serialized = GST_EVENT_IS_SERIALIZED (event);
4427 sticky = GST_EVENT_IS_STICKY (event);
4428 } else if (GST_PAD_IS_SRC (pad)) {
4429 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4430 goto wrong_direction;
4431 /* events on srcpad never are serialized and sticky */
4432 serialized = sticky = FALSE;
4434 goto unknown_direction;
4436 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4438 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4440 switch (GST_EVENT_TYPE (event)) {
4441 case GST_EVENT_FLUSH_START:
4442 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4443 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4445 /* can't even accept a flush begin event when flushing */
4446 if (GST_PAD_IS_FLUSHING (pad))
4449 GST_PAD_SET_FLUSHING (pad);
4450 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4451 needs_events = FALSE;
4453 case GST_EVENT_FLUSH_STOP:
4454 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_PAD_ACTIVATE_NONE)) {
4455 GST_PAD_UNSET_FLUSHING (pad);
4456 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4458 /* Remove pending EOS events */
4459 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4460 gst_event_replace (&pad->priv->
4461 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].pending, NULL);
4462 gst_event_replace (&pad->priv->
4463 events[GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS)].event, NULL);
4465 GST_OBJECT_UNLOCK (pad);
4466 /* grab stream lock */
4467 GST_PAD_STREAM_LOCK (pad);
4469 GST_OBJECT_LOCK (pad);
4470 needs_events = FALSE;
4472 case GST_EVENT_RECONFIGURE:
4473 if (GST_PAD_IS_SRC (pad))
4474 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4476 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4477 GST_EVENT_TYPE_NAME (event));
4480 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4481 GST_OBJECT_UNLOCK (pad);
4482 GST_PAD_STREAM_LOCK (pad);
4484 GST_OBJECT_LOCK (pad);
4486 /* don't forward events on non-serialized events */
4487 needs_events = FALSE;
4490 /* store the event on the pad, but only on srcpads. We need to store the
4491 * event before checking the flushing flag. */
4496 switch (GST_EVENT_TYPE (event)) {
4497 case GST_EVENT_SEGMENT:
4498 if (pad->offset != 0) {
4501 /* copy segment values */
4502 gst_event_copy_segment (event, &segment);
4503 gst_event_unref (event);
4505 /* adjust and make a new event with the offset applied */
4506 segment.base += pad->offset;
4507 event = gst_event_new_segment (&segment);
4514 idx = GST_EVENT_STICKY_IDX (event);
4515 ev = &pad->priv->events[idx];
4517 if (ev->event != event) {
4518 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4519 GST_EVENT_TYPE_NAME (event), idx);
4520 gst_event_replace (&ev->pending, event);
4521 /* set the flag so that we update the events next time. We would
4522 * usually update below but we might be flushing too. */
4523 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4524 needs_events = TRUE;
4527 /* now do the probe */
4528 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4531 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_EVENT, event,
4537 if (G_UNLIKELY (needs_events)) {
4540 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4542 GST_DEBUG_OBJECT (pad, "need to update all events");
4543 ret = gst_pad_update_events (pad);
4544 if (ret != GST_FLOW_OK)
4546 GST_OBJECT_UNLOCK (pad);
4548 gst_event_unref (event);
4553 /* ensure to pass on event;
4554 * note that a sticky event has already been updated above */
4555 if (G_LIKELY (!needs_events || !sticky)) {
4556 GstPadEventFunction eventfunc;
4558 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4561 GST_OBJECT_UNLOCK (pad);
4563 result = eventfunc (pad, event);
4567 GST_PAD_STREAM_UNLOCK (pad);
4569 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4573 /* ERROR handling */
4576 g_warning ("pad %s:%s sending %s event in wrong direction",
4577 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4578 GST_OBJECT_UNLOCK (pad);
4579 gst_event_unref (event);
4584 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4585 GST_OBJECT_UNLOCK (pad);
4586 gst_event_unref (event);
4591 g_warning ("pad %s:%s has no event handler, file a bug.",
4592 GST_DEBUG_PAD_NAME (pad));
4593 GST_OBJECT_UNLOCK (pad);
4595 GST_PAD_STREAM_UNLOCK (pad);
4596 gst_event_unref (event);
4601 GST_OBJECT_UNLOCK (pad);
4603 GST_PAD_STREAM_UNLOCK (pad);
4604 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4605 "Received event on flushing pad. Discarding");
4606 gst_event_unref (event);
4611 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4612 GST_OBJECT_UNLOCK (pad);
4614 GST_PAD_STREAM_UNLOCK (pad);
4615 gst_event_unref (event);
4620 GST_OBJECT_UNLOCK (pad);
4622 GST_PAD_STREAM_UNLOCK (pad);
4623 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4624 gst_event_unref (event);
4630 * gst_pad_set_element_private:
4631 * @pad: the #GstPad to set the private data of.
4632 * @priv: The private data to attach to the pad.
4634 * Set the given private data gpointer on the pad.
4635 * This function can only be used by the element that owns the pad.
4636 * No locking is performed in this function.
4639 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4641 pad->element_private = priv;
4645 * gst_pad_get_element_private:
4646 * @pad: the #GstPad to get the private data of.
4648 * Gets the private data of a pad.
4649 * No locking is performed in this function.
4651 * Returns: (transfer none): a #gpointer to the private data.
4654 gst_pad_get_element_private (GstPad * pad)
4656 return pad->element_private;
4660 * gst_pad_get_sticky_event:
4661 * @pad: the #GstPad to get the event from.
4662 * @event_type: the #GstEventType that should be retrieved.
4664 * Returns a new reference of the sticky event of type @event_type
4667 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4670 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4672 GstEvent *event = NULL;
4675 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4676 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4678 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4680 GST_OBJECT_LOCK (pad);
4681 if ((event = pad->priv->events[idx].event)) {
4682 gst_event_ref (event);
4684 GST_OBJECT_UNLOCK (pad);
4690 * gst_pad_sticky_events_foreach:
4691 * @pad: the #GstPad that should be used for iteration.
4692 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4693 * @user_data: (closure): the optional user data.
4695 * Iterates all active sticky events on @pad and calls @foreach_func for every
4696 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4697 * is immediately stopped.
4699 * Returns: GST_FLOW_OK if iteration was successful
4702 gst_pad_sticky_events_foreach (GstPad * pad,
4703 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4705 GstFlowReturn ret = GST_FLOW_OK;
4709 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4710 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4712 GST_OBJECT_LOCK (pad);
4715 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4719 ev = &pad->priv->events[i];
4721 /* skip without active event */
4722 if ((event = ev->event) == NULL)
4725 gst_event_ref (event);
4726 GST_OBJECT_UNLOCK (pad);
4728 res = foreach_func (pad, event, user_data);
4730 GST_OBJECT_LOCK (pad);
4731 gst_event_unref (event);
4733 if (res != GST_FLOW_OK) {
4738 /* things could have changed while we release the lock, check if we still
4739 * are handling the same event, if we don't something changed and we have
4740 * to try again. FIXME. we need a cookie here. */
4741 if (event != ev->event) {
4742 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4746 GST_OBJECT_UNLOCK (pad);
4752 do_stream_status (GstPad * pad, GstStreamStatusType type,
4753 GThread * thread, GstTask * task)
4757 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4759 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4760 if (GST_IS_ELEMENT (parent)) {
4761 GstMessage *message;
4762 GValue value = { 0 };
4764 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4765 gchar *tname, *ename, *pname;
4767 /* create a good task name */
4768 ename = gst_element_get_name (parent);
4769 pname = gst_pad_get_name (pad);
4770 tname = g_strdup_printf ("%s:%s", ename, pname);
4774 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4778 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4781 g_value_init (&value, GST_TYPE_TASK);
4782 g_value_set_object (&value, task);
4783 gst_message_set_stream_status_object (message, &value);
4784 g_value_unset (&value);
4786 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4787 gst_element_post_message (parent, message);
4789 gst_object_unref (parent);
4794 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4796 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4801 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4803 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4807 static GstTaskThreadCallbacks thr_callbacks = {
4813 * gst_pad_start_task:
4814 * @pad: the #GstPad to start the task of
4815 * @func: the task function to call
4816 * @data: data passed to the task function
4818 * Starts a task that repeatedly calls @func with @data. This function
4819 * is mostly used in pad activation functions to start the dataflow.
4820 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4821 * before @func is called.
4823 * Returns: a %TRUE if the task could be started.
4826 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4831 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4832 g_return_val_if_fail (func != NULL, FALSE);
4834 GST_DEBUG_OBJECT (pad, "start task");
4836 GST_OBJECT_LOCK (pad);
4837 task = GST_PAD_TASK (pad);
4839 task = gst_task_new (func, data);
4840 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4841 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4842 GST_DEBUG_OBJECT (pad, "created task");
4843 GST_PAD_TASK (pad) = task;
4844 gst_object_ref (task);
4845 /* release lock to post the message */
4846 GST_OBJECT_UNLOCK (pad);
4848 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4850 gst_object_unref (task);
4852 GST_OBJECT_LOCK (pad);
4853 /* nobody else is supposed to have changed the pad now */
4854 if (GST_PAD_TASK (pad) != task)
4855 goto concurrent_stop;
4857 res = gst_task_set_state (task, GST_TASK_STARTED);
4858 GST_OBJECT_UNLOCK (pad);
4865 GST_OBJECT_UNLOCK (pad);
4871 * gst_pad_pause_task:
4872 * @pad: the #GstPad to pause the task of
4874 * Pause the task of @pad. This function will also wait until the
4875 * function executed by the task is finished if this function is not
4876 * called from the task function.
4878 * Returns: a TRUE if the task could be paused or FALSE when the pad
4882 gst_pad_pause_task (GstPad * pad)
4887 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4889 GST_DEBUG_OBJECT (pad, "pause task");
4891 GST_OBJECT_LOCK (pad);
4892 task = GST_PAD_TASK (pad);
4895 res = gst_task_set_state (task, GST_TASK_PAUSED);
4896 GST_OBJECT_UNLOCK (pad);
4898 /* wait for task function to finish, this lock is recursive so it does nothing
4899 * when the pause is called from the task itself */
4900 GST_PAD_STREAM_LOCK (pad);
4901 GST_PAD_STREAM_UNLOCK (pad);
4907 GST_DEBUG_OBJECT (pad, "pad has no task");
4908 GST_OBJECT_UNLOCK (pad);
4914 * gst_pad_stop_task:
4915 * @pad: the #GstPad to stop the task of
4917 * Stop the task of @pad. This function will also make sure that the
4918 * function executed by the task will effectively stop if not called
4919 * from the GstTaskFunction.
4921 * This function will deadlock if called from the GstTaskFunction of
4922 * the task. Use gst_task_pause() instead.
4924 * Regardless of whether the pad has a task, the stream lock is acquired and
4925 * released so as to ensure that streaming through this pad has finished.
4927 * Returns: a TRUE if the task could be stopped or FALSE on error.
4930 gst_pad_stop_task (GstPad * pad)
4935 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4937 GST_DEBUG_OBJECT (pad, "stop task");
4939 GST_OBJECT_LOCK (pad);
4940 task = GST_PAD_TASK (pad);
4943 GST_PAD_TASK (pad) = NULL;
4944 res = gst_task_set_state (task, GST_TASK_STOPPED);
4945 GST_OBJECT_UNLOCK (pad);
4947 GST_PAD_STREAM_LOCK (pad);
4948 GST_PAD_STREAM_UNLOCK (pad);
4950 if (!gst_task_join (task))
4953 gst_object_unref (task);
4959 GST_DEBUG_OBJECT (pad, "no task");
4960 GST_OBJECT_UNLOCK (pad);
4962 GST_PAD_STREAM_LOCK (pad);
4963 GST_PAD_STREAM_UNLOCK (pad);
4965 /* this is not an error */
4970 /* this is bad, possibly the application tried to join the task from
4971 * the task's thread. We install the task again so that it will be stopped
4972 * again from the right thread next time hopefully. */
4973 GST_OBJECT_LOCK (pad);
4974 GST_DEBUG_OBJECT (pad, "join failed");
4975 /* we can only install this task if there was no other task */
4976 if (GST_PAD_TASK (pad) == NULL)
4977 GST_PAD_TASK (pad) = task;
4978 GST_OBJECT_UNLOCK (pad);