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 */
98 #define GST_PAD_GET_PRIVATE(obj) \
99 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
101 /* we have a pending and an active event on the pad. On source pads only the
102 * active event is used. On sinkpads, events are copied to the pending entry and
103 * moved to the active event when the eventfunc returned TRUE. */
110 struct _GstPadPrivate
112 PadEvent events[GST_EVENT_MAX_STICKY];
124 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
136 static void gst_pad_dispose (GObject * object);
137 static void gst_pad_finalize (GObject * object);
138 static void gst_pad_set_property (GObject * object, guint prop_id,
139 const GValue * value, GParamSpec * pspec);
140 static void gst_pad_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec);
143 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
144 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
145 static gboolean gst_pad_activate_default (GstPad * pad);
146 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
147 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
148 GstBufferList * list);
150 static GstObjectClass *parent_class = NULL;
151 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
153 static GParamSpec *pspec_caps = NULL;
155 /* quarks for probe signals */
156 static GQuark buffer_quark;
157 static GQuark buffer_list_quark;
158 static GQuark event_quark;
167 static GstFlowQuarks flow_quarks[] = {
168 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
169 {GST_FLOW_RESEND, "resend", 0},
170 {GST_FLOW_OK, "ok", 0},
171 {GST_FLOW_NOT_LINKED, "not-linked", 0},
172 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
173 {GST_FLOW_UNEXPECTED, "unexpected", 0},
174 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
175 {GST_FLOW_ERROR, "error", 0},
176 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
177 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
182 * @ret: a #GstFlowReturn to get the name of.
184 * Gets a string representing the given flow return.
186 * Returns: a static string with the name of the flow return.
188 G_CONST_RETURN gchar *
189 gst_flow_get_name (GstFlowReturn ret)
193 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
195 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
196 if (ret == flow_quarks[i].ret)
197 return flow_quarks[i].name;
204 * @ret: a #GstFlowReturn to get the quark of.
206 * Get the unique quark for the given GstFlowReturn.
208 * Returns: the quark associated with the flow return or 0 if an
209 * invalid return was specified.
212 gst_flow_to_quark (GstFlowReturn ret)
216 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
218 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
219 if (ret == flow_quarks[i].ret)
220 return flow_quarks[i].quark;
229 buffer_quark = g_quark_from_static_string ("buffer"); \
230 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
231 event_quark = g_quark_from_static_string ("event"); \
233 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
234 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
237 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
241 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
244 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
245 GValue * return_accu, const GValue * handler_return, gpointer dummy)
247 gboolean ret = g_value_get_boolean (handler_return);
249 GST_DEBUG ("accumulated %d", ret);
250 g_value_set_boolean (return_accu, ret);
256 default_have_data (GstPad * pad, GstMiniObject * o)
262 gst_pad_class_init (GstPadClass * klass)
264 GObjectClass *gobject_class;
265 GstObjectClass *gstobject_class;
267 gobject_class = G_OBJECT_CLASS (klass);
268 gstobject_class = GST_OBJECT_CLASS (klass);
270 g_type_class_add_private (klass, sizeof (GstPadPrivate));
272 parent_class = g_type_class_peek_parent (klass);
274 gobject_class->dispose = gst_pad_dispose;
275 gobject_class->finalize = gst_pad_finalize;
276 gobject_class->set_property = gst_pad_set_property;
277 gobject_class->get_property = gst_pad_get_property;
281 * @pad: the pad that emitted the signal
282 * @peer: the peer pad that has been connected
284 * Signals that a pad has been linked to the peer pad.
286 gst_pad_signals[PAD_LINKED] =
287 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
288 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
289 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
292 * @pad: the pad that emitted the signal
293 * @peer: the peer pad that has been disconnected
295 * Signals that a pad has been unlinked from the peer pad.
297 gst_pad_signals[PAD_UNLINKED] =
298 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
299 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
300 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
302 * GstPad::request-link:
303 * @pad: the pad that emitted the signal
304 * @peer: the peer pad for which a connection is requested
306 * Signals that a pad connection has been requested.
308 gst_pad_signals[PAD_REQUEST_LINK] =
309 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
310 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
311 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
315 * @pad: the pad that emitted the signal
316 * @mini_obj: new data
318 * Signals that new data is available on the pad. This signal is used
319 * internally for implementing pad probes.
320 * See gst_pad_add_*_probe functions.
322 * Returns: %TRUE to keep the data, %FALSE to drop it
324 gst_pad_signals[PAD_HAVE_DATA] =
325 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
326 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
327 G_STRUCT_OFFSET (GstPadClass, have_data),
328 _gst_do_pass_data_accumulator,
329 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
331 pspec_caps = g_param_spec_boxed ("caps", "Caps",
332 "The capabilities of the pad", GST_TYPE_CAPS,
333 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
334 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
336 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
337 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
338 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
339 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
340 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
341 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
342 g_param_spec_object ("template", "Template",
343 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 gstobject_class->path_string_separator = ".";
348 /* Register common function pointer descriptions */
349 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
350 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
351 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
352 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
353 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
354 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
355 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
357 klass->have_data = default_have_data;
361 gst_pad_init (GstPad * pad)
363 pad->priv = GST_PAD_GET_PRIVATE (pad);
365 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
367 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
368 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
369 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
370 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
371 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
372 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
373 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
375 GST_PAD_SET_FLUSHING (pad);
377 /* FIXME 0.11: Store this directly in the instance struct */
378 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
379 g_static_rec_mutex_init (pad->stream_rec_lock);
381 pad->block_cond = g_cond_new ();
383 g_hook_list_init (&pad->probes, sizeof (GstProbe));
386 /* called when setting the pad inactive. It removes all sticky events from
389 clear_events (PadEvent events[])
393 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
394 gst_event_replace (&events[i].event, NULL);
395 gst_event_replace (&events[i].pending, NULL);
399 /* The sticky event with @idx from the srcpad is copied to the
400 * pending event on the sinkpad (when different).
401 * This function applies the pad offsets in case of segment events.
402 * This will make sure that we send the event to the sinkpad event
403 * function when the next buffer of event arrives.
404 * Should be called with the OBJECT lock of both pads.
405 * This function returns TRUE when there is a pending event on the
408 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
410 PadEvent *srcev, *sinkev;
412 gboolean pending = FALSE;
414 srcev = &srcpad->priv->events[idx];
416 if ((event = srcev->event)) {
417 sinkev = &sinkpad->priv->events[idx];
419 switch (GST_EVENT_TYPE (event)) {
420 case GST_EVENT_SEGMENT:
425 offset = srcpad->offset + sinkpad->offset;
427 gst_event_copy_segment (event, &segment);
428 /* adjust the base time. FIXME, check negative times, try to tweak the
429 * start to do clipping on negative times */
430 segment.base += offset;
431 /* make a new event from the updated segment */
432 event = gst_event_new_segment (&segment);
439 if (sinkev->event != event) {
440 /* put in the pending entry when different */
441 gst_event_replace (&sinkev->pending, event);
450 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
455 /* make sure we push the events from the source to this new peer, for this we
456 * copy the events on the sinkpad and mark EVENTS_PENDING */
458 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
459 pending |= replace_event (srcpad, sinkpad, i);
461 /* we had some new pending events, set our flag */
463 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
466 /* should be called with the OBJECT_LOCK */
468 get_pad_caps (GstPad * pad)
470 GstCaps *caps = NULL;
474 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
475 /* we can only use the caps when we have successfully send the caps
476 * event to the event function and is thus in the active entry */
477 if ((event = pad->priv->events[idx].event))
478 gst_event_parse_caps (event, &caps);
484 gst_pad_dispose (GObject * object)
486 GstPad *pad = GST_PAD_CAST (object);
489 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
491 /* unlink the peer pad */
492 if ((peer = gst_pad_get_peer (pad))) {
493 /* window for MT unsafeness, someone else could unlink here
494 * and then we call unlink with wrong pads. The unlink
495 * function would catch this and safely return failed. */
496 if (GST_PAD_IS_SRC (pad))
497 gst_pad_unlink (pad, peer);
499 gst_pad_unlink (peer, pad);
501 gst_object_unref (peer);
504 gst_pad_set_pad_template (pad, NULL);
506 clear_events (pad->priv->events);
508 g_hook_list_clear (&pad->probes);
510 G_OBJECT_CLASS (parent_class)->dispose (object);
514 gst_pad_finalize (GObject * object)
516 GstPad *pad = GST_PAD_CAST (object);
519 /* in case the task is still around, clean it up */
520 if ((task = GST_PAD_TASK (pad))) {
521 gst_task_join (task);
522 GST_PAD_TASK (pad) = NULL;
523 gst_object_unref (task);
526 if (pad->stream_rec_lock) {
527 g_static_rec_mutex_free (pad->stream_rec_lock);
528 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
529 pad->stream_rec_lock = NULL;
531 if (pad->block_cond) {
532 g_cond_free (pad->block_cond);
533 pad->block_cond = NULL;
536 G_OBJECT_CLASS (parent_class)->finalize (object);
540 gst_pad_set_property (GObject * object, guint prop_id,
541 const GValue * value, GParamSpec * pspec)
543 g_return_if_fail (GST_IS_PAD (object));
546 case PAD_PROP_DIRECTION:
547 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
549 case PAD_PROP_TEMPLATE:
550 gst_pad_set_pad_template (GST_PAD_CAST (object),
551 (GstPadTemplate *) g_value_get_object (value));
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 gst_pad_get_property (GObject * object, guint prop_id,
561 GValue * value, GParamSpec * pspec)
563 g_return_if_fail (GST_IS_PAD (object));
567 GST_OBJECT_LOCK (object);
568 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
569 GST_OBJECT_UNLOCK (object);
571 case PAD_PROP_DIRECTION:
572 g_value_set_enum (value, GST_PAD_DIRECTION (object));
574 case PAD_PROP_TEMPLATE:
575 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
578 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585 * @name: the name of the new pad.
586 * @direction: the #GstPadDirection of the pad.
588 * Creates a new pad with the given name in the given direction.
589 * If name is NULL, a guaranteed unique name (across all pads)
591 * This function makes a copy of the name so you can safely free the name.
593 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
598 gst_pad_new (const gchar * name, GstPadDirection direction)
600 return g_object_new (GST_TYPE_PAD,
601 "name", name, "direction", direction, NULL);
605 * gst_pad_new_from_template:
606 * @templ: the pad template to use
607 * @name: the name of the element
609 * Creates a new pad with the given name from the given template.
610 * If name is NULL, a guaranteed unique name (across all pads)
612 * This function makes a copy of the name so you can safely free the name.
614 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
617 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
619 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
621 return g_object_new (GST_TYPE_PAD,
622 "name", name, "direction", templ->direction, "template", templ, NULL);
626 * gst_pad_new_from_static_template:
627 * @templ: the #GstStaticPadTemplate to use
628 * @name: the name of the element
630 * Creates a new pad with the given name from the given static template.
631 * If name is NULL, a guaranteed unique name (across all pads)
633 * This function makes a copy of the name so you can safely free the name.
635 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
638 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
642 GstPadTemplate *template;
644 template = gst_static_pad_template_get (templ);
645 pad = gst_pad_new_from_template (template, name);
646 gst_object_unref (template);
651 * gst_pad_get_direction:
652 * @pad: a #GstPad to get the direction of.
654 * Gets the direction of the pad. The direction of the pad is
655 * decided at construction time so this function does not take
658 * Returns: the #GstPadDirection of the pad.
663 gst_pad_get_direction (GstPad * pad)
665 GstPadDirection result;
667 /* PAD_UNKNOWN is a little silly but we need some sort of
668 * error return value */
669 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
671 result = GST_PAD_DIRECTION (pad);
677 gst_pad_activate_default (GstPad * pad)
679 return gst_pad_activate_push (pad, TRUE);
683 pre_activate (GstPad * pad, GstActivateMode new_mode)
686 case GST_ACTIVATE_PUSH:
687 case GST_ACTIVATE_PULL:
688 GST_OBJECT_LOCK (pad);
689 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
691 GST_PAD_UNSET_FLUSHING (pad);
692 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
693 GST_OBJECT_UNLOCK (pad);
695 case GST_ACTIVATE_NONE:
696 GST_OBJECT_LOCK (pad);
697 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
698 GST_PAD_SET_FLUSHING (pad);
699 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
700 /* unlock blocked pads so element can resume and stop */
701 GST_PAD_BLOCK_BROADCAST (pad);
702 GST_OBJECT_UNLOCK (pad);
708 post_activate (GstPad * pad, GstActivateMode new_mode)
711 case GST_ACTIVATE_PUSH:
712 case GST_ACTIVATE_PULL:
715 case GST_ACTIVATE_NONE:
716 /* ensures that streaming stops */
717 GST_PAD_STREAM_LOCK (pad);
718 GST_DEBUG_OBJECT (pad, "stopped streaming");
719 GST_OBJECT_LOCK (pad);
720 clear_events (pad->priv->events);
721 GST_OBJECT_UNLOCK (pad);
722 GST_PAD_STREAM_UNLOCK (pad);
728 * gst_pad_set_active:
729 * @pad: the #GstPad to activate or deactivate.
730 * @active: whether or not the pad should be active.
732 * Activates or deactivates the given pad.
733 * Normally called from within core state change functions.
735 * If @active, makes sure the pad is active. If it is already active, either in
736 * push or pull mode, just return. Otherwise dispatches to the pad's activate
737 * function to perform the actual activation.
739 * If not @active, checks the pad's current mode and calls
740 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
743 * Returns: #TRUE if the operation was successful.
748 gst_pad_set_active (GstPad * pad, gboolean active)
751 gboolean ret = FALSE;
753 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
755 GST_OBJECT_LOCK (pad);
756 old = GST_PAD_ACTIVATE_MODE (pad);
757 GST_OBJECT_UNLOCK (pad);
761 case GST_ACTIVATE_PUSH:
762 GST_DEBUG_OBJECT (pad, "activating pad from push");
765 case GST_ACTIVATE_PULL:
766 GST_DEBUG_OBJECT (pad, "activating pad from pull");
769 case GST_ACTIVATE_NONE:
770 GST_DEBUG_OBJECT (pad, "activating pad from none");
771 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
774 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
779 case GST_ACTIVATE_PUSH:
780 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
781 ret = gst_pad_activate_push (pad, FALSE);
783 case GST_ACTIVATE_PULL:
784 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
785 ret = gst_pad_activate_pull (pad, FALSE);
787 case GST_ACTIVATE_NONE:
788 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
792 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
798 GST_OBJECT_LOCK (pad);
800 g_critical ("Failed to deactivate pad %s:%s, very bad",
801 GST_DEBUG_PAD_NAME (pad));
803 GST_WARNING_OBJECT (pad, "Failed to activate pad");
805 GST_OBJECT_UNLOCK (pad);
808 GST_OBJECT_LOCK (pad);
809 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
810 GST_OBJECT_UNLOCK (pad);
818 * gst_pad_activate_pull:
819 * @pad: the #GstPad to activate or deactivate.
820 * @active: whether or not the pad should be active.
822 * Activates or deactivates the given pad in pull mode via dispatching to the
823 * pad's activatepullfunc. For use from within pad activation functions only.
824 * When called on sink pads, will first proxy the call to the peer pad, which
825 * is expected to activate its internally linked pads from within its
826 * activate_pull function.
828 * If you don't know what this is, you probably don't want to call it.
830 * Returns: TRUE if the operation was successful.
835 gst_pad_activate_pull (GstPad * pad, gboolean active)
837 GstActivateMode old, new;
840 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
842 GST_OBJECT_LOCK (pad);
843 old = GST_PAD_ACTIVATE_MODE (pad);
844 GST_OBJECT_UNLOCK (pad);
848 case GST_ACTIVATE_PULL:
849 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
851 case GST_ACTIVATE_PUSH:
852 GST_DEBUG_OBJECT (pad,
853 "activating pad from push, deactivate push first");
854 /* pad was activate in the wrong direction, deactivate it
855 * and reactivate it in pull mode */
856 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
857 goto deactivate_failed;
858 /* fallthrough, pad is deactivated now. */
859 case GST_ACTIVATE_NONE:
860 GST_DEBUG_OBJECT (pad, "activating pad from none");
865 case GST_ACTIVATE_NONE:
866 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
868 case GST_ACTIVATE_PUSH:
869 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
870 /* pad was activated in the other direction, deactivate it
871 * in push mode, this should not happen... */
872 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
873 goto deactivate_failed;
874 /* everything is fine now */
876 case GST_ACTIVATE_PULL:
877 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
882 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
883 if ((peer = gst_pad_get_peer (pad))) {
884 GST_DEBUG_OBJECT (pad, "calling peer");
885 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
887 gst_object_unref (peer);
889 /* there is no peer, this is only fatal when we activate. When we
890 * deactivate, we must assume the application has unlinked the peer and
891 * will deactivate it eventually. */
895 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
898 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
899 goto failure; /* Can't activate pull on a src without a
903 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
904 pre_activate (pad, new);
906 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
907 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
910 /* can happen for sinks of passthrough elements */
913 post_activate (pad, new);
915 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
916 active ? "activated" : "deactivated");
922 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
923 active ? "activated" : "deactivated");
928 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
929 "failed to %s in switch to pull from mode %d",
930 (active ? "activate" : "deactivate"), old);
935 GST_OBJECT_LOCK (peer);
936 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
937 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
938 GST_OBJECT_UNLOCK (peer);
939 gst_object_unref (peer);
944 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
950 GST_OBJECT_LOCK (pad);
951 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
952 active ? "activate" : "deactivate");
953 GST_PAD_SET_FLUSHING (pad);
954 GST_PAD_ACTIVATE_MODE (pad) = old;
955 GST_OBJECT_UNLOCK (pad);
961 * gst_pad_activate_push:
962 * @pad: the #GstPad to activate or deactivate.
963 * @active: whether the pad should be active or not.
965 * Activates or deactivates the given pad in push mode via dispatching to the
966 * pad's activatepushfunc. For use from within pad activation functions only.
968 * If you don't know what this is, you probably don't want to call it.
970 * Returns: %TRUE if the operation was successful.
975 gst_pad_activate_push (GstPad * pad, gboolean active)
977 GstActivateMode old, new;
979 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
980 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
981 active ? "activated" : "deactivated");
983 GST_OBJECT_LOCK (pad);
984 old = GST_PAD_ACTIVATE_MODE (pad);
985 GST_OBJECT_UNLOCK (pad);
989 case GST_ACTIVATE_PUSH:
990 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
992 case GST_ACTIVATE_PULL:
993 GST_DEBUG_OBJECT (pad,
994 "activating pad from push, deactivating pull first");
995 /* pad was activate in the wrong direction, deactivate it
996 * an reactivate it in push mode */
997 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
998 goto deactivate_failed;
999 /* fallthrough, pad is deactivated now. */
1000 case GST_ACTIVATE_NONE:
1001 GST_DEBUG_OBJECT (pad, "activating pad from none");
1006 case GST_ACTIVATE_NONE:
1007 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
1009 case GST_ACTIVATE_PULL:
1010 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
1011 /* pad was activated in the other direction, deactivate it
1012 * in pull mode, this should not happen... */
1013 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
1014 goto deactivate_failed;
1015 /* everything is fine now */
1017 case GST_ACTIVATE_PUSH:
1018 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
1023 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1024 pre_activate (pad, new);
1026 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
1027 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
1031 /* quite ok, element relies on state change func to prepare itself */
1034 post_activate (pad, new);
1036 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
1037 active ? "activated" : "deactivated");
1042 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
1043 active ? "activated" : "deactivated");
1048 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1049 "failed to %s in switch to push from mode %d",
1050 (active ? "activate" : "deactivate"), old);
1055 GST_OBJECT_LOCK (pad);
1056 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1057 active ? "activate" : "deactivate");
1058 GST_PAD_SET_FLUSHING (pad);
1059 GST_PAD_ACTIVATE_MODE (pad) = old;
1060 GST_OBJECT_UNLOCK (pad);
1066 * gst_pad_is_active:
1067 * @pad: the #GstPad to query
1069 * Query if a pad is active
1071 * Returns: TRUE if the pad is active.
1076 gst_pad_is_active (GstPad * pad)
1078 gboolean result = FALSE;
1080 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1082 GST_OBJECT_LOCK (pad);
1083 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1084 GST_OBJECT_UNLOCK (pad);
1090 * gst_pad_add_probe:
1091 * @pad: the #GstPad to add the probe to
1092 * @mask: the probe mask
1093 * @callback: #GstPadProbeCallback that will be called with notifications of
1095 * @user_data: (closure): user data passed to the callback
1096 * @destroy_data: #GDestroyNotify for user_data
1098 * Be notified of different states of pads. The provided callback is called for
1099 * every state that matches @mask.
1102 * Pad probe handlers are only called for source pads in push mode
1103 * and sink pads in pull mode.
1106 * Returns: an id or 0 on error. The id can be used to remove the probe with
1107 * gst_pad_remove_probe().
1112 gst_pad_add_probe (GstPad * pad, GstProbeType mask,
1113 GstPadProbeCallback callback, gpointer user_data,
1114 GDestroyNotify destroy_data)
1119 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1120 g_return_val_if_fail (mask != 0, 0);
1122 GST_OBJECT_LOCK (pad);
1123 /* make a new probe */
1124 hook = g_hook_alloc (&pad->probes);
1126 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1129 /* when no contraints are given for the types, assume all types are
1131 if ((mask & GST_PROBE_TYPE_DATA) == 0)
1132 mask |= GST_PROBE_TYPE_DATA;
1133 if ((mask & GST_PROBE_TYPE_SCHEDULING) == 0)
1134 mask |= GST_PROBE_TYPE_SCHEDULING;
1136 /* store our flags and other fields */
1137 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1138 hook->func = callback;
1139 hook->data = user_data;
1140 hook->destroy = destroy_data;
1141 PROBE_COOKIE (hook) = 0;
1143 /* incremenent cookie so that the new hook get's called */
1144 pad->priv->probe_cookie++;
1147 g_hook_prepend (&pad->probes, hook);
1150 /* get the id of the hook, we return this and it can be used to remove the
1152 res = hook->hook_id;
1154 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1156 if (mask & GST_PROBE_TYPE_BLOCKING) {
1157 /* we have a block probe */
1159 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1160 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1161 "now %d blocking probes", pad->num_blocked);
1164 /* call the callback if we need to be called for idle callbacks */
1165 if ((mask & GST_PROBE_TYPE_IDLE) && (callback != NULL)) {
1166 if (pad->priv->using > 0) {
1167 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1168 * flag above, the last thread to leave the push will do the callback. New
1169 * threads going into the push will block. */
1170 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1171 "pad is in use, delay idle callback");
1172 GST_OBJECT_UNLOCK (pad);
1174 /* the pad is idle now, we can signal the idle callback now */
1175 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1176 "pad is idle, trigger idle callback");
1177 GST_OBJECT_UNLOCK (pad);
1179 callback (pad, GST_PROBE_TYPE_IDLE, NULL, user_data);
1182 GST_OBJECT_UNLOCK (pad);
1188 cleanup_hook (GstPad * pad, GHook * hook)
1192 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1194 if (type & GST_PROBE_TYPE_BLOCKING) {
1195 /* unblock when we remove the last blocking probe */
1197 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1199 if (pad->num_blocked == 0) {
1200 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1201 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1202 GST_PAD_BLOCK_BROADCAST (pad);
1205 g_hook_destroy_link (&pad->probes, hook);
1210 * gst_pad_remove_probe:
1211 * @pad: the #GstPad with the probe
1212 * @id: the probe id to remove
1214 * Remove the probe with @id from @pad.
1219 gst_pad_remove_probe (GstPad * pad, gulong id)
1223 g_return_if_fail (GST_IS_PAD (pad));
1225 GST_OBJECT_LOCK (pad);
1227 hook = g_hook_get (&pad->probes, id);
1231 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1233 cleanup_hook (pad, hook);
1234 GST_OBJECT_UNLOCK (pad);
1240 GST_OBJECT_UNLOCK (pad);
1241 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1247 * gst_pad_is_blocked:
1248 * @pad: the #GstPad to query
1250 * Checks if the pad is blocked or not. This function returns the
1251 * last requested state of the pad. It is not certain that the pad
1252 * is actually blocking at this point (see gst_pad_is_blocking()).
1254 * Returns: TRUE if the pad is blocked.
1259 gst_pad_is_blocked (GstPad * pad)
1261 gboolean result = FALSE;
1263 g_return_val_if_fail (GST_IS_PAD (pad), result);
1265 GST_OBJECT_LOCK (pad);
1266 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1267 GST_OBJECT_UNLOCK (pad);
1273 * gst_pad_is_blocking:
1274 * @pad: the #GstPad to query
1276 * Checks if the pad is blocking or not. This is a guaranteed state
1277 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1279 * Returns: TRUE if the pad is blocking.
1286 gst_pad_is_blocking (GstPad * pad)
1288 gboolean result = FALSE;
1290 g_return_val_if_fail (GST_IS_PAD (pad), result);
1292 GST_OBJECT_LOCK (pad);
1293 /* the blocking flag is only valid if the pad is not flushing */
1294 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1295 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1296 GST_OBJECT_UNLOCK (pad);
1302 * gst_pad_set_activate_function:
1304 * @activate: the #GstPadActivateFunction to set.
1306 * Sets the given activate function for @pad. The activate function will
1307 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1308 * the actual activation. Only makes sense to set on sink pads.
1310 * Call this function if your sink pad can start a pull-based task.
1313 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1315 g_return_if_fail (GST_IS_PAD (pad));
1317 GST_PAD_ACTIVATEFUNC (pad) = activate;
1318 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1319 GST_DEBUG_FUNCPTR_NAME (activate));
1323 * gst_pad_set_activatepull_function:
1325 * @activatepull: the #GstPadActivateModeFunction to set.
1327 * Sets the given activate_pull function for the pad. An activate_pull function
1328 * prepares the element and any upstream connections for pulling. See XXX
1329 * part-activation.txt for details.
1332 gst_pad_set_activatepull_function (GstPad * pad,
1333 GstPadActivateModeFunction activatepull)
1335 g_return_if_fail (GST_IS_PAD (pad));
1337 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1338 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1339 GST_DEBUG_FUNCPTR_NAME (activatepull));
1343 * gst_pad_set_activatepush_function:
1345 * @activatepush: the #GstPadActivateModeFunction to set.
1347 * Sets the given activate_push function for the pad. An activate_push function
1348 * prepares the element for pushing. See XXX part-activation.txt for details.
1351 gst_pad_set_activatepush_function (GstPad * pad,
1352 GstPadActivateModeFunction activatepush)
1354 g_return_if_fail (GST_IS_PAD (pad));
1356 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1357 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1358 GST_DEBUG_FUNCPTR_NAME (activatepush));
1362 * gst_pad_set_chain_function:
1363 * @pad: a sink #GstPad.
1364 * @chain: the #GstPadChainFunction to set.
1366 * Sets the given chain function for the pad. The chain function is called to
1367 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1370 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1372 g_return_if_fail (GST_IS_PAD (pad));
1373 g_return_if_fail (GST_PAD_IS_SINK (pad));
1375 GST_PAD_CHAINFUNC (pad) = chain;
1376 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1377 GST_DEBUG_FUNCPTR_NAME (chain));
1381 * gst_pad_set_chain_list_function:
1382 * @pad: a sink #GstPad.
1383 * @chainlist: the #GstPadChainListFunction to set.
1385 * Sets the given chain list function for the pad. The chainlist function is
1386 * called to process a #GstBufferList input buffer list. See
1387 * #GstPadChainListFunction for more details.
1392 gst_pad_set_chain_list_function (GstPad * pad,
1393 GstPadChainListFunction chainlist)
1395 g_return_if_fail (GST_IS_PAD (pad));
1396 g_return_if_fail (GST_PAD_IS_SINK (pad));
1398 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1399 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1400 GST_DEBUG_FUNCPTR_NAME (chainlist));
1404 * gst_pad_set_getrange_function:
1405 * @pad: a source #GstPad.
1406 * @get: the #GstPadGetRangeFunction to set.
1408 * Sets the given getrange function for the pad. The getrange function is
1409 * called to produce a new #GstBuffer to start the processing pipeline. see
1410 * #GstPadGetRangeFunction for a description of the getrange function.
1413 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1415 g_return_if_fail (GST_IS_PAD (pad));
1416 g_return_if_fail (GST_PAD_IS_SRC (pad));
1418 GST_PAD_GETRANGEFUNC (pad) = get;
1420 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1421 GST_DEBUG_FUNCPTR_NAME (get));
1425 * gst_pad_set_event_function:
1426 * @pad: a #GstPad of either direction.
1427 * @event: the #GstPadEventFunction to set.
1429 * Sets the given event handler for the pad.
1432 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1434 g_return_if_fail (GST_IS_PAD (pad));
1436 GST_PAD_EVENTFUNC (pad) = event;
1438 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1439 GST_DEBUG_FUNCPTR_NAME (event));
1443 * gst_pad_set_query_function:
1444 * @pad: a #GstPad of either direction.
1445 * @query: the #GstPadQueryFunction to set.
1447 * Set the given query function for the pad.
1450 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1452 g_return_if_fail (GST_IS_PAD (pad));
1454 GST_PAD_QUERYFUNC (pad) = query;
1456 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1457 GST_DEBUG_FUNCPTR_NAME (query));
1461 * gst_pad_set_query_type_function:
1462 * @pad: a #GstPad of either direction.
1463 * @type_func: the #GstPadQueryTypeFunction to set.
1465 * Set the given query type function for the pad.
1468 gst_pad_set_query_type_function (GstPad * pad,
1469 GstPadQueryTypeFunction type_func)
1471 g_return_if_fail (GST_IS_PAD (pad));
1473 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1475 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1476 GST_DEBUG_FUNCPTR_NAME (type_func));
1480 * gst_pad_get_query_types:
1483 * Get an array of supported queries that can be performed
1486 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1489 const GstQueryType *
1490 gst_pad_get_query_types (GstPad * pad)
1492 GstPadQueryTypeFunction func;
1494 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1496 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1508 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1510 *data = gst_pad_get_query_types (pad);
1516 * gst_pad_get_query_types_default:
1519 * Invoke the default dispatcher for the query types on
1522 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1523 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1524 * query types function.
1526 const GstQueryType *
1527 gst_pad_get_query_types_default (GstPad * pad)
1529 GstQueryType *result = NULL;
1531 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1533 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1534 gst_pad_get_query_types_dispatcher, &result);
1540 * gst_pad_set_iterate_internal_links_function:
1541 * @pad: a #GstPad of either direction.
1542 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1544 * Sets the given internal link iterator function for the pad.
1549 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1550 GstPadIterIntLinkFunction iterintlink)
1552 g_return_if_fail (GST_IS_PAD (pad));
1554 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1555 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1556 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1560 * gst_pad_set_link_function:
1562 * @link: the #GstPadLinkFunction to set.
1564 * Sets the given link function for the pad. It will be called when
1565 * the pad is linked with another pad.
1567 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1570 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1571 * cannot be made for some reason.
1573 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1574 * of the peer sink pad, if present.
1577 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1579 g_return_if_fail (GST_IS_PAD (pad));
1581 GST_PAD_LINKFUNC (pad) = link;
1582 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1583 GST_DEBUG_FUNCPTR_NAME (link));
1587 * gst_pad_set_unlink_function:
1589 * @unlink: the #GstPadUnlinkFunction to set.
1591 * Sets the given unlink function for the pad. It will be called
1592 * when the pad is unlinked.
1595 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1597 g_return_if_fail (GST_IS_PAD (pad));
1599 GST_PAD_UNLINKFUNC (pad) = unlink;
1600 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1601 GST_DEBUG_FUNCPTR_NAME (unlink));
1605 * gst_pad_set_getcaps_function:
1607 * @getcaps: the #GstPadGetCapsFunction to set.
1609 * Sets the given getcaps function for the pad. @getcaps should return the
1610 * allowable caps for a pad in the context of the element's state, its link to
1611 * other elements, and the devices or files it has opened. These caps must be a
1612 * subset of the pad template caps. In the NULL state with no links, @getcaps
1613 * should ideally return the same caps as the pad template. In rare
1614 * circumstances, an object property can affect the caps returned by @getcaps,
1615 * but this is discouraged.
1617 * You do not need to call this function if @pad's allowed caps are always the
1618 * same as the pad template caps. This can only be true if the padtemplate
1619 * has fixed simple caps.
1621 * For most filters, the caps returned by @getcaps is directly affected by the
1622 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1623 * the srcpad's getcaps function is directly related to the stream data. Again,
1624 * @getcaps should return the most specific caps it reasonably can, since this
1625 * helps with autoplugging.
1627 * Note that the return value from @getcaps is owned by the caller, so the
1628 * caller should unref the caps after usage.
1631 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1633 g_return_if_fail (GST_IS_PAD (pad));
1635 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1636 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1637 GST_DEBUG_FUNCPTR_NAME (getcaps));
1641 * gst_pad_set_acceptcaps_function:
1643 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1645 * Sets the given acceptcaps function for the pad. The acceptcaps function
1646 * will be called to check if the pad can accept the given caps. Setting the
1647 * acceptcaps function to NULL restores the default behaviour of allowing
1648 * any caps that matches the caps from gst_pad_get_caps().
1651 gst_pad_set_acceptcaps_function (GstPad * pad,
1652 GstPadAcceptCapsFunction acceptcaps)
1654 g_return_if_fail (GST_IS_PAD (pad));
1656 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1657 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1658 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1662 * gst_pad_set_fixatecaps_function:
1664 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1666 * Sets the given fixatecaps function for the pad. The fixatecaps function
1667 * will be called whenever the default values for a GstCaps needs to be
1671 gst_pad_set_fixatecaps_function (GstPad * pad,
1672 GstPadFixateCapsFunction fixatecaps)
1674 g_return_if_fail (GST_IS_PAD (pad));
1676 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1677 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1678 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1682 * gst_pad_set_setcaps_function:
1684 * @setcaps: the #GstPadSetCapsFunction to set.
1686 * Sets the given setcaps function for the pad. The setcaps function
1687 * will be called whenever a buffer with a new media type is pushed or
1688 * pulled from the pad. The pad/element needs to update its internal
1689 * structures to process the new media type. If this new type is not
1690 * acceptable, the setcaps function should return FALSE.
1693 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1695 g_return_if_fail (GST_IS_PAD (pad));
1697 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1698 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1699 GST_DEBUG_FUNCPTR_NAME (setcaps));
1704 * @srcpad: the source #GstPad to unlink.
1705 * @sinkpad: the sink #GstPad to unlink.
1707 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1708 * signal on both pads.
1710 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1711 * the pads were not linked together.
1716 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1718 gboolean result = FALSE;
1719 GstElement *parent = NULL;
1722 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1723 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1724 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1725 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1727 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1728 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1729 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1731 /* We need to notify the parent before taking any pad locks as the bin in
1732 * question might be waiting for a lock on the pad while holding its lock
1733 * that our message will try to take. */
1734 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1735 if (GST_IS_ELEMENT (parent)) {
1736 gst_element_post_message (parent,
1737 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1738 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1740 gst_object_unref (parent);
1745 GST_OBJECT_LOCK (srcpad);
1747 GST_OBJECT_LOCK (sinkpad);
1749 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1750 goto not_linked_together;
1752 if (GST_PAD_UNLINKFUNC (srcpad)) {
1753 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1755 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1756 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1759 /* first clear peers */
1760 GST_PAD_PEER (srcpad) = NULL;
1761 GST_PAD_PEER (sinkpad) = NULL;
1763 /* clear pending caps if any */
1764 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1765 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1767 GST_OBJECT_UNLOCK (sinkpad);
1768 GST_OBJECT_UNLOCK (srcpad);
1770 /* fire off a signal to each of the pads telling them
1771 * that they've been unlinked */
1772 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1773 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1775 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1776 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1781 if (parent != NULL) {
1782 gst_element_post_message (parent,
1783 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1784 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1785 gst_object_unref (parent);
1790 not_linked_together:
1792 /* we do not emit a warning in this case because unlinking cannot
1793 * be made MT safe.*/
1794 GST_OBJECT_UNLOCK (sinkpad);
1795 GST_OBJECT_UNLOCK (srcpad);
1801 * gst_pad_is_linked:
1802 * @pad: pad to check
1804 * Checks if a @pad is linked to another pad or not.
1806 * Returns: TRUE if the pad is linked, FALSE otherwise.
1811 gst_pad_is_linked (GstPad * pad)
1815 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1817 GST_OBJECT_LOCK (pad);
1818 result = (GST_PAD_PEER (pad) != NULL);
1819 GST_OBJECT_UNLOCK (pad);
1824 /* get the caps from both pads and see if the intersection
1827 * This function should be called with the pad LOCK on both
1831 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1832 GstPadLinkCheck flags)
1834 GstCaps *srccaps = NULL;
1835 GstCaps *sinkcaps = NULL;
1836 gboolean compatible = FALSE;
1838 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1841 /* Doing the expensive caps checking takes priority over only checking the template caps */
1842 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1843 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1844 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1846 /* If one of the two pads doesn't have a template, consider the intersection
1848 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1849 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1853 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1855 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1858 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1860 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1863 /* if we have caps on both pads we can check the intersection. If one
1864 * of the caps is NULL, we return TRUE. */
1865 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1867 gst_caps_unref (srccaps);
1869 gst_caps_unref (sinkcaps);
1873 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1874 gst_caps_unref (srccaps);
1875 gst_caps_unref (sinkcaps);
1878 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1879 (compatible ? "" : "not"));
1884 /* check if the grandparents of both pads are the same.
1885 * This check is required so that we don't try to link
1886 * pads from elements in different bins without ghostpads.
1888 * The LOCK should be held on both pads
1891 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1893 GstObject *psrc, *psink;
1895 psrc = GST_OBJECT_PARENT (src);
1896 psink = GST_OBJECT_PARENT (sink);
1898 /* if one of the pads has no parent, we allow the link */
1899 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1902 /* only care about parents that are elements */
1903 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1904 goto no_element_parent;
1906 /* if the parents are the same, we have a loop */
1907 if (G_UNLIKELY (psrc == psink))
1910 /* if they both have a parent, we check the grandparents. We can not lock
1911 * the parent because we hold on the child (pad) and the locking order is
1912 * parent >> child. */
1913 psrc = GST_OBJECT_PARENT (psrc);
1914 psink = GST_OBJECT_PARENT (psink);
1916 /* if they have grandparents but they are not the same */
1917 if (G_UNLIKELY (psrc != psink))
1918 goto wrong_grandparents;
1925 GST_CAT_DEBUG (GST_CAT_CAPS,
1926 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1927 GST_PTR_FORMAT, psrc, psink);
1932 GST_CAT_DEBUG (GST_CAT_CAPS,
1933 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1934 GST_PTR_FORMAT, psrc, psink);
1939 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1945 GST_CAT_DEBUG (GST_CAT_CAPS,
1946 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1947 GST_PTR_FORMAT, psrc, psink);
1952 /* FIXME leftover from an attempt at refactoring... */
1953 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1954 * the two pads will be locked in the srcpad, sinkpad order. */
1955 static GstPadLinkReturn
1956 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1958 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1959 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1961 GST_OBJECT_LOCK (srcpad);
1963 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1964 goto src_was_linked;
1966 GST_OBJECT_LOCK (sinkpad);
1968 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1969 goto sink_was_linked;
1971 /* check hierarchy, pads can only be linked if the grandparents
1973 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1974 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1975 goto wrong_hierarchy;
1977 /* check pad caps for non-empty intersection */
1978 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1981 /* FIXME check pad scheduling for non-empty intersection */
1983 return GST_PAD_LINK_OK;
1987 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1988 GST_DEBUG_PAD_NAME (srcpad),
1989 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1990 /* we do not emit a warning in this case because unlinking cannot
1991 * be made MT safe.*/
1992 GST_OBJECT_UNLOCK (srcpad);
1993 return GST_PAD_LINK_WAS_LINKED;
1997 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1998 GST_DEBUG_PAD_NAME (sinkpad),
1999 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2000 /* we do not emit a warning in this case because unlinking cannot
2001 * be made MT safe.*/
2002 GST_OBJECT_UNLOCK (sinkpad);
2003 GST_OBJECT_UNLOCK (srcpad);
2004 return GST_PAD_LINK_WAS_LINKED;
2008 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2009 GST_OBJECT_UNLOCK (sinkpad);
2010 GST_OBJECT_UNLOCK (srcpad);
2011 return GST_PAD_LINK_WRONG_HIERARCHY;
2015 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2016 GST_OBJECT_UNLOCK (sinkpad);
2017 GST_OBJECT_UNLOCK (srcpad);
2018 return GST_PAD_LINK_NOFORMAT;
2024 * @srcpad: the source #GstPad.
2025 * @sinkpad: the sink #GstPad.
2027 * Checks if the source pad and the sink pad are compatible so they can be
2030 * Returns: TRUE if the pads can be linked.
2033 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2035 GstPadLinkReturn result;
2037 /* generic checks */
2038 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2039 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2041 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2042 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2044 /* gst_pad_link_prepare does everything for us, we only release the locks
2045 * on the pads that it gets us. If this function returns !OK the locks are not
2047 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2048 if (result != GST_PAD_LINK_OK)
2051 GST_OBJECT_UNLOCK (srcpad);
2052 GST_OBJECT_UNLOCK (sinkpad);
2055 return result == GST_PAD_LINK_OK;
2059 * gst_pad_link_full:
2060 * @srcpad: the source #GstPad to link.
2061 * @sinkpad: the sink #GstPad to link.
2062 * @flags: the checks to validate when linking
2064 * Links the source pad and the sink pad.
2066 * This variant of #gst_pad_link provides a more granular control on the
2067 * checks being done when linking. While providing some considerable speedups
2068 * the caller of this method must be aware that wrong usage of those flags
2069 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2070 * for more information.
2074 * Returns: A result code indicating if the connection worked or
2080 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2082 GstPadLinkReturn result;
2085 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2086 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2087 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2088 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2089 GST_PAD_LINK_WRONG_DIRECTION);
2091 /* Notify the parent early. See gst_pad_unlink for details. */
2092 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2093 if (GST_IS_ELEMENT (parent)) {
2094 gst_element_post_message (parent,
2095 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2096 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2098 gst_object_unref (parent);
2103 /* prepare will also lock the two pads */
2104 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2106 if (result != GST_PAD_LINK_OK)
2109 /* must set peers before calling the link function */
2110 GST_PAD_PEER (srcpad) = sinkpad;
2111 GST_PAD_PEER (sinkpad) = srcpad;
2113 /* make sure we update events */
2114 prepare_event_update (srcpad, sinkpad);
2116 GST_OBJECT_UNLOCK (sinkpad);
2117 GST_OBJECT_UNLOCK (srcpad);
2119 /* FIXME released the locks here, concurrent thread might link
2120 * something else. */
2121 if (GST_PAD_LINKFUNC (srcpad)) {
2122 /* this one will call the peer link function */
2123 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2124 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2125 /* if no source link function, we need to call the sink link
2126 * function ourselves. */
2127 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2129 result = GST_PAD_LINK_OK;
2132 GST_OBJECT_LOCK (srcpad);
2133 GST_OBJECT_LOCK (sinkpad);
2135 if (result == GST_PAD_LINK_OK) {
2136 GST_OBJECT_UNLOCK (sinkpad);
2137 GST_OBJECT_UNLOCK (srcpad);
2139 /* fire off a signal to each of the pads telling them
2140 * that they've been linked */
2141 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2142 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2144 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2145 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2147 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2149 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2150 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2152 GST_PAD_PEER (srcpad) = NULL;
2153 GST_PAD_PEER (sinkpad) = NULL;
2155 GST_OBJECT_UNLOCK (sinkpad);
2156 GST_OBJECT_UNLOCK (srcpad);
2161 gst_element_post_message (parent,
2162 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2163 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2164 gst_object_unref (parent);
2172 * @srcpad: the source #GstPad to link.
2173 * @sinkpad: the sink #GstPad to link.
2175 * Links the source pad and the sink pad.
2177 * Returns: A result code indicating if the connection worked or
2183 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2185 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2189 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2191 GstPadTemplate **template_p;
2193 /* this function would need checks if it weren't static */
2195 GST_OBJECT_LOCK (pad);
2196 template_p = &pad->padtemplate;
2197 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2198 GST_OBJECT_UNLOCK (pad);
2201 gst_pad_template_pad_created (templ, pad);
2205 * gst_pad_get_pad_template:
2208 * Gets the template for @pad.
2210 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2211 * instantiated, or %NULL if this pad has no template. Unref after
2215 gst_pad_get_pad_template (GstPad * pad)
2217 GstPadTemplate *templ;
2219 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2221 templ = GST_PAD_PAD_TEMPLATE (pad);
2223 return (templ ? gst_object_ref (templ) : NULL);
2226 /* should be called with the pad LOCK held */
2227 /* refs the caps, so caller is responsible for getting it unreffed */
2229 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2231 GstCaps *result = NULL;
2232 GstPadTemplate *templ;
2233 gboolean fixed_caps;
2235 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2237 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2239 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2241 "dispatching to pad getcaps function with "
2242 "filter %" GST_PTR_FORMAT, filter);
2244 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2245 GST_OBJECT_UNLOCK (pad);
2246 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2247 GST_OBJECT_LOCK (pad);
2248 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2250 if (result == NULL) {
2251 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2252 GST_DEBUG_PAD_NAME (pad));
2254 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2255 "pad getcaps returned %" GST_PTR_FORMAT, result);
2256 #ifndef G_DISABLE_ASSERT
2257 /* check that the returned caps are a real subset of the template caps */
2258 if (GST_PAD_PAD_TEMPLATE (pad)) {
2259 const GstCaps *templ_caps =
2260 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2261 if (!gst_caps_is_subset (result, templ_caps)) {
2264 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2265 "pad returned caps %" GST_PTR_FORMAT
2266 " which are not a real subset of its template caps %"
2267 GST_PTR_FORMAT, result, templ_caps);
2269 ("pad %s:%s returned caps which are not a real "
2270 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2271 temp = gst_caps_intersect (templ_caps, result);
2272 gst_caps_unref (result);
2277 if (!gst_caps_is_subset (result, filter)) {
2280 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2281 "pad returned caps %" GST_PTR_FORMAT
2282 " which are not a real subset of the filter caps %"
2283 GST_PTR_FORMAT, result, filter);
2284 g_warning ("pad %s:%s returned caps which are not a real "
2285 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2286 /* FIXME: Order? But shouldn't happen anyway... */
2288 gst_caps_intersect_full (filter, result,
2289 GST_CAPS_INTERSECT_FIRST);
2290 gst_caps_unref (result);
2298 if (fixed_caps && (result = get_pad_caps (pad))) {
2300 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2301 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2302 GST_PTR_FORMAT, result, result, filter, filter);
2304 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2305 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2308 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2309 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2310 result = gst_caps_ref (result);
2314 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2315 result = GST_PAD_TEMPLATE_CAPS (templ);
2318 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2319 "using pad template %p with caps %p %" GST_PTR_FORMAT
2320 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2323 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2324 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2327 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2328 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2330 result = gst_caps_ref (result);
2335 if (!fixed_caps && (result = get_pad_caps (pad))) {
2337 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2338 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2339 GST_PTR_FORMAT, result, result, filter, filter);
2341 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2342 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2345 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2346 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2347 result = gst_caps_ref (result);
2353 /* this almost never happens */
2354 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2355 result = gst_caps_new_empty ();
2362 * gst_pad_has_current_caps:
2363 * @pad: a #GstPad to check
2365 * Check if @pad has caps set on it with gst_pad_set_caps().
2367 * Returns: TRUE when @pad has caps associated with it.
2370 gst_pad_has_current_caps (GstPad * pad)
2374 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2376 GST_OBJECT_LOCK (pad);
2377 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2378 result = (get_pad_caps (pad) != NULL);
2379 GST_OBJECT_UNLOCK (pad);
2385 * gst_pad_get_current_caps:
2386 * @pad: a #GstPad to get the current capabilities of.
2388 * Gets the capabilities currently configured on @pad with the last call to
2389 * gst_pad_set_caps().
2391 * Returns: the current caps of the pad with incremented ref-count.
2394 gst_pad_get_current_caps (GstPad * pad)
2398 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2400 GST_OBJECT_LOCK (pad);
2401 if ((result = get_pad_caps (pad)))
2402 gst_caps_ref (result);
2403 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2404 "get current pad caps %" GST_PTR_FORMAT, result);
2405 GST_OBJECT_UNLOCK (pad);
2412 * @pad: a #GstPad to get the capabilities of.
2413 * @filter: suggested #GstCaps.
2415 * Gets the capabilities this pad can produce or consume.
2416 * Note that this method doesn't necessarily return the caps set by
2417 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2418 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2419 * the pad's get_caps function;
2420 * this returns the pad template caps if not explicitly set.
2422 * When called on sinkpads @filter contains the caps that
2423 * upstream could produce in the order preferred by upstream. When
2424 * called on srcpads @filter contains the caps accepted by
2425 * downstream in the preffered order. @filter might be %NULL but
2426 * if it is not %NULL the returned caps will be a subset of @filter.
2428 * Note that this function does not return writable #GstCaps, use
2429 * gst_caps_make_writable() before modifying the caps.
2431 * Returns: the caps of the pad with incremented ref-count.
2434 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2436 GstCaps *result = NULL;
2438 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2439 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2441 GST_OBJECT_LOCK (pad);
2443 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2445 result = gst_pad_get_caps_unlocked (pad, filter);
2447 GST_OBJECT_UNLOCK (pad);
2454 * gst_pad_peer_get_caps:
2455 * @pad: a #GstPad to get the capabilities of.
2456 * @filter: a #GstCaps filter.
2458 * Gets the capabilities of the peer connected to this pad. Similar to
2459 * gst_pad_get_caps().
2461 * When called on srcpads @filter contains the caps that
2462 * upstream could produce in the order preferred by upstream. When
2463 * called on sinkpads @filter contains the caps accepted by
2464 * downstream in the preffered order. @filter might be %NULL but
2465 * if it is not %NULL the returned caps will be a subset of @filter.
2467 * Returns: the caps of the peer pad with incremented ref-count. This function
2468 * returns %NULL when there is no peer pad.
2471 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2474 GstCaps *result = NULL;
2476 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2477 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2479 GST_OBJECT_LOCK (pad);
2481 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2483 peerpad = GST_PAD_PEER (pad);
2484 if (G_UNLIKELY (peerpad == NULL))
2487 gst_object_ref (peerpad);
2488 GST_OBJECT_UNLOCK (pad);
2490 result = gst_pad_get_caps (peerpad, filter);
2492 gst_object_unref (peerpad);
2498 GST_OBJECT_UNLOCK (pad);
2504 fixate_value (GValue * dest, const GValue * src)
2506 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2507 g_value_init (dest, G_TYPE_INT);
2508 g_value_set_int (dest, gst_value_get_int_range_min (src));
2509 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2510 g_value_init (dest, G_TYPE_DOUBLE);
2511 g_value_set_double (dest, gst_value_get_double_range_min (src));
2512 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2513 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2514 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2515 GValue temp = { 0 };
2517 /* list could be empty */
2518 if (gst_value_list_get_size (src) <= 0)
2521 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2523 if (!fixate_value (dest, &temp))
2524 gst_value_init_and_copy (dest, &temp);
2525 g_value_unset (&temp);
2526 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2527 gboolean res = FALSE;
2530 len = gst_value_array_get_size (src);
2531 g_value_init (dest, GST_TYPE_ARRAY);
2532 for (n = 0; n < len; n++) {
2534 const GValue *orig_kid = gst_value_array_get_value (src, n);
2536 if (!fixate_value (&kid, orig_kid))
2537 gst_value_init_and_copy (&kid, orig_kid);
2540 gst_value_array_append_value (dest, &kid);
2541 g_value_unset (&kid);
2545 g_value_unset (dest);
2556 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2558 GstStructure *s = data;
2561 if (fixate_value (&v, value)) {
2562 gst_structure_id_set_value (s, field_id, &v);
2570 * gst_pad_fixate_caps:
2571 * @pad: a #GstPad to fixate
2572 * @caps: the #GstCaps to fixate
2574 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2575 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2578 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2580 GstPadFixateCapsFunction fixatefunc;
2583 g_return_if_fail (GST_IS_PAD (pad));
2584 g_return_if_fail (caps != NULL);
2585 g_return_if_fail (!gst_caps_is_empty (caps));
2586 g_return_if_fail (!gst_caps_is_any (caps));
2588 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2591 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2593 fixatefunc (pad, caps);
2596 /* default fixation */
2597 gst_caps_truncate (caps);
2598 s = gst_caps_get_structure (caps, 0);
2599 gst_structure_foreach (s, gst_pad_default_fixate, s);
2602 /* Default accept caps implementation just checks against
2603 * against the allowed caps for the pad */
2605 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2607 /* get the caps and see if it intersects to something not empty */
2609 gboolean result = FALSE;
2611 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2613 allowed = gst_pad_get_caps (pad, NULL);
2615 goto nothing_allowed;
2617 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2619 result = gst_caps_can_intersect (allowed, caps);
2621 gst_caps_unref (allowed);
2628 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2634 * gst_pad_accept_caps:
2635 * @pad: a #GstPad to check
2636 * @caps: a #GstCaps to check on the pad
2638 * Check if the given pad accepts the caps.
2640 * Returns: TRUE if the pad can accept the caps.
2643 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2646 GstPadAcceptCapsFunction acceptfunc;
2648 GstCaps *existing = NULL;
2651 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2653 /* any pad can be unnegotiated */
2657 /* lock for checking the existing caps */
2658 GST_OBJECT_LOCK (pad);
2659 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2661 /* The current caps on a pad are trivially acceptable */
2662 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2663 if (caps == existing || gst_caps_is_equal (caps, existing))
2667 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2668 GST_OBJECT_UNLOCK (pad);
2670 if (G_LIKELY (acceptfunc)) {
2671 /* we can call the function */
2672 result = acceptfunc (pad, caps);
2673 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2675 /* Only null if the element explicitly unset it */
2676 result = gst_pad_acceptcaps_default (pad, caps);
2677 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2684 GST_DEBUG_OBJECT (pad, "pad had same caps");
2685 GST_OBJECT_UNLOCK (pad);
2692 * gst_pad_peer_accept_caps:
2693 * @pad: a #GstPad to check the peer of
2694 * @caps: a #GstCaps to check on the pad
2696 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2699 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2702 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2707 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2709 GST_OBJECT_LOCK (pad);
2711 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2713 peerpad = GST_PAD_PEER (pad);
2714 if (G_UNLIKELY (peerpad == NULL))
2717 gst_object_ref (peerpad);
2718 /* release lock before calling external methods but keep ref to pad */
2719 GST_OBJECT_UNLOCK (pad);
2721 result = gst_pad_accept_caps (peerpad, caps);
2723 gst_object_unref (peerpad);
2729 GST_OBJECT_UNLOCK (pad);
2736 * @pad: a #GstPad to set the capabilities of.
2737 * @caps: (transfer none): a #GstCaps to set.
2739 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2740 * caps on the pad will be unreffed. This function refs the caps so you should
2741 * unref if as soon as you don't need it anymore.
2742 * It is possible to set NULL caps, which will make the pad unnegotiated
2745 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2746 * or bad parameters were provided to this function.
2751 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2754 gboolean res = TRUE;
2756 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2757 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2759 event = gst_event_new_caps (caps);
2761 if (GST_PAD_IS_SRC (pad))
2762 res = gst_pad_push_event (pad, event);
2764 res = gst_pad_send_event (pad, event);
2770 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2772 GstPadSetCapsFunction setcaps;
2774 GST_OBJECT_LOCK (pad);
2775 setcaps = GST_PAD_SETCAPSFUNC (pad);
2777 /* call setcaps function to configure the pad only if the
2778 * caps is not NULL */
2779 if (setcaps != NULL) {
2780 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2781 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2782 GST_OBJECT_UNLOCK (pad);
2783 if (!setcaps (pad, caps))
2784 goto setcaps_failed;
2785 GST_OBJECT_LOCK (pad);
2786 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2788 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2791 GST_OBJECT_UNLOCK (pad);
2793 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2805 do_event_function (GstPad * pad, GstEvent * event,
2806 GstPadEventFunction eventfunc)
2808 gboolean result = TRUE;
2809 GstCaps *caps, *templ;
2811 switch (GST_EVENT_TYPE (event)) {
2812 case GST_EVENT_CAPS:
2814 /* backwards compatibility mode for caps */
2815 gst_event_parse_caps (event, &caps);
2817 /* See if pad accepts the caps */
2818 templ = gst_pad_get_pad_template_caps (pad);
2819 if (!gst_caps_can_intersect (caps, templ))
2822 if (!gst_pad_call_setcaps (pad, caps))
2825 gst_caps_unref (templ);
2832 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2833 result = eventfunc (pad, event);
2840 gst_caps_unref (templ);
2841 gst_event_unref (event);
2842 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2843 "caps %" GST_PTR_FORMAT " not accepted", caps);
2848 /* function to send all pending events on the sinkpad to the event
2849 * function and collect the results. This function should be called with
2850 * the object lock. The object lock might be released by this function.
2852 static GstFlowReturn
2853 gst_pad_update_events (GstPad * pad)
2855 GstFlowReturn ret = GST_FLOW_OK;
2857 GstPadEventFunction eventfunc;
2860 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2864 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2868 ev = &pad->priv->events[i];
2870 /* skip without pending event */
2871 if ((event = ev->pending) == NULL)
2874 gst_event_ref (event);
2875 GST_OBJECT_UNLOCK (pad);
2877 res = do_event_function (pad, event, eventfunc);
2879 GST_OBJECT_LOCK (pad);
2880 /* things could have changed while we release the lock, check if we still
2881 * are handling the same event, if we don't something changed and we have
2882 * to try again. FIXME. we need a cookie here. */
2883 if (event != ev->pending) {
2884 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2888 /* remove the event from the pending entry in all cases */
2892 /* make the event active */
2894 gst_event_unref (ev->event);
2897 gst_event_unref (event);
2898 ret = GST_FLOW_ERROR;
2901 /* when we get here all events were successfully updated. */
2908 g_warning ("pad %s:%s has no event handler, file a bug.",
2909 GST_DEBUG_PAD_NAME (pad));
2910 return GST_FLOW_NOT_SUPPORTED;
2915 * gst_pad_get_pad_template_caps:
2916 * @pad: a #GstPad to get the template capabilities from.
2918 * Gets the capabilities for @pad's template.
2920 * Returns: (transfer full): the #GstCaps of this pad template.
2921 * Unref after usage.
2924 gst_pad_get_pad_template_caps (GstPad * pad)
2926 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2928 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2930 if (GST_PAD_PAD_TEMPLATE (pad))
2931 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2933 return gst_static_caps_get (&anycaps);
2938 * @pad: a #GstPad to get the peer of.
2940 * Gets the peer of @pad. This function refs the peer pad so
2941 * you need to unref it after use.
2943 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2948 gst_pad_get_peer (GstPad * pad)
2952 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2954 GST_OBJECT_LOCK (pad);
2955 result = GST_PAD_PEER (pad);
2957 gst_object_ref (result);
2958 GST_OBJECT_UNLOCK (pad);
2964 * gst_pad_get_allowed_caps:
2967 * Gets the capabilities of the allowed media types that can flow through
2968 * @pad and its peer.
2970 * The allowed capabilities is calculated as the intersection of the results of
2971 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2972 * on the resulting caps.
2974 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2975 * caps when you no longer need it. This function returns NULL when @pad
2981 gst_pad_get_allowed_caps (GstPad * pad)
2988 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2990 GST_OBJECT_LOCK (pad);
2991 peer = GST_PAD_PEER (pad);
2992 if (G_UNLIKELY (peer == NULL))
2995 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2997 gst_object_ref (peer);
2998 GST_OBJECT_UNLOCK (pad);
2999 mycaps = gst_pad_get_caps (pad, NULL);
3001 peercaps = gst_pad_get_caps (peer, NULL);
3002 gst_object_unref (peer);
3004 caps = gst_caps_intersect (mycaps, peercaps);
3005 gst_caps_unref (peercaps);
3006 gst_caps_unref (mycaps);
3008 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
3015 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3016 GST_OBJECT_UNLOCK (pad);
3023 * gst_pad_get_negotiated_caps:
3026 * Gets the capabilities of the media type that currently flows through @pad
3029 * This function can be used on both src and sinkpads. Note that srcpads are
3030 * always negotiated before sinkpads so it is possible that the negotiated caps
3031 * on the srcpad do not match the negotiated caps of the peer.
3033 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
3034 * the caps when you no longer need it. This function returns NULL when
3035 * the @pad has no peer or is not negotiated yet.
3040 gst_pad_get_negotiated_caps (GstPad * pad)
3042 GstCaps *caps = NULL;
3045 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3047 GST_OBJECT_LOCK (pad);
3049 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3052 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
3054 if ((caps = get_pad_caps (pad)))
3055 gst_caps_ref (caps);
3057 GST_OBJECT_UNLOCK (pad);
3059 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
3066 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3067 GST_OBJECT_UNLOCK (pad);
3073 * gst_pad_iterate_internal_links_default:
3074 * @pad: the #GstPad to get the internal links of.
3076 * Iterate the list of pads to which the given pad is linked to inside of
3077 * the parent element.
3078 * This is the default handler, and thus returns an iterator of all of the
3079 * pads inside the parent element with opposite direction.
3081 * The caller must free this iterator after use with gst_iterator_free().
3083 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3084 * returned pad with gst_object_unref().
3089 gst_pad_iterate_internal_links_default (GstPad * pad)
3097 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3102 GST_OBJECT_LOCK (pad);
3103 parent = GST_PAD_PARENT (pad);
3104 if (!parent || !GST_IS_ELEMENT (parent))
3107 gst_object_ref (parent);
3108 GST_OBJECT_UNLOCK (pad);
3110 if (pad->direction == GST_PAD_SRC)
3111 padlist = &parent->sinkpads;
3113 padlist = &parent->srcpads;
3115 GST_DEBUG_OBJECT (pad, "Making iterator");
3117 cookie = &parent->pads_cookie;
3119 lock = GST_OBJECT_GET_LOCK (parent);
3122 res = gst_iterator_new_list (GST_TYPE_PAD,
3123 lock, cookie, padlist, (GObject *) owner, NULL);
3125 gst_object_unref (owner);
3132 GST_OBJECT_UNLOCK (pad);
3133 GST_DEBUG_OBJECT (pad, "no parent element");
3139 * gst_pad_iterate_internal_links:
3140 * @pad: the GstPad to get the internal links of.
3142 * Gets an iterator for the pads to which the given pad is linked to inside
3143 * of the parent element.
3145 * Each #GstPad element yielded by the iterator will have its refcount increased,
3146 * so unref after use.
3148 * Free-function: gst_iterator_free
3150 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3151 * pad does not have an iterator function configured. Use
3152 * gst_iterator_free() after usage.
3157 gst_pad_iterate_internal_links (GstPad * pad)
3159 GstIterator *res = NULL;
3161 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3163 if (GST_PAD_ITERINTLINKFUNC (pad))
3164 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3170 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3172 gboolean result = FALSE;
3174 gboolean done = FALSE;
3175 GValue item = { 0, };
3177 GList *pushed_pads = NULL;
3179 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3180 event, GST_EVENT_TYPE_NAME (event));
3182 iter = gst_pad_iterate_internal_links (pad);
3188 switch (gst_iterator_next (iter, &item)) {
3189 case GST_ITERATOR_OK:
3190 eventpad = g_value_get_object (&item);
3192 /* if already pushed, skip */
3193 if (g_list_find (pushed_pads, eventpad)) {
3194 g_value_reset (&item);
3198 if (GST_PAD_IS_SRC (eventpad)) {
3199 /* for each pad we send to, we should ref the event; it's up
3200 * to downstream to unref again when handled. */
3201 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3202 event, GST_EVENT_TYPE_NAME (event),
3203 GST_DEBUG_PAD_NAME (eventpad));
3204 gst_event_ref (event);
3205 result |= gst_pad_push_event (eventpad, event);
3207 /* we only send the event on one pad, multi-sinkpad elements
3208 * should implement a handler */
3209 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3210 event, GST_EVENT_TYPE_NAME (event),
3211 GST_DEBUG_PAD_NAME (eventpad));
3212 result = gst_pad_push_event (eventpad, event);
3217 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3219 g_value_reset (&item);
3221 case GST_ITERATOR_RESYNC:
3222 /* We don't reset the result here because we don't push the event
3223 * again on pads that got the event already and because we need
3224 * to consider the result of the previous pushes */
3225 gst_iterator_resync (iter);
3227 case GST_ITERATOR_ERROR:
3228 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3231 case GST_ITERATOR_DONE:
3236 g_value_unset (&item);
3237 gst_iterator_free (iter);
3241 /* If this is a sinkpad and we don't have pads to send the event to, we
3242 * return TRUE. This is so that when using the default handler on a sink
3243 * element, we don't fail to push it. */
3245 result = GST_PAD_IS_SINK (pad);
3247 g_list_free (pushed_pads);
3249 /* we handled the incoming event so we unref once */
3251 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3252 gst_event_unref (event);
3259 * gst_pad_event_default:
3260 * @pad: a #GstPad to call the default event handler on.
3261 * @event: (transfer full): the #GstEvent to handle.
3263 * Invokes the default event handler for the given pad. End-of-stream and
3264 * discontinuity events are handled specially, and then the event is sent to all
3265 * pads internally linked to @pad. Note that if there are many possible sink
3266 * pads that are internally linked to @pad, only one will be sent an event.
3267 * Multi-sinkpad elements should implement custom event handlers.
3269 * Returns: TRUE if the event was sent succesfully.
3272 gst_pad_event_default (GstPad * pad, GstEvent * event)
3274 gboolean result = TRUE, forward = TRUE;
3276 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3277 g_return_val_if_fail (event != NULL, FALSE);
3279 GST_LOG_OBJECT (pad, "default event handler");
3281 switch (GST_EVENT_TYPE (event)) {
3284 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3285 gst_pad_pause_task (pad);
3288 case GST_EVENT_CAPS:
3290 /* don't forward by default */
3299 result = gst_pad_event_default_dispatch (pad, event);
3301 gst_event_unref (event);
3307 * gst_pad_dispatcher:
3308 * @pad: a #GstPad to dispatch.
3309 * @dispatch: the #GstPadDispatcherFunction to call.
3310 * @data: (closure): gpointer user data passed to the dispatcher function.
3312 * Invokes the given dispatcher function on each respective peer of
3313 * all pads that are internally linked to the given pad.
3314 * The GstPadDispatcherFunction should return TRUE when no further pads
3315 * need to be processed.
3317 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3320 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3323 gboolean res = FALSE;
3324 GstIterator *iter = NULL;
3325 gboolean done = FALSE;
3326 GValue item = { 0, };
3328 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3329 g_return_val_if_fail (dispatch != NULL, FALSE);
3331 iter = gst_pad_iterate_internal_links (pad);
3337 switch (gst_iterator_next (iter, &item)) {
3338 case GST_ITERATOR_OK:
3340 GstPad *int_pad = g_value_get_object (&item);
3341 GstPad *int_peer = gst_pad_get_peer (int_pad);
3344 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3345 GST_DEBUG_PAD_NAME (int_peer));
3346 done = res = dispatch (int_peer, data);
3347 gst_object_unref (int_peer);
3349 GST_DEBUG_OBJECT (int_pad, "no peer");
3351 g_value_reset (&item);
3354 case GST_ITERATOR_RESYNC:
3355 gst_iterator_resync (iter);
3357 case GST_ITERATOR_ERROR:
3359 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3361 case GST_ITERATOR_DONE:
3366 g_value_unset (&item);
3367 gst_iterator_free (iter);
3369 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3378 * @pad: a #GstPad to invoke the default query on.
3379 * @query: (transfer none): the #GstQuery to perform.
3381 * Dispatches a query to a pad. The query should have been allocated by the
3382 * caller via one of the type-specific allocation functions. The element that
3383 * the pad belongs to is responsible for filling the query with an appropriate
3384 * response, which should then be parsed with a type-specific query parsing
3387 * Again, the caller is responsible for both the allocation and deallocation of
3388 * the query structure.
3390 * Please also note that some queries might need a running pipeline to work.
3392 * Returns: TRUE if the query could be performed.
3395 gst_pad_query (GstPad * pad, GstQuery * query)
3398 GstPadQueryFunction func;
3400 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3401 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3403 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3404 GST_QUERY_TYPE_NAME (query));
3406 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3409 res = func (pad, query);
3411 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3412 GST_QUERY_TYPE_NAME (query), res);
3418 GST_DEBUG_OBJECT (pad, "had no query function");
3424 * gst_pad_peer_query:
3425 * @pad: a #GstPad to invoke the peer query on.
3426 * @query: (transfer none): the #GstQuery to perform.
3428 * Performs gst_pad_query() on the peer of @pad.
3430 * The caller is responsible for both the allocation and deallocation of
3431 * the query structure.
3433 * Returns: TRUE if the query could be performed. This function returns %FALSE
3434 * if @pad has no peer.
3439 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3444 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3445 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3447 GST_OBJECT_LOCK (pad);
3449 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3450 GST_QUERY_TYPE_NAME (query));
3452 peerpad = GST_PAD_PEER (pad);
3453 if (G_UNLIKELY (peerpad == NULL))
3456 gst_object_ref (peerpad);
3457 GST_OBJECT_UNLOCK (pad);
3459 result = gst_pad_query (peerpad, query);
3461 gst_object_unref (peerpad);
3468 GST_WARNING_OBJECT (pad, "pad has no peer");
3469 GST_OBJECT_UNLOCK (pad);
3475 * gst_pad_query_default:
3476 * @pad: a #GstPad to call the default query handler on.
3477 * @query: (transfer none): the #GstQuery to handle.
3479 * Invokes the default query handler for the given pad.
3480 * The query is sent to all pads internally linked to @pad. Note that
3481 * if there are many possible sink pads that are internally linked to
3482 * @pad, only one will be sent the query.
3483 * Multi-sinkpad elements should implement custom query handlers.
3485 * Returns: TRUE if the query was performed succesfully.
3488 gst_pad_query_default (GstPad * pad, GstQuery * query)
3490 switch (GST_QUERY_TYPE (query)) {
3491 case GST_QUERY_POSITION:
3492 case GST_QUERY_SEEKING:
3493 case GST_QUERY_FORMATS:
3494 case GST_QUERY_LATENCY:
3495 case GST_QUERY_JITTER:
3496 case GST_QUERY_RATE:
3497 case GST_QUERY_CONVERT:
3499 return gst_pad_dispatcher
3500 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3505 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3507 GstPad *pad = data->pad;
3509 GstPadProbeCallback callback;
3512 /* if we have called this callback, do nothing */
3513 if (PROBE_COOKIE (hook) == data->cookie)
3516 PROBE_COOKIE (hook) = data->cookie;
3518 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3520 /* check if type matches */
3521 if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3524 if ((flags & 0xc0 & data->mask) == 0)
3527 if ((flags & 0x6) != (data->mask & 0x6))
3530 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3531 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3533 callback = (GstPadProbeCallback) hook->func;
3534 if (callback == NULL)
3537 GST_OBJECT_UNLOCK (pad);
3539 ret = callback (pad, data->mask, data->type_data, hook->data);
3541 GST_OBJECT_LOCK (pad);
3544 case GST_PROBE_REMOVE:
3545 /* remove the probe */
3546 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3547 cleanup_hook (pad, hook);
3549 case GST_PROBE_DROP:
3550 /* need to drop the data, make sure other probes don't get called
3552 GST_DEBUG_OBJECT (pad, "asked to drop item");
3553 data->mask = GST_PROBE_TYPE_INVALID;
3554 data->ret = GST_PROBE_DROP;
3556 case GST_PROBE_PASS:
3557 /* inform the pad block to let things pass */
3558 GST_DEBUG_OBJECT (pad, "asked to pass item");
3562 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3567 #define PROBE(pad,mask,data,label) \
3569 if (G_UNLIKELY (pad->num_probes)) { \
3570 ret = do_probe_callbacks (pad, mask, data); \
3571 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3576 static GstFlowReturn
3577 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3584 data.type_data = type_data;
3585 data.ret = GST_PROBE_OK;
3587 data.cookie = pad->priv->probe_cookie++;
3590 cookie = pad->priv->probe_cookie;
3592 g_hook_list_marshal (&pad->probes, FALSE,
3593 (GHookMarshaller) probe_hook_marshal, &data);
3595 /* if the list changed, call the new callbacks (they will not have their
3596 * cookie set to data.cookie */
3597 if (cookie != pad->priv->probe_cookie) {
3598 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3599 "probe list changed, restarting");
3603 if (data.ret == GST_PROBE_DROP)
3609 if (mask & GST_PROBE_TYPE_BLOCK) {
3610 while (GST_PAD_IS_BLOCKED (pad)) {
3611 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3612 "we are blocked %d times", pad->num_blocked);
3614 /* we might have released the lock */
3615 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3618 /* now we block the streaming thread. It can be unlocked when we
3619 * deactivate the pad (which will also set the FLUSHING flag) or
3620 * when the pad is unblocked. A flushing event will also unblock
3621 * the pad after setting the FLUSHING flag. */
3622 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3623 "Waiting to be unblocked or set flushing");
3624 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3625 GST_PAD_BLOCK_WAIT (pad);
3626 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3627 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3629 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3639 GST_DEBUG_OBJECT (pad, "pad is flushing");
3640 return GST_FLOW_WRONG_STATE;
3644 GST_DEBUG_OBJECT (pad, "data is dropped");
3645 return GST_FLOW_CUSTOM_SUCCESS;
3649 GST_DEBUG_OBJECT (pad, "data is passed");
3657 * gst_pad_get_offset:
3660 * Get the offset applied to the running time of @pad. @pad has to be a source
3663 * Returns: the offset.
3666 gst_pad_get_offset (GstPad * pad)
3670 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3672 GST_OBJECT_LOCK (pad);
3673 result = pad->offset;
3674 GST_OBJECT_UNLOCK (pad);
3680 * gst_pad_set_offset:
3682 * @offset: the offset
3684 * Set the offset that will be applied to the running time of @pad.
3687 gst_pad_set_offset (GstPad * pad, gint64 offset)
3693 g_return_if_fail (GST_IS_PAD (pad));
3695 GST_OBJECT_LOCK (pad);
3696 /* if nothing changed, do nothing */
3697 if (pad->offset == offset)
3700 pad->offset = offset;
3702 /* if no peer, we just updated the offset */
3703 if ((peer = GST_PAD_PEER (pad)) == NULL)
3706 /* switch pads around when dealing with a sinkpad */
3707 if (GST_PAD_IS_SINK (pad)) {
3708 /* ref the peer so it doesn't go away when we release the lock */
3709 tmp = gst_object_ref (peer);
3710 /* make sure we get the peer (the srcpad) */
3711 GST_OBJECT_UNLOCK (pad);
3717 GST_OBJECT_LOCK (pad);
3718 /* check if the pad didn't get relinked */
3719 if (GST_PAD_PEER (pad) != peer)
3722 /* we can release the ref now */
3723 gst_object_unref (peer);
3726 /* the index of the segment event in the array */
3727 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3729 /* lock order is srcpad >> sinkpad */
3730 GST_OBJECT_LOCK (peer);
3731 /* take the current segment event, adjust it and then place
3732 * it on the sinkpad. events on the srcpad are always active. */
3733 if (replace_event (pad, peer, idx))
3734 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3736 GST_OBJECT_UNLOCK (peer);
3739 GST_OBJECT_UNLOCK (pad);
3743 /**********************************************************************
3744 * Data passing functions
3749 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3752 GValue args[2] = { {0}, {0} };
3757 g_value_init (&ret, G_TYPE_BOOLEAN);
3758 g_value_set_boolean (&ret, TRUE);
3759 g_value_init (&args[0], GST_TYPE_PAD);
3760 g_value_set_object (&args[0], pad);
3761 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3762 g_value_set_boxed (&args[1], obj);
3764 if (GST_IS_EVENT (obj))
3765 detail = event_quark;
3766 else if (GST_IS_BUFFER (obj))
3767 detail = buffer_quark;
3768 else if (GST_IS_BUFFER_LIST (obj))
3769 detail = buffer_list_quark;
3773 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "Emiting have-data signal");
3776 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3777 res = g_value_get_boolean (&ret);
3780 g_value_unset (&ret);
3781 g_value_unset (&args[0]);
3782 g_value_unset (&args[1]);
3788 /* this is the chain function that does not perform the additional argument
3789 * checking for that little extra speed.
3791 static inline GstFlowReturn
3792 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3795 gboolean needs_events;
3797 GST_PAD_STREAM_LOCK (pad);
3799 GST_OBJECT_LOCK (pad);
3800 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3803 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3804 if (G_UNLIKELY (needs_events)) {
3805 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3807 GST_DEBUG_OBJECT (pad, "need to update all events");
3808 ret = gst_pad_update_events (pad);
3809 if (G_UNLIKELY (ret != GST_FLOW_OK))
3813 PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3815 GST_OBJECT_UNLOCK (pad);
3817 /* NOTE: we read the chainfunc unlocked.
3818 * we cannot hold the lock for the pad so we might send
3819 * the data to the wrong function. This is not really a
3820 * problem since functions are assigned at creation time
3821 * and don't change that often... */
3822 if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3823 GstPadChainFunction chainfunc;
3825 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3828 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3829 "calling chainfunction &%s with buffer %p",
3830 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3832 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3834 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3835 "called chainfunction &%s with buffer %p, returned %s",
3836 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3838 GstPadChainListFunction chainlistfunc;
3840 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3843 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3844 "calling chainlistfunction &%s",
3845 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3847 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3849 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3850 "called chainlistfunction &%s, returned %s",
3851 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3854 GST_PAD_STREAM_UNLOCK (pad);
3861 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3862 "chaining, but pad was flushing");
3863 GST_OBJECT_UNLOCK (pad);
3864 GST_PAD_STREAM_UNLOCK (pad);
3865 return GST_FLOW_WRONG_STATE;
3869 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3870 GST_OBJECT_UNLOCK (pad);
3871 GST_PAD_STREAM_UNLOCK (pad);
3876 GST_OBJECT_UNLOCK (pad);
3877 GST_PAD_STREAM_UNLOCK (pad);
3878 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3881 case GST_FLOW_CUSTOM_SUCCESS:
3882 GST_DEBUG_OBJECT (pad, "dropped buffer");
3886 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3893 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3894 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3895 "pushing, but not chainhandler");
3896 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3897 ("push on pad %s:%s but it has no chainfunction",
3898 GST_DEBUG_PAD_NAME (pad)));
3899 GST_PAD_STREAM_UNLOCK (pad);
3900 return GST_FLOW_NOT_SUPPORTED;
3906 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3907 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3910 * Chain a buffer to @pad.
3912 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3914 * If the caps on @buffer are different from the current caps on @pad, this
3915 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3916 * installed on @pad. If the new caps are not acceptable for @pad, this
3917 * function returns #GST_FLOW_NOT_NEGOTIATED.
3919 * The function proceeds calling the chain function installed on @pad (see
3920 * gst_pad_set_chain_function()) and the return value of that function is
3921 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3924 * In all cases, success or failure, the caller loses its reference to @buffer
3925 * after calling this function.
3927 * Returns: a #GstFlowReturn from the pad.
3932 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3934 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3935 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3936 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3938 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3941 static GstFlowReturn
3942 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3948 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3950 len = gst_buffer_list_len (list);
3953 for (i = 0; i < len; i++) {
3954 buffer = gst_buffer_list_get (list, i);
3956 gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3957 gst_buffer_ref (buffer));
3958 if (ret != GST_FLOW_OK)
3961 gst_buffer_list_unref (list);
3967 * gst_pad_chain_list:
3968 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3969 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3972 * Chain a bufferlist to @pad.
3974 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3976 * If the caps on the first buffer of @list are different from the current
3977 * caps on @pad, this function will call any setcaps function
3978 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3979 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3981 * The function proceeds calling the chainlist function installed on @pad (see
3982 * gst_pad_set_chain_list_function()) and the return value of that function is
3983 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3984 * chainlist function.
3986 * In all cases, success or failure, the caller loses its reference to @list
3987 * after calling this function.
3991 * Returns: a #GstFlowReturn from the pad.
3996 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3998 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3999 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4000 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4002 return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
4005 static GstFlowReturn
4006 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
4011 GST_OBJECT_LOCK (pad);
4012 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4015 type |= GST_PROBE_TYPE_PUSH;
4017 /* do block probes */
4018 PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
4020 /* do post-blocking probes */
4021 PROBE (pad, type, data, probe_stopped);
4023 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4026 /* take ref to peer pad before releasing the lock */
4027 gst_object_ref (peer);
4029 GST_OBJECT_UNLOCK (pad);
4031 ret = gst_pad_chain_data_unchecked (peer, type, data);
4033 gst_object_unref (peer);
4035 GST_OBJECT_LOCK (pad);
4037 if (pad->priv->using == 0) {
4038 /* pad is not active anymore, trigger idle callbacks */
4039 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4041 GST_OBJECT_UNLOCK (pad);
4045 /* ERROR recovery here */
4049 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4050 "pushing, but pad was flushing");
4051 GST_OBJECT_UNLOCK (pad);
4052 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4053 return GST_FLOW_WRONG_STATE;
4057 GST_OBJECT_UNLOCK (pad);
4058 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4061 case GST_FLOW_CUSTOM_SUCCESS:
4062 GST_DEBUG_OBJECT (pad, "dropped buffer");
4066 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4073 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4074 "pushing, but it was not linked");
4075 GST_OBJECT_UNLOCK (pad);
4076 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4077 return GST_FLOW_NOT_LINKED;
4083 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4084 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4087 * Pushes a buffer to the peer of @pad.
4089 * This function will call an installed pad block before triggering any
4090 * installed pad probes.
4092 * If the caps on @buffer are different from the currently configured caps on
4093 * @pad, this function will call any installed setcaps function on @pad (see
4094 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4095 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4097 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4098 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4101 * In all cases, success or failure, the caller loses its reference to @buffer
4102 * after calling this function.
4104 * Returns: a #GstFlowReturn from the peer pad.
4109 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4111 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4112 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4113 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4115 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
4119 * gst_pad_push_list:
4120 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4121 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4124 * Pushes a buffer list to the peer of @pad.
4126 * This function will call an installed pad block before triggering any
4127 * installed pad probes.
4129 * If the caps on the first buffer in the first group of @list are different
4130 * from the currently configured caps on @pad, this function will call any
4131 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4132 * case of failure to renegotiate the new format, this function returns
4133 * #GST_FLOW_NOT_NEGOTIATED.
4135 * If there are any probes installed on @pad every group of the buffer list
4136 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4137 * buffer list will be unreffed.
4139 * The function proceeds calling the chain function on the peer pad and returns
4140 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4141 * be returned. If the peer pad does not have any installed chainlist function
4142 * every group buffer of the list will be merged into a normal #GstBuffer and
4143 * chained via gst_pad_chain().
4145 * In all cases, success or failure, the caller loses its reference to @list
4146 * after calling this function.
4148 * Returns: a #GstFlowReturn from the peer pad.
4155 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4157 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4158 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4159 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4161 return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
4164 static GstFlowReturn
4165 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4166 GstBuffer ** buffer)
4169 GstPadGetRangeFunction getrangefunc;
4171 GST_PAD_STREAM_LOCK (pad);
4173 GST_OBJECT_LOCK (pad);
4174 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4176 GST_OBJECT_UNLOCK (pad);
4178 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4181 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4182 "calling getrangefunc %s, offset %"
4183 G_GUINT64_FORMAT ", size %u",
4184 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4186 ret = getrangefunc (pad, offset, size, buffer);
4188 if (G_UNLIKELY (ret != GST_FLOW_OK))
4189 goto get_range_failed;
4191 /* can only fire the signal if we have a valid buffer */
4192 GST_OBJECT_LOCK (pad);
4193 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
4195 GST_OBJECT_UNLOCK (pad);
4197 GST_PAD_STREAM_UNLOCK (pad);
4204 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4205 "pulling range, but pad was flushing");
4206 GST_OBJECT_UNLOCK (pad);
4207 GST_PAD_STREAM_UNLOCK (pad);
4208 return GST_FLOW_WRONG_STATE;
4212 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4213 ("pullrange on pad %s:%s but it has no getrangefunction",
4214 GST_DEBUG_PAD_NAME (pad)));
4215 GST_PAD_STREAM_UNLOCK (pad);
4216 return GST_FLOW_NOT_SUPPORTED;
4220 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4221 "probe returned %s", gst_flow_get_name (ret));
4222 GST_OBJECT_UNLOCK (pad);
4223 GST_PAD_STREAM_UNLOCK (pad);
4224 gst_buffer_unref (*buffer);
4230 GST_PAD_STREAM_UNLOCK (pad);
4232 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4233 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4234 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4240 * gst_pad_get_range:
4241 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4242 * @offset: The start offset of the buffer
4243 * @size: The length of the buffer
4244 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4245 * returns #GST_FLOW_ERROR if %NULL.
4247 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4248 * immediatly and @buffer is %NULL.
4250 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4251 * description of a getrange function. If @pad has no getrange function
4252 * installed (see gst_pad_set_getrange_function()) this function returns
4253 * #GST_FLOW_NOT_SUPPORTED.
4255 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4257 * Returns: a #GstFlowReturn from the pad.
4262 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4263 GstBuffer ** buffer)
4265 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4266 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4267 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4269 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4273 * gst_pad_pull_range:
4274 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4275 * @offset: The start offset of the buffer
4276 * @size: The length of the buffer
4277 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4278 * GST_FLOW_ERROR if %NULL.
4280 * Pulls a @buffer from the peer pad.
4282 * This function will first trigger the pad block signal if it was
4285 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4286 * function returns the result of gst_pad_get_range() on the peer pad.
4287 * See gst_pad_get_range() for a list of return values and for the
4288 * semantics of the arguments of this function.
4290 * @buffer's caps must either be unset or the same as what is already
4291 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4294 * Returns: a #GstFlowReturn from the peer pad.
4295 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4296 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4297 * @buffer may not be used or freed when any other return value than
4298 * #GST_FLOW_OK is returned.
4303 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4304 GstBuffer ** buffer)
4308 gboolean needs_events;
4310 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4311 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4312 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4314 GST_OBJECT_LOCK (pad);
4316 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4319 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4322 gst_object_ref (peer);
4324 GST_OBJECT_UNLOCK (pad);
4326 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4328 gst_object_unref (peer);
4330 GST_OBJECT_LOCK (pad);
4332 if (pad->priv->using == 0) {
4333 /* pad is not active anymore, trigger idle callbacks */
4334 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4335 post_probe_stopped);
4338 if (G_UNLIKELY (ret != GST_FLOW_OK))
4339 goto pull_range_failed;
4341 PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4342 post_probe_stopped);
4344 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4345 if (G_UNLIKELY (needs_events)) {
4346 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4348 GST_DEBUG_OBJECT (pad, "we need to update the events");
4349 ret = gst_pad_update_events (pad);
4350 if (G_UNLIKELY (ret != GST_FLOW_OK))
4353 GST_OBJECT_UNLOCK (pad);
4357 /* ERROR recovery here */
4360 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4361 gst_flow_get_name (ret));
4362 GST_OBJECT_UNLOCK (pad);
4367 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4368 "pulling range, but it was not linked");
4369 GST_OBJECT_UNLOCK (pad);
4370 return GST_FLOW_NOT_LINKED;
4375 GST_OBJECT_UNLOCK (pad);
4376 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4377 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4378 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4383 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4384 "post probe returned %s", gst_flow_get_name (ret));
4385 GST_OBJECT_UNLOCK (pad);
4386 if (ret == GST_FLOW_OK)
4387 gst_buffer_unref (*buffer);
4393 GST_OBJECT_UNLOCK (pad);
4394 gst_buffer_unref (*buffer);
4396 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4397 "pullrange returned events that were not accepted");
4403 * gst_pad_push_event:
4404 * @pad: a #GstPad to push the event to.
4405 * @event: (transfer full): the #GstEvent to send to the pad.
4407 * Sends the event to the peer of the given pad. This function is
4408 * mainly used by elements to send events to their peer
4411 * This function takes owership of the provided event so you should
4412 * gst_event_ref() it if you want to reuse the event after this call.
4414 * Returns: TRUE if the event was handled.
4419 gst_pad_push_event (GstPad * pad, GstEvent * event)
4423 gboolean result, do_event_actions = TRUE;
4424 gboolean stored = FALSE;
4426 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4427 g_return_val_if_fail (event != NULL, FALSE);
4428 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4430 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4433 GST_OBJECT_LOCK (pad);
4435 peerpad = GST_PAD_PEER (pad);
4437 /* Two checks to be made:
4438 * . (un)set the FLUSHING flag for flushing events,
4439 * . handle pad blocking */
4440 switch (GST_EVENT_TYPE (event)) {
4441 case GST_EVENT_FLUSH_START:
4442 GST_PAD_SET_FLUSHING (pad);
4444 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4445 /* flush start will have set the FLUSHING flag and will then
4446 * unlock all threads doing a GCond wait on the blocking pad. This
4447 * will typically unblock the STREAMING thread blocked on a pad. */
4448 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4449 "doing block signal.");
4450 GST_PAD_BLOCK_BROADCAST (pad);
4454 case GST_EVENT_FLUSH_STOP:
4455 GST_PAD_UNSET_FLUSHING (pad);
4456 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4457 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4463 /* store the event on the pad, but only on srcpads */
4464 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4467 idx = GST_EVENT_STICKY_IDX (event);
4468 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4469 GST_EVENT_TYPE_NAME (event), idx);
4471 /* srcpad sticky events always become active immediately */
4472 gst_event_replace (&pad->priv->events[idx].event, event);
4477 /* backwards compatibility mode for caps */
4478 if (do_event_actions) {
4479 do_event_actions = FALSE;
4481 switch (GST_EVENT_TYPE (event)) {
4482 case GST_EVENT_CAPS:
4486 GST_OBJECT_UNLOCK (pad);
4488 gst_event_parse_caps (event, &caps);
4489 /* FIXME, this is awkward because we don't check flushing here which means
4490 * that we can call the setcaps functions on flushing pads, this is not
4491 * quite what we want, otoh, this code should just go away and elements
4492 * that set caps on their srcpad should just setup stuff themselves. */
4493 gst_pad_call_setcaps (pad, caps);
4495 /* recheck everything, we released the lock */
4498 case GST_EVENT_SEGMENT:
4502 offset = pad->offset;
4503 /* check if we need to adjust the segment */
4504 if (offset != 0 && (peerpad != NULL)) {
4507 /* copy segment values */
4508 gst_event_copy_segment (event, &segment);
4509 gst_event_unref (event);
4511 /* adjust and make a new event with the offset applied */
4512 segment.base += offset;
4513 event = gst_event_new_segment (&segment);
4517 case GST_EVENT_RECONFIGURE:
4518 if (GST_PAD_IS_SINK (pad))
4519 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4525 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4528 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4529 | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4535 /* send probes after modifying the events above */
4536 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4538 /* now check the peer pad */
4539 if (peerpad == NULL)
4542 gst_object_ref (peerpad);
4544 GST_OBJECT_UNLOCK (pad);
4546 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4547 GST_EVENT_TYPE_NAME (event), peerpad);
4549 result = gst_pad_send_event (peerpad, event);
4551 /* Note: we gave away ownership of the event at this point */
4552 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4555 gst_object_unref (peerpad);
4557 GST_OBJECT_LOCK (pad);
4559 if (pad->priv->using == 0) {
4560 /* pad is not active anymore, trigger idle callbacks */
4561 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4563 GST_OBJECT_UNLOCK (pad);
4565 return result | stored;
4567 /* ERROR handling */
4570 GST_DEBUG_OBJECT (pad, "We're flushing");
4571 GST_OBJECT_UNLOCK (pad);
4572 gst_event_unref (event);
4577 GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4578 GST_OBJECT_UNLOCK (pad);
4579 gst_event_unref (event);
4584 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4585 GST_OBJECT_UNLOCK (pad);
4586 gst_event_unref (event);
4592 * gst_pad_send_event:
4593 * @pad: a #GstPad to send the event to.
4594 * @event: (transfer full): the #GstEvent to send to the pad.
4596 * Sends the event to the pad. This function can be used
4597 * by applications to send events in the pipeline.
4599 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4600 * sink pad, @event should be a downstream event. For example, you would not
4601 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4602 * Furthermore, some downstream events have to be serialized with data flow,
4603 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4604 * the event needs to be serialized with data flow, this function will take the
4605 * pad's stream lock while calling its event function.
4607 * To find out whether an event type is upstream, downstream, or downstream and
4608 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4609 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4610 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4611 * plugin doesn't need to bother itself with this information; the core handles
4612 * all necessary locks and checks.
4614 * This function takes owership of the provided event so you should
4615 * gst_event_ref() it if you want to reuse the event after this call.
4617 * Returns: TRUE if the event was handled.
4620 gst_pad_send_event (GstPad * pad, GstEvent * event)
4623 gboolean result = FALSE;
4624 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4626 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4627 g_return_val_if_fail (event != NULL, FALSE);
4629 GST_OBJECT_LOCK (pad);
4630 if (GST_PAD_IS_SINK (pad)) {
4631 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4632 goto wrong_direction;
4633 serialized = GST_EVENT_IS_SERIALIZED (event);
4634 sticky = GST_EVENT_IS_STICKY (event);
4635 } else if (GST_PAD_IS_SRC (pad)) {
4636 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4637 goto wrong_direction;
4638 /* events on srcpad never are serialized and sticky */
4639 serialized = sticky = FALSE;
4641 goto unknown_direction;
4643 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4645 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4647 switch (GST_EVENT_TYPE (event)) {
4648 case GST_EVENT_FLUSH_START:
4649 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4650 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4652 /* can't even accept a flush begin event when flushing */
4653 if (GST_PAD_IS_FLUSHING (pad))
4656 GST_PAD_SET_FLUSHING (pad);
4657 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4658 needs_events = FALSE;
4660 case GST_EVENT_FLUSH_STOP:
4661 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4662 GST_PAD_UNSET_FLUSHING (pad);
4663 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4665 GST_OBJECT_UNLOCK (pad);
4666 /* grab stream lock */
4667 GST_PAD_STREAM_LOCK (pad);
4669 GST_OBJECT_LOCK (pad);
4670 needs_events = FALSE;
4672 case GST_EVENT_RECONFIGURE:
4673 if (GST_PAD_IS_SRC (pad))
4674 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4676 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4677 GST_EVENT_TYPE_NAME (event));
4680 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4681 GST_OBJECT_UNLOCK (pad);
4682 GST_PAD_STREAM_LOCK (pad);
4684 GST_OBJECT_LOCK (pad);
4686 /* don't forward events on non-serialized events */
4687 needs_events = FALSE;
4690 /* store the event on the pad, but only on srcpads. We need to store the
4691 * event before checking the flushing flag. */
4696 switch (GST_EVENT_TYPE (event)) {
4697 case GST_EVENT_SEGMENT:
4698 if (pad->offset != 0) {
4701 /* copy segment values */
4702 gst_event_copy_segment (event, &segment);
4703 gst_event_unref (event);
4705 /* adjust and make a new event with the offset applied */
4706 segment.base += pad->offset;
4707 event = gst_event_new_segment (&segment);
4714 idx = GST_EVENT_STICKY_IDX (event);
4715 ev = &pad->priv->events[idx];
4717 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4718 GST_EVENT_TYPE_NAME (event), idx);
4720 if (ev->event != event) {
4721 gst_event_replace (&ev->pending, event);
4722 /* set the flag so that we update the events next time. We would
4723 * usually update below but we might be flushing too. */
4724 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4725 needs_events = TRUE;
4728 /* now do the probe */
4729 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4732 PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4738 if (G_UNLIKELY (needs_events)) {
4741 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4743 GST_DEBUG_OBJECT (pad, "need to update all events");
4744 ret = gst_pad_update_events (pad);
4745 if (ret != GST_FLOW_OK)
4747 GST_OBJECT_UNLOCK (pad);
4749 gst_event_unref (event);
4753 GstPadEventFunction eventfunc;
4755 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4758 GST_OBJECT_UNLOCK (pad);
4760 result = do_event_function (pad, event, eventfunc);
4764 GST_PAD_STREAM_UNLOCK (pad);
4766 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4770 /* ERROR handling */
4773 g_warning ("pad %s:%s sending %s event in wrong direction",
4774 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4775 GST_OBJECT_UNLOCK (pad);
4776 gst_event_unref (event);
4781 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4782 GST_OBJECT_UNLOCK (pad);
4783 gst_event_unref (event);
4788 g_warning ("pad %s:%s has no event handler, file a bug.",
4789 GST_DEBUG_PAD_NAME (pad));
4790 GST_OBJECT_UNLOCK (pad);
4792 GST_PAD_STREAM_UNLOCK (pad);
4793 gst_event_unref (event);
4798 GST_OBJECT_UNLOCK (pad);
4800 GST_PAD_STREAM_UNLOCK (pad);
4801 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4802 "Received event on flushing pad. Discarding");
4803 gst_event_unref (event);
4808 GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4809 GST_OBJECT_UNLOCK (pad);
4811 GST_PAD_STREAM_UNLOCK (pad);
4812 gst_event_unref (event);
4817 GST_OBJECT_UNLOCK (pad);
4819 GST_PAD_STREAM_UNLOCK (pad);
4820 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4821 gst_event_unref (event);
4827 * gst_pad_set_element_private:
4828 * @pad: the #GstPad to set the private data of.
4829 * @priv: The private data to attach to the pad.
4831 * Set the given private data gpointer on the pad.
4832 * This function can only be used by the element that owns the pad.
4833 * No locking is performed in this function.
4836 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4838 pad->element_private = priv;
4842 * gst_pad_get_element_private:
4843 * @pad: the #GstPad to get the private data of.
4845 * Gets the private data of a pad.
4846 * No locking is performed in this function.
4848 * Returns: (transfer none): a #gpointer to the private data.
4851 gst_pad_get_element_private (GstPad * pad)
4853 return pad->element_private;
4857 * gst_pad_get_sticky_event:
4858 * @pad: the #GstPad to get the event from.
4859 * @event_type: the #GstEventType that should be retrieved.
4861 * Returns a new reference of the sticky event of type @event_type
4864 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4867 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4869 GstEvent *event = NULL;
4872 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4873 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4875 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4877 GST_OBJECT_LOCK (pad);
4878 if ((event = pad->priv->events[idx].event)) {
4879 gst_event_ref (event);
4881 GST_OBJECT_UNLOCK (pad);
4887 * gst_pad_sticky_events_foreach:
4888 * @pad: the #GstPad that should be used for iteration.
4889 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4890 * @user_data: (closure): the optional user data.
4892 * Iterates all active sticky events on @pad and calls @foreach_func for every
4893 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4894 * is immediately stopped.
4896 * Returns: GST_FLOW_OK if iteration was successful
4899 gst_pad_sticky_events_foreach (GstPad * pad,
4900 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4902 GstFlowReturn ret = GST_FLOW_OK;
4906 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4907 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4909 GST_OBJECT_LOCK (pad);
4912 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4916 ev = &pad->priv->events[i];
4918 /* skip without active event */
4919 if ((event = ev->event) == NULL)
4922 gst_event_ref (event);
4923 GST_OBJECT_UNLOCK (pad);
4925 res = foreach_func (pad, event, user_data);
4927 GST_OBJECT_LOCK (pad);
4928 gst_event_unref (event);
4930 if (res != GST_FLOW_OK) {
4935 /* things could have changed while we release the lock, check if we still
4936 * are handling the same event, if we don't something changed and we have
4937 * to try again. FIXME. we need a cookie here. */
4938 if (event != ev->event) {
4939 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4943 GST_OBJECT_UNLOCK (pad);
4949 do_stream_status (GstPad * pad, GstStreamStatusType type,
4950 GThread * thread, GstTask * task)
4954 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4956 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4957 if (GST_IS_ELEMENT (parent)) {
4958 GstMessage *message;
4959 GValue value = { 0 };
4961 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4962 gchar *tname, *ename, *pname;
4964 /* create a good task name */
4965 ename = gst_element_get_name (parent);
4966 pname = gst_pad_get_name (pad);
4967 tname = g_strdup_printf ("%s:%s", ename, pname);
4971 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4975 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4978 g_value_init (&value, GST_TYPE_TASK);
4979 g_value_set_object (&value, task);
4980 gst_message_set_stream_status_object (message, &value);
4981 g_value_unset (&value);
4983 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4984 gst_element_post_message (parent, message);
4986 gst_object_unref (parent);
4991 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4993 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4998 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5000 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5004 static GstTaskThreadCallbacks thr_callbacks = {
5010 * gst_pad_start_task:
5011 * @pad: the #GstPad to start the task of
5012 * @func: the task function to call
5013 * @data: data passed to the task function
5015 * Starts a task that repeatedly calls @func with @data. This function
5016 * is mostly used in pad activation functions to start the dataflow.
5017 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5018 * before @func is called.
5020 * Returns: a %TRUE if the task could be started.
5023 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5028 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5029 g_return_val_if_fail (func != NULL, FALSE);
5031 GST_DEBUG_OBJECT (pad, "start task");
5033 GST_OBJECT_LOCK (pad);
5034 task = GST_PAD_TASK (pad);
5036 task = gst_task_create (func, data);
5037 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5038 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5039 GST_DEBUG_OBJECT (pad, "created task");
5040 GST_PAD_TASK (pad) = task;
5041 gst_object_ref (task);
5042 /* release lock to post the message */
5043 GST_OBJECT_UNLOCK (pad);
5045 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5047 gst_object_unref (task);
5049 GST_OBJECT_LOCK (pad);
5050 /* nobody else is supposed to have changed the pad now */
5051 if (GST_PAD_TASK (pad) != task)
5052 goto concurrent_stop;
5054 res = gst_task_set_state (task, GST_TASK_STARTED);
5055 GST_OBJECT_UNLOCK (pad);
5062 GST_OBJECT_UNLOCK (pad);
5068 * gst_pad_pause_task:
5069 * @pad: the #GstPad to pause the task of
5071 * Pause the task of @pad. This function will also wait until the
5072 * function executed by the task is finished if this function is not
5073 * called from the task function.
5075 * Returns: a TRUE if the task could be paused or FALSE when the pad
5079 gst_pad_pause_task (GstPad * pad)
5084 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5086 GST_DEBUG_OBJECT (pad, "pause task");
5088 GST_OBJECT_LOCK (pad);
5089 task = GST_PAD_TASK (pad);
5092 res = gst_task_set_state (task, GST_TASK_PAUSED);
5093 GST_OBJECT_UNLOCK (pad);
5095 /* wait for task function to finish, this lock is recursive so it does nothing
5096 * when the pause is called from the task itself */
5097 GST_PAD_STREAM_LOCK (pad);
5098 GST_PAD_STREAM_UNLOCK (pad);
5104 GST_DEBUG_OBJECT (pad, "pad has no task");
5105 GST_OBJECT_UNLOCK (pad);
5111 * gst_pad_stop_task:
5112 * @pad: the #GstPad to stop the task of
5114 * Stop the task of @pad. This function will also make sure that the
5115 * function executed by the task will effectively stop if not called
5116 * from the GstTaskFunction.
5118 * This function will deadlock if called from the GstTaskFunction of
5119 * the task. Use gst_task_pause() instead.
5121 * Regardless of whether the pad has a task, the stream lock is acquired and
5122 * released so as to ensure that streaming through this pad has finished.
5124 * Returns: a TRUE if the task could be stopped or FALSE on error.
5127 gst_pad_stop_task (GstPad * pad)
5132 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5134 GST_DEBUG_OBJECT (pad, "stop task");
5136 GST_OBJECT_LOCK (pad);
5137 task = GST_PAD_TASK (pad);
5140 GST_PAD_TASK (pad) = NULL;
5141 res = gst_task_set_state (task, GST_TASK_STOPPED);
5142 GST_OBJECT_UNLOCK (pad);
5144 GST_PAD_STREAM_LOCK (pad);
5145 GST_PAD_STREAM_UNLOCK (pad);
5147 if (!gst_task_join (task))
5150 gst_object_unref (task);
5156 GST_DEBUG_OBJECT (pad, "no task");
5157 GST_OBJECT_UNLOCK (pad);
5159 GST_PAD_STREAM_LOCK (pad);
5160 GST_PAD_STREAM_UNLOCK (pad);
5162 /* this is not an error */
5167 /* this is bad, possibly the application tried to join the task from
5168 * the task's thread. We install the task again so that it will be stopped
5169 * again from the right thread next time hopefully. */
5170 GST_OBJECT_LOCK (pad);
5171 GST_DEBUG_OBJECT (pad, "join failed");
5172 /* we can only install this task if there was no other task */
5173 if (GST_PAD_TASK (pad) == NULL)
5174 GST_PAD_TASK (pad) = task;
5175 GST_OBJECT_UNLOCK (pad);