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 */
97 #define GST_PAD_GET_PRIVATE(obj) \
98 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
100 /* we have a pending and an active event on the pad. On source pads only the
101 * active event is used. On sinkpads, events are copied to the pending entry and
102 * moved to the active event when the eventfunc returned TRUE. */
109 struct _GstPadPrivate
111 PadEvent events[GST_EVENT_MAX_STICKY];
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
135 static void gst_pad_dispose (GObject * object);
136 static void gst_pad_finalize (GObject * object);
137 static void gst_pad_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec);
139 static void gst_pad_get_property (GObject * object, guint prop_id,
140 GValue * value, GParamSpec * pspec);
142 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
143 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
144 static gboolean gst_pad_activate_default (GstPad * pad);
145 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
146 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
147 GstBufferList * list);
149 static GstObjectClass *parent_class = NULL;
150 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
152 static GParamSpec *pspec_caps = NULL;
154 /* quarks for probe signals */
155 static GQuark buffer_quark;
156 static GQuark buffer_list_quark;
157 static GQuark event_quark;
166 static GstFlowQuarks flow_quarks[] = {
167 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
168 {GST_FLOW_RESEND, "resend", 0},
169 {GST_FLOW_OK, "ok", 0},
170 {GST_FLOW_NOT_LINKED, "not-linked", 0},
171 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
172 {GST_FLOW_UNEXPECTED, "unexpected", 0},
173 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
174 {GST_FLOW_ERROR, "error", 0},
175 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
176 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
181 * @ret: a #GstFlowReturn to get the name of.
183 * Gets a string representing the given flow return.
185 * Returns: a static string with the name of the flow return.
187 G_CONST_RETURN gchar *
188 gst_flow_get_name (GstFlowReturn ret)
192 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
194 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
195 if (ret == flow_quarks[i].ret)
196 return flow_quarks[i].name;
203 * @ret: a #GstFlowReturn to get the quark of.
205 * Get the unique quark for the given GstFlowReturn.
207 * Returns: the quark associated with the flow return or 0 if an
208 * invalid return was specified.
211 gst_flow_to_quark (GstFlowReturn ret)
215 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
217 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
218 if (ret == flow_quarks[i].ret)
219 return flow_quarks[i].quark;
228 buffer_quark = g_quark_from_static_string ("buffer"); \
229 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
230 event_quark = g_quark_from_static_string ("event"); \
232 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
233 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
236 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
237 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
240 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
243 gst_pad_class_init (GstPadClass * klass)
245 GObjectClass *gobject_class;
246 GstObjectClass *gstobject_class;
248 gobject_class = G_OBJECT_CLASS (klass);
249 gstobject_class = GST_OBJECT_CLASS (klass);
251 g_type_class_add_private (klass, sizeof (GstPadPrivate));
253 parent_class = g_type_class_peek_parent (klass);
255 gobject_class->dispose = gst_pad_dispose;
256 gobject_class->finalize = gst_pad_finalize;
257 gobject_class->set_property = gst_pad_set_property;
258 gobject_class->get_property = gst_pad_get_property;
262 * @pad: the pad that emitted the signal
263 * @peer: the peer pad that has been connected
265 * Signals that a pad has been linked to the peer pad.
267 gst_pad_signals[PAD_LINKED] =
268 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
270 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
273 * @pad: the pad that emitted the signal
274 * @peer: the peer pad that has been disconnected
276 * Signals that a pad has been unlinked from the peer pad.
278 gst_pad_signals[PAD_UNLINKED] =
279 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
281 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
283 * GstPad::request-link:
284 * @pad: the pad that emitted the signal
285 * @peer: the peer pad for which a connection is requested
287 * Signals that a pad connection has been requested.
289 gst_pad_signals[PAD_REQUEST_LINK] =
290 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
291 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
292 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
294 pspec_caps = g_param_spec_boxed ("caps", "Caps",
295 "The capabilities of the pad", GST_TYPE_CAPS,
296 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
297 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
299 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
300 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
301 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
302 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
303 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
304 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
305 g_param_spec_object ("template", "Template",
306 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 gstobject_class->path_string_separator = ".";
311 /* Register common function pointer descriptions */
312 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
313 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
314 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
315 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
316 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
317 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
318 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
322 gst_pad_init (GstPad * pad)
324 pad->priv = GST_PAD_GET_PRIVATE (pad);
326 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
328 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
329 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
330 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
331 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
332 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
333 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
334 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
336 GST_PAD_SET_FLUSHING (pad);
338 /* FIXME 0.11: Store this directly in the instance struct */
339 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
340 g_static_rec_mutex_init (pad->stream_rec_lock);
342 pad->block_cond = g_cond_new ();
344 g_hook_list_init (&pad->probes, sizeof (GstProbe));
347 /* called when setting the pad inactive. It removes all sticky events from
350 clear_events (PadEvent events[])
354 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
355 gst_event_replace (&events[i].event, NULL);
356 gst_event_replace (&events[i].pending, NULL);
360 /* The sticky event with @idx from the srcpad is copied to the
361 * pending event on the sinkpad (when different).
362 * This function applies the pad offsets in case of segment events.
363 * This will make sure that we send the event to the sinkpad event
364 * function when the next buffer of event arrives.
365 * Should be called with the OBJECT lock of both pads.
366 * This function returns TRUE when there is a pending event on the
369 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
371 PadEvent *srcev, *sinkev;
373 gboolean pending = FALSE;
375 srcev = &srcpad->priv->events[idx];
377 if ((event = srcev->event)) {
378 sinkev = &sinkpad->priv->events[idx];
380 switch (GST_EVENT_TYPE (event)) {
381 case GST_EVENT_SEGMENT:
386 offset = srcpad->offset + sinkpad->offset;
388 gst_event_copy_segment (event, &segment);
389 /* adjust the base time. FIXME, check negative times, try to tweak the
390 * start to do clipping on negative times */
391 segment.base += offset;
392 /* make a new event from the updated segment */
393 event = gst_event_new_segment (&segment);
400 if (sinkev->event != event) {
401 /* put in the pending entry when different */
402 gst_event_replace (&sinkev->pending, event);
411 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
416 /* make sure we push the events from the source to this new peer, for this we
417 * copy the events on the sinkpad and mark EVENTS_PENDING */
419 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
420 pending |= replace_event (srcpad, sinkpad, i);
422 /* we had some new pending events, set our flag */
424 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
427 /* should be called with the OBJECT_LOCK */
429 get_pad_caps (GstPad * pad)
431 GstCaps *caps = NULL;
435 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
436 /* we can only use the caps when we have successfully send the caps
437 * event to the event function and is thus in the active entry */
438 if ((event = pad->priv->events[idx].event))
439 gst_event_parse_caps (event, &caps);
445 gst_pad_dispose (GObject * object)
447 GstPad *pad = GST_PAD_CAST (object);
450 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
452 /* unlink the peer pad */
453 if ((peer = gst_pad_get_peer (pad))) {
454 /* window for MT unsafeness, someone else could unlink here
455 * and then we call unlink with wrong pads. The unlink
456 * function would catch this and safely return failed. */
457 if (GST_PAD_IS_SRC (pad))
458 gst_pad_unlink (pad, peer);
460 gst_pad_unlink (peer, pad);
462 gst_object_unref (peer);
465 gst_pad_set_pad_template (pad, NULL);
467 clear_events (pad->priv->events);
469 g_hook_list_clear (&pad->probes);
471 G_OBJECT_CLASS (parent_class)->dispose (object);
475 gst_pad_finalize (GObject * object)
477 GstPad *pad = GST_PAD_CAST (object);
480 /* in case the task is still around, clean it up */
481 if ((task = GST_PAD_TASK (pad))) {
482 gst_task_join (task);
483 GST_PAD_TASK (pad) = NULL;
484 gst_object_unref (task);
487 if (pad->stream_rec_lock) {
488 g_static_rec_mutex_free (pad->stream_rec_lock);
489 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
490 pad->stream_rec_lock = NULL;
492 if (pad->block_cond) {
493 g_cond_free (pad->block_cond);
494 pad->block_cond = NULL;
497 G_OBJECT_CLASS (parent_class)->finalize (object);
501 gst_pad_set_property (GObject * object, guint prop_id,
502 const GValue * value, GParamSpec * pspec)
504 g_return_if_fail (GST_IS_PAD (object));
507 case PAD_PROP_DIRECTION:
508 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
510 case PAD_PROP_TEMPLATE:
511 gst_pad_set_pad_template (GST_PAD_CAST (object),
512 (GstPadTemplate *) g_value_get_object (value));
515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521 gst_pad_get_property (GObject * object, guint prop_id,
522 GValue * value, GParamSpec * pspec)
524 g_return_if_fail (GST_IS_PAD (object));
528 GST_OBJECT_LOCK (object);
529 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
530 GST_OBJECT_UNLOCK (object);
532 case PAD_PROP_DIRECTION:
533 g_value_set_enum (value, GST_PAD_DIRECTION (object));
535 case PAD_PROP_TEMPLATE:
536 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546 * @name: the name of the new pad.
547 * @direction: the #GstPadDirection of the pad.
549 * Creates a new pad with the given name in the given direction.
550 * If name is NULL, a guaranteed unique name (across all pads)
552 * This function makes a copy of the name so you can safely free the name.
554 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
559 gst_pad_new (const gchar * name, GstPadDirection direction)
561 return g_object_new (GST_TYPE_PAD,
562 "name", name, "direction", direction, NULL);
566 * gst_pad_new_from_template:
567 * @templ: the pad template to use
568 * @name: the name of the element
570 * Creates a new pad with the given name from the given template.
571 * If name is NULL, a guaranteed unique name (across all pads)
573 * This function makes a copy of the name so you can safely free the name.
575 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
578 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
580 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
582 return g_object_new (GST_TYPE_PAD,
583 "name", name, "direction", templ->direction, "template", templ, NULL);
587 * gst_pad_new_from_static_template:
588 * @templ: the #GstStaticPadTemplate to use
589 * @name: the name of the element
591 * Creates a new pad with the given name from the given static template.
592 * If name is NULL, a guaranteed unique name (across all pads)
594 * This function makes a copy of the name so you can safely free the name.
596 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
599 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
603 GstPadTemplate *template;
605 template = gst_static_pad_template_get (templ);
606 pad = gst_pad_new_from_template (template, name);
607 gst_object_unref (template);
612 * gst_pad_get_direction:
613 * @pad: a #GstPad to get the direction of.
615 * Gets the direction of the pad. The direction of the pad is
616 * decided at construction time so this function does not take
619 * Returns: the #GstPadDirection of the pad.
624 gst_pad_get_direction (GstPad * pad)
626 GstPadDirection result;
628 /* PAD_UNKNOWN is a little silly but we need some sort of
629 * error return value */
630 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
632 result = GST_PAD_DIRECTION (pad);
638 gst_pad_activate_default (GstPad * pad)
640 return gst_pad_activate_push (pad, TRUE);
644 pre_activate (GstPad * pad, GstActivateMode new_mode)
647 case GST_ACTIVATE_PUSH:
648 case GST_ACTIVATE_PULL:
649 GST_OBJECT_LOCK (pad);
650 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
652 GST_PAD_UNSET_FLUSHING (pad);
653 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
654 GST_OBJECT_UNLOCK (pad);
656 case GST_ACTIVATE_NONE:
657 GST_OBJECT_LOCK (pad);
658 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
659 GST_PAD_SET_FLUSHING (pad);
660 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
661 /* unlock blocked pads so element can resume and stop */
662 GST_PAD_BLOCK_BROADCAST (pad);
663 GST_OBJECT_UNLOCK (pad);
669 post_activate (GstPad * pad, GstActivateMode new_mode)
672 case GST_ACTIVATE_PUSH:
673 case GST_ACTIVATE_PULL:
676 case GST_ACTIVATE_NONE:
677 /* ensures that streaming stops */
678 GST_PAD_STREAM_LOCK (pad);
679 GST_DEBUG_OBJECT (pad, "stopped streaming");
680 GST_OBJECT_LOCK (pad);
681 clear_events (pad->priv->events);
682 GST_OBJECT_UNLOCK (pad);
683 GST_PAD_STREAM_UNLOCK (pad);
689 * gst_pad_set_active:
690 * @pad: the #GstPad to activate or deactivate.
691 * @active: whether or not the pad should be active.
693 * Activates or deactivates the given pad.
694 * Normally called from within core state change functions.
696 * If @active, makes sure the pad is active. If it is already active, either in
697 * push or pull mode, just return. Otherwise dispatches to the pad's activate
698 * function to perform the actual activation.
700 * If not @active, checks the pad's current mode and calls
701 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
704 * Returns: #TRUE if the operation was successful.
709 gst_pad_set_active (GstPad * pad, gboolean active)
712 gboolean ret = FALSE;
714 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
716 GST_OBJECT_LOCK (pad);
717 old = GST_PAD_ACTIVATE_MODE (pad);
718 GST_OBJECT_UNLOCK (pad);
722 case GST_ACTIVATE_PUSH:
723 GST_DEBUG_OBJECT (pad, "activating pad from push");
726 case GST_ACTIVATE_PULL:
727 GST_DEBUG_OBJECT (pad, "activating pad from pull");
730 case GST_ACTIVATE_NONE:
731 GST_DEBUG_OBJECT (pad, "activating pad from none");
732 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
735 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
740 case GST_ACTIVATE_PUSH:
741 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
742 ret = gst_pad_activate_push (pad, FALSE);
744 case GST_ACTIVATE_PULL:
745 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
746 ret = gst_pad_activate_pull (pad, FALSE);
748 case GST_ACTIVATE_NONE:
749 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
753 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
759 GST_OBJECT_LOCK (pad);
761 g_critical ("Failed to deactivate pad %s:%s, very bad",
762 GST_DEBUG_PAD_NAME (pad));
764 GST_WARNING_OBJECT (pad, "Failed to activate pad");
766 GST_OBJECT_UNLOCK (pad);
769 GST_OBJECT_LOCK (pad);
770 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
771 GST_OBJECT_UNLOCK (pad);
779 * gst_pad_activate_pull:
780 * @pad: the #GstPad to activate or deactivate.
781 * @active: whether or not the pad should be active.
783 * Activates or deactivates the given pad in pull mode via dispatching to the
784 * pad's activatepullfunc. For use from within pad activation functions only.
785 * When called on sink pads, will first proxy the call to the peer pad, which
786 * is expected to activate its internally linked pads from within its
787 * activate_pull function.
789 * If you don't know what this is, you probably don't want to call it.
791 * Returns: TRUE if the operation was successful.
796 gst_pad_activate_pull (GstPad * pad, gboolean active)
798 GstActivateMode old, new;
801 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
803 GST_OBJECT_LOCK (pad);
804 old = GST_PAD_ACTIVATE_MODE (pad);
805 GST_OBJECT_UNLOCK (pad);
809 case GST_ACTIVATE_PULL:
810 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
812 case GST_ACTIVATE_PUSH:
813 GST_DEBUG_OBJECT (pad,
814 "activating pad from push, deactivate push first");
815 /* pad was activate in the wrong direction, deactivate it
816 * and reactivate it in pull mode */
817 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
818 goto deactivate_failed;
819 /* fallthrough, pad is deactivated now. */
820 case GST_ACTIVATE_NONE:
821 GST_DEBUG_OBJECT (pad, "activating pad from none");
826 case GST_ACTIVATE_NONE:
827 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
829 case GST_ACTIVATE_PUSH:
830 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
831 /* pad was activated in the other direction, deactivate it
832 * in push mode, this should not happen... */
833 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
834 goto deactivate_failed;
835 /* everything is fine now */
837 case GST_ACTIVATE_PULL:
838 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
843 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
844 if ((peer = gst_pad_get_peer (pad))) {
845 GST_DEBUG_OBJECT (pad, "calling peer");
846 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
848 gst_object_unref (peer);
850 /* there is no peer, this is only fatal when we activate. When we
851 * deactivate, we must assume the application has unlinked the peer and
852 * will deactivate it eventually. */
856 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
859 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
860 goto failure; /* Can't activate pull on a src without a
864 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
865 pre_activate (pad, new);
867 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
868 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
871 /* can happen for sinks of passthrough elements */
874 post_activate (pad, new);
876 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
877 active ? "activated" : "deactivated");
883 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
884 active ? "activated" : "deactivated");
889 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
890 "failed to %s in switch to pull from mode %d",
891 (active ? "activate" : "deactivate"), old);
896 GST_OBJECT_LOCK (peer);
897 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
898 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
899 GST_OBJECT_UNLOCK (peer);
900 gst_object_unref (peer);
905 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
911 GST_OBJECT_LOCK (pad);
912 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
913 active ? "activate" : "deactivate");
914 GST_PAD_SET_FLUSHING (pad);
915 GST_PAD_ACTIVATE_MODE (pad) = old;
916 GST_OBJECT_UNLOCK (pad);
922 * gst_pad_activate_push:
923 * @pad: the #GstPad to activate or deactivate.
924 * @active: whether the pad should be active or not.
926 * Activates or deactivates the given pad in push mode via dispatching to the
927 * pad's activatepushfunc. For use from within pad activation functions only.
929 * If you don't know what this is, you probably don't want to call it.
931 * Returns: %TRUE if the operation was successful.
936 gst_pad_activate_push (GstPad * pad, gboolean active)
938 GstActivateMode old, new;
940 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
941 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
942 active ? "activated" : "deactivated");
944 GST_OBJECT_LOCK (pad);
945 old = GST_PAD_ACTIVATE_MODE (pad);
946 GST_OBJECT_UNLOCK (pad);
950 case GST_ACTIVATE_PUSH:
951 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
953 case GST_ACTIVATE_PULL:
954 GST_DEBUG_OBJECT (pad,
955 "activating pad from push, deactivating pull first");
956 /* pad was activate in the wrong direction, deactivate it
957 * an reactivate it in push mode */
958 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
959 goto deactivate_failed;
960 /* fallthrough, pad is deactivated now. */
961 case GST_ACTIVATE_NONE:
962 GST_DEBUG_OBJECT (pad, "activating pad from none");
967 case GST_ACTIVATE_NONE:
968 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
970 case GST_ACTIVATE_PULL:
971 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
972 /* pad was activated in the other direction, deactivate it
973 * in pull mode, this should not happen... */
974 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
975 goto deactivate_failed;
976 /* everything is fine now */
978 case GST_ACTIVATE_PUSH:
979 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
984 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
985 pre_activate (pad, new);
987 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
988 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
992 /* quite ok, element relies on state change func to prepare itself */
995 post_activate (pad, new);
997 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
998 active ? "activated" : "deactivated");
1003 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
1004 active ? "activated" : "deactivated");
1009 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1010 "failed to %s in switch to push from mode %d",
1011 (active ? "activate" : "deactivate"), old);
1016 GST_OBJECT_LOCK (pad);
1017 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1018 active ? "activate" : "deactivate");
1019 GST_PAD_SET_FLUSHING (pad);
1020 GST_PAD_ACTIVATE_MODE (pad) = old;
1021 GST_OBJECT_UNLOCK (pad);
1027 * gst_pad_is_active:
1028 * @pad: the #GstPad to query
1030 * Query if a pad is active
1032 * Returns: TRUE if the pad is active.
1037 gst_pad_is_active (GstPad * pad)
1039 gboolean result = FALSE;
1041 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1043 GST_OBJECT_LOCK (pad);
1044 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1045 GST_OBJECT_UNLOCK (pad);
1051 * gst_pad_add_probe:
1052 * @pad: the #GstPad to add the probe to
1053 * @mask: the probe mask
1054 * @callback: #GstPadProbeCallback that will be called with notifications of
1056 * @user_data: (closure): user data passed to the callback
1057 * @destroy_data: #GDestroyNotify for user_data
1059 * Be notified of different states of pads. The provided callback is called for
1060 * every state that matches @mask.
1063 * Pad probe handlers are only called for source pads in push mode
1064 * and sink pads in pull mode.
1067 * Returns: an id or 0 on error. The id can be used to remove the probe with
1068 * gst_pad_remove_probe().
1073 gst_pad_add_probe (GstPad * pad, GstProbeType mask,
1074 GstPadProbeCallback callback, gpointer user_data,
1075 GDestroyNotify destroy_data)
1080 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1081 g_return_val_if_fail (mask != 0, 0);
1083 GST_OBJECT_LOCK (pad);
1084 /* make a new probe */
1085 hook = g_hook_alloc (&pad->probes);
1087 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1090 /* when no contraints are given for the types, assume all types are
1092 if ((mask & GST_PROBE_TYPE_DATA) == 0)
1093 mask |= GST_PROBE_TYPE_DATA;
1094 if ((mask & GST_PROBE_TYPE_SCHEDULING) == 0)
1095 mask |= GST_PROBE_TYPE_SCHEDULING;
1097 /* store our flags and other fields */
1098 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1099 hook->func = callback;
1100 hook->data = user_data;
1101 hook->destroy = destroy_data;
1102 PROBE_COOKIE (hook) = 0;
1104 /* incremenent cookie so that the new hook get's called */
1105 pad->priv->probe_cookie++;
1108 g_hook_prepend (&pad->probes, hook);
1111 /* get the id of the hook, we return this and it can be used to remove the
1113 res = hook->hook_id;
1115 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1117 if (mask & GST_PROBE_TYPE_BLOCKING) {
1118 /* we have a block probe */
1120 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1121 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1122 "now %d blocking probes", pad->num_blocked);
1125 /* call the callback if we need to be called for idle callbacks */
1126 if ((mask & GST_PROBE_TYPE_IDLE) && (callback != NULL)) {
1127 if (pad->priv->using > 0) {
1128 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1129 * flag above, the last thread to leave the push will do the callback. New
1130 * threads going into the push will block. */
1131 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1132 "pad is in use, delay idle callback");
1133 GST_OBJECT_UNLOCK (pad);
1135 /* the pad is idle now, we can signal the idle callback now */
1136 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1137 "pad is idle, trigger idle callback");
1138 GST_OBJECT_UNLOCK (pad);
1140 callback (pad, GST_PROBE_TYPE_IDLE, NULL, user_data);
1143 GST_OBJECT_UNLOCK (pad);
1149 cleanup_hook (GstPad * pad, GHook * hook)
1153 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1155 if (type & GST_PROBE_TYPE_BLOCKING) {
1156 /* unblock when we remove the last blocking probe */
1158 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1160 if (pad->num_blocked == 0) {
1161 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1162 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1163 GST_PAD_BLOCK_BROADCAST (pad);
1166 g_hook_destroy_link (&pad->probes, hook);
1171 * gst_pad_remove_probe:
1172 * @pad: the #GstPad with the probe
1173 * @id: the probe id to remove
1175 * Remove the probe with @id from @pad.
1180 gst_pad_remove_probe (GstPad * pad, gulong id)
1184 g_return_if_fail (GST_IS_PAD (pad));
1186 GST_OBJECT_LOCK (pad);
1188 hook = g_hook_get (&pad->probes, id);
1192 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1194 cleanup_hook (pad, hook);
1195 GST_OBJECT_UNLOCK (pad);
1201 GST_OBJECT_UNLOCK (pad);
1202 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1208 * gst_pad_is_blocked:
1209 * @pad: the #GstPad to query
1211 * Checks if the pad is blocked or not. This function returns the
1212 * last requested state of the pad. It is not certain that the pad
1213 * is actually blocking at this point (see gst_pad_is_blocking()).
1215 * Returns: TRUE if the pad is blocked.
1220 gst_pad_is_blocked (GstPad * pad)
1222 gboolean result = FALSE;
1224 g_return_val_if_fail (GST_IS_PAD (pad), result);
1226 GST_OBJECT_LOCK (pad);
1227 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1228 GST_OBJECT_UNLOCK (pad);
1234 * gst_pad_is_blocking:
1235 * @pad: the #GstPad to query
1237 * Checks if the pad is blocking or not. This is a guaranteed state
1238 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1240 * Returns: TRUE if the pad is blocking.
1247 gst_pad_is_blocking (GstPad * pad)
1249 gboolean result = FALSE;
1251 g_return_val_if_fail (GST_IS_PAD (pad), result);
1253 GST_OBJECT_LOCK (pad);
1254 /* the blocking flag is only valid if the pad is not flushing */
1255 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1256 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1257 GST_OBJECT_UNLOCK (pad);
1263 * gst_pad_set_activate_function:
1265 * @activate: the #GstPadActivateFunction to set.
1267 * Sets the given activate function for @pad. The activate function will
1268 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1269 * the actual activation. Only makes sense to set on sink pads.
1271 * Call this function if your sink pad can start a pull-based task.
1274 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1276 g_return_if_fail (GST_IS_PAD (pad));
1278 GST_PAD_ACTIVATEFUNC (pad) = activate;
1279 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1280 GST_DEBUG_FUNCPTR_NAME (activate));
1284 * gst_pad_set_activatepull_function:
1286 * @activatepull: the #GstPadActivateModeFunction to set.
1288 * Sets the given activate_pull function for the pad. An activate_pull function
1289 * prepares the element and any upstream connections for pulling. See XXX
1290 * part-activation.txt for details.
1293 gst_pad_set_activatepull_function (GstPad * pad,
1294 GstPadActivateModeFunction activatepull)
1296 g_return_if_fail (GST_IS_PAD (pad));
1298 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1299 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1300 GST_DEBUG_FUNCPTR_NAME (activatepull));
1304 * gst_pad_set_activatepush_function:
1306 * @activatepush: the #GstPadActivateModeFunction to set.
1308 * Sets the given activate_push function for the pad. An activate_push function
1309 * prepares the element for pushing. See XXX part-activation.txt for details.
1312 gst_pad_set_activatepush_function (GstPad * pad,
1313 GstPadActivateModeFunction activatepush)
1315 g_return_if_fail (GST_IS_PAD (pad));
1317 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1318 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1319 GST_DEBUG_FUNCPTR_NAME (activatepush));
1323 * gst_pad_set_chain_function:
1324 * @pad: a sink #GstPad.
1325 * @chain: the #GstPadChainFunction to set.
1327 * Sets the given chain function for the pad. The chain function is called to
1328 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1331 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1333 g_return_if_fail (GST_IS_PAD (pad));
1334 g_return_if_fail (GST_PAD_IS_SINK (pad));
1336 GST_PAD_CHAINFUNC (pad) = chain;
1337 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1338 GST_DEBUG_FUNCPTR_NAME (chain));
1342 * gst_pad_set_chain_list_function:
1343 * @pad: a sink #GstPad.
1344 * @chainlist: the #GstPadChainListFunction to set.
1346 * Sets the given chain list function for the pad. The chainlist function is
1347 * called to process a #GstBufferList input buffer list. See
1348 * #GstPadChainListFunction for more details.
1353 gst_pad_set_chain_list_function (GstPad * pad,
1354 GstPadChainListFunction chainlist)
1356 g_return_if_fail (GST_IS_PAD (pad));
1357 g_return_if_fail (GST_PAD_IS_SINK (pad));
1359 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1360 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1361 GST_DEBUG_FUNCPTR_NAME (chainlist));
1365 * gst_pad_set_getrange_function:
1366 * @pad: a source #GstPad.
1367 * @get: the #GstPadGetRangeFunction to set.
1369 * Sets the given getrange function for the pad. The getrange function is
1370 * called to produce a new #GstBuffer to start the processing pipeline. see
1371 * #GstPadGetRangeFunction for a description of the getrange function.
1374 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1376 g_return_if_fail (GST_IS_PAD (pad));
1377 g_return_if_fail (GST_PAD_IS_SRC (pad));
1379 GST_PAD_GETRANGEFUNC (pad) = get;
1381 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1382 GST_DEBUG_FUNCPTR_NAME (get));
1386 * gst_pad_set_event_function:
1387 * @pad: a #GstPad of either direction.
1388 * @event: the #GstPadEventFunction to set.
1390 * Sets the given event handler for the pad.
1393 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1395 g_return_if_fail (GST_IS_PAD (pad));
1397 GST_PAD_EVENTFUNC (pad) = event;
1399 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1400 GST_DEBUG_FUNCPTR_NAME (event));
1404 * gst_pad_set_query_function:
1405 * @pad: a #GstPad of either direction.
1406 * @query: the #GstPadQueryFunction to set.
1408 * Set the given query function for the pad.
1411 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1413 g_return_if_fail (GST_IS_PAD (pad));
1415 GST_PAD_QUERYFUNC (pad) = query;
1417 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1418 GST_DEBUG_FUNCPTR_NAME (query));
1422 * gst_pad_set_query_type_function:
1423 * @pad: a #GstPad of either direction.
1424 * @type_func: the #GstPadQueryTypeFunction to set.
1426 * Set the given query type function for the pad.
1429 gst_pad_set_query_type_function (GstPad * pad,
1430 GstPadQueryTypeFunction type_func)
1432 g_return_if_fail (GST_IS_PAD (pad));
1434 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1436 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1437 GST_DEBUG_FUNCPTR_NAME (type_func));
1441 * gst_pad_get_query_types:
1444 * Get an array of supported queries that can be performed
1447 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1450 const GstQueryType *
1451 gst_pad_get_query_types (GstPad * pad)
1453 GstPadQueryTypeFunction func;
1455 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1457 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1469 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1471 *data = gst_pad_get_query_types (pad);
1477 * gst_pad_get_query_types_default:
1480 * Invoke the default dispatcher for the query types on
1483 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1484 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1485 * query types function.
1487 const GstQueryType *
1488 gst_pad_get_query_types_default (GstPad * pad)
1490 GstQueryType *result = NULL;
1492 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1494 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1495 gst_pad_get_query_types_dispatcher, &result);
1501 * gst_pad_set_iterate_internal_links_function:
1502 * @pad: a #GstPad of either direction.
1503 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1505 * Sets the given internal link iterator function for the pad.
1510 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1511 GstPadIterIntLinkFunction iterintlink)
1513 g_return_if_fail (GST_IS_PAD (pad));
1515 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1516 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1517 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1521 * gst_pad_set_link_function:
1523 * @link: the #GstPadLinkFunction to set.
1525 * Sets the given link function for the pad. It will be called when
1526 * the pad is linked with another pad.
1528 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1531 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1532 * cannot be made for some reason.
1534 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1535 * of the peer sink pad, if present.
1538 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1540 g_return_if_fail (GST_IS_PAD (pad));
1542 GST_PAD_LINKFUNC (pad) = link;
1543 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1544 GST_DEBUG_FUNCPTR_NAME (link));
1548 * gst_pad_set_unlink_function:
1550 * @unlink: the #GstPadUnlinkFunction to set.
1552 * Sets the given unlink function for the pad. It will be called
1553 * when the pad is unlinked.
1556 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1558 g_return_if_fail (GST_IS_PAD (pad));
1560 GST_PAD_UNLINKFUNC (pad) = unlink;
1561 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1562 GST_DEBUG_FUNCPTR_NAME (unlink));
1566 * gst_pad_set_getcaps_function:
1568 * @getcaps: the #GstPadGetCapsFunction to set.
1570 * Sets the given getcaps function for the pad. @getcaps should return the
1571 * allowable caps for a pad in the context of the element's state, its link to
1572 * other elements, and the devices or files it has opened. These caps must be a
1573 * subset of the pad template caps. In the NULL state with no links, @getcaps
1574 * should ideally return the same caps as the pad template. In rare
1575 * circumstances, an object property can affect the caps returned by @getcaps,
1576 * but this is discouraged.
1578 * You do not need to call this function if @pad's allowed caps are always the
1579 * same as the pad template caps. This can only be true if the padtemplate
1580 * has fixed simple caps.
1582 * For most filters, the caps returned by @getcaps is directly affected by the
1583 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1584 * the srcpad's getcaps function is directly related to the stream data. Again,
1585 * @getcaps should return the most specific caps it reasonably can, since this
1586 * helps with autoplugging.
1588 * Note that the return value from @getcaps is owned by the caller, so the
1589 * caller should unref the caps after usage.
1592 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1594 g_return_if_fail (GST_IS_PAD (pad));
1596 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1597 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1598 GST_DEBUG_FUNCPTR_NAME (getcaps));
1602 * gst_pad_set_acceptcaps_function:
1604 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1606 * Sets the given acceptcaps function for the pad. The acceptcaps function
1607 * will be called to check if the pad can accept the given caps. Setting the
1608 * acceptcaps function to NULL restores the default behaviour of allowing
1609 * any caps that matches the caps from gst_pad_get_caps().
1612 gst_pad_set_acceptcaps_function (GstPad * pad,
1613 GstPadAcceptCapsFunction acceptcaps)
1615 g_return_if_fail (GST_IS_PAD (pad));
1617 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1618 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1619 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1623 * gst_pad_set_fixatecaps_function:
1625 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1627 * Sets the given fixatecaps function for the pad. The fixatecaps function
1628 * will be called whenever the default values for a GstCaps needs to be
1632 gst_pad_set_fixatecaps_function (GstPad * pad,
1633 GstPadFixateCapsFunction fixatecaps)
1635 g_return_if_fail (GST_IS_PAD (pad));
1637 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1638 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1639 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1643 * gst_pad_set_setcaps_function:
1645 * @setcaps: the #GstPadSetCapsFunction to set.
1647 * Sets the given setcaps function for the pad. The setcaps function
1648 * will be called whenever a buffer with a new media type is pushed or
1649 * pulled from the pad. The pad/element needs to update its internal
1650 * structures to process the new media type. If this new type is not
1651 * acceptable, the setcaps function should return FALSE.
1654 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1656 g_return_if_fail (GST_IS_PAD (pad));
1658 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1659 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1660 GST_DEBUG_FUNCPTR_NAME (setcaps));
1665 * @srcpad: the source #GstPad to unlink.
1666 * @sinkpad: the sink #GstPad to unlink.
1668 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1669 * signal on both pads.
1671 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1672 * the pads were not linked together.
1677 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1679 gboolean result = FALSE;
1680 GstElement *parent = NULL;
1683 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1684 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1685 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1686 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1688 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1689 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1690 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1692 /* We need to notify the parent before taking any pad locks as the bin in
1693 * question might be waiting for a lock on the pad while holding its lock
1694 * that our message will try to take. */
1695 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1696 if (GST_IS_ELEMENT (parent)) {
1697 gst_element_post_message (parent,
1698 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1699 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1701 gst_object_unref (parent);
1706 GST_OBJECT_LOCK (srcpad);
1707 GST_OBJECT_LOCK (sinkpad);
1709 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1710 goto not_linked_together;
1712 if (GST_PAD_UNLINKFUNC (srcpad)) {
1713 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1715 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1716 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1719 /* first clear peers */
1720 GST_PAD_PEER (srcpad) = NULL;
1721 GST_PAD_PEER (sinkpad) = NULL;
1723 /* clear pending caps if any */
1724 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1725 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1727 GST_OBJECT_UNLOCK (sinkpad);
1728 GST_OBJECT_UNLOCK (srcpad);
1730 /* fire off a signal to each of the pads telling them
1731 * that they've been unlinked */
1732 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1733 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1735 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1736 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1741 if (parent != NULL) {
1742 gst_element_post_message (parent,
1743 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1744 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1745 gst_object_unref (parent);
1750 not_linked_together:
1752 /* we do not emit a warning in this case because unlinking cannot
1753 * be made MT safe.*/
1754 GST_OBJECT_UNLOCK (sinkpad);
1755 GST_OBJECT_UNLOCK (srcpad);
1761 * gst_pad_is_linked:
1762 * @pad: pad to check
1764 * Checks if a @pad is linked to another pad or not.
1766 * Returns: TRUE if the pad is linked, FALSE otherwise.
1771 gst_pad_is_linked (GstPad * pad)
1775 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1777 GST_OBJECT_LOCK (pad);
1778 result = (GST_PAD_PEER (pad) != NULL);
1779 GST_OBJECT_UNLOCK (pad);
1784 /* get the caps from both pads and see if the intersection
1787 * This function should be called with the pad LOCK on both
1791 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1792 GstPadLinkCheck flags)
1794 GstCaps *srccaps = NULL;
1795 GstCaps *sinkcaps = NULL;
1796 gboolean compatible = FALSE;
1798 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1801 /* Doing the expensive caps checking takes priority over only checking the template caps */
1802 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1803 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1804 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1806 /* If one of the two pads doesn't have a template, consider the intersection
1808 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1809 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1813 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1815 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1818 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1820 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1823 /* if we have caps on both pads we can check the intersection. If one
1824 * of the caps is NULL, we return TRUE. */
1825 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1827 gst_caps_unref (srccaps);
1829 gst_caps_unref (sinkcaps);
1833 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1834 gst_caps_unref (srccaps);
1835 gst_caps_unref (sinkcaps);
1838 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1839 (compatible ? "" : "not"));
1844 /* check if the grandparents of both pads are the same.
1845 * This check is required so that we don't try to link
1846 * pads from elements in different bins without ghostpads.
1848 * The LOCK should be held on both pads
1851 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1853 GstObject *psrc, *psink;
1855 psrc = GST_OBJECT_PARENT (src);
1856 psink = GST_OBJECT_PARENT (sink);
1858 /* if one of the pads has no parent, we allow the link */
1859 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1862 /* only care about parents that are elements */
1863 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1864 goto no_element_parent;
1866 /* if the parents are the same, we have a loop */
1867 if (G_UNLIKELY (psrc == psink))
1870 /* if they both have a parent, we check the grandparents. We can not lock
1871 * the parent because we hold on the child (pad) and the locking order is
1872 * parent >> child. */
1873 psrc = GST_OBJECT_PARENT (psrc);
1874 psink = GST_OBJECT_PARENT (psink);
1876 /* if they have grandparents but they are not the same */
1877 if (G_UNLIKELY (psrc != psink))
1878 goto wrong_grandparents;
1885 GST_CAT_DEBUG (GST_CAT_CAPS,
1886 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1887 GST_PTR_FORMAT, psrc, psink);
1892 GST_CAT_DEBUG (GST_CAT_CAPS,
1893 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1894 GST_PTR_FORMAT, psrc, psink);
1899 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1905 GST_CAT_DEBUG (GST_CAT_CAPS,
1906 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1907 GST_PTR_FORMAT, psrc, psink);
1912 /* FIXME leftover from an attempt at refactoring... */
1913 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1914 * the two pads will be locked in the srcpad, sinkpad order. */
1915 static GstPadLinkReturn
1916 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1918 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1919 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1921 GST_OBJECT_LOCK (srcpad);
1923 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1924 goto src_was_linked;
1926 GST_OBJECT_LOCK (sinkpad);
1928 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1929 goto sink_was_linked;
1931 /* check hierarchy, pads can only be linked if the grandparents
1933 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1934 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1935 goto wrong_hierarchy;
1937 /* check pad caps for non-empty intersection */
1938 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1941 /* FIXME check pad scheduling for non-empty intersection */
1943 return GST_PAD_LINK_OK;
1947 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1948 GST_DEBUG_PAD_NAME (srcpad),
1949 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1950 /* we do not emit a warning in this case because unlinking cannot
1951 * be made MT safe.*/
1952 GST_OBJECT_UNLOCK (srcpad);
1953 return GST_PAD_LINK_WAS_LINKED;
1957 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1958 GST_DEBUG_PAD_NAME (sinkpad),
1959 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1960 /* we do not emit a warning in this case because unlinking cannot
1961 * be made MT safe.*/
1962 GST_OBJECT_UNLOCK (sinkpad);
1963 GST_OBJECT_UNLOCK (srcpad);
1964 return GST_PAD_LINK_WAS_LINKED;
1968 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1969 GST_OBJECT_UNLOCK (sinkpad);
1970 GST_OBJECT_UNLOCK (srcpad);
1971 return GST_PAD_LINK_WRONG_HIERARCHY;
1975 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1976 GST_OBJECT_UNLOCK (sinkpad);
1977 GST_OBJECT_UNLOCK (srcpad);
1978 return GST_PAD_LINK_NOFORMAT;
1984 * @srcpad: the source #GstPad.
1985 * @sinkpad: the sink #GstPad.
1987 * Checks if the source pad and the sink pad are compatible so they can be
1990 * Returns: TRUE if the pads can be linked.
1993 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1995 GstPadLinkReturn result;
1997 /* generic checks */
1998 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1999 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2001 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2002 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2004 /* gst_pad_link_prepare does everything for us, we only release the locks
2005 * on the pads that it gets us. If this function returns !OK the locks are not
2007 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2008 if (result != GST_PAD_LINK_OK)
2011 GST_OBJECT_UNLOCK (srcpad);
2012 GST_OBJECT_UNLOCK (sinkpad);
2015 return result == GST_PAD_LINK_OK;
2019 * gst_pad_link_full:
2020 * @srcpad: the source #GstPad to link.
2021 * @sinkpad: the sink #GstPad to link.
2022 * @flags: the checks to validate when linking
2024 * Links the source pad and the sink pad.
2026 * This variant of #gst_pad_link provides a more granular control on the
2027 * checks being done when linking. While providing some considerable speedups
2028 * the caller of this method must be aware that wrong usage of those flags
2029 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2030 * for more information.
2034 * Returns: A result code indicating if the connection worked or
2040 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2042 GstPadLinkReturn result;
2044 GstPadLinkFunction srcfunc, sinkfunc;
2046 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2047 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2048 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2049 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2050 GST_PAD_LINK_WRONG_DIRECTION);
2052 /* Notify the parent early. See gst_pad_unlink for details. */
2053 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2054 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2055 gst_element_post_message (parent,
2056 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2057 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2059 gst_object_unref (parent);
2064 /* prepare will also lock the two pads */
2065 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2067 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2070 /* must set peers before calling the link function */
2071 GST_PAD_PEER (srcpad) = sinkpad;
2072 GST_PAD_PEER (sinkpad) = srcpad;
2074 /* make sure we update events */
2075 prepare_event_update (srcpad, sinkpad);
2077 /* get the link functions */
2078 srcfunc = GST_PAD_LINKFUNC (srcpad);
2079 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2081 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2082 /* custom link functions, execute them */
2083 GST_OBJECT_UNLOCK (sinkpad);
2084 GST_OBJECT_UNLOCK (srcpad);
2087 /* this one will call the peer link function */
2088 result = srcfunc (srcpad, sinkpad);
2089 } else if (sinkfunc) {
2090 /* if no source link function, we need to call the sink link
2091 * function ourselves. */
2092 result = sinkfunc (sinkpad, srcpad);
2095 GST_OBJECT_LOCK (srcpad);
2096 GST_OBJECT_LOCK (sinkpad);
2098 /* we released the lock, check if the same pads are linked still */
2099 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2100 goto concurrent_link;
2102 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2105 GST_OBJECT_UNLOCK (sinkpad);
2106 GST_OBJECT_UNLOCK (srcpad);
2108 /* fire off a signal to each of the pads telling them
2109 * that they've been linked */
2110 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2111 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2113 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2114 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2116 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2119 if (G_LIKELY (parent)) {
2120 gst_element_post_message (parent,
2121 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2122 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2123 gst_object_unref (parent);
2131 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2132 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2133 GST_OBJECT_UNLOCK (sinkpad);
2134 GST_OBJECT_UNLOCK (srcpad);
2136 /* The other link operation succeeded first */
2137 result = GST_PAD_LINK_WAS_LINKED;
2142 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2143 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2145 GST_PAD_PEER (srcpad) = NULL;
2146 GST_PAD_PEER (sinkpad) = NULL;
2148 GST_OBJECT_UNLOCK (sinkpad);
2149 GST_OBJECT_UNLOCK (srcpad);
2157 * @srcpad: the source #GstPad to link.
2158 * @sinkpad: the sink #GstPad to link.
2160 * Links the source pad and the sink pad.
2162 * Returns: A result code indicating if the connection worked or
2168 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2170 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2174 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2176 GstPadTemplate **template_p;
2178 /* this function would need checks if it weren't static */
2180 GST_OBJECT_LOCK (pad);
2181 template_p = &pad->padtemplate;
2182 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2183 GST_OBJECT_UNLOCK (pad);
2186 gst_pad_template_pad_created (templ, pad);
2190 * gst_pad_get_pad_template:
2193 * Gets the template for @pad.
2195 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2196 * instantiated, or %NULL if this pad has no template. Unref after
2200 gst_pad_get_pad_template (GstPad * pad)
2202 GstPadTemplate *templ;
2204 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2206 templ = GST_PAD_PAD_TEMPLATE (pad);
2208 return (templ ? gst_object_ref (templ) : NULL);
2211 /* should be called with the pad LOCK held */
2212 /* refs the caps, so caller is responsible for getting it unreffed */
2214 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2216 GstCaps *result = NULL;
2217 GstPadTemplate *templ;
2218 gboolean fixed_caps;
2220 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2222 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2224 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2225 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2226 "dispatching to pad getcaps function with "
2227 "filter %" GST_PTR_FORMAT, filter);
2229 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2230 GST_OBJECT_UNLOCK (pad);
2231 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2232 GST_OBJECT_LOCK (pad);
2233 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2235 if (result == NULL) {
2236 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2237 GST_DEBUG_PAD_NAME (pad));
2239 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2240 "pad getcaps returned %" GST_PTR_FORMAT, result);
2241 #ifndef G_DISABLE_ASSERT
2242 /* check that the returned caps are a real subset of the template caps */
2243 if (GST_PAD_PAD_TEMPLATE (pad)) {
2244 const GstCaps *templ_caps =
2245 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2246 if (!gst_caps_is_subset (result, templ_caps)) {
2249 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2250 "pad returned caps %" GST_PTR_FORMAT
2251 " which are not a real subset of its template caps %"
2252 GST_PTR_FORMAT, result, templ_caps);
2254 ("pad %s:%s returned caps which are not a real "
2255 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2256 temp = gst_caps_intersect (templ_caps, result);
2257 gst_caps_unref (result);
2262 if (!gst_caps_is_subset (result, filter)) {
2265 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2266 "pad returned caps %" GST_PTR_FORMAT
2267 " which are not a real subset of the filter caps %"
2268 GST_PTR_FORMAT, result, filter);
2269 g_warning ("pad %s:%s returned caps which are not a real "
2270 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2271 /* FIXME: Order? But shouldn't happen anyway... */
2273 gst_caps_intersect_full (filter, result,
2274 GST_CAPS_INTERSECT_FIRST);
2275 gst_caps_unref (result);
2283 if (fixed_caps && (result = get_pad_caps (pad))) {
2285 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2286 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2287 GST_PTR_FORMAT, result, result, filter, filter);
2289 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2290 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2293 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2294 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2295 result = gst_caps_ref (result);
2299 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2300 result = GST_PAD_TEMPLATE_CAPS (templ);
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2304 "using pad template %p with caps %p %" GST_PTR_FORMAT
2305 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2308 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2309 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2312 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2313 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2315 result = gst_caps_ref (result);
2320 if (!fixed_caps && (result = get_pad_caps (pad))) {
2322 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2323 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2324 GST_PTR_FORMAT, result, result, filter, filter);
2326 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2327 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2330 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2331 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2332 result = gst_caps_ref (result);
2338 /* this almost never happens */
2339 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2340 result = gst_caps_new_empty ();
2347 * gst_pad_has_current_caps:
2348 * @pad: a #GstPad to check
2350 * Check if @pad has caps set on it with gst_pad_set_caps().
2352 * Returns: TRUE when @pad has caps associated with it.
2355 gst_pad_has_current_caps (GstPad * pad)
2359 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2361 GST_OBJECT_LOCK (pad);
2362 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2363 result = (get_pad_caps (pad) != NULL);
2364 GST_OBJECT_UNLOCK (pad);
2370 * gst_pad_get_current_caps:
2371 * @pad: a #GstPad to get the current capabilities of.
2373 * Gets the capabilities currently configured on @pad with the last call to
2374 * gst_pad_set_caps().
2376 * Returns: the current caps of the pad with incremented ref-count.
2379 gst_pad_get_current_caps (GstPad * pad)
2383 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2385 GST_OBJECT_LOCK (pad);
2386 if ((result = get_pad_caps (pad)))
2387 gst_caps_ref (result);
2388 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2389 "get current pad caps %" GST_PTR_FORMAT, result);
2390 GST_OBJECT_UNLOCK (pad);
2397 * @pad: a #GstPad to get the capabilities of.
2398 * @filter: suggested #GstCaps.
2400 * Gets the capabilities this pad can produce or consume.
2401 * Note that this method doesn't necessarily return the caps set by
2402 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2403 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2404 * the pad's get_caps function;
2405 * this returns the pad template caps if not explicitly set.
2407 * When called on sinkpads @filter contains the caps that
2408 * upstream could produce in the order preferred by upstream. When
2409 * called on srcpads @filter contains the caps accepted by
2410 * downstream in the preffered order. @filter might be %NULL but
2411 * if it is not %NULL the returned caps will be a subset of @filter.
2413 * Note that this function does not return writable #GstCaps, use
2414 * gst_caps_make_writable() before modifying the caps.
2416 * Returns: the caps of the pad with incremented ref-count.
2419 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2421 GstCaps *result = NULL;
2423 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2424 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2426 GST_OBJECT_LOCK (pad);
2428 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2430 result = gst_pad_get_caps_unlocked (pad, filter);
2432 GST_OBJECT_UNLOCK (pad);
2439 * gst_pad_peer_get_caps:
2440 * @pad: a #GstPad to get the capabilities of.
2441 * @filter: a #GstCaps filter.
2443 * Gets the capabilities of the peer connected to this pad. Similar to
2444 * gst_pad_get_caps().
2446 * When called on srcpads @filter contains the caps that
2447 * upstream could produce in the order preferred by upstream. When
2448 * called on sinkpads @filter contains the caps accepted by
2449 * downstream in the preffered order. @filter might be %NULL but
2450 * if it is not %NULL the returned caps will be a subset of @filter.
2452 * Returns: the caps of the peer pad with incremented ref-count. This function
2453 * returns %NULL when there is no peer pad.
2456 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2459 GstCaps *result = NULL;
2461 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2462 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2464 GST_OBJECT_LOCK (pad);
2466 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2468 peerpad = GST_PAD_PEER (pad);
2469 if (G_UNLIKELY (peerpad == NULL))
2472 gst_object_ref (peerpad);
2473 GST_OBJECT_UNLOCK (pad);
2475 result = gst_pad_get_caps (peerpad, filter);
2477 gst_object_unref (peerpad);
2483 GST_OBJECT_UNLOCK (pad);
2489 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2491 GstStructure *s = data;
2494 if (gst_value_fixate (&v, value)) {
2495 gst_structure_id_set_value (s, field_id, &v);
2502 gst_pad_default_fixate (GstPad * pad, GstCaps * caps)
2506 /* default fixation */
2507 gst_caps_truncate (caps);
2508 s = gst_caps_get_structure (caps, 0);
2509 gst_structure_foreach (s, default_fixate, s);
2513 * gst_pad_fixate_caps:
2514 * @pad: a #GstPad to fixate
2515 * @caps: the #GstCaps to fixate
2517 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2518 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2521 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2523 GstPadFixateCapsFunction fixatefunc;
2525 g_return_if_fail (GST_IS_PAD (pad));
2526 g_return_if_fail (caps != NULL);
2527 g_return_if_fail (!gst_caps_is_empty (caps));
2528 g_return_if_fail (!gst_caps_is_any (caps));
2530 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2533 g_return_if_fail (gst_caps_is_writable (caps));
2535 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2537 fixatefunc (pad, caps);
2539 gst_pad_default_fixate (pad, caps);
2542 /* Default accept caps implementation just checks against
2543 * against the allowed caps for the pad */
2545 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2547 /* get the caps and see if it intersects to something not empty */
2549 gboolean result = FALSE;
2551 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2553 allowed = gst_pad_get_caps (pad, NULL);
2555 goto nothing_allowed;
2557 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2559 result = gst_caps_can_intersect (allowed, caps);
2561 gst_caps_unref (allowed);
2568 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2574 * gst_pad_accept_caps:
2575 * @pad: a #GstPad to check
2576 * @caps: a #GstCaps to check on the pad
2578 * Check if the given pad accepts the caps.
2580 * Returns: TRUE if the pad can accept the caps.
2583 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2586 GstPadAcceptCapsFunction acceptfunc;
2588 GstCaps *existing = NULL;
2591 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2593 /* any pad can be unnegotiated */
2597 /* lock for checking the existing caps */
2598 GST_OBJECT_LOCK (pad);
2599 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2601 /* The current caps on a pad are trivially acceptable */
2602 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2603 if (caps == existing || gst_caps_is_equal (caps, existing))
2607 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2608 GST_OBJECT_UNLOCK (pad);
2610 if (G_LIKELY (acceptfunc)) {
2611 /* we can call the function */
2612 result = acceptfunc (pad, caps);
2613 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2615 /* Only null if the element explicitly unset it */
2616 result = gst_pad_acceptcaps_default (pad, caps);
2617 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2624 GST_DEBUG_OBJECT (pad, "pad had same caps");
2625 GST_OBJECT_UNLOCK (pad);
2632 * gst_pad_peer_accept_caps:
2633 * @pad: a #GstPad to check the peer of
2634 * @caps: a #GstCaps to check on the pad
2636 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2639 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2642 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2647 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2649 GST_OBJECT_LOCK (pad);
2651 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2653 peerpad = GST_PAD_PEER (pad);
2654 if (G_UNLIKELY (peerpad == NULL))
2657 gst_object_ref (peerpad);
2658 /* release lock before calling external methods but keep ref to pad */
2659 GST_OBJECT_UNLOCK (pad);
2661 result = gst_pad_accept_caps (peerpad, caps);
2663 gst_object_unref (peerpad);
2669 GST_OBJECT_UNLOCK (pad);
2676 * @pad: a #GstPad to set the capabilities of.
2677 * @caps: (transfer none): a #GstCaps to set.
2679 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2680 * caps on the pad will be unreffed. This function refs the caps so you should
2681 * unref if as soon as you don't need it anymore.
2682 * It is possible to set NULL caps, which will make the pad unnegotiated
2685 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2686 * or bad parameters were provided to this function.
2691 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2694 gboolean res = TRUE;
2696 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2697 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2699 event = gst_event_new_caps (caps);
2701 if (GST_PAD_IS_SRC (pad))
2702 res = gst_pad_push_event (pad, event);
2704 res = gst_pad_send_event (pad, event);
2710 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2712 GstPadSetCapsFunction setcaps;
2714 GST_OBJECT_LOCK (pad);
2715 setcaps = GST_PAD_SETCAPSFUNC (pad);
2717 /* call setcaps function to configure the pad only if the
2718 * caps is not NULL */
2719 if (setcaps != NULL) {
2720 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2721 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2722 GST_OBJECT_UNLOCK (pad);
2723 if (!setcaps (pad, caps))
2724 goto setcaps_failed;
2725 GST_OBJECT_LOCK (pad);
2726 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2728 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2731 GST_OBJECT_UNLOCK (pad);
2733 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2745 do_event_function (GstPad * pad, GstEvent * event,
2746 GstPadEventFunction eventfunc)
2748 gboolean result = TRUE;
2749 GstCaps *caps, *templ;
2751 switch (GST_EVENT_TYPE (event)) {
2752 case GST_EVENT_CAPS:
2754 /* backwards compatibility mode for caps */
2755 gst_event_parse_caps (event, &caps);
2757 /* See if pad accepts the caps */
2758 templ = gst_pad_get_pad_template_caps (pad);
2759 if (!gst_caps_can_intersect (caps, templ))
2762 if (!gst_pad_call_setcaps (pad, caps))
2765 gst_caps_unref (templ);
2772 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2773 result = eventfunc (pad, event);
2780 gst_caps_unref (templ);
2781 gst_event_unref (event);
2782 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2783 "caps %" GST_PTR_FORMAT " not accepted", caps);
2788 /* function to send all pending events on the sinkpad to the event
2789 * function and collect the results. This function should be called with
2790 * the object lock. The object lock might be released by this function.
2792 static GstFlowReturn
2793 gst_pad_update_events (GstPad * pad)
2795 GstFlowReturn ret = GST_FLOW_OK;
2797 GstPadEventFunction eventfunc;
2800 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2804 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2808 ev = &pad->priv->events[i];
2810 /* skip without pending event */
2811 if ((event = ev->pending) == NULL)
2814 gst_event_ref (event);
2815 GST_OBJECT_UNLOCK (pad);
2817 res = do_event_function (pad, event, eventfunc);
2819 GST_OBJECT_LOCK (pad);
2820 /* things could have changed while we release the lock, check if we still
2821 * are handling the same event, if we don't something changed and we have
2822 * to try again. FIXME. we need a cookie here. */
2823 if (event != ev->pending) {
2824 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2828 /* remove the event from the pending entry in all cases */
2832 /* make the event active */
2834 gst_event_unref (ev->event);
2837 gst_event_unref (event);
2838 ret = GST_FLOW_ERROR;
2841 /* when we get here all events were successfully updated. */
2848 g_warning ("pad %s:%s has no event handler, file a bug.",
2849 GST_DEBUG_PAD_NAME (pad));
2850 return GST_FLOW_NOT_SUPPORTED;
2855 * gst_pad_get_pad_template_caps:
2856 * @pad: a #GstPad to get the template capabilities from.
2858 * Gets the capabilities for @pad's template.
2860 * Returns: (transfer full): the #GstCaps of this pad template.
2861 * Unref after usage.
2864 gst_pad_get_pad_template_caps (GstPad * pad)
2866 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2868 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2870 if (GST_PAD_PAD_TEMPLATE (pad))
2871 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2873 return gst_static_caps_get (&anycaps);
2878 * @pad: a #GstPad to get the peer of.
2880 * Gets the peer of @pad. This function refs the peer pad so
2881 * you need to unref it after use.
2883 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2888 gst_pad_get_peer (GstPad * pad)
2892 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2894 GST_OBJECT_LOCK (pad);
2895 result = GST_PAD_PEER (pad);
2897 gst_object_ref (result);
2898 GST_OBJECT_UNLOCK (pad);
2904 * gst_pad_get_allowed_caps:
2907 * Gets the capabilities of the allowed media types that can flow through
2908 * @pad and its peer.
2910 * The allowed capabilities is calculated as the intersection of the results of
2911 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2912 * on the resulting caps.
2914 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2915 * caps when you no longer need it. This function returns NULL when @pad
2921 gst_pad_get_allowed_caps (GstPad * pad)
2928 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2930 GST_OBJECT_LOCK (pad);
2931 peer = GST_PAD_PEER (pad);
2932 if (G_UNLIKELY (peer == NULL))
2935 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2937 gst_object_ref (peer);
2938 GST_OBJECT_UNLOCK (pad);
2939 mycaps = gst_pad_get_caps (pad, NULL);
2941 peercaps = gst_pad_get_caps (peer, NULL);
2942 gst_object_unref (peer);
2944 caps = gst_caps_intersect (mycaps, peercaps);
2945 gst_caps_unref (peercaps);
2946 gst_caps_unref (mycaps);
2948 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2955 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2956 GST_OBJECT_UNLOCK (pad);
2963 * gst_pad_get_negotiated_caps:
2966 * Gets the capabilities of the media type that currently flows through @pad
2969 * This function can be used on both src and sinkpads. Note that srcpads are
2970 * always negotiated before sinkpads so it is possible that the negotiated caps
2971 * on the srcpad do not match the negotiated caps of the peer.
2973 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2974 * the caps when you no longer need it. This function returns NULL when
2975 * the @pad has no peer or is not negotiated yet.
2980 gst_pad_get_negotiated_caps (GstPad * pad)
2982 GstCaps *caps = NULL;
2985 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2987 GST_OBJECT_LOCK (pad);
2989 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2992 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2994 if ((caps = get_pad_caps (pad)))
2995 gst_caps_ref (caps);
2997 GST_OBJECT_UNLOCK (pad);
2999 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
3006 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3007 GST_OBJECT_UNLOCK (pad);
3013 * gst_pad_iterate_internal_links_default:
3014 * @pad: the #GstPad to get the internal links of.
3016 * Iterate the list of pads to which the given pad is linked to inside of
3017 * the parent element.
3018 * This is the default handler, and thus returns an iterator of all of the
3019 * pads inside the parent element with opposite direction.
3021 * The caller must free this iterator after use with gst_iterator_free().
3023 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3024 * returned pad with gst_object_unref().
3029 gst_pad_iterate_internal_links_default (GstPad * pad)
3037 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3042 GST_OBJECT_LOCK (pad);
3043 parent = GST_PAD_PARENT (pad);
3044 if (!parent || !GST_IS_ELEMENT (parent))
3047 gst_object_ref (parent);
3048 GST_OBJECT_UNLOCK (pad);
3050 if (pad->direction == GST_PAD_SRC)
3051 padlist = &parent->sinkpads;
3053 padlist = &parent->srcpads;
3055 GST_DEBUG_OBJECT (pad, "Making iterator");
3057 cookie = &parent->pads_cookie;
3059 lock = GST_OBJECT_GET_LOCK (parent);
3062 res = gst_iterator_new_list (GST_TYPE_PAD,
3063 lock, cookie, padlist, (GObject *) owner, NULL);
3065 gst_object_unref (owner);
3072 GST_OBJECT_UNLOCK (pad);
3073 GST_DEBUG_OBJECT (pad, "no parent element");
3079 * gst_pad_iterate_internal_links:
3080 * @pad: the GstPad to get the internal links of.
3082 * Gets an iterator for the pads to which the given pad is linked to inside
3083 * of the parent element.
3085 * Each #GstPad element yielded by the iterator will have its refcount increased,
3086 * so unref after use.
3088 * Free-function: gst_iterator_free
3090 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3091 * pad does not have an iterator function configured. Use
3092 * gst_iterator_free() after usage.
3097 gst_pad_iterate_internal_links (GstPad * pad)
3099 GstIterator *res = NULL;
3101 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3103 if (GST_PAD_ITERINTLINKFUNC (pad))
3104 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3110 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3112 gboolean result = FALSE;
3114 gboolean done = FALSE;
3115 GValue item = { 0, };
3117 GList *pushed_pads = NULL;
3119 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3120 event, GST_EVENT_TYPE_NAME (event));
3122 iter = gst_pad_iterate_internal_links (pad);
3128 switch (gst_iterator_next (iter, &item)) {
3129 case GST_ITERATOR_OK:
3130 eventpad = g_value_get_object (&item);
3132 /* if already pushed, skip */
3133 if (g_list_find (pushed_pads, eventpad)) {
3134 g_value_reset (&item);
3138 if (GST_PAD_IS_SRC (eventpad)) {
3139 /* for each pad we send to, we should ref the event; it's up
3140 * to downstream to unref again when handled. */
3141 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3142 event, GST_EVENT_TYPE_NAME (event),
3143 GST_DEBUG_PAD_NAME (eventpad));
3144 gst_event_ref (event);
3145 result |= gst_pad_push_event (eventpad, event);
3147 /* we only send the event on one pad, multi-sinkpad elements
3148 * should implement a handler */
3149 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3150 event, GST_EVENT_TYPE_NAME (event),
3151 GST_DEBUG_PAD_NAME (eventpad));
3152 result = gst_pad_push_event (eventpad, event);
3157 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3159 g_value_reset (&item);
3161 case GST_ITERATOR_RESYNC:
3162 /* We don't reset the result here because we don't push the event
3163 * again on pads that got the event already and because we need
3164 * to consider the result of the previous pushes */
3165 gst_iterator_resync (iter);
3167 case GST_ITERATOR_ERROR:
3168 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3171 case GST_ITERATOR_DONE:
3176 g_value_unset (&item);
3177 gst_iterator_free (iter);
3181 /* If this is a sinkpad and we don't have pads to send the event to, we
3182 * return TRUE. This is so that when using the default handler on a sink
3183 * element, we don't fail to push it. */
3185 result = GST_PAD_IS_SINK (pad);
3187 g_list_free (pushed_pads);
3189 /* we handled the incoming event so we unref once */
3191 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3192 gst_event_unref (event);
3199 * gst_pad_event_default:
3200 * @pad: a #GstPad to call the default event handler on.
3201 * @event: (transfer full): the #GstEvent to handle.
3203 * Invokes the default event handler for the given pad. End-of-stream and
3204 * discontinuity events are handled specially, and then the event is sent to all
3205 * pads internally linked to @pad. Note that if there are many possible sink
3206 * pads that are internally linked to @pad, only one will be sent an event.
3207 * Multi-sinkpad elements should implement custom event handlers.
3209 * Returns: TRUE if the event was sent succesfully.
3212 gst_pad_event_default (GstPad * pad, GstEvent * event)
3214 gboolean result = TRUE, forward = TRUE;
3216 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3217 g_return_val_if_fail (event != NULL, FALSE);
3219 GST_LOG_OBJECT (pad, "default event handler");
3221 switch (GST_EVENT_TYPE (event)) {
3224 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3225 gst_pad_pause_task (pad);
3228 case GST_EVENT_CAPS:
3230 /* don't forward by default */
3239 result = gst_pad_event_default_dispatch (pad, event);
3241 gst_event_unref (event);
3247 * gst_pad_dispatcher:
3248 * @pad: a #GstPad to dispatch.
3249 * @dispatch: the #GstPadDispatcherFunction to call.
3250 * @data: (closure): gpointer user data passed to the dispatcher function.
3252 * Invokes the given dispatcher function on each respective peer of
3253 * all pads that are internally linked to the given pad.
3254 * The GstPadDispatcherFunction should return TRUE when no further pads
3255 * need to be processed.
3257 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3260 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3263 gboolean res = FALSE;
3264 GstIterator *iter = NULL;
3265 gboolean done = FALSE;
3266 GValue item = { 0, };
3268 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3269 g_return_val_if_fail (dispatch != NULL, FALSE);
3271 iter = gst_pad_iterate_internal_links (pad);
3277 switch (gst_iterator_next (iter, &item)) {
3278 case GST_ITERATOR_OK:
3280 GstPad *int_pad = g_value_get_object (&item);
3281 GstPad *int_peer = gst_pad_get_peer (int_pad);
3284 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3285 GST_DEBUG_PAD_NAME (int_peer));
3286 done = res = dispatch (int_peer, data);
3287 gst_object_unref (int_peer);
3289 GST_DEBUG_OBJECT (int_pad, "no peer");
3291 g_value_reset (&item);
3294 case GST_ITERATOR_RESYNC:
3295 gst_iterator_resync (iter);
3297 case GST_ITERATOR_ERROR:
3299 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3301 case GST_ITERATOR_DONE:
3306 g_value_unset (&item);
3307 gst_iterator_free (iter);
3309 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3318 * @pad: a #GstPad to invoke the default query on.
3319 * @query: (transfer none): the #GstQuery to perform.
3321 * Dispatches a query to a pad. The query should have been allocated by the
3322 * caller via one of the type-specific allocation functions. The element that
3323 * the pad belongs to is responsible for filling the query with an appropriate
3324 * response, which should then be parsed with a type-specific query parsing
3327 * Again, the caller is responsible for both the allocation and deallocation of
3328 * the query structure.
3330 * Please also note that some queries might need a running pipeline to work.
3332 * Returns: TRUE if the query could be performed.
3335 gst_pad_query (GstPad * pad, GstQuery * query)
3338 GstPadQueryFunction func;
3340 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3341 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3343 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3344 GST_QUERY_TYPE_NAME (query));
3346 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3349 res = func (pad, query);
3351 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3352 GST_QUERY_TYPE_NAME (query), res);
3358 GST_DEBUG_OBJECT (pad, "had no query function");
3364 * gst_pad_peer_query:
3365 * @pad: a #GstPad to invoke the peer query on.
3366 * @query: (transfer none): the #GstQuery to perform.
3368 * Performs gst_pad_query() on the peer of @pad.
3370 * The caller is responsible for both the allocation and deallocation of
3371 * the query structure.
3373 * Returns: TRUE if the query could be performed. This function returns %FALSE
3374 * if @pad has no peer.
3379 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3384 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3385 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3387 GST_OBJECT_LOCK (pad);
3389 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3390 GST_QUERY_TYPE_NAME (query));
3392 peerpad = GST_PAD_PEER (pad);
3393 if (G_UNLIKELY (peerpad == NULL))
3396 gst_object_ref (peerpad);
3397 GST_OBJECT_UNLOCK (pad);
3399 result = gst_pad_query (peerpad, query);
3401 gst_object_unref (peerpad);
3408 GST_WARNING_OBJECT (pad, "pad has no peer");
3409 GST_OBJECT_UNLOCK (pad);
3415 * gst_pad_query_default:
3416 * @pad: a #GstPad to call the default query handler on.
3417 * @query: (transfer none): the #GstQuery to handle.
3419 * Invokes the default query handler for the given pad.
3420 * The query is sent to all pads internally linked to @pad. Note that
3421 * if there are many possible sink pads that are internally linked to
3422 * @pad, only one will be sent the query.
3423 * Multi-sinkpad elements should implement custom query handlers.
3425 * Returns: TRUE if the query was performed succesfully.
3428 gst_pad_query_default (GstPad * pad, GstQuery * query)
3430 switch (GST_QUERY_TYPE (query)) {
3431 case GST_QUERY_POSITION:
3432 case GST_QUERY_SEEKING:
3433 case GST_QUERY_FORMATS:
3434 case GST_QUERY_LATENCY:
3435 case GST_QUERY_JITTER:
3436 case GST_QUERY_RATE:
3437 case GST_QUERY_CONVERT:
3439 return gst_pad_dispatcher
3440 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3445 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3447 GstPad *pad = data->pad;
3449 GstPadProbeCallback callback;
3452 /* if we have called this callback, do nothing */
3453 if (PROBE_COOKIE (hook) == data->cookie)
3456 PROBE_COOKIE (hook) = data->cookie;
3458 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3460 /* check if type matches */
3461 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3464 if ((flags & 0xc0 & data->mask) == 0)
3467 if ((flags & 0x6) != (data->mask & 0x6))
3470 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3471 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3473 callback = (GstPadProbeCallback) hook->func;
3474 if (callback == NULL)
3477 GST_OBJECT_UNLOCK (pad);
3479 ret = callback (pad, data->mask, data->type_data, hook->data);
3481 GST_OBJECT_LOCK (pad);
3484 case GST_PROBE_REMOVE:
3485 /* remove the probe */
3486 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3487 cleanup_hook (pad, hook);
3489 case GST_PROBE_DROP:
3490 /* need to drop the data, make sure other probes don't get called
3492 GST_DEBUG_OBJECT (pad, "asked to drop item");
3493 data->mask = GST_PROBE_TYPE_INVALID;
3494 data->ret = GST_PROBE_DROP;
3496 case GST_PROBE_PASS:
3497 /* inform the pad block to let things pass */
3498 GST_DEBUG_OBJECT (pad, "asked to pass item");
3502 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3507 #define PROBE(pad,mask,data,label) \
3509 if (G_UNLIKELY (pad->num_probes)) { \
3510 ret = do_probe_callbacks (pad, mask, data); \
3511 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3516 static GstFlowReturn
3517 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3524 data.type_data = type_data;
3525 data.ret = GST_PROBE_OK;
3527 data.cookie = pad->priv->probe_cookie++;
3530 cookie = pad->priv->probe_cookie;
3532 g_hook_list_marshal (&pad->probes, FALSE,
3533 (GHookMarshaller) probe_hook_marshal, &data);
3535 /* if the list changed, call the new callbacks (they will not have their
3536 * cookie set to data.cookie */
3537 if (cookie != pad->priv->probe_cookie) {
3538 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3539 "probe list changed, restarting");
3543 if (data.ret == GST_PROBE_DROP)
3549 if (mask & GST_PROBE_TYPE_BLOCK) {
3550 while (GST_PAD_IS_BLOCKED (pad)) {
3551 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3552 "we are blocked %d times", pad->num_blocked);
3554 /* we might have released the lock */
3555 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3558 /* now we block the streaming thread. It can be unlocked when we
3559 * deactivate the pad (which will also set the FLUSHING flag) or
3560 * when the pad is unblocked. A flushing event will also unblock
3561 * the pad after setting the FLUSHING flag. */
3562 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3563 "Waiting to be unblocked or set flushing");
3564 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3565 GST_PAD_BLOCK_WAIT (pad);
3566 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3567 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3569 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3579 GST_DEBUG_OBJECT (pad, "pad is flushing");
3580 return GST_FLOW_WRONG_STATE;
3584 GST_DEBUG_OBJECT (pad, "data is dropped");
3585 return GST_FLOW_CUSTOM_SUCCESS;
3589 GST_DEBUG_OBJECT (pad, "data is passed");
3597 * gst_pad_get_offset:
3600 * Get the offset applied to the running time of @pad. @pad has to be a source
3603 * Returns: the offset.
3606 gst_pad_get_offset (GstPad * pad)
3610 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3612 GST_OBJECT_LOCK (pad);
3613 result = pad->offset;
3614 GST_OBJECT_UNLOCK (pad);
3620 * gst_pad_set_offset:
3622 * @offset: the offset
3624 * Set the offset that will be applied to the running time of @pad.
3627 gst_pad_set_offset (GstPad * pad, gint64 offset)
3633 g_return_if_fail (GST_IS_PAD (pad));
3635 GST_OBJECT_LOCK (pad);
3636 /* if nothing changed, do nothing */
3637 if (pad->offset == offset)
3640 pad->offset = offset;
3642 /* if no peer, we just updated the offset */
3643 if ((peer = GST_PAD_PEER (pad)) == NULL)
3646 /* switch pads around when dealing with a sinkpad */
3647 if (GST_PAD_IS_SINK (pad)) {
3648 /* ref the peer so it doesn't go away when we release the lock */
3649 tmp = gst_object_ref (peer);
3650 /* make sure we get the peer (the srcpad) */
3651 GST_OBJECT_UNLOCK (pad);
3657 GST_OBJECT_LOCK (pad);
3658 /* check if the pad didn't get relinked */
3659 if (GST_PAD_PEER (pad) != peer)
3662 /* we can release the ref now */
3663 gst_object_unref (peer);
3666 /* the index of the segment event in the array */
3667 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3669 /* lock order is srcpad >> sinkpad */
3670 GST_OBJECT_LOCK (peer);
3671 /* take the current segment event, adjust it and then place
3672 * it on the sinkpad. events on the srcpad are always active. */
3673 if (replace_event (pad, peer, idx))
3674 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3676 GST_OBJECT_UNLOCK (peer);
3679 GST_OBJECT_UNLOCK (pad);
3683 /**********************************************************************
3684 * Data passing functions
3687 /* this is the chain function that does not perform the additional argument
3688 * checking for that little extra speed.
3690 static inline GstFlowReturn
3691 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3694 gboolean needs_events;
3696 GST_PAD_STREAM_LOCK (pad);
3698 GST_OBJECT_LOCK (pad);
3699 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3702 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3703 if (G_UNLIKELY (needs_events)) {
3704 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3706 GST_DEBUG_OBJECT (pad, "need to update all events");
3707 ret = gst_pad_update_events (pad);
3708 if (G_UNLIKELY (ret != GST_FLOW_OK))
3712 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3714 GST_OBJECT_UNLOCK (pad);
3716 /* NOTE: we read the chainfunc unlocked.
3717 * we cannot hold the lock for the pad so we might send
3718 * the data to the wrong function. This is not really a
3719 * problem since functions are assigned at creation time
3720 * and don't change that often... */
3721 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3722 GstPadChainFunction chainfunc;
3724 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3727 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3728 "calling chainfunction &%s with buffer %p",
3729 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3731 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3733 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3734 "called chainfunction &%s with buffer %p, returned %s",
3735 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3737 GstPadChainListFunction chainlistfunc;
3739 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3742 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3743 "calling chainlistfunction &%s",
3744 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3746 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3748 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3749 "called chainlistfunction &%s, returned %s",
3750 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3753 GST_PAD_STREAM_UNLOCK (pad);
3760 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3761 "chaining, but pad was flushing");
3762 GST_OBJECT_UNLOCK (pad);
3763 GST_PAD_STREAM_UNLOCK (pad);
3764 return GST_FLOW_WRONG_STATE;
3768 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3769 GST_OBJECT_UNLOCK (pad);
3770 GST_PAD_STREAM_UNLOCK (pad);
3775 GST_OBJECT_UNLOCK (pad);
3776 GST_PAD_STREAM_UNLOCK (pad);
3777 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3780 case GST_FLOW_CUSTOM_SUCCESS:
3781 GST_DEBUG_OBJECT (pad, "dropped buffer");
3785 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3792 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3793 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3794 "pushing, but not chainhandler");
3795 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3796 ("push on pad %s:%s but it has no chainfunction",
3797 GST_DEBUG_PAD_NAME (pad)));
3798 GST_PAD_STREAM_UNLOCK (pad);
3799 return GST_FLOW_NOT_SUPPORTED;
3805 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3806 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3809 * Chain a buffer to @pad.
3811 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3813 * If the caps on @buffer are different from the current caps on @pad, this
3814 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3815 * installed on @pad. If the new caps are not acceptable for @pad, this
3816 * function returns #GST_FLOW_NOT_NEGOTIATED.
3818 * The function proceeds calling the chain function installed on @pad (see
3819 * gst_pad_set_chain_function()) and the return value of that function is
3820 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3823 * In all cases, success or failure, the caller loses its reference to @buffer
3824 * after calling this function.
3826 * Returns: a #GstFlowReturn from the pad.
3831 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3833 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3834 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3835 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3837 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3840 static GstFlowReturn
3841 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3847 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3849 len = gst_buffer_list_len (list);
3852 for (i = 0; i < len; i++) {
3853 buffer = gst_buffer_list_get (list, i);
3855 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3856 gst_buffer_ref (buffer));
3857 if (ret != GST_FLOW_OK)
3860 gst_buffer_list_unref (list);
3866 * gst_pad_chain_list:
3867 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3868 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3871 * Chain a bufferlist to @pad.
3873 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3875 * If the caps on the first buffer of @list are different from the current
3876 * caps on @pad, this function will call any setcaps function
3877 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3878 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3880 * The function proceeds calling the chainlist function installed on @pad (see
3881 * gst_pad_set_chain_list_function()) and the return value of that function is
3882 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3883 * chainlist function.
3885 * In all cases, success or failure, the caller loses its reference to @list
3886 * after calling this function.
3890 * Returns: a #GstFlowReturn from the pad.
3895 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3897 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3898 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3899 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3901 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3904 static GstFlowReturn
3905 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3910 GST_OBJECT_LOCK (pad);
3911 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3914 type |= GST_PROBE_TYPE_PUSH;
3916 /* do block probes */
3917 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3919 /* do post-blocking probes */
3920 PROBE (pad, type, data, probe_stopped);
3922 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3925 /* take ref to peer pad before releasing the lock */
3926 gst_object_ref (peer);
3928 GST_OBJECT_UNLOCK (pad);
3930 ret = gst_pad_chain_data_unchecked (peer, type, data);
3932 gst_object_unref (peer);
3934 GST_OBJECT_LOCK (pad);
3936 if (pad->priv->using == 0) {
3937 /* pad is not active anymore, trigger idle callbacks */
3938 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3940 GST_OBJECT_UNLOCK (pad);
3944 /* ERROR recovery here */
3948 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3949 "pushing, but pad was flushing");
3950 GST_OBJECT_UNLOCK (pad);
3951 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3952 return GST_FLOW_WRONG_STATE;
3956 GST_OBJECT_UNLOCK (pad);
3957 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3960 case GST_FLOW_CUSTOM_SUCCESS:
3961 GST_DEBUG_OBJECT (pad, "dropped buffer");
3965 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3972 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3973 "pushing, but it was not linked");
3974 GST_OBJECT_UNLOCK (pad);
3975 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3976 return GST_FLOW_NOT_LINKED;
3982 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3983 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3986 * Pushes a buffer to the peer of @pad.
3988 * This function will call an installed pad block before triggering any
3989 * installed pad probes.
3991 * If the caps on @buffer are different from the currently configured caps on
3992 * @pad, this function will call any installed setcaps function on @pad (see
3993 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
3994 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
3996 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3997 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4000 * In all cases, success or failure, the caller loses its reference to @buffer
4001 * after calling this function.
4003 * Returns: a #GstFlowReturn from the peer pad.
4008 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4010 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4011 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4012 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4014 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
4018 * gst_pad_push_list:
4019 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4020 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4023 * Pushes a buffer list to the peer of @pad.
4025 * This function will call an installed pad block before triggering any
4026 * installed pad probes.
4028 * If the caps on the first buffer in the first group of @list are different
4029 * from the currently configured caps on @pad, this function will call any
4030 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4031 * case of failure to renegotiate the new format, this function returns
4032 * #GST_FLOW_NOT_NEGOTIATED.
4034 * If there are any probes installed on @pad every group of the buffer list
4035 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4036 * buffer list will be unreffed.
4038 * The function proceeds calling the chain function on the peer pad and returns
4039 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4040 * be returned. If the peer pad does not have any installed chainlist function
4041 * every group buffer of the list will be merged into a normal #GstBuffer and
4042 * chained via gst_pad_chain().
4044 * In all cases, success or failure, the caller loses its reference to @list
4045 * after calling this function.
4047 * Returns: a #GstFlowReturn from the peer pad.
4054 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4056 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4057 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4058 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4060 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
4063 static GstFlowReturn
4064 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4065 GstBuffer ** buffer)
4068 GstPadGetRangeFunction getrangefunc;
4070 GST_PAD_STREAM_LOCK (pad);
4072 GST_OBJECT_LOCK (pad);
4073 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4075 GST_OBJECT_UNLOCK (pad);
4077 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4080 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4081 "calling getrangefunc %s, offset %"
4082 G_GUINT64_FORMAT ", size %u",
4083 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4085 ret = getrangefunc (pad, offset, size, buffer);
4087 if (G_UNLIKELY (ret != GST_FLOW_OK))
4088 goto get_range_failed;
4090 /* can only fire the signal if we have a valid buffer */
4091 GST_OBJECT_LOCK (pad);
4092 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
4094 GST_OBJECT_UNLOCK (pad);
4096 GST_PAD_STREAM_UNLOCK (pad);
4103 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4104 "pulling range, but pad was flushing");
4105 GST_OBJECT_UNLOCK (pad);
4106 GST_PAD_STREAM_UNLOCK (pad);
4107 return GST_FLOW_WRONG_STATE;
4111 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4112 ("pullrange on pad %s:%s but it has no getrangefunction",
4113 GST_DEBUG_PAD_NAME (pad)));
4114 GST_PAD_STREAM_UNLOCK (pad);
4115 return GST_FLOW_NOT_SUPPORTED;
4119 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4120 "probe returned %s", gst_flow_get_name (ret));
4121 GST_OBJECT_UNLOCK (pad);
4122 GST_PAD_STREAM_UNLOCK (pad);
4123 gst_buffer_unref (*buffer);
4129 GST_PAD_STREAM_UNLOCK (pad);
4131 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4132 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4133 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4139 * gst_pad_get_range:
4140 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4141 * @offset: The start offset of the buffer
4142 * @size: The length of the buffer
4143 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4144 * returns #GST_FLOW_ERROR if %NULL.
4146 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4147 * immediatly and @buffer is %NULL.
4149 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4150 * description of a getrange function. If @pad has no getrange function
4151 * installed (see gst_pad_set_getrange_function()) this function returns
4152 * #GST_FLOW_NOT_SUPPORTED.
4154 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4156 * Returns: a #GstFlowReturn from the pad.
4161 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4162 GstBuffer ** buffer)
4164 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4165 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4166 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4168 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4172 * gst_pad_pull_range:
4173 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4174 * @offset: The start offset of the buffer
4175 * @size: The length of the buffer
4176 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4177 * GST_FLOW_ERROR if %NULL.
4179 * Pulls a @buffer from the peer pad.
4181 * This function will first trigger the pad block signal if it was
4184 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4185 * function returns the result of gst_pad_get_range() on the peer pad.
4186 * See gst_pad_get_range() for a list of return values and for the
4187 * semantics of the arguments of this function.
4189 * @buffer's caps must either be unset or the same as what is already
4190 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4193 * Returns: a #GstFlowReturn from the peer pad.
4194 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4195 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4196 * @buffer may not be used or freed when any other return value than
4197 * #GST_FLOW_OK is returned.
4202 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4203 GstBuffer ** buffer)
4207 gboolean needs_events;
4209 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4210 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4211 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4213 GST_OBJECT_LOCK (pad);
4215 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4218 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4221 gst_object_ref (peer);
4223 GST_OBJECT_UNLOCK (pad);
4225 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4227 gst_object_unref (peer);
4229 GST_OBJECT_LOCK (pad);
4231 if (pad->priv->using == 0) {
4232 /* pad is not active anymore, trigger idle callbacks */
4233 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4234 post_probe_stopped);
4237 if (G_UNLIKELY (ret != GST_FLOW_OK))
4238 goto pull_range_failed;
4240 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4241 post_probe_stopped);
4243 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4244 if (G_UNLIKELY (needs_events)) {
4245 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4247 GST_DEBUG_OBJECT (pad, "we need to update the events");
4248 ret = gst_pad_update_events (pad);
4249 if (G_UNLIKELY (ret != GST_FLOW_OK))
4252 GST_OBJECT_UNLOCK (pad);
4256 /* ERROR recovery here */
4259 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4260 gst_flow_get_name (ret));
4261 GST_OBJECT_UNLOCK (pad);
4266 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4267 "pulling range, but it was not linked");
4268 GST_OBJECT_UNLOCK (pad);
4269 return GST_FLOW_NOT_LINKED;
4274 GST_OBJECT_UNLOCK (pad);
4275 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4276 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4277 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4282 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4283 "post probe returned %s", gst_flow_get_name (ret));
4284 GST_OBJECT_UNLOCK (pad);
4285 if (ret == GST_FLOW_OK)
4286 gst_buffer_unref (*buffer);
4292 GST_OBJECT_UNLOCK (pad);
4293 gst_buffer_unref (*buffer);
4295 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4296 "pullrange returned events that were not accepted");
4302 * gst_pad_push_event:
4303 * @pad: a #GstPad to push the event to.
4304 * @event: (transfer full): the #GstEvent to send to the pad.
4306 * Sends the event to the peer of the given pad. This function is
4307 * mainly used by elements to send events to their peer
4310 * This function takes owership of the provided event so you should
4311 * gst_event_ref() it if you want to reuse the event after this call.
4313 * Returns: TRUE if the event was handled.
4318 gst_pad_push_event (GstPad * pad, GstEvent * event)
4322 gboolean result, do_event_actions = TRUE;
4323 gboolean stored = FALSE;
4325 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4326 g_return_val_if_fail (event != NULL, FALSE);
4327 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4329 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4332 GST_OBJECT_LOCK (pad);
4334 peerpad = GST_PAD_PEER (pad);
4336 /* Two checks to be made:
4337 * . (un)set the FLUSHING flag for flushing events,
4338 * . handle pad blocking */
4339 switch (GST_EVENT_TYPE (event)) {
4340 case GST_EVENT_FLUSH_START:
4341 GST_PAD_SET_FLUSHING (pad);
4343 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4344 /* flush start will have set the FLUSHING flag and will then
4345 * unlock all threads doing a GCond wait on the blocking pad. This
4346 * will typically unblock the STREAMING thread blocked on a pad. */
4347 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4348 "doing block signal.");
4349 GST_PAD_BLOCK_BROADCAST (pad);
4353 case GST_EVENT_FLUSH_STOP:
4354 GST_PAD_UNSET_FLUSHING (pad);
4355 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4356 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4362 /* store the event on the pad, but only on srcpads */
4363 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4366 idx = GST_EVENT_STICKY_IDX (event);
4367 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4368 GST_EVENT_TYPE_NAME (event), idx);
4370 /* srcpad sticky events always become active immediately */
4371 gst_event_replace (&pad->priv->events[idx].event, event);
4376 /* backwards compatibility mode for caps */
4377 if (do_event_actions) {
4378 do_event_actions = FALSE;
4380 switch (GST_EVENT_TYPE (event)) {
4381 case GST_EVENT_CAPS:
4385 GST_OBJECT_UNLOCK (pad);
4387 gst_event_parse_caps (event, &caps);
4388 /* FIXME, this is awkward because we don't check flushing here which means
4389 * that we can call the setcaps functions on flushing pads, this is not
4390 * quite what we want, otoh, this code should just go away and elements
4391 * that set caps on their srcpad should just setup stuff themselves. */
4392 gst_pad_call_setcaps (pad, caps);
4394 /* recheck everything, we released the lock */
4397 case GST_EVENT_SEGMENT:
4401 offset = pad->offset;
4402 /* check if we need to adjust the segment */
4403 if (offset != 0 && (peerpad != NULL)) {
4406 /* copy segment values */
4407 gst_event_copy_segment (event, &segment);
4408 gst_event_unref (event);
4410 /* adjust and make a new event with the offset applied */
4411 segment.base += offset;
4412 event = gst_event_new_segment (&segment);
4416 case GST_EVENT_RECONFIGURE:
4417 if (GST_PAD_IS_SINK (pad))
4418 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4424 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4427 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4428 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4434 /* send probes after modifying the events above */
4435 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4437 /* now check the peer pad */
4438 if (peerpad == NULL)
4441 gst_object_ref (peerpad);
4443 GST_OBJECT_UNLOCK (pad);
4445 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4446 GST_EVENT_TYPE_NAME (event), peerpad);
4448 result = gst_pad_send_event (peerpad, event);
4450 /* Note: we gave away ownership of the event at this point */
4451 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4454 gst_object_unref (peerpad);
4456 GST_OBJECT_LOCK (pad);
4458 if (pad->priv->using == 0) {
4459 /* pad is not active anymore, trigger idle callbacks */
4460 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4462 GST_OBJECT_UNLOCK (pad);
4464 return result | stored;
4466 /* ERROR handling */
4469 GST_DEBUG_OBJECT (pad, "We're flushing");
4470 GST_OBJECT_UNLOCK (pad);
4471 gst_event_unref (event);
4476 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4477 GST_OBJECT_UNLOCK (pad);
4478 gst_event_unref (event);
4483 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4484 GST_OBJECT_UNLOCK (pad);
4485 gst_event_unref (event);
4491 * gst_pad_send_event:
4492 * @pad: a #GstPad to send the event to.
4493 * @event: (transfer full): the #GstEvent to send to the pad.
4495 * Sends the event to the pad. This function can be used
4496 * by applications to send events in the pipeline.
4498 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4499 * sink pad, @event should be a downstream event. For example, you would not
4500 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4501 * Furthermore, some downstream events have to be serialized with data flow,
4502 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4503 * the event needs to be serialized with data flow, this function will take the
4504 * pad's stream lock while calling its event function.
4506 * To find out whether an event type is upstream, downstream, or downstream and
4507 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4508 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4509 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4510 * plugin doesn't need to bother itself with this information; the core handles
4511 * all necessary locks and checks.
4513 * This function takes owership of the provided event so you should
4514 * gst_event_ref() it if you want to reuse the event after this call.
4516 * Returns: TRUE if the event was handled.
4519 gst_pad_send_event (GstPad * pad, GstEvent * event)
4522 gboolean result = FALSE;
4523 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4525 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4526 g_return_val_if_fail (event != NULL, FALSE);
4528 GST_OBJECT_LOCK (pad);
4529 if (GST_PAD_IS_SINK (pad)) {
4530 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4531 goto wrong_direction;
4532 serialized = GST_EVENT_IS_SERIALIZED (event);
4533 sticky = GST_EVENT_IS_STICKY (event);
4534 } else if (GST_PAD_IS_SRC (pad)) {
4535 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4536 goto wrong_direction;
4537 /* events on srcpad never are serialized and sticky */
4538 serialized = sticky = FALSE;
4540 goto unknown_direction;
4542 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4544 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4546 switch (GST_EVENT_TYPE (event)) {
4547 case GST_EVENT_FLUSH_START:
4548 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4549 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4551 /* can't even accept a flush begin event when flushing */
4552 if (GST_PAD_IS_FLUSHING (pad))
4555 GST_PAD_SET_FLUSHING (pad);
4556 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4557 needs_events = FALSE;
4559 case GST_EVENT_FLUSH_STOP:
4560 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4561 GST_PAD_UNSET_FLUSHING (pad);
4562 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4564 GST_OBJECT_UNLOCK (pad);
4565 /* grab stream lock */
4566 GST_PAD_STREAM_LOCK (pad);
4568 GST_OBJECT_LOCK (pad);
4569 needs_events = FALSE;
4571 case GST_EVENT_RECONFIGURE:
4572 if (GST_PAD_IS_SRC (pad))
4573 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4575 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4576 GST_EVENT_TYPE_NAME (event));
4579 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4580 GST_OBJECT_UNLOCK (pad);
4581 GST_PAD_STREAM_LOCK (pad);
4583 GST_OBJECT_LOCK (pad);
4585 /* don't forward events on non-serialized events */
4586 needs_events = FALSE;
4589 /* store the event on the pad, but only on srcpads. We need to store the
4590 * event before checking the flushing flag. */
4595 switch (GST_EVENT_TYPE (event)) {
4596 case GST_EVENT_SEGMENT:
4597 if (pad->offset != 0) {
4600 /* copy segment values */
4601 gst_event_copy_segment (event, &segment);
4602 gst_event_unref (event);
4604 /* adjust and make a new event with the offset applied */
4605 segment.base += pad->offset;
4606 event = gst_event_new_segment (&segment);
4613 idx = GST_EVENT_STICKY_IDX (event);
4614 ev = &pad->priv->events[idx];
4616 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4617 GST_EVENT_TYPE_NAME (event), idx);
4619 if (ev->event != event) {
4620 gst_event_replace (&ev->pending, event);
4621 /* set the flag so that we update the events next time. We would
4622 * usually update below but we might be flushing too. */
4623 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4624 needs_events = TRUE;
4627 /* now do the probe */
4628 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4631 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4637 if (G_UNLIKELY (needs_events)) {
4640 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4642 GST_DEBUG_OBJECT (pad, "need to update all events");
4643 ret = gst_pad_update_events (pad);
4644 if (ret != GST_FLOW_OK)
4646 GST_OBJECT_UNLOCK (pad);
4648 gst_event_unref (event);
4652 GstPadEventFunction eventfunc;
4654 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4657 GST_OBJECT_UNLOCK (pad);
4659 result = do_event_function (pad, event, eventfunc);
4663 GST_PAD_STREAM_UNLOCK (pad);
4665 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4669 /* ERROR handling */
4672 g_warning ("pad %s:%s sending %s event in wrong direction",
4673 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4674 GST_OBJECT_UNLOCK (pad);
4675 gst_event_unref (event);
4680 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4681 GST_OBJECT_UNLOCK (pad);
4682 gst_event_unref (event);
4687 g_warning ("pad %s:%s has no event handler, file a bug.",
4688 GST_DEBUG_PAD_NAME (pad));
4689 GST_OBJECT_UNLOCK (pad);
4691 GST_PAD_STREAM_UNLOCK (pad);
4692 gst_event_unref (event);
4697 GST_OBJECT_UNLOCK (pad);
4699 GST_PAD_STREAM_UNLOCK (pad);
4700 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4701 "Received event on flushing pad. Discarding");
4702 gst_event_unref (event);
4707 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4708 GST_OBJECT_UNLOCK (pad);
4710 GST_PAD_STREAM_UNLOCK (pad);
4711 gst_event_unref (event);
4716 GST_OBJECT_UNLOCK (pad);
4718 GST_PAD_STREAM_UNLOCK (pad);
4719 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4720 gst_event_unref (event);
4726 * gst_pad_set_element_private:
4727 * @pad: the #GstPad to set the private data of.
4728 * @priv: The private data to attach to the pad.
4730 * Set the given private data gpointer on the pad.
4731 * This function can only be used by the element that owns the pad.
4732 * No locking is performed in this function.
4735 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4737 pad->element_private = priv;
4741 * gst_pad_get_element_private:
4742 * @pad: the #GstPad to get the private data of.
4744 * Gets the private data of a pad.
4745 * No locking is performed in this function.
4747 * Returns: (transfer none): a #gpointer to the private data.
4750 gst_pad_get_element_private (GstPad * pad)
4752 return pad->element_private;
4756 * gst_pad_get_sticky_event:
4757 * @pad: the #GstPad to get the event from.
4758 * @event_type: the #GstEventType that should be retrieved.
4760 * Returns a new reference of the sticky event of type @event_type
4763 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4766 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4768 GstEvent *event = NULL;
4771 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4772 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4774 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4776 GST_OBJECT_LOCK (pad);
4777 if ((event = pad->priv->events[idx].event)) {
4778 gst_event_ref (event);
4780 GST_OBJECT_UNLOCK (pad);
4786 * gst_pad_sticky_events_foreach:
4787 * @pad: the #GstPad that should be used for iteration.
4788 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4789 * @user_data: (closure): the optional user data.
4791 * Iterates all active sticky events on @pad and calls @foreach_func for every
4792 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4793 * is immediately stopped.
4795 * Returns: GST_FLOW_OK if iteration was successful
4798 gst_pad_sticky_events_foreach (GstPad * pad,
4799 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4801 GstFlowReturn ret = GST_FLOW_OK;
4805 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4806 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4808 GST_OBJECT_LOCK (pad);
4811 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4815 ev = &pad->priv->events[i];
4817 /* skip without active event */
4818 if ((event = ev->event) == NULL)
4821 gst_event_ref (event);
4822 GST_OBJECT_UNLOCK (pad);
4824 res = foreach_func (pad, event, user_data);
4826 GST_OBJECT_LOCK (pad);
4827 gst_event_unref (event);
4829 if (res != GST_FLOW_OK) {
4834 /* things could have changed while we release the lock, check if we still
4835 * are handling the same event, if we don't something changed and we have
4836 * to try again. FIXME. we need a cookie here. */
4837 if (event != ev->event) {
4838 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4842 GST_OBJECT_UNLOCK (pad);
4848 do_stream_status (GstPad * pad, GstStreamStatusType type,
4849 GThread * thread, GstTask * task)
4853 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4855 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4856 if (GST_IS_ELEMENT (parent)) {
4857 GstMessage *message;
4858 GValue value = { 0 };
4860 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4861 gchar *tname, *ename, *pname;
4863 /* create a good task name */
4864 ename = gst_element_get_name (parent);
4865 pname = gst_pad_get_name (pad);
4866 tname = g_strdup_printf ("%s:%s", ename, pname);
4870 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4874 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4877 g_value_init (&value, GST_TYPE_TASK);
4878 g_value_set_object (&value, task);
4879 gst_message_set_stream_status_object (message, &value);
4880 g_value_unset (&value);
4882 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4883 gst_element_post_message (parent, message);
4885 gst_object_unref (parent);
4890 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4892 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4897 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4899 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4903 static GstTaskThreadCallbacks thr_callbacks = {
4909 * gst_pad_start_task:
4910 * @pad: the #GstPad to start the task of
4911 * @func: the task function to call
4912 * @data: data passed to the task function
4914 * Starts a task that repeatedly calls @func with @data. This function
4915 * is mostly used in pad activation functions to start the dataflow.
4916 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4917 * before @func is called.
4919 * Returns: a %TRUE if the task could be started.
4922 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4927 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4928 g_return_val_if_fail (func != NULL, FALSE);
4930 GST_DEBUG_OBJECT (pad, "start task");
4932 GST_OBJECT_LOCK (pad);
4933 task = GST_PAD_TASK (pad);
4935 task = gst_task_create (func, data);
4936 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4937 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4938 GST_DEBUG_OBJECT (pad, "created task");
4939 GST_PAD_TASK (pad) = task;
4940 gst_object_ref (task);
4941 /* release lock to post the message */
4942 GST_OBJECT_UNLOCK (pad);
4944 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4946 gst_object_unref (task);
4948 GST_OBJECT_LOCK (pad);
4949 /* nobody else is supposed to have changed the pad now */
4950 if (GST_PAD_TASK (pad) != task)
4951 goto concurrent_stop;
4953 res = gst_task_set_state (task, GST_TASK_STARTED);
4954 GST_OBJECT_UNLOCK (pad);
4961 GST_OBJECT_UNLOCK (pad);
4967 * gst_pad_pause_task:
4968 * @pad: the #GstPad to pause the task of
4970 * Pause the task of @pad. This function will also wait until the
4971 * function executed by the task is finished if this function is not
4972 * called from the task function.
4974 * Returns: a TRUE if the task could be paused or FALSE when the pad
4978 gst_pad_pause_task (GstPad * pad)
4983 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4985 GST_DEBUG_OBJECT (pad, "pause task");
4987 GST_OBJECT_LOCK (pad);
4988 task = GST_PAD_TASK (pad);
4991 res = gst_task_set_state (task, GST_TASK_PAUSED);
4992 GST_OBJECT_UNLOCK (pad);
4994 /* wait for task function to finish, this lock is recursive so it does nothing
4995 * when the pause is called from the task itself */
4996 GST_PAD_STREAM_LOCK (pad);
4997 GST_PAD_STREAM_UNLOCK (pad);
5003 GST_DEBUG_OBJECT (pad, "pad has no task");
5004 GST_OBJECT_UNLOCK (pad);
5010 * gst_pad_stop_task:
5011 * @pad: the #GstPad to stop the task of
5013 * Stop the task of @pad. This function will also make sure that the
5014 * function executed by the task will effectively stop if not called
5015 * from the GstTaskFunction.
5017 * This function will deadlock if called from the GstTaskFunction of
5018 * the task. Use gst_task_pause() instead.
5020 * Regardless of whether the pad has a task, the stream lock is acquired and
5021 * released so as to ensure that streaming through this pad has finished.
5023 * Returns: a TRUE if the task could be stopped or FALSE on error.
5026 gst_pad_stop_task (GstPad * pad)
5031 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5033 GST_DEBUG_OBJECT (pad, "stop task");
5035 GST_OBJECT_LOCK (pad);
5036 task = GST_PAD_TASK (pad);
5039 GST_PAD_TASK (pad) = NULL;
5040 res = gst_task_set_state (task, GST_TASK_STOPPED);
5041 GST_OBJECT_UNLOCK (pad);
5043 GST_PAD_STREAM_LOCK (pad);
5044 GST_PAD_STREAM_UNLOCK (pad);
5046 if (!gst_task_join (task))
5049 gst_object_unref (task);
5055 GST_DEBUG_OBJECT (pad, "no task");
5056 GST_OBJECT_UNLOCK (pad);
5058 GST_PAD_STREAM_LOCK (pad);
5059 GST_PAD_STREAM_UNLOCK (pad);
5061 /* this is not an error */
5066 /* this is bad, possibly the application tried to join the task from
5067 * the task's thread. We install the task again so that it will be stopped
5068 * again from the right thread next time hopefully. */
5069 GST_OBJECT_LOCK (pad);
5070 GST_DEBUG_OBJECT (pad, "join failed");
5071 /* we can only install this task if there was no other task */
5072 if (GST_PAD_TASK (pad) == NULL)
5073 GST_PAD_TASK (pad) = task;
5074 GST_OBJECT_UNLOCK (pad);