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;
2045 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2046 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2047 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2048 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2049 GST_PAD_LINK_WRONG_DIRECTION);
2051 /* Notify the parent early. See gst_pad_unlink for details. */
2052 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2053 if (GST_IS_ELEMENT (parent)) {
2054 gst_element_post_message (parent,
2055 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2056 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2058 gst_object_unref (parent);
2063 /* prepare will also lock the two pads */
2064 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2066 if (result != GST_PAD_LINK_OK)
2069 /* must set peers before calling the link function */
2070 GST_PAD_PEER (srcpad) = sinkpad;
2071 GST_PAD_PEER (sinkpad) = srcpad;
2073 /* make sure we update events */
2074 prepare_event_update (srcpad, sinkpad);
2076 GST_OBJECT_UNLOCK (sinkpad);
2077 GST_OBJECT_UNLOCK (srcpad);
2079 /* FIXME released the locks here, concurrent thread might link
2080 * something else. */
2081 if (GST_PAD_LINKFUNC (srcpad)) {
2082 /* this one will call the peer link function */
2083 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2084 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2085 /* if no source link function, we need to call the sink link
2086 * function ourselves. */
2087 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2089 result = GST_PAD_LINK_OK;
2092 GST_OBJECT_LOCK (srcpad);
2093 GST_OBJECT_LOCK (sinkpad);
2095 if (result == GST_PAD_LINK_OK) {
2096 GST_OBJECT_UNLOCK (sinkpad);
2097 GST_OBJECT_UNLOCK (srcpad);
2099 /* fire off a signal to each of the pads telling them
2100 * that they've been linked */
2101 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2102 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2104 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2105 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2107 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2109 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2110 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2112 GST_PAD_PEER (srcpad) = NULL;
2113 GST_PAD_PEER (sinkpad) = NULL;
2115 GST_OBJECT_UNLOCK (sinkpad);
2116 GST_OBJECT_UNLOCK (srcpad);
2121 gst_element_post_message (parent,
2122 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2123 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2124 gst_object_unref (parent);
2132 * @srcpad: the source #GstPad to link.
2133 * @sinkpad: the sink #GstPad to link.
2135 * Links the source pad and the sink pad.
2137 * Returns: A result code indicating if the connection worked or
2143 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2145 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2149 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2151 GstPadTemplate **template_p;
2153 /* this function would need checks if it weren't static */
2155 GST_OBJECT_LOCK (pad);
2156 template_p = &pad->padtemplate;
2157 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2158 GST_OBJECT_UNLOCK (pad);
2161 gst_pad_template_pad_created (templ, pad);
2165 * gst_pad_get_pad_template:
2168 * Gets the template for @pad.
2170 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2171 * instantiated, or %NULL if this pad has no template. Unref after
2175 gst_pad_get_pad_template (GstPad * pad)
2177 GstPadTemplate *templ;
2179 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2181 templ = GST_PAD_PAD_TEMPLATE (pad);
2183 return (templ ? gst_object_ref (templ) : NULL);
2186 /* should be called with the pad LOCK held */
2187 /* refs the caps, so caller is responsible for getting it unreffed */
2189 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2191 GstCaps *result = NULL;
2192 GstPadTemplate *templ;
2193 gboolean fixed_caps;
2195 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2197 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2199 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2200 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2201 "dispatching to pad getcaps function with "
2202 "filter %" GST_PTR_FORMAT, filter);
2204 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2205 GST_OBJECT_UNLOCK (pad);
2206 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2207 GST_OBJECT_LOCK (pad);
2208 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2210 if (result == NULL) {
2211 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2212 GST_DEBUG_PAD_NAME (pad));
2214 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2215 "pad getcaps returned %" GST_PTR_FORMAT, result);
2216 #ifndef G_DISABLE_ASSERT
2217 /* check that the returned caps are a real subset of the template caps */
2218 if (GST_PAD_PAD_TEMPLATE (pad)) {
2219 const GstCaps *templ_caps =
2220 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2221 if (!gst_caps_is_subset (result, templ_caps)) {
2224 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2225 "pad returned caps %" GST_PTR_FORMAT
2226 " which are not a real subset of its template caps %"
2227 GST_PTR_FORMAT, result, templ_caps);
2229 ("pad %s:%s returned caps which are not a real "
2230 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2231 temp = gst_caps_intersect (templ_caps, result);
2232 gst_caps_unref (result);
2237 if (!gst_caps_is_subset (result, filter)) {
2240 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2241 "pad returned caps %" GST_PTR_FORMAT
2242 " which are not a real subset of the filter caps %"
2243 GST_PTR_FORMAT, result, filter);
2244 g_warning ("pad %s:%s returned caps which are not a real "
2245 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2246 /* FIXME: Order? But shouldn't happen anyway... */
2248 gst_caps_intersect_full (filter, result,
2249 GST_CAPS_INTERSECT_FIRST);
2250 gst_caps_unref (result);
2258 if (fixed_caps && (result = get_pad_caps (pad))) {
2260 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2261 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2262 GST_PTR_FORMAT, result, result, filter, filter);
2264 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2265 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2268 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2269 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2270 result = gst_caps_ref (result);
2274 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2275 result = GST_PAD_TEMPLATE_CAPS (templ);
2278 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2279 "using pad template %p with caps %p %" GST_PTR_FORMAT
2280 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2283 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2284 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2287 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2288 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2290 result = gst_caps_ref (result);
2295 if (!fixed_caps && (result = get_pad_caps (pad))) {
2297 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2298 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2299 GST_PTR_FORMAT, result, result, filter, filter);
2301 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2302 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2305 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2306 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2307 result = gst_caps_ref (result);
2313 /* this almost never happens */
2314 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2315 result = gst_caps_new_empty ();
2322 * gst_pad_has_current_caps:
2323 * @pad: a #GstPad to check
2325 * Check if @pad has caps set on it with gst_pad_set_caps().
2327 * Returns: TRUE when @pad has caps associated with it.
2330 gst_pad_has_current_caps (GstPad * pad)
2334 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2336 GST_OBJECT_LOCK (pad);
2337 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2338 result = (get_pad_caps (pad) != NULL);
2339 GST_OBJECT_UNLOCK (pad);
2345 * gst_pad_get_current_caps:
2346 * @pad: a #GstPad to get the current capabilities of.
2348 * Gets the capabilities currently configured on @pad with the last call to
2349 * gst_pad_set_caps().
2351 * Returns: the current caps of the pad with incremented ref-count.
2354 gst_pad_get_current_caps (GstPad * pad)
2358 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2360 GST_OBJECT_LOCK (pad);
2361 if ((result = get_pad_caps (pad)))
2362 gst_caps_ref (result);
2363 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2364 "get current pad caps %" GST_PTR_FORMAT, result);
2365 GST_OBJECT_UNLOCK (pad);
2372 * @pad: a #GstPad to get the capabilities of.
2373 * @filter: suggested #GstCaps.
2375 * Gets the capabilities this pad can produce or consume.
2376 * Note that this method doesn't necessarily return the caps set by
2377 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2378 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2379 * the pad's get_caps function;
2380 * this returns the pad template caps if not explicitly set.
2382 * When called on sinkpads @filter contains the caps that
2383 * upstream could produce in the order preferred by upstream. When
2384 * called on srcpads @filter contains the caps accepted by
2385 * downstream in the preffered order. @filter might be %NULL but
2386 * if it is not %NULL the returned caps will be a subset of @filter.
2388 * Note that this function does not return writable #GstCaps, use
2389 * gst_caps_make_writable() before modifying the caps.
2391 * Returns: the caps of the pad with incremented ref-count.
2394 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2396 GstCaps *result = NULL;
2398 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2399 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2401 GST_OBJECT_LOCK (pad);
2403 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2405 result = gst_pad_get_caps_unlocked (pad, filter);
2407 GST_OBJECT_UNLOCK (pad);
2414 * gst_pad_peer_get_caps:
2415 * @pad: a #GstPad to get the capabilities of.
2416 * @filter: a #GstCaps filter.
2418 * Gets the capabilities of the peer connected to this pad. Similar to
2419 * gst_pad_get_caps().
2421 * When called on srcpads @filter contains the caps that
2422 * upstream could produce in the order preferred by upstream. When
2423 * called on sinkpads @filter contains the caps accepted by
2424 * downstream in the preffered order. @filter might be %NULL but
2425 * if it is not %NULL the returned caps will be a subset of @filter.
2427 * Returns: the caps of the peer pad with incremented ref-count. This function
2428 * returns %NULL when there is no peer pad.
2431 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2434 GstCaps *result = NULL;
2436 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2437 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2439 GST_OBJECT_LOCK (pad);
2441 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2443 peerpad = GST_PAD_PEER (pad);
2444 if (G_UNLIKELY (peerpad == NULL))
2447 gst_object_ref (peerpad);
2448 GST_OBJECT_UNLOCK (pad);
2450 result = gst_pad_get_caps (peerpad, filter);
2452 gst_object_unref (peerpad);
2458 GST_OBJECT_UNLOCK (pad);
2464 fixate_value (GValue * dest, const GValue * src)
2466 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2467 g_value_init (dest, G_TYPE_INT);
2468 g_value_set_int (dest, gst_value_get_int_range_min (src));
2469 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2470 g_value_init (dest, G_TYPE_DOUBLE);
2471 g_value_set_double (dest, gst_value_get_double_range_min (src));
2472 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2473 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2474 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2475 GValue temp = { 0 };
2477 /* list could be empty */
2478 if (gst_value_list_get_size (src) <= 0)
2481 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2483 if (!fixate_value (dest, &temp))
2484 gst_value_init_and_copy (dest, &temp);
2485 g_value_unset (&temp);
2486 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2487 gboolean res = FALSE;
2490 len = gst_value_array_get_size (src);
2491 g_value_init (dest, GST_TYPE_ARRAY);
2492 for (n = 0; n < len; n++) {
2494 const GValue *orig_kid = gst_value_array_get_value (src, n);
2496 if (!fixate_value (&kid, orig_kid))
2497 gst_value_init_and_copy (&kid, orig_kid);
2500 gst_value_array_append_value (dest, &kid);
2501 g_value_unset (&kid);
2505 g_value_unset (dest);
2516 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2518 GstStructure *s = data;
2521 if (fixate_value (&v, value)) {
2522 gst_structure_id_set_value (s, field_id, &v);
2530 * gst_pad_fixate_caps:
2531 * @pad: a #GstPad to fixate
2532 * @caps: the #GstCaps to fixate
2534 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2535 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2538 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2540 GstPadFixateCapsFunction fixatefunc;
2543 g_return_if_fail (GST_IS_PAD (pad));
2544 g_return_if_fail (caps != NULL);
2545 g_return_if_fail (!gst_caps_is_empty (caps));
2546 g_return_if_fail (!gst_caps_is_any (caps));
2548 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2551 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2553 fixatefunc (pad, caps);
2556 /* default fixation */
2557 gst_caps_truncate (caps);
2558 s = gst_caps_get_structure (caps, 0);
2559 gst_structure_foreach (s, gst_pad_default_fixate, s);
2562 /* Default accept caps implementation just checks against
2563 * against the allowed caps for the pad */
2565 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2567 /* get the caps and see if it intersects to something not empty */
2569 gboolean result = FALSE;
2571 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2573 allowed = gst_pad_get_caps (pad, NULL);
2575 goto nothing_allowed;
2577 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2579 result = gst_caps_can_intersect (allowed, caps);
2581 gst_caps_unref (allowed);
2588 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2594 * gst_pad_accept_caps:
2595 * @pad: a #GstPad to check
2596 * @caps: a #GstCaps to check on the pad
2598 * Check if the given pad accepts the caps.
2600 * Returns: TRUE if the pad can accept the caps.
2603 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2606 GstPadAcceptCapsFunction acceptfunc;
2608 GstCaps *existing = NULL;
2611 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2613 /* any pad can be unnegotiated */
2617 /* lock for checking the existing caps */
2618 GST_OBJECT_LOCK (pad);
2619 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2621 /* The current caps on a pad are trivially acceptable */
2622 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2623 if (caps == existing || gst_caps_is_equal (caps, existing))
2627 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2628 GST_OBJECT_UNLOCK (pad);
2630 if (G_LIKELY (acceptfunc)) {
2631 /* we can call the function */
2632 result = acceptfunc (pad, caps);
2633 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2635 /* Only null if the element explicitly unset it */
2636 result = gst_pad_acceptcaps_default (pad, caps);
2637 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2644 GST_DEBUG_OBJECT (pad, "pad had same caps");
2645 GST_OBJECT_UNLOCK (pad);
2652 * gst_pad_peer_accept_caps:
2653 * @pad: a #GstPad to check the peer of
2654 * @caps: a #GstCaps to check on the pad
2656 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2659 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2662 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2667 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2669 GST_OBJECT_LOCK (pad);
2671 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2673 peerpad = GST_PAD_PEER (pad);
2674 if (G_UNLIKELY (peerpad == NULL))
2677 gst_object_ref (peerpad);
2678 /* release lock before calling external methods but keep ref to pad */
2679 GST_OBJECT_UNLOCK (pad);
2681 result = gst_pad_accept_caps (peerpad, caps);
2683 gst_object_unref (peerpad);
2689 GST_OBJECT_UNLOCK (pad);
2696 * @pad: a #GstPad to set the capabilities of.
2697 * @caps: (transfer none): a #GstCaps to set.
2699 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2700 * caps on the pad will be unreffed. This function refs the caps so you should
2701 * unref if as soon as you don't need it anymore.
2702 * It is possible to set NULL caps, which will make the pad unnegotiated
2705 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2706 * or bad parameters were provided to this function.
2711 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2714 gboolean res = TRUE;
2716 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2717 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2719 event = gst_event_new_caps (caps);
2721 if (GST_PAD_IS_SRC (pad))
2722 res = gst_pad_push_event (pad, event);
2724 res = gst_pad_send_event (pad, event);
2730 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2732 GstPadSetCapsFunction setcaps;
2734 GST_OBJECT_LOCK (pad);
2735 setcaps = GST_PAD_SETCAPSFUNC (pad);
2737 /* call setcaps function to configure the pad only if the
2738 * caps is not NULL */
2739 if (setcaps != NULL) {
2740 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2741 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2742 GST_OBJECT_UNLOCK (pad);
2743 if (!setcaps (pad, caps))
2744 goto setcaps_failed;
2745 GST_OBJECT_LOCK (pad);
2746 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2748 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2751 GST_OBJECT_UNLOCK (pad);
2753 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2765 do_event_function (GstPad * pad, GstEvent * event,
2766 GstPadEventFunction eventfunc)
2768 gboolean result = TRUE;
2769 GstCaps *caps, *templ;
2771 switch (GST_EVENT_TYPE (event)) {
2772 case GST_EVENT_CAPS:
2774 /* backwards compatibility mode for caps */
2775 gst_event_parse_caps (event, &caps);
2777 /* See if pad accepts the caps */
2778 templ = gst_pad_get_pad_template_caps (pad);
2779 if (!gst_caps_can_intersect (caps, templ))
2782 if (!gst_pad_call_setcaps (pad, caps))
2785 gst_caps_unref (templ);
2792 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2793 result = eventfunc (pad, event);
2800 gst_caps_unref (templ);
2801 gst_event_unref (event);
2802 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2803 "caps %" GST_PTR_FORMAT " not accepted", caps);
2808 /* function to send all pending events on the sinkpad to the event
2809 * function and collect the results. This function should be called with
2810 * the object lock. The object lock might be released by this function.
2812 static GstFlowReturn
2813 gst_pad_update_events (GstPad * pad)
2815 GstFlowReturn ret = GST_FLOW_OK;
2817 GstPadEventFunction eventfunc;
2820 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2824 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2828 ev = &pad->priv->events[i];
2830 /* skip without pending event */
2831 if ((event = ev->pending) == NULL)
2834 gst_event_ref (event);
2835 GST_OBJECT_UNLOCK (pad);
2837 res = do_event_function (pad, event, eventfunc);
2839 GST_OBJECT_LOCK (pad);
2840 /* things could have changed while we release the lock, check if we still
2841 * are handling the same event, if we don't something changed and we have
2842 * to try again. FIXME. we need a cookie here. */
2843 if (event != ev->pending) {
2844 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2848 /* remove the event from the pending entry in all cases */
2852 /* make the event active */
2854 gst_event_unref (ev->event);
2857 gst_event_unref (event);
2858 ret = GST_FLOW_ERROR;
2861 /* when we get here all events were successfully updated. */
2868 g_warning ("pad %s:%s has no event handler, file a bug.",
2869 GST_DEBUG_PAD_NAME (pad));
2870 return GST_FLOW_NOT_SUPPORTED;
2875 * gst_pad_get_pad_template_caps:
2876 * @pad: a #GstPad to get the template capabilities from.
2878 * Gets the capabilities for @pad's template.
2880 * Returns: (transfer full): the #GstCaps of this pad template.
2881 * Unref after usage.
2884 gst_pad_get_pad_template_caps (GstPad * pad)
2886 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2888 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2890 if (GST_PAD_PAD_TEMPLATE (pad))
2891 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2893 return gst_static_caps_get (&anycaps);
2898 * @pad: a #GstPad to get the peer of.
2900 * Gets the peer of @pad. This function refs the peer pad so
2901 * you need to unref it after use.
2903 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2908 gst_pad_get_peer (GstPad * pad)
2912 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2914 GST_OBJECT_LOCK (pad);
2915 result = GST_PAD_PEER (pad);
2917 gst_object_ref (result);
2918 GST_OBJECT_UNLOCK (pad);
2924 * gst_pad_get_allowed_caps:
2927 * Gets the capabilities of the allowed media types that can flow through
2928 * @pad and its peer.
2930 * The allowed capabilities is calculated as the intersection of the results of
2931 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2932 * on the resulting caps.
2934 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2935 * caps when you no longer need it. This function returns NULL when @pad
2941 gst_pad_get_allowed_caps (GstPad * pad)
2948 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2950 GST_OBJECT_LOCK (pad);
2951 peer = GST_PAD_PEER (pad);
2952 if (G_UNLIKELY (peer == NULL))
2955 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2957 gst_object_ref (peer);
2958 GST_OBJECT_UNLOCK (pad);
2959 mycaps = gst_pad_get_caps (pad, NULL);
2961 peercaps = gst_pad_get_caps (peer, NULL);
2962 gst_object_unref (peer);
2964 caps = gst_caps_intersect (mycaps, peercaps);
2965 gst_caps_unref (peercaps);
2966 gst_caps_unref (mycaps);
2968 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2975 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2976 GST_OBJECT_UNLOCK (pad);
2983 * gst_pad_get_negotiated_caps:
2986 * Gets the capabilities of the media type that currently flows through @pad
2989 * This function can be used on both src and sinkpads. Note that srcpads are
2990 * always negotiated before sinkpads so it is possible that the negotiated caps
2991 * on the srcpad do not match the negotiated caps of the peer.
2993 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2994 * the caps when you no longer need it. This function returns NULL when
2995 * the @pad has no peer or is not negotiated yet.
3000 gst_pad_get_negotiated_caps (GstPad * pad)
3002 GstCaps *caps = NULL;
3005 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3007 GST_OBJECT_LOCK (pad);
3009 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3012 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
3014 if ((caps = get_pad_caps (pad)))
3015 gst_caps_ref (caps);
3017 GST_OBJECT_UNLOCK (pad);
3019 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
3026 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3027 GST_OBJECT_UNLOCK (pad);
3033 * gst_pad_iterate_internal_links_default:
3034 * @pad: the #GstPad to get the internal links of.
3036 * Iterate the list of pads to which the given pad is linked to inside of
3037 * the parent element.
3038 * This is the default handler, and thus returns an iterator of all of the
3039 * pads inside the parent element with opposite direction.
3041 * The caller must free this iterator after use with gst_iterator_free().
3043 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3044 * returned pad with gst_object_unref().
3049 gst_pad_iterate_internal_links_default (GstPad * pad)
3057 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3062 GST_OBJECT_LOCK (pad);
3063 parent = GST_PAD_PARENT (pad);
3064 if (!parent || !GST_IS_ELEMENT (parent))
3067 gst_object_ref (parent);
3068 GST_OBJECT_UNLOCK (pad);
3070 if (pad->direction == GST_PAD_SRC)
3071 padlist = &parent->sinkpads;
3073 padlist = &parent->srcpads;
3075 GST_DEBUG_OBJECT (pad, "Making iterator");
3077 cookie = &parent->pads_cookie;
3079 lock = GST_OBJECT_GET_LOCK (parent);
3082 res = gst_iterator_new_list (GST_TYPE_PAD,
3083 lock, cookie, padlist, (GObject *) owner, NULL);
3085 gst_object_unref (owner);
3092 GST_OBJECT_UNLOCK (pad);
3093 GST_DEBUG_OBJECT (pad, "no parent element");
3099 * gst_pad_iterate_internal_links:
3100 * @pad: the GstPad to get the internal links of.
3102 * Gets an iterator for the pads to which the given pad is linked to inside
3103 * of the parent element.
3105 * Each #GstPad element yielded by the iterator will have its refcount increased,
3106 * so unref after use.
3108 * Free-function: gst_iterator_free
3110 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3111 * pad does not have an iterator function configured. Use
3112 * gst_iterator_free() after usage.
3117 gst_pad_iterate_internal_links (GstPad * pad)
3119 GstIterator *res = NULL;
3121 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3123 if (GST_PAD_ITERINTLINKFUNC (pad))
3124 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3130 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3132 gboolean result = FALSE;
3134 gboolean done = FALSE;
3135 GValue item = { 0, };
3137 GList *pushed_pads = NULL;
3139 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3140 event, GST_EVENT_TYPE_NAME (event));
3142 iter = gst_pad_iterate_internal_links (pad);
3148 switch (gst_iterator_next (iter, &item)) {
3149 case GST_ITERATOR_OK:
3150 eventpad = g_value_get_object (&item);
3152 /* if already pushed, skip */
3153 if (g_list_find (pushed_pads, eventpad)) {
3154 g_value_reset (&item);
3158 if (GST_PAD_IS_SRC (eventpad)) {
3159 /* for each pad we send to, we should ref the event; it's up
3160 * to downstream to unref again when handled. */
3161 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3162 event, GST_EVENT_TYPE_NAME (event),
3163 GST_DEBUG_PAD_NAME (eventpad));
3164 gst_event_ref (event);
3165 result |= gst_pad_push_event (eventpad, event);
3167 /* we only send the event on one pad, multi-sinkpad elements
3168 * should implement a handler */
3169 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3170 event, GST_EVENT_TYPE_NAME (event),
3171 GST_DEBUG_PAD_NAME (eventpad));
3172 result = gst_pad_push_event (eventpad, event);
3177 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3179 g_value_reset (&item);
3181 case GST_ITERATOR_RESYNC:
3182 /* We don't reset the result here because we don't push the event
3183 * again on pads that got the event already and because we need
3184 * to consider the result of the previous pushes */
3185 gst_iterator_resync (iter);
3187 case GST_ITERATOR_ERROR:
3188 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3191 case GST_ITERATOR_DONE:
3196 g_value_unset (&item);
3197 gst_iterator_free (iter);
3201 /* If this is a sinkpad and we don't have pads to send the event to, we
3202 * return TRUE. This is so that when using the default handler on a sink
3203 * element, we don't fail to push it. */
3205 result = GST_PAD_IS_SINK (pad);
3207 g_list_free (pushed_pads);
3209 /* we handled the incoming event so we unref once */
3211 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3212 gst_event_unref (event);
3219 * gst_pad_event_default:
3220 * @pad: a #GstPad to call the default event handler on.
3221 * @event: (transfer full): the #GstEvent to handle.
3223 * Invokes the default event handler for the given pad. End-of-stream and
3224 * discontinuity events are handled specially, and then the event is sent to all
3225 * pads internally linked to @pad. Note that if there are many possible sink
3226 * pads that are internally linked to @pad, only one will be sent an event.
3227 * Multi-sinkpad elements should implement custom event handlers.
3229 * Returns: TRUE if the event was sent succesfully.
3232 gst_pad_event_default (GstPad * pad, GstEvent * event)
3234 gboolean result = TRUE, forward = TRUE;
3236 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3237 g_return_val_if_fail (event != NULL, FALSE);
3239 GST_LOG_OBJECT (pad, "default event handler");
3241 switch (GST_EVENT_TYPE (event)) {
3244 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3245 gst_pad_pause_task (pad);
3248 case GST_EVENT_CAPS:
3250 /* don't forward by default */
3259 result = gst_pad_event_default_dispatch (pad, event);
3261 gst_event_unref (event);
3267 * gst_pad_dispatcher:
3268 * @pad: a #GstPad to dispatch.
3269 * @dispatch: the #GstPadDispatcherFunction to call.
3270 * @data: (closure): gpointer user data passed to the dispatcher function.
3272 * Invokes the given dispatcher function on each respective peer of
3273 * all pads that are internally linked to the given pad.
3274 * The GstPadDispatcherFunction should return TRUE when no further pads
3275 * need to be processed.
3277 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3280 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3283 gboolean res = FALSE;
3284 GstIterator *iter = NULL;
3285 gboolean done = FALSE;
3286 GValue item = { 0, };
3288 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3289 g_return_val_if_fail (dispatch != NULL, FALSE);
3291 iter = gst_pad_iterate_internal_links (pad);
3297 switch (gst_iterator_next (iter, &item)) {
3298 case GST_ITERATOR_OK:
3300 GstPad *int_pad = g_value_get_object (&item);
3301 GstPad *int_peer = gst_pad_get_peer (int_pad);
3304 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3305 GST_DEBUG_PAD_NAME (int_peer));
3306 done = res = dispatch (int_peer, data);
3307 gst_object_unref (int_peer);
3309 GST_DEBUG_OBJECT (int_pad, "no peer");
3311 g_value_reset (&item);
3314 case GST_ITERATOR_RESYNC:
3315 gst_iterator_resync (iter);
3317 case GST_ITERATOR_ERROR:
3319 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3321 case GST_ITERATOR_DONE:
3326 g_value_unset (&item);
3327 gst_iterator_free (iter);
3329 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3338 * @pad: a #GstPad to invoke the default query on.
3339 * @query: (transfer none): the #GstQuery to perform.
3341 * Dispatches a query to a pad. The query should have been allocated by the
3342 * caller via one of the type-specific allocation functions. The element that
3343 * the pad belongs to is responsible for filling the query with an appropriate
3344 * response, which should then be parsed with a type-specific query parsing
3347 * Again, the caller is responsible for both the allocation and deallocation of
3348 * the query structure.
3350 * Please also note that some queries might need a running pipeline to work.
3352 * Returns: TRUE if the query could be performed.
3355 gst_pad_query (GstPad * pad, GstQuery * query)
3358 GstPadQueryFunction func;
3360 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3361 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3363 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3364 GST_QUERY_TYPE_NAME (query));
3366 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3369 res = func (pad, query);
3371 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3372 GST_QUERY_TYPE_NAME (query), res);
3378 GST_DEBUG_OBJECT (pad, "had no query function");
3384 * gst_pad_peer_query:
3385 * @pad: a #GstPad to invoke the peer query on.
3386 * @query: (transfer none): the #GstQuery to perform.
3388 * Performs gst_pad_query() on the peer of @pad.
3390 * The caller is responsible for both the allocation and deallocation of
3391 * the query structure.
3393 * Returns: TRUE if the query could be performed. This function returns %FALSE
3394 * if @pad has no peer.
3399 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3404 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3405 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3407 GST_OBJECT_LOCK (pad);
3409 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3410 GST_QUERY_TYPE_NAME (query));
3412 peerpad = GST_PAD_PEER (pad);
3413 if (G_UNLIKELY (peerpad == NULL))
3416 gst_object_ref (peerpad);
3417 GST_OBJECT_UNLOCK (pad);
3419 result = gst_pad_query (peerpad, query);
3421 gst_object_unref (peerpad);
3428 GST_WARNING_OBJECT (pad, "pad has no peer");
3429 GST_OBJECT_UNLOCK (pad);
3435 * gst_pad_query_default:
3436 * @pad: a #GstPad to call the default query handler on.
3437 * @query: (transfer none): the #GstQuery to handle.
3439 * Invokes the default query handler for the given pad.
3440 * The query is sent to all pads internally linked to @pad. Note that
3441 * if there are many possible sink pads that are internally linked to
3442 * @pad, only one will be sent the query.
3443 * Multi-sinkpad elements should implement custom query handlers.
3445 * Returns: TRUE if the query was performed succesfully.
3448 gst_pad_query_default (GstPad * pad, GstQuery * query)
3450 switch (GST_QUERY_TYPE (query)) {
3451 case GST_QUERY_POSITION:
3452 case GST_QUERY_SEEKING:
3453 case GST_QUERY_FORMATS:
3454 case GST_QUERY_LATENCY:
3455 case GST_QUERY_JITTER:
3456 case GST_QUERY_RATE:
3457 case GST_QUERY_CONVERT:
3459 return gst_pad_dispatcher
3460 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3465 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3467 GstPad *pad = data->pad;
3469 GstPadProbeCallback callback;
3472 /* if we have called this callback, do nothing */
3473 if (PROBE_COOKIE (hook) == data->cookie)
3476 PROBE_COOKIE (hook) = data->cookie;
3478 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3480 /* check if type matches */
3481 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3484 if ((flags & 0xc0 & data->mask) == 0)
3487 if ((flags & 0x6) != (data->mask & 0x6))
3490 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3491 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3493 callback = (GstPadProbeCallback) hook->func;
3494 if (callback == NULL)
3497 GST_OBJECT_UNLOCK (pad);
3499 ret = callback (pad, data->mask, data->type_data, hook->data);
3501 GST_OBJECT_LOCK (pad);
3504 case GST_PROBE_REMOVE:
3505 /* remove the probe */
3506 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3507 cleanup_hook (pad, hook);
3509 case GST_PROBE_DROP:
3510 /* need to drop the data, make sure other probes don't get called
3512 GST_DEBUG_OBJECT (pad, "asked to drop item");
3513 data->mask = GST_PROBE_TYPE_INVALID;
3514 data->ret = GST_PROBE_DROP;
3516 case GST_PROBE_PASS:
3517 /* inform the pad block to let things pass */
3518 GST_DEBUG_OBJECT (pad, "asked to pass item");
3522 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3527 #define PROBE(pad,mask,data,label) \
3529 if (G_UNLIKELY (pad->num_probes)) { \
3530 ret = do_probe_callbacks (pad, mask, data); \
3531 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3536 static GstFlowReturn
3537 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3544 data.type_data = type_data;
3545 data.ret = GST_PROBE_OK;
3547 data.cookie = pad->priv->probe_cookie++;
3550 cookie = pad->priv->probe_cookie;
3552 g_hook_list_marshal (&pad->probes, FALSE,
3553 (GHookMarshaller) probe_hook_marshal, &data);
3555 /* if the list changed, call the new callbacks (they will not have their
3556 * cookie set to data.cookie */
3557 if (cookie != pad->priv->probe_cookie) {
3558 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3559 "probe list changed, restarting");
3563 if (data.ret == GST_PROBE_DROP)
3569 if (mask & GST_PROBE_TYPE_BLOCK) {
3570 while (GST_PAD_IS_BLOCKED (pad)) {
3571 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3572 "we are blocked %d times", pad->num_blocked);
3574 /* we might have released the lock */
3575 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3578 /* now we block the streaming thread. It can be unlocked when we
3579 * deactivate the pad (which will also set the FLUSHING flag) or
3580 * when the pad is unblocked. A flushing event will also unblock
3581 * the pad after setting the FLUSHING flag. */
3582 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3583 "Waiting to be unblocked or set flushing");
3584 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3585 GST_PAD_BLOCK_WAIT (pad);
3586 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3587 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3589 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3599 GST_DEBUG_OBJECT (pad, "pad is flushing");
3600 return GST_FLOW_WRONG_STATE;
3604 GST_DEBUG_OBJECT (pad, "data is dropped");
3605 return GST_FLOW_CUSTOM_SUCCESS;
3609 GST_DEBUG_OBJECT (pad, "data is passed");
3617 * gst_pad_get_offset:
3620 * Get the offset applied to the running time of @pad. @pad has to be a source
3623 * Returns: the offset.
3626 gst_pad_get_offset (GstPad * pad)
3630 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3632 GST_OBJECT_LOCK (pad);
3633 result = pad->offset;
3634 GST_OBJECT_UNLOCK (pad);
3640 * gst_pad_set_offset:
3642 * @offset: the offset
3644 * Set the offset that will be applied to the running time of @pad.
3647 gst_pad_set_offset (GstPad * pad, gint64 offset)
3653 g_return_if_fail (GST_IS_PAD (pad));
3655 GST_OBJECT_LOCK (pad);
3656 /* if nothing changed, do nothing */
3657 if (pad->offset == offset)
3660 pad->offset = offset;
3662 /* if no peer, we just updated the offset */
3663 if ((peer = GST_PAD_PEER (pad)) == NULL)
3666 /* switch pads around when dealing with a sinkpad */
3667 if (GST_PAD_IS_SINK (pad)) {
3668 /* ref the peer so it doesn't go away when we release the lock */
3669 tmp = gst_object_ref (peer);
3670 /* make sure we get the peer (the srcpad) */
3671 GST_OBJECT_UNLOCK (pad);
3677 GST_OBJECT_LOCK (pad);
3678 /* check if the pad didn't get relinked */
3679 if (GST_PAD_PEER (pad) != peer)
3682 /* we can release the ref now */
3683 gst_object_unref (peer);
3686 /* the index of the segment event in the array */
3687 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3689 /* lock order is srcpad >> sinkpad */
3690 GST_OBJECT_LOCK (peer);
3691 /* take the current segment event, adjust it and then place
3692 * it on the sinkpad. events on the srcpad are always active. */
3693 if (replace_event (pad, peer, idx))
3694 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3696 GST_OBJECT_UNLOCK (peer);
3699 GST_OBJECT_UNLOCK (pad);
3703 /**********************************************************************
3704 * Data passing functions
3707 /* this is the chain function that does not perform the additional argument
3708 * checking for that little extra speed.
3710 static inline GstFlowReturn
3711 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3714 gboolean needs_events;
3716 GST_PAD_STREAM_LOCK (pad);
3718 GST_OBJECT_LOCK (pad);
3719 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3722 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3723 if (G_UNLIKELY (needs_events)) {
3724 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3726 GST_DEBUG_OBJECT (pad, "need to update all events");
3727 ret = gst_pad_update_events (pad);
3728 if (G_UNLIKELY (ret != GST_FLOW_OK))
3732 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3734 GST_OBJECT_UNLOCK (pad);
3736 /* NOTE: we read the chainfunc unlocked.
3737 * we cannot hold the lock for the pad so we might send
3738 * the data to the wrong function. This is not really a
3739 * problem since functions are assigned at creation time
3740 * and don't change that often... */
3741 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3742 GstPadChainFunction chainfunc;
3744 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3747 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3748 "calling chainfunction &%s with buffer %p",
3749 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3751 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3753 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3754 "called chainfunction &%s with buffer %p, returned %s",
3755 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3757 GstPadChainListFunction chainlistfunc;
3759 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3762 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3763 "calling chainlistfunction &%s",
3764 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3766 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3768 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3769 "called chainlistfunction &%s, returned %s",
3770 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3773 GST_PAD_STREAM_UNLOCK (pad);
3780 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3781 "chaining, but pad was flushing");
3782 GST_OBJECT_UNLOCK (pad);
3783 GST_PAD_STREAM_UNLOCK (pad);
3784 return GST_FLOW_WRONG_STATE;
3788 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3789 GST_OBJECT_UNLOCK (pad);
3790 GST_PAD_STREAM_UNLOCK (pad);
3795 GST_OBJECT_UNLOCK (pad);
3796 GST_PAD_STREAM_UNLOCK (pad);
3797 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3800 case GST_FLOW_CUSTOM_SUCCESS:
3801 GST_DEBUG_OBJECT (pad, "dropped buffer");
3805 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3812 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3813 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3814 "pushing, but not chainhandler");
3815 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3816 ("push on pad %s:%s but it has no chainfunction",
3817 GST_DEBUG_PAD_NAME (pad)));
3818 GST_PAD_STREAM_UNLOCK (pad);
3819 return GST_FLOW_NOT_SUPPORTED;
3825 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3826 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3829 * Chain a buffer to @pad.
3831 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3833 * If the caps on @buffer are different from the current caps on @pad, this
3834 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3835 * installed on @pad. If the new caps are not acceptable for @pad, this
3836 * function returns #GST_FLOW_NOT_NEGOTIATED.
3838 * The function proceeds calling the chain function installed on @pad (see
3839 * gst_pad_set_chain_function()) and the return value of that function is
3840 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3843 * In all cases, success or failure, the caller loses its reference to @buffer
3844 * after calling this function.
3846 * Returns: a #GstFlowReturn from the pad.
3851 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3853 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3854 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3855 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3857 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3860 static GstFlowReturn
3861 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3867 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3869 len = gst_buffer_list_len (list);
3872 for (i = 0; i < len; i++) {
3873 buffer = gst_buffer_list_get (list, i);
3875 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3876 gst_buffer_ref (buffer));
3877 if (ret != GST_FLOW_OK)
3880 gst_buffer_list_unref (list);
3886 * gst_pad_chain_list:
3887 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3888 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3891 * Chain a bufferlist to @pad.
3893 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3895 * If the caps on the first buffer of @list are different from the current
3896 * caps on @pad, this function will call any setcaps function
3897 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3898 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3900 * The function proceeds calling the chainlist function installed on @pad (see
3901 * gst_pad_set_chain_list_function()) and the return value of that function is
3902 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3903 * chainlist function.
3905 * In all cases, success or failure, the caller loses its reference to @list
3906 * after calling this function.
3910 * Returns: a #GstFlowReturn from the pad.
3915 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3917 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3918 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3919 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3921 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3924 static GstFlowReturn
3925 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3930 GST_OBJECT_LOCK (pad);
3931 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3934 type |= GST_PROBE_TYPE_PUSH;
3936 /* do block probes */
3937 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3939 /* do post-blocking probes */
3940 PROBE (pad, type, data, probe_stopped);
3942 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3945 /* take ref to peer pad before releasing the lock */
3946 gst_object_ref (peer);
3948 GST_OBJECT_UNLOCK (pad);
3950 ret = gst_pad_chain_data_unchecked (peer, type, data);
3952 gst_object_unref (peer);
3954 GST_OBJECT_LOCK (pad);
3956 if (pad->priv->using == 0) {
3957 /* pad is not active anymore, trigger idle callbacks */
3958 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3960 GST_OBJECT_UNLOCK (pad);
3964 /* ERROR recovery here */
3968 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3969 "pushing, but pad was flushing");
3970 GST_OBJECT_UNLOCK (pad);
3971 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3972 return GST_FLOW_WRONG_STATE;
3976 GST_OBJECT_UNLOCK (pad);
3977 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3980 case GST_FLOW_CUSTOM_SUCCESS:
3981 GST_DEBUG_OBJECT (pad, "dropped buffer");
3985 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3992 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3993 "pushing, but it was not linked");
3994 GST_OBJECT_UNLOCK (pad);
3995 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3996 return GST_FLOW_NOT_LINKED;
4002 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4003 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4006 * Pushes a buffer to the peer of @pad.
4008 * This function will call an installed pad block before triggering any
4009 * installed pad probes.
4011 * If the caps on @buffer are different from the currently configured caps on
4012 * @pad, this function will call any installed setcaps function on @pad (see
4013 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4014 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4016 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4017 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4020 * In all cases, success or failure, the caller loses its reference to @buffer
4021 * after calling this function.
4023 * Returns: a #GstFlowReturn from the peer pad.
4028 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4030 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4031 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4032 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4034 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
4038 * gst_pad_push_list:
4039 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4040 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4043 * Pushes a buffer list to the peer of @pad.
4045 * This function will call an installed pad block before triggering any
4046 * installed pad probes.
4048 * If the caps on the first buffer in the first group of @list are different
4049 * from the currently configured caps on @pad, this function will call any
4050 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4051 * case of failure to renegotiate the new format, this function returns
4052 * #GST_FLOW_NOT_NEGOTIATED.
4054 * If there are any probes installed on @pad every group of the buffer list
4055 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4056 * buffer list will be unreffed.
4058 * The function proceeds calling the chain function on the peer pad and returns
4059 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4060 * be returned. If the peer pad does not have any installed chainlist function
4061 * every group buffer of the list will be merged into a normal #GstBuffer and
4062 * chained via gst_pad_chain().
4064 * In all cases, success or failure, the caller loses its reference to @list
4065 * after calling this function.
4067 * Returns: a #GstFlowReturn from the peer pad.
4074 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4076 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4077 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4078 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4080 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
4083 static GstFlowReturn
4084 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4085 GstBuffer ** buffer)
4088 GstPadGetRangeFunction getrangefunc;
4090 GST_PAD_STREAM_LOCK (pad);
4092 GST_OBJECT_LOCK (pad);
4093 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4095 GST_OBJECT_UNLOCK (pad);
4097 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4100 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4101 "calling getrangefunc %s, offset %"
4102 G_GUINT64_FORMAT ", size %u",
4103 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4105 ret = getrangefunc (pad, offset, size, buffer);
4107 if (G_UNLIKELY (ret != GST_FLOW_OK))
4108 goto get_range_failed;
4110 /* can only fire the signal if we have a valid buffer */
4111 GST_OBJECT_LOCK (pad);
4112 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
4114 GST_OBJECT_UNLOCK (pad);
4116 GST_PAD_STREAM_UNLOCK (pad);
4123 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4124 "pulling range, but pad was flushing");
4125 GST_OBJECT_UNLOCK (pad);
4126 GST_PAD_STREAM_UNLOCK (pad);
4127 return GST_FLOW_WRONG_STATE;
4131 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4132 ("pullrange on pad %s:%s but it has no getrangefunction",
4133 GST_DEBUG_PAD_NAME (pad)));
4134 GST_PAD_STREAM_UNLOCK (pad);
4135 return GST_FLOW_NOT_SUPPORTED;
4139 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4140 "probe returned %s", gst_flow_get_name (ret));
4141 GST_OBJECT_UNLOCK (pad);
4142 GST_PAD_STREAM_UNLOCK (pad);
4143 gst_buffer_unref (*buffer);
4149 GST_PAD_STREAM_UNLOCK (pad);
4151 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4152 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4153 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4159 * gst_pad_get_range:
4160 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4161 * @offset: The start offset of the buffer
4162 * @size: The length of the buffer
4163 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4164 * returns #GST_FLOW_ERROR if %NULL.
4166 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4167 * immediatly and @buffer is %NULL.
4169 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4170 * description of a getrange function. If @pad has no getrange function
4171 * installed (see gst_pad_set_getrange_function()) this function returns
4172 * #GST_FLOW_NOT_SUPPORTED.
4174 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4176 * Returns: a #GstFlowReturn from the pad.
4181 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4182 GstBuffer ** buffer)
4184 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4185 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4186 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4188 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4192 * gst_pad_pull_range:
4193 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4194 * @offset: The start offset of the buffer
4195 * @size: The length of the buffer
4196 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4197 * GST_FLOW_ERROR if %NULL.
4199 * Pulls a @buffer from the peer pad.
4201 * This function will first trigger the pad block signal if it was
4204 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4205 * function returns the result of gst_pad_get_range() on the peer pad.
4206 * See gst_pad_get_range() for a list of return values and for the
4207 * semantics of the arguments of this function.
4209 * @buffer's caps must either be unset or the same as what is already
4210 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4213 * Returns: a #GstFlowReturn from the peer pad.
4214 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4215 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4216 * @buffer may not be used or freed when any other return value than
4217 * #GST_FLOW_OK is returned.
4222 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4223 GstBuffer ** buffer)
4227 gboolean needs_events;
4229 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4230 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4231 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4233 GST_OBJECT_LOCK (pad);
4235 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4238 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4241 gst_object_ref (peer);
4243 GST_OBJECT_UNLOCK (pad);
4245 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4247 gst_object_unref (peer);
4249 GST_OBJECT_LOCK (pad);
4251 if (pad->priv->using == 0) {
4252 /* pad is not active anymore, trigger idle callbacks */
4253 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4254 post_probe_stopped);
4257 if (G_UNLIKELY (ret != GST_FLOW_OK))
4258 goto pull_range_failed;
4260 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4261 post_probe_stopped);
4263 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4264 if (G_UNLIKELY (needs_events)) {
4265 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4267 GST_DEBUG_OBJECT (pad, "we need to update the events");
4268 ret = gst_pad_update_events (pad);
4269 if (G_UNLIKELY (ret != GST_FLOW_OK))
4272 GST_OBJECT_UNLOCK (pad);
4276 /* ERROR recovery here */
4279 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4280 gst_flow_get_name (ret));
4281 GST_OBJECT_UNLOCK (pad);
4286 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4287 "pulling range, but it was not linked");
4288 GST_OBJECT_UNLOCK (pad);
4289 return GST_FLOW_NOT_LINKED;
4294 GST_OBJECT_UNLOCK (pad);
4295 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4296 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4297 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4302 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4303 "post probe returned %s", gst_flow_get_name (ret));
4304 GST_OBJECT_UNLOCK (pad);
4305 if (ret == GST_FLOW_OK)
4306 gst_buffer_unref (*buffer);
4312 GST_OBJECT_UNLOCK (pad);
4313 gst_buffer_unref (*buffer);
4315 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4316 "pullrange returned events that were not accepted");
4322 * gst_pad_push_event:
4323 * @pad: a #GstPad to push the event to.
4324 * @event: (transfer full): the #GstEvent to send to the pad.
4326 * Sends the event to the peer of the given pad. This function is
4327 * mainly used by elements to send events to their peer
4330 * This function takes owership of the provided event so you should
4331 * gst_event_ref() it if you want to reuse the event after this call.
4333 * Returns: TRUE if the event was handled.
4338 gst_pad_push_event (GstPad * pad, GstEvent * event)
4342 gboolean result, do_event_actions = TRUE;
4343 gboolean stored = FALSE;
4345 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4346 g_return_val_if_fail (event != NULL, FALSE);
4347 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4349 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4352 GST_OBJECT_LOCK (pad);
4354 peerpad = GST_PAD_PEER (pad);
4356 /* Two checks to be made:
4357 * . (un)set the FLUSHING flag for flushing events,
4358 * . handle pad blocking */
4359 switch (GST_EVENT_TYPE (event)) {
4360 case GST_EVENT_FLUSH_START:
4361 GST_PAD_SET_FLUSHING (pad);
4363 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4364 /* flush start will have set the FLUSHING flag and will then
4365 * unlock all threads doing a GCond wait on the blocking pad. This
4366 * will typically unblock the STREAMING thread blocked on a pad. */
4367 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4368 "doing block signal.");
4369 GST_PAD_BLOCK_BROADCAST (pad);
4373 case GST_EVENT_FLUSH_STOP:
4374 GST_PAD_UNSET_FLUSHING (pad);
4375 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4376 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4382 /* store the event on the pad, but only on srcpads */
4383 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4386 idx = GST_EVENT_STICKY_IDX (event);
4387 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4388 GST_EVENT_TYPE_NAME (event), idx);
4390 /* srcpad sticky events always become active immediately */
4391 gst_event_replace (&pad->priv->events[idx].event, event);
4396 /* backwards compatibility mode for caps */
4397 if (do_event_actions) {
4398 do_event_actions = FALSE;
4400 switch (GST_EVENT_TYPE (event)) {
4401 case GST_EVENT_CAPS:
4405 GST_OBJECT_UNLOCK (pad);
4407 gst_event_parse_caps (event, &caps);
4408 /* FIXME, this is awkward because we don't check flushing here which means
4409 * that we can call the setcaps functions on flushing pads, this is not
4410 * quite what we want, otoh, this code should just go away and elements
4411 * that set caps on their srcpad should just setup stuff themselves. */
4412 gst_pad_call_setcaps (pad, caps);
4414 /* recheck everything, we released the lock */
4417 case GST_EVENT_SEGMENT:
4421 offset = pad->offset;
4422 /* check if we need to adjust the segment */
4423 if (offset != 0 && (peerpad != NULL)) {
4426 /* copy segment values */
4427 gst_event_copy_segment (event, &segment);
4428 gst_event_unref (event);
4430 /* adjust and make a new event with the offset applied */
4431 segment.base += offset;
4432 event = gst_event_new_segment (&segment);
4436 case GST_EVENT_RECONFIGURE:
4437 if (GST_PAD_IS_SINK (pad))
4438 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4444 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4447 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4448 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4454 /* send probes after modifying the events above */
4455 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4457 /* now check the peer pad */
4458 if (peerpad == NULL)
4461 gst_object_ref (peerpad);
4463 GST_OBJECT_UNLOCK (pad);
4465 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4466 GST_EVENT_TYPE_NAME (event), peerpad);
4468 result = gst_pad_send_event (peerpad, event);
4470 /* Note: we gave away ownership of the event at this point */
4471 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4474 gst_object_unref (peerpad);
4476 GST_OBJECT_LOCK (pad);
4478 if (pad->priv->using == 0) {
4479 /* pad is not active anymore, trigger idle callbacks */
4480 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4482 GST_OBJECT_UNLOCK (pad);
4484 return result | stored;
4486 /* ERROR handling */
4489 GST_DEBUG_OBJECT (pad, "We're flushing");
4490 GST_OBJECT_UNLOCK (pad);
4491 gst_event_unref (event);
4496 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4497 GST_OBJECT_UNLOCK (pad);
4498 gst_event_unref (event);
4503 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4504 GST_OBJECT_UNLOCK (pad);
4505 gst_event_unref (event);
4511 * gst_pad_send_event:
4512 * @pad: a #GstPad to send the event to.
4513 * @event: (transfer full): the #GstEvent to send to the pad.
4515 * Sends the event to the pad. This function can be used
4516 * by applications to send events in the pipeline.
4518 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4519 * sink pad, @event should be a downstream event. For example, you would not
4520 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4521 * Furthermore, some downstream events have to be serialized with data flow,
4522 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4523 * the event needs to be serialized with data flow, this function will take the
4524 * pad's stream lock while calling its event function.
4526 * To find out whether an event type is upstream, downstream, or downstream and
4527 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4528 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4529 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4530 * plugin doesn't need to bother itself with this information; the core handles
4531 * all necessary locks and checks.
4533 * This function takes owership of the provided event so you should
4534 * gst_event_ref() it if you want to reuse the event after this call.
4536 * Returns: TRUE if the event was handled.
4539 gst_pad_send_event (GstPad * pad, GstEvent * event)
4542 gboolean result = FALSE;
4543 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4545 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4546 g_return_val_if_fail (event != NULL, FALSE);
4548 GST_OBJECT_LOCK (pad);
4549 if (GST_PAD_IS_SINK (pad)) {
4550 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4551 goto wrong_direction;
4552 serialized = GST_EVENT_IS_SERIALIZED (event);
4553 sticky = GST_EVENT_IS_STICKY (event);
4554 } else if (GST_PAD_IS_SRC (pad)) {
4555 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4556 goto wrong_direction;
4557 /* events on srcpad never are serialized and sticky */
4558 serialized = sticky = FALSE;
4560 goto unknown_direction;
4562 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4564 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4566 switch (GST_EVENT_TYPE (event)) {
4567 case GST_EVENT_FLUSH_START:
4568 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4569 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4571 /* can't even accept a flush begin event when flushing */
4572 if (GST_PAD_IS_FLUSHING (pad))
4575 GST_PAD_SET_FLUSHING (pad);
4576 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4577 needs_events = FALSE;
4579 case GST_EVENT_FLUSH_STOP:
4580 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4581 GST_PAD_UNSET_FLUSHING (pad);
4582 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4584 GST_OBJECT_UNLOCK (pad);
4585 /* grab stream lock */
4586 GST_PAD_STREAM_LOCK (pad);
4588 GST_OBJECT_LOCK (pad);
4589 needs_events = FALSE;
4591 case GST_EVENT_RECONFIGURE:
4592 if (GST_PAD_IS_SRC (pad))
4593 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4595 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4596 GST_EVENT_TYPE_NAME (event));
4599 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4600 GST_OBJECT_UNLOCK (pad);
4601 GST_PAD_STREAM_LOCK (pad);
4603 GST_OBJECT_LOCK (pad);
4605 /* don't forward events on non-serialized events */
4606 needs_events = FALSE;
4609 /* store the event on the pad, but only on srcpads. We need to store the
4610 * event before checking the flushing flag. */
4615 switch (GST_EVENT_TYPE (event)) {
4616 case GST_EVENT_SEGMENT:
4617 if (pad->offset != 0) {
4620 /* copy segment values */
4621 gst_event_copy_segment (event, &segment);
4622 gst_event_unref (event);
4624 /* adjust and make a new event with the offset applied */
4625 segment.base += pad->offset;
4626 event = gst_event_new_segment (&segment);
4633 idx = GST_EVENT_STICKY_IDX (event);
4634 ev = &pad->priv->events[idx];
4636 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4637 GST_EVENT_TYPE_NAME (event), idx);
4639 if (ev->event != event) {
4640 gst_event_replace (&ev->pending, event);
4641 /* set the flag so that we update the events next time. We would
4642 * usually update below but we might be flushing too. */
4643 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4644 needs_events = TRUE;
4647 /* now do the probe */
4648 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4651 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4657 if (G_UNLIKELY (needs_events)) {
4660 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4662 GST_DEBUG_OBJECT (pad, "need to update all events");
4663 ret = gst_pad_update_events (pad);
4664 if (ret != GST_FLOW_OK)
4666 GST_OBJECT_UNLOCK (pad);
4668 gst_event_unref (event);
4672 GstPadEventFunction eventfunc;
4674 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4677 GST_OBJECT_UNLOCK (pad);
4679 result = do_event_function (pad, event, eventfunc);
4683 GST_PAD_STREAM_UNLOCK (pad);
4685 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4689 /* ERROR handling */
4692 g_warning ("pad %s:%s sending %s event in wrong direction",
4693 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4694 GST_OBJECT_UNLOCK (pad);
4695 gst_event_unref (event);
4700 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4701 GST_OBJECT_UNLOCK (pad);
4702 gst_event_unref (event);
4707 g_warning ("pad %s:%s has no event handler, file a bug.",
4708 GST_DEBUG_PAD_NAME (pad));
4709 GST_OBJECT_UNLOCK (pad);
4711 GST_PAD_STREAM_UNLOCK (pad);
4712 gst_event_unref (event);
4717 GST_OBJECT_UNLOCK (pad);
4719 GST_PAD_STREAM_UNLOCK (pad);
4720 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4721 "Received event on flushing pad. Discarding");
4722 gst_event_unref (event);
4727 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4728 GST_OBJECT_UNLOCK (pad);
4730 GST_PAD_STREAM_UNLOCK (pad);
4731 gst_event_unref (event);
4736 GST_OBJECT_UNLOCK (pad);
4738 GST_PAD_STREAM_UNLOCK (pad);
4739 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4740 gst_event_unref (event);
4746 * gst_pad_set_element_private:
4747 * @pad: the #GstPad to set the private data of.
4748 * @priv: The private data to attach to the pad.
4750 * Set the given private data gpointer on the pad.
4751 * This function can only be used by the element that owns the pad.
4752 * No locking is performed in this function.
4755 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4757 pad->element_private = priv;
4761 * gst_pad_get_element_private:
4762 * @pad: the #GstPad to get the private data of.
4764 * Gets the private data of a pad.
4765 * No locking is performed in this function.
4767 * Returns: (transfer none): a #gpointer to the private data.
4770 gst_pad_get_element_private (GstPad * pad)
4772 return pad->element_private;
4776 * gst_pad_get_sticky_event:
4777 * @pad: the #GstPad to get the event from.
4778 * @event_type: the #GstEventType that should be retrieved.
4780 * Returns a new reference of the sticky event of type @event_type
4783 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4786 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4788 GstEvent *event = NULL;
4791 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4792 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4794 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4796 GST_OBJECT_LOCK (pad);
4797 if ((event = pad->priv->events[idx].event)) {
4798 gst_event_ref (event);
4800 GST_OBJECT_UNLOCK (pad);
4806 * gst_pad_sticky_events_foreach:
4807 * @pad: the #GstPad that should be used for iteration.
4808 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4809 * @user_data: (closure): the optional user data.
4811 * Iterates all active sticky events on @pad and calls @foreach_func for every
4812 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4813 * is immediately stopped.
4815 * Returns: GST_FLOW_OK if iteration was successful
4818 gst_pad_sticky_events_foreach (GstPad * pad,
4819 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4821 GstFlowReturn ret = GST_FLOW_OK;
4825 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4826 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4828 GST_OBJECT_LOCK (pad);
4831 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4835 ev = &pad->priv->events[i];
4837 /* skip without active event */
4838 if ((event = ev->event) == NULL)
4841 gst_event_ref (event);
4842 GST_OBJECT_UNLOCK (pad);
4844 res = foreach_func (pad, event, user_data);
4846 GST_OBJECT_LOCK (pad);
4847 gst_event_unref (event);
4849 if (res != GST_FLOW_OK) {
4854 /* things could have changed while we release the lock, check if we still
4855 * are handling the same event, if we don't something changed and we have
4856 * to try again. FIXME. we need a cookie here. */
4857 if (event != ev->event) {
4858 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4862 GST_OBJECT_UNLOCK (pad);
4868 do_stream_status (GstPad * pad, GstStreamStatusType type,
4869 GThread * thread, GstTask * task)
4873 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4875 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4876 if (GST_IS_ELEMENT (parent)) {
4877 GstMessage *message;
4878 GValue value = { 0 };
4880 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4881 gchar *tname, *ename, *pname;
4883 /* create a good task name */
4884 ename = gst_element_get_name (parent);
4885 pname = gst_pad_get_name (pad);
4886 tname = g_strdup_printf ("%s:%s", ename, pname);
4890 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4894 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4897 g_value_init (&value, GST_TYPE_TASK);
4898 g_value_set_object (&value, task);
4899 gst_message_set_stream_status_object (message, &value);
4900 g_value_unset (&value);
4902 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4903 gst_element_post_message (parent, message);
4905 gst_object_unref (parent);
4910 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4912 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4917 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4919 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4923 static GstTaskThreadCallbacks thr_callbacks = {
4929 * gst_pad_start_task:
4930 * @pad: the #GstPad to start the task of
4931 * @func: the task function to call
4932 * @data: data passed to the task function
4934 * Starts a task that repeatedly calls @func with @data. This function
4935 * is mostly used in pad activation functions to start the dataflow.
4936 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4937 * before @func is called.
4939 * Returns: a %TRUE if the task could be started.
4942 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4947 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4948 g_return_val_if_fail (func != NULL, FALSE);
4950 GST_DEBUG_OBJECT (pad, "start task");
4952 GST_OBJECT_LOCK (pad);
4953 task = GST_PAD_TASK (pad);
4955 task = gst_task_create (func, data);
4956 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4957 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4958 GST_DEBUG_OBJECT (pad, "created task");
4959 GST_PAD_TASK (pad) = task;
4960 gst_object_ref (task);
4961 /* release lock to post the message */
4962 GST_OBJECT_UNLOCK (pad);
4964 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4966 gst_object_unref (task);
4968 GST_OBJECT_LOCK (pad);
4969 /* nobody else is supposed to have changed the pad now */
4970 if (GST_PAD_TASK (pad) != task)
4971 goto concurrent_stop;
4973 res = gst_task_set_state (task, GST_TASK_STARTED);
4974 GST_OBJECT_UNLOCK (pad);
4981 GST_OBJECT_UNLOCK (pad);
4987 * gst_pad_pause_task:
4988 * @pad: the #GstPad to pause the task of
4990 * Pause the task of @pad. This function will also wait until the
4991 * function executed by the task is finished if this function is not
4992 * called from the task function.
4994 * Returns: a TRUE if the task could be paused or FALSE when the pad
4998 gst_pad_pause_task (GstPad * pad)
5003 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5005 GST_DEBUG_OBJECT (pad, "pause task");
5007 GST_OBJECT_LOCK (pad);
5008 task = GST_PAD_TASK (pad);
5011 res = gst_task_set_state (task, GST_TASK_PAUSED);
5012 GST_OBJECT_UNLOCK (pad);
5014 /* wait for task function to finish, this lock is recursive so it does nothing
5015 * when the pause is called from the task itself */
5016 GST_PAD_STREAM_LOCK (pad);
5017 GST_PAD_STREAM_UNLOCK (pad);
5023 GST_DEBUG_OBJECT (pad, "pad has no task");
5024 GST_OBJECT_UNLOCK (pad);
5030 * gst_pad_stop_task:
5031 * @pad: the #GstPad to stop the task of
5033 * Stop the task of @pad. This function will also make sure that the
5034 * function executed by the task will effectively stop if not called
5035 * from the GstTaskFunction.
5037 * This function will deadlock if called from the GstTaskFunction of
5038 * the task. Use gst_task_pause() instead.
5040 * Regardless of whether the pad has a task, the stream lock is acquired and
5041 * released so as to ensure that streaming through this pad has finished.
5043 * Returns: a TRUE if the task could be stopped or FALSE on error.
5046 gst_pad_stop_task (GstPad * pad)
5051 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5053 GST_DEBUG_OBJECT (pad, "stop task");
5055 GST_OBJECT_LOCK (pad);
5056 task = GST_PAD_TASK (pad);
5059 GST_PAD_TASK (pad) = NULL;
5060 res = gst_task_set_state (task, GST_TASK_STOPPED);
5061 GST_OBJECT_UNLOCK (pad);
5063 GST_PAD_STREAM_LOCK (pad);
5064 GST_PAD_STREAM_UNLOCK (pad);
5066 if (!gst_task_join (task))
5069 gst_object_unref (task);
5075 GST_DEBUG_OBJECT (pad, "no task");
5076 GST_OBJECT_UNLOCK (pad);
5078 GST_PAD_STREAM_LOCK (pad);
5079 GST_PAD_STREAM_UNLOCK (pad);
5081 /* this is not an error */
5086 /* this is bad, possibly the application tried to join the task from
5087 * the task's thread. We install the task again so that it will be stopped
5088 * again from the right thread next time hopefully. */
5089 GST_OBJECT_LOCK (pad);
5090 GST_DEBUG_OBJECT (pad, "join failed");
5091 /* we can only install this task if there was no other task */
5092 if (GST_PAD_TASK (pad) == NULL)
5093 GST_PAD_TASK (pad) = task;
5094 GST_OBJECT_UNLOCK (pad);