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];
113 guint probe_list_cookie;
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
128 GstPadProbeType mask;
136 static void gst_pad_dispose (GObject * object);
137 static void gst_pad_finalize (GObject * object);
138 static void gst_pad_set_property (GObject * object, guint prop_id,
139 const GValue * value, GParamSpec * pspec);
140 static void gst_pad_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec);
143 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
144 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
145 static gboolean gst_pad_activate_default (GstPad * pad);
146 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
147 static void gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps);
148 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
149 GstBufferList * list);
151 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
153 static GParamSpec *pspec_caps = NULL;
155 /* quarks for probe signals */
156 static GQuark buffer_quark;
157 static GQuark buffer_list_quark;
158 static GQuark event_quark;
167 static GstFlowQuarks flow_quarks[] = {
168 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
169 {GST_FLOW_RESEND, "resend", 0},
170 {GST_FLOW_OK, "ok", 0},
171 {GST_FLOW_NOT_LINKED, "not-linked", 0},
172 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
173 {GST_FLOW_EOS, "eos", 0},
174 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
175 {GST_FLOW_ERROR, "error", 0},
176 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
177 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
182 * @ret: a #GstFlowReturn to get the name of.
184 * Gets a string representing the given flow return.
186 * Returns: a static string with the name of the flow return.
189 gst_flow_get_name (GstFlowReturn ret)
193 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
195 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
196 if (ret == flow_quarks[i].ret)
197 return flow_quarks[i].name;
204 * @ret: a #GstFlowReturn to get the quark of.
206 * Get the unique quark for the given GstFlowReturn.
208 * Returns: the quark associated with the flow return or 0 if an
209 * invalid return was specified.
212 gst_flow_to_quark (GstFlowReturn ret)
216 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
218 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
219 if (ret == flow_quarks[i].ret)
220 return flow_quarks[i].quark;
229 buffer_quark = g_quark_from_static_string ("buffer"); \
230 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
231 event_quark = g_quark_from_static_string ("event"); \
233 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
234 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
237 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
241 #define gst_pad_parent_class parent_class
242 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
245 gst_pad_class_init (GstPadClass * klass)
247 GObjectClass *gobject_class;
248 GstObjectClass *gstobject_class;
250 gobject_class = G_OBJECT_CLASS (klass);
251 gstobject_class = GST_OBJECT_CLASS (klass);
253 g_type_class_add_private (klass, sizeof (GstPadPrivate));
255 gobject_class->dispose = gst_pad_dispose;
256 gobject_class->finalize = gst_pad_finalize;
257 gobject_class->set_property = gst_pad_set_property;
258 gobject_class->get_property = gst_pad_get_property;
262 * @pad: the pad that emitted the signal
263 * @peer: the peer pad that has been connected
265 * Signals that a pad has been linked to the peer pad.
267 gst_pad_signals[PAD_LINKED] =
268 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
270 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
273 * @pad: the pad that emitted the signal
274 * @peer: the peer pad that has been disconnected
276 * Signals that a pad has been unlinked from the peer pad.
278 gst_pad_signals[PAD_UNLINKED] =
279 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
281 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
283 pspec_caps = g_param_spec_boxed ("caps", "Caps",
284 "The capabilities of the pad", GST_TYPE_CAPS,
285 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
286 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
288 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
289 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
290 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
293 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
294 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
295 g_param_spec_object ("template", "Template",
296 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 gstobject_class->path_string_separator = ".";
301 /* Register common function pointer descriptions */
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
308 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
309 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_fixate_caps_default);
313 gst_pad_init (GstPad * pad)
315 pad->priv = GST_PAD_GET_PRIVATE (pad);
317 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
319 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
320 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
321 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
322 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
323 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
324 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
325 GST_PAD_FIXATECAPSFUNC (pad) = gst_pad_fixate_caps_default;
326 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
328 GST_PAD_SET_FLUSHING (pad);
330 g_static_rec_mutex_init (&pad->stream_rec_lock);
332 pad->block_cond = g_cond_new ();
334 g_hook_list_init (&pad->probes, sizeof (GstProbe));
338 clear_event (PadEvent events[], guint idx)
340 gst_event_replace (&events[idx].event, NULL);
341 gst_event_replace (&events[idx].pending, NULL);
344 /* called when setting the pad inactive. It removes all sticky events from
347 clear_events (PadEvent events[])
351 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
352 clear_event (events, i);
355 /* The sticky event with @idx from the srcpad is copied to the
356 * pending event on the sinkpad (when different).
357 * This function applies the pad offsets in case of segment events.
358 * This will make sure that we send the event to the sinkpad event
359 * function when the next buffer of event arrives.
360 * Should be called with the OBJECT lock of both pads.
361 * This function returns TRUE when there is a pending event on the
364 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
366 PadEvent *srcev, *sinkev;
368 gboolean pending = FALSE;
370 srcev = &srcpad->priv->events[idx];
372 if ((event = srcev->event)) {
373 sinkev = &sinkpad->priv->events[idx];
375 switch (GST_EVENT_TYPE (event)) {
376 case GST_EVENT_SEGMENT:
381 offset = srcpad->offset + sinkpad->offset;
383 gst_event_copy_segment (event, &segment);
384 /* adjust the base time. FIXME, check negative times, try to tweak the
385 * start to do clipping on negative times */
386 segment.base += offset;
387 /* make a new event from the updated segment */
388 event = gst_event_new_segment (&segment);
395 if (sinkev->event != event) {
396 /* put in the pending entry when different */
397 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, srcpad,
398 "Putting event %p (%s) on pad %s:%s", event,
399 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (sinkpad));
400 gst_event_replace (&sinkev->pending, event);
409 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
414 /* make sure we push the events from the source to this new peer, for this we
415 * copy the events on the sinkpad and mark EVENTS_PENDING */
417 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
418 pending |= replace_event (srcpad, sinkpad, i);
420 /* we had some new pending events, set our flag */
422 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
425 /* should be called with the OBJECT_LOCK */
427 get_pad_caps (GstPad * pad)
429 GstCaps *caps = NULL;
433 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
434 /* we can only use the caps when we have successfully send the caps
435 * event to the event function and is thus in the active entry */
436 if ((event = pad->priv->events[idx].event))
437 gst_event_parse_caps (event, &caps);
443 gst_pad_dispose (GObject * object)
445 GstPad *pad = GST_PAD_CAST (object);
448 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
450 /* unlink the peer pad */
451 if ((peer = gst_pad_get_peer (pad))) {
452 /* window for MT unsafeness, someone else could unlink here
453 * and then we call unlink with wrong pads. The unlink
454 * function would catch this and safely return failed. */
455 if (GST_PAD_IS_SRC (pad))
456 gst_pad_unlink (pad, peer);
458 gst_pad_unlink (peer, pad);
460 gst_object_unref (peer);
463 gst_pad_set_pad_template (pad, NULL);
465 clear_events (pad->priv->events);
467 g_hook_list_clear (&pad->probes);
469 G_OBJECT_CLASS (parent_class)->dispose (object);
473 gst_pad_finalize (GObject * object)
475 GstPad *pad = GST_PAD_CAST (object);
478 /* in case the task is still around, clean it up */
479 if ((task = GST_PAD_TASK (pad))) {
480 gst_task_join (task);
481 GST_PAD_TASK (pad) = NULL;
482 gst_object_unref (task);
485 g_static_rec_mutex_free (&pad->stream_rec_lock);
486 g_cond_free (pad->block_cond);
488 G_OBJECT_CLASS (parent_class)->finalize (object);
492 gst_pad_set_property (GObject * object, guint prop_id,
493 const GValue * value, GParamSpec * pspec)
495 g_return_if_fail (GST_IS_PAD (object));
498 case PAD_PROP_DIRECTION:
499 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
501 case PAD_PROP_TEMPLATE:
502 gst_pad_set_pad_template (GST_PAD_CAST (object),
503 (GstPadTemplate *) g_value_get_object (value));
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
512 gst_pad_get_property (GObject * object, guint prop_id,
513 GValue * value, GParamSpec * pspec)
515 g_return_if_fail (GST_IS_PAD (object));
519 GST_OBJECT_LOCK (object);
520 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
521 GST_OBJECT_UNLOCK (object);
523 case PAD_PROP_DIRECTION:
524 g_value_set_enum (value, GST_PAD_DIRECTION (object));
526 case PAD_PROP_TEMPLATE:
527 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 * @name: the name of the new pad.
538 * @direction: the #GstPadDirection of the pad.
540 * Creates a new pad with the given name in the given direction.
541 * If name is NULL, a guaranteed unique name (across all pads)
543 * This function makes a copy of the name so you can safely free the name.
545 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
550 gst_pad_new (const gchar * name, GstPadDirection direction)
552 return g_object_new (GST_TYPE_PAD,
553 "name", name, "direction", direction, NULL);
557 * gst_pad_new_from_template:
558 * @templ: the pad template to use
559 * @name: the name of the element
561 * Creates a new pad with the given name from the given template.
562 * If name is NULL, a guaranteed unique name (across all pads)
564 * This function makes a copy of the name so you can safely free the name.
566 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
569 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
571 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
573 return g_object_new (GST_TYPE_PAD,
574 "name", name, "direction", templ->direction, "template", templ, NULL);
578 * gst_pad_new_from_static_template:
579 * @templ: the #GstStaticPadTemplate to use
580 * @name: the name of the element
582 * Creates a new pad with the given name from the given static template.
583 * If name is NULL, a guaranteed unique name (across all pads)
585 * This function makes a copy of the name so you can safely free the name.
587 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
590 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
594 GstPadTemplate *template;
596 template = gst_static_pad_template_get (templ);
597 pad = gst_pad_new_from_template (template, name);
598 gst_object_unref (template);
603 * gst_pad_get_direction:
604 * @pad: a #GstPad to get the direction of.
606 * Gets the direction of the pad. The direction of the pad is
607 * decided at construction time so this function does not take
610 * Returns: the #GstPadDirection of the pad.
615 gst_pad_get_direction (GstPad * pad)
617 GstPadDirection result;
619 /* PAD_UNKNOWN is a little silly but we need some sort of
620 * error return value */
621 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
623 result = GST_PAD_DIRECTION (pad);
629 gst_pad_activate_default (GstPad * pad)
631 return gst_pad_activate_push (pad, TRUE);
635 pre_activate (GstPad * pad, GstPadActivateMode new_mode)
638 case GST_PAD_ACTIVATE_PUSH:
639 case GST_PAD_ACTIVATE_PULL:
640 GST_OBJECT_LOCK (pad);
641 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
643 GST_PAD_UNSET_FLUSHING (pad);
644 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
645 GST_OBJECT_UNLOCK (pad);
647 case GST_PAD_ACTIVATE_NONE:
648 GST_OBJECT_LOCK (pad);
649 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
650 GST_PAD_SET_FLUSHING (pad);
651 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
652 /* unlock blocked pads so element can resume and stop */
653 GST_PAD_BLOCK_BROADCAST (pad);
654 GST_OBJECT_UNLOCK (pad);
660 post_activate (GstPad * pad, GstPadActivateMode new_mode)
663 case GST_PAD_ACTIVATE_PUSH:
664 case GST_PAD_ACTIVATE_PULL:
667 case GST_PAD_ACTIVATE_NONE:
668 /* ensures that streaming stops */
669 GST_PAD_STREAM_LOCK (pad);
670 GST_DEBUG_OBJECT (pad, "stopped streaming");
671 GST_OBJECT_LOCK (pad);
672 clear_events (pad->priv->events);
673 GST_OBJECT_UNLOCK (pad);
674 GST_PAD_STREAM_UNLOCK (pad);
680 * gst_pad_set_active:
681 * @pad: the #GstPad to activate or deactivate.
682 * @active: whether or not the pad should be active.
684 * Activates or deactivates the given pad.
685 * Normally called from within core state change functions.
687 * If @active, makes sure the pad is active. If it is already active, either in
688 * push or pull mode, just return. Otherwise dispatches to the pad's activate
689 * function to perform the actual activation.
691 * If not @active, checks the pad's current mode and calls
692 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
695 * Returns: #TRUE if the operation was successful.
700 gst_pad_set_active (GstPad * pad, gboolean active)
702 GstPadActivateMode old;
703 gboolean ret = FALSE;
705 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
707 GST_OBJECT_LOCK (pad);
708 old = GST_PAD_ACTIVATE_MODE (pad);
709 GST_OBJECT_UNLOCK (pad);
713 case GST_PAD_ACTIVATE_PUSH:
714 GST_DEBUG_OBJECT (pad, "activating pad from push");
717 case GST_PAD_ACTIVATE_PULL:
718 GST_DEBUG_OBJECT (pad, "activating pad from pull");
721 case GST_PAD_ACTIVATE_NONE:
722 GST_DEBUG_OBJECT (pad, "activating pad from none");
723 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
726 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
731 case GST_PAD_ACTIVATE_PUSH:
732 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
733 ret = gst_pad_activate_push (pad, FALSE);
735 case GST_PAD_ACTIVATE_PULL:
736 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
737 ret = gst_pad_activate_pull (pad, FALSE);
739 case GST_PAD_ACTIVATE_NONE:
740 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
744 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
750 GST_OBJECT_LOCK (pad);
752 g_critical ("Failed to deactivate pad %s:%s, very bad",
753 GST_DEBUG_PAD_NAME (pad));
755 GST_WARNING_OBJECT (pad, "Failed to activate pad");
757 GST_OBJECT_UNLOCK (pad);
760 GST_OBJECT_LOCK (pad);
761 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
762 GST_OBJECT_UNLOCK (pad);
770 * gst_pad_activate_pull:
771 * @pad: the #GstPad to activate or deactivate.
772 * @active: whether or not the pad should be active.
774 * Activates or deactivates the given pad in pull mode via dispatching to the
775 * pad's activatepullfunc. For use from within pad activation functions only.
776 * When called on sink pads, will first proxy the call to the peer pad, which
777 * is expected to activate its internally linked pads from within its
778 * activate_pull function.
780 * If you don't know what this is, you probably don't want to call it.
782 * Returns: TRUE if the operation was successful.
787 gst_pad_activate_pull (GstPad * pad, gboolean active)
789 GstPadActivateMode old, new;
792 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
794 GST_OBJECT_LOCK (pad);
795 old = GST_PAD_ACTIVATE_MODE (pad);
796 GST_OBJECT_UNLOCK (pad);
800 case GST_PAD_ACTIVATE_PULL:
801 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
803 case GST_PAD_ACTIVATE_PUSH:
804 GST_DEBUG_OBJECT (pad,
805 "activating pad from push, deactivate push first");
806 /* pad was activate in the wrong direction, deactivate it
807 * and reactivate it in pull mode */
808 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
809 goto deactivate_failed;
810 /* fallthrough, pad is deactivated now. */
811 case GST_PAD_ACTIVATE_NONE:
812 GST_DEBUG_OBJECT (pad, "activating pad from none");
817 case GST_PAD_ACTIVATE_NONE:
818 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
820 case GST_PAD_ACTIVATE_PUSH:
821 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
822 /* pad was activated in the other direction, deactivate it
823 * in push mode, this should not happen... */
824 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
825 goto deactivate_failed;
826 /* everything is fine now */
828 case GST_PAD_ACTIVATE_PULL:
829 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
834 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
835 if ((peer = gst_pad_get_peer (pad))) {
836 GST_DEBUG_OBJECT (pad, "calling peer");
837 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
839 gst_object_unref (peer);
841 /* there is no peer, this is only fatal when we activate. When we
842 * deactivate, we must assume the application has unlinked the peer and
843 * will deactivate it eventually. */
847 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
850 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
851 goto failure; /* Can't activate pull on a src without a
855 new = active ? GST_PAD_ACTIVATE_PULL : GST_PAD_ACTIVATE_NONE;
856 pre_activate (pad, new);
858 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
859 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
862 /* can happen for sinks of passthrough elements */
865 post_activate (pad, new);
867 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
868 active ? "activated" : "deactivated");
874 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
875 active ? "activated" : "deactivated");
880 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
881 "failed to %s in switch to pull from mode %d",
882 (active ? "activate" : "deactivate"), old);
887 GST_OBJECT_LOCK (peer);
888 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
889 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
890 GST_OBJECT_UNLOCK (peer);
891 gst_object_unref (peer);
896 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
902 GST_OBJECT_LOCK (pad);
903 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
904 active ? "activate" : "deactivate");
905 GST_PAD_SET_FLUSHING (pad);
906 GST_PAD_ACTIVATE_MODE (pad) = old;
907 GST_OBJECT_UNLOCK (pad);
913 * gst_pad_activate_push:
914 * @pad: the #GstPad to activate or deactivate.
915 * @active: whether the pad should be active or not.
917 * Activates or deactivates the given pad in push mode via dispatching to the
918 * pad's activatepushfunc. For use from within pad activation functions only.
920 * If you don't know what this is, you probably don't want to call it.
922 * Returns: %TRUE if the operation was successful.
927 gst_pad_activate_push (GstPad * pad, gboolean active)
929 GstPadActivateMode old, new;
931 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
932 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
933 active ? "activated" : "deactivated");
935 GST_OBJECT_LOCK (pad);
936 old = GST_PAD_ACTIVATE_MODE (pad);
937 GST_OBJECT_UNLOCK (pad);
941 case GST_PAD_ACTIVATE_PUSH:
942 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
944 case GST_PAD_ACTIVATE_PULL:
945 GST_DEBUG_OBJECT (pad,
946 "activating pad from push, deactivating pull first");
947 /* pad was activate in the wrong direction, deactivate it
948 * an reactivate it in push mode */
949 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
950 goto deactivate_failed;
951 /* fallthrough, pad is deactivated now. */
952 case GST_PAD_ACTIVATE_NONE:
953 GST_DEBUG_OBJECT (pad, "activating pad from none");
958 case GST_PAD_ACTIVATE_NONE:
959 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
961 case GST_PAD_ACTIVATE_PULL:
962 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
963 /* pad was activated in the other direction, deactivate it
964 * in pull mode, this should not happen... */
965 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
966 goto deactivate_failed;
967 /* everything is fine now */
969 case GST_PAD_ACTIVATE_PUSH:
970 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
975 new = active ? GST_PAD_ACTIVATE_PUSH : GST_PAD_ACTIVATE_NONE;
976 pre_activate (pad, new);
978 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
979 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
983 /* quite ok, element relies on state change func to prepare itself */
986 post_activate (pad, new);
988 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
989 active ? "activated" : "deactivated");
994 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
995 active ? "activated" : "deactivated");
1000 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1001 "failed to %s in switch to push from mode %d",
1002 (active ? "activate" : "deactivate"), old);
1007 GST_OBJECT_LOCK (pad);
1008 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1009 active ? "activate" : "deactivate");
1010 GST_PAD_SET_FLUSHING (pad);
1011 GST_PAD_ACTIVATE_MODE (pad) = old;
1012 GST_OBJECT_UNLOCK (pad);
1018 * gst_pad_is_active:
1019 * @pad: the #GstPad to query
1021 * Query if a pad is active
1023 * Returns: TRUE if the pad is active.
1028 gst_pad_is_active (GstPad * pad)
1030 gboolean result = FALSE;
1032 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1034 GST_OBJECT_LOCK (pad);
1035 result = GST_PAD_IS_ACTIVE (pad);
1036 GST_OBJECT_UNLOCK (pad);
1042 * gst_pad_add_probe:
1043 * @pad: the #GstPad to add the probe to
1044 * @mask: the probe mask
1045 * @callback: #GstPadProbeCallback that will be called with notifications of
1047 * @user_data: (closure): user data passed to the callback
1048 * @destroy_data: #GDestroyNotify for user_data
1050 * Be notified of different states of pads. The provided callback is called for
1051 * every state that matches @mask.
1053 * Returns: an id or 0 on error. The id can be used to remove the probe with
1054 * gst_pad_remove_probe().
1059 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1060 GstPadProbeCallback callback, gpointer user_data,
1061 GDestroyNotify destroy_data)
1066 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1067 g_return_val_if_fail (mask != 0, 0);
1069 GST_OBJECT_LOCK (pad);
1071 /* make a new probe */
1072 hook = g_hook_alloc (&pad->probes);
1074 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1077 /* when no contraints are given for the types, assume all types are
1079 if ((mask & GST_PAD_PROBE_TYPE_DATA_BOTH) == 0)
1080 mask |= GST_PAD_PROBE_TYPE_DATA_BOTH;
1081 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1082 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1084 /* store our flags and other fields */
1085 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1086 hook->func = callback;
1087 hook->data = user_data;
1088 hook->destroy = destroy_data;
1089 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1092 g_hook_prepend (&pad->probes, hook);
1094 /* incremenent cookie so that the new hook get's called */
1095 pad->priv->probe_list_cookie++;
1097 /* get the id of the hook, we return this and it can be used to remove the
1099 res = hook->hook_id;
1101 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1103 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1104 /* we have a block probe */
1106 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1107 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1108 "now %d blocking probes", pad->num_blocked);
1111 /* call the callback if we need to be called for idle callbacks */
1112 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1113 if (pad->priv->using > 0) {
1114 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1115 * flag above, the last thread to leave the push will do the callback. New
1116 * threads going into the push will block. */
1117 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1118 "pad is in use, delay idle callback");
1119 GST_OBJECT_UNLOCK (pad);
1121 /* the pad is idle now, we can signal the idle callback now */
1122 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1123 "pad is idle, trigger idle callback");
1124 GST_OBJECT_UNLOCK (pad);
1126 callback (pad, GST_PAD_PROBE_TYPE_IDLE, NULL, user_data);
1129 GST_OBJECT_UNLOCK (pad);
1135 cleanup_hook (GstPad * pad, GHook * hook)
1137 GstPadProbeType type;
1139 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1141 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1142 /* unblock when we remove the last blocking probe */
1144 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1146 if (pad->num_blocked == 0) {
1147 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1148 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1149 GST_PAD_BLOCK_BROADCAST (pad);
1152 g_hook_destroy_link (&pad->probes, hook);
1157 * gst_pad_remove_probe:
1158 * @pad: the #GstPad with the probe
1159 * @id: the probe id to remove
1161 * Remove the probe with @id from @pad.
1166 gst_pad_remove_probe (GstPad * pad, gulong id)
1170 g_return_if_fail (GST_IS_PAD (pad));
1172 GST_OBJECT_LOCK (pad);
1174 hook = g_hook_get (&pad->probes, id);
1178 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1180 cleanup_hook (pad, hook);
1181 GST_OBJECT_UNLOCK (pad);
1187 GST_OBJECT_UNLOCK (pad);
1188 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1194 * gst_pad_is_blocked:
1195 * @pad: the #GstPad to query
1197 * Checks if the pad is blocked or not. This function returns the
1198 * last requested state of the pad. It is not certain that the pad
1199 * is actually blocking at this point (see gst_pad_is_blocking()).
1201 * Returns: TRUE if the pad is blocked.
1206 gst_pad_is_blocked (GstPad * pad)
1208 gboolean result = FALSE;
1210 g_return_val_if_fail (GST_IS_PAD (pad), result);
1212 GST_OBJECT_LOCK (pad);
1213 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1214 GST_OBJECT_UNLOCK (pad);
1220 * gst_pad_is_blocking:
1221 * @pad: the #GstPad to query
1223 * Checks if the pad is blocking or not. This is a guaranteed state
1224 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1226 * Returns: TRUE if the pad is blocking.
1233 gst_pad_is_blocking (GstPad * pad)
1235 gboolean result = FALSE;
1237 g_return_val_if_fail (GST_IS_PAD (pad), result);
1239 GST_OBJECT_LOCK (pad);
1240 /* the blocking flag is only valid if the pad is not flushing */
1241 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1242 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1243 GST_OBJECT_UNLOCK (pad);
1249 * gst_pad_check_reconfigure:
1250 * @pad: the #GstPad to check
1252 * Check and clear the #GST_PAD_NEED_RECONFIGURE flag on @pad and return %TRUE
1253 * if the flag was set.
1255 * Returns: %TRUE is the GST_PAD_NEED_RECONFIGURE flag was set on @pad.
1258 gst_pad_check_reconfigure (GstPad * pad)
1260 gboolean reconfigure;
1262 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1264 GST_OBJECT_LOCK (pad);
1265 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1266 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
1267 GST_OBJECT_UNLOCK (pad);
1273 * gst_pad_mark_reconfigure:
1274 * @pad: the #GstPad to mark
1276 * Mark a pad for needing reconfiguration. The next call to
1277 * gst_pad_check_reconfigure() will return %TRUE after this call.
1280 gst_pad_mark_reconfigure (GstPad * pad)
1282 g_return_if_fail (GST_IS_PAD (pad));
1284 GST_OBJECT_LOCK (pad);
1285 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
1286 GST_OBJECT_UNLOCK (pad);
1290 * gst_pad_set_activate_function:
1292 * @activate: the #GstPadActivateFunction to set.
1294 * Sets the given activate function for @pad. The activate function will
1295 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1296 * the actual activation. Only makes sense to set on sink pads.
1298 * Call this function if your sink pad can start a pull-based task.
1301 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1303 g_return_if_fail (GST_IS_PAD (pad));
1305 GST_PAD_ACTIVATEFUNC (pad) = activate;
1306 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1307 GST_DEBUG_FUNCPTR_NAME (activate));
1311 * gst_pad_set_activatepull_function:
1313 * @activatepull: the #GstPadActivateModeFunction to set.
1315 * Sets the given activate_pull function for the pad. An activate_pull function
1316 * prepares the element and any upstream connections for pulling. See XXX
1317 * part-activation.txt for details.
1320 gst_pad_set_activatepull_function (GstPad * pad,
1321 GstPadActivateModeFunction activatepull)
1323 g_return_if_fail (GST_IS_PAD (pad));
1325 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1326 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1327 GST_DEBUG_FUNCPTR_NAME (activatepull));
1331 * gst_pad_set_activatepush_function:
1333 * @activatepush: the #GstPadActivateModeFunction to set.
1335 * Sets the given activate_push function for the pad. An activate_push function
1336 * prepares the element for pushing. See XXX part-activation.txt for details.
1339 gst_pad_set_activatepush_function (GstPad * pad,
1340 GstPadActivateModeFunction activatepush)
1342 g_return_if_fail (GST_IS_PAD (pad));
1344 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1345 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1346 GST_DEBUG_FUNCPTR_NAME (activatepush));
1350 * gst_pad_set_chain_function:
1351 * @pad: a sink #GstPad.
1352 * @chain: the #GstPadChainFunction to set.
1354 * Sets the given chain function for the pad. The chain function is called to
1355 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1358 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1360 g_return_if_fail (GST_IS_PAD (pad));
1361 g_return_if_fail (GST_PAD_IS_SINK (pad));
1363 GST_PAD_CHAINFUNC (pad) = chain;
1364 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1365 GST_DEBUG_FUNCPTR_NAME (chain));
1369 * gst_pad_set_chain_list_function:
1370 * @pad: a sink #GstPad.
1371 * @chainlist: the #GstPadChainListFunction to set.
1373 * Sets the given chain list function for the pad. The chainlist function is
1374 * called to process a #GstBufferList input buffer list. See
1375 * #GstPadChainListFunction for more details.
1380 gst_pad_set_chain_list_function (GstPad * pad,
1381 GstPadChainListFunction chainlist)
1383 g_return_if_fail (GST_IS_PAD (pad));
1384 g_return_if_fail (GST_PAD_IS_SINK (pad));
1386 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1387 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1388 GST_DEBUG_FUNCPTR_NAME (chainlist));
1392 * gst_pad_set_getrange_function:
1393 * @pad: a source #GstPad.
1394 * @get: the #GstPadGetRangeFunction to set.
1396 * Sets the given getrange function for the pad. The getrange function is
1397 * called to produce a new #GstBuffer to start the processing pipeline. see
1398 * #GstPadGetRangeFunction for a description of the getrange function.
1401 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1403 g_return_if_fail (GST_IS_PAD (pad));
1404 g_return_if_fail (GST_PAD_IS_SRC (pad));
1406 GST_PAD_GETRANGEFUNC (pad) = get;
1408 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1409 GST_DEBUG_FUNCPTR_NAME (get));
1413 * gst_pad_set_event_function:
1414 * @pad: a #GstPad of either direction.
1415 * @event: the #GstPadEventFunction to set.
1417 * Sets the given event handler for the pad.
1420 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1422 g_return_if_fail (GST_IS_PAD (pad));
1424 GST_PAD_EVENTFUNC (pad) = event;
1426 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1427 GST_DEBUG_FUNCPTR_NAME (event));
1431 * gst_pad_set_query_function:
1432 * @pad: a #GstPad of either direction.
1433 * @query: the #GstPadQueryFunction to set.
1435 * Set the given query function for the pad.
1438 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1440 g_return_if_fail (GST_IS_PAD (pad));
1442 GST_PAD_QUERYFUNC (pad) = query;
1444 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1445 GST_DEBUG_FUNCPTR_NAME (query));
1449 * gst_pad_set_query_type_function:
1450 * @pad: a #GstPad of either direction.
1451 * @type_func: the #GstPadQueryTypeFunction to set.
1453 * Set the given query type function for the pad.
1456 gst_pad_set_query_type_function (GstPad * pad,
1457 GstPadQueryTypeFunction type_func)
1459 g_return_if_fail (GST_IS_PAD (pad));
1461 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1463 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1464 GST_DEBUG_FUNCPTR_NAME (type_func));
1468 * gst_pad_get_query_types:
1471 * Get an array of supported queries that can be performed
1474 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1477 const GstQueryType *
1478 gst_pad_get_query_types (GstPad * pad)
1480 GstPadQueryTypeFunction func;
1482 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1484 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1496 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1500 if ((peer = gst_pad_get_peer (pad))) {
1501 *data = gst_pad_get_query_types (peer);
1502 gst_object_unref (peer);
1508 * gst_pad_get_query_types_default:
1511 * Invoke the default query types function on the pad. This function will get
1512 * the supported query type from the peer of an internally linked pad of @pad.
1514 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1515 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1516 * query types function.
1518 const GstQueryType *
1519 gst_pad_get_query_types_default (GstPad * pad)
1521 GstQueryType *result = NULL;
1523 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1525 gst_pad_forward (pad, (GstPadForwardFunction)
1526 gst_pad_get_query_types_dispatcher, &result);
1532 * gst_pad_set_iterate_internal_links_function:
1533 * @pad: a #GstPad of either direction.
1534 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1536 * Sets the given internal link iterator function for the pad.
1541 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1542 GstPadIterIntLinkFunction iterintlink)
1544 g_return_if_fail (GST_IS_PAD (pad));
1546 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1547 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1548 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1552 * gst_pad_set_link_function:
1554 * @link: the #GstPadLinkFunction to set.
1556 * Sets the given link function for the pad. It will be called when
1557 * the pad is linked with another pad.
1559 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1562 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1563 * cannot be made for some reason.
1565 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1566 * of the peer sink pad, if present.
1569 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1571 g_return_if_fail (GST_IS_PAD (pad));
1573 GST_PAD_LINKFUNC (pad) = link;
1574 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1575 GST_DEBUG_FUNCPTR_NAME (link));
1579 * gst_pad_set_unlink_function:
1581 * @unlink: the #GstPadUnlinkFunction to set.
1583 * Sets the given unlink function for the pad. It will be called
1584 * when the pad is unlinked.
1587 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1589 g_return_if_fail (GST_IS_PAD (pad));
1591 GST_PAD_UNLINKFUNC (pad) = unlink;
1592 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1593 GST_DEBUG_FUNCPTR_NAME (unlink));
1597 * gst_pad_set_getcaps_function:
1599 * @getcaps: the #GstPadGetCapsFunction to set.
1601 * Sets the given getcaps function for the pad. @getcaps should return the
1602 * allowable caps for a pad in the context of the element's state, its link to
1603 * other elements, and the devices or files it has opened. These caps must be a
1604 * subset of the pad template caps. In the NULL state with no links, @getcaps
1605 * should ideally return the same caps as the pad template. In rare
1606 * circumstances, an object property can affect the caps returned by @getcaps,
1607 * but this is discouraged.
1609 * You do not need to call this function if @pad's allowed caps are always the
1610 * same as the pad template caps. This can only be true if the padtemplate
1611 * has fixed simple caps.
1613 * For most filters, the caps returned by @getcaps is directly affected by the
1614 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1615 * the srcpad's getcaps function is directly related to the stream data. Again,
1616 * @getcaps should return the most specific caps it reasonably can, since this
1617 * helps with autoplugging.
1619 * Note that the return value from @getcaps is owned by the caller, so the
1620 * caller should unref the caps after usage.
1623 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1625 g_return_if_fail (GST_IS_PAD (pad));
1627 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1628 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1629 GST_DEBUG_FUNCPTR_NAME (getcaps));
1633 * gst_pad_set_acceptcaps_function:
1635 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1637 * Sets the given acceptcaps function for the pad. The acceptcaps function
1638 * will be called to check if the pad can accept the given caps. Setting the
1639 * acceptcaps function to NULL restores the default behaviour of allowing
1640 * any caps that matches the caps from gst_pad_get_caps().
1643 gst_pad_set_acceptcaps_function (GstPad * pad,
1644 GstPadAcceptCapsFunction acceptcaps)
1646 g_return_if_fail (GST_IS_PAD (pad));
1648 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1649 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1650 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1654 * gst_pad_set_fixatecaps_function:
1656 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1658 * Sets the given fixatecaps function for the pad. The fixatecaps function
1659 * will be called whenever the default values for a GstCaps needs to be
1663 gst_pad_set_fixatecaps_function (GstPad * pad,
1664 GstPadFixateCapsFunction fixatecaps)
1666 g_return_if_fail (GST_IS_PAD (pad));
1668 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1669 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1670 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1675 * @srcpad: the source #GstPad to unlink.
1676 * @sinkpad: the sink #GstPad to unlink.
1678 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1679 * signal on both pads.
1681 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1682 * the pads were not linked together.
1687 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1689 gboolean result = FALSE;
1690 GstElement *parent = NULL;
1693 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1694 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1695 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1696 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1698 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1699 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1700 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1702 /* We need to notify the parent before taking any pad locks as the bin in
1703 * question might be waiting for a lock on the pad while holding its lock
1704 * that our message will try to take. */
1705 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1706 if (GST_IS_ELEMENT (parent)) {
1707 gst_element_post_message (parent,
1708 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1709 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1711 gst_object_unref (parent);
1716 GST_OBJECT_LOCK (srcpad);
1717 GST_OBJECT_LOCK (sinkpad);
1719 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1720 goto not_linked_together;
1722 if (GST_PAD_UNLINKFUNC (srcpad)) {
1723 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1725 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1726 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1729 /* first clear peers */
1730 GST_PAD_PEER (srcpad) = NULL;
1731 GST_PAD_PEER (sinkpad) = NULL;
1733 /* clear pending caps if any */
1734 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1735 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1737 GST_OBJECT_UNLOCK (sinkpad);
1738 GST_OBJECT_UNLOCK (srcpad);
1740 /* fire off a signal to each of the pads telling them
1741 * that they've been unlinked */
1742 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1743 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1745 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1746 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1751 if (parent != NULL) {
1752 gst_element_post_message (parent,
1753 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1754 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1755 gst_object_unref (parent);
1760 not_linked_together:
1762 /* we do not emit a warning in this case because unlinking cannot
1763 * be made MT safe.*/
1764 GST_OBJECT_UNLOCK (sinkpad);
1765 GST_OBJECT_UNLOCK (srcpad);
1771 * gst_pad_is_linked:
1772 * @pad: pad to check
1774 * Checks if a @pad is linked to another pad or not.
1776 * Returns: TRUE if the pad is linked, FALSE otherwise.
1781 gst_pad_is_linked (GstPad * pad)
1785 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1787 GST_OBJECT_LOCK (pad);
1788 result = (GST_PAD_PEER (pad) != NULL);
1789 GST_OBJECT_UNLOCK (pad);
1794 /* get the caps from both pads and see if the intersection
1797 * This function should be called with the pad LOCK on both
1801 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1802 GstPadLinkCheck flags)
1804 GstCaps *srccaps = NULL;
1805 GstCaps *sinkcaps = NULL;
1806 gboolean compatible = FALSE;
1808 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1811 /* Doing the expensive caps checking takes priority over only checking the template caps */
1812 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1813 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1814 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1816 /* If one of the two pads doesn't have a template, consider the intersection
1818 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1819 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1823 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1825 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1828 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1830 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1833 /* if we have caps on both pads we can check the intersection. If one
1834 * of the caps is NULL, we return TRUE. */
1835 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1837 gst_caps_unref (srccaps);
1839 gst_caps_unref (sinkcaps);
1843 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1844 gst_caps_unref (srccaps);
1845 gst_caps_unref (sinkcaps);
1848 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1849 (compatible ? "" : "not"));
1854 /* check if the grandparents of both pads are the same.
1855 * This check is required so that we don't try to link
1856 * pads from elements in different bins without ghostpads.
1858 * The LOCK should be held on both pads
1861 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1863 GstObject *psrc, *psink;
1865 psrc = GST_OBJECT_PARENT (src);
1866 psink = GST_OBJECT_PARENT (sink);
1868 /* if one of the pads has no parent, we allow the link */
1869 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1872 /* only care about parents that are elements */
1873 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1874 goto no_element_parent;
1876 /* if the parents are the same, we have a loop */
1877 if (G_UNLIKELY (psrc == psink))
1880 /* if they both have a parent, we check the grandparents. We can not lock
1881 * the parent because we hold on the child (pad) and the locking order is
1882 * parent >> child. */
1883 psrc = GST_OBJECT_PARENT (psrc);
1884 psink = GST_OBJECT_PARENT (psink);
1886 /* if they have grandparents but they are not the same */
1887 if (G_UNLIKELY (psrc != psink))
1888 goto wrong_grandparents;
1895 GST_CAT_DEBUG (GST_CAT_CAPS,
1896 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1897 GST_PTR_FORMAT, psrc, psink);
1902 GST_CAT_DEBUG (GST_CAT_CAPS,
1903 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1904 GST_PTR_FORMAT, psrc, psink);
1909 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1915 GST_CAT_DEBUG (GST_CAT_CAPS,
1916 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1917 GST_PTR_FORMAT, psrc, psink);
1922 /* FIXME leftover from an attempt at refactoring... */
1923 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1924 * the two pads will be locked in the srcpad, sinkpad order. */
1925 static GstPadLinkReturn
1926 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1928 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1929 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1931 GST_OBJECT_LOCK (srcpad);
1933 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1934 goto src_was_linked;
1936 GST_OBJECT_LOCK (sinkpad);
1938 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1939 goto sink_was_linked;
1941 /* check hierarchy, pads can only be linked if the grandparents
1943 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1944 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1945 goto wrong_hierarchy;
1947 /* check pad caps for non-empty intersection */
1948 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1951 /* FIXME check pad scheduling for non-empty intersection */
1953 return GST_PAD_LINK_OK;
1957 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1958 GST_DEBUG_PAD_NAME (srcpad),
1959 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1960 /* we do not emit a warning in this case because unlinking cannot
1961 * be made MT safe.*/
1962 GST_OBJECT_UNLOCK (srcpad);
1963 return GST_PAD_LINK_WAS_LINKED;
1967 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1968 GST_DEBUG_PAD_NAME (sinkpad),
1969 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1970 /* we do not emit a warning in this case because unlinking cannot
1971 * be made MT safe.*/
1972 GST_OBJECT_UNLOCK (sinkpad);
1973 GST_OBJECT_UNLOCK (srcpad);
1974 return GST_PAD_LINK_WAS_LINKED;
1978 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1979 GST_OBJECT_UNLOCK (sinkpad);
1980 GST_OBJECT_UNLOCK (srcpad);
1981 return GST_PAD_LINK_WRONG_HIERARCHY;
1985 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1986 GST_OBJECT_UNLOCK (sinkpad);
1987 GST_OBJECT_UNLOCK (srcpad);
1988 return GST_PAD_LINK_NOFORMAT;
1994 * @srcpad: the source #GstPad.
1995 * @sinkpad: the sink #GstPad.
1997 * Checks if the source pad and the sink pad are compatible so they can be
2000 * Returns: TRUE if the pads can be linked.
2003 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2005 GstPadLinkReturn result;
2007 /* generic checks */
2008 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2009 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2011 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2012 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2014 /* gst_pad_link_prepare does everything for us, we only release the locks
2015 * on the pads that it gets us. If this function returns !OK the locks are not
2017 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2018 if (result != GST_PAD_LINK_OK)
2021 GST_OBJECT_UNLOCK (srcpad);
2022 GST_OBJECT_UNLOCK (sinkpad);
2025 return result == GST_PAD_LINK_OK;
2029 * gst_pad_link_full:
2030 * @srcpad: the source #GstPad to link.
2031 * @sinkpad: the sink #GstPad to link.
2032 * @flags: the checks to validate when linking
2034 * Links the source pad and the sink pad.
2036 * This variant of #gst_pad_link provides a more granular control on the
2037 * checks being done when linking. While providing some considerable speedups
2038 * the caller of this method must be aware that wrong usage of those flags
2039 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2040 * for more information.
2044 * Returns: A result code indicating if the connection worked or
2050 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2052 GstPadLinkReturn result;
2054 GstPadLinkFunction srcfunc, sinkfunc;
2056 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2057 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2058 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2059 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2060 GST_PAD_LINK_WRONG_DIRECTION);
2062 /* Notify the parent early. See gst_pad_unlink for details. */
2063 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2064 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2065 gst_element_post_message (parent,
2066 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2067 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2069 gst_object_unref (parent);
2074 /* prepare will also lock the two pads */
2075 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2077 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2080 /* must set peers before calling the link function */
2081 GST_PAD_PEER (srcpad) = sinkpad;
2082 GST_PAD_PEER (sinkpad) = srcpad;
2084 /* make sure we update events */
2085 prepare_event_update (srcpad, sinkpad);
2087 /* get the link functions */
2088 srcfunc = GST_PAD_LINKFUNC (srcpad);
2089 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2091 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2092 /* custom link functions, execute them */
2093 GST_OBJECT_UNLOCK (sinkpad);
2094 GST_OBJECT_UNLOCK (srcpad);
2097 /* this one will call the peer link function */
2098 result = srcfunc (srcpad, sinkpad);
2099 } else if (sinkfunc) {
2100 /* if no source link function, we need to call the sink link
2101 * function ourselves. */
2102 result = sinkfunc (sinkpad, srcpad);
2105 GST_OBJECT_LOCK (srcpad);
2106 GST_OBJECT_LOCK (sinkpad);
2108 /* we released the lock, check if the same pads are linked still */
2109 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2110 goto concurrent_link;
2112 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2115 GST_OBJECT_UNLOCK (sinkpad);
2116 GST_OBJECT_UNLOCK (srcpad);
2118 /* fire off a signal to each of the pads telling them
2119 * that they've been linked */
2120 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2121 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2123 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2124 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2126 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2129 if (G_LIKELY (parent)) {
2130 gst_element_post_message (parent,
2131 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2132 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2133 gst_object_unref (parent);
2141 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2142 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2143 GST_OBJECT_UNLOCK (sinkpad);
2144 GST_OBJECT_UNLOCK (srcpad);
2146 /* The other link operation succeeded first */
2147 result = GST_PAD_LINK_WAS_LINKED;
2152 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2153 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2155 GST_PAD_PEER (srcpad) = NULL;
2156 GST_PAD_PEER (sinkpad) = NULL;
2158 GST_OBJECT_UNLOCK (sinkpad);
2159 GST_OBJECT_UNLOCK (srcpad);
2167 * @srcpad: the source #GstPad to link.
2168 * @sinkpad: the sink #GstPad to link.
2170 * Links the source pad and the sink pad.
2172 * Returns: A result code indicating if the connection worked or
2178 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2180 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2184 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2186 GstPadTemplate **template_p;
2188 /* this function would need checks if it weren't static */
2190 GST_OBJECT_LOCK (pad);
2191 template_p = &pad->padtemplate;
2192 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2193 GST_OBJECT_UNLOCK (pad);
2196 gst_pad_template_pad_created (templ, pad);
2200 * gst_pad_get_pad_template:
2203 * Gets the template for @pad.
2205 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2206 * instantiated, or %NULL if this pad has no template. Unref after
2210 gst_pad_get_pad_template (GstPad * pad)
2212 GstPadTemplate *templ;
2214 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2216 templ = GST_PAD_PAD_TEMPLATE (pad);
2218 return (templ ? gst_object_ref (templ) : NULL);
2222 caps_with_getcaps (GstPad * pad, GstCaps * filter)
2226 if (GST_PAD_GETCAPSFUNC (pad) == NULL)
2229 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2230 "dispatching to pad getcaps function with "
2231 "filter %" GST_PTR_FORMAT, filter);
2233 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2234 GST_OBJECT_UNLOCK (pad);
2235 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2236 GST_OBJECT_LOCK (pad);
2237 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2239 if (G_UNLIKELY (result == NULL))
2242 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2243 "pad getcaps returned %" GST_PTR_FORMAT, result);
2245 #ifndef G_DISABLE_ASSERT
2246 /* check that the returned caps are a real subset of the template caps */
2247 if (GST_PAD_PAD_TEMPLATE (pad)) {
2248 const GstCaps *templ_caps =
2249 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2250 if (!gst_caps_is_subset (result, templ_caps)) {
2253 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2254 "pad returned caps %" GST_PTR_FORMAT
2255 " which are not a real subset of its template caps %"
2256 GST_PTR_FORMAT, result, templ_caps);
2258 ("pad %s:%s returned caps which are not a real "
2259 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2260 temp = gst_caps_intersect (templ_caps, result);
2261 gst_caps_unref (result);
2266 if (!gst_caps_is_subset (result, filter)) {
2269 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2270 "pad returned caps %" GST_PTR_FORMAT
2271 " which are not a real subset of the filter caps %"
2272 GST_PTR_FORMAT, result, filter);
2273 g_warning ("pad %s:%s returned caps which are not a real "
2274 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2275 /* FIXME: Order? But shouldn't happen anyway... */
2276 temp = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2277 gst_caps_unref (result);
2288 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2289 GST_DEBUG_PAD_NAME (pad));
2294 /* should be called with the pad LOCK held */
2295 /* refs the caps, so caller is responsible for getting it unreffed */
2297 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2299 GstCaps *result = NULL;
2300 GstPadTemplate *templ;
2301 gboolean fixed_caps;
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2305 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2308 /* fixed caps, try the negotiated caps first */
2309 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2310 if ((result = get_pad_caps (pad)))
2314 /* try the getcaps function next */
2315 if ((result = caps_with_getcaps (pad, filter)))
2318 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2319 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2320 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2325 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2326 "non-fixed pad caps: trying pad caps");
2327 /* non fixed caps, try the negotiated caps */
2328 if ((result = get_pad_caps (pad)))
2332 /* this almost never happens */
2333 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2334 result = gst_caps_new_empty ();
2338 /* run the filter on the result */
2340 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2341 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2342 GST_PTR_FORMAT, result, result, filter, filter);
2343 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2344 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2347 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2348 "using caps %p %" GST_PTR_FORMAT, result, result);
2349 result = gst_caps_ref (result);
2356 * gst_pad_has_current_caps:
2357 * @pad: a #GstPad to check
2359 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2361 * Returns: TRUE when @pad has caps associated with it.
2364 gst_pad_has_current_caps (GstPad * pad)
2369 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2371 GST_OBJECT_LOCK (pad);
2372 caps = get_pad_caps (pad);
2373 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2374 "check current pad caps %" GST_PTR_FORMAT, caps);
2375 result = (caps != NULL);
2376 GST_OBJECT_UNLOCK (pad);
2382 * gst_pad_get_current_caps:
2383 * @pad: a #GstPad to get the current capabilities of.
2385 * Gets the capabilities currently configured on @pad with the last
2386 * #GST_EVENT_CAPS event.
2388 * Returns: the current caps of the pad with incremented ref-count.
2391 gst_pad_get_current_caps (GstPad * pad)
2395 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2397 GST_OBJECT_LOCK (pad);
2398 if ((result = get_pad_caps (pad)))
2399 gst_caps_ref (result);
2400 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2401 "get current pad caps %" GST_PTR_FORMAT, result);
2402 GST_OBJECT_UNLOCK (pad);
2409 * @pad: a #GstPad to get the capabilities of.
2410 * @filter: suggested #GstCaps.
2412 * Gets the capabilities this pad can produce or consume.
2413 * Note that this method doesn't necessarily return the caps set by
2414 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2415 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2416 * the pad's get_caps function;
2417 * this returns the pad template caps if not explicitly set.
2419 * When called on sinkpads @filter contains the caps that
2420 * upstream could produce in the order preferred by upstream. When
2421 * called on srcpads @filter contains the caps accepted by
2422 * downstream in the preffered order. @filter might be %NULL but
2423 * if it is not %NULL the returned caps will be a subset of @filter.
2425 * Note that this function does not return writable #GstCaps, use
2426 * gst_caps_make_writable() before modifying the caps.
2428 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2431 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2433 GstCaps *result = NULL;
2435 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2436 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2438 GST_OBJECT_LOCK (pad);
2440 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2442 result = gst_pad_get_caps_unlocked (pad, filter);
2444 GST_OBJECT_UNLOCK (pad);
2451 * gst_pad_peer_get_caps:
2452 * @pad: a #GstPad to get the capabilities of.
2453 * @filter: a #GstCaps filter.
2455 * Gets the capabilities of the peer connected to this pad. Similar to
2456 * gst_pad_get_caps().
2458 * When called on srcpads @filter contains the caps that
2459 * upstream could produce in the order preferred by upstream. When
2460 * called on sinkpads @filter contains the caps accepted by
2461 * downstream in the preffered order. @filter might be %NULL but
2462 * if it is not %NULL the returned caps will be a subset of @filter.
2464 * Returns: the caps of the peer pad with incremented ref-count. This function
2465 * returns %NULL when there is no peer pad.
2468 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2471 GstCaps *result = NULL;
2473 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2474 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2476 GST_OBJECT_LOCK (pad);
2478 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2480 peerpad = GST_PAD_PEER (pad);
2481 if (G_UNLIKELY (peerpad == NULL))
2484 gst_object_ref (peerpad);
2485 GST_OBJECT_UNLOCK (pad);
2487 result = gst_pad_get_caps (peerpad, filter);
2489 gst_object_unref (peerpad);
2495 GST_OBJECT_UNLOCK (pad);
2501 gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps)
2503 /* default fixation */
2504 gst_caps_fixate (caps);
2508 * gst_pad_fixate_caps:
2509 * @pad: a #GstPad to fixate
2510 * @caps: the #GstCaps to fixate
2512 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2513 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2516 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2518 GstPadFixateCapsFunction fixatefunc;
2520 g_return_if_fail (GST_IS_PAD (pad));
2521 g_return_if_fail (caps != NULL);
2522 g_return_if_fail (!gst_caps_is_empty (caps));
2523 g_return_if_fail (!gst_caps_is_any (caps));
2525 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2528 g_return_if_fail (gst_caps_is_writable (caps));
2530 if (G_LIKELY ((fixatefunc = GST_PAD_FIXATECAPSFUNC (pad))))
2531 fixatefunc (pad, caps);
2534 /* Default accept caps implementation just checks against
2535 * against the allowed caps for the pad */
2537 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2539 /* get the caps and see if it intersects to something not empty */
2541 gboolean result = FALSE;
2543 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2545 allowed = gst_pad_get_caps (pad, NULL);
2547 goto nothing_allowed;
2549 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2551 result = gst_caps_is_subset (caps, allowed);
2553 gst_caps_unref (allowed);
2560 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2566 * gst_pad_accept_caps:
2567 * @pad: a #GstPad to check
2568 * @caps: a #GstCaps to check on the pad
2570 * Check if the given pad accepts the caps.
2572 * Returns: TRUE if the pad can accept the caps.
2575 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2578 GstPadAcceptCapsFunction acceptfunc;
2580 GstCaps *existing = NULL;
2583 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2585 /* any pad can be unnegotiated */
2589 /* lock for checking the existing caps */
2590 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2592 GST_OBJECT_LOCK (pad);
2593 /* The current caps on a pad are trivially acceptable */
2594 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2595 if (caps == existing || gst_caps_is_equal (caps, existing))
2598 GST_OBJECT_UNLOCK (pad);
2600 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2602 /* Only null if the element explicitly unset it */
2603 if (G_UNLIKELY (acceptfunc == NULL))
2606 /* we can call the function */
2607 result = acceptfunc (pad, caps);
2608 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2615 GST_DEBUG_OBJECT (pad, "pad had same caps");
2616 GST_OBJECT_UNLOCK (pad);
2622 GST_DEBUG_OBJECT (pad, "no acceptcaps function");
2628 * gst_pad_peer_accept_caps:
2629 * @pad: a #GstPad to check the peer of
2630 * @caps: a #GstCaps to check on the pad
2632 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2635 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2638 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2643 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2645 GST_OBJECT_LOCK (pad);
2647 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2649 peerpad = GST_PAD_PEER (pad);
2650 if (G_UNLIKELY (peerpad == NULL))
2653 gst_object_ref (peerpad);
2654 /* release lock before calling external methods but keep ref to pad */
2655 GST_OBJECT_UNLOCK (pad);
2657 result = gst_pad_accept_caps (peerpad, caps);
2659 gst_object_unref (peerpad);
2665 GST_OBJECT_UNLOCK (pad);
2672 * @pad: a #GstPad to set the capabilities of.
2673 * @caps: (transfer none): a #GstCaps to set.
2675 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2676 * caps on the pad will be unreffed. This function refs the caps so you should
2677 * unref if as soon as you don't need it anymore.
2678 * It is possible to set NULL caps, which will make the pad unnegotiated
2681 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2682 * or bad parameters were provided to this function.
2687 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2690 gboolean res = TRUE;
2692 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2693 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2695 event = gst_event_new_caps (caps);
2697 if (GST_PAD_IS_SRC (pad))
2698 res = gst_pad_push_event (pad, event);
2700 res = gst_pad_send_event (pad, event);
2706 do_event_function (GstPad * pad, GstEvent * event,
2707 GstPadEventFunction eventfunc, gboolean * caps_notify)
2709 gboolean result = TRUE, call_event = TRUE;
2710 GstCaps *caps, *old, *templ;
2712 switch (GST_EVENT_TYPE (event)) {
2713 case GST_EVENT_CAPS:
2715 /* backwards compatibility mode for caps */
2716 gst_event_parse_caps (event, &caps);
2718 /* See if pad accepts the caps */
2719 templ = gst_pad_get_pad_template_caps (pad);
2720 if (!gst_caps_is_subset (caps, templ))
2723 /* check if it changed */
2724 if ((old = gst_pad_get_current_caps (pad))) {
2725 call_event = !gst_caps_is_equal (caps, old);
2726 gst_caps_unref (old);
2729 *caps_notify = TRUE;
2730 gst_caps_unref (templ);
2738 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2739 result = eventfunc (pad, event);
2741 gst_event_unref (event);
2748 gst_caps_unref (templ);
2749 gst_event_unref (event);
2750 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2751 "caps %" GST_PTR_FORMAT " not accepted", caps);
2756 /* function to send all pending events on the sinkpad to the event
2757 * function and collect the results. This function should be called with
2758 * the object lock. The object lock might be released by this function.
2760 static GstFlowReturn
2761 gst_pad_update_events (GstPad * pad)
2763 GstFlowReturn ret = GST_FLOW_OK;
2765 GstPadEventFunction eventfunc;
2767 gboolean caps_notify = FALSE;
2769 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2772 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2776 ev = &pad->priv->events[i];
2778 /* skip without pending event */
2779 if ((event = gst_event_steal (&ev->pending)) == NULL)
2782 gst_event_ref (event);
2783 GST_OBJECT_UNLOCK (pad);
2785 res = do_event_function (pad, event, eventfunc, &caps_notify);
2787 /* things could have changed while we release the lock, check if we still
2788 * are handling the same event, if we don't something changed and we have
2789 * to try again. FIXME. we need a cookie here. FIXME, we also want to remove
2790 * that lock eventually and then do the retry elsewhere. */
2793 /* make the event active */
2794 gst_event_take (&ev->event, event);
2796 /* notify caps change when needed */
2798 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2799 caps_notify = FALSE;
2802 gst_event_unref (event);
2803 ret = GST_FLOW_ERROR;
2805 GST_OBJECT_LOCK (pad);
2807 /* when we get here all events were successfully updated. */
2813 g_warning ("pad %s:%s has no event handler, file a bug.",
2814 GST_DEBUG_PAD_NAME (pad));
2815 return GST_FLOW_NOT_SUPPORTED;
2820 * gst_pad_get_pad_template_caps:
2821 * @pad: a #GstPad to get the template capabilities from.
2823 * Gets the capabilities for @pad's template.
2825 * Returns: (transfer full): the #GstCaps of this pad template.
2826 * Unref after usage.
2829 gst_pad_get_pad_template_caps (GstPad * pad)
2831 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2833 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2835 if (GST_PAD_PAD_TEMPLATE (pad))
2836 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2838 return gst_static_caps_get (&anycaps);
2843 * @pad: a #GstPad to get the peer of.
2845 * Gets the peer of @pad. This function refs the peer pad so
2846 * you need to unref it after use.
2848 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2853 gst_pad_get_peer (GstPad * pad)
2857 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2859 GST_OBJECT_LOCK (pad);
2860 result = GST_PAD_PEER (pad);
2862 gst_object_ref (result);
2863 GST_OBJECT_UNLOCK (pad);
2869 * gst_pad_get_allowed_caps:
2872 * Gets the capabilities of the allowed media types that can flow through
2873 * @pad and its peer.
2875 * The allowed capabilities is calculated as the intersection of the results of
2876 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2877 * on the resulting caps.
2879 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2880 * caps when you no longer need it. This function returns NULL when @pad
2886 gst_pad_get_allowed_caps (GstPad * pad)
2893 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2895 GST_OBJECT_LOCK (pad);
2896 peer = GST_PAD_PEER (pad);
2897 if (G_UNLIKELY (peer == NULL))
2900 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2902 gst_object_ref (peer);
2903 GST_OBJECT_UNLOCK (pad);
2904 mycaps = gst_pad_get_caps (pad, NULL);
2906 peercaps = gst_pad_get_caps (peer, NULL);
2907 gst_object_unref (peer);
2909 caps = gst_caps_intersect (mycaps, peercaps);
2910 gst_caps_unref (peercaps);
2911 gst_caps_unref (mycaps);
2913 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2920 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2921 GST_OBJECT_UNLOCK (pad);
2928 * gst_pad_iterate_internal_links_default:
2929 * @pad: the #GstPad to get the internal links of.
2931 * Iterate the list of pads to which the given pad is linked to inside of
2932 * the parent element.
2933 * This is the default handler, and thus returns an iterator of all of the
2934 * pads inside the parent element with opposite direction.
2936 * The caller must free this iterator after use with gst_iterator_free().
2938 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2939 * returned pad with gst_object_unref().
2944 gst_pad_iterate_internal_links_default (GstPad * pad)
2952 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2957 GST_OBJECT_LOCK (pad);
2958 parent = GST_PAD_PARENT (pad);
2959 if (!parent || !GST_IS_ELEMENT (parent))
2962 gst_object_ref (parent);
2963 GST_OBJECT_UNLOCK (pad);
2965 if (pad->direction == GST_PAD_SRC)
2966 padlist = &parent->sinkpads;
2968 padlist = &parent->srcpads;
2970 GST_DEBUG_OBJECT (pad, "Making iterator");
2972 cookie = &parent->pads_cookie;
2974 lock = GST_OBJECT_GET_LOCK (parent);
2977 res = gst_iterator_new_list (GST_TYPE_PAD,
2978 lock, cookie, padlist, (GObject *) owner, NULL);
2980 gst_object_unref (owner);
2987 GST_OBJECT_UNLOCK (pad);
2988 GST_DEBUG_OBJECT (pad, "no parent element");
2994 * gst_pad_iterate_internal_links:
2995 * @pad: the GstPad to get the internal links of.
2997 * Gets an iterator for the pads to which the given pad is linked to inside
2998 * of the parent element.
3000 * Each #GstPad element yielded by the iterator will have its refcount increased,
3001 * so unref after use.
3003 * Free-function: gst_iterator_free
3005 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3006 * pad does not have an iterator function configured. Use
3007 * gst_iterator_free() after usage.
3012 gst_pad_iterate_internal_links (GstPad * pad)
3014 GstIterator *res = NULL;
3016 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3018 if (GST_PAD_ITERINTLINKFUNC (pad))
3019 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3027 * @forward: a #GstPadForwardFunction
3028 * @user_data: user data passed to @forward
3030 * Calls @forward for all internally linked pads of @pad. This function deals with
3031 * dynamically changing internal pads and will make sure that the @forward
3032 * function is only called once for each pad.
3034 * When @forward returns TRUE, no further pads will be processed.
3036 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3039 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3042 gboolean result = FALSE;
3044 gboolean done = FALSE;
3045 GValue item = { 0, };
3046 GList *pushed_pads = NULL;
3048 iter = gst_pad_iterate_internal_links (pad);
3054 switch (gst_iterator_next (iter, &item)) {
3055 case GST_ITERATOR_OK:
3059 intpad = g_value_get_object (&item);
3061 /* if already pushed, skip. FIXME, find something faster to tag pads */
3062 if (g_list_find (pushed_pads, intpad)) {
3063 g_value_reset (&item);
3067 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3068 GST_DEBUG_PAD_NAME (intpad));
3069 done = result = forward (intpad, user_data);
3071 pushed_pads = g_list_prepend (pushed_pads, intpad);
3073 g_value_reset (&item);
3076 case GST_ITERATOR_RESYNC:
3077 /* We don't reset the result here because we don't push the event
3078 * again on pads that got the event already and because we need
3079 * to consider the result of the previous pushes */
3080 gst_iterator_resync (iter);
3082 case GST_ITERATOR_ERROR:
3083 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3086 case GST_ITERATOR_DONE:
3091 g_value_unset (&item);
3092 gst_iterator_free (iter);
3094 g_list_free (pushed_pads);
3104 gboolean dispatched;
3108 event_forward_func (GstPad * pad, EventData * data)
3110 /* for each pad we send to, we should ref the event; it's up
3111 * to downstream to unref again when handled. */
3112 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3113 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3115 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3117 data->dispatched = TRUE;
3124 * gst_pad_event_default:
3125 * @pad: a #GstPad to call the default event handler on.
3126 * @event: (transfer full): the #GstEvent to handle.
3128 * Invokes the default event handler for the given pad.
3130 * The EOS event will pause the task associated with @pad before it is forwarded
3131 * to all internally linked pads,
3133 * The CAPS event will never be forwarded.
3135 * The the event is sent to all pads internally linked to @pad. This function
3136 * takes ownership of @event.
3138 * Returns: TRUE if the event was sent successfully.
3141 gst_pad_event_default (GstPad * pad, GstEvent * event)
3146 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3147 g_return_val_if_fail (event != NULL, FALSE);
3149 GST_LOG_OBJECT (pad, "default event handler");
3151 switch (GST_EVENT_TYPE (event)) {
3154 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3155 gst_pad_pause_task (pad);
3163 data.dispatched = FALSE;
3164 data.result = FALSE;
3166 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3168 /* for sinkpads without a parent element or without internal links, nothing
3169 * will be dispatched but we still want to return TRUE. */
3170 if (data.dispatched)
3171 result = data.result;
3175 gst_event_unref (event);
3182 * @pad: a #GstPad to invoke the default query on.
3183 * @query: (transfer none): the #GstQuery to perform.
3185 * Dispatches a query to a pad. The query should have been allocated by the
3186 * caller via one of the type-specific allocation functions. The element that
3187 * the pad belongs to is responsible for filling the query with an appropriate
3188 * response, which should then be parsed with a type-specific query parsing
3191 * Again, the caller is responsible for both the allocation and deallocation of
3192 * the query structure.
3194 * Please also note that some queries might need a running pipeline to work.
3196 * Returns: TRUE if the query could be performed.
3199 gst_pad_query (GstPad * pad, GstQuery * query)
3202 GstPadQueryFunction func;
3204 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3205 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3207 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3208 GST_QUERY_TYPE_NAME (query));
3210 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3213 res = func (pad, query);
3215 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3216 GST_QUERY_TYPE_NAME (query), res);
3222 GST_DEBUG_OBJECT (pad, "had no query function");
3228 * gst_pad_peer_query:
3229 * @pad: a #GstPad to invoke the peer query on.
3230 * @query: (transfer none): the #GstQuery to perform.
3232 * Performs gst_pad_query() on the peer of @pad.
3234 * The caller is responsible for both the allocation and deallocation of
3235 * the query structure.
3237 * Returns: TRUE if the query could be performed. This function returns %FALSE
3238 * if @pad has no peer.
3243 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3248 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3249 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3251 GST_OBJECT_LOCK (pad);
3253 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3254 GST_QUERY_TYPE_NAME (query));
3256 peerpad = GST_PAD_PEER (pad);
3257 if (G_UNLIKELY (peerpad == NULL))
3260 gst_object_ref (peerpad);
3261 GST_OBJECT_UNLOCK (pad);
3263 result = gst_pad_query (peerpad, query);
3265 gst_object_unref (peerpad);
3272 GST_WARNING_OBJECT (pad, "pad has no peer");
3273 GST_OBJECT_UNLOCK (pad);
3279 * gst_pad_query_default:
3280 * @pad: a #GstPad to call the default query handler on.
3281 * @query: (transfer none): the #GstQuery to handle.
3283 * Invokes the default query handler for the given pad.
3284 * The query is sent to all pads internally linked to @pad. Note that
3285 * if there are many possible sink pads that are internally linked to
3286 * @pad, only one will be sent the query.
3287 * Multi-sinkpad elements should implement custom query handlers.
3289 * Returns: TRUE if the query was performed successfully.
3292 gst_pad_query_default (GstPad * pad, GstQuery * query)
3294 gboolean forward = TRUE, ret = FALSE;
3296 switch (GST_QUERY_TYPE (query)) {
3297 case GST_QUERY_SCHEDULING:
3300 case GST_QUERY_POSITION:
3301 case GST_QUERY_SEEKING:
3302 case GST_QUERY_FORMATS:
3303 case GST_QUERY_LATENCY:
3304 case GST_QUERY_JITTER:
3305 case GST_QUERY_RATE:
3306 case GST_QUERY_CONVERT:
3307 case GST_QUERY_ALLOCATION:
3313 ret = gst_pad_forward
3314 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
3320 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3322 GstPad *pad = data->pad;
3323 GstPadProbeType flags;
3324 GstPadProbeCallback callback;
3325 GstPadProbeReturn ret;
3327 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3328 "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
3330 /* if we have called this callback, do nothing */
3331 if (PROBE_COOKIE (hook) == data->cookie) {
3332 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3333 "hook %lu, cookie %u already called", hook->hook_id,
3334 PROBE_COOKIE (hook));
3338 PROBE_COOKIE (hook) = data->cookie;
3340 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3342 /* one of the data types */
3343 if ((flags & GST_PAD_PROBE_TYPE_DATA_BOTH & data->mask) == 0)
3345 /* one of the scheduling types */
3346 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3348 /* all of the blocking types must match */
3349 if ((flags & GST_PAD_PROBE_TYPE_BLOCKING) !=
3350 (data->mask & GST_PAD_PROBE_TYPE_BLOCKING))
3353 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3354 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3356 callback = (GstPadProbeCallback) hook->func;
3357 if (callback == NULL)
3360 GST_OBJECT_UNLOCK (pad);
3362 ret = callback (pad, data->mask, data->type_data, hook->data);
3364 GST_OBJECT_LOCK (pad);
3365 data->marshalled = TRUE;
3368 case GST_PAD_PROBE_REMOVE:
3369 /* remove the probe */
3370 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3371 cleanup_hook (pad, hook);
3373 case GST_PAD_PROBE_DROP:
3374 /* need to drop the data, make sure other probes don't get called
3376 GST_DEBUG_OBJECT (pad, "asked to drop item");
3377 data->mask = GST_PAD_PROBE_TYPE_INVALID;
3378 data->dropped = TRUE;
3380 case GST_PAD_PROBE_PASS:
3381 /* inform the pad block to let things pass */
3382 GST_DEBUG_OBJECT (pad, "asked to pass item");
3386 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3393 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3394 "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
3400 #define PROBE_FULL(pad,mask,data,label,defaultval) \
3402 if (G_UNLIKELY (pad->num_probes)) { \
3403 ret = do_probe_callbacks (pad, mask, data, defaultval); \
3404 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3409 #define PROBE(pad,mask,data,label) \
3410 PROBE_FULL(pad, mask, data, label, GST_FLOW_OK);
3412 static GstFlowReturn
3413 do_probe_callbacks (GstPad * pad, GstPadProbeType mask, gpointer type_data,
3414 GstFlowReturn defaultval)
3420 is_block = (mask & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3424 data.type_data = type_data;
3426 data.marshalled = FALSE;
3427 data.dropped = FALSE;
3428 data.cookie = ++pad->priv->probe_cookie;
3431 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3432 "do probes cookie %u", data.cookie);
3433 cookie = pad->priv->probe_list_cookie;
3435 g_hook_list_marshal (&pad->probes, TRUE,
3436 (GHookMarshaller) probe_hook_marshal, &data);
3438 /* if the list changed, call the new callbacks (they will not have their
3439 * cookie set to data.cookie */
3440 if (cookie != pad->priv->probe_list_cookie) {
3441 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3442 "probe list changed, restarting");
3446 /* the first item that dropped will stop the hooks and then we drop here */
3450 /* if no handler matched and we are blocking, let the item pass */
3451 if (!data.marshalled && is_block)
3454 /* At this point, all handlers returned either OK or PASS. If one handler
3455 * returned PASS, let the item pass */
3460 while (GST_PAD_IS_BLOCKED (pad)) {
3461 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3462 "we are blocked %d times", pad->num_blocked);
3464 /* we might have released the lock */
3465 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3468 /* now we block the streaming thread. It can be unlocked when we
3469 * deactivate the pad (which will also set the FLUSHING flag) or
3470 * when the pad is unblocked. A flushing event will also unblock
3471 * the pad after setting the FLUSHING flag. */
3472 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3473 "Waiting to be unblocked or set flushing");
3474 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3475 GST_PAD_BLOCK_WAIT (pad);
3476 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3477 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3479 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3489 GST_DEBUG_OBJECT (pad, "pad is flushing");
3490 return GST_FLOW_WRONG_STATE;
3494 GST_DEBUG_OBJECT (pad, "data is dropped");
3495 return GST_FLOW_CUSTOM_SUCCESS;
3499 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3500 GST_DEBUG_OBJECT (pad, "data is passed");
3508 * gst_pad_get_offset:
3511 * Get the offset applied to the running time of @pad. @pad has to be a source
3514 * Returns: the offset.
3517 gst_pad_get_offset (GstPad * pad)
3521 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3523 GST_OBJECT_LOCK (pad);
3524 result = pad->offset;
3525 GST_OBJECT_UNLOCK (pad);
3531 * gst_pad_set_offset:
3533 * @offset: the offset
3535 * Set the offset that will be applied to the running time of @pad.
3538 gst_pad_set_offset (GstPad * pad, gint64 offset)
3544 g_return_if_fail (GST_IS_PAD (pad));
3546 GST_OBJECT_LOCK (pad);
3547 /* if nothing changed, do nothing */
3548 if (pad->offset == offset)
3551 pad->offset = offset;
3553 /* if no peer, we just updated the offset */
3554 if ((peer = GST_PAD_PEER (pad)) == NULL)
3557 /* switch pads around when dealing with a sinkpad */
3558 if (GST_PAD_IS_SINK (pad)) {
3559 /* ref the peer so it doesn't go away when we release the lock */
3560 tmp = gst_object_ref (peer);
3561 /* make sure we get the peer (the srcpad) */
3562 GST_OBJECT_UNLOCK (pad);
3568 GST_OBJECT_LOCK (pad);
3569 /* check if the pad didn't get relinked */
3570 if (GST_PAD_PEER (pad) != peer)
3573 /* we can release the ref now */
3574 gst_object_unref (peer);
3577 /* the index of the segment event in the array */
3578 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3580 /* lock order is srcpad >> sinkpad */
3581 GST_OBJECT_LOCK (peer);
3582 /* take the current segment event, adjust it and then place
3583 * it on the sinkpad. events on the srcpad are always active. */
3584 if (replace_event (pad, peer, idx))
3585 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3587 GST_OBJECT_UNLOCK (peer);
3590 GST_OBJECT_UNLOCK (pad);
3594 /**********************************************************************
3595 * Data passing functions
3598 /* this is the chain function that does not perform the additional argument
3599 * checking for that little extra speed.
3601 static inline GstFlowReturn
3602 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3605 gboolean needs_events;
3607 GST_PAD_STREAM_LOCK (pad);
3609 GST_OBJECT_LOCK (pad);
3610 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3613 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3614 if (G_UNLIKELY (needs_events)) {
3615 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3617 GST_DEBUG_OBJECT (pad, "need to update all events");
3618 ret = gst_pad_update_events (pad);
3619 if (G_UNLIKELY (ret != GST_FLOW_OK))
3623 PROBE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3625 PROBE (pad, type, data, probe_stopped);
3627 GST_OBJECT_UNLOCK (pad);
3629 /* NOTE: we read the chainfunc unlocked.
3630 * we cannot hold the lock for the pad so we might send
3631 * the data to the wrong function. This is not really a
3632 * problem since functions are assigned at creation time
3633 * and don't change that often... */
3634 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3635 GstPadChainFunction chainfunc;
3637 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3640 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3641 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3642 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3644 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3646 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3647 "called chainfunction &%s with buffer %p, returned %s",
3648 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3650 GstPadChainListFunction chainlistfunc;
3652 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3655 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3656 "calling chainlistfunction &%s",
3657 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3659 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3661 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3662 "called chainlistfunction &%s, returned %s",
3663 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3666 GST_PAD_STREAM_UNLOCK (pad);
3673 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3674 "chaining, but pad was flushing");
3675 GST_OBJECT_UNLOCK (pad);
3676 GST_PAD_STREAM_UNLOCK (pad);
3677 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3678 return GST_FLOW_WRONG_STATE;
3682 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3683 GST_OBJECT_UNLOCK (pad);
3684 GST_PAD_STREAM_UNLOCK (pad);
3685 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3690 GST_OBJECT_UNLOCK (pad);
3691 GST_PAD_STREAM_UNLOCK (pad);
3692 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3695 case GST_FLOW_CUSTOM_SUCCESS:
3696 GST_DEBUG_OBJECT (pad, "dropped buffer");
3700 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3707 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3708 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3709 "pushing, but not chainhandler");
3710 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3711 ("push on pad %s:%s but it has no chainfunction",
3712 GST_DEBUG_PAD_NAME (pad)));
3713 GST_PAD_STREAM_UNLOCK (pad);
3714 return GST_FLOW_NOT_SUPPORTED;
3720 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3721 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3724 * Chain a buffer to @pad.
3726 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3728 * If the buffer type is not acceptable for @pad (as negotiated with a
3729 * preceeding GST_EVENT_CAPS event), this function returns
3730 * #GST_FLOW_NOT_NEGOTIATED.
3732 * The function proceeds calling the chain function installed on @pad (see
3733 * gst_pad_set_chain_function()) and the return value of that function is
3734 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3737 * In all cases, success or failure, the caller loses its reference to @buffer
3738 * after calling this function.
3740 * Returns: a #GstFlowReturn from the pad.
3745 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3747 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3748 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3749 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3751 return gst_pad_chain_data_unchecked (pad,
3752 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3755 static GstFlowReturn
3756 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3762 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3764 len = gst_buffer_list_length (list);
3767 for (i = 0; i < len; i++) {
3768 buffer = gst_buffer_list_get (list, i);
3770 gst_pad_chain_data_unchecked (pad,
3771 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3772 gst_buffer_ref (buffer));
3773 if (ret != GST_FLOW_OK)
3776 gst_buffer_list_unref (list);
3782 * gst_pad_chain_list:
3783 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3784 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3787 * Chain a bufferlist to @pad.
3789 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3791 * If @pad was not negotiated properly with a CAPS event, this function
3792 * returns #GST_FLOW_NOT_NEGOTIATED.
3794 * The function proceeds calling the chainlist function installed on @pad (see
3795 * gst_pad_set_chain_list_function()) and the return value of that function is
3796 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3797 * chainlist function.
3799 * In all cases, success or failure, the caller loses its reference to @list
3800 * after calling this function.
3804 * Returns: a #GstFlowReturn from the pad.
3809 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3811 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3812 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3813 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3815 return gst_pad_chain_data_unchecked (pad,
3816 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3819 static GstFlowReturn
3820 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3825 GST_OBJECT_LOCK (pad);
3826 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3829 /* do block probes */
3830 PROBE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3832 /* do post-blocking probes */
3833 PROBE (pad, type, data, probe_stopped);
3835 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3838 /* take ref to peer pad before releasing the lock */
3839 gst_object_ref (peer);
3841 GST_OBJECT_UNLOCK (pad);
3843 ret = gst_pad_chain_data_unchecked (peer, type, data);
3845 gst_object_unref (peer);
3847 GST_OBJECT_LOCK (pad);
3849 if (pad->priv->using == 0) {
3850 /* pad is not active anymore, trigger idle callbacks */
3851 PROBE_FULL (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
3852 probe_stopped, ret);
3854 GST_OBJECT_UNLOCK (pad);
3858 /* ERROR recovery here */
3862 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3863 "pushing, but pad was flushing");
3864 GST_OBJECT_UNLOCK (pad);
3865 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3866 return GST_FLOW_WRONG_STATE;
3870 GST_OBJECT_UNLOCK (pad);
3871 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3874 case GST_FLOW_CUSTOM_SUCCESS:
3875 GST_DEBUG_OBJECT (pad, "dropped buffer");
3879 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3886 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3887 "pushing, but it was not linked");
3888 GST_OBJECT_UNLOCK (pad);
3889 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3890 return GST_FLOW_NOT_LINKED;
3896 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3897 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3900 * Pushes a buffer to the peer of @pad.
3902 * This function will call installed block probes before triggering any
3903 * installed data probes.
3905 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3906 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3909 * In all cases, success or failure, the caller loses its reference to @buffer
3910 * after calling this function.
3912 * Returns: a #GstFlowReturn from the peer pad.
3917 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3919 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3920 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3921 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3923 return gst_pad_push_data (pad,
3924 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3928 * gst_pad_push_list:
3929 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3930 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3933 * Pushes a buffer list to the peer of @pad.
3935 * This function will call installed block probes before triggering any
3936 * installed data probes.
3938 * The function proceeds calling the chain function on the peer pad and returns
3939 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3940 * be returned. If the peer pad does not have any installed chainlist function
3941 * every group buffer of the list will be merged into a normal #GstBuffer and
3942 * chained via gst_pad_chain().
3944 * In all cases, success or failure, the caller loses its reference to @list
3945 * after calling this function.
3947 * Returns: a #GstFlowReturn from the peer pad.
3954 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3956 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3957 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3958 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3960 return gst_pad_push_data (pad,
3961 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3964 static GstFlowReturn
3965 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3966 GstBuffer ** buffer)
3969 GstPadGetRangeFunction getrangefunc;
3971 GST_PAD_STREAM_LOCK (pad);
3973 GST_OBJECT_LOCK (pad);
3974 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3977 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK, NULL,
3979 GST_OBJECT_UNLOCK (pad);
3981 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3984 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3985 "calling getrangefunc %s, offset %"
3986 G_GUINT64_FORMAT ", size %u",
3987 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3989 ret = getrangefunc (pad, offset, size, buffer);
3991 if (G_UNLIKELY (ret != GST_FLOW_OK))
3992 goto get_range_failed;
3994 /* can only fire the signal if we have a valid buffer */
3995 GST_OBJECT_LOCK (pad);
3996 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, *buffer,
3997 probe_stopped_unref);
3998 GST_OBJECT_UNLOCK (pad);
4000 GST_PAD_STREAM_UNLOCK (pad);
4007 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4008 "getrange, but pad was flushing");
4009 GST_OBJECT_UNLOCK (pad);
4010 GST_PAD_STREAM_UNLOCK (pad);
4011 return GST_FLOW_WRONG_STATE;
4015 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4016 ("getrange on pad %s:%s but it has no getrangefunction",
4017 GST_DEBUG_PAD_NAME (pad)));
4018 GST_PAD_STREAM_UNLOCK (pad);
4019 return GST_FLOW_NOT_SUPPORTED;
4023 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4024 "probe returned %s", gst_flow_get_name (ret));
4025 GST_OBJECT_UNLOCK (pad);
4026 GST_PAD_STREAM_UNLOCK (pad);
4029 probe_stopped_unref:
4031 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4032 "probe returned %s", gst_flow_get_name (ret));
4033 GST_OBJECT_UNLOCK (pad);
4034 GST_PAD_STREAM_UNLOCK (pad);
4035 gst_buffer_unref (*buffer);
4041 GST_PAD_STREAM_UNLOCK (pad);
4043 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4044 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4045 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4051 * gst_pad_get_range:
4052 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4053 * @offset: The start offset of the buffer
4054 * @size: The length of the buffer
4055 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4056 * returns #GST_FLOW_ERROR if %NULL.
4058 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4059 * immediately and @buffer is %NULL.
4061 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4062 * description of a getrange function. If @pad has no getrange function
4063 * installed (see gst_pad_set_getrange_function()) this function returns
4064 * #GST_FLOW_NOT_SUPPORTED.
4066 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4068 * Returns: a #GstFlowReturn from the pad.
4073 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4074 GstBuffer ** buffer)
4076 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4077 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4078 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4080 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4084 * gst_pad_pull_range:
4085 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4086 * @offset: The start offset of the buffer
4087 * @size: The length of the buffer
4088 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4089 * GST_FLOW_ERROR if %NULL.
4091 * Pulls a @buffer from the peer pad.
4093 * This function will first trigger the pad block signal if it was
4096 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4097 * function returns the result of gst_pad_get_range() on the peer pad.
4098 * See gst_pad_get_range() for a list of return values and for the
4099 * semantics of the arguments of this function.
4101 * Returns: a #GstFlowReturn from the peer pad.
4102 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4103 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4104 * @buffer may not be used or freed when any other return value than
4105 * #GST_FLOW_OK is returned.
4110 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4111 GstBuffer ** buffer)
4115 gboolean needs_events;
4117 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4118 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4119 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4121 GST_OBJECT_LOCK (pad);
4122 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4125 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK, NULL,
4128 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4131 gst_object_ref (peer);
4133 GST_OBJECT_UNLOCK (pad);
4135 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4137 gst_object_unref (peer);
4139 GST_OBJECT_LOCK (pad);
4141 if (pad->priv->using == 0) {
4142 /* pad is not active anymore, trigger idle callbacks */
4143 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE, NULL,
4144 post_probe_stopped);
4147 if (G_UNLIKELY (ret != GST_FLOW_OK))
4148 goto pull_range_failed;
4150 PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, buffer,
4151 post_probe_stopped);
4153 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4154 if (G_UNLIKELY (needs_events)) {
4155 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4157 GST_DEBUG_OBJECT (pad, "we need to update the events");
4158 ret = gst_pad_update_events (pad);
4159 if (G_UNLIKELY (ret != GST_FLOW_OK))
4162 GST_OBJECT_UNLOCK (pad);
4166 /* ERROR recovery here */
4169 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4170 "pullrange, but pad was flushing");
4171 GST_OBJECT_UNLOCK (pad);
4172 return GST_FLOW_WRONG_STATE;
4176 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4177 gst_flow_get_name (ret));
4178 GST_OBJECT_UNLOCK (pad);
4183 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4184 "pulling range, but it was not linked");
4185 GST_OBJECT_UNLOCK (pad);
4186 return GST_FLOW_NOT_LINKED;
4191 GST_OBJECT_UNLOCK (pad);
4192 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4193 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4194 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4199 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4200 "post probe returned %s", gst_flow_get_name (ret));
4201 GST_OBJECT_UNLOCK (pad);
4202 if (ret == GST_FLOW_OK)
4203 gst_buffer_unref (*buffer);
4209 GST_OBJECT_UNLOCK (pad);
4210 gst_buffer_unref (*buffer);
4212 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4213 "pullrange returned events that were not accepted");
4219 * gst_pad_push_event:
4220 * @pad: a #GstPad to push the event to.
4221 * @event: (transfer full): the #GstEvent to send to the pad.
4223 * Sends the event to the peer of the given pad. This function is
4224 * mainly used by elements to send events to their peer
4227 * This function takes owership of the provided event so you should
4228 * gst_event_ref() it if you want to reuse the event after this call.
4230 * Returns: TRUE if the event was handled.
4235 gst_pad_push_event (GstPad * pad, GstEvent * event)
4240 gboolean stored = FALSE;
4241 GstPadProbeType type;
4243 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4244 g_return_val_if_fail (event != NULL, FALSE);
4245 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4247 if (GST_EVENT_IS_DOWNSTREAM (event))
4248 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4250 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4252 GST_OBJECT_LOCK (pad);
4254 peerpad = GST_PAD_PEER (pad);
4256 /* Two checks to be made:
4257 * . (un)set the FLUSHING flag for flushing events,
4258 * . handle pad blocking */
4259 switch (GST_EVENT_TYPE (event)) {
4260 case GST_EVENT_FLUSH_START:
4261 GST_PAD_SET_FLUSHING (pad);
4263 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4264 /* flush start will have set the FLUSHING flag and will then
4265 * unlock all threads doing a GCond wait on the blocking pad. This
4266 * will typically unblock the STREAMING thread blocked on a pad. */
4267 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4268 "doing block signal.");
4269 GST_PAD_BLOCK_BROADCAST (pad);
4273 case GST_EVENT_FLUSH_STOP:
4274 GST_PAD_UNSET_FLUSHING (pad);
4276 /* Remove sticky EOS events */
4277 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4278 clear_event (pad->priv->events,
4279 GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4281 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4282 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4288 /* store the event on the pad, but only on srcpads */
4289 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4292 idx = GST_EVENT_STICKY_IDX (event);
4293 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4294 GST_EVENT_TYPE_NAME (event), idx);
4296 /* srcpad sticky events always become active immediately */
4297 gst_event_replace (&pad->priv->events[idx].event, event);
4302 /* backwards compatibility mode for caps */
4303 switch (GST_EVENT_TYPE (event)) {
4304 case GST_EVENT_CAPS:
4306 GST_OBJECT_UNLOCK (pad);
4308 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4310 GST_OBJECT_LOCK (pad);
4311 /* the peerpad might have changed. Things we checked above could not
4313 peerpad = GST_PAD_PEER (pad);
4316 case GST_EVENT_SEGMENT:
4320 offset = pad->offset;
4321 /* check if we need to adjust the segment */
4322 if (offset != 0 && (peerpad != NULL)) {
4325 /* copy segment values */
4326 gst_event_copy_segment (event, &segment);
4327 gst_event_unref (event);
4329 /* adjust and make a new event with the offset applied */
4330 segment.base += offset;
4331 event = gst_event_new_segment (&segment);
4335 case GST_EVENT_RECONFIGURE:
4336 if (GST_PAD_IS_SINK (pad))
4337 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4343 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4346 PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4347 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4353 /* send probes after modifying the events above */
4354 PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4356 /* now check the peer pad */
4357 if (peerpad == NULL)
4360 gst_object_ref (peerpad);
4362 GST_OBJECT_UNLOCK (pad);
4364 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4365 event, GST_EVENT_TYPE_NAME (event), peerpad);
4367 result = gst_pad_send_event (peerpad, event);
4369 /* Note: we gave away ownership of the event at this point but we can still
4370 * print the old pointer */
4371 GST_LOG_OBJECT (pad,
4372 "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4375 gst_object_unref (peerpad);
4377 GST_OBJECT_LOCK (pad);
4379 if (pad->priv->using == 0) {
4380 /* pad is not active anymore, trigger idle callbacks */
4381 PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
4384 GST_OBJECT_UNLOCK (pad);
4386 return result | stored;
4388 /* ERROR handling */
4391 GST_DEBUG_OBJECT (pad, "We're flushing");
4392 GST_OBJECT_UNLOCK (pad);
4393 gst_event_unref (event);
4398 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4399 GST_OBJECT_UNLOCK (pad);
4400 gst_event_unref (event);
4405 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4406 GST_OBJECT_UNLOCK (pad);
4407 gst_event_unref (event);
4413 * gst_pad_send_event:
4414 * @pad: a #GstPad to send the event to.
4415 * @event: (transfer full): the #GstEvent to send to the pad.
4417 * Sends the event to the pad. This function can be used
4418 * by applications to send events in the pipeline.
4420 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4421 * sink pad, @event should be a downstream event. For example, you would not
4422 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4423 * Furthermore, some downstream events have to be serialized with data flow,
4424 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4425 * the event needs to be serialized with data flow, this function will take the
4426 * pad's stream lock while calling its event function.
4428 * To find out whether an event type is upstream, downstream, or downstream and
4429 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4430 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4431 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4432 * plugin doesn't need to bother itself with this information; the core handles
4433 * all necessary locks and checks.
4435 * This function takes owership of the provided event so you should
4436 * gst_event_ref() it if you want to reuse the event after this call.
4438 * Returns: TRUE if the event was handled.
4441 gst_pad_send_event (GstPad * pad, GstEvent * event)
4444 gboolean result = FALSE;
4445 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4446 GstPadProbeType type;
4448 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4449 g_return_val_if_fail (event != NULL, FALSE);
4451 GST_OBJECT_LOCK (pad);
4452 if (GST_PAD_IS_SINK (pad)) {
4453 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4454 goto wrong_direction;
4455 serialized = GST_EVENT_IS_SERIALIZED (event);
4456 sticky = GST_EVENT_IS_STICKY (event);
4457 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4458 } else if (GST_PAD_IS_SRC (pad)) {
4459 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4460 goto wrong_direction;
4461 /* events on srcpad never are serialized and sticky */
4462 serialized = sticky = FALSE;
4463 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4465 goto unknown_direction;
4467 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4469 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4471 switch (GST_EVENT_TYPE (event)) {
4472 case GST_EVENT_FLUSH_START:
4473 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4474 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4476 /* can't even accept a flush begin event when flushing */
4477 if (GST_PAD_IS_FLUSHING (pad))
4480 GST_PAD_SET_FLUSHING (pad);
4481 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4482 needs_events = FALSE;
4484 case GST_EVENT_FLUSH_STOP:
4485 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_PAD_ACTIVATE_NONE)) {
4486 GST_PAD_UNSET_FLUSHING (pad);
4487 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4489 /* Remove pending EOS events */
4490 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4491 clear_event (pad->priv->events,
4492 GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4494 GST_OBJECT_UNLOCK (pad);
4495 /* grab stream lock */
4496 GST_PAD_STREAM_LOCK (pad);
4498 GST_OBJECT_LOCK (pad);
4499 needs_events = FALSE;
4501 case GST_EVENT_RECONFIGURE:
4502 if (GST_PAD_IS_SRC (pad))
4503 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4505 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4506 GST_EVENT_TYPE_NAME (event));
4509 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4510 GST_OBJECT_UNLOCK (pad);
4511 GST_PAD_STREAM_LOCK (pad);
4513 GST_OBJECT_LOCK (pad);
4515 /* don't forward events on non-serialized events */
4516 needs_events = FALSE;
4519 /* store the event on the pad, but only on srcpads. We need to store the
4520 * event before checking the flushing flag. */
4525 switch (GST_EVENT_TYPE (event)) {
4526 case GST_EVENT_SEGMENT:
4527 if (pad->offset != 0) {
4530 /* copy segment values */
4531 gst_event_copy_segment (event, &segment);
4532 gst_event_unref (event);
4534 /* adjust and make a new event with the offset applied */
4535 segment.base += pad->offset;
4536 event = gst_event_new_segment (&segment);
4543 idx = GST_EVENT_STICKY_IDX (event);
4544 ev = &pad->priv->events[idx];
4546 if (ev->event != event) {
4547 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4548 GST_EVENT_TYPE_NAME (event), idx);
4549 gst_event_replace (&ev->pending, event);
4550 /* set the flag so that we update the events next time. We would
4551 * usually update below but we might be flushing too. */
4552 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4553 needs_events = TRUE;
4556 /* now do the probe */
4557 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4561 type | GST_PAD_PROBE_TYPE_PUSH |
4562 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4564 PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4569 if (G_UNLIKELY (needs_events)) {
4572 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4574 GST_DEBUG_OBJECT (pad, "need to update all events");
4575 ret = gst_pad_update_events (pad);
4576 if (ret != GST_FLOW_OK)
4578 GST_OBJECT_UNLOCK (pad);
4580 gst_event_unref (event);
4585 /* ensure to pass on event;
4586 * note that a sticky event has already been updated above */
4587 if (G_LIKELY (!needs_events || !sticky)) {
4588 GstPadEventFunction eventfunc;
4590 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4593 GST_OBJECT_UNLOCK (pad);
4595 result = eventfunc (pad, event);
4599 GST_PAD_STREAM_UNLOCK (pad);
4601 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4605 /* ERROR handling */
4608 g_warning ("pad %s:%s sending %s event in wrong direction",
4609 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4610 GST_OBJECT_UNLOCK (pad);
4611 gst_event_unref (event);
4616 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4617 GST_OBJECT_UNLOCK (pad);
4618 gst_event_unref (event);
4623 g_warning ("pad %s:%s has no event handler, file a bug.",
4624 GST_DEBUG_PAD_NAME (pad));
4625 GST_OBJECT_UNLOCK (pad);
4627 GST_PAD_STREAM_UNLOCK (pad);
4628 gst_event_unref (event);
4633 GST_OBJECT_UNLOCK (pad);
4635 GST_PAD_STREAM_UNLOCK (pad);
4636 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4637 "Received event on flushing pad. Discarding");
4638 gst_event_unref (event);
4643 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4644 GST_OBJECT_UNLOCK (pad);
4646 GST_PAD_STREAM_UNLOCK (pad);
4647 gst_event_unref (event);
4652 GST_OBJECT_UNLOCK (pad);
4654 GST_PAD_STREAM_UNLOCK (pad);
4655 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4656 gst_event_unref (event);
4662 * gst_pad_set_element_private:
4663 * @pad: the #GstPad to set the private data of.
4664 * @priv: The private data to attach to the pad.
4666 * Set the given private data gpointer on the pad.
4667 * This function can only be used by the element that owns the pad.
4668 * No locking is performed in this function.
4671 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4673 pad->element_private = priv;
4677 * gst_pad_get_element_private:
4678 * @pad: the #GstPad to get the private data of.
4680 * Gets the private data of a pad.
4681 * No locking is performed in this function.
4683 * Returns: (transfer none): a #gpointer to the private data.
4686 gst_pad_get_element_private (GstPad * pad)
4688 return pad->element_private;
4692 * gst_pad_get_sticky_event:
4693 * @pad: the #GstPad to get the event from.
4694 * @event_type: the #GstEventType that should be retrieved.
4696 * Returns a new reference of the sticky event of type @event_type
4699 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4702 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4704 GstEvent *event = NULL;
4707 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4708 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4710 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4712 GST_OBJECT_LOCK (pad);
4713 if ((event = pad->priv->events[idx].event)) {
4714 gst_event_ref (event);
4716 GST_OBJECT_UNLOCK (pad);
4722 * gst_pad_sticky_events_foreach:
4723 * @pad: the #GstPad that should be used for iteration.
4724 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4725 * @user_data: (closure): the optional user data.
4727 * Iterates all active sticky events on @pad and calls @foreach_func for every
4728 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4729 * is immediately stopped.
4731 * Returns: GST_FLOW_OK if iteration was successful
4734 gst_pad_sticky_events_foreach (GstPad * pad,
4735 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4737 GstFlowReturn ret = GST_FLOW_OK;
4741 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4742 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4744 GST_OBJECT_LOCK (pad);
4747 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4751 ev = &pad->priv->events[i];
4753 /* skip without active event */
4754 if ((event = ev->event) == NULL)
4757 gst_event_ref (event);
4758 GST_OBJECT_UNLOCK (pad);
4760 res = foreach_func (pad, event, user_data);
4762 GST_OBJECT_LOCK (pad);
4763 gst_event_unref (event);
4765 if (res != GST_FLOW_OK) {
4770 /* things could have changed while we release the lock, check if we still
4771 * are handling the same event, if we don't something changed and we have
4772 * to try again. FIXME. we need a cookie here. */
4773 if (event != ev->event) {
4774 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4778 GST_OBJECT_UNLOCK (pad);
4784 do_stream_status (GstPad * pad, GstStreamStatusType type,
4785 GThread * thread, GstTask * task)
4789 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4791 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4792 if (GST_IS_ELEMENT (parent)) {
4793 GstMessage *message;
4794 GValue value = { 0 };
4796 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4797 gchar *tname, *ename, *pname;
4799 /* create a good task name */
4800 ename = gst_element_get_name (parent);
4801 pname = gst_pad_get_name (pad);
4802 tname = g_strdup_printf ("%s:%s", ename, pname);
4806 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4810 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4813 g_value_init (&value, GST_TYPE_TASK);
4814 g_value_set_object (&value, task);
4815 gst_message_set_stream_status_object (message, &value);
4816 g_value_unset (&value);
4818 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4819 gst_element_post_message (parent, message);
4821 gst_object_unref (parent);
4826 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4828 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4833 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4835 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4839 static GstTaskThreadCallbacks thr_callbacks = {
4845 * gst_pad_start_task:
4846 * @pad: the #GstPad to start the task of
4847 * @func: the task function to call
4848 * @data: data passed to the task function
4850 * Starts a task that repeatedly calls @func with @data. This function
4851 * is mostly used in pad activation functions to start the dataflow.
4852 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4853 * before @func is called.
4855 * Returns: a %TRUE if the task could be started.
4858 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4863 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4864 g_return_val_if_fail (func != NULL, FALSE);
4866 GST_DEBUG_OBJECT (pad, "start task");
4868 GST_OBJECT_LOCK (pad);
4869 task = GST_PAD_TASK (pad);
4871 task = gst_task_new (func, data);
4872 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4873 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4874 GST_DEBUG_OBJECT (pad, "created task");
4875 GST_PAD_TASK (pad) = task;
4876 gst_object_ref (task);
4877 /* release lock to post the message */
4878 GST_OBJECT_UNLOCK (pad);
4880 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4882 gst_object_unref (task);
4884 GST_OBJECT_LOCK (pad);
4885 /* nobody else is supposed to have changed the pad now */
4886 if (GST_PAD_TASK (pad) != task)
4887 goto concurrent_stop;
4889 res = gst_task_set_state (task, GST_TASK_STARTED);
4890 GST_OBJECT_UNLOCK (pad);
4897 GST_OBJECT_UNLOCK (pad);
4903 * gst_pad_pause_task:
4904 * @pad: the #GstPad to pause the task of
4906 * Pause the task of @pad. This function will also wait until the
4907 * function executed by the task is finished if this function is not
4908 * called from the task function.
4910 * Returns: a TRUE if the task could be paused or FALSE when the pad
4914 gst_pad_pause_task (GstPad * pad)
4919 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4921 GST_DEBUG_OBJECT (pad, "pause task");
4923 GST_OBJECT_LOCK (pad);
4924 task = GST_PAD_TASK (pad);
4927 res = gst_task_set_state (task, GST_TASK_PAUSED);
4928 GST_OBJECT_UNLOCK (pad);
4930 /* wait for task function to finish, this lock is recursive so it does nothing
4931 * when the pause is called from the task itself */
4932 GST_PAD_STREAM_LOCK (pad);
4933 GST_PAD_STREAM_UNLOCK (pad);
4939 GST_DEBUG_OBJECT (pad, "pad has no task");
4940 GST_OBJECT_UNLOCK (pad);
4946 * gst_pad_stop_task:
4947 * @pad: the #GstPad to stop the task of
4949 * Stop the task of @pad. This function will also make sure that the
4950 * function executed by the task will effectively stop if not called
4951 * from the GstTaskFunction.
4953 * This function will deadlock if called from the GstTaskFunction of
4954 * the task. Use gst_task_pause() instead.
4956 * Regardless of whether the pad has a task, the stream lock is acquired and
4957 * released so as to ensure that streaming through this pad has finished.
4959 * Returns: a TRUE if the task could be stopped or FALSE on error.
4962 gst_pad_stop_task (GstPad * pad)
4967 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4969 GST_DEBUG_OBJECT (pad, "stop task");
4971 GST_OBJECT_LOCK (pad);
4972 task = GST_PAD_TASK (pad);
4975 GST_PAD_TASK (pad) = NULL;
4976 res = gst_task_set_state (task, GST_TASK_STOPPED);
4977 GST_OBJECT_UNLOCK (pad);
4979 GST_PAD_STREAM_LOCK (pad);
4980 GST_PAD_STREAM_UNLOCK (pad);
4982 if (!gst_task_join (task))
4985 gst_object_unref (task);
4991 GST_DEBUG_OBJECT (pad, "no task");
4992 GST_OBJECT_UNLOCK (pad);
4994 GST_PAD_STREAM_LOCK (pad);
4995 GST_PAD_STREAM_UNLOCK (pad);
4997 /* this is not an error */
5002 /* this is bad, possibly the application tried to join the task from
5003 * the task's thread. We install the task again so that it will be stopped
5004 * again from the right thread next time hopefully. */
5005 GST_OBJECT_LOCK (pad);
5006 GST_DEBUG_OBJECT (pad, "join failed");
5007 /* we can only install this task if there was no other task */
5008 if (GST_PAD_TASK (pad) == NULL)
5009 GST_PAD_TASK (pad) = task;
5010 GST_OBJECT_UNLOCK (pad);