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 typedef struct _GstPadPushCache GstPadPushCache;
100 struct _GstPadPushCache
102 GstPad *peer; /* reffed peer pad */
105 static GstPadPushCache _pad_cache_invalid = { NULL, };
107 #define PAD_CACHE_INVALID (&_pad_cache_invalid)
109 #define GST_PAD_GET_PRIVATE(obj) \
110 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
112 /* we have a pending and an active event on the pad. On source pads only the
113 * active event is used. On sinkpads, events are copied to the pending entry and
114 * moved to the active event when the eventfunc returned TRUE. */
121 struct _GstPadPrivate
123 GstPadPushCache *cache_ptr;
125 PadEvent events[GST_EVENT_MAX_STICKY];
128 static void gst_pad_dispose (GObject * object);
129 static void gst_pad_finalize (GObject * object);
130 static void gst_pad_set_property (GObject * object, guint prop_id,
131 const GValue * value, GParamSpec * pspec);
132 static void gst_pad_get_property (GObject * object, guint prop_id,
133 GValue * value, GParamSpec * pspec);
135 static GstFlowReturn handle_pad_block (GstPad * pad);
136 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
137 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
138 static gboolean gst_pad_activate_default (GstPad * pad);
139 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
140 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
141 GstBufferList * list);
143 static GstObjectClass *parent_class = NULL;
144 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
146 static GParamSpec *pspec_caps = NULL;
148 /* quarks for probe signals */
149 static GQuark buffer_quark;
150 static GQuark buffer_list_quark;
151 static GQuark event_quark;
160 static GstFlowQuarks flow_quarks[] = {
161 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
162 {GST_FLOW_RESEND, "resend", 0},
163 {GST_FLOW_OK, "ok", 0},
164 {GST_FLOW_NOT_LINKED, "not-linked", 0},
165 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
166 {GST_FLOW_UNEXPECTED, "unexpected", 0},
167 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
168 {GST_FLOW_ERROR, "error", 0},
169 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
170 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
175 * @ret: a #GstFlowReturn to get the name of.
177 * Gets a string representing the given flow return.
179 * Returns: a static string with the name of the flow return.
181 G_CONST_RETURN gchar *
182 gst_flow_get_name (GstFlowReturn ret)
186 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
188 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
189 if (ret == flow_quarks[i].ret)
190 return flow_quarks[i].name;
197 * @ret: a #GstFlowReturn to get the quark of.
199 * Get the unique quark for the given GstFlowReturn.
201 * Returns: the quark associated with the flow return or 0 if an
202 * invalid return was specified.
205 gst_flow_to_quark (GstFlowReturn ret)
209 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
211 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
212 if (ret == flow_quarks[i].ret)
213 return flow_quarks[i].quark;
222 buffer_quark = g_quark_from_static_string ("buffer"); \
223 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
224 event_quark = g_quark_from_static_string ("event"); \
226 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
227 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
230 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
231 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
234 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
237 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
238 GValue * return_accu, const GValue * handler_return, gpointer dummy)
240 gboolean ret = g_value_get_boolean (handler_return);
242 GST_DEBUG ("accumulated %d", ret);
243 g_value_set_boolean (return_accu, ret);
249 default_have_data (GstPad * pad, GstMiniObject * o)
255 gst_pad_class_init (GstPadClass * klass)
257 GObjectClass *gobject_class;
258 GstObjectClass *gstobject_class;
260 gobject_class = G_OBJECT_CLASS (klass);
261 gstobject_class = GST_OBJECT_CLASS (klass);
263 g_type_class_add_private (klass, sizeof (GstPadPrivate));
265 parent_class = g_type_class_peek_parent (klass);
267 gobject_class->dispose = gst_pad_dispose;
268 gobject_class->finalize = gst_pad_finalize;
269 gobject_class->set_property = gst_pad_set_property;
270 gobject_class->get_property = gst_pad_get_property;
274 * @pad: the pad that emitted the signal
275 * @peer: the peer pad that has been connected
277 * Signals that a pad has been linked to the peer pad.
279 gst_pad_signals[PAD_LINKED] =
280 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
281 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
282 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
285 * @pad: the pad that emitted the signal
286 * @peer: the peer pad that has been disconnected
288 * Signals that a pad has been unlinked from the peer pad.
290 gst_pad_signals[PAD_UNLINKED] =
291 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
292 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
293 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
295 * GstPad::request-link:
296 * @pad: the pad that emitted the signal
297 * @peer: the peer pad for which a connection is requested
299 * Signals that a pad connection has been requested.
301 gst_pad_signals[PAD_REQUEST_LINK] =
302 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
303 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
304 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
308 * @pad: the pad that emitted the signal
309 * @mini_obj: new data
311 * Signals that new data is available on the pad. This signal is used
312 * internally for implementing pad probes.
313 * See gst_pad_add_*_probe functions.
315 * Returns: %TRUE to keep the data, %FALSE to drop it
317 gst_pad_signals[PAD_HAVE_DATA] =
318 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
319 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
320 G_STRUCT_OFFSET (GstPadClass, have_data),
321 _gst_do_pass_data_accumulator,
322 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
324 pspec_caps = g_param_spec_boxed ("caps", "Caps",
325 "The capabilities of the pad", GST_TYPE_CAPS,
326 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
327 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
329 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
330 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
331 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
332 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
333 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
334 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
335 g_param_spec_object ("template", "Template",
336 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 gstobject_class->path_string_separator = ".";
341 /* Register common function pointer descriptions */
342 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
343 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
344 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
345 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
346 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
347 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
348 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
350 klass->have_data = default_have_data;
354 gst_pad_init (GstPad * pad)
356 pad->priv = GST_PAD_GET_PRIVATE (pad);
358 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
360 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
361 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
362 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
363 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
364 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
365 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
366 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
368 GST_PAD_SET_FLUSHING (pad);
370 /* FIXME 0.11: Store this directly in the instance struct */
371 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
372 g_static_rec_mutex_init (pad->stream_rec_lock);
374 pad->block_cond = g_cond_new ();
377 /* called when setting the pad inactive. It removes all sticky events from
380 clear_events (PadEvent events[])
384 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
385 gst_event_replace (&events[i].event, NULL);
386 gst_event_replace (&events[i].pending, NULL);
390 /* The sticky event with @idx from the srcpad is copied to the
391 * pending event on the sinkpad (when different).
392 * This function applies the pad offsets in case of segment events.
393 * This will make sure that we send the event to the sinkpad event
394 * function when the next buffer of event arrives.
395 * Should be called with the OBJECT lock of both pads.
396 * This function returns TRUE when there is a pending event on the
399 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
401 PadEvent *srcev, *sinkev;
403 gboolean pending = FALSE;
405 srcev = &srcpad->priv->events[idx];
407 if ((event = srcev->event)) {
408 sinkev = &sinkpad->priv->events[idx];
410 switch (GST_EVENT_TYPE (event)) {
411 case GST_EVENT_SEGMENT:
416 offset = srcpad->offset + sinkpad->offset;
418 gst_event_copy_segment (event, &segment);
419 /* adjust the base time. FIXME, check negative times, try to tweak the
420 * start to do clipping on negative times */
421 segment.base += offset;
422 /* make a new event from the updated segment */
423 event = gst_event_new_segment (&segment);
430 if (sinkev->event != event) {
431 /* put in the pending entry when different */
432 gst_event_replace (&sinkev->pending, event);
439 /* should be called with the OBJECT_LOCK */
441 get_pad_caps (GstPad * pad)
443 GstCaps *caps = NULL;
447 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
448 /* we can only use the caps when we have successfully send the caps
449 * event to the event function and is thus in the active entry */
450 if ((event = pad->priv->events[idx].event))
451 gst_event_parse_caps (event, &caps);
457 gst_pad_dispose (GObject * object)
459 GstPad *pad = GST_PAD_CAST (object);
462 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
464 /* unlink the peer pad */
465 if ((peer = gst_pad_get_peer (pad))) {
466 /* window for MT unsafeness, someone else could unlink here
467 * and then we call unlink with wrong pads. The unlink
468 * function would catch this and safely return failed. */
469 if (GST_PAD_IS_SRC (pad))
470 gst_pad_unlink (pad, peer);
472 gst_pad_unlink (peer, pad);
474 gst_object_unref (peer);
477 gst_pad_set_pad_template (pad, NULL);
479 if (pad->block_destroy_data && pad->block_data) {
480 pad->block_destroy_data (pad->block_data);
481 pad->block_data = NULL;
484 clear_events (pad->priv->events);
486 G_OBJECT_CLASS (parent_class)->dispose (object);
490 gst_pad_finalize (GObject * object)
492 GstPad *pad = GST_PAD_CAST (object);
495 /* in case the task is still around, clean it up */
496 if ((task = GST_PAD_TASK (pad))) {
497 gst_task_join (task);
498 GST_PAD_TASK (pad) = NULL;
499 gst_object_unref (task);
502 if (pad->stream_rec_lock) {
503 g_static_rec_mutex_free (pad->stream_rec_lock);
504 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
505 pad->stream_rec_lock = NULL;
507 if (pad->block_cond) {
508 g_cond_free (pad->block_cond);
509 pad->block_cond = NULL;
512 G_OBJECT_CLASS (parent_class)->finalize (object);
516 gst_pad_set_property (GObject * object, guint prop_id,
517 const GValue * value, GParamSpec * pspec)
519 g_return_if_fail (GST_IS_PAD (object));
522 case PAD_PROP_DIRECTION:
523 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
525 case PAD_PROP_TEMPLATE:
526 gst_pad_set_pad_template (GST_PAD_CAST (object),
527 (GstPadTemplate *) g_value_get_object (value));
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 gst_pad_get_property (GObject * object, guint prop_id,
537 GValue * value, GParamSpec * pspec)
539 g_return_if_fail (GST_IS_PAD (object));
543 GST_OBJECT_LOCK (object);
544 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
545 GST_OBJECT_UNLOCK (object);
547 case PAD_PROP_DIRECTION:
548 g_value_set_enum (value, GST_PAD_DIRECTION (object));
550 case PAD_PROP_TEMPLATE:
551 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 * @name: the name of the new pad.
562 * @direction: the #GstPadDirection of the pad.
564 * Creates a new pad with the given name in the given direction.
565 * If name is NULL, a guaranteed unique name (across all pads)
567 * This function makes a copy of the name so you can safely free the name.
569 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
574 gst_pad_new (const gchar * name, GstPadDirection direction)
576 return g_object_new (GST_TYPE_PAD,
577 "name", name, "direction", direction, NULL);
581 * gst_pad_new_from_template:
582 * @templ: the pad template to use
583 * @name: the name of the element
585 * Creates a new pad with the given name from the given template.
586 * If name is NULL, a guaranteed unique name (across all pads)
588 * This function makes a copy of the name so you can safely free the name.
590 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
593 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
595 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
597 return g_object_new (GST_TYPE_PAD,
598 "name", name, "direction", templ->direction, "template", templ, NULL);
602 * gst_pad_new_from_static_template:
603 * @templ: the #GstStaticPadTemplate to use
604 * @name: the name of the element
606 * Creates a new pad with the given name from the given static template.
607 * If name is NULL, a guaranteed unique name (across all pads)
609 * This function makes a copy of the name so you can safely free the name.
611 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
614 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
618 GstPadTemplate *template;
620 template = gst_static_pad_template_get (templ);
621 pad = gst_pad_new_from_template (template, name);
622 gst_object_unref (template);
627 * gst_pad_get_direction:
628 * @pad: a #GstPad to get the direction of.
630 * Gets the direction of the pad. The direction of the pad is
631 * decided at construction time so this function does not take
634 * Returns: the #GstPadDirection of the pad.
639 gst_pad_get_direction (GstPad * pad)
641 GstPadDirection result;
643 /* PAD_UNKNOWN is a little silly but we need some sort of
644 * error return value */
645 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
647 result = GST_PAD_DIRECTION (pad);
653 gst_pad_activate_default (GstPad * pad)
655 return gst_pad_activate_push (pad, TRUE);
659 pre_activate (GstPad * pad, GstActivateMode new_mode)
662 case GST_ACTIVATE_PUSH:
663 case GST_ACTIVATE_PULL:
664 GST_OBJECT_LOCK (pad);
665 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
667 GST_PAD_UNSET_FLUSHING (pad);
668 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
669 GST_OBJECT_UNLOCK (pad);
671 case GST_ACTIVATE_NONE:
672 GST_OBJECT_LOCK (pad);
673 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
674 _priv_gst_pad_invalidate_cache (pad);
675 GST_PAD_SET_FLUSHING (pad);
676 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
677 /* unlock blocked pads so element can resume and stop */
678 GST_PAD_BLOCK_BROADCAST (pad);
679 GST_OBJECT_UNLOCK (pad);
685 post_activate (GstPad * pad, GstActivateMode new_mode)
688 case GST_ACTIVATE_PUSH:
689 case GST_ACTIVATE_PULL:
692 case GST_ACTIVATE_NONE:
693 /* ensures that streaming stops */
694 GST_PAD_STREAM_LOCK (pad);
695 GST_DEBUG_OBJECT (pad, "stopped streaming");
696 GST_OBJECT_LOCK (pad);
697 clear_events (pad->priv->events);
698 GST_OBJECT_UNLOCK (pad);
699 GST_PAD_STREAM_UNLOCK (pad);
705 * gst_pad_set_active:
706 * @pad: the #GstPad to activate or deactivate.
707 * @active: whether or not the pad should be active.
709 * Activates or deactivates the given pad.
710 * Normally called from within core state change functions.
712 * If @active, makes sure the pad is active. If it is already active, either in
713 * push or pull mode, just return. Otherwise dispatches to the pad's activate
714 * function to perform the actual activation.
716 * If not @active, checks the pad's current mode and calls
717 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
720 * Returns: #TRUE if the operation was successful.
725 gst_pad_set_active (GstPad * pad, gboolean active)
728 gboolean ret = FALSE;
730 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
732 GST_OBJECT_LOCK (pad);
733 old = GST_PAD_ACTIVATE_MODE (pad);
734 GST_OBJECT_UNLOCK (pad);
738 case GST_ACTIVATE_PUSH:
739 GST_DEBUG_OBJECT (pad, "activating pad from push");
742 case GST_ACTIVATE_PULL:
743 GST_DEBUG_OBJECT (pad, "activating pad from pull");
746 case GST_ACTIVATE_NONE:
747 GST_DEBUG_OBJECT (pad, "activating pad from none");
748 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
751 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
756 case GST_ACTIVATE_PUSH:
757 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
758 ret = gst_pad_activate_push (pad, FALSE);
760 case GST_ACTIVATE_PULL:
761 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
762 ret = gst_pad_activate_pull (pad, FALSE);
764 case GST_ACTIVATE_NONE:
765 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
769 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
775 GST_OBJECT_LOCK (pad);
777 g_critical ("Failed to deactivate pad %s:%s, very bad",
778 GST_DEBUG_PAD_NAME (pad));
780 GST_WARNING_OBJECT (pad, "Failed to activate pad");
782 GST_OBJECT_UNLOCK (pad);
785 GST_OBJECT_LOCK (pad);
786 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
787 GST_OBJECT_UNLOCK (pad);
795 * gst_pad_activate_pull:
796 * @pad: the #GstPad to activate or deactivate.
797 * @active: whether or not the pad should be active.
799 * Activates or deactivates the given pad in pull mode via dispatching to the
800 * pad's activatepullfunc. For use from within pad activation functions only.
801 * When called on sink pads, will first proxy the call to the peer pad, which
802 * is expected to activate its internally linked pads from within its
803 * activate_pull function.
805 * If you don't know what this is, you probably don't want to call it.
807 * Returns: TRUE if the operation was successful.
812 gst_pad_activate_pull (GstPad * pad, gboolean active)
814 GstActivateMode old, new;
817 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
819 GST_OBJECT_LOCK (pad);
820 old = GST_PAD_ACTIVATE_MODE (pad);
821 GST_OBJECT_UNLOCK (pad);
825 case GST_ACTIVATE_PULL:
826 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
828 case GST_ACTIVATE_PUSH:
829 GST_DEBUG_OBJECT (pad,
830 "activating pad from push, deactivate push first");
831 /* pad was activate in the wrong direction, deactivate it
832 * and reactivate it in pull mode */
833 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
834 goto deactivate_failed;
835 /* fallthrough, pad is deactivated now. */
836 case GST_ACTIVATE_NONE:
837 GST_DEBUG_OBJECT (pad, "activating pad from none");
842 case GST_ACTIVATE_NONE:
843 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
845 case GST_ACTIVATE_PUSH:
846 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
847 /* pad was activated in the other direction, deactivate it
848 * in push mode, this should not happen... */
849 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
850 goto deactivate_failed;
851 /* everything is fine now */
853 case GST_ACTIVATE_PULL:
854 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
859 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
860 if ((peer = gst_pad_get_peer (pad))) {
861 GST_DEBUG_OBJECT (pad, "calling peer");
862 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
864 gst_object_unref (peer);
866 /* there is no peer, this is only fatal when we activate. When we
867 * deactivate, we must assume the application has unlinked the peer and
868 * will deactivate it eventually. */
872 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
875 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
876 goto failure; /* Can't activate pull on a src without a
880 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
881 pre_activate (pad, new);
883 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
884 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
887 /* can happen for sinks of passthrough elements */
890 post_activate (pad, new);
892 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
893 active ? "activated" : "deactivated");
899 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
900 active ? "activated" : "deactivated");
905 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
906 "failed to %s in switch to pull from mode %d",
907 (active ? "activate" : "deactivate"), old);
912 GST_OBJECT_LOCK (peer);
913 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
914 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
915 GST_OBJECT_UNLOCK (peer);
916 gst_object_unref (peer);
921 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
927 GST_OBJECT_LOCK (pad);
928 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
929 active ? "activate" : "deactivate");
930 _priv_gst_pad_invalidate_cache (pad);
931 GST_PAD_SET_FLUSHING (pad);
932 GST_PAD_ACTIVATE_MODE (pad) = old;
933 GST_OBJECT_UNLOCK (pad);
939 * gst_pad_activate_push:
940 * @pad: the #GstPad to activate or deactivate.
941 * @active: whether the pad should be active or not.
943 * Activates or deactivates the given pad in push mode via dispatching to the
944 * pad's activatepushfunc. For use from within pad activation functions only.
946 * If you don't know what this is, you probably don't want to call it.
948 * Returns: %TRUE if the operation was successful.
953 gst_pad_activate_push (GstPad * pad, gboolean active)
955 GstActivateMode old, new;
957 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
958 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
959 active ? "activated" : "deactivated");
961 GST_OBJECT_LOCK (pad);
962 old = GST_PAD_ACTIVATE_MODE (pad);
963 GST_OBJECT_UNLOCK (pad);
967 case GST_ACTIVATE_PUSH:
968 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
970 case GST_ACTIVATE_PULL:
971 GST_DEBUG_OBJECT (pad,
972 "activating pad from push, deactivating pull first");
973 /* pad was activate in the wrong direction, deactivate it
974 * an reactivate it in push mode */
975 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
976 goto deactivate_failed;
977 /* fallthrough, pad is deactivated now. */
978 case GST_ACTIVATE_NONE:
979 GST_DEBUG_OBJECT (pad, "activating pad from none");
984 case GST_ACTIVATE_NONE:
985 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
987 case GST_ACTIVATE_PULL:
988 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
989 /* pad was activated in the other direction, deactivate it
990 * in pull mode, this should not happen... */
991 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
992 goto deactivate_failed;
993 /* everything is fine now */
995 case GST_ACTIVATE_PUSH:
996 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
1001 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1002 pre_activate (pad, new);
1004 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
1005 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
1009 /* quite ok, element relies on state change func to prepare itself */
1012 post_activate (pad, new);
1014 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
1015 active ? "activated" : "deactivated");
1020 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
1021 active ? "activated" : "deactivated");
1026 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1027 "failed to %s in switch to push from mode %d",
1028 (active ? "activate" : "deactivate"), old);
1033 GST_OBJECT_LOCK (pad);
1034 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1035 active ? "activate" : "deactivate");
1036 _priv_gst_pad_invalidate_cache (pad);
1037 GST_PAD_SET_FLUSHING (pad);
1038 GST_PAD_ACTIVATE_MODE (pad) = old;
1039 GST_OBJECT_UNLOCK (pad);
1045 * gst_pad_is_active:
1046 * @pad: the #GstPad to query
1048 * Query if a pad is active
1050 * Returns: TRUE if the pad is active.
1055 gst_pad_is_active (GstPad * pad)
1057 gboolean result = FALSE;
1059 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1061 GST_OBJECT_LOCK (pad);
1062 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1063 GST_OBJECT_UNLOCK (pad);
1069 * gst_pad_set_blocked:
1070 * @pad: the #GstPad to block or unblock
1071 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1072 * @callback: #GstPadBlockCallback that will be called when the
1073 * operation succeeds
1074 * @user_data: (closure): user data passed to the callback
1075 * @destroy_data: #GDestroyNotify for user_data
1077 * Blocks or unblocks the dataflow on a pad. The provided callback
1078 * is called when the operation succeeds; this happens right before the next
1079 * attempt at pushing a buffer on the pad.
1081 * This can take a while as the pad can only become blocked when real dataflow
1083 * When the pipeline is stalled, for example in PAUSED, this can
1084 * take an indeterminate amount of time.
1085 * You can pass NULL as the callback to make this call block. Be careful with
1086 * this blocking call as it might not return for reasons stated above.
1089 * Pad block handlers are only called for source pads in push mode
1090 * and sink pads in pull mode.
1093 * Returns: TRUE if the pad could be blocked. This function can fail if the
1094 * wrong parameters were passed or the pad was already in the requested state.
1099 gst_pad_set_blocked (GstPad * pad, gboolean blocked,
1100 GstPadBlockCallback callback, gpointer user_data,
1101 GDestroyNotify destroy_data)
1103 gboolean was_blocked = FALSE;
1105 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1107 GST_OBJECT_LOCK (pad);
1109 was_blocked = GST_PAD_IS_BLOCKED (pad);
1111 if (G_UNLIKELY (was_blocked == blocked))
1112 goto had_right_state;
1115 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1117 _priv_gst_pad_invalidate_cache (pad);
1118 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1120 if (pad->block_destroy_data && pad->block_data)
1121 pad->block_destroy_data (pad->block_data);
1123 pad->block_callback = callback;
1124 pad->block_data = user_data;
1125 pad->block_destroy_data = destroy_data;
1126 pad->block_callback_called = FALSE;
1128 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1129 GST_PAD_BLOCK_WAIT (pad);
1130 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1133 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1135 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1137 if (pad->block_destroy_data && pad->block_data)
1138 pad->block_destroy_data (pad->block_data);
1140 pad->block_callback = callback;
1141 pad->block_data = user_data;
1142 pad->block_destroy_data = destroy_data;
1143 pad->block_callback_called = FALSE;
1145 GST_PAD_BLOCK_BROADCAST (pad);
1147 /* no callback, wait for the unblock to happen */
1148 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1149 GST_PAD_BLOCK_WAIT (pad);
1150 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1153 GST_OBJECT_UNLOCK (pad);
1159 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1160 "pad was in right state (%d)", was_blocked);
1161 GST_OBJECT_UNLOCK (pad);
1168 * gst_pad_is_blocked:
1169 * @pad: the #GstPad to query
1171 * Checks if the pad is blocked or not. This function returns the
1172 * last requested state of the pad. It is not certain that the pad
1173 * is actually blocking at this point (see gst_pad_is_blocking()).
1175 * Returns: TRUE if the pad is blocked.
1180 gst_pad_is_blocked (GstPad * pad)
1182 gboolean result = FALSE;
1184 g_return_val_if_fail (GST_IS_PAD (pad), result);
1186 GST_OBJECT_LOCK (pad);
1187 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1188 GST_OBJECT_UNLOCK (pad);
1194 * gst_pad_is_blocking:
1195 * @pad: the #GstPad to query
1197 * Checks if the pad is blocking or not. This is a guaranteed state
1198 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1200 * Returns: TRUE if the pad is blocking.
1207 gst_pad_is_blocking (GstPad * pad)
1209 gboolean result = FALSE;
1211 g_return_val_if_fail (GST_IS_PAD (pad), result);
1213 GST_OBJECT_LOCK (pad);
1214 /* the blocking flag is only valid if the pad is not flushing */
1215 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1216 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1217 GST_OBJECT_UNLOCK (pad);
1223 * gst_pad_set_activate_function:
1225 * @activate: the #GstPadActivateFunction to set.
1227 * Sets the given activate function for @pad. The activate function will
1228 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1229 * the actual activation. Only makes sense to set on sink pads.
1231 * Call this function if your sink pad can start a pull-based task.
1234 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1236 g_return_if_fail (GST_IS_PAD (pad));
1238 GST_PAD_ACTIVATEFUNC (pad) = activate;
1239 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1240 GST_DEBUG_FUNCPTR_NAME (activate));
1244 * gst_pad_set_activatepull_function:
1246 * @activatepull: the #GstPadActivateModeFunction to set.
1248 * Sets the given activate_pull function for the pad. An activate_pull function
1249 * prepares the element and any upstream connections for pulling. See XXX
1250 * part-activation.txt for details.
1253 gst_pad_set_activatepull_function (GstPad * pad,
1254 GstPadActivateModeFunction activatepull)
1256 g_return_if_fail (GST_IS_PAD (pad));
1258 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1259 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1260 GST_DEBUG_FUNCPTR_NAME (activatepull));
1264 * gst_pad_set_activatepush_function:
1266 * @activatepush: the #GstPadActivateModeFunction to set.
1268 * Sets the given activate_push function for the pad. An activate_push function
1269 * prepares the element for pushing. See XXX part-activation.txt for details.
1272 gst_pad_set_activatepush_function (GstPad * pad,
1273 GstPadActivateModeFunction activatepush)
1275 g_return_if_fail (GST_IS_PAD (pad));
1277 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1278 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1279 GST_DEBUG_FUNCPTR_NAME (activatepush));
1283 * gst_pad_set_chain_function:
1284 * @pad: a sink #GstPad.
1285 * @chain: the #GstPadChainFunction to set.
1287 * Sets the given chain function for the pad. The chain function is called to
1288 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1291 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1293 g_return_if_fail (GST_IS_PAD (pad));
1294 g_return_if_fail (GST_PAD_IS_SINK (pad));
1296 GST_PAD_CHAINFUNC (pad) = chain;
1297 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1298 GST_DEBUG_FUNCPTR_NAME (chain));
1302 * gst_pad_set_chain_list_function:
1303 * @pad: a sink #GstPad.
1304 * @chainlist: the #GstPadChainListFunction to set.
1306 * Sets the given chain list function for the pad. The chainlist function is
1307 * called to process a #GstBufferList input buffer list. See
1308 * #GstPadChainListFunction for more details.
1313 gst_pad_set_chain_list_function (GstPad * pad,
1314 GstPadChainListFunction chainlist)
1316 g_return_if_fail (GST_IS_PAD (pad));
1317 g_return_if_fail (GST_PAD_IS_SINK (pad));
1319 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1320 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1321 GST_DEBUG_FUNCPTR_NAME (chainlist));
1325 * gst_pad_set_getrange_function:
1326 * @pad: a source #GstPad.
1327 * @get: the #GstPadGetRangeFunction to set.
1329 * Sets the given getrange function for the pad. The getrange function is
1330 * called to produce a new #GstBuffer to start the processing pipeline. see
1331 * #GstPadGetRangeFunction for a description of the getrange function.
1334 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1336 g_return_if_fail (GST_IS_PAD (pad));
1337 g_return_if_fail (GST_PAD_IS_SRC (pad));
1339 GST_PAD_GETRANGEFUNC (pad) = get;
1341 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1342 GST_DEBUG_FUNCPTR_NAME (get));
1346 * gst_pad_set_event_function:
1347 * @pad: a #GstPad of either direction.
1348 * @event: the #GstPadEventFunction to set.
1350 * Sets the given event handler for the pad.
1353 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1355 g_return_if_fail (GST_IS_PAD (pad));
1357 GST_PAD_EVENTFUNC (pad) = event;
1359 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1360 GST_DEBUG_FUNCPTR_NAME (event));
1364 * gst_pad_set_query_function:
1365 * @pad: a #GstPad of either direction.
1366 * @query: the #GstPadQueryFunction to set.
1368 * Set the given query function for the pad.
1371 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1373 g_return_if_fail (GST_IS_PAD (pad));
1375 GST_PAD_QUERYFUNC (pad) = query;
1377 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1378 GST_DEBUG_FUNCPTR_NAME (query));
1382 * gst_pad_set_query_type_function:
1383 * @pad: a #GstPad of either direction.
1384 * @type_func: the #GstPadQueryTypeFunction to set.
1386 * Set the given query type function for the pad.
1389 gst_pad_set_query_type_function (GstPad * pad,
1390 GstPadQueryTypeFunction type_func)
1392 g_return_if_fail (GST_IS_PAD (pad));
1394 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1396 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1397 GST_DEBUG_FUNCPTR_NAME (type_func));
1401 * gst_pad_get_query_types:
1404 * Get an array of supported queries that can be performed
1407 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1410 const GstQueryType *
1411 gst_pad_get_query_types (GstPad * pad)
1413 GstPadQueryTypeFunction func;
1415 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1417 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1429 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1431 *data = gst_pad_get_query_types (pad);
1437 * gst_pad_get_query_types_default:
1440 * Invoke the default dispatcher for the query types on
1443 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1444 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1445 * query types function.
1447 const GstQueryType *
1448 gst_pad_get_query_types_default (GstPad * pad)
1450 GstQueryType *result = NULL;
1452 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1454 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1455 gst_pad_get_query_types_dispatcher, &result);
1461 * gst_pad_set_iterate_internal_links_function:
1462 * @pad: a #GstPad of either direction.
1463 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1465 * Sets the given internal link iterator function for the pad.
1470 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1471 GstPadIterIntLinkFunction iterintlink)
1473 g_return_if_fail (GST_IS_PAD (pad));
1475 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1476 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1477 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1481 * gst_pad_set_link_function:
1483 * @link: the #GstPadLinkFunction to set.
1485 * Sets the given link function for the pad. It will be called when
1486 * the pad is linked with another pad.
1488 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1491 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1492 * cannot be made for some reason.
1494 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1495 * of the peer sink pad, if present.
1498 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1500 g_return_if_fail (GST_IS_PAD (pad));
1502 GST_PAD_LINKFUNC (pad) = link;
1503 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1504 GST_DEBUG_FUNCPTR_NAME (link));
1508 * gst_pad_set_unlink_function:
1510 * @unlink: the #GstPadUnlinkFunction to set.
1512 * Sets the given unlink function for the pad. It will be called
1513 * when the pad is unlinked.
1516 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1518 g_return_if_fail (GST_IS_PAD (pad));
1520 GST_PAD_UNLINKFUNC (pad) = unlink;
1521 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1522 GST_DEBUG_FUNCPTR_NAME (unlink));
1526 * gst_pad_set_getcaps_function:
1528 * @getcaps: the #GstPadGetCapsFunction to set.
1530 * Sets the given getcaps function for the pad. @getcaps should return the
1531 * allowable caps for a pad in the context of the element's state, its link to
1532 * other elements, and the devices or files it has opened. These caps must be a
1533 * subset of the pad template caps. In the NULL state with no links, @getcaps
1534 * should ideally return the same caps as the pad template. In rare
1535 * circumstances, an object property can affect the caps returned by @getcaps,
1536 * but this is discouraged.
1538 * You do not need to call this function if @pad's allowed caps are always the
1539 * same as the pad template caps. This can only be true if the padtemplate
1540 * has fixed simple caps.
1542 * For most filters, the caps returned by @getcaps is directly affected by the
1543 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1544 * the srcpad's getcaps function is directly related to the stream data. Again,
1545 * @getcaps should return the most specific caps it reasonably can, since this
1546 * helps with autoplugging.
1548 * Note that the return value from @getcaps is owned by the caller, so the
1549 * caller should unref the caps after usage.
1552 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1554 g_return_if_fail (GST_IS_PAD (pad));
1556 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1557 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1558 GST_DEBUG_FUNCPTR_NAME (getcaps));
1562 * gst_pad_set_acceptcaps_function:
1564 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1566 * Sets the given acceptcaps function for the pad. The acceptcaps function
1567 * will be called to check if the pad can accept the given caps. Setting the
1568 * acceptcaps function to NULL restores the default behaviour of allowing
1569 * any caps that matches the caps from gst_pad_get_caps().
1572 gst_pad_set_acceptcaps_function (GstPad * pad,
1573 GstPadAcceptCapsFunction acceptcaps)
1575 g_return_if_fail (GST_IS_PAD (pad));
1577 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1578 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1579 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1583 * gst_pad_set_fixatecaps_function:
1585 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1587 * Sets the given fixatecaps function for the pad. The fixatecaps function
1588 * will be called whenever the default values for a GstCaps needs to be
1592 gst_pad_set_fixatecaps_function (GstPad * pad,
1593 GstPadFixateCapsFunction fixatecaps)
1595 g_return_if_fail (GST_IS_PAD (pad));
1597 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1598 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1599 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1603 * gst_pad_set_setcaps_function:
1605 * @setcaps: the #GstPadSetCapsFunction to set.
1607 * Sets the given setcaps function for the pad. The setcaps function
1608 * will be called whenever a buffer with a new media type is pushed or
1609 * pulled from the pad. The pad/element needs to update its internal
1610 * structures to process the new media type. If this new type is not
1611 * acceptable, the setcaps function should return FALSE.
1614 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1616 g_return_if_fail (GST_IS_PAD (pad));
1618 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1619 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1620 GST_DEBUG_FUNCPTR_NAME (setcaps));
1625 * @srcpad: the source #GstPad to unlink.
1626 * @sinkpad: the sink #GstPad to unlink.
1628 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1629 * signal on both pads.
1631 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1632 * the pads were not linked together.
1637 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1639 gboolean result = FALSE;
1640 GstElement *parent = NULL;
1643 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1644 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1645 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1646 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1648 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1649 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1650 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1652 /* We need to notify the parent before taking any pad locks as the bin in
1653 * question might be waiting for a lock on the pad while holding its lock
1654 * that our message will try to take. */
1655 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1656 if (GST_IS_ELEMENT (parent)) {
1657 gst_element_post_message (parent,
1658 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1659 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1661 gst_object_unref (parent);
1666 GST_OBJECT_LOCK (srcpad);
1668 GST_OBJECT_LOCK (sinkpad);
1670 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1671 goto not_linked_together;
1673 if (GST_PAD_UNLINKFUNC (srcpad)) {
1674 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1676 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1677 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1680 _priv_gst_pad_invalidate_cache (srcpad);
1682 /* first clear peers */
1683 GST_PAD_PEER (srcpad) = NULL;
1684 GST_PAD_PEER (sinkpad) = NULL;
1686 /* clear pending caps if any */
1687 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1688 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1690 GST_OBJECT_UNLOCK (sinkpad);
1691 GST_OBJECT_UNLOCK (srcpad);
1693 /* fire off a signal to each of the pads telling them
1694 * that they've been unlinked */
1695 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1696 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1698 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1699 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1704 if (parent != NULL) {
1705 gst_element_post_message (parent,
1706 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1707 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1708 gst_object_unref (parent);
1713 not_linked_together:
1715 /* we do not emit a warning in this case because unlinking cannot
1716 * be made MT safe.*/
1717 GST_OBJECT_UNLOCK (sinkpad);
1718 GST_OBJECT_UNLOCK (srcpad);
1724 * gst_pad_is_linked:
1725 * @pad: pad to check
1727 * Checks if a @pad is linked to another pad or not.
1729 * Returns: TRUE if the pad is linked, FALSE otherwise.
1734 gst_pad_is_linked (GstPad * pad)
1738 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1740 GST_OBJECT_LOCK (pad);
1741 result = (GST_PAD_PEER (pad) != NULL);
1742 GST_OBJECT_UNLOCK (pad);
1747 /* get the caps from both pads and see if the intersection
1750 * This function should be called with the pad LOCK on both
1754 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1755 GstPadLinkCheck flags)
1757 GstCaps *srccaps = NULL;
1758 GstCaps *sinkcaps = NULL;
1759 gboolean compatible = FALSE;
1761 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1764 /* Doing the expensive caps checking takes priority over only checking the template caps */
1765 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1766 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1767 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1769 /* If one of the two pads doesn't have a template, consider the intersection
1771 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1772 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1776 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1778 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1781 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1783 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1786 /* if we have caps on both pads we can check the intersection. If one
1787 * of the caps is NULL, we return TRUE. */
1788 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1790 gst_caps_unref (srccaps);
1792 gst_caps_unref (sinkcaps);
1796 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1797 gst_caps_unref (srccaps);
1798 gst_caps_unref (sinkcaps);
1801 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1802 (compatible ? "" : "not"));
1807 /* check if the grandparents of both pads are the same.
1808 * This check is required so that we don't try to link
1809 * pads from elements in different bins without ghostpads.
1811 * The LOCK should be held on both pads
1814 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1816 GstObject *psrc, *psink;
1818 psrc = GST_OBJECT_PARENT (src);
1819 psink = GST_OBJECT_PARENT (sink);
1821 /* if one of the pads has no parent, we allow the link */
1822 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1825 /* only care about parents that are elements */
1826 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1827 goto no_element_parent;
1829 /* if the parents are the same, we have a loop */
1830 if (G_UNLIKELY (psrc == psink))
1833 /* if they both have a parent, we check the grandparents. We can not lock
1834 * the parent because we hold on the child (pad) and the locking order is
1835 * parent >> child. */
1836 psrc = GST_OBJECT_PARENT (psrc);
1837 psink = GST_OBJECT_PARENT (psink);
1839 /* if they have grandparents but they are not the same */
1840 if (G_UNLIKELY (psrc != psink))
1841 goto wrong_grandparents;
1848 GST_CAT_DEBUG (GST_CAT_CAPS,
1849 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1850 GST_PTR_FORMAT, psrc, psink);
1855 GST_CAT_DEBUG (GST_CAT_CAPS,
1856 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1857 GST_PTR_FORMAT, psrc, psink);
1862 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1868 GST_CAT_DEBUG (GST_CAT_CAPS,
1869 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1870 GST_PTR_FORMAT, psrc, psink);
1875 /* FIXME leftover from an attempt at refactoring... */
1876 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1877 * the two pads will be locked in the srcpad, sinkpad order. */
1878 static GstPadLinkReturn
1879 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1881 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1882 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1884 GST_OBJECT_LOCK (srcpad);
1886 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1887 goto src_was_linked;
1889 GST_OBJECT_LOCK (sinkpad);
1891 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1892 goto sink_was_linked;
1894 /* check hierarchy, pads can only be linked if the grandparents
1896 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1897 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1898 goto wrong_hierarchy;
1900 /* check pad caps for non-empty intersection */
1901 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1904 /* FIXME check pad scheduling for non-empty intersection */
1906 return GST_PAD_LINK_OK;
1910 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1911 GST_DEBUG_PAD_NAME (srcpad),
1912 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1913 /* we do not emit a warning in this case because unlinking cannot
1914 * be made MT safe.*/
1915 GST_OBJECT_UNLOCK (srcpad);
1916 return GST_PAD_LINK_WAS_LINKED;
1920 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1921 GST_DEBUG_PAD_NAME (sinkpad),
1922 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1923 /* we do not emit a warning in this case because unlinking cannot
1924 * be made MT safe.*/
1925 GST_OBJECT_UNLOCK (sinkpad);
1926 GST_OBJECT_UNLOCK (srcpad);
1927 return GST_PAD_LINK_WAS_LINKED;
1931 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1932 GST_OBJECT_UNLOCK (sinkpad);
1933 GST_OBJECT_UNLOCK (srcpad);
1934 return GST_PAD_LINK_WRONG_HIERARCHY;
1938 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1939 GST_OBJECT_UNLOCK (sinkpad);
1940 GST_OBJECT_UNLOCK (srcpad);
1941 return GST_PAD_LINK_NOFORMAT;
1947 * @srcpad: the source #GstPad.
1948 * @sinkpad: the sink #GstPad.
1950 * Checks if the source pad and the sink pad are compatible so they can be
1953 * Returns: TRUE if the pads can be linked.
1956 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1958 GstPadLinkReturn result;
1960 /* generic checks */
1961 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1962 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1964 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1965 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1967 /* gst_pad_link_prepare does everything for us, we only release the locks
1968 * on the pads that it gets us. If this function returns !OK the locks are not
1970 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1971 if (result != GST_PAD_LINK_OK)
1974 GST_OBJECT_UNLOCK (srcpad);
1975 GST_OBJECT_UNLOCK (sinkpad);
1978 return result == GST_PAD_LINK_OK;
1982 * gst_pad_link_full:
1983 * @srcpad: the source #GstPad to link.
1984 * @sinkpad: the sink #GstPad to link.
1985 * @flags: the checks to validate when linking
1987 * Links the source pad and the sink pad.
1989 * This variant of #gst_pad_link provides a more granular control on the
1990 * checks being done when linking. While providing some considerable speedups
1991 * the caller of this method must be aware that wrong usage of those flags
1992 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1993 * for more information.
1997 * Returns: A result code indicating if the connection worked or
2003 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2005 GstPadLinkReturn result;
2010 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2011 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2012 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2013 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2014 GST_PAD_LINK_WRONG_DIRECTION);
2016 /* Notify the parent early. See gst_pad_unlink for details. */
2017 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2018 if (GST_IS_ELEMENT (parent)) {
2019 gst_element_post_message (parent,
2020 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2021 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2023 gst_object_unref (parent);
2028 /* prepare will also lock the two pads */
2029 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2031 if (result != GST_PAD_LINK_OK)
2034 /* must set peers before calling the link function */
2035 GST_PAD_PEER (srcpad) = sinkpad;
2036 GST_PAD_PEER (sinkpad) = srcpad;
2038 /* make sure we push the events from the source to this new peer, for this we
2039 * copy the events on the sinkpad and mark EVENTS_PENDING */
2041 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
2042 pending |= replace_event (srcpad, sinkpad, i);
2044 /* we had some new pending events, set our flag */
2046 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
2048 GST_OBJECT_UNLOCK (sinkpad);
2049 GST_OBJECT_UNLOCK (srcpad);
2051 /* FIXME released the locks here, concurrent thread might link
2052 * something else. */
2053 if (GST_PAD_LINKFUNC (srcpad)) {
2054 /* this one will call the peer link function */
2055 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2056 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2057 /* if no source link function, we need to call the sink link
2058 * function ourselves. */
2059 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2061 result = GST_PAD_LINK_OK;
2064 GST_OBJECT_LOCK (srcpad);
2065 GST_OBJECT_LOCK (sinkpad);
2067 if (result == GST_PAD_LINK_OK) {
2068 GST_OBJECT_UNLOCK (sinkpad);
2069 GST_OBJECT_UNLOCK (srcpad);
2071 /* fire off a signal to each of the pads telling them
2072 * that they've been linked */
2073 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2074 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2076 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2077 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2079 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2081 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2082 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2084 GST_PAD_PEER (srcpad) = NULL;
2085 GST_PAD_PEER (sinkpad) = NULL;
2087 GST_OBJECT_UNLOCK (sinkpad);
2088 GST_OBJECT_UNLOCK (srcpad);
2093 gst_element_post_message (parent,
2094 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2095 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2096 gst_object_unref (parent);
2104 * @srcpad: the source #GstPad to link.
2105 * @sinkpad: the sink #GstPad to link.
2107 * Links the source pad and the sink pad.
2109 * Returns: A result code indicating if the connection worked or
2115 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2117 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2121 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2123 GstPadTemplate **template_p;
2125 /* this function would need checks if it weren't static */
2127 GST_OBJECT_LOCK (pad);
2128 template_p = &pad->padtemplate;
2129 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2130 GST_OBJECT_UNLOCK (pad);
2133 gst_pad_template_pad_created (templ, pad);
2137 * gst_pad_get_pad_template:
2140 * Gets the template for @pad.
2142 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2143 * instantiated, or %NULL if this pad has no template. Unref after
2147 gst_pad_get_pad_template (GstPad * pad)
2149 GstPadTemplate *templ;
2151 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2153 templ = GST_PAD_PAD_TEMPLATE (pad);
2155 return (templ ? gst_object_ref (templ) : NULL);
2158 /* should be called with the pad LOCK held */
2159 /* refs the caps, so caller is responsible for getting it unreffed */
2161 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2163 GstCaps *result = NULL;
2164 GstPadTemplate *templ;
2165 gboolean fixed_caps;
2167 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2169 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2171 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2172 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2173 "dispatching to pad getcaps function with "
2174 "filter %" GST_PTR_FORMAT, filter);
2176 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2177 GST_OBJECT_UNLOCK (pad);
2178 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2179 GST_OBJECT_LOCK (pad);
2180 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2182 if (result == NULL) {
2183 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2184 GST_DEBUG_PAD_NAME (pad));
2186 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2187 "pad getcaps returned %" GST_PTR_FORMAT, result);
2188 #ifndef G_DISABLE_ASSERT
2189 /* check that the returned caps are a real subset of the template caps */
2190 if (GST_PAD_PAD_TEMPLATE (pad)) {
2191 const GstCaps *templ_caps =
2192 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2193 if (!gst_caps_is_subset (result, templ_caps)) {
2196 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2197 "pad returned caps %" GST_PTR_FORMAT
2198 " which are not a real subset of its template caps %"
2199 GST_PTR_FORMAT, result, templ_caps);
2201 ("pad %s:%s returned caps which are not a real "
2202 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2203 temp = gst_caps_intersect (templ_caps, result);
2204 gst_caps_unref (result);
2209 if (!gst_caps_is_subset (result, filter)) {
2212 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2213 "pad returned caps %" GST_PTR_FORMAT
2214 " which are not a real subset of the filter caps %"
2215 GST_PTR_FORMAT, result, filter);
2216 g_warning ("pad %s:%s returned caps which are not a real "
2217 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2218 /* FIXME: Order? But shouldn't happen anyway... */
2220 gst_caps_intersect_full (filter, result,
2221 GST_CAPS_INTERSECT_FIRST);
2222 gst_caps_unref (result);
2230 if (fixed_caps && (result = get_pad_caps (pad))) {
2232 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2233 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2234 GST_PTR_FORMAT, result, result, filter, filter);
2236 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2237 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2241 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2242 result = gst_caps_ref (result);
2246 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2247 result = GST_PAD_TEMPLATE_CAPS (templ);
2250 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2251 "using pad template %p with caps %p %" GST_PTR_FORMAT
2252 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2255 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2256 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2259 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2260 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2262 result = gst_caps_ref (result);
2267 if (!fixed_caps && (result = get_pad_caps (pad))) {
2269 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2270 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2271 GST_PTR_FORMAT, result, result, filter, filter);
2273 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2274 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2277 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2278 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2279 result = gst_caps_ref (result);
2285 /* this almost never happens */
2286 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2287 result = gst_caps_new_empty ();
2294 * gst_pad_has_current_caps:
2295 * @pad: a #GstPad to check
2297 * Check if @pad has caps set on it with gst_pad_set_caps().
2299 * Returns: TRUE when @pad has caps associated with it.
2302 gst_pad_has_current_caps (GstPad * pad)
2306 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2308 GST_OBJECT_LOCK (pad);
2309 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2310 result = (get_pad_caps (pad) != NULL);
2311 GST_OBJECT_UNLOCK (pad);
2317 * gst_pad_get_current_caps:
2318 * @pad: a #GstPad to get the current capabilities of.
2320 * Gets the capabilities currently configured on @pad with the last call to
2321 * gst_pad_set_caps().
2323 * Returns: the current caps of the pad with incremented ref-count.
2326 gst_pad_get_current_caps (GstPad * pad)
2330 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2332 GST_OBJECT_LOCK (pad);
2333 if ((result = get_pad_caps (pad)))
2334 gst_caps_ref (result);
2335 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2336 "get current pad caps %" GST_PTR_FORMAT, result);
2337 GST_OBJECT_UNLOCK (pad);
2344 * @pad: a #GstPad to get the capabilities of.
2345 * @filter: suggested #GstCaps.
2347 * Gets the capabilities this pad can produce or consume.
2348 * Note that this method doesn't necessarily return the caps set by
2349 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2350 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2351 * the pad's get_caps function;
2352 * this returns the pad template caps if not explicitly set.
2354 * When called on sinkpads @filter contains the caps that
2355 * upstream could produce in the order preferred by upstream. When
2356 * called on srcpads @filter contains the caps accepted by
2357 * downstream in the preffered order. @filter might be %NULL but
2358 * if it is not %NULL the returned caps will be a subset of @filter.
2360 * Note that this function does not return writable #GstCaps, use
2361 * gst_caps_make_writable() before modifying the caps.
2363 * Returns: the caps of the pad with incremented ref-count.
2366 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2368 GstCaps *result = NULL;
2370 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2371 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2373 GST_OBJECT_LOCK (pad);
2375 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2377 result = gst_pad_get_caps_unlocked (pad, filter);
2379 GST_OBJECT_UNLOCK (pad);
2386 * gst_pad_peer_get_caps:
2387 * @pad: a #GstPad to get the capabilities of.
2388 * @filter: a #GstCaps filter.
2390 * Gets the capabilities of the peer connected to this pad. Similar to
2391 * gst_pad_get_caps().
2393 * When called on srcpads @filter contains the caps that
2394 * upstream could produce in the order preferred by upstream. When
2395 * called on sinkpads @filter contains the caps accepted by
2396 * downstream in the preffered order. @filter might be %NULL but
2397 * if it is not %NULL the returned caps will be a subset of @filter.
2399 * Returns: the caps of the peer pad with incremented ref-count. This function
2400 * returns %NULL when there is no peer pad.
2403 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2406 GstCaps *result = NULL;
2408 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2409 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2411 GST_OBJECT_LOCK (pad);
2413 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2415 peerpad = GST_PAD_PEER (pad);
2416 if (G_UNLIKELY (peerpad == NULL))
2419 gst_object_ref (peerpad);
2420 GST_OBJECT_UNLOCK (pad);
2422 result = gst_pad_get_caps (peerpad, filter);
2424 gst_object_unref (peerpad);
2430 GST_OBJECT_UNLOCK (pad);
2436 fixate_value (GValue * dest, const GValue * src)
2438 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2439 g_value_init (dest, G_TYPE_INT);
2440 g_value_set_int (dest, gst_value_get_int_range_min (src));
2441 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2442 g_value_init (dest, G_TYPE_DOUBLE);
2443 g_value_set_double (dest, gst_value_get_double_range_min (src));
2444 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2445 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2446 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2447 GValue temp = { 0 };
2449 /* list could be empty */
2450 if (gst_value_list_get_size (src) <= 0)
2453 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2455 if (!fixate_value (dest, &temp))
2456 gst_value_init_and_copy (dest, &temp);
2457 g_value_unset (&temp);
2458 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2459 gboolean res = FALSE;
2462 len = gst_value_array_get_size (src);
2463 g_value_init (dest, GST_TYPE_ARRAY);
2464 for (n = 0; n < len; n++) {
2466 const GValue *orig_kid = gst_value_array_get_value (src, n);
2468 if (!fixate_value (&kid, orig_kid))
2469 gst_value_init_and_copy (&kid, orig_kid);
2472 gst_value_array_append_value (dest, &kid);
2473 g_value_unset (&kid);
2477 g_value_unset (dest);
2488 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2490 GstStructure *s = data;
2493 if (fixate_value (&v, value)) {
2494 gst_structure_id_set_value (s, field_id, &v);
2502 * gst_pad_fixate_caps:
2503 * @pad: a #GstPad to fixate
2504 * @caps: the #GstCaps to fixate
2506 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2507 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2510 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2512 GstPadFixateCapsFunction fixatefunc;
2515 g_return_if_fail (GST_IS_PAD (pad));
2516 g_return_if_fail (caps != NULL);
2517 g_return_if_fail (!gst_caps_is_empty (caps));
2518 g_return_if_fail (!gst_caps_is_any (caps));
2520 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2523 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2525 fixatefunc (pad, caps);
2528 /* default fixation */
2529 gst_caps_truncate (caps);
2530 s = gst_caps_get_structure (caps, 0);
2531 gst_structure_foreach (s, gst_pad_default_fixate, s);
2534 /* Default accept caps implementation just checks against
2535 * against the allowed caps for the pad */
2537 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2539 /* get the caps and see if it intersects to something not empty */
2541 gboolean result = FALSE;
2543 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2545 allowed = gst_pad_get_caps (pad, NULL);
2547 goto nothing_allowed;
2549 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2551 result = gst_caps_can_intersect (allowed, caps);
2553 gst_caps_unref (allowed);
2560 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2566 * gst_pad_accept_caps:
2567 * @pad: a #GstPad to check
2568 * @caps: a #GstCaps to check on the pad
2570 * Check if the given pad accepts the caps.
2572 * Returns: TRUE if the pad can accept the caps.
2575 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2578 GstPadAcceptCapsFunction acceptfunc;
2580 GstCaps *existing = NULL;
2583 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2585 /* any pad can be unnegotiated */
2589 /* lock for checking the existing caps */
2590 GST_OBJECT_LOCK (pad);
2591 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2593 /* The current caps on a pad are trivially acceptable */
2594 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2595 if (caps == existing || gst_caps_is_equal (caps, existing))
2599 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2600 GST_OBJECT_UNLOCK (pad);
2602 if (G_LIKELY (acceptfunc)) {
2603 /* we can call the function */
2604 result = acceptfunc (pad, caps);
2605 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2607 /* Only null if the element explicitly unset it */
2608 result = gst_pad_acceptcaps_default (pad, caps);
2609 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2616 GST_DEBUG_OBJECT (pad, "pad had same caps");
2617 GST_OBJECT_UNLOCK (pad);
2624 * gst_pad_peer_accept_caps:
2625 * @pad: a #GstPad to check the peer of
2626 * @caps: a #GstCaps to check on the pad
2628 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2631 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2634 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2639 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2641 GST_OBJECT_LOCK (pad);
2643 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2645 peerpad = GST_PAD_PEER (pad);
2646 if (G_UNLIKELY (peerpad == NULL))
2649 gst_object_ref (peerpad);
2650 /* release lock before calling external methods but keep ref to pad */
2651 GST_OBJECT_UNLOCK (pad);
2653 result = gst_pad_accept_caps (peerpad, caps);
2655 gst_object_unref (peerpad);
2661 GST_OBJECT_UNLOCK (pad);
2668 * @pad: a #GstPad to set the capabilities of.
2669 * @caps: (transfer none): a #GstCaps to set.
2671 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2672 * caps on the pad will be unreffed. This function refs the caps so you should
2673 * unref if as soon as you don't need it anymore.
2674 * It is possible to set NULL caps, which will make the pad unnegotiated
2677 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2678 * or bad parameters were provided to this function.
2683 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2686 gboolean res = TRUE;
2688 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2689 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2691 event = gst_event_new_caps (caps);
2693 if (GST_PAD_IS_SRC (pad))
2694 gst_pad_push_event (pad, event);
2696 res = gst_pad_send_event (pad, event);
2702 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2704 GstPadSetCapsFunction setcaps;
2706 GST_OBJECT_LOCK (pad);
2707 setcaps = GST_PAD_SETCAPSFUNC (pad);
2709 /* call setcaps function to configure the pad only if the
2710 * caps is not NULL */
2711 if (setcaps != NULL) {
2712 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2713 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2714 GST_OBJECT_UNLOCK (pad);
2715 if (!setcaps (pad, caps))
2716 goto setcaps_failed;
2717 GST_OBJECT_LOCK (pad);
2718 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2720 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2723 GST_OBJECT_UNLOCK (pad);
2725 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2737 do_event_function (GstPad * pad, GstEvent * event,
2738 GstPadEventFunction eventfunc)
2740 gboolean result = TRUE;
2741 GstCaps *caps, *templ;
2743 switch (GST_EVENT_TYPE (event)) {
2744 case GST_EVENT_CAPS:
2746 /* backwards compatibility mode for caps */
2747 gst_event_parse_caps (event, &caps);
2749 /* See if pad accepts the caps */
2750 templ = gst_pad_get_pad_template_caps (pad);
2751 if (!gst_caps_can_intersect (caps, templ))
2754 if (!gst_pad_call_setcaps (pad, caps))
2757 gst_caps_unref (templ);
2764 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2765 result = eventfunc (pad, event);
2772 gst_caps_unref (templ);
2773 gst_event_unref (event);
2774 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2775 "caps %" GST_PTR_FORMAT " not accepted", caps);
2780 /* function to send all pending events on the sinkpad to the event
2781 * function and collect the results. This function should be called with
2782 * the object lock. The object lock might be released by this function.
2784 static GstFlowReturn
2785 gst_pad_update_events (GstPad * pad)
2787 GstFlowReturn ret = GST_FLOW_OK;
2789 GstPadEventFunction eventfunc;
2792 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2796 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2800 ev = &pad->priv->events[i];
2802 /* skip without pending event */
2803 if ((event = ev->pending) == NULL)
2806 gst_event_ref (event);
2807 GST_OBJECT_UNLOCK (pad);
2809 res = do_event_function (pad, event, eventfunc);
2811 GST_OBJECT_LOCK (pad);
2812 /* things could have changed while we release the lock, check if we still
2813 * are handling the same event, if we don't something changed and we have
2814 * to try again. FIXME. we need a cookie here. */
2815 if (event != ev->pending) {
2816 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2820 /* remove the event from the pending entry in all cases */
2824 /* make the event active */
2826 gst_event_unref (ev->event);
2829 gst_event_unref (event);
2830 ret = GST_FLOW_ERROR;
2833 /* when we get here all events were successfully updated. */
2840 g_warning ("pad %s:%s has no event handler, file a bug.",
2841 GST_DEBUG_PAD_NAME (pad));
2842 return GST_FLOW_NOT_SUPPORTED;
2847 * gst_pad_get_pad_template_caps:
2848 * @pad: a #GstPad to get the template capabilities from.
2850 * Gets the capabilities for @pad's template.
2852 * Returns: (transfer full): the #GstCaps of this pad template.
2853 * Unref after usage.
2856 gst_pad_get_pad_template_caps (GstPad * pad)
2858 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2860 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2862 if (GST_PAD_PAD_TEMPLATE (pad))
2863 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2865 return gst_static_caps_get (&anycaps);
2870 * @pad: a #GstPad to get the peer of.
2872 * Gets the peer of @pad. This function refs the peer pad so
2873 * you need to unref it after use.
2875 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2880 gst_pad_get_peer (GstPad * pad)
2884 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2886 GST_OBJECT_LOCK (pad);
2887 result = GST_PAD_PEER (pad);
2889 gst_object_ref (result);
2890 GST_OBJECT_UNLOCK (pad);
2896 * gst_pad_get_allowed_caps:
2899 * Gets the capabilities of the allowed media types that can flow through
2900 * @pad and its peer.
2902 * The allowed capabilities is calculated as the intersection of the results of
2903 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2904 * on the resulting caps.
2906 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2907 * caps when you no longer need it. This function returns NULL when @pad
2913 gst_pad_get_allowed_caps (GstPad * pad)
2920 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2922 GST_OBJECT_LOCK (pad);
2923 peer = GST_PAD_PEER (pad);
2924 if (G_UNLIKELY (peer == NULL))
2927 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2929 gst_object_ref (peer);
2930 GST_OBJECT_UNLOCK (pad);
2931 mycaps = gst_pad_get_caps (pad, NULL);
2933 peercaps = gst_pad_get_caps (peer, NULL);
2934 gst_object_unref (peer);
2936 caps = gst_caps_intersect (mycaps, peercaps);
2937 gst_caps_unref (peercaps);
2938 gst_caps_unref (mycaps);
2940 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2947 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2948 GST_OBJECT_UNLOCK (pad);
2955 * gst_pad_get_negotiated_caps:
2958 * Gets the capabilities of the media type that currently flows through @pad
2961 * This function can be used on both src and sinkpads. Note that srcpads are
2962 * always negotiated before sinkpads so it is possible that the negotiated caps
2963 * on the srcpad do not match the negotiated caps of the peer.
2965 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2966 * the caps when you no longer need it. This function returns NULL when
2967 * the @pad has no peer or is not negotiated yet.
2972 gst_pad_get_negotiated_caps (GstPad * pad)
2974 GstCaps *caps = NULL;
2977 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2979 GST_OBJECT_LOCK (pad);
2981 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2984 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2986 if ((caps = get_pad_caps (pad)))
2987 gst_caps_ref (caps);
2989 GST_OBJECT_UNLOCK (pad);
2991 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2998 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2999 GST_OBJECT_UNLOCK (pad);
3005 * gst_pad_iterate_internal_links_default:
3006 * @pad: the #GstPad to get the internal links of.
3008 * Iterate the list of pads to which the given pad is linked to inside of
3009 * the parent element.
3010 * This is the default handler, and thus returns an iterator of all of the
3011 * pads inside the parent element with opposite direction.
3013 * The caller must free this iterator after use with gst_iterator_free().
3015 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3016 * returned pad with gst_object_unref().
3021 gst_pad_iterate_internal_links_default (GstPad * pad)
3029 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3034 GST_OBJECT_LOCK (pad);
3035 parent = GST_PAD_PARENT (pad);
3036 if (!parent || !GST_IS_ELEMENT (parent))
3039 gst_object_ref (parent);
3040 GST_OBJECT_UNLOCK (pad);
3042 if (pad->direction == GST_PAD_SRC)
3043 padlist = &parent->sinkpads;
3045 padlist = &parent->srcpads;
3047 GST_DEBUG_OBJECT (pad, "Making iterator");
3049 cookie = &parent->pads_cookie;
3051 lock = GST_OBJECT_GET_LOCK (parent);
3054 res = gst_iterator_new_list (GST_TYPE_PAD,
3055 lock, cookie, padlist, (GObject *) owner, NULL);
3057 gst_object_unref (owner);
3064 GST_OBJECT_UNLOCK (pad);
3065 GST_DEBUG_OBJECT (pad, "no parent element");
3071 * gst_pad_iterate_internal_links:
3072 * @pad: the GstPad to get the internal links of.
3074 * Gets an iterator for the pads to which the given pad is linked to inside
3075 * of the parent element.
3077 * Each #GstPad element yielded by the iterator will have its refcount increased,
3078 * so unref after use.
3080 * Free-function: gst_iterator_free
3082 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3083 * pad does not have an iterator function configured. Use
3084 * gst_iterator_free() after usage.
3089 gst_pad_iterate_internal_links (GstPad * pad)
3091 GstIterator *res = NULL;
3093 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3095 if (GST_PAD_ITERINTLINKFUNC (pad))
3096 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3102 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3104 gboolean result = FALSE;
3106 gboolean done = FALSE;
3107 GValue item = { 0, };
3109 GList *pushed_pads = NULL;
3111 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3112 event, GST_EVENT_TYPE_NAME (event));
3114 iter = gst_pad_iterate_internal_links (pad);
3120 switch (gst_iterator_next (iter, &item)) {
3121 case GST_ITERATOR_OK:
3122 eventpad = g_value_get_object (&item);
3124 /* if already pushed, skip */
3125 if (g_list_find (pushed_pads, eventpad)) {
3126 g_value_reset (&item);
3130 if (GST_PAD_IS_SRC (eventpad)) {
3131 /* for each pad we send to, we should ref the event; it's up
3132 * to downstream to unref again when handled. */
3133 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3134 event, GST_EVENT_TYPE_NAME (event),
3135 GST_DEBUG_PAD_NAME (eventpad));
3136 gst_event_ref (event);
3137 result |= gst_pad_push_event (eventpad, event);
3139 /* we only send the event on one pad, multi-sinkpad elements
3140 * should implement a handler */
3141 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3142 event, GST_EVENT_TYPE_NAME (event),
3143 GST_DEBUG_PAD_NAME (eventpad));
3144 result = gst_pad_push_event (eventpad, event);
3149 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3151 g_value_reset (&item);
3153 case GST_ITERATOR_RESYNC:
3154 /* We don't reset the result here because we don't push the event
3155 * again on pads that got the event already and because we need
3156 * to consider the result of the previous pushes */
3157 gst_iterator_resync (iter);
3159 case GST_ITERATOR_ERROR:
3160 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3163 case GST_ITERATOR_DONE:
3168 g_value_unset (&item);
3169 gst_iterator_free (iter);
3173 /* If this is a sinkpad and we don't have pads to send the event to, we
3174 * return TRUE. This is so that when using the default handler on a sink
3175 * element, we don't fail to push it. */
3177 result = GST_PAD_IS_SINK (pad);
3179 g_list_free (pushed_pads);
3181 /* we handled the incoming event so we unref once */
3183 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3184 gst_event_unref (event);
3191 * gst_pad_event_default:
3192 * @pad: a #GstPad to call the default event handler on.
3193 * @event: (transfer full): the #GstEvent to handle.
3195 * Invokes the default event handler for the given pad. End-of-stream and
3196 * discontinuity events are handled specially, and then the event is sent to all
3197 * pads internally linked to @pad. Note that if there are many possible sink
3198 * pads that are internally linked to @pad, only one will be sent an event.
3199 * Multi-sinkpad elements should implement custom event handlers.
3201 * Returns: TRUE if the event was sent succesfully.
3204 gst_pad_event_default (GstPad * pad, GstEvent * event)
3206 gboolean result = TRUE, forward = TRUE;
3208 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3209 g_return_val_if_fail (event != NULL, FALSE);
3211 GST_LOG_OBJECT (pad, "default event handler");
3213 switch (GST_EVENT_TYPE (event)) {
3216 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3217 gst_pad_pause_task (pad);
3220 case GST_EVENT_CAPS:
3222 /* don't forward by default */
3231 result = gst_pad_event_default_dispatch (pad, event);
3233 gst_event_unref (event);
3239 * gst_pad_dispatcher:
3240 * @pad: a #GstPad to dispatch.
3241 * @dispatch: the #GstPadDispatcherFunction to call.
3242 * @data: (closure): gpointer user data passed to the dispatcher function.
3244 * Invokes the given dispatcher function on each respective peer of
3245 * all pads that are internally linked to the given pad.
3246 * The GstPadDispatcherFunction should return TRUE when no further pads
3247 * need to be processed.
3249 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3252 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3255 gboolean res = FALSE;
3256 GstIterator *iter = NULL;
3257 gboolean done = FALSE;
3258 GValue item = { 0, };
3260 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3261 g_return_val_if_fail (dispatch != NULL, FALSE);
3263 iter = gst_pad_iterate_internal_links (pad);
3269 switch (gst_iterator_next (iter, &item)) {
3270 case GST_ITERATOR_OK:
3272 GstPad *int_pad = g_value_get_object (&item);
3273 GstPad *int_peer = gst_pad_get_peer (int_pad);
3276 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3277 GST_DEBUG_PAD_NAME (int_peer));
3278 done = res = dispatch (int_peer, data);
3279 gst_object_unref (int_peer);
3281 GST_DEBUG_OBJECT (int_pad, "no peer");
3283 g_value_reset (&item);
3286 case GST_ITERATOR_RESYNC:
3287 gst_iterator_resync (iter);
3289 case GST_ITERATOR_ERROR:
3291 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3293 case GST_ITERATOR_DONE:
3298 g_value_unset (&item);
3299 gst_iterator_free (iter);
3301 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3310 * @pad: a #GstPad to invoke the default query on.
3311 * @query: (transfer none): the #GstQuery to perform.
3313 * Dispatches a query to a pad. The query should have been allocated by the
3314 * caller via one of the type-specific allocation functions. The element that
3315 * the pad belongs to is responsible for filling the query with an appropriate
3316 * response, which should then be parsed with a type-specific query parsing
3319 * Again, the caller is responsible for both the allocation and deallocation of
3320 * the query structure.
3322 * Please also note that some queries might need a running pipeline to work.
3324 * Returns: TRUE if the query could be performed.
3327 gst_pad_query (GstPad * pad, GstQuery * query)
3330 GstPadQueryFunction func;
3332 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3333 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3335 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3336 GST_QUERY_TYPE_NAME (query));
3338 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3341 res = func (pad, query);
3343 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3344 GST_QUERY_TYPE_NAME (query), res);
3350 GST_DEBUG_OBJECT (pad, "had no query function");
3356 * gst_pad_peer_query:
3357 * @pad: a #GstPad to invoke the peer query on.
3358 * @query: (transfer none): the #GstQuery to perform.
3360 * Performs gst_pad_query() on the peer of @pad.
3362 * The caller is responsible for both the allocation and deallocation of
3363 * the query structure.
3365 * Returns: TRUE if the query could be performed. This function returns %FALSE
3366 * if @pad has no peer.
3371 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3376 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3377 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3379 GST_OBJECT_LOCK (pad);
3381 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3382 GST_QUERY_TYPE_NAME (query));
3384 peerpad = GST_PAD_PEER (pad);
3385 if (G_UNLIKELY (peerpad == NULL))
3388 gst_object_ref (peerpad);
3389 GST_OBJECT_UNLOCK (pad);
3391 result = gst_pad_query (peerpad, query);
3393 gst_object_unref (peerpad);
3400 GST_WARNING_OBJECT (pad, "pad has no peer");
3401 GST_OBJECT_UNLOCK (pad);
3407 * gst_pad_query_default:
3408 * @pad: a #GstPad to call the default query handler on.
3409 * @query: (transfer none): the #GstQuery to handle.
3411 * Invokes the default query handler for the given pad.
3412 * The query is sent to all pads internally linked to @pad. Note that
3413 * if there are many possible sink pads that are internally linked to
3414 * @pad, only one will be sent the query.
3415 * Multi-sinkpad elements should implement custom query handlers.
3417 * Returns: TRUE if the query was performed succesfully.
3420 gst_pad_query_default (GstPad * pad, GstQuery * query)
3422 switch (GST_QUERY_TYPE (query)) {
3423 case GST_QUERY_POSITION:
3424 case GST_QUERY_SEEKING:
3425 case GST_QUERY_FORMATS:
3426 case GST_QUERY_LATENCY:
3427 case GST_QUERY_JITTER:
3428 case GST_QUERY_RATE:
3429 case GST_QUERY_CONVERT:
3431 return gst_pad_dispatcher
3432 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3437 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3438 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3441 * This function performs the pad blocking when an event, buffer push
3442 * is performed on a _SRC_ pad. It blocks the streaming thread after
3443 * informing the pad has been blocked.
3445 * An application can with this method wait and block any streaming
3446 * thread and perform operations such as seeking or linking.
3448 * Two methods are available for notifying the application of the
3450 * - the callback method, which happens in the STREAMING thread with
3451 * the STREAM_LOCK held. With this method, the most useful way of
3452 * dealing with the callback is to post a message to the main thread
3453 * where the pad block can then be handled outside of the streaming
3454 * thread. With the last method one can perform all operations such
3455 * as doing a state change, linking, unblocking, seeking etc on the
3457 * - the GCond signal method, which makes any thread unblock when
3458 * the pad block happens.
3460 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3461 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3465 static GstFlowReturn
3466 handle_pad_block (GstPad * pad)
3468 GstPadBlockCallback callback;
3470 GstFlowReturn ret = GST_FLOW_OK;
3472 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3474 /* flushing, don't bother trying to block and return WRONG_STATE
3476 if (GST_PAD_IS_FLUSHING (pad))
3477 goto flushingnonref;
3479 /* we grab an extra ref for the callbacks, this is probably not
3480 * needed (callback code does not have a ref and cannot unref). I
3481 * think this was done to make it possible to unref the element in
3482 * the callback, which is in the end totally impossible as it
3483 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3484 * all taken when calling this function. */
3485 gst_object_ref (pad);
3487 while (GST_PAD_IS_BLOCKED (pad)) {
3489 /* we either have a callback installed to notify the block or
3490 * some other thread is doing a GCond wait. */
3491 callback = pad->block_callback;
3492 pad->block_callback_called = TRUE;
3494 /* there is a callback installed, call it. We release the
3495 * lock so that the callback can do something usefull with the
3497 user_data = pad->block_data;
3498 GST_OBJECT_UNLOCK (pad);
3499 callback (pad, TRUE, user_data);
3500 GST_OBJECT_LOCK (pad);
3502 /* we released the lock, recheck flushing */
3503 if (GST_PAD_IS_FLUSHING (pad))
3506 /* no callback, signal the thread that is doing a GCond wait
3508 GST_PAD_BLOCK_BROADCAST (pad);
3510 } while (pad->block_callback_called == FALSE && GST_PAD_IS_BLOCKED (pad));
3512 /* OBJECT_LOCK could have been released when we did the callback, which
3513 * then could have made the pad unblock so we need to check the blocking
3514 * condition again. */
3515 if (!GST_PAD_IS_BLOCKED (pad))
3518 /* now we block the streaming thread. It can be unlocked when we
3519 * deactivate the pad (which will also set the FLUSHING flag) or
3520 * when the pad is unblocked. A flushing event will also unblock
3521 * the pad after setting the FLUSHING flag. */
3522 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3523 "Waiting to be unblocked or set flushing");
3524 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3525 GST_PAD_BLOCK_WAIT (pad);
3526 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3528 /* see if we got unblocked by a flush or not */
3529 if (GST_PAD_IS_FLUSHING (pad))
3533 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3535 /* when we get here, the pad is unblocked again and we perform
3536 * the needed unblock code. */
3537 callback = pad->block_callback;
3539 /* we need to call the callback */
3540 user_data = pad->block_data;
3541 GST_OBJECT_UNLOCK (pad);
3542 callback (pad, FALSE, user_data);
3543 GST_OBJECT_LOCK (pad);
3545 /* we need to signal the thread waiting on the GCond */
3546 GST_PAD_BLOCK_BROADCAST (pad);
3549 gst_object_unref (pad);
3555 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3556 return GST_FLOW_WRONG_STATE;
3560 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3561 gst_object_unref (pad);
3562 return GST_FLOW_WRONG_STATE;
3569 * gst_pad_get_offset:
3572 * Get the offset applied to the running time of @pad. @pad has to be a source
3575 * Returns: the offset.
3578 gst_pad_get_offset (GstPad * pad)
3582 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3584 GST_OBJECT_LOCK (pad);
3585 result = pad->offset;
3586 GST_OBJECT_UNLOCK (pad);
3592 * gst_pad_set_offset:
3594 * @offset: the offset
3596 * Set the offset that will be applied to the running time of @pad.
3599 gst_pad_set_offset (GstPad * pad, gint64 offset)
3605 g_return_if_fail (GST_IS_PAD (pad));
3607 GST_OBJECT_LOCK (pad);
3608 /* if nothing changed, do nothing */
3609 if (pad->offset == offset)
3612 pad->offset = offset;
3614 /* if no peer, we just updated the offset */
3615 if ((peer = GST_PAD_PEER (pad)) == NULL)
3618 /* switch pads around when dealing with a sinkpad */
3619 if (GST_PAD_IS_SINK (pad)) {
3620 /* ref the peer so it doesn't go away when we release the lock */
3621 tmp = gst_object_ref (peer);
3622 /* make sure we get the peer (the srcpad) */
3623 GST_OBJECT_UNLOCK (pad);
3629 GST_OBJECT_LOCK (pad);
3630 /* check if the pad didn't get relinked */
3631 if (GST_PAD_PEER (pad) != peer)
3634 /* we can release the ref now */
3635 gst_object_unref (peer);
3638 /* the index of the segment event in the array */
3639 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3641 /* lock order is srcpad >> sinkpad */
3642 GST_OBJECT_LOCK (peer);
3643 /* take the current segment event, adjust it and then place
3644 * it on the sinkpad. events on the srcpad are always active. */
3645 if (replace_event (pad, peer, idx))
3646 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3648 GST_OBJECT_UNLOCK (peer);
3651 GST_OBJECT_UNLOCK (pad);
3655 /**********************************************************************
3656 * Data passing functions
3660 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3663 GValue args[2] = { {0}, {0} };
3668 g_value_init (&ret, G_TYPE_BOOLEAN);
3669 g_value_set_boolean (&ret, TRUE);
3670 g_value_init (&args[0], GST_TYPE_PAD);
3671 g_value_set_object (&args[0], pad);
3672 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3673 g_value_set_boxed (&args[1], obj);
3675 if (GST_IS_EVENT (obj))
3676 detail = event_quark;
3677 else if (GST_IS_BUFFER (obj))
3678 detail = buffer_quark;
3679 else if (GST_IS_BUFFER_LIST (obj))
3680 detail = buffer_list_quark;
3685 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3686 res = g_value_get_boolean (&ret);
3689 g_value_unset (&ret);
3690 g_value_unset (&args[0]);
3691 g_value_unset (&args[1]);
3697 gst_pad_data_unref (gboolean is_buffer, void *data)
3699 if (G_LIKELY (is_buffer)) {
3700 gst_buffer_unref (data);
3702 gst_buffer_list_unref (data);
3706 /* this is the chain function that does not perform the additional argument
3707 * checking for that little extra speed.
3709 static inline GstFlowReturn
3710 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
3711 GstPadPushCache * cache)
3713 gboolean needs_events;
3715 gboolean emit_signal;
3717 GST_PAD_STREAM_LOCK (pad);
3719 GST_OBJECT_LOCK (pad);
3720 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3723 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3724 if (G_UNLIKELY (needs_events)) {
3725 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3727 GST_DEBUG_OBJECT (pad, "need to update all events");
3728 ret = gst_pad_update_events (pad);
3729 if (G_UNLIKELY (ret != GST_FLOW_OK))
3732 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3733 GST_OBJECT_UNLOCK (pad);
3735 /* see if the signal should be emited */
3736 if (G_UNLIKELY (emit_signal)) {
3738 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3742 /* NOTE: we read the chainfunc unlocked.
3743 * we cannot hold the lock for the pad so we might send
3744 * the data to the wrong function. This is not really a
3745 * problem since functions are assigned at creation time
3746 * and don't change that often... */
3747 if (G_LIKELY (is_buffer)) {
3748 GstPadChainFunction chainfunc;
3750 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3753 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3754 "calling chainfunction &%s with buffer %p",
3755 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3758 cache->peer = gst_object_ref (pad);
3761 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3763 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3764 "called chainfunction &%s with buffer %p, returned %s",
3765 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3767 GstPadChainListFunction chainlistfunc;
3769 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3772 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3773 "calling chainlistfunction &%s",
3774 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3776 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3778 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3779 "called chainlistfunction &%s, returned %s",
3780 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3783 GST_PAD_STREAM_UNLOCK (pad);
3790 gst_pad_data_unref (is_buffer, data);
3791 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3792 "pushing, but pad was flushing");
3793 GST_OBJECT_UNLOCK (pad);
3794 GST_PAD_STREAM_UNLOCK (pad);
3795 return GST_FLOW_WRONG_STATE;
3799 gst_pad_data_unref (is_buffer, data);
3800 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3801 GST_PAD_STREAM_UNLOCK (pad);
3806 gst_pad_data_unref (is_buffer, data);
3807 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3808 GST_OBJECT_UNLOCK (pad);
3809 GST_PAD_STREAM_UNLOCK (pad);
3814 gst_pad_data_unref (is_buffer, data);
3815 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3816 "pushing, but not chainhandler");
3817 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3818 ("push on pad %s:%s but it has no chainfunction",
3819 GST_DEBUG_PAD_NAME (pad)));
3820 GST_PAD_STREAM_UNLOCK (pad);
3821 return GST_FLOW_NOT_SUPPORTED;
3827 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3828 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3831 * Chain a buffer to @pad.
3833 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3835 * If the caps on @buffer are different from the current caps on @pad, this
3836 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3837 * installed on @pad. If the new caps are not acceptable for @pad, this
3838 * function returns #GST_FLOW_NOT_NEGOTIATED.
3840 * The function proceeds calling the chain function installed on @pad (see
3841 * gst_pad_set_chain_function()) and the return value of that function is
3842 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3845 * In all cases, success or failure, the caller loses its reference to @buffer
3846 * after calling this function.
3848 * Returns: a #GstFlowReturn from the pad.
3853 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3855 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3856 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3857 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3859 return gst_pad_chain_data_unchecked (pad, TRUE, buffer, NULL);
3862 static GstFlowReturn
3863 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3869 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3871 len = gst_buffer_list_len (list);
3874 for (i = 0; i < len; i++) {
3875 buffer = gst_buffer_list_get (list, i);
3877 gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_ref (buffer), NULL);
3878 if (ret != GST_FLOW_OK)
3881 gst_buffer_list_unref (list);
3887 * gst_pad_chain_list:
3888 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3889 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3892 * Chain a bufferlist to @pad.
3894 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3896 * If the caps on the first buffer of @list are different from the current
3897 * caps on @pad, this function will call any setcaps function
3898 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3899 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3901 * The function proceeds calling the chainlist function installed on @pad (see
3902 * gst_pad_set_chain_list_function()) and the return value of that function is
3903 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3904 * chainlist function.
3906 * In all cases, success or failure, the caller loses its reference to @list
3907 * after calling this function.
3911 * Returns: a #GstFlowReturn from the pad.
3916 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3918 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3919 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3920 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3922 return gst_pad_chain_data_unchecked (pad, FALSE, list, NULL);
3925 static GstFlowReturn
3926 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
3927 GstPadPushCache * cache)
3932 GST_OBJECT_LOCK (pad);
3934 /* FIXME: this check can go away; pad_set_blocked could be implemented with
3935 * probes completely or probes with an extended pad block. */
3936 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3937 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3940 /* we emit signals on the pad arg, the peer will have a chance to
3941 * emit in the _chain() function */
3942 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
3944 /* unlock before emitting */
3945 GST_OBJECT_UNLOCK (pad);
3947 /* if the signal handler returned FALSE, it means we should just drop the
3949 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3952 GST_OBJECT_LOCK (pad);
3955 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3958 /* take ref to peer pad before releasing the lock */
3959 gst_object_ref (peer);
3960 GST_OBJECT_UNLOCK (pad);
3962 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, cache);
3964 gst_object_unref (peer);
3968 /* ERROR recovery here */
3971 gst_pad_data_unref (is_buffer, data);
3972 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
3973 GST_OBJECT_UNLOCK (pad);
3978 gst_pad_data_unref (is_buffer, data);
3979 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3984 gst_pad_data_unref (is_buffer, data);
3985 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3986 "pushing, but it was not linked");
3987 GST_OBJECT_UNLOCK (pad);
3988 return GST_FLOW_NOT_LINKED;
3992 static inline GstPadPushCache *
3993 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
3995 GstPadPushCache *cache;
3997 /* try to get the cached data */
3999 cache = g_atomic_pointer_get (cache_ptr);
4000 /* now try to replace the pointer with NULL to mark that we are busy
4002 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
4004 /* we could have a leftover invalid entry */
4005 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
4012 pad_free_cache (GstPadPushCache * cache)
4014 gst_object_unref (cache->peer);
4015 g_slice_free (GstPadPushCache, cache);
4019 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
4022 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
4023 /* something changed, clean up our cache */
4024 pad_free_cache (cache);
4028 /* must be called with the pad lock */
4030 _priv_gst_pad_invalidate_cache (GstPad * pad)
4032 GstPadPushCache *cache;
4033 gpointer *cache_ptr;
4035 GST_LOG_OBJECT (pad, "Invalidating pad cache");
4037 /* we hold the pad lock here so we can get the peer and it stays
4038 * alive during this call */
4039 if (GST_PAD_IS_SINK (pad)) {
4040 if (!(pad = GST_PAD_PEER (pad)))
4044 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4046 /* try to get the cached data */
4048 cache = g_atomic_pointer_get (cache_ptr);
4049 /* now try to replace the pointer with INVALID. If nothing is busy with this
4050 * caps, we get the cache and clean it up. If something is busy, we replace
4051 * with INVALID so that when the function finishes and tries to put the
4052 * cache back, it'll fail and cleanup */
4053 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
4054 PAD_CACHE_INVALID));
4056 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
4057 pad_free_cache (cache);
4062 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4063 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4066 * Pushes a buffer to the peer of @pad.
4068 * This function will call an installed pad block before triggering any
4069 * installed pad probes.
4071 * If the caps on @buffer are different from the currently configured caps on
4072 * @pad, this function will call any installed setcaps function on @pad (see
4073 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4074 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4076 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4077 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4080 * In all cases, success or failure, the caller loses its reference to @buffer
4081 * after calling this function.
4083 * Returns: a #GstFlowReturn from the peer pad.
4088 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4090 GstPadPushCache *cache;
4092 gpointer *cache_ptr;
4095 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4096 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4097 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4099 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4101 cache = pad_take_cache (pad, cache_ptr);
4103 if (G_UNLIKELY (cache == NULL))
4108 GST_PAD_STREAM_LOCK (peer);
4109 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4112 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4113 "calling chainfunction &%s with buffer %p",
4114 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer);
4116 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
4118 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4119 "called chainfunction &%s with buffer %p, returned %s",
4120 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer,
4121 gst_flow_get_name (ret));
4123 GST_PAD_STREAM_UNLOCK (peer);
4125 pad_put_cache (pad, cache, cache_ptr);
4132 GstPadPushCache scache = { NULL, };
4134 GST_LOG_OBJECT (pad, "Taking slow path");
4136 ret = gst_pad_push_data (pad, TRUE, buffer, &scache);
4139 GstPadPushCache *ncache;
4141 GST_LOG_OBJECT (pad, "Caching push data");
4143 /* make cache structure */
4144 ncache = g_slice_new (GstPadPushCache);
4147 pad_put_cache (pad, ncache, cache_ptr);
4153 GST_PAD_STREAM_UNLOCK (peer);
4154 pad_free_cache (cache);
4160 * gst_pad_push_list:
4161 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4162 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4165 * Pushes a buffer list to the peer of @pad.
4167 * This function will call an installed pad block before triggering any
4168 * installed pad probes.
4170 * If the caps on the first buffer in the first group of @list are different
4171 * from the currently configured caps on @pad, this function will call any
4172 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4173 * case of failure to renegotiate the new format, this function returns
4174 * #GST_FLOW_NOT_NEGOTIATED.
4176 * If there are any probes installed on @pad every group of the buffer list
4177 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4178 * buffer list will be unreffed.
4180 * The function proceeds calling the chain function on the peer pad and returns
4181 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4182 * be returned. If the peer pad does not have any installed chainlist function
4183 * every group buffer of the list will be merged into a normal #GstBuffer and
4184 * chained via gst_pad_chain().
4186 * In all cases, success or failure, the caller loses its reference to @list
4187 * after calling this function.
4189 * Returns: a #GstFlowReturn from the peer pad.
4196 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4198 GstPadPushCache *cache;
4200 gpointer *cache_ptr;
4203 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4204 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4205 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4207 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4209 cache = pad_take_cache (pad, cache_ptr);
4211 if (G_UNLIKELY (cache == NULL))
4216 GST_PAD_STREAM_LOCK (peer);
4217 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4220 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
4222 GST_PAD_STREAM_UNLOCK (peer);
4224 pad_put_cache (pad, cache, cache_ptr);
4231 GstPadPushCache scache = { NULL, };
4233 GST_LOG_OBJECT (pad, "Taking slow path");
4235 ret = gst_pad_push_data (pad, FALSE, list, &scache);
4238 GstPadPushCache *ncache;
4240 GST_LOG_OBJECT (pad, "Caching push data");
4242 /* make cache structure */
4243 ncache = g_slice_new (GstPadPushCache);
4246 pad_put_cache (pad, ncache, cache_ptr);
4252 GST_PAD_STREAM_UNLOCK (peer);
4253 pad_free_cache (cache);
4258 static GstFlowReturn
4259 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4260 GstBuffer ** buffer)
4263 GstPadGetRangeFunction getrangefunc;
4264 gboolean emit_signal;
4266 GST_PAD_STREAM_LOCK (pad);
4268 GST_OBJECT_LOCK (pad);
4269 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4272 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4273 GST_OBJECT_UNLOCK (pad);
4275 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4278 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4279 "calling getrangefunc %s, offset %"
4280 G_GUINT64_FORMAT ", size %u",
4281 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4283 ret = getrangefunc (pad, offset, size, buffer);
4285 /* can only fire the signal if we have a valid buffer */
4286 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4287 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4290 GST_PAD_STREAM_UNLOCK (pad);
4292 if (G_UNLIKELY (ret != GST_FLOW_OK))
4293 goto get_range_failed;
4300 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4301 "pulling range, but pad was flushing");
4302 GST_OBJECT_UNLOCK (pad);
4303 GST_PAD_STREAM_UNLOCK (pad);
4304 return GST_FLOW_WRONG_STATE;
4308 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4309 ("pullrange on pad %s:%s but it has no getrangefunction",
4310 GST_DEBUG_PAD_NAME (pad)));
4311 GST_PAD_STREAM_UNLOCK (pad);
4312 return GST_FLOW_NOT_SUPPORTED;
4316 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4317 "Dropping data after FALSE probe return");
4318 GST_PAD_STREAM_UNLOCK (pad);
4319 gst_buffer_unref (*buffer);
4321 return GST_FLOW_UNEXPECTED;
4326 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4327 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4328 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4334 * gst_pad_get_range:
4335 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4336 * @offset: The start offset of the buffer
4337 * @size: The length of the buffer
4338 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4339 * returns #GST_FLOW_ERROR if %NULL.
4341 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4342 * immediatly and @buffer is %NULL.
4344 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4345 * description of a getrange function. If @pad has no getrange function
4346 * installed (see gst_pad_set_getrange_function()) this function returns
4347 * #GST_FLOW_NOT_SUPPORTED.
4349 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4351 * Returns: a #GstFlowReturn from the pad.
4356 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4357 GstBuffer ** buffer)
4359 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4360 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4361 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4363 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4367 * gst_pad_pull_range:
4368 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4369 * @offset: The start offset of the buffer
4370 * @size: The length of the buffer
4371 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4372 * GST_FLOW_ERROR if %NULL.
4374 * Pulls a @buffer from the peer pad.
4376 * This function will first trigger the pad block signal if it was
4379 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4380 * function returns the result of gst_pad_get_range() on the peer pad.
4381 * See gst_pad_get_range() for a list of return values and for the
4382 * semantics of the arguments of this function.
4384 * @buffer's caps must either be unset or the same as what is already
4385 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4388 * Returns: a #GstFlowReturn from the peer pad.
4389 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4390 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4391 * @buffer may not be used or freed when any other return value than
4392 * #GST_FLOW_OK is returned.
4397 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4398 GstBuffer ** buffer)
4402 gboolean emit_signal;
4403 gboolean needs_events;
4405 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4406 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4407 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4409 GST_OBJECT_LOCK (pad);
4411 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4412 handle_pad_block (pad);
4414 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4417 /* signal emision for the pad, peer has chance to emit when
4418 * we call _get_range() */
4419 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4421 gst_object_ref (peer);
4422 GST_OBJECT_UNLOCK (pad);
4424 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4426 gst_object_unref (peer);
4428 if (G_UNLIKELY (ret != GST_FLOW_OK))
4429 goto pull_range_failed;
4431 /* can only fire the signal if we have a valid buffer */
4432 if (G_UNLIKELY (emit_signal)) {
4433 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4437 GST_OBJECT_LOCK (pad);
4439 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4440 if (G_UNLIKELY (needs_events)) {
4441 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4443 GST_DEBUG_OBJECT (pad, "we need to update the events");
4444 ret = gst_pad_update_events (pad);
4445 if (G_UNLIKELY (ret != GST_FLOW_OK))
4448 GST_OBJECT_UNLOCK (pad);
4452 /* ERROR recovery here */
4455 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4456 "pulling range, but it was not linked");
4457 GST_OBJECT_UNLOCK (pad);
4458 return GST_FLOW_NOT_LINKED;
4463 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4464 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4465 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4470 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4471 "Dropping data after FALSE probe return");
4472 gst_buffer_unref (*buffer);
4474 return GST_FLOW_UNEXPECTED;
4478 GST_OBJECT_UNLOCK (pad);
4479 gst_buffer_unref (*buffer);
4481 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4482 "pullrange returned events that were not accepted");
4488 * gst_pad_push_event:
4489 * @pad: a #GstPad to push the event to.
4490 * @event: (transfer full): the #GstEvent to send to the pad.
4492 * Sends the event to the peer of the given pad. This function is
4493 * mainly used by elements to send events to their peer
4496 * This function takes owership of the provided event so you should
4497 * gst_event_ref() it if you want to reuse the event after this call.
4499 * Returns: TRUE if the event was handled.
4504 gst_pad_push_event (GstPad * pad, GstEvent * event)
4510 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4511 g_return_val_if_fail (event != NULL, FALSE);
4512 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4514 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4516 GST_OBJECT_LOCK (pad);
4518 /* Two checks to be made:
4519 * . (un)set the FLUSHING flag for flushing events,
4520 * . handle pad blocking */
4521 switch (GST_EVENT_TYPE (event)) {
4522 case GST_EVENT_FLUSH_START:
4523 _priv_gst_pad_invalidate_cache (pad);
4524 GST_PAD_SET_FLUSHING (pad);
4526 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4527 /* flush start will have set the FLUSHING flag and will then
4528 * unlock all threads doing a GCond wait on the blocking pad. This
4529 * will typically unblock the STREAMING thread blocked on a pad. */
4530 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4531 "doing block signal.");
4532 GST_PAD_BLOCK_BROADCAST (pad);
4536 case GST_EVENT_FLUSH_STOP:
4537 GST_PAD_UNSET_FLUSHING (pad);
4539 /* if we are blocked, flush away the FLUSH_STOP event */
4540 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4541 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4545 case GST_EVENT_RECONFIGURE:
4546 if (GST_PAD_IS_SINK (pad))
4547 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4549 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4550 /* block the event as long as the pad is blocked */
4551 if (handle_pad_block (pad) != GST_FLOW_OK)
4557 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4558 GST_OBJECT_UNLOCK (pad);
4560 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4563 GST_OBJECT_LOCK (pad);
4566 /* store the event on the pad, but only on srcpads */
4567 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4570 idx = GST_EVENT_STICKY_IDX (event);
4571 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4572 GST_EVENT_TYPE_NAME (event), idx);
4574 /* srcpad sticky events always become active immediately */
4575 gst_event_replace (&pad->priv->events[idx].event, event);
4578 if ((peerpad = GST_PAD_PEER (pad)))
4579 gst_object_ref (peerpad);
4581 offset = pad->offset;
4582 GST_OBJECT_UNLOCK (pad);
4584 /* backwards compatibility mode for caps */
4585 switch (GST_EVENT_TYPE (event)) {
4586 case GST_EVENT_CAPS:
4590 gst_event_parse_caps (event, &caps);
4591 /* FIXME, this is awkward because we don't check flushing here which means
4592 * that we can call the setcaps functions on flushing pads, this is not
4593 * quite what we want, otoh, this code should just go away and elements
4594 * that set caps on their sinkpad should just setup stuff themselves. */
4595 gst_pad_call_setcaps (pad, caps);
4598 case GST_EVENT_SEGMENT:
4599 /* check if we need to adjust the segment */
4600 if (offset != 0 && peerpad != NULL) {
4603 /* copy segment values */
4604 gst_event_copy_segment (event, &segment);
4605 gst_event_unref (event);
4607 /* adjust and make a new event with the offset applied */
4608 segment.base += offset;
4609 event = gst_event_new_segment (&segment);
4616 /* now check the peer pad */
4617 if (peerpad == NULL)
4620 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4621 GST_EVENT_TYPE_NAME (event), peerpad);
4623 result = gst_pad_send_event (peerpad, event);
4625 /* Note: we gave away ownership of the event at this point */
4626 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4629 gst_object_unref (peerpad);
4633 /* ERROR handling */
4636 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4637 gst_event_unref (event);
4642 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4643 gst_event_unref (event);
4648 GST_DEBUG_OBJECT (pad,
4649 "Not forwarding event since we're flushing and blocking");
4650 gst_event_unref (event);
4651 GST_OBJECT_UNLOCK (pad);
4657 * gst_pad_send_event:
4658 * @pad: a #GstPad to send the event to.
4659 * @event: (transfer full): the #GstEvent to send to the pad.
4661 * Sends the event to the pad. This function can be used
4662 * by applications to send events in the pipeline.
4664 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4665 * sink pad, @event should be a downstream event. For example, you would not
4666 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4667 * Furthermore, some downstream events have to be serialized with data flow,
4668 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4669 * the event needs to be serialized with data flow, this function will take the
4670 * pad's stream lock while calling its event function.
4672 * To find out whether an event type is upstream, downstream, or downstream and
4673 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4674 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4675 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4676 * plugin doesn't need to bother itself with this information; the core handles
4677 * all necessary locks and checks.
4679 * This function takes owership of the provided event so you should
4680 * gst_event_ref() it if you want to reuse the event after this call.
4682 * Returns: TRUE if the event was handled.
4685 gst_pad_send_event (GstPad * pad, GstEvent * event)
4687 gboolean result = FALSE;
4688 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4690 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4691 g_return_val_if_fail (event != NULL, FALSE);
4693 GST_OBJECT_LOCK (pad);
4694 if (GST_PAD_IS_SINK (pad)) {
4695 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4696 goto wrong_direction;
4697 serialized = GST_EVENT_IS_SERIALIZED (event);
4698 sticky = GST_EVENT_IS_STICKY (event);
4699 } else if (GST_PAD_IS_SRC (pad)) {
4700 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4701 goto wrong_direction;
4702 /* events on srcpad never are serialized and sticky */
4703 serialized = sticky = FALSE;
4705 goto unknown_direction;
4708 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4709 GST_OBJECT_UNLOCK (pad);
4711 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4714 GST_OBJECT_LOCK (pad);
4716 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4718 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4720 switch (GST_EVENT_TYPE (event)) {
4721 case GST_EVENT_FLUSH_START:
4722 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4723 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4725 /* can't even accept a flush begin event when flushing */
4726 if (GST_PAD_IS_FLUSHING (pad))
4729 _priv_gst_pad_invalidate_cache (pad);
4730 GST_PAD_SET_FLUSHING (pad);
4731 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4732 needs_events = FALSE;
4734 case GST_EVENT_FLUSH_STOP:
4735 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4736 GST_PAD_UNSET_FLUSHING (pad);
4737 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4739 GST_OBJECT_UNLOCK (pad);
4740 /* grab stream lock */
4741 GST_PAD_STREAM_LOCK (pad);
4743 GST_OBJECT_LOCK (pad);
4744 needs_events = FALSE;
4746 case GST_EVENT_RECONFIGURE:
4747 if (GST_PAD_IS_SRC (pad))
4748 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4750 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4751 GST_EVENT_TYPE_NAME (event));
4754 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4755 GST_OBJECT_UNLOCK (pad);
4756 GST_PAD_STREAM_LOCK (pad);
4758 GST_OBJECT_LOCK (pad);
4760 /* don't forward events on non-serialized events */
4761 needs_events = FALSE;
4764 /* store the event on the pad, but only on srcpads. We need to store the
4765 * event before checking the flushing flag. */
4770 switch (GST_EVENT_TYPE (event)) {
4771 case GST_EVENT_SEGMENT:
4772 if (pad->offset != 0) {
4775 /* copy segment values */
4776 gst_event_copy_segment (event, &segment);
4777 gst_event_unref (event);
4779 /* adjust and make a new event with the offset applied */
4780 segment.base += pad->offset;
4781 event = gst_event_new_segment (&segment);
4788 idx = GST_EVENT_STICKY_IDX (event);
4789 ev = &pad->priv->events[idx];
4791 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4792 GST_EVENT_TYPE_NAME (event), idx);
4794 if (ev->event != event) {
4795 gst_event_replace (&ev->pending, event);
4796 /* set the flag so that we update the events next time. We would
4797 * usually update below but we might be flushing too. */
4798 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4799 needs_events = TRUE;
4802 /* now check the flushing flag */
4803 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4809 if (G_UNLIKELY (needs_events)) {
4812 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4814 GST_DEBUG_OBJECT (pad, "need to update all events");
4815 ret = gst_pad_update_events (pad);
4816 if (ret != GST_FLOW_OK)
4818 GST_OBJECT_UNLOCK (pad);
4820 gst_event_unref (event);
4824 GstPadEventFunction eventfunc;
4826 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4829 GST_OBJECT_UNLOCK (pad);
4831 result = do_event_function (pad, event, eventfunc);
4835 GST_PAD_STREAM_UNLOCK (pad);
4837 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4841 /* ERROR handling */
4844 g_warning ("pad %s:%s sending %s event in wrong direction",
4845 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4846 GST_OBJECT_UNLOCK (pad);
4847 gst_event_unref (event);
4852 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4853 GST_OBJECT_UNLOCK (pad);
4854 gst_event_unref (event);
4859 g_warning ("pad %s:%s has no event handler, file a bug.",
4860 GST_DEBUG_PAD_NAME (pad));
4861 GST_OBJECT_UNLOCK (pad);
4863 GST_PAD_STREAM_UNLOCK (pad);
4864 gst_event_unref (event);
4869 GST_OBJECT_UNLOCK (pad);
4871 GST_PAD_STREAM_UNLOCK (pad);
4872 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4873 "Received event on flushing pad. Discarding");
4874 gst_event_unref (event);
4879 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4880 gst_event_unref (event);
4885 GST_OBJECT_UNLOCK (pad);
4887 GST_PAD_STREAM_UNLOCK (pad);
4888 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4889 gst_event_unref (event);
4895 * gst_pad_set_element_private:
4896 * @pad: the #GstPad to set the private data of.
4897 * @priv: The private data to attach to the pad.
4899 * Set the given private data gpointer on the pad.
4900 * This function can only be used by the element that owns the pad.
4901 * No locking is performed in this function.
4904 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4906 pad->element_private = priv;
4910 * gst_pad_get_element_private:
4911 * @pad: the #GstPad to get the private data of.
4913 * Gets the private data of a pad.
4914 * No locking is performed in this function.
4916 * Returns: (transfer none): a #gpointer to the private data.
4919 gst_pad_get_element_private (GstPad * pad)
4921 return pad->element_private;
4925 * gst_pad_get_sticky_event:
4926 * @pad: the #GstPad to get the event from.
4927 * @event_type: the #GstEventType that should be retrieved.
4929 * Returns a new reference of the sticky event of type @event_type
4932 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4935 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4937 GstEvent *event = NULL;
4940 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4941 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4943 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4945 GST_OBJECT_LOCK (pad);
4946 if ((event = pad->priv->events[idx].event)) {
4947 gst_event_ref (event);
4949 GST_OBJECT_UNLOCK (pad);
4955 * gst_pad_sticky_events_foreach:
4956 * @pad: the #GstPad that should be used for iteration.
4957 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4958 * @user_data: (closure): the optional user data.
4960 * Iterates all active sticky events on @pad and calls @foreach_func for every
4961 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4962 * is immediately stopped.
4964 * Returns: GST_FLOW_OK if iteration was successful
4967 gst_pad_sticky_events_foreach (GstPad * pad,
4968 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4970 GstFlowReturn ret = GST_FLOW_OK;
4974 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4975 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4977 GST_OBJECT_LOCK (pad);
4980 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4984 ev = &pad->priv->events[i];
4986 /* skip without active event */
4987 if ((event = ev->event) == NULL)
4990 gst_event_ref (event);
4991 GST_OBJECT_UNLOCK (pad);
4993 res = foreach_func (pad, event, user_data);
4995 GST_OBJECT_LOCK (pad);
4996 gst_event_unref (event);
4998 if (res != GST_FLOW_OK) {
5003 /* things could have changed while we release the lock, check if we still
5004 * are handling the same event, if we don't something changed and we have
5005 * to try again. FIXME. we need a cookie here. */
5006 if (event != ev->event) {
5007 GST_DEBUG_OBJECT (pad, "events changed, restarting");
5011 GST_OBJECT_UNLOCK (pad);
5017 do_stream_status (GstPad * pad, GstStreamStatusType type,
5018 GThread * thread, GstTask * task)
5022 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5024 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5025 if (GST_IS_ELEMENT (parent)) {
5026 GstMessage *message;
5027 GValue value = { 0 };
5029 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5030 gchar *tname, *ename, *pname;
5032 /* create a good task name */
5033 ename = gst_element_get_name (parent);
5034 pname = gst_pad_get_name (pad);
5035 tname = g_strdup_printf ("%s:%s", ename, pname);
5039 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5043 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5046 g_value_init (&value, GST_TYPE_TASK);
5047 g_value_set_object (&value, task);
5048 gst_message_set_stream_status_object (message, &value);
5049 g_value_unset (&value);
5051 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5052 gst_element_post_message (parent, message);
5054 gst_object_unref (parent);
5059 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5061 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5066 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5068 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5072 static GstTaskThreadCallbacks thr_callbacks = {
5078 * gst_pad_start_task:
5079 * @pad: the #GstPad to start the task of
5080 * @func: the task function to call
5081 * @data: data passed to the task function
5083 * Starts a task that repeatedly calls @func with @data. This function
5084 * is mostly used in pad activation functions to start the dataflow.
5085 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5086 * before @func is called.
5088 * Returns: a %TRUE if the task could be started.
5091 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5096 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5097 g_return_val_if_fail (func != NULL, FALSE);
5099 GST_DEBUG_OBJECT (pad, "start task");
5101 GST_OBJECT_LOCK (pad);
5102 task = GST_PAD_TASK (pad);
5104 task = gst_task_create (func, data);
5105 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5106 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5107 GST_DEBUG_OBJECT (pad, "created task");
5108 GST_PAD_TASK (pad) = task;
5109 gst_object_ref (task);
5110 /* release lock to post the message */
5111 GST_OBJECT_UNLOCK (pad);
5113 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5115 gst_object_unref (task);
5117 GST_OBJECT_LOCK (pad);
5118 /* nobody else is supposed to have changed the pad now */
5119 if (GST_PAD_TASK (pad) != task)
5120 goto concurrent_stop;
5122 res = gst_task_set_state (task, GST_TASK_STARTED);
5123 GST_OBJECT_UNLOCK (pad);
5130 GST_OBJECT_UNLOCK (pad);
5136 * gst_pad_pause_task:
5137 * @pad: the #GstPad to pause the task of
5139 * Pause the task of @pad. This function will also wait until the
5140 * function executed by the task is finished if this function is not
5141 * called from the task function.
5143 * Returns: a TRUE if the task could be paused or FALSE when the pad
5147 gst_pad_pause_task (GstPad * pad)
5152 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5154 GST_DEBUG_OBJECT (pad, "pause task");
5156 GST_OBJECT_LOCK (pad);
5157 task = GST_PAD_TASK (pad);
5160 res = gst_task_set_state (task, GST_TASK_PAUSED);
5161 GST_OBJECT_UNLOCK (pad);
5163 /* wait for task function to finish, this lock is recursive so it does nothing
5164 * when the pause is called from the task itself */
5165 GST_PAD_STREAM_LOCK (pad);
5166 GST_PAD_STREAM_UNLOCK (pad);
5172 GST_DEBUG_OBJECT (pad, "pad has no task");
5173 GST_OBJECT_UNLOCK (pad);
5179 * gst_pad_stop_task:
5180 * @pad: the #GstPad to stop the task of
5182 * Stop the task of @pad. This function will also make sure that the
5183 * function executed by the task will effectively stop if not called
5184 * from the GstTaskFunction.
5186 * This function will deadlock if called from the GstTaskFunction of
5187 * the task. Use gst_task_pause() instead.
5189 * Regardless of whether the pad has a task, the stream lock is acquired and
5190 * released so as to ensure that streaming through this pad has finished.
5192 * Returns: a TRUE if the task could be stopped or FALSE on error.
5195 gst_pad_stop_task (GstPad * pad)
5200 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5202 GST_DEBUG_OBJECT (pad, "stop task");
5204 GST_OBJECT_LOCK (pad);
5205 task = GST_PAD_TASK (pad);
5208 GST_PAD_TASK (pad) = NULL;
5209 res = gst_task_set_state (task, GST_TASK_STOPPED);
5210 GST_OBJECT_UNLOCK (pad);
5212 GST_PAD_STREAM_LOCK (pad);
5213 GST_PAD_STREAM_UNLOCK (pad);
5215 if (!gst_task_join (task))
5218 gst_object_unref (task);
5224 GST_DEBUG_OBJECT (pad, "no task");
5225 GST_OBJECT_UNLOCK (pad);
5227 GST_PAD_STREAM_LOCK (pad);
5228 GST_PAD_STREAM_UNLOCK (pad);
5230 /* this is not an error */
5235 /* this is bad, possibly the application tried to join the task from
5236 * the task's thread. We install the task again so that it will be stopped
5237 * again from the right thread next time hopefully. */
5238 GST_OBJECT_LOCK (pad);
5239 GST_DEBUG_OBJECT (pad, "join failed");
5240 /* we can only install this task if there was no other task */
5241 if (GST_PAD_TASK (pad) == NULL)
5242 GST_PAD_TASK (pad) = task;
5243 GST_OBJECT_UNLOCK (pad);