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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
31 * Pads have a #GstPadDirection, source pads produce data, sink pads consume
34 * Pads are typically created from a #GstPadTemplate with
35 * gst_pad_new_from_template() and are then added to a #GstElement. This usually
36 * happens when the element is created but it can also happen dynamically based
37 * on the data that the element is processing or based on the pads that the
38 * application requests.
40 * Pads without pad templates can be created with gst_pad_new(),
41 * which takes a direction and a name as an argument. If the name is %NULL,
42 * then a guaranteed unique name will be assigned to it.
44 * A #GstElement creating a pad will typically use the various
45 * gst_pad_set_*_function() calls to register callbacks for events, queries or
46 * dataflow on the pads.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * After two pads are retrieved from an element by gst_element_get_static_pad(),
51 * the pads can be linked with gst_pad_link(). (For quick links,
52 * you can also use gst_element_link(), which will make the obvious
53 * link for you if it's straightforward.). Pads can be unlinked again with
54 * gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
57 * Before dataflow is possible on the pads, they need to be activated with
58 * gst_pad_set_active().
60 * gst_pad_query() and gst_pad_peer_query() can be used to query various
61 * properties of the pad and the stream.
63 * To send a #GstEvent on a pad, use gst_pad_send_event() and
64 * gst_pad_push_event(). Some events will be sticky on the pad, meaning that
65 * after they pass on the pad they can be queried later with
66 * gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
67 * gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
68 * functions to query the current sticky CAPS event on a pad.
70 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
71 * or pull in a buffer.
73 * The dataflow, events and queries that happen on a pad can be monitored with
74 * probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
75 * can be used to check if a block probe is installed on the pad.
76 * gst_pad_is_blocking() checks if the blocking probe is currently blocking the
77 * pad. gst_pad_remove_probe() is used to remove a previously installed probe
78 * and unblock blocking probes if any.
80 * Pad have an offset that can be retrieved with gst_pad_get_offset(). This
81 * offset will be applied to the running_time of all data passing over the pad.
82 * gst_pad_set_offset() can be used to change the offset.
84 * Convenience functions exist to start, pause and stop the task on a pad with
85 * gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
89 #include "gst_private.h"
92 #include "gstpadtemplate.h"
93 #include "gstenumtypes.h"
98 #include "glib-compat-private.h"
100 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
101 #define GST_CAT_DEFAULT GST_CAT_PADS
103 /* Pad signals and args */
122 #define GST_PAD_GET_PRIVATE(obj) \
123 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
125 /* we have a pending and an active event on the pad. On source pads only the
126 * active event is used. On sinkpads, events are copied to the pending entry and
127 * moved to the active event when the eventfunc returned %TRUE. */
134 struct _GstPadPrivate
141 guint probe_list_cookie;
151 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
156 GstPadProbeInfo *info;
163 static void gst_pad_dispose (GObject * object);
164 static void gst_pad_finalize (GObject * object);
165 static void gst_pad_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec);
167 static void gst_pad_get_property (GObject * object, guint prop_id,
168 GValue * value, GParamSpec * pspec);
170 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
171 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
172 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
173 GstObject * parent, GstBufferList * list);
175 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
176 GstEvent * event, GstPadProbeType type);
177 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
178 GstEvent * event, GstPadProbeType type);
180 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
182 static GParamSpec *pspec_caps = NULL;
184 /* quarks for probe signals */
185 static GQuark buffer_quark;
186 static GQuark buffer_list_quark;
187 static GQuark event_quark;
196 static GstFlowQuarks flow_quarks[] = {
197 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
198 {GST_FLOW_OK, "ok", 0},
199 {GST_FLOW_NOT_LINKED, "not-linked", 0},
200 {GST_FLOW_FLUSHING, "flushing", 0},
201 {GST_FLOW_EOS, "eos", 0},
202 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
203 {GST_FLOW_ERROR, "error", 0},
204 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
205 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
210 * @ret: a #GstFlowReturn to get the name of.
212 * Gets a string representing the given flow return.
214 * Returns: a static string with the name of the flow return.
217 gst_flow_get_name (GstFlowReturn ret)
221 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
223 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
224 if (ret == flow_quarks[i].ret)
225 return flow_quarks[i].name;
232 * @ret: a #GstFlowReturn to get the quark of.
234 * Get the unique quark for the given GstFlowReturn.
236 * Returns: the quark associated with the flow return or 0 if an
237 * invalid return was specified.
240 gst_flow_to_quark (GstFlowReturn ret)
244 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
246 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
247 if (ret == flow_quarks[i].ret)
248 return flow_quarks[i].quark;
254 * gst_pad_link_get_name:
255 * @ret: a #GstPadLinkReturn to get the name of.
257 * Gets a string representing the given pad-link return.
259 * Returns: a static string with the name of the pad-link return.
264 gst_pad_link_get_name (GstPadLinkReturn ret)
267 case GST_PAD_LINK_OK:
269 case GST_PAD_LINK_WRONG_HIERARCHY:
270 return "wrong hierarchy";
271 case GST_PAD_LINK_WAS_LINKED:
273 case GST_PAD_LINK_WRONG_DIRECTION:
274 return "wrong direction";
275 case GST_PAD_LINK_NOFORMAT:
276 return "no common format";
277 case GST_PAD_LINK_NOSCHED:
278 return "incompatible scheduling";
279 case GST_PAD_LINK_REFUSED:
282 g_return_val_if_reached ("unknown");
289 buffer_quark = g_quark_from_static_string ("buffer"); \
290 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
291 event_quark = g_quark_from_static_string ("event"); \
293 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
294 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
297 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
298 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
301 #define gst_pad_parent_class parent_class
302 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
305 gst_pad_class_init (GstPadClass * klass)
307 GObjectClass *gobject_class;
308 GstObjectClass *gstobject_class;
310 gobject_class = G_OBJECT_CLASS (klass);
311 gstobject_class = GST_OBJECT_CLASS (klass);
313 g_type_class_add_private (klass, sizeof (GstPadPrivate));
315 gobject_class->dispose = gst_pad_dispose;
316 gobject_class->finalize = gst_pad_finalize;
317 gobject_class->set_property = gst_pad_set_property;
318 gobject_class->get_property = gst_pad_get_property;
322 * @pad: the pad that emitted the signal
323 * @peer: the peer pad that has been connected
325 * Signals that a pad has been linked to the peer pad.
327 gst_pad_signals[PAD_LINKED] =
328 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
329 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
330 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
333 * @pad: the pad that emitted the signal
334 * @peer: the peer pad that has been disconnected
336 * Signals that a pad has been unlinked from the peer pad.
338 gst_pad_signals[PAD_UNLINKED] =
339 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
340 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
341 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
343 pspec_caps = g_param_spec_boxed ("caps", "Caps",
344 "The capabilities of the pad", GST_TYPE_CAPS,
345 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
346 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
348 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
349 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
350 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
351 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
353 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
354 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
355 g_param_spec_object ("template", "Template",
356 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 * The offset that will be applied to the running time of the pad.
366 g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
367 g_param_spec_int64 ("offset", "Offset",
368 "The running time offset of the pad", 0, G_MAXINT64, 0,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 gstobject_class->path_string_separator = ".";
373 /* Register common function pointer descriptions */
374 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
375 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
376 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
377 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
378 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
382 gst_pad_init (GstPad * pad)
384 pad->priv = GST_PAD_GET_PRIVATE (pad);
386 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
388 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
389 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
390 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
391 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
392 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
394 GST_PAD_SET_FLUSHING (pad);
396 g_rec_mutex_init (&pad->stream_rec_lock);
398 g_cond_init (&pad->block_cond);
400 g_hook_list_init (&pad->probes, sizeof (GstProbe));
402 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
403 pad->priv->events_cookie = 0;
404 pad->priv->last_cookie = -1;
405 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
408 /* called when setting the pad inactive. It removes all sticky events from
409 * the pad. must be called with object lock */
411 remove_events (GstPad * pad)
415 gboolean notify = FALSE;
417 events = pad->priv->events;
420 for (i = 0; i < len; i++) {
421 PadEvent *ev = &g_array_index (events, PadEvent, i);
422 GstEvent *event = ev->event;
426 if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
429 gst_event_unref (event);
432 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
433 g_array_set_size (events, 0);
434 pad->priv->events_cookie++;
437 GST_OBJECT_UNLOCK (pad);
439 GST_DEBUG_OBJECT (pad, "notify caps");
440 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
442 GST_OBJECT_LOCK (pad);
446 /* should be called with object lock */
448 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
454 events = pad->priv->events;
457 for (i = 0; i < len; i++) {
458 ev = &g_array_index (events, PadEvent, i);
459 if (ev->event == NULL)
462 if (GST_EVENT_TYPE (ev->event) == type) {
473 /* should be called with OBJECT lock */
475 find_event (GstPad * pad, GstEvent * event)
481 events = pad->priv->events;
484 for (i = 0; i < len; i++) {
485 ev = &g_array_index (events, PadEvent, i);
486 if (event == ev->event)
494 /* should be called with OBJECT lock */
496 remove_event_by_type (GstPad * pad, GstEventType type)
502 events = pad->priv->events;
507 ev = &g_array_index (events, PadEvent, i);
508 if (ev->event == NULL)
511 if (GST_EVENT_TYPE (ev->event) != type)
514 gst_event_unref (ev->event);
515 g_array_remove_index (events, i);
517 pad->priv->events_cookie++;
525 /* check all events on srcpad against those on sinkpad. All events that are not
526 * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
527 * srcpad so that the events will be sent next time */
528 /* should be called with srcpad and sinkpad LOCKS */
530 schedule_events (GstPad * srcpad, GstPad * sinkpad)
535 gboolean pending = FALSE;
537 events = srcpad->priv->events;
540 for (i = 0; i < len; i++) {
541 ev = &g_array_index (events, PadEvent, i);
542 if (ev->event == NULL)
545 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
546 ev->received = FALSE;
551 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
554 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
557 /* should be called with pad LOCK */
559 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
566 events = pad->priv->events;
569 cookie = pad->priv->events_cookie;
573 PadEvent *ev, ev_ret;
575 ev = &g_array_index (events, PadEvent, i);
576 if (G_UNLIKELY (ev->event == NULL))
579 /* take aditional ref, func might release the lock */
580 ev_ret.event = gst_event_ref (ev->event);
581 ev_ret.received = ev->received;
583 ret = func (pad, &ev_ret, user_data);
585 /* recheck the cookie, lock might have been released and the list could have
587 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
588 if (G_LIKELY (ev_ret.event))
589 gst_event_unref (ev_ret.event);
593 /* store the received state */
594 ev->received = ev_ret.received;
596 /* if the event changed, we need to do something */
597 if (G_UNLIKELY (ev->event != ev_ret.event)) {
598 if (G_UNLIKELY (ev_ret.event == NULL)) {
599 /* function unreffed and set the event to NULL, remove it */
600 gst_event_unref (ev->event);
601 g_array_remove_index (events, i);
603 cookie = ++pad->priv->events_cookie;
606 /* function gave a new event for us */
607 gst_event_take (&ev->event, ev_ret.event);
610 /* just unref, nothing changed */
611 gst_event_unref (ev_ret.event);
620 /* should be called with LOCK */
622 _apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
626 GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_TIME_FORMAT,
627 GST_TIME_ARGS (pad->offset));
629 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
632 g_assert (!upstream);
634 /* copy segment values */
635 gst_event_copy_segment (event, &segment);
636 gst_event_unref (event);
638 gst_segment_offset_running_time (&segment, segment.format, pad->offset);
639 event = gst_event_new_segment (&segment);
642 event = gst_event_make_writable (event);
643 offset = gst_event_get_running_time_offset (event);
645 offset -= pad->offset;
647 offset += pad->offset;
648 gst_event_set_running_time_offset (event, offset);
653 static inline GstEvent *
654 apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
656 if (G_UNLIKELY (pad->offset != 0))
657 return _apply_pad_offset (pad, event, upstream);
662 /* should be called with the OBJECT_LOCK */
664 get_pad_caps (GstPad * pad)
666 GstCaps *caps = NULL;
669 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
671 gst_event_parse_caps (ev->event, &caps);
677 gst_pad_dispose (GObject * object)
679 GstPad *pad = GST_PAD_CAST (object);
682 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
684 /* unlink the peer pad */
685 if ((peer = gst_pad_get_peer (pad))) {
686 /* window for MT unsafeness, someone else could unlink here
687 * and then we call unlink with wrong pads. The unlink
688 * function would catch this and safely return failed. */
689 if (GST_PAD_IS_SRC (pad))
690 gst_pad_unlink (pad, peer);
692 gst_pad_unlink (peer, pad);
694 gst_object_unref (peer);
697 gst_pad_set_pad_template (pad, NULL);
699 GST_OBJECT_LOCK (pad);
701 GST_OBJECT_UNLOCK (pad);
703 g_hook_list_clear (&pad->probes);
705 G_OBJECT_CLASS (parent_class)->dispose (object);
709 gst_pad_finalize (GObject * object)
711 GstPad *pad = GST_PAD_CAST (object);
714 /* in case the task is still around, clean it up */
715 if ((task = GST_PAD_TASK (pad))) {
716 gst_task_join (task);
717 GST_PAD_TASK (pad) = NULL;
718 gst_object_unref (task);
721 if (pad->activatenotify)
722 pad->activatenotify (pad->activatedata);
723 if (pad->activatemodenotify)
724 pad->activatemodenotify (pad->activatemodedata);
726 pad->linknotify (pad->linkdata);
727 if (pad->unlinknotify)
728 pad->unlinknotify (pad->unlinkdata);
729 if (pad->chainnotify)
730 pad->chainnotify (pad->chaindata);
731 if (pad->chainlistnotify)
732 pad->chainlistnotify (pad->chainlistdata);
733 if (pad->getrangenotify)
734 pad->getrangenotify (pad->getrangedata);
735 if (pad->eventnotify)
736 pad->eventnotify (pad->eventdata);
737 if (pad->querynotify)
738 pad->querynotify (pad->querydata);
739 if (pad->iterintlinknotify)
740 pad->iterintlinknotify (pad->iterintlinkdata);
742 g_rec_mutex_clear (&pad->stream_rec_lock);
743 g_cond_clear (&pad->block_cond);
744 g_array_free (pad->priv->events, TRUE);
746 G_OBJECT_CLASS (parent_class)->finalize (object);
750 gst_pad_set_property (GObject * object, guint prop_id,
751 const GValue * value, GParamSpec * pspec)
753 g_return_if_fail (GST_IS_PAD (object));
756 case PAD_PROP_DIRECTION:
757 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
759 case PAD_PROP_TEMPLATE:
760 gst_pad_set_pad_template (GST_PAD_CAST (object),
761 (GstPadTemplate *) g_value_get_object (value));
763 case PAD_PROP_OFFSET:
764 gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
773 gst_pad_get_property (GObject * object, guint prop_id,
774 GValue * value, GParamSpec * pspec)
776 g_return_if_fail (GST_IS_PAD (object));
780 GST_OBJECT_LOCK (object);
781 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
782 GST_OBJECT_UNLOCK (object);
784 case PAD_PROP_DIRECTION:
785 g_value_set_enum (value, GST_PAD_DIRECTION (object));
787 case PAD_PROP_TEMPLATE:
788 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
790 case PAD_PROP_OFFSET:
791 g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
801 * @name: (allow-none): the name of the new pad.
802 * @direction: the #GstPadDirection of the pad.
804 * Creates a new pad with the given name in the given direction.
805 * If name is %NULL, a guaranteed unique name (across all pads)
807 * This function makes a copy of the name so you can safely free the name.
809 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
815 gst_pad_new (const gchar * name, GstPadDirection direction)
817 return g_object_new (GST_TYPE_PAD,
818 "name", name, "direction", direction, NULL);
822 * gst_pad_new_from_template:
823 * @templ: the pad template to use
824 * @name: (allow-none): the name of the pad
826 * Creates a new pad with the given name from the given template.
827 * If name is %NULL, a guaranteed unique name (across all pads)
829 * This function makes a copy of the name so you can safely free the name.
831 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
835 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
837 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
839 return g_object_new (GST_TYPE_PAD,
840 "name", name, "direction", templ->direction, "template", templ, NULL);
844 * gst_pad_new_from_static_template:
845 * @templ: the #GstStaticPadTemplate to use
846 * @name: the name of the pad
848 * Creates a new pad with the given name from the given static template.
849 * If name is %NULL, a guaranteed unique name (across all pads)
851 * This function makes a copy of the name so you can safely free the name.
853 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
857 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
861 GstPadTemplate *template;
863 template = gst_static_pad_template_get (templ);
864 pad = gst_pad_new_from_template (template, name);
865 gst_object_unref (template);
869 #define ACQUIRE_PARENT(pad, parent, label) \
871 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
872 gst_object_ref (parent); \
873 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
877 #define RELEASE_PARENT(parent) \
879 if (G_LIKELY (parent)) \
880 gst_object_unref (parent); \
884 * gst_pad_get_direction:
885 * @pad: a #GstPad to get the direction of.
887 * Gets the direction of the pad. The direction of the pad is
888 * decided at construction time so this function does not take
891 * Returns: the #GstPadDirection of the pad.
896 gst_pad_get_direction (GstPad * pad)
898 GstPadDirection result;
900 /* PAD_UNKNOWN is a little silly but we need some sort of
901 * error return value */
902 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
904 result = GST_PAD_DIRECTION (pad);
910 gst_pad_activate_default (GstPad * pad, GstObject * parent)
912 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
916 * gst_pad_mode_get_name:
917 * @mode: the pad mode
919 * Return the name of a pad mode, for use in debug messages mostly.
921 * Returns: short mnemonic for pad mode @mode
924 gst_pad_mode_get_name (GstPadMode mode)
927 case GST_PAD_MODE_NONE:
929 case GST_PAD_MODE_PUSH:
931 case GST_PAD_MODE_PULL:
940 pre_activate (GstPad * pad, GstPadMode new_mode)
943 case GST_PAD_MODE_NONE:
944 GST_OBJECT_LOCK (pad);
945 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
946 GST_PAD_SET_FLUSHING (pad);
947 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
948 GST_PAD_MODE (pad) = new_mode;
949 /* unlock blocked pads so element can resume and stop */
950 GST_PAD_BLOCK_BROADCAST (pad);
951 GST_OBJECT_UNLOCK (pad);
953 case GST_PAD_MODE_PUSH:
954 case GST_PAD_MODE_PULL:
955 GST_OBJECT_LOCK (pad);
956 GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
957 gst_pad_mode_get_name (new_mode));
958 GST_PAD_UNSET_FLUSHING (pad);
959 pad->ABI.abi.last_flowret = GST_FLOW_OK;
960 GST_PAD_MODE (pad) = new_mode;
961 if (GST_PAD_IS_SINK (pad)) {
963 /* make sure the peer src pad sends us all events */
964 if ((peer = GST_PAD_PEER (pad))) {
965 gst_object_ref (peer);
966 GST_OBJECT_UNLOCK (pad);
968 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
969 GST_DEBUG_PAD_NAME (peer));
971 GST_OBJECT_LOCK (peer);
972 schedule_events (peer, NULL);
973 GST_OBJECT_UNLOCK (peer);
975 gst_object_unref (peer);
977 GST_OBJECT_UNLOCK (pad);
980 GST_OBJECT_UNLOCK (pad);
987 post_activate (GstPad * pad, GstPadMode new_mode)
990 case GST_PAD_MODE_NONE:
991 /* ensures that streaming stops */
992 GST_PAD_STREAM_LOCK (pad);
993 GST_DEBUG_OBJECT (pad, "stopped streaming");
994 GST_OBJECT_LOCK (pad);
996 GST_OBJECT_UNLOCK (pad);
997 GST_PAD_STREAM_UNLOCK (pad);
999 case GST_PAD_MODE_PUSH:
1000 case GST_PAD_MODE_PULL:
1007 * gst_pad_set_active:
1008 * @pad: the #GstPad to activate or deactivate.
1009 * @active: whether or not the pad should be active.
1011 * Activates or deactivates the given pad.
1012 * Normally called from within core state change functions.
1014 * If @active, makes sure the pad is active. If it is already active, either in
1015 * push or pull mode, just return. Otherwise dispatches to the pad's activate
1016 * function to perform the actual activation.
1018 * If not @active, calls gst_pad_activate_mode() with the pad's current mode
1019 * and a %FALSE argument.
1021 * Returns: %TRUE if the operation was successful.
1026 gst_pad_set_active (GstPad * pad, gboolean active)
1030 gboolean ret = FALSE;
1032 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1034 GST_OBJECT_LOCK (pad);
1035 old = GST_PAD_MODE (pad);
1036 ACQUIRE_PARENT (pad, parent, no_parent);
1037 GST_OBJECT_UNLOCK (pad);
1040 if (old == GST_PAD_MODE_NONE) {
1041 GST_DEBUG_OBJECT (pad, "activating pad from none");
1042 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
1044 pad->ABI.abi.last_flowret = GST_FLOW_OK;
1046 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
1047 gst_pad_mode_get_name (old));
1051 if (old == GST_PAD_MODE_NONE) {
1052 GST_DEBUG_OBJECT (pad, "pad was inactive");
1055 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1056 gst_pad_mode_get_name (old));
1057 ret = gst_pad_activate_mode (pad, old, FALSE);
1059 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
1063 RELEASE_PARENT (parent);
1065 if (G_UNLIKELY (!ret))
1073 GST_DEBUG_OBJECT (pad, "no parent");
1074 GST_OBJECT_UNLOCK (pad);
1079 GST_OBJECT_LOCK (pad);
1081 g_critical ("Failed to deactivate pad %s:%s, very bad",
1082 GST_DEBUG_PAD_NAME (pad));
1084 GST_WARNING_OBJECT (pad, "Failed to activate pad");
1086 GST_OBJECT_UNLOCK (pad);
1092 * gst_pad_activate_mode:
1093 * @pad: the #GstPad to activate or deactivate.
1094 * @mode: the requested activation mode
1095 * @active: whether or not the pad should be active.
1097 * Activates or deactivates the given pad in @mode via dispatching to the
1098 * pad's activatemodefunc. For use from within pad activation functions only.
1100 * If you don't know what this is, you probably don't want to call it.
1102 * Returns: %TRUE if the operation was successful.
1107 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1109 gboolean res = FALSE;
1111 GstPadMode old, new;
1112 GstPadDirection dir;
1115 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1117 GST_OBJECT_LOCK (pad);
1118 old = GST_PAD_MODE (pad);
1119 dir = GST_PAD_DIRECTION (pad);
1120 ACQUIRE_PARENT (pad, parent, no_parent);
1121 GST_OBJECT_UNLOCK (pad);
1123 new = active ? mode : GST_PAD_MODE_NONE;
1128 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1129 /* pad was activate in the wrong direction, deactivate it
1130 * and reactivate it in the requested mode */
1131 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1132 gst_pad_mode_get_name (old));
1134 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
1135 goto deactivate_failed;
1139 case GST_PAD_MODE_PULL:
1141 if (dir == GST_PAD_SINK) {
1142 if ((peer = gst_pad_get_peer (pad))) {
1143 GST_DEBUG_OBJECT (pad, "calling peer");
1144 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1146 gst_object_unref (peer);
1148 /* there is no peer, this is only fatal when we activate. When we
1149 * deactivate, we must assume the application has unlinked the peer and
1150 * will deactivate it eventually. */
1154 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1157 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1158 goto failure; /* Can't activate pull on a src without a
1159 getrange function */
1167 /* Mark pad as needing reconfiguration */
1169 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1170 pre_activate (pad, new);
1172 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1173 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1177 /* can happen for sinks of passthrough elements */
1180 post_activate (pad, new);
1182 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1183 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1188 /* Clear sticky flags on deactivation */
1190 GST_OBJECT_LOCK (pad);
1191 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1192 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1193 GST_OBJECT_UNLOCK (pad);
1197 RELEASE_PARENT (parent);
1203 GST_DEBUG_OBJECT (pad, "no parent");
1204 GST_OBJECT_UNLOCK (pad);
1209 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1210 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1215 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1216 "failed to %s in switch to %s mode from %s mode",
1217 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1218 gst_pad_mode_get_name (old));
1223 GST_OBJECT_LOCK (peer);
1224 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1225 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1226 GST_OBJECT_UNLOCK (peer);
1227 gst_object_unref (peer);
1232 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1233 "pad in pull mode");
1238 GST_OBJECT_LOCK (pad);
1239 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1240 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1241 GST_PAD_SET_FLUSHING (pad);
1242 GST_PAD_MODE (pad) = old;
1243 GST_OBJECT_UNLOCK (pad);
1249 * gst_pad_is_active:
1250 * @pad: the #GstPad to query
1252 * Query if a pad is active
1254 * Returns: %TRUE if the pad is active.
1259 gst_pad_is_active (GstPad * pad)
1261 gboolean result = FALSE;
1263 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1265 GST_OBJECT_LOCK (pad);
1266 result = GST_PAD_IS_ACTIVE (pad);
1267 GST_OBJECT_UNLOCK (pad);
1273 cleanup_hook (GstPad * pad, GHook * hook)
1275 GstPadProbeType type;
1277 if (!G_HOOK_IS_VALID (hook))
1280 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1282 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1283 /* unblock when we remove the last blocking probe */
1285 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1288 /* Might have new probes now that want to be called */
1289 GST_PAD_BLOCK_BROADCAST (pad);
1291 if (pad->num_blocked == 0) {
1292 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1293 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1296 g_hook_destroy_link (&pad->probes, hook);
1301 * gst_pad_add_probe:
1302 * @pad: the #GstPad to add the probe to
1303 * @mask: the probe mask
1304 * @callback: #GstPadProbeCallback that will be called with notifications of
1306 * @user_data: (closure): user data passed to the callback
1307 * @destroy_data: #GDestroyNotify for user_data
1309 * Be notified of different states of pads. The provided callback is called for
1310 * every state that matches @mask.
1312 * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1313 * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1314 * happen that the probe can be run immediately and if the probe returns
1315 * GST_PAD_PROBE_REMOVE this functions returns 0.
1320 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1321 GstPadProbeCallback callback, gpointer user_data,
1322 GDestroyNotify destroy_data)
1327 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1328 g_return_val_if_fail (mask != 0, 0);
1330 GST_OBJECT_LOCK (pad);
1332 /* make a new probe */
1333 hook = g_hook_alloc (&pad->probes);
1335 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1338 /* when no contraints are given for the types, assume all types are
1340 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1341 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1342 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1343 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1345 /* store our flags and other fields */
1346 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1347 hook->func = callback;
1348 hook->data = user_data;
1349 hook->destroy = destroy_data;
1350 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1353 g_hook_prepend (&pad->probes, hook);
1355 /* incremenent cookie so that the new hook get's called */
1356 pad->priv->probe_list_cookie++;
1358 /* get the id of the hook, we return this and it can be used to remove the
1360 res = hook->hook_id;
1362 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1364 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1365 /* we have a block probe */
1367 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1368 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1369 "now %d blocking probes", pad->num_blocked);
1371 /* Might have new probes now that want to be called */
1372 GST_PAD_BLOCK_BROADCAST (pad);
1375 /* call the callback if we need to be called for idle callbacks */
1376 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1377 if (pad->priv->using > 0) {
1378 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1379 * flag above, the last thread to leave the push will do the callback. New
1380 * threads going into the push will block. */
1381 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1382 "pad is in use, delay idle callback");
1383 GST_OBJECT_UNLOCK (pad);
1385 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1386 GstPadProbeReturn ret;
1388 /* Keep another ref, the callback could destroy the pad */
1389 gst_object_ref (pad);
1391 /* the pad is idle now, we can signal the idle callback now */
1392 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1393 "pad is idle, trigger idle callback");
1394 GST_OBJECT_UNLOCK (pad);
1396 ret = callback (pad, &info, user_data);
1398 GST_OBJECT_LOCK (pad);
1400 case GST_PAD_PROBE_REMOVE:
1401 /* remove the probe */
1402 GST_DEBUG_OBJECT (pad, "asked to remove hook");
1403 cleanup_hook (pad, hook);
1406 case GST_PAD_PROBE_DROP:
1407 GST_DEBUG_OBJECT (pad, "asked to drop item");
1409 case GST_PAD_PROBE_PASS:
1410 GST_DEBUG_OBJECT (pad, "asked to pass item");
1412 case GST_PAD_PROBE_OK:
1413 GST_DEBUG_OBJECT (pad, "probe returned OK");
1416 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1419 GST_OBJECT_UNLOCK (pad);
1421 gst_object_unref (pad);
1424 GST_OBJECT_UNLOCK (pad);
1430 * gst_pad_remove_probe:
1431 * @pad: the #GstPad with the probe
1432 * @id: the probe id to remove
1434 * Remove the probe with @id from @pad.
1439 gst_pad_remove_probe (GstPad * pad, gulong id)
1443 g_return_if_fail (GST_IS_PAD (pad));
1445 GST_OBJECT_LOCK (pad);
1447 hook = g_hook_get (&pad->probes, id);
1451 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1453 cleanup_hook (pad, hook);
1454 GST_OBJECT_UNLOCK (pad);
1460 GST_OBJECT_UNLOCK (pad);
1461 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1467 * gst_pad_is_blocked:
1468 * @pad: the #GstPad to query
1470 * Checks if the pad is blocked or not. This function returns the
1471 * last requested state of the pad. It is not certain that the pad
1472 * is actually blocking at this point (see gst_pad_is_blocking()).
1474 * Returns: %TRUE if the pad is blocked.
1479 gst_pad_is_blocked (GstPad * pad)
1481 gboolean result = FALSE;
1483 g_return_val_if_fail (GST_IS_PAD (pad), result);
1485 GST_OBJECT_LOCK (pad);
1486 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1487 GST_OBJECT_UNLOCK (pad);
1493 * gst_pad_is_blocking:
1494 * @pad: the #GstPad to query
1496 * Checks if the pad is blocking or not. This is a guaranteed state
1497 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1499 * Returns: %TRUE if the pad is blocking.
1504 gst_pad_is_blocking (GstPad * pad)
1506 gboolean result = FALSE;
1508 g_return_val_if_fail (GST_IS_PAD (pad), result);
1510 GST_OBJECT_LOCK (pad);
1511 /* the blocking flag is only valid if the pad is not flushing */
1512 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1513 GST_OBJECT_UNLOCK (pad);
1519 * gst_pad_needs_reconfigure:
1520 * @pad: the #GstPad to check
1522 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1523 * if the flag was set.
1525 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1528 gst_pad_needs_reconfigure (GstPad * pad)
1530 gboolean reconfigure;
1532 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1534 GST_OBJECT_LOCK (pad);
1535 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1536 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1537 GST_OBJECT_UNLOCK (pad);
1543 * gst_pad_check_reconfigure:
1544 * @pad: the #GstPad to check
1546 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1547 * if the flag was set.
1549 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1552 gst_pad_check_reconfigure (GstPad * pad)
1554 gboolean reconfigure;
1556 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1558 GST_OBJECT_LOCK (pad);
1559 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1561 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1562 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1564 GST_OBJECT_UNLOCK (pad);
1570 * gst_pad_mark_reconfigure:
1571 * @pad: the #GstPad to mark
1573 * Mark a pad for needing reconfiguration. The next call to
1574 * gst_pad_check_reconfigure() will return %TRUE after this call.
1577 gst_pad_mark_reconfigure (GstPad * pad)
1579 g_return_if_fail (GST_IS_PAD (pad));
1581 GST_OBJECT_LOCK (pad);
1582 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1583 GST_OBJECT_UNLOCK (pad);
1587 * gst_pad_set_activate_function:
1589 * @f: the #GstPadActivateFunction to set.
1591 * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1595 * gst_pad_set_activate_function_full:
1597 * @activate: the #GstPadActivateFunction to set.
1598 * @user_data: user_data passed to @notify
1599 * @notify: notify called when @activate will not be used anymore.
1601 * Sets the given activate function for @pad. The activate function will
1602 * dispatch to gst_pad_activate_mode() to perform the actual activation.
1603 * Only makes sense to set on sink pads.
1605 * Call this function if your sink pad can start a pull-based task.
1608 gst_pad_set_activate_function_full (GstPad * pad,
1609 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1611 g_return_if_fail (GST_IS_PAD (pad));
1613 if (pad->activatenotify)
1614 pad->activatenotify (pad->activatedata);
1615 GST_PAD_ACTIVATEFUNC (pad) = activate;
1616 pad->activatedata = user_data;
1617 pad->activatenotify = notify;
1619 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1620 GST_DEBUG_FUNCPTR_NAME (activate));
1624 * gst_pad_set_activatemode_function:
1626 * @f: the #GstPadActivateModeFunction to set.
1628 * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1632 * gst_pad_set_activatemode_function_full:
1634 * @activatemode: the #GstPadActivateModeFunction to set.
1635 * @user_data: user_data passed to @notify
1636 * @notify: notify called when @activatemode will not be used anymore.
1638 * Sets the given activate_mode function for the pad. An activate_mode function
1639 * prepares the element for data passing.
1642 gst_pad_set_activatemode_function_full (GstPad * pad,
1643 GstPadActivateModeFunction activatemode, gpointer user_data,
1644 GDestroyNotify notify)
1646 g_return_if_fail (GST_IS_PAD (pad));
1648 if (pad->activatemodenotify)
1649 pad->activatemodenotify (pad->activatemodedata);
1650 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1651 pad->activatemodedata = user_data;
1652 pad->activatemodenotify = notify;
1654 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1655 GST_DEBUG_FUNCPTR_NAME (activatemode));
1659 * gst_pad_set_chain_function:
1660 * @p: a sink #GstPad.
1661 * @f: the #GstPadChainFunction to set.
1663 * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1667 * gst_pad_set_chain_function_full:
1668 * @pad: a sink #GstPad.
1669 * @chain: the #GstPadChainFunction to set.
1670 * @user_data: user_data passed to @notify
1671 * @notify: notify called when @chain will not be used anymore.
1673 * Sets the given chain function for the pad. The chain function is called to
1674 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1677 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1678 gpointer user_data, GDestroyNotify notify)
1680 g_return_if_fail (GST_IS_PAD (pad));
1681 g_return_if_fail (GST_PAD_IS_SINK (pad));
1683 if (pad->chainnotify)
1684 pad->chainnotify (pad->chaindata);
1685 GST_PAD_CHAINFUNC (pad) = chain;
1686 pad->chaindata = user_data;
1687 pad->chainnotify = notify;
1689 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1690 GST_DEBUG_FUNCPTR_NAME (chain));
1694 * gst_pad_set_chain_list_function:
1695 * @p: a sink #GstPad.
1696 * @f: the #GstPadChainListFunction to set.
1698 * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1702 * gst_pad_set_chain_list_function_full:
1703 * @pad: a sink #GstPad.
1704 * @chainlist: the #GstPadChainListFunction to set.
1705 * @user_data: user_data passed to @notify
1706 * @notify: notify called when @chainlist will not be used anymore.
1708 * Sets the given chain list function for the pad. The chainlist function is
1709 * called to process a #GstBufferList input buffer list. See
1710 * #GstPadChainListFunction for more details.
1713 gst_pad_set_chain_list_function_full (GstPad * pad,
1714 GstPadChainListFunction chainlist, gpointer user_data,
1715 GDestroyNotify notify)
1717 g_return_if_fail (GST_IS_PAD (pad));
1718 g_return_if_fail (GST_PAD_IS_SINK (pad));
1720 if (pad->chainlistnotify)
1721 pad->chainlistnotify (pad->chainlistdata);
1722 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1723 pad->chainlistdata = user_data;
1724 pad->chainlistnotify = notify;
1726 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1727 GST_DEBUG_FUNCPTR_NAME (chainlist));
1731 * gst_pad_set_getrange_function:
1732 * @p: a source #GstPad.
1733 * @f: the #GstPadGetRangeFunction to set.
1735 * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1739 * gst_pad_set_getrange_function_full:
1740 * @pad: a source #GstPad.
1741 * @get: the #GstPadGetRangeFunction to set.
1742 * @user_data: user_data passed to @notify
1743 * @notify: notify called when @get will not be used anymore.
1745 * Sets the given getrange function for the pad. The getrange function is
1746 * called to produce a new #GstBuffer to start the processing pipeline. see
1747 * #GstPadGetRangeFunction for a description of the getrange function.
1750 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1751 gpointer user_data, GDestroyNotify notify)
1753 g_return_if_fail (GST_IS_PAD (pad));
1754 g_return_if_fail (GST_PAD_IS_SRC (pad));
1756 if (pad->getrangenotify)
1757 pad->getrangenotify (pad->getrangedata);
1758 GST_PAD_GETRANGEFUNC (pad) = get;
1759 pad->getrangedata = user_data;
1760 pad->getrangenotify = notify;
1762 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1763 GST_DEBUG_FUNCPTR_NAME (get));
1767 * gst_pad_set_event_function:
1768 * @p: a #GstPad of either direction.
1769 * @f: the #GstPadEventFunction to set.
1771 * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1775 * gst_pad_set_event_function_full:
1776 * @pad: a #GstPad of either direction.
1777 * @event: the #GstPadEventFunction to set.
1778 * @user_data: user_data passed to @notify
1779 * @notify: notify called when @event will not be used anymore.
1781 * Sets the given event handler for the pad.
1784 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1785 gpointer user_data, GDestroyNotify notify)
1787 g_return_if_fail (GST_IS_PAD (pad));
1789 if (pad->eventnotify)
1790 pad->eventnotify (pad->eventdata);
1791 GST_PAD_EVENTFUNC (pad) = event;
1792 pad->eventdata = user_data;
1793 pad->eventnotify = notify;
1795 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1796 GST_DEBUG_FUNCPTR_NAME (event));
1800 * gst_pad_set_query_function:
1801 * @p: a #GstPad of either direction.
1802 * @f: the #GstPadQueryFunction to set.
1804 * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1808 * gst_pad_set_query_function_full:
1809 * @pad: a #GstPad of either direction.
1810 * @query: the #GstPadQueryFunction to set.
1811 * @user_data: user_data passed to @notify
1812 * @notify: notify called when @query will not be used anymore.
1814 * Set the given query function for the pad.
1817 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1818 gpointer user_data, GDestroyNotify notify)
1820 g_return_if_fail (GST_IS_PAD (pad));
1822 if (pad->querynotify)
1823 pad->querynotify (pad->querydata);
1824 GST_PAD_QUERYFUNC (pad) = query;
1825 pad->querydata = user_data;
1826 pad->querynotify = notify;
1828 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1829 GST_DEBUG_FUNCPTR_NAME (query));
1833 * gst_pad_set_iterate_internal_links_function:
1834 * @p: a #GstPad of either direction.
1835 * @f: the #GstPadIterIntLinkFunction to set.
1837 * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1838 * for the user_data and notify.
1841 * gst_pad_set_iterate_internal_links_function_full:
1842 * @pad: a #GstPad of either direction.
1843 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1844 * @user_data: user_data passed to @notify
1845 * @notify: notify called when @iterintlink will not be used anymore.
1847 * Sets the given internal link iterator function for the pad.
1850 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1851 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1852 GDestroyNotify notify)
1854 g_return_if_fail (GST_IS_PAD (pad));
1856 if (pad->iterintlinknotify)
1857 pad->iterintlinknotify (pad->iterintlinkdata);
1858 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1859 pad->iterintlinkdata = user_data;
1860 pad->iterintlinknotify = notify;
1862 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1863 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1867 * gst_pad_set_link_function:
1869 * @f: the #GstPadLinkFunction to set.
1871 * Calls gst_pad_set_link_function_full() with %NULL
1872 * for the user_data and notify.
1875 * gst_pad_set_link_function_full:
1877 * @link: the #GstPadLinkFunction to set.
1878 * @user_data: user_data passed to @notify
1879 * @notify: notify called when @link will not be used anymore.
1881 * Sets the given link function for the pad. It will be called when
1882 * the pad is linked with another pad.
1884 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1887 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1888 * cannot be made for some reason.
1890 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1891 * of the peer sink pad, if present.
1894 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1895 gpointer user_data, GDestroyNotify notify)
1897 g_return_if_fail (GST_IS_PAD (pad));
1899 if (pad->linknotify)
1900 pad->linknotify (pad->linkdata);
1901 GST_PAD_LINKFUNC (pad) = link;
1902 pad->linkdata = user_data;
1903 pad->linknotify = notify;
1905 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1906 GST_DEBUG_FUNCPTR_NAME (link));
1910 * gst_pad_set_unlink_function:
1912 * @f: the #GstPadUnlinkFunction to set.
1914 * Calls gst_pad_set_unlink_function_full() with %NULL
1915 * for the user_data and notify.
1918 * gst_pad_set_unlink_function_full:
1920 * @unlink: the #GstPadUnlinkFunction to set.
1921 * @user_data: user_data passed to @notify
1922 * @notify: notify called when @unlink will not be used anymore.
1924 * Sets the given unlink function for the pad. It will be called
1925 * when the pad is unlinked.
1928 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1929 gpointer user_data, GDestroyNotify notify)
1931 g_return_if_fail (GST_IS_PAD (pad));
1933 if (pad->unlinknotify)
1934 pad->unlinknotify (pad->unlinkdata);
1935 GST_PAD_UNLINKFUNC (pad) = unlink;
1936 pad->unlinkdata = user_data;
1937 pad->unlinknotify = notify;
1939 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1940 GST_DEBUG_FUNCPTR_NAME (unlink));
1945 * @srcpad: the source #GstPad to unlink.
1946 * @sinkpad: the sink #GstPad to unlink.
1948 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1949 * signal on both pads.
1951 * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
1952 * the pads were not linked together.
1957 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1959 gboolean result = FALSE;
1960 GstElement *parent = NULL;
1962 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1963 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1964 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1965 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1967 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1968 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1969 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1971 /* We need to notify the parent before taking any pad locks as the bin in
1972 * question might be waiting for a lock on the pad while holding its lock
1973 * that our message will try to take. */
1974 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1975 if (GST_IS_ELEMENT (parent)) {
1976 gst_element_post_message (parent,
1977 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1978 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1980 gst_object_unref (parent);
1985 GST_OBJECT_LOCK (srcpad);
1986 GST_OBJECT_LOCK (sinkpad);
1988 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1989 goto not_linked_together;
1991 if (GST_PAD_UNLINKFUNC (srcpad)) {
1992 GstObject *tmpparent;
1994 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
1996 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
1997 RELEASE_PARENT (tmpparent);
2000 if (GST_PAD_UNLINKFUNC (sinkpad)) {
2001 GstObject *tmpparent;
2003 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2005 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2006 RELEASE_PARENT (tmpparent);
2010 /* first clear peers */
2011 GST_PAD_PEER (srcpad) = NULL;
2012 GST_PAD_PEER (sinkpad) = NULL;
2014 GST_OBJECT_UNLOCK (sinkpad);
2015 GST_OBJECT_UNLOCK (srcpad);
2017 /* fire off a signal to each of the pads telling them
2018 * that they've been unlinked */
2019 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2020 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2022 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2023 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2028 if (parent != NULL) {
2029 gst_element_post_message (parent,
2030 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2031 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2032 gst_object_unref (parent);
2037 not_linked_together:
2039 /* we do not emit a warning in this case because unlinking cannot
2040 * be made MT safe.*/
2041 GST_OBJECT_UNLOCK (sinkpad);
2042 GST_OBJECT_UNLOCK (srcpad);
2048 * gst_pad_is_linked:
2049 * @pad: pad to check
2051 * Checks if a @pad is linked to another pad or not.
2053 * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2058 gst_pad_is_linked (GstPad * pad)
2062 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2064 GST_OBJECT_LOCK (pad);
2065 result = (GST_PAD_PEER (pad) != NULL);
2066 GST_OBJECT_UNLOCK (pad);
2071 /* get the caps from both pads and see if the intersection
2074 * This function should be called with the pad LOCK on both
2078 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2079 GstPadLinkCheck flags)
2081 GstCaps *srccaps = NULL;
2082 GstCaps *sinkcaps = NULL;
2083 gboolean compatible = FALSE;
2085 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2088 /* Doing the expensive caps checking takes priority over only checking the template caps */
2089 if (flags & GST_PAD_LINK_CHECK_CAPS) {
2090 GST_OBJECT_UNLOCK (sink);
2091 GST_OBJECT_UNLOCK (src);
2093 srccaps = gst_pad_query_caps (src, NULL);
2094 sinkcaps = gst_pad_query_caps (sink, NULL);
2096 GST_OBJECT_LOCK (src);
2097 GST_OBJECT_LOCK (sink);
2099 /* If one of the two pads doesn't have a template, consider the intersection
2101 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2102 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2106 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2108 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2111 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2113 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2116 /* if we have caps on both pads we can check the intersection. If one
2117 * of the caps is %NULL, we return %TRUE. */
2118 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2120 gst_caps_unref (srccaps);
2122 gst_caps_unref (sinkcaps);
2126 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2127 gst_caps_unref (srccaps);
2128 gst_caps_unref (sinkcaps);
2131 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2132 (compatible ? "" : "not "));
2137 /* check if the grandparents of both pads are the same.
2138 * This check is required so that we don't try to link
2139 * pads from elements in different bins without ghostpads.
2141 * The LOCK should be held on both pads
2144 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2146 GstObject *psrc, *psink;
2148 psrc = GST_OBJECT_PARENT (src);
2149 psink = GST_OBJECT_PARENT (sink);
2151 /* if one of the pads has no parent, we allow the link */
2152 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2155 /* only care about parents that are elements */
2156 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2157 goto no_element_parent;
2159 /* if the parents are the same, we have a loop */
2160 if (G_UNLIKELY (psrc == psink))
2163 /* if they both have a parent, we check the grandparents. We can not lock
2164 * the parent because we hold on the child (pad) and the locking order is
2165 * parent >> child. */
2166 psrc = GST_OBJECT_PARENT (psrc);
2167 psink = GST_OBJECT_PARENT (psink);
2169 /* if they have grandparents but they are not the same */
2170 if (G_UNLIKELY (psrc != psink))
2171 goto wrong_grandparents;
2178 GST_CAT_DEBUG (GST_CAT_CAPS,
2179 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2180 GST_PTR_FORMAT, psrc, psink);
2185 GST_CAT_DEBUG (GST_CAT_CAPS,
2186 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2187 GST_PTR_FORMAT, psrc, psink);
2192 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2198 GST_CAT_DEBUG (GST_CAT_CAPS,
2199 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2200 GST_PTR_FORMAT, psrc, psink);
2205 /* FIXME leftover from an attempt at refactoring... */
2206 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2207 * the two pads will be locked in the srcpad, sinkpad order. */
2208 static GstPadLinkReturn
2209 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2211 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2212 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2214 GST_OBJECT_LOCK (srcpad);
2216 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2217 goto src_was_linked;
2219 GST_OBJECT_LOCK (sinkpad);
2221 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2222 goto sink_was_linked;
2224 /* check hierarchy, pads can only be linked if the grandparents
2226 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2227 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2228 goto wrong_hierarchy;
2230 /* check pad caps for non-empty intersection */
2231 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2234 /* FIXME check pad scheduling for non-empty intersection */
2236 return GST_PAD_LINK_OK;
2240 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2241 GST_DEBUG_PAD_NAME (srcpad),
2242 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2243 /* we do not emit a warning in this case because unlinking cannot
2244 * be made MT safe.*/
2245 GST_OBJECT_UNLOCK (srcpad);
2246 return GST_PAD_LINK_WAS_LINKED;
2250 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2251 GST_DEBUG_PAD_NAME (sinkpad),
2252 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2253 /* we do not emit a warning in this case because unlinking cannot
2254 * be made MT safe.*/
2255 GST_OBJECT_UNLOCK (sinkpad);
2256 GST_OBJECT_UNLOCK (srcpad);
2257 return GST_PAD_LINK_WAS_LINKED;
2261 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2262 GST_OBJECT_UNLOCK (sinkpad);
2263 GST_OBJECT_UNLOCK (srcpad);
2264 return GST_PAD_LINK_WRONG_HIERARCHY;
2268 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2269 GST_OBJECT_UNLOCK (sinkpad);
2270 GST_OBJECT_UNLOCK (srcpad);
2271 return GST_PAD_LINK_NOFORMAT;
2277 * @srcpad: the source #GstPad.
2278 * @sinkpad: the sink #GstPad.
2280 * Checks if the source pad and the sink pad are compatible so they can be
2283 * Returns: %TRUE if the pads can be linked.
2286 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2288 GstPadLinkReturn result;
2290 /* generic checks */
2291 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2292 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2294 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2295 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2297 /* gst_pad_link_prepare does everything for us, we only release the locks
2298 * on the pads that it gets us. If this function returns !OK the locks are not
2300 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2301 if (result != GST_PAD_LINK_OK)
2304 GST_OBJECT_UNLOCK (srcpad);
2305 GST_OBJECT_UNLOCK (sinkpad);
2308 return result == GST_PAD_LINK_OK;
2312 * gst_pad_link_full:
2313 * @srcpad: the source #GstPad to link.
2314 * @sinkpad: the sink #GstPad to link.
2315 * @flags: the checks to validate when linking
2317 * Links the source pad and the sink pad.
2319 * This variant of #gst_pad_link provides a more granular control on the
2320 * checks being done when linking. While providing some considerable speedups
2321 * the caller of this method must be aware that wrong usage of those flags
2322 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2323 * for more information.
2327 * Returns: A result code indicating if the connection worked or
2331 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2333 GstPadLinkReturn result;
2335 GstPadLinkFunction srcfunc, sinkfunc;
2337 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2338 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2339 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2340 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2341 GST_PAD_LINK_WRONG_DIRECTION);
2343 /* Notify the parent early. See gst_pad_unlink for details. */
2344 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2345 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2346 gst_element_post_message (parent,
2347 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2348 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2350 gst_object_unref (parent);
2355 /* prepare will also lock the two pads */
2356 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2358 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2361 /* must set peers before calling the link function */
2362 GST_PAD_PEER (srcpad) = sinkpad;
2363 GST_PAD_PEER (sinkpad) = srcpad;
2365 /* check events, when something is different, mark pending */
2366 schedule_events (srcpad, sinkpad);
2368 /* get the link functions */
2369 srcfunc = GST_PAD_LINKFUNC (srcpad);
2370 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2372 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2373 /* custom link functions, execute them */
2374 GST_OBJECT_UNLOCK (sinkpad);
2375 GST_OBJECT_UNLOCK (srcpad);
2378 GstObject *tmpparent;
2380 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2381 /* this one will call the peer link function */
2382 result = srcfunc (srcpad, tmpparent, sinkpad);
2383 RELEASE_PARENT (tmpparent);
2384 } else if (sinkfunc) {
2385 GstObject *tmpparent;
2387 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2388 /* if no source link function, we need to call the sink link
2389 * function ourselves. */
2390 result = sinkfunc (sinkpad, tmpparent, srcpad);
2391 RELEASE_PARENT (tmpparent);
2395 GST_OBJECT_LOCK (srcpad);
2396 GST_OBJECT_LOCK (sinkpad);
2398 /* we released the lock, check if the same pads are linked still */
2399 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2400 goto concurrent_link;
2402 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2405 GST_OBJECT_UNLOCK (sinkpad);
2406 GST_OBJECT_UNLOCK (srcpad);
2408 /* fire off a signal to each of the pads telling them
2409 * that they've been linked */
2410 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2411 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2413 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2414 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2416 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2419 if (G_LIKELY (parent)) {
2420 gst_element_post_message (parent,
2421 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2422 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2423 gst_object_unref (parent);
2431 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2432 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2433 GST_OBJECT_UNLOCK (sinkpad);
2434 GST_OBJECT_UNLOCK (srcpad);
2436 /* The other link operation succeeded first */
2437 result = GST_PAD_LINK_WAS_LINKED;
2442 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2443 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2444 gst_pad_link_get_name (result));
2446 GST_PAD_PEER (srcpad) = NULL;
2447 GST_PAD_PEER (sinkpad) = NULL;
2449 GST_OBJECT_UNLOCK (sinkpad);
2450 GST_OBJECT_UNLOCK (srcpad);
2458 * @srcpad: the source #GstPad to link.
2459 * @sinkpad: the sink #GstPad to link.
2461 * Links the source pad and the sink pad.
2463 * Returns: A result code indicating if the connection worked or
2469 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2471 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2475 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2477 GstPadTemplate **template_p;
2479 /* this function would need checks if it weren't static */
2481 GST_OBJECT_LOCK (pad);
2482 template_p = &pad->padtemplate;
2483 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2484 GST_OBJECT_UNLOCK (pad);
2487 gst_pad_template_pad_created (templ, pad);
2491 * gst_pad_get_pad_template:
2494 * Gets the template for @pad.
2496 * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2497 * this pad was instantiated, or %NULL if this pad has no
2498 * template. Unref after usage.
2501 gst_pad_get_pad_template (GstPad * pad)
2503 GstPadTemplate *templ;
2505 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2507 templ = GST_PAD_PAD_TEMPLATE (pad);
2509 return (templ ? gst_object_ref (templ) : NULL);
2513 * gst_pad_has_current_caps:
2514 * @pad: a #GstPad to check
2516 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2518 * Returns: %TRUE when @pad has caps associated with it.
2521 gst_pad_has_current_caps (GstPad * pad)
2526 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2528 GST_OBJECT_LOCK (pad);
2529 caps = get_pad_caps (pad);
2530 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2531 "check current pad caps %" GST_PTR_FORMAT, caps);
2532 result = (caps != NULL);
2533 GST_OBJECT_UNLOCK (pad);
2539 * gst_pad_get_current_caps:
2540 * @pad: a #GstPad to get the current capabilities of.
2542 * Gets the capabilities currently configured on @pad with the last
2543 * #GST_EVENT_CAPS event.
2545 * Returns: the current caps of the pad with incremented ref-count.
2548 gst_pad_get_current_caps (GstPad * pad)
2552 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2554 GST_OBJECT_LOCK (pad);
2555 if ((result = get_pad_caps (pad)))
2556 gst_caps_ref (result);
2557 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2558 "get current pad caps %" GST_PTR_FORMAT, result);
2559 GST_OBJECT_UNLOCK (pad);
2565 * gst_pad_get_pad_template_caps:
2566 * @pad: a #GstPad to get the template capabilities from.
2568 * Gets the capabilities for @pad's template.
2570 * Returns: (transfer full): the #GstCaps of this pad template.
2571 * Unref after usage.
2574 gst_pad_get_pad_template_caps (GstPad * pad)
2576 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2578 if (GST_PAD_PAD_TEMPLATE (pad))
2579 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2581 return gst_caps_ref (GST_CAPS_ANY);
2586 * @pad: a #GstPad to get the peer of.
2588 * Gets the peer of @pad. This function refs the peer pad so
2589 * you need to unref it after use.
2591 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2596 gst_pad_get_peer (GstPad * pad)
2600 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2602 GST_OBJECT_LOCK (pad);
2603 result = GST_PAD_PEER (pad);
2605 gst_object_ref (result);
2606 GST_OBJECT_UNLOCK (pad);
2612 * gst_pad_get_allowed_caps:
2615 * Gets the capabilities of the allowed media types that can flow through
2616 * @pad and its peer.
2618 * The allowed capabilities is calculated as the intersection of the results of
2619 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2620 * on the resulting caps.
2622 * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2623 * pad link. Unref the caps when you no longer need it. This
2624 * function returns %NULL when @pad has no peer.
2629 gst_pad_get_allowed_caps (GstPad * pad)
2635 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2637 GST_OBJECT_LOCK (pad);
2638 peer = GST_PAD_PEER (pad);
2639 if (G_UNLIKELY (peer == NULL))
2642 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2644 gst_object_ref (peer);
2645 GST_OBJECT_UNLOCK (pad);
2646 mycaps = gst_pad_query_caps (pad, NULL);
2648 caps = gst_pad_query_caps (peer, mycaps);
2649 gst_object_unref (peer);
2651 gst_caps_unref (mycaps);
2653 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2660 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2661 GST_OBJECT_UNLOCK (pad);
2668 * gst_pad_iterate_internal_links_default:
2669 * @pad: the #GstPad to get the internal links of.
2670 * @parent: (allow-none): the parent of @pad or %NULL
2672 * Iterate the list of pads to which the given pad is linked to inside of
2673 * the parent element.
2674 * This is the default handler, and thus returns an iterator of all of the
2675 * pads inside the parent element with opposite direction.
2677 * The caller must free this iterator after use with gst_iterator_free().
2679 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2680 * has no parent. Unref each returned pad with gst_object_unref().
2683 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2690 GstElement *eparent;
2692 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2694 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2695 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2697 GST_OBJECT_LOCK (pad);
2698 eparent = GST_PAD_PARENT (pad);
2699 if (!eparent || !GST_IS_ELEMENT (eparent))
2702 gst_object_ref (eparent);
2703 GST_OBJECT_UNLOCK (pad);
2706 if (pad->direction == GST_PAD_SRC)
2707 padlist = &eparent->sinkpads;
2709 padlist = &eparent->srcpads;
2711 GST_DEBUG_OBJECT (pad, "Making iterator");
2713 cookie = &eparent->pads_cookie;
2715 lock = GST_OBJECT_GET_LOCK (eparent);
2717 res = gst_iterator_new_list (GST_TYPE_PAD,
2718 lock, cookie, padlist, (GObject *) owner, NULL);
2720 gst_object_unref (owner);
2727 GST_OBJECT_UNLOCK (pad);
2728 GST_DEBUG_OBJECT (pad, "no parent element");
2734 * gst_pad_iterate_internal_links:
2735 * @pad: the GstPad to get the internal links of.
2737 * Gets an iterator for the pads to which the given pad is linked to inside
2738 * of the parent element.
2740 * Each #GstPad element yielded by the iterator will have its refcount increased,
2741 * so unref after use.
2743 * Free-function: gst_iterator_free
2745 * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2746 * or %NULL when the pad does not have an iterator function
2747 * configured. Use gst_iterator_free() after usage.
2750 gst_pad_iterate_internal_links (GstPad * pad)
2752 GstIterator *res = NULL;
2755 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2757 GST_OBJECT_LOCK (pad);
2758 ACQUIRE_PARENT (pad, parent, no_parent);
2759 GST_OBJECT_UNLOCK (pad);
2761 if (GST_PAD_ITERINTLINKFUNC (pad))
2762 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2764 RELEASE_PARENT (parent);
2771 GST_DEBUG_OBJECT (pad, "no parent");
2772 GST_OBJECT_UNLOCK (pad);
2780 * @forward: (scope call): a #GstPadForwardFunction
2781 * @user_data: user data passed to @forward
2783 * Calls @forward for all internally linked pads of @pad. This function deals with
2784 * dynamically changing internal pads and will make sure that the @forward
2785 * function is only called once for each pad.
2787 * When @forward returns %TRUE, no further pads will be processed.
2789 * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2792 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2795 gboolean result = FALSE;
2797 gboolean done = FALSE;
2798 GValue item = { 0, };
2799 GList *pushed_pads = NULL;
2801 iter = gst_pad_iterate_internal_links (pad);
2807 switch (gst_iterator_next (iter, &item)) {
2808 case GST_ITERATOR_OK:
2812 intpad = g_value_get_object (&item);
2814 /* if already pushed, skip. FIXME, find something faster to tag pads */
2815 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2816 g_value_reset (&item);
2820 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2821 GST_DEBUG_PAD_NAME (intpad));
2822 done = result = forward (intpad, user_data);
2824 pushed_pads = g_list_prepend (pushed_pads, intpad);
2826 g_value_reset (&item);
2829 case GST_ITERATOR_RESYNC:
2830 /* We don't reset the result here because we don't push the event
2831 * again on pads that got the event already and because we need
2832 * to consider the result of the previous pushes */
2833 gst_iterator_resync (iter);
2835 case GST_ITERATOR_ERROR:
2836 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2839 case GST_ITERATOR_DONE:
2844 g_value_unset (&item);
2845 gst_iterator_free (iter);
2847 g_list_free (pushed_pads);
2857 gboolean dispatched;
2861 event_forward_func (GstPad * pad, EventData * data)
2863 /* for each pad we send to, we should ref the event; it's up
2864 * to downstream to unref again when handled. */
2865 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2866 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2868 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2870 data->dispatched = TRUE;
2877 * gst_pad_event_default:
2878 * @pad: a #GstPad to call the default event handler on.
2879 * @parent: (allow-none): the parent of @pad or %NULL
2880 * @event: (transfer full): the #GstEvent to handle.
2882 * Invokes the default event handler for the given pad.
2884 * The EOS event will pause the task associated with @pad before it is forwarded
2885 * to all internally linked pads,
2887 * The the event is sent to all pads internally linked to @pad. This function
2888 * takes ownership of @event.
2890 * Returns: %TRUE if the event was sent successfully.
2893 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2895 gboolean result, forward = TRUE;
2897 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2898 g_return_val_if_fail (event != NULL, FALSE);
2900 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2903 switch (GST_EVENT_TYPE (event)) {
2904 case GST_EVENT_CAPS:
2905 forward = GST_PAD_IS_PROXY_CAPS (pad);
2916 data.dispatched = FALSE;
2917 data.result = FALSE;
2919 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2921 /* for sinkpads without a parent element or without internal links, nothing
2922 * will be dispatched but we still want to return TRUE. */
2923 if (data.dispatched)
2924 result = data.result;
2929 gst_event_unref (event);
2934 /* Default accept caps implementation just checks against
2935 * the allowed caps for the pad */
2937 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2939 /* get the caps and see if it intersects to something not empty */
2940 GstCaps *caps, *allowed;
2943 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2945 /* first forward the query to internally linked pads when we are dealing with
2947 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2948 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2953 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2954 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2956 gst_query_parse_accept_caps (query, &caps);
2957 allowed = gst_pad_query_caps (pad, caps);
2960 if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
2961 GST_DEBUG_OBJECT (pad,
2962 "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
2964 result = gst_caps_can_intersect (caps, allowed);
2966 GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
2967 GST_PTR_FORMAT, allowed, caps);
2968 result = gst_caps_is_subset (caps, allowed);
2970 gst_caps_unref (allowed);
2972 GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
2975 gst_query_set_accept_caps_result (query, result);
2981 /* Default caps implementation */
2983 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2985 GstCaps *result = NULL, *filter;
2986 GstPadTemplate *templ;
2987 gboolean fixed_caps;
2989 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
2992 /* first try to proxy if we must */
2993 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2994 if ((gst_pad_proxy_query_caps (pad, query))) {
2999 gst_query_parse_caps (query, &filter);
3001 /* no proxy or it failed, do default handling */
3002 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3004 GST_OBJECT_LOCK (pad);
3006 /* fixed caps, try the negotiated caps first */
3007 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3008 if ((result = get_pad_caps (pad)))
3009 goto filter_done_unlock;
3012 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3013 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3014 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3015 goto filter_done_unlock;
3019 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3020 "non-fixed pad caps: trying pad caps");
3021 /* non fixed caps, try the negotiated caps */
3022 if ((result = get_pad_caps (pad)))
3023 goto filter_done_unlock;
3026 /* this almost never happens */
3027 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3028 result = GST_CAPS_ANY;
3031 GST_OBJECT_UNLOCK (pad);
3033 /* run the filter on the result */
3035 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3036 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3037 GST_PTR_FORMAT, result, result, filter, filter);
3038 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3039 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3042 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3043 "using caps %p %" GST_PTR_FORMAT, result, result);
3044 result = gst_caps_ref (result);
3046 gst_query_set_caps_result (query, result);
3047 gst_caps_unref (result);
3053 /* Default latency implementation */
3057 GstClockTime min, max;
3061 query_latency_default_fold (const GValue * item, GValue * ret,
3064 GstPad *pad = g_value_get_object (item);
3065 LatencyFoldData *fold_data = user_data;
3069 query = gst_query_new_latency ();
3070 res = gst_pad_peer_query (pad, query);
3074 GstClockTime min, max;
3076 gst_query_parse_latency (query, &live, &min, &max);
3078 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3079 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3082 if (min > fold_data->min)
3083 fold_data->min = min;
3085 if (fold_data->max == GST_CLOCK_TIME_NONE)
3086 fold_data->max = max;
3087 else if (max < fold_data->max)
3088 fold_data->max = max;
3090 fold_data->live = TRUE;
3092 g_value_set_boolean (ret, TRUE);
3094 gst_query_unref (query);
3100 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3103 GstIteratorResult res;
3104 GValue ret = G_VALUE_INIT;
3106 LatencyFoldData fold_data;
3108 it = gst_pad_iterate_internal_links (pad);
3110 GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3114 g_value_init (&ret, G_TYPE_BOOLEAN);
3117 fold_data.live = FALSE;
3119 fold_data.max = GST_CLOCK_TIME_NONE;
3121 g_value_set_boolean (&ret, FALSE);
3122 res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3124 case GST_ITERATOR_OK:
3125 g_assert_not_reached ();
3127 case GST_ITERATOR_DONE:
3129 case GST_ITERATOR_ERROR:
3130 g_value_set_boolean (&ret, FALSE);
3132 case GST_ITERATOR_RESYNC:
3133 gst_iterator_resync (it);
3136 g_assert_not_reached ();
3139 gst_iterator_free (it);
3141 query_ret = g_value_get_boolean (&ret);
3143 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3144 " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3145 fold_data.min, fold_data.max);
3147 if (fold_data.min > fold_data.max) {
3148 GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3151 gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3153 GST_LOG_OBJECT (pad, "latency query failed");
3163 gboolean dispatched;
3167 query_forward_func (GstPad * pad, QueryData * data)
3169 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3170 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3172 data->result |= gst_pad_peer_query (pad, data->query);
3174 data->dispatched = TRUE;
3176 /* stop on first successful reply */
3177 return data->result;
3181 * gst_pad_query_default:
3182 * @pad: a #GstPad to call the default query handler on.
3183 * @parent: (allow-none): the parent of @pad or %NULL
3184 * @query: (transfer none): the #GstQuery to handle.
3186 * Invokes the default query handler for the given pad.
3187 * The query is sent to all pads internally linked to @pad. Note that
3188 * if there are many possible sink pads that are internally linked to
3189 * @pad, only one will be sent the query.
3190 * Multi-sinkpad elements should implement custom query handlers.
3192 * Returns: %TRUE if the query was performed successfully.
3195 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3197 gboolean forward, ret = FALSE;
3199 switch (GST_QUERY_TYPE (query)) {
3200 case GST_QUERY_SCHEDULING:
3201 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3203 case GST_QUERY_ALLOCATION:
3204 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3206 case GST_QUERY_ACCEPT_CAPS:
3207 ret = gst_pad_query_accept_caps_default (pad, query);
3210 case GST_QUERY_CAPS:
3211 ret = gst_pad_query_caps_default (pad, query);
3214 case GST_QUERY_LATENCY:
3215 ret = gst_pad_query_latency_default (pad, query);
3218 case GST_QUERY_POSITION:
3219 case GST_QUERY_SEEKING:
3220 case GST_QUERY_FORMATS:
3221 case GST_QUERY_JITTER:
3222 case GST_QUERY_RATE:
3223 case GST_QUERY_CONVERT:
3229 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3230 query, GST_QUERY_TYPE_NAME (query));
3236 data.dispatched = FALSE;
3237 data.result = FALSE;
3239 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3241 if (data.dispatched) {
3244 /* nothing dispatched, assume drained */
3245 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3255 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3257 GstPad *pad = data->pad;
3258 GstPadProbeInfo *info = data->info;
3259 GstPadProbeType type, flags;
3260 GstPadProbeCallback callback;
3261 GstPadProbeReturn ret;
3263 /* if we have called this callback, do nothing */
3264 if (PROBE_COOKIE (hook) == data->cookie) {
3265 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3266 "hook %lu, cookie %u already called", hook->hook_id,
3267 PROBE_COOKIE (hook));
3271 PROBE_COOKIE (hook) = data->cookie;
3273 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3276 /* one of the data types for non-idle probes */
3277 if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3278 && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3280 /* one of the scheduling types */
3281 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3283 /* one of the blocking types must match */
3284 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3285 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3287 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3288 (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3290 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3291 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3292 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3295 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3296 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3297 PROBE_COOKIE (hook), flags);
3299 data->marshalled = TRUE;
3301 callback = (GstPadProbeCallback) hook->func;
3302 if (callback == NULL)
3305 info->id = hook->hook_id;
3307 GST_OBJECT_UNLOCK (pad);
3309 ret = callback (pad, info, hook->data);
3311 GST_OBJECT_LOCK (pad);
3314 case GST_PAD_PROBE_REMOVE:
3315 /* remove the probe */
3316 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3317 cleanup_hook (pad, hook);
3319 case GST_PAD_PROBE_DROP:
3320 /* need to drop the data, make sure other probes don't get called
3322 GST_DEBUG_OBJECT (pad, "asked to drop item");
3323 info->type = GST_PAD_PROBE_TYPE_INVALID;
3324 data->dropped = TRUE;
3326 case GST_PAD_PROBE_PASS:
3327 /* inform the pad block to let things pass */
3328 GST_DEBUG_OBJECT (pad, "asked to pass item");
3331 case GST_PAD_PROBE_OK:
3332 GST_DEBUG_OBJECT (pad, "probe returned OK");
3335 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3342 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3343 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3344 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3349 /* a probe that does not take or return any data */
3350 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3352 if (G_UNLIKELY (pad->num_probes)) { \
3353 GstFlowReturn pval = defaultval; \
3354 /* pass NULL as the data item */ \
3355 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3356 ret = do_probe_callbacks (pad, &info, defaultval); \
3357 if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK)) \
3362 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3364 if (G_UNLIKELY (pad->num_probes)) { \
3365 /* pass the data item */ \
3366 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3367 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3368 /* store the possibly updated data item */ \
3369 data = GST_PAD_PROBE_INFO_DATA (&info); \
3370 /* if something went wrong, exit */ \
3371 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3376 #define PROBE_PUSH(pad,mask,data,label) \
3377 PROBE_FULL(pad, mask, data, -1, -1, label);
3378 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3379 PROBE_FULL(pad, mask, data, offs, size, label);
3381 static GstFlowReturn
3382 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3383 GstFlowReturn defaultval)
3392 data.marshalled = FALSE;
3393 data.dropped = FALSE;
3394 data.cookie = ++pad->priv->probe_cookie;
3397 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3400 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3401 "do probes cookie %u", data.cookie);
3402 cookie = pad->priv->probe_list_cookie;
3404 g_hook_list_marshal (&pad->probes, TRUE,
3405 (GHookMarshaller) probe_hook_marshal, &data);
3407 /* if the list changed, call the new callbacks (they will not have their
3408 * cookie set to data.cookie */
3409 if (cookie != pad->priv->probe_list_cookie) {
3410 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3411 "probe list changed, restarting");
3415 /* the first item that dropped will stop the hooks and then we drop here */
3419 /* if no handler matched and we are blocking, let the item pass */
3420 if (!data.marshalled && is_block)
3423 /* At this point, all handlers returned either OK or PASS. If one handler
3424 * returned PASS, let the item pass */
3429 while (GST_PAD_IS_BLOCKED (pad)) {
3430 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3431 "we are blocked %d times", pad->num_blocked);
3433 /* we might have released the lock */
3434 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3437 /* now we block the streaming thread. It can be unlocked when we
3438 * deactivate the pad (which will also set the FLUSHING flag) or
3439 * when the pad is unblocked. A flushing event will also unblock
3440 * the pad after setting the FLUSHING flag. */
3441 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3442 "Waiting to be unblocked or set flushing");
3443 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3444 GST_PAD_BLOCK_WAIT (pad);
3445 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3446 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3448 /* if the list changed, call the new callbacks (they will not have their
3449 * cookie set to data.cookie */
3450 if (cookie != pad->priv->probe_list_cookie) {
3451 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3452 "probe list changed, restarting");
3456 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3466 GST_DEBUG_OBJECT (pad, "pad is flushing");
3467 return GST_FLOW_FLUSHING;
3471 GST_DEBUG_OBJECT (pad, "data is dropped");
3472 return GST_FLOW_CUSTOM_SUCCESS;
3476 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3477 GST_DEBUG_OBJECT (pad, "data is passed");
3485 * gst_pad_get_offset:
3488 * Get the offset applied to the running time of @pad. @pad has to be a source
3491 * Returns: the offset.
3494 gst_pad_get_offset (GstPad * pad)
3498 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3500 GST_OBJECT_LOCK (pad);
3501 result = pad->offset;
3502 GST_OBJECT_UNLOCK (pad);
3508 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3510 ev->received = FALSE;
3515 * gst_pad_set_offset:
3517 * @offset: the offset
3519 * Set the offset that will be applied to the running time of @pad.
3522 gst_pad_set_offset (GstPad * pad, gint64 offset)
3524 g_return_if_fail (GST_IS_PAD (pad));
3526 GST_OBJECT_LOCK (pad);
3527 /* if nothing changed, do nothing */
3528 if (pad->offset == offset)
3531 pad->offset = offset;
3532 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3534 /* resend all sticky events with updated offset on next buffer push */
3535 events_foreach (pad, mark_event_not_received, NULL);
3536 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3539 GST_OBJECT_UNLOCK (pad);
3546 /* If TRUE and ret is not OK this means
3547 * that pushing the EOS event failed
3551 /* If called for an event this is
3552 * the event that would be pushed
3553 * next. Don't forward sticky events
3554 * that would come after that */
3558 /* should be called with pad LOCK */
3560 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3562 PushStickyData *data = user_data;
3563 GstEvent *event = ev->event;
3566 GST_DEBUG_OBJECT (pad, "event %s was already received",
3567 GST_EVENT_TYPE_NAME (event));
3571 /* If we're called because of an sticky event, only forward
3572 * events that would come before this new event and the
3574 if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3575 GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3576 GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3577 data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3579 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3580 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3583 switch (data->ret) {
3585 ev->received = TRUE;
3586 GST_DEBUG_OBJECT (pad, "event %s marked received",
3587 GST_EVENT_TYPE_NAME (event));
3589 case GST_FLOW_CUSTOM_SUCCESS:
3590 /* we can't assume the event is received when it was dropped */
3591 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3592 GST_EVENT_TYPE_NAME (event));
3593 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3594 data->ret = GST_FLOW_OK;
3596 case GST_FLOW_CUSTOM_SUCCESS_1:
3597 /* event was ignored and should be sent later */
3598 GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3599 GST_EVENT_TYPE_NAME (event));
3600 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3601 data->ret = GST_FLOW_OK;
3603 case GST_FLOW_NOT_LINKED:
3604 /* not linked is not a problem, we are sticky so the event will be
3605 * sent later but only for non-EOS events */
3606 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3607 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3608 data->ret = GST_FLOW_OK;
3609 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3612 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3613 gst_flow_get_name (data->ret));
3614 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3618 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3619 data->was_eos = TRUE;
3621 return data->ret == GST_FLOW_OK;
3624 /* check sticky events and push them when needed. should be called
3626 static inline GstFlowReturn
3627 check_sticky (GstPad * pad, GstEvent * event)
3629 PushStickyData data = { GST_FLOW_OK, FALSE, event };
3631 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3632 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3634 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3635 events_foreach (pad, push_sticky, &data);
3637 /* If there's an EOS event we must push it downstream
3638 * even if sending a previous sticky event failed.
3639 * Otherwise the pipeline might wait forever for EOS.
3641 * Only do this if pushing another event than the EOS
3644 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3645 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3647 if (ev && !ev->received) {
3648 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3649 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3650 /* the event could have been dropped. Because this can only
3651 * happen if the user asked for it, it's not an error */
3652 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3653 data.ret = GST_FLOW_OK;
3663 * @pad: a #GstPad to invoke the default query on.
3664 * @query: (transfer none): the #GstQuery to perform.
3666 * Dispatches a query to a pad. The query should have been allocated by the
3667 * caller via one of the type-specific allocation functions. The element that
3668 * the pad belongs to is responsible for filling the query with an appropriate
3669 * response, which should then be parsed with a type-specific query parsing
3672 * Again, the caller is responsible for both the allocation and deallocation of
3673 * the query structure.
3675 * Please also note that some queries might need a running pipeline to work.
3677 * Returns: %TRUE if the query could be performed.
3680 gst_pad_query (GstPad * pad, GstQuery * query)
3683 gboolean res, serialized;
3684 GstPadQueryFunction func;
3685 GstPadProbeType type;
3688 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3689 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3691 if (GST_PAD_IS_SRC (pad)) {
3692 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3693 goto wrong_direction;
3694 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3695 } else if (GST_PAD_IS_SINK (pad)) {
3696 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3697 goto wrong_direction;
3698 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3700 goto unknown_direction;
3702 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3703 GST_QUERY_TYPE_NAME (query));
3705 serialized = GST_QUERY_IS_SERIALIZED (query);
3706 if (G_UNLIKELY (serialized))
3707 GST_PAD_STREAM_LOCK (pad);
3709 GST_OBJECT_LOCK (pad);
3710 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3711 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3712 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3714 ACQUIRE_PARENT (pad, parent, no_parent);
3715 GST_OBJECT_UNLOCK (pad);
3717 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3720 res = func (pad, parent, query);
3722 RELEASE_PARENT (parent);
3724 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3725 GST_QUERY_TYPE_NAME (query), res);
3730 GST_OBJECT_LOCK (pad);
3731 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3732 GST_OBJECT_UNLOCK (pad);
3734 if (G_UNLIKELY (serialized))
3735 GST_PAD_STREAM_UNLOCK (pad);
3742 g_warning ("pad %s:%s query %s in wrong direction",
3743 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3748 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3753 GST_DEBUG_OBJECT (pad, "had no parent");
3754 GST_OBJECT_UNLOCK (pad);
3755 if (G_UNLIKELY (serialized))
3756 GST_PAD_STREAM_UNLOCK (pad);
3761 GST_DEBUG_OBJECT (pad, "had no query function");
3762 RELEASE_PARENT (parent);
3763 if (G_UNLIKELY (serialized))
3764 GST_PAD_STREAM_UNLOCK (pad);
3769 GST_DEBUG_OBJECT (pad, "query failed");
3770 if (G_UNLIKELY (serialized))
3771 GST_PAD_STREAM_UNLOCK (pad);
3776 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3777 GST_OBJECT_UNLOCK (pad);
3778 if (G_UNLIKELY (serialized))
3779 GST_PAD_STREAM_UNLOCK (pad);
3781 /* if a probe dropped, we don't sent it further but assume that the probe
3782 * did not answer the query and return FALSE */
3790 * gst_pad_peer_query:
3791 * @pad: a #GstPad to invoke the peer query on.
3792 * @query: (transfer none): the #GstQuery to perform.
3794 * Performs gst_pad_query() on the peer of @pad.
3796 * The caller is responsible for both the allocation and deallocation of
3797 * the query structure.
3799 * Returns: %TRUE if the query could be performed. This function returns %FALSE
3800 * if @pad has no peer.
3803 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3806 GstPadProbeType type;
3807 gboolean res, serialized;
3810 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3811 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3813 if (GST_PAD_IS_SRC (pad)) {
3814 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3815 goto wrong_direction;
3816 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3817 } else if (GST_PAD_IS_SINK (pad)) {
3818 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3819 goto wrong_direction;
3820 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3822 goto unknown_direction;
3824 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3825 GST_QUERY_TYPE_NAME (query));
3827 serialized = GST_QUERY_IS_SERIALIZED (query);
3829 GST_OBJECT_LOCK (pad);
3830 if (GST_PAD_IS_SRC (pad) && serialized) {
3831 /* all serialized queries on the srcpad trigger push of
3833 if (check_sticky (pad, NULL) != GST_FLOW_OK)
3837 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3838 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3839 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3841 peerpad = GST_PAD_PEER (pad);
3842 if (G_UNLIKELY (peerpad == NULL))
3845 gst_object_ref (peerpad);
3846 GST_OBJECT_UNLOCK (pad);
3848 res = gst_pad_query (peerpad, query);
3850 gst_object_unref (peerpad);
3855 GST_OBJECT_LOCK (pad);
3856 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3857 GST_OBJECT_UNLOCK (pad);
3864 g_warning ("pad %s:%s query %s in wrong direction",
3865 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3870 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3875 GST_WARNING_OBJECT (pad, "could not send sticky events");
3876 GST_OBJECT_UNLOCK (pad);
3881 GST_INFO_OBJECT (pad, "pad has no peer");
3882 GST_OBJECT_UNLOCK (pad);
3887 GST_DEBUG_OBJECT (pad, "query failed");
3892 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3893 GST_OBJECT_UNLOCK (pad);
3895 /* if a probe dropped, we don't sent it further but assume that the probe
3896 * did not answer the query and return FALSE */
3903 /**********************************************************************
3904 * Data passing functions
3907 /* this is the chain function that does not perform the additional argument
3908 * checking for that little extra speed.
3910 static inline GstFlowReturn
3911 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3916 GST_PAD_STREAM_LOCK (pad);
3918 GST_OBJECT_LOCK (pad);
3919 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3922 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3925 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3928 #ifndef G_DISABLE_ASSERT
3929 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
3930 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
3932 ":%s:<%s:%s> Got data flow before stream-start event",
3933 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3935 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
3937 ":%s:<%s:%s> Got data flow before segment event",
3938 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3940 pad->priv->last_cookie = pad->priv->events_cookie;
3944 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3946 PROBE_PUSH (pad, type, data, probe_stopped);
3948 parent = GST_OBJECT_PARENT (pad);
3949 GST_OBJECT_UNLOCK (pad);
3951 /* NOTE: we read the chainfunc unlocked.
3952 * we cannot hold the lock for the pad so we might send
3953 * the data to the wrong function. This is not really a
3954 * problem since functions are assigned at creation time
3955 * and don't change that often... */
3956 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3957 GstPadChainFunction chainfunc;
3959 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3962 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3963 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3964 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3966 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3968 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3969 "called chainfunction &%s with buffer %p, returned %s",
3970 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3972 GstPadChainListFunction chainlistfunc;
3974 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3977 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3978 "calling chainlistfunction &%s",
3979 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3981 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3983 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3984 "called chainlistfunction &%s, returned %s",
3985 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3988 GST_PAD_STREAM_UNLOCK (pad);
3995 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3996 "chaining, but pad was flushing");
3997 GST_OBJECT_UNLOCK (pad);
3998 GST_PAD_STREAM_UNLOCK (pad);
3999 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4000 return GST_FLOW_FLUSHING;
4004 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4005 GST_OBJECT_UNLOCK (pad);
4006 GST_PAD_STREAM_UNLOCK (pad);
4007 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4008 return GST_FLOW_EOS;
4012 g_critical ("chain on pad %s:%s but it was not in push mode",
4013 GST_DEBUG_PAD_NAME (pad));
4014 GST_OBJECT_UNLOCK (pad);
4015 GST_PAD_STREAM_UNLOCK (pad);
4016 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4017 return GST_FLOW_ERROR;
4021 GST_OBJECT_UNLOCK (pad);
4022 GST_PAD_STREAM_UNLOCK (pad);
4023 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4026 case GST_FLOW_CUSTOM_SUCCESS:
4027 GST_DEBUG_OBJECT (pad, "dropped buffer");
4031 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4038 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4039 g_critical ("chain on pad %s:%s but it has no chainfunction",
4040 GST_DEBUG_PAD_NAME (pad));
4041 GST_PAD_STREAM_UNLOCK (pad);
4042 return GST_FLOW_NOT_SUPPORTED;
4048 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4049 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4052 * Chain a buffer to @pad.
4054 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4056 * If the buffer type is not acceptable for @pad (as negotiated with a
4057 * preceding GST_EVENT_CAPS event), this function returns
4058 * #GST_FLOW_NOT_NEGOTIATED.
4060 * The function proceeds calling the chain function installed on @pad (see
4061 * gst_pad_set_chain_function()) and the return value of that function is
4062 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4065 * In all cases, success or failure, the caller loses its reference to @buffer
4066 * after calling this function.
4068 * Returns: a #GstFlowReturn from the pad.
4073 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4075 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4076 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4077 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4079 return gst_pad_chain_data_unchecked (pad,
4080 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4083 static GstFlowReturn
4084 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4085 GstBufferList * list)
4091 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4093 len = gst_buffer_list_length (list);
4096 for (i = 0; i < len; i++) {
4097 buffer = gst_buffer_list_get (list, i);
4099 gst_pad_chain_data_unchecked (pad,
4100 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4101 gst_buffer_ref (buffer));
4102 if (ret != GST_FLOW_OK)
4105 gst_buffer_list_unref (list);
4111 * gst_pad_chain_list:
4112 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4113 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4116 * Chain a bufferlist to @pad.
4118 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4120 * If @pad was not negotiated properly with a CAPS event, this function
4121 * returns #GST_FLOW_NOT_NEGOTIATED.
4123 * The function proceeds calling the chainlist function installed on @pad (see
4124 * gst_pad_set_chain_list_function()) and the return value of that function is
4125 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4126 * chainlist function.
4128 * In all cases, success or failure, the caller loses its reference to @list
4129 * after calling this function.
4133 * Returns: a #GstFlowReturn from the pad.
4136 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4138 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4139 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4140 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4142 return gst_pad_chain_data_unchecked (pad,
4143 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4146 static GstFlowReturn
4147 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4152 GST_OBJECT_LOCK (pad);
4153 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4156 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4159 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4162 #ifndef G_DISABLE_ASSERT
4163 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4164 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4166 ":%s:<%s:%s> Got data flow before stream-start event",
4167 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4169 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4171 ":%s:<%s:%s> Got data flow before segment event",
4172 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4174 pad->priv->last_cookie = pad->priv->events_cookie;
4178 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4181 /* do block probes */
4182 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4184 /* recheck sticky events because the probe might have cause a relink */
4185 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4188 /* do post-blocking probes */
4189 PROBE_PUSH (pad, type, data, probe_stopped);
4191 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4194 /* take ref to peer pad before releasing the lock */
4195 gst_object_ref (peer);
4197 GST_OBJECT_UNLOCK (pad);
4199 ret = gst_pad_chain_data_unchecked (peer, type, data);
4201 gst_object_unref (peer);
4203 GST_OBJECT_LOCK (pad);
4204 pad->ABI.abi.last_flowret = ret;
4206 if (pad->priv->using == 0) {
4207 /* pad is not active anymore, trigger idle callbacks */
4208 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4209 probe_stopped, ret);
4211 GST_OBJECT_UNLOCK (pad);
4215 /* ERROR recovery here */
4219 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4220 "pushing, but pad was flushing");
4221 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4222 GST_OBJECT_UNLOCK (pad);
4223 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4224 return GST_FLOW_FLUSHING;
4228 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4229 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4230 GST_OBJECT_UNLOCK (pad);
4231 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4232 return GST_FLOW_EOS;
4236 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4237 GST_DEBUG_PAD_NAME (pad));
4238 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4239 GST_OBJECT_UNLOCK (pad);
4240 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4241 return GST_FLOW_ERROR;
4245 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4246 "error pushing events, return %s", gst_flow_get_name (ret));
4247 pad->ABI.abi.last_flowret = ret;
4248 GST_OBJECT_UNLOCK (pad);
4249 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4254 GST_OBJECT_UNLOCK (pad);
4255 pad->ABI.abi.last_flowret =
4256 ret == GST_FLOW_CUSTOM_SUCCESS ? GST_FLOW_OK : ret;
4257 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4260 case GST_FLOW_CUSTOM_SUCCESS:
4261 GST_DEBUG_OBJECT (pad, "dropped buffer");
4265 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4272 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4273 "pushing, but it was not linked");
4274 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4275 GST_OBJECT_UNLOCK (pad);
4276 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4277 return GST_FLOW_NOT_LINKED;
4283 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4284 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4287 * Pushes a buffer to the peer of @pad.
4289 * This function will call installed block probes before triggering any
4290 * installed data probes.
4292 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4293 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4296 * In all cases, success or failure, the caller loses its reference to @buffer
4297 * after calling this function.
4299 * Returns: a #GstFlowReturn from the peer pad.
4304 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4306 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4307 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4308 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4310 return gst_pad_push_data (pad,
4311 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4315 * gst_pad_push_list:
4316 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4317 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4320 * Pushes a buffer list to the peer of @pad.
4322 * This function will call installed block probes before triggering any
4323 * installed data probes.
4325 * The function proceeds calling the chain function on the peer pad and returns
4326 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4327 * be returned. If the peer pad does not have any installed chainlist function
4328 * every group buffer of the list will be merged into a normal #GstBuffer and
4329 * chained via gst_pad_chain().
4331 * In all cases, success or failure, the caller loses its reference to @list
4332 * after calling this function.
4334 * Returns: a #GstFlowReturn from the peer pad.
4339 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4341 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4342 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4343 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4345 return gst_pad_push_data (pad,
4346 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4349 static GstFlowReturn
4350 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4351 GstBuffer ** buffer)
4354 GstPadGetRangeFunction getrangefunc;
4358 GST_PAD_STREAM_LOCK (pad);
4360 GST_OBJECT_LOCK (pad);
4361 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4364 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4367 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4372 /* when one of the probes returns DROPPED, probe_stopped will be called
4373 * and we skip calling the getrange function, res_buf should then contain a
4374 * valid result buffer */
4375 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4376 res_buf, offset, size, probe_stopped);
4378 /* recheck sticky events because the probe might have cause a relink */
4379 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4382 ACQUIRE_PARENT (pad, parent, no_parent);
4383 GST_OBJECT_UNLOCK (pad);
4385 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4388 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4389 "calling getrangefunc %s, offset %"
4390 G_GUINT64_FORMAT ", size %u",
4391 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4393 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4395 RELEASE_PARENT (parent);
4397 GST_OBJECT_LOCK (pad);
4398 if (G_UNLIKELY (ret != GST_FLOW_OK))
4399 goto get_range_failed;
4401 /* can only fire the signal if we have a valid buffer */
4403 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4404 res_buf, offset, size, probe_stopped_unref);
4405 pad->ABI.abi.last_flowret = ret;
4406 GST_OBJECT_UNLOCK (pad);
4408 GST_PAD_STREAM_UNLOCK (pad);
4417 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4418 "getrange, but pad was flushing");
4419 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4420 GST_OBJECT_UNLOCK (pad);
4421 GST_PAD_STREAM_UNLOCK (pad);
4422 return GST_FLOW_FLUSHING;
4426 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4427 GST_DEBUG_PAD_NAME (pad));
4428 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4429 GST_OBJECT_UNLOCK (pad);
4430 GST_PAD_STREAM_UNLOCK (pad);
4431 return GST_FLOW_ERROR;
4435 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4436 pad->ABI.abi.last_flowret = ret;
4437 GST_OBJECT_UNLOCK (pad);
4438 GST_PAD_STREAM_UNLOCK (pad);
4443 GST_DEBUG_OBJECT (pad, "no parent");
4444 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4445 GST_OBJECT_UNLOCK (pad);
4446 GST_PAD_STREAM_UNLOCK (pad);
4447 return GST_FLOW_FLUSHING;
4451 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4452 GST_DEBUG_PAD_NAME (pad));
4453 RELEASE_PARENT (parent);
4454 GST_PAD_STREAM_UNLOCK (pad);
4455 return GST_FLOW_NOT_SUPPORTED;
4459 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4460 "probe returned %s", gst_flow_get_name (ret));
4461 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4463 /* the probe filled the buffer and asks us to not call the getrange
4464 * anymore, we continue with the post probes then. */
4465 GST_DEBUG_OBJECT (pad, "handled buffer");
4469 /* no buffer, we are EOS */
4470 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4474 pad->ABI.abi.last_flowret = ret;
4475 GST_OBJECT_UNLOCK (pad);
4476 GST_PAD_STREAM_UNLOCK (pad);
4480 probe_stopped_unref:
4482 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4483 "probe returned %s", gst_flow_get_name (ret));
4484 /* if we drop here, it signals EOS */
4485 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4487 pad->ABI.abi.last_flowret = ret;
4488 GST_OBJECT_UNLOCK (pad);
4489 GST_PAD_STREAM_UNLOCK (pad);
4490 if (*buffer == NULL)
4491 gst_buffer_unref (res_buf);
4496 pad->ABI.abi.last_flowret = ret;
4497 GST_OBJECT_UNLOCK (pad);
4498 GST_PAD_STREAM_UNLOCK (pad);
4499 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4500 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4501 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4507 * gst_pad_get_range:
4508 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4509 * @offset: The start offset of the buffer
4510 * @size: The length of the buffer
4511 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4512 * returns #GST_FLOW_ERROR if %NULL.
4514 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4515 * immediately and @buffer is %NULL.
4517 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4518 * description of a getrange function. If @pad has no getrange function
4519 * installed (see gst_pad_set_getrange_function()) this function returns
4520 * #GST_FLOW_NOT_SUPPORTED.
4522 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4523 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4524 * must be freed with gst_buffer_unref() after usage.
4526 * When @buffer points to a variable that points to a valid #GstBuffer, the
4527 * buffer will be filled with the result data when this function returns
4528 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4529 * @size bytes will be filled in the result buffer and its size will be updated
4532 * Note that less than @size bytes can be returned in @buffer when, for example,
4533 * an EOS condition is near or when @buffer is not large enough to hold @size
4534 * bytes. The caller should check the result buffer size to get the result size.
4536 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4537 * will be unchanged.
4539 * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4541 * Returns: a #GstFlowReturn from the pad.
4546 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4547 GstBuffer ** buffer)
4549 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4550 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4551 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4552 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4553 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4555 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4559 * gst_pad_pull_range:
4560 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4561 * @offset: The start offset of the buffer
4562 * @size: The length of the buffer
4563 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4564 * GST_FLOW_ERROR if %NULL.
4566 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4568 * This function will first trigger the pad block signal if it was
4571 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4572 * function returns the result of gst_pad_get_range() on the peer pad.
4573 * See gst_pad_get_range() for a list of return values and for the
4574 * semantics of the arguments of this function.
4576 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4577 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4578 * must be freed with gst_buffer_unref() after usage. When this function
4579 * returns any other result value, @buffer will still point to %NULL.
4581 * When @buffer points to a variable that points to a valid #GstBuffer, the
4582 * buffer will be filled with the result data when this function returns
4583 * #GST_FLOW_OK. When this function returns any other result value,
4584 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4585 * @size bytes will be filled in the result buffer and its size will be updated
4588 * Note that less than @size bytes can be returned in @buffer when, for example,
4589 * an EOS condition is near or when @buffer is not large enough to hold @size
4590 * bytes. The caller should check the result buffer size to get the result size.
4592 * Returns: a #GstFlowReturn from the peer pad.
4597 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4598 GstBuffer ** buffer)
4604 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4605 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4606 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4607 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4608 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4610 GST_OBJECT_LOCK (pad);
4611 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4614 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4619 /* when one of the probes returns DROPPED, probe_stopped will be
4620 * called and we skip calling the peer getrange function. *buffer should then
4621 * contain a valid buffer */
4622 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4623 res_buf, offset, size, probe_stopped);
4625 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4628 gst_object_ref (peer);
4630 GST_OBJECT_UNLOCK (pad);
4632 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4634 gst_object_unref (peer);
4636 GST_OBJECT_LOCK (pad);
4638 pad->ABI.abi.last_flowret = ret;
4639 if (pad->priv->using == 0) {
4640 /* pad is not active anymore, trigger idle callbacks */
4641 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4642 probe_stopped_unref, ret);
4645 if (G_UNLIKELY (ret != GST_FLOW_OK))
4646 goto pull_range_failed;
4649 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4650 res_buf, offset, size, probe_stopped_unref);
4652 GST_OBJECT_UNLOCK (pad);
4658 /* ERROR recovery here */
4661 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4662 "pullrange, but pad was flushing");
4663 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4664 GST_OBJECT_UNLOCK (pad);
4665 return GST_FLOW_FLUSHING;
4669 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4670 GST_DEBUG_PAD_NAME (pad));
4671 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4672 GST_OBJECT_UNLOCK (pad);
4673 return GST_FLOW_ERROR;
4677 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4678 gst_flow_get_name (ret));
4679 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4681 /* the probe filled the buffer and asks us to not forward to the peer
4682 * anymore, we continue with the post probes then */
4683 GST_DEBUG_OBJECT (pad, "handled buffer");
4687 /* no buffer, we are EOS then */
4688 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4692 pad->ABI.abi.last_flowret = ret;
4693 GST_OBJECT_UNLOCK (pad);
4698 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4699 "pulling range, but it was not linked");
4700 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4701 GST_OBJECT_UNLOCK (pad);
4702 return GST_FLOW_NOT_LINKED;
4706 pad->ABI.abi.last_flowret = ret;
4707 GST_OBJECT_UNLOCK (pad);
4708 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4709 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4710 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4713 probe_stopped_unref:
4715 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4716 "post probe returned %s", gst_flow_get_name (ret));
4718 /* if we drop here, it signals EOS */
4719 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4722 pad->ABI.abi.last_flowret = ret;
4723 GST_OBJECT_UNLOCK (pad);
4725 if (*buffer == NULL)
4726 gst_buffer_unref (res_buf);
4731 /* must be called with pad object lock */
4732 static GstFlowReturn
4733 store_sticky_event (GstPad * pad, GstEvent * event)
4738 gboolean res = FALSE;
4739 const gchar *name = NULL;
4740 gboolean insert = TRUE;
4742 type = GST_EVENT_TYPE (event);
4744 /* Store all sticky events except SEGMENT/EOS when we're flushing,
4745 * otherwise they can be dropped and nothing would ever resend them.
4746 * Only do that for activated pads though, everything else is a bug!
4748 if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4749 || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4750 || type == GST_EVENT_EOS))))
4753 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4756 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4757 name = gst_structure_get_name (gst_event_get_structure (event));
4759 events = pad->priv->events;
4762 for (i = 0; i < len; i++) {
4763 PadEvent *ev = &g_array_index (events, PadEvent, i);
4765 if (ev->event == NULL)
4768 if (type == GST_EVENT_TYPE (ev->event)) {
4769 /* matching types, check matching name if needed */
4770 if (name && !gst_event_has_name (ev->event, name))
4774 if ((res = gst_event_replace (&ev->event, event)))
4775 ev->received = FALSE;
4781 if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4782 && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4783 /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4784 * storing the sticky ordered we can check that this is respected. */
4785 if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4786 || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4788 ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4789 G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4790 gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4791 gst_event_type_get_name (type));
4797 ev.event = gst_event_ref (event);
4798 ev.received = FALSE;
4799 g_array_insert_val (events, i, ev);
4804 pad->priv->events_cookie++;
4805 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4807 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4809 switch (GST_EVENT_TYPE (event)) {
4810 case GST_EVENT_CAPS:
4811 GST_OBJECT_UNLOCK (pad);
4813 GST_DEBUG_OBJECT (pad, "notify caps");
4814 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4816 GST_OBJECT_LOCK (pad);
4822 if (type == GST_EVENT_EOS) {
4823 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4824 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4827 return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4832 GST_DEBUG_OBJECT (pad, "pad is flushing");
4833 return GST_FLOW_FLUSHING;
4837 GST_DEBUG_OBJECT (pad, "pad is EOS");
4838 return GST_FLOW_EOS;
4843 * gst_pad_store_sticky_event:
4845 * @event: a #GstEvent
4847 * Store the sticky @event on @pad
4849 * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
4850 * was flushing or #GST_FLOW_EOS when the pad was EOS.
4855 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4859 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4860 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4862 GST_OBJECT_LOCK (pad);
4863 ret = store_sticky_event (pad, event);
4864 GST_OBJECT_UNLOCK (pad);
4870 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
4872 PushStickyData *data = user_data;
4874 /* Forward all sticky events before our current one that are pending */
4875 if (ev->event != data->event
4876 && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
4877 return push_sticky (pad, ev, data);
4882 /* should be called with pad LOCK */
4883 static GstFlowReturn
4884 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4885 GstPadProbeType type)
4889 GstEventType event_type;
4891 /* pass the adjusted event on. We need to do this even if
4892 * there is no peer pad because of the probes. */
4893 event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
4895 /* Two checks to be made:
4896 * . (un)set the FLUSHING flag for flushing events,
4897 * . handle pad blocking */
4898 event_type = GST_EVENT_TYPE (event);
4899 switch (event_type) {
4900 case GST_EVENT_FLUSH_START:
4901 GST_PAD_SET_FLUSHING (pad);
4903 GST_PAD_BLOCK_BROADCAST (pad);
4904 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4906 case GST_EVENT_FLUSH_STOP:
4907 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
4910 GST_PAD_UNSET_FLUSHING (pad);
4912 /* Remove sticky EOS events */
4913 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4914 remove_event_by_type (pad, GST_EVENT_EOS);
4915 remove_event_by_type (pad, GST_EVENT_SEGMENT);
4916 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4917 pad->ABI.abi.last_flowret = GST_FLOW_OK;
4919 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4923 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4926 /* No need to check for EOS here as either the caller (gst_pad_push_event())
4927 * checked already or this is called as part of pushing sticky events,
4928 * in which case we still want to forward the EOS event downstream.
4931 switch (GST_EVENT_TYPE (event)) {
4932 case GST_EVENT_RECONFIGURE:
4933 if (GST_PAD_IS_SINK (pad))
4934 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4939 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4940 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4945 /* send probes after modifying the events above */
4946 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4948 /* recheck sticky events because the probe might have cause a relink */
4949 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
4950 && (GST_EVENT_IS_SERIALIZED (event)
4951 || GST_EVENT_IS_STICKY (event))) {
4952 PushStickyData data = { GST_FLOW_OK, FALSE, event };
4953 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4955 /* Push all sticky events before our current one
4956 * that have changed */
4957 events_foreach (pad, sticky_changed, &data);
4960 /* now check the peer pad */
4961 peerpad = GST_PAD_PEER (pad);
4962 if (peerpad == NULL)
4965 gst_object_ref (peerpad);
4967 GST_OBJECT_UNLOCK (pad);
4969 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4970 event, gst_event_type_get_name (event_type), peerpad);
4972 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4974 /* Note: we gave away ownership of the event at this point but we can still
4975 * print the old pointer */
4976 GST_LOG_OBJECT (pad,
4977 "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
4978 gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
4980 gst_object_unref (peerpad);
4982 GST_OBJECT_LOCK (pad);
4984 if (pad->priv->using == 0) {
4985 /* pad is not active anymore, trigger idle callbacks */
4986 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4987 idle_probe_stopped, ret);
4991 /* ERROR handling */
4994 GST_DEBUG_OBJECT (pad, "We're flushing");
4995 gst_event_unref (event);
4996 return GST_FLOW_FLUSHING;
5000 GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5001 gst_event_unref (event);
5002 return GST_FLOW_FLUSHING;
5006 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5007 gst_event_unref (event);
5010 case GST_FLOW_CUSTOM_SUCCESS:
5011 GST_DEBUG_OBJECT (pad, "dropped event");
5014 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5021 GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5022 gst_event_type_get_name (GST_EVENT_TYPE (event)));
5023 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5024 gst_event_unref (event);
5026 /* unlinked pads should not influence latency configuration */
5027 if (event_type == GST_EVENT_LATENCY)
5030 return GST_FLOW_NOT_LINKED;
5034 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5040 * gst_pad_push_event:
5041 * @pad: a #GstPad to push the event to.
5042 * @event: (transfer full): the #GstEvent to send to the pad.
5044 * Sends the event to the peer of the given pad. This function is
5045 * mainly used by elements to send events to their peer
5048 * This function takes ownership of the provided event so you should
5049 * gst_event_ref() it if you want to reuse the event after this call.
5051 * Returns: %TRUE if the event was handled.
5056 gst_pad_push_event (GstPad * pad, GstEvent * event)
5058 gboolean res = FALSE;
5059 GstPadProbeType type;
5060 gboolean sticky, serialized;
5062 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5063 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5065 if (GST_PAD_IS_SRC (pad)) {
5066 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5067 goto wrong_direction;
5068 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5069 } else if (GST_PAD_IS_SINK (pad)) {
5070 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5071 goto wrong_direction;
5072 /* events pushed on sinkpad never are sticky */
5073 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5075 goto unknown_direction;
5077 GST_OBJECT_LOCK (pad);
5078 sticky = GST_EVENT_IS_STICKY (event);
5079 serialized = GST_EVENT_IS_SERIALIZED (event);
5082 /* srcpad sticky events are stored immediately, the received flag is set
5083 * to FALSE and will be set to TRUE when we can successfully push the
5084 * event to the peer pad */
5085 switch (store_sticky_event (pad, event)) {
5086 case GST_FLOW_FLUSHING:
5094 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5095 /* all serialized or sticky events on the srcpad trigger push of
5097 res = (check_sticky (pad, event) == GST_FLOW_OK);
5102 /* other events are pushed right away */
5103 ret = gst_pad_push_event_unchecked (pad, event, type);
5104 /* dropped events by a probe are not an error */
5105 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
5107 /* Errors in sticky event pushing are no problem and ignored here
5108 * as they will cause more meaningful errors during data flow.
5109 * For EOS events, that are not followed by data flow, we still
5110 * return FALSE here though.
5112 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5114 gst_event_unref (event);
5116 GST_OBJECT_UNLOCK (pad);
5120 /* ERROR handling */
5123 g_warning ("pad %s:%s pushing %s event in wrong direction",
5124 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5125 gst_event_unref (event);
5130 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5131 gst_event_unref (event);
5136 GST_DEBUG_OBJECT (pad, "We're flushing");
5137 GST_OBJECT_UNLOCK (pad);
5138 gst_event_unref (event);
5143 GST_DEBUG_OBJECT (pad, "We're EOS");
5144 GST_OBJECT_UNLOCK (pad);
5145 gst_event_unref (event);
5150 /* Check if we can call the event function with the given event */
5151 static GstFlowReturn
5152 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5156 switch (GST_EVENT_TYPE (event)) {
5157 case GST_EVENT_CAPS:
5159 /* backwards compatibility mode for caps */
5160 gst_event_parse_caps (event, &caps);
5162 if (!gst_pad_query_accept_caps (pad, caps))
5174 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5175 "caps %" GST_PTR_FORMAT " not accepted", caps);
5176 return GST_FLOW_NOT_NEGOTIATED;
5180 static GstFlowReturn
5181 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5182 GstPadProbeType type)
5185 GstEventType event_type;
5186 gboolean serialized, need_unlock = FALSE, sticky;
5187 GstPadEventFunction eventfunc;
5190 GST_OBJECT_LOCK (pad);
5192 event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5194 if (GST_PAD_IS_SINK (pad))
5195 serialized = GST_EVENT_IS_SERIALIZED (event);
5198 sticky = GST_EVENT_IS_STICKY (event);
5199 event_type = GST_EVENT_TYPE (event);
5200 switch (event_type) {
5201 case GST_EVENT_FLUSH_START:
5202 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5203 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5205 /* can't even accept a flush begin event when flushing */
5206 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5209 GST_PAD_SET_FLUSHING (pad);
5210 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5212 case GST_EVENT_FLUSH_STOP:
5213 /* we can't accept flush-stop on inactive pads else the flushing flag
5214 * would be cleared and it would look like the pad can accept data.
5215 * Also, some elements restart a streaming thread in flush-stop which we
5216 * can't allow on inactive pads */
5217 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5220 GST_PAD_UNSET_FLUSHING (pad);
5221 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5222 /* Remove pending EOS events */
5223 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5224 remove_event_by_type (pad, GST_EVENT_EOS);
5225 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5226 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5227 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5229 GST_OBJECT_UNLOCK (pad);
5230 /* grab stream lock */
5231 GST_PAD_STREAM_LOCK (pad);
5233 GST_OBJECT_LOCK (pad);
5234 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5237 case GST_EVENT_RECONFIGURE:
5238 if (GST_PAD_IS_SRC (pad))
5239 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5241 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5242 "have event type %" GST_PTR_FORMAT, event);
5244 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5248 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5251 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5252 GST_OBJECT_UNLOCK (pad);
5253 GST_PAD_STREAM_LOCK (pad);
5255 GST_OBJECT_LOCK (pad);
5256 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5259 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5265 /* now do the probe */
5267 type | GST_PAD_PROBE_TYPE_PUSH |
5268 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5270 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5272 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5275 ACQUIRE_PARENT (pad, parent, no_parent);
5276 GST_OBJECT_UNLOCK (pad);
5278 ret = pre_eventfunc_check (pad, event);
5279 if (G_UNLIKELY (ret != GST_FLOW_OK))
5280 goto precheck_failed;
5283 gst_event_ref (event);
5285 if (eventfunc (pad, parent, event)) {
5288 /* something went wrong */
5289 switch (event_type) {
5290 case GST_EVENT_CAPS:
5291 ret = GST_FLOW_NOT_NEGOTIATED;
5294 ret = GST_FLOW_ERROR;
5298 RELEASE_PARENT (parent);
5300 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5303 if (ret == GST_FLOW_OK) {
5304 GST_OBJECT_LOCK (pad);
5305 /* after the event function accepted the event, we can store the sticky
5306 * event on the pad */
5307 switch (store_sticky_event (pad, event)) {
5308 case GST_FLOW_FLUSHING:
5315 GST_OBJECT_UNLOCK (pad);
5317 gst_event_unref (event);
5321 GST_PAD_STREAM_UNLOCK (pad);
5325 /* ERROR handling */
5328 GST_OBJECT_UNLOCK (pad);
5330 GST_PAD_STREAM_UNLOCK (pad);
5331 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5332 "Received event on flushing pad. Discarding");
5333 gst_event_unref (event);
5334 return GST_FLOW_FLUSHING;
5338 GST_OBJECT_UNLOCK (pad);
5340 GST_PAD_STREAM_UNLOCK (pad);
5341 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5342 "Received flush-stop on inactive pad. Discarding");
5343 gst_event_unref (event);
5344 return GST_FLOW_FLUSHING;
5348 GST_OBJECT_UNLOCK (pad);
5350 GST_PAD_STREAM_UNLOCK (pad);
5351 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5352 "Received event on EOS pad. Discarding");
5353 gst_event_unref (event);
5354 return GST_FLOW_EOS;
5358 GST_OBJECT_UNLOCK (pad);
5360 GST_PAD_STREAM_UNLOCK (pad);
5361 gst_event_unref (event);
5364 case GST_FLOW_CUSTOM_SUCCESS:
5365 GST_DEBUG_OBJECT (pad, "dropped event");
5369 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5376 g_warning ("pad %s:%s has no event handler, file a bug.",
5377 GST_DEBUG_PAD_NAME (pad));
5378 GST_OBJECT_UNLOCK (pad);
5380 GST_PAD_STREAM_UNLOCK (pad);
5381 gst_event_unref (event);
5382 return GST_FLOW_NOT_SUPPORTED;
5386 GST_DEBUG_OBJECT (pad, "no parent");
5387 GST_OBJECT_UNLOCK (pad);
5389 GST_PAD_STREAM_UNLOCK (pad);
5390 gst_event_unref (event);
5391 return GST_FLOW_FLUSHING;
5395 GST_DEBUG_OBJECT (pad, "pre event check failed");
5396 RELEASE_PARENT (parent);
5398 GST_PAD_STREAM_UNLOCK (pad);
5399 gst_event_unref (event);
5405 * gst_pad_send_event:
5406 * @pad: a #GstPad to send the event to.
5407 * @event: (transfer full): the #GstEvent to send to the pad.
5409 * Sends the event to the pad. This function can be used
5410 * by applications to send events in the pipeline.
5412 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5413 * sink pad, @event should be a downstream event. For example, you would not
5414 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5415 * Furthermore, some downstream events have to be serialized with data flow,
5416 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5417 * the event needs to be serialized with data flow, this function will take the
5418 * pad's stream lock while calling its event function.
5420 * To find out whether an event type is upstream, downstream, or downstream and
5421 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5422 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5423 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5424 * plugin doesn't need to bother itself with this information; the core handles
5425 * all necessary locks and checks.
5427 * This function takes ownership of the provided event so you should
5428 * gst_event_ref() it if you want to reuse the event after this call.
5430 * Returns: %TRUE if the event was handled.
5433 gst_pad_send_event (GstPad * pad, GstEvent * event)
5436 GstPadProbeType type;
5438 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5439 g_return_val_if_fail (event != NULL, FALSE);
5441 if (GST_PAD_IS_SINK (pad)) {
5442 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5443 goto wrong_direction;
5444 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5445 } else if (GST_PAD_IS_SRC (pad)) {
5446 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5447 goto wrong_direction;
5448 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5450 goto unknown_direction;
5452 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5459 /* ERROR handling */
5462 g_warning ("pad %s:%s sending %s event in wrong direction",
5463 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5464 gst_event_unref (event);
5469 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5470 gst_event_unref (event);
5476 * gst_pad_set_element_private:
5477 * @pad: the #GstPad to set the private data of.
5478 * @priv: The private data to attach to the pad.
5480 * Set the given private data gpointer on the pad.
5481 * This function can only be used by the element that owns the pad.
5482 * No locking is performed in this function.
5485 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5487 pad->element_private = priv;
5491 * gst_pad_get_element_private:
5492 * @pad: the #GstPad to get the private data of.
5494 * Gets the private data of a pad.
5495 * No locking is performed in this function.
5497 * Returns: (transfer none): a #gpointer to the private data.
5500 gst_pad_get_element_private (GstPad * pad)
5502 return pad->element_private;
5506 * gst_pad_get_sticky_event:
5507 * @pad: the #GstPad to get the event from.
5508 * @event_type: the #GstEventType that should be retrieved.
5509 * @idx: the index of the event
5511 * Returns a new reference of the sticky event of type @event_type
5514 * Returns: (transfer full) (nullable): a #GstEvent of type
5515 * @event_type or %NULL when no event of @event_type was on
5516 * @pad. Unref after usage.
5519 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5521 GstEvent *event = NULL;
5524 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5525 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5527 GST_OBJECT_LOCK (pad);
5528 ev = find_event_by_type (pad, event_type, idx);
5529 if (ev && (event = ev->event))
5530 gst_event_ref (event);
5531 GST_OBJECT_UNLOCK (pad);
5538 GstPadStickyEventsForeachFunction func;
5543 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5545 ForeachDispatch *data = user_data;
5546 gboolean ret = TRUE;
5549 GST_OBJECT_UNLOCK (pad);
5551 ret = data->func (pad, &ev->event, data->user_data);
5553 GST_OBJECT_LOCK (pad);
5560 * gst_pad_sticky_events_foreach:
5561 * @pad: the #GstPad that should be used for iteration.
5562 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5563 * should be called for every event.
5564 * @user_data: (closure): the optional user data.
5566 * Iterates all sticky events on @pad and calls @foreach_func for every
5567 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5570 gst_pad_sticky_events_foreach (GstPad * pad,
5571 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5573 ForeachDispatch data;
5575 g_return_if_fail (GST_IS_PAD (pad));
5576 g_return_if_fail (foreach_func != NULL);
5578 data.func = foreach_func;
5579 data.user_data = user_data;
5581 GST_OBJECT_LOCK (pad);
5582 events_foreach (pad, foreach_dispatch_function, &data);
5583 GST_OBJECT_UNLOCK (pad);
5587 do_stream_status (GstPad * pad, GstStreamStatusType type,
5588 GThread * thread, GstTask * task)
5592 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5594 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5595 if (GST_IS_ELEMENT (parent)) {
5596 GstMessage *message;
5597 GValue value = { 0 };
5599 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5600 gchar *tname, *ename, *pname;
5602 /* create a good task name */
5603 ename = gst_element_get_name (parent);
5604 pname = gst_pad_get_name (pad);
5605 tname = g_strdup_printf ("%s:%s", ename, pname);
5609 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5613 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5616 g_value_init (&value, GST_TYPE_TASK);
5617 g_value_set_object (&value, task);
5618 gst_message_set_stream_status_object (message, &value);
5619 g_value_unset (&value);
5621 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5622 gst_element_post_message (parent, message);
5624 gst_object_unref (parent);
5629 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5631 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5636 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5638 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5643 * gst_pad_start_task:
5644 * @pad: the #GstPad to start the task of
5645 * @func: the task function to call
5646 * @user_data: user data passed to the task function
5647 * @notify: called when @user_data is no longer referenced
5649 * Starts a task that repeatedly calls @func with @user_data. This function
5650 * is mostly used in pad activation functions to start the dataflow.
5651 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5652 * before @func is called.
5654 * Returns: a %TRUE if the task could be started.
5657 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5658 GDestroyNotify notify)
5663 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5664 g_return_val_if_fail (func != NULL, FALSE);
5666 GST_DEBUG_OBJECT (pad, "start task");
5668 GST_OBJECT_LOCK (pad);
5669 task = GST_PAD_TASK (pad);
5671 task = gst_task_new (func, user_data, notify);
5672 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5673 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5674 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5675 GST_INFO_OBJECT (pad, "created task %p", task);
5676 GST_PAD_TASK (pad) = task;
5677 gst_object_ref (task);
5678 /* release lock to post the message */
5679 GST_OBJECT_UNLOCK (pad);
5681 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5683 gst_object_unref (task);
5685 GST_OBJECT_LOCK (pad);
5686 /* nobody else is supposed to have changed the pad now */
5687 if (GST_PAD_TASK (pad) != task)
5688 goto concurrent_stop;
5690 res = gst_task_set_state (task, GST_TASK_STARTED);
5691 GST_OBJECT_UNLOCK (pad);
5698 GST_OBJECT_UNLOCK (pad);
5704 * gst_pad_pause_task:
5705 * @pad: the #GstPad to pause the task of
5707 * Pause the task of @pad. This function will also wait until the
5708 * function executed by the task is finished if this function is not
5709 * called from the task function.
5711 * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5715 gst_pad_pause_task (GstPad * pad)
5720 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5722 GST_DEBUG_OBJECT (pad, "pause task");
5724 GST_OBJECT_LOCK (pad);
5725 task = GST_PAD_TASK (pad);
5728 res = gst_task_set_state (task, GST_TASK_PAUSED);
5729 GST_OBJECT_UNLOCK (pad);
5731 /* wait for task function to finish, this lock is recursive so it does nothing
5732 * when the pause is called from the task itself */
5733 GST_PAD_STREAM_LOCK (pad);
5734 GST_PAD_STREAM_UNLOCK (pad);
5740 GST_DEBUG_OBJECT (pad, "pad has no task");
5741 GST_OBJECT_UNLOCK (pad);
5747 * gst_pad_stop_task:
5748 * @pad: the #GstPad to stop the task of
5750 * Stop the task of @pad. This function will also make sure that the
5751 * function executed by the task will effectively stop if not called
5752 * from the GstTaskFunction.
5754 * This function will deadlock if called from the GstTaskFunction of
5755 * the task. Use gst_task_pause() instead.
5757 * Regardless of whether the pad has a task, the stream lock is acquired and
5758 * released so as to ensure that streaming through this pad has finished.
5760 * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5763 gst_pad_stop_task (GstPad * pad)
5768 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5770 GST_DEBUG_OBJECT (pad, "stop task");
5772 GST_OBJECT_LOCK (pad);
5773 task = GST_PAD_TASK (pad);
5776 GST_PAD_TASK (pad) = NULL;
5777 res = gst_task_set_state (task, GST_TASK_STOPPED);
5778 GST_OBJECT_UNLOCK (pad);
5780 GST_PAD_STREAM_LOCK (pad);
5781 GST_PAD_STREAM_UNLOCK (pad);
5783 if (!gst_task_join (task))
5786 gst_object_unref (task);
5792 GST_DEBUG_OBJECT (pad, "no task");
5793 GST_OBJECT_UNLOCK (pad);
5795 GST_PAD_STREAM_LOCK (pad);
5796 GST_PAD_STREAM_UNLOCK (pad);
5798 /* this is not an error */
5803 /* this is bad, possibly the application tried to join the task from
5804 * the task's thread. We install the task again so that it will be stopped
5805 * again from the right thread next time hopefully. */
5806 GST_OBJECT_LOCK (pad);
5807 GST_DEBUG_OBJECT (pad, "join failed");
5808 /* we can only install this task if there was no other task */
5809 if (GST_PAD_TASK (pad) == NULL)
5810 GST_PAD_TASK (pad) = task;
5811 GST_OBJECT_UNLOCK (pad);
5818 * gst_pad_probe_info_get_event:
5819 * @info: a #GstPadProbeInfo
5821 * Returns: (transfer none): The #GstEvent from the probe
5825 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5827 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5828 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5830 return GST_PAD_PROBE_INFO_EVENT (info);
5835 * gst_pad_probe_info_get_query:
5836 * @info: a #GstPadProbeInfo
5838 * Returns: (transfer none): The #GstQuery from the probe
5842 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5844 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5845 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5847 return GST_PAD_PROBE_INFO_QUERY (info);
5851 * gst_pad_probe_info_get_buffer:
5852 * @info: a #GstPadProbeInfo
5854 * Returns: (transfer none): The #GstBuffer from the probe
5858 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5860 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5862 return GST_PAD_PROBE_INFO_BUFFER (info);
5866 * gst_pad_probe_info_get_bufferlist:
5867 * @info: a #GstPadProbeInfo
5869 * Returns: (transfer none): The #GstBufferlist from the probe
5873 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5875 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5877 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5881 * gst_pad_get_last_flow_return:
5884 * Gets the #GstFlowReturn return from the last data passed by this pad.
5889 gst_pad_get_last_flow_return (GstPad * pad)
5893 GST_OBJECT_LOCK (pad);
5894 ret = GST_PAD_LAST_FLOW_RETURN (pad);
5895 GST_OBJECT_UNLOCK (pad);