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;
144 /* counter of how many idle probes are running directly from the add_probe
145 * call. Used to block any data flowing in the pad while the idle callback
146 * Doesn't finish its work */
156 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
157 #define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
158 (((GstPad *)(p))->priv->idle_running > 0)
163 GstPadProbeInfo *info;
170 static void gst_pad_dispose (GObject * object);
171 static void gst_pad_finalize (GObject * object);
172 static void gst_pad_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec);
174 static void gst_pad_get_property (GObject * object, guint prop_id,
175 GValue * value, GParamSpec * pspec);
177 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
178 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
179 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
180 GstObject * parent, GstBufferList * list);
182 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
183 GstEvent * event, GstPadProbeType type);
184 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
185 GstEvent * event, GstPadProbeType type);
187 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
189 static GParamSpec *pspec_caps = NULL;
191 /* quarks for probe signals */
192 static GQuark buffer_quark;
193 static GQuark buffer_list_quark;
194 static GQuark event_quark;
203 static GstFlowQuarks flow_quarks[] = {
204 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
205 {GST_FLOW_OK, "ok", 0},
206 {GST_FLOW_NOT_LINKED, "not-linked", 0},
207 {GST_FLOW_FLUSHING, "flushing", 0},
208 {GST_FLOW_EOS, "eos", 0},
209 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
210 {GST_FLOW_ERROR, "error", 0},
211 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
212 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
217 * @ret: a #GstFlowReturn to get the name of.
219 * Gets a string representing the given flow return.
221 * Returns: a static string with the name of the flow return.
224 gst_flow_get_name (GstFlowReturn ret)
228 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
230 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
231 if (ret == flow_quarks[i].ret)
232 return flow_quarks[i].name;
239 * @ret: a #GstFlowReturn to get the quark of.
241 * Get the unique quark for the given GstFlowReturn.
243 * Returns: the quark associated with the flow return or 0 if an
244 * invalid return was specified.
247 gst_flow_to_quark (GstFlowReturn ret)
251 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
253 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
254 if (ret == flow_quarks[i].ret)
255 return flow_quarks[i].quark;
261 * gst_pad_link_get_name:
262 * @ret: a #GstPadLinkReturn to get the name of.
264 * Gets a string representing the given pad-link return.
266 * Returns: a static string with the name of the pad-link return.
271 gst_pad_link_get_name (GstPadLinkReturn ret)
274 case GST_PAD_LINK_OK:
276 case GST_PAD_LINK_WRONG_HIERARCHY:
277 return "wrong hierarchy";
278 case GST_PAD_LINK_WAS_LINKED:
280 case GST_PAD_LINK_WRONG_DIRECTION:
281 return "wrong direction";
282 case GST_PAD_LINK_NOFORMAT:
283 return "no common format";
284 case GST_PAD_LINK_NOSCHED:
285 return "incompatible scheduling";
286 case GST_PAD_LINK_REFUSED:
289 g_return_val_if_reached ("unknown");
296 buffer_quark = g_quark_from_static_string ("buffer"); \
297 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
298 event_quark = g_quark_from_static_string ("event"); \
300 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
301 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
304 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
305 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
308 #define gst_pad_parent_class parent_class
309 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
312 gst_pad_class_init (GstPadClass * klass)
314 GObjectClass *gobject_class;
315 GstObjectClass *gstobject_class;
317 gobject_class = G_OBJECT_CLASS (klass);
318 gstobject_class = GST_OBJECT_CLASS (klass);
320 g_type_class_add_private (klass, sizeof (GstPadPrivate));
322 gobject_class->dispose = gst_pad_dispose;
323 gobject_class->finalize = gst_pad_finalize;
324 gobject_class->set_property = gst_pad_set_property;
325 gobject_class->get_property = gst_pad_get_property;
329 * @pad: the pad that emitted the signal
330 * @peer: the peer pad that has been connected
332 * Signals that a pad has been linked to the peer pad.
334 gst_pad_signals[PAD_LINKED] =
335 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
336 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
337 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
340 * @pad: the pad that emitted the signal
341 * @peer: the peer pad that has been disconnected
343 * Signals that a pad has been unlinked from the peer pad.
345 gst_pad_signals[PAD_UNLINKED] =
346 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
347 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
348 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
350 pspec_caps = g_param_spec_boxed ("caps", "Caps",
351 "The capabilities of the pad", GST_TYPE_CAPS,
352 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
353 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
355 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
356 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
357 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
358 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
360 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
361 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
362 g_param_spec_object ("template", "Template",
363 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 * The offset that will be applied to the running time of the pad.
373 g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
374 g_param_spec_int64 ("offset", "Offset",
375 "The running time offset of the pad", 0, G_MAXINT64, 0,
376 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 gstobject_class->path_string_separator = ".";
380 /* Register common function pointer descriptions */
381 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
382 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
383 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
384 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
385 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
389 gst_pad_init (GstPad * pad)
391 pad->priv = GST_PAD_GET_PRIVATE (pad);
393 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
395 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
396 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
397 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
398 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
399 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
401 GST_PAD_SET_FLUSHING (pad);
403 g_rec_mutex_init (&pad->stream_rec_lock);
405 g_cond_init (&pad->block_cond);
407 g_hook_list_init (&pad->probes, sizeof (GstProbe));
409 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
410 pad->priv->events_cookie = 0;
411 pad->priv->last_cookie = -1;
412 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
415 /* called when setting the pad inactive. It removes all sticky events from
416 * the pad. must be called with object lock */
418 remove_events (GstPad * pad)
422 gboolean notify = FALSE;
424 events = pad->priv->events;
427 for (i = 0; i < len; i++) {
428 PadEvent *ev = &g_array_index (events, PadEvent, i);
429 GstEvent *event = ev->event;
433 if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
436 gst_event_unref (event);
439 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
440 g_array_set_size (events, 0);
441 pad->priv->events_cookie++;
444 GST_OBJECT_UNLOCK (pad);
446 GST_DEBUG_OBJECT (pad, "notify caps");
447 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
449 GST_OBJECT_LOCK (pad);
453 /* should be called with object lock */
455 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
461 events = pad->priv->events;
464 for (i = 0; i < len; i++) {
465 ev = &g_array_index (events, PadEvent, i);
466 if (ev->event == NULL)
469 if (GST_EVENT_TYPE (ev->event) == type) {
480 /* should be called with OBJECT lock */
482 find_event (GstPad * pad, GstEvent * event)
488 events = pad->priv->events;
491 for (i = 0; i < len; i++) {
492 ev = &g_array_index (events, PadEvent, i);
493 if (event == ev->event)
501 /* should be called with OBJECT lock */
503 remove_event_by_type (GstPad * pad, GstEventType type)
509 events = pad->priv->events;
514 ev = &g_array_index (events, PadEvent, i);
515 if (ev->event == NULL)
518 if (GST_EVENT_TYPE (ev->event) != type)
521 gst_event_unref (ev->event);
522 g_array_remove_index (events, i);
524 pad->priv->events_cookie++;
532 /* check all events on srcpad against those on sinkpad. All events that are not
533 * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
534 * srcpad so that the events will be sent next time */
535 /* should be called with srcpad and sinkpad LOCKS */
537 schedule_events (GstPad * srcpad, GstPad * sinkpad)
542 gboolean pending = FALSE;
544 events = srcpad->priv->events;
547 for (i = 0; i < len; i++) {
548 ev = &g_array_index (events, PadEvent, i);
549 if (ev->event == NULL)
552 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
553 ev->received = FALSE;
558 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
561 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
564 /* should be called with pad LOCK */
566 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
573 events = pad->priv->events;
576 cookie = pad->priv->events_cookie;
580 PadEvent *ev, ev_ret;
582 ev = &g_array_index (events, PadEvent, i);
583 if (G_UNLIKELY (ev->event == NULL))
586 /* take aditional ref, func might release the lock */
587 ev_ret.event = gst_event_ref (ev->event);
588 ev_ret.received = ev->received;
590 ret = func (pad, &ev_ret, user_data);
592 /* recheck the cookie, lock might have been released and the list could have
594 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
595 if (G_LIKELY (ev_ret.event))
596 gst_event_unref (ev_ret.event);
600 /* store the received state */
601 ev->received = ev_ret.received;
603 /* if the event changed, we need to do something */
604 if (G_UNLIKELY (ev->event != ev_ret.event)) {
605 if (G_UNLIKELY (ev_ret.event == NULL)) {
606 /* function unreffed and set the event to NULL, remove it */
607 gst_event_unref (ev->event);
608 g_array_remove_index (events, i);
610 cookie = ++pad->priv->events_cookie;
613 /* function gave a new event for us */
614 gst_event_take (&ev->event, ev_ret.event);
617 /* just unref, nothing changed */
618 gst_event_unref (ev_ret.event);
627 /* should be called with LOCK */
629 _apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
633 GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_TIME_FORMAT,
634 GST_TIME_ARGS (pad->offset));
636 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
639 g_assert (!upstream);
641 /* copy segment values */
642 gst_event_copy_segment (event, &segment);
643 gst_event_unref (event);
645 gst_segment_offset_running_time (&segment, segment.format, pad->offset);
646 event = gst_event_new_segment (&segment);
649 event = gst_event_make_writable (event);
650 offset = gst_event_get_running_time_offset (event);
652 offset -= pad->offset;
654 offset += pad->offset;
655 gst_event_set_running_time_offset (event, offset);
660 static inline GstEvent *
661 apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
663 if (G_UNLIKELY (pad->offset != 0))
664 return _apply_pad_offset (pad, event, upstream);
669 /* should be called with the OBJECT_LOCK */
671 get_pad_caps (GstPad * pad)
673 GstCaps *caps = NULL;
676 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
678 gst_event_parse_caps (ev->event, &caps);
684 gst_pad_dispose (GObject * object)
686 GstPad *pad = GST_PAD_CAST (object);
689 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
691 /* unlink the peer pad */
692 if ((peer = gst_pad_get_peer (pad))) {
693 /* window for MT unsafeness, someone else could unlink here
694 * and then we call unlink with wrong pads. The unlink
695 * function would catch this and safely return failed. */
696 if (GST_PAD_IS_SRC (pad))
697 gst_pad_unlink (pad, peer);
699 gst_pad_unlink (peer, pad);
701 gst_object_unref (peer);
704 gst_pad_set_pad_template (pad, NULL);
706 GST_OBJECT_LOCK (pad);
708 GST_OBJECT_UNLOCK (pad);
710 g_hook_list_clear (&pad->probes);
712 G_OBJECT_CLASS (parent_class)->dispose (object);
716 gst_pad_finalize (GObject * object)
718 GstPad *pad = GST_PAD_CAST (object);
721 /* in case the task is still around, clean it up */
722 if ((task = GST_PAD_TASK (pad))) {
723 gst_task_join (task);
724 GST_PAD_TASK (pad) = NULL;
725 gst_object_unref (task);
728 if (pad->activatenotify)
729 pad->activatenotify (pad->activatedata);
730 if (pad->activatemodenotify)
731 pad->activatemodenotify (pad->activatemodedata);
733 pad->linknotify (pad->linkdata);
734 if (pad->unlinknotify)
735 pad->unlinknotify (pad->unlinkdata);
736 if (pad->chainnotify)
737 pad->chainnotify (pad->chaindata);
738 if (pad->chainlistnotify)
739 pad->chainlistnotify (pad->chainlistdata);
740 if (pad->getrangenotify)
741 pad->getrangenotify (pad->getrangedata);
742 if (pad->eventnotify)
743 pad->eventnotify (pad->eventdata);
744 if (pad->querynotify)
745 pad->querynotify (pad->querydata);
746 if (pad->iterintlinknotify)
747 pad->iterintlinknotify (pad->iterintlinkdata);
749 g_rec_mutex_clear (&pad->stream_rec_lock);
750 g_cond_clear (&pad->block_cond);
751 g_array_free (pad->priv->events, TRUE);
753 G_OBJECT_CLASS (parent_class)->finalize (object);
757 gst_pad_set_property (GObject * object, guint prop_id,
758 const GValue * value, GParamSpec * pspec)
760 g_return_if_fail (GST_IS_PAD (object));
763 case PAD_PROP_DIRECTION:
764 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
766 case PAD_PROP_TEMPLATE:
767 gst_pad_set_pad_template (GST_PAD_CAST (object),
768 (GstPadTemplate *) g_value_get_object (value));
770 case PAD_PROP_OFFSET:
771 gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
774 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
780 gst_pad_get_property (GObject * object, guint prop_id,
781 GValue * value, GParamSpec * pspec)
783 g_return_if_fail (GST_IS_PAD (object));
787 GST_OBJECT_LOCK (object);
788 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
789 GST_OBJECT_UNLOCK (object);
791 case PAD_PROP_DIRECTION:
792 g_value_set_enum (value, GST_PAD_DIRECTION (object));
794 case PAD_PROP_TEMPLATE:
795 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
797 case PAD_PROP_OFFSET:
798 g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
801 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
808 * @name: (allow-none): the name of the new pad.
809 * @direction: the #GstPadDirection of the pad.
811 * Creates a new pad with the given name in the given direction.
812 * If name is %NULL, a guaranteed unique name (across all pads)
814 * This function makes a copy of the name so you can safely free the name.
816 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
822 gst_pad_new (const gchar * name, GstPadDirection direction)
824 return g_object_new (GST_TYPE_PAD,
825 "name", name, "direction", direction, NULL);
829 * gst_pad_new_from_template:
830 * @templ: the pad template to use
831 * @name: (allow-none): the name of the pad
833 * Creates a new pad with the given name from the given template.
834 * If name is %NULL, a guaranteed unique name (across all pads)
836 * This function makes a copy of the name so you can safely free the name.
838 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
842 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
844 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
846 return g_object_new (GST_TYPE_PAD,
847 "name", name, "direction", templ->direction, "template", templ, NULL);
851 * gst_pad_new_from_static_template:
852 * @templ: the #GstStaticPadTemplate to use
853 * @name: the name of the pad
855 * Creates a new pad with the given name from the given static template.
856 * If name is %NULL, a guaranteed unique name (across all pads)
858 * This function makes a copy of the name so you can safely free the name.
860 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
864 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
868 GstPadTemplate *template;
870 template = gst_static_pad_template_get (templ);
871 pad = gst_pad_new_from_template (template, name);
872 gst_object_unref (template);
876 #define ACQUIRE_PARENT(pad, parent, label) \
878 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
879 gst_object_ref (parent); \
880 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
884 #define RELEASE_PARENT(parent) \
886 if (G_LIKELY (parent)) \
887 gst_object_unref (parent); \
891 * gst_pad_get_direction:
892 * @pad: a #GstPad to get the direction of.
894 * Gets the direction of the pad. The direction of the pad is
895 * decided at construction time so this function does not take
898 * Returns: the #GstPadDirection of the pad.
903 gst_pad_get_direction (GstPad * pad)
905 GstPadDirection result;
907 /* PAD_UNKNOWN is a little silly but we need some sort of
908 * error return value */
909 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
911 result = GST_PAD_DIRECTION (pad);
917 gst_pad_activate_default (GstPad * pad, GstObject * parent)
919 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
923 * gst_pad_mode_get_name:
924 * @mode: the pad mode
926 * Return the name of a pad mode, for use in debug messages mostly.
928 * Returns: short mnemonic for pad mode @mode
931 gst_pad_mode_get_name (GstPadMode mode)
934 case GST_PAD_MODE_NONE:
936 case GST_PAD_MODE_PUSH:
938 case GST_PAD_MODE_PULL:
947 pre_activate (GstPad * pad, GstPadMode new_mode)
950 case GST_PAD_MODE_NONE:
951 GST_OBJECT_LOCK (pad);
952 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
953 GST_PAD_SET_FLUSHING (pad);
954 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
955 GST_PAD_MODE (pad) = new_mode;
956 /* unlock blocked pads so element can resume and stop */
957 GST_PAD_BLOCK_BROADCAST (pad);
958 GST_OBJECT_UNLOCK (pad);
960 case GST_PAD_MODE_PUSH:
961 case GST_PAD_MODE_PULL:
962 GST_OBJECT_LOCK (pad);
963 GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
964 gst_pad_mode_get_name (new_mode));
965 GST_PAD_UNSET_FLUSHING (pad);
966 pad->ABI.abi.last_flowret = GST_FLOW_OK;
967 GST_PAD_MODE (pad) = new_mode;
968 if (GST_PAD_IS_SINK (pad)) {
970 /* make sure the peer src pad sends us all events */
971 if ((peer = GST_PAD_PEER (pad))) {
972 gst_object_ref (peer);
973 GST_OBJECT_UNLOCK (pad);
975 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
976 GST_DEBUG_PAD_NAME (peer));
978 GST_OBJECT_LOCK (peer);
979 schedule_events (peer, NULL);
980 GST_OBJECT_UNLOCK (peer);
982 gst_object_unref (peer);
984 GST_OBJECT_UNLOCK (pad);
987 GST_OBJECT_UNLOCK (pad);
994 post_activate (GstPad * pad, GstPadMode new_mode)
997 case GST_PAD_MODE_NONE:
998 /* ensures that streaming stops */
999 GST_PAD_STREAM_LOCK (pad);
1000 GST_DEBUG_OBJECT (pad, "stopped streaming");
1001 GST_OBJECT_LOCK (pad);
1002 remove_events (pad);
1003 GST_OBJECT_UNLOCK (pad);
1004 GST_PAD_STREAM_UNLOCK (pad);
1006 case GST_PAD_MODE_PUSH:
1007 case GST_PAD_MODE_PULL:
1014 * gst_pad_set_active:
1015 * @pad: the #GstPad to activate or deactivate.
1016 * @active: whether or not the pad should be active.
1018 * Activates or deactivates the given pad.
1019 * Normally called from within core state change functions.
1021 * If @active, makes sure the pad is active. If it is already active, either in
1022 * push or pull mode, just return. Otherwise dispatches to the pad's activate
1023 * function to perform the actual activation.
1025 * If not @active, calls gst_pad_activate_mode() with the pad's current mode
1026 * and a %FALSE argument.
1028 * Returns: %TRUE if the operation was successful.
1033 gst_pad_set_active (GstPad * pad, gboolean active)
1037 gboolean ret = FALSE;
1039 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1041 GST_OBJECT_LOCK (pad);
1042 old = GST_PAD_MODE (pad);
1043 ACQUIRE_PARENT (pad, parent, no_parent);
1044 GST_OBJECT_UNLOCK (pad);
1047 if (old == GST_PAD_MODE_NONE) {
1048 GST_DEBUG_OBJECT (pad, "activating pad from none");
1049 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
1051 pad->ABI.abi.last_flowret = GST_FLOW_OK;
1053 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
1054 gst_pad_mode_get_name (old));
1058 if (old == GST_PAD_MODE_NONE) {
1059 GST_DEBUG_OBJECT (pad, "pad was inactive");
1062 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1063 gst_pad_mode_get_name (old));
1064 ret = gst_pad_activate_mode (pad, old, FALSE);
1066 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
1070 RELEASE_PARENT (parent);
1072 if (G_UNLIKELY (!ret))
1080 GST_DEBUG_OBJECT (pad, "no parent");
1081 GST_OBJECT_UNLOCK (pad);
1086 GST_OBJECT_LOCK (pad);
1088 g_critical ("Failed to deactivate pad %s:%s, very bad",
1089 GST_DEBUG_PAD_NAME (pad));
1091 GST_WARNING_OBJECT (pad, "Failed to activate pad");
1093 GST_OBJECT_UNLOCK (pad);
1099 * gst_pad_activate_mode:
1100 * @pad: the #GstPad to activate or deactivate.
1101 * @mode: the requested activation mode
1102 * @active: whether or not the pad should be active.
1104 * Activates or deactivates the given pad in @mode via dispatching to the
1105 * pad's activatemodefunc. For use from within pad activation functions only.
1107 * If you don't know what this is, you probably don't want to call it.
1109 * Returns: %TRUE if the operation was successful.
1114 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1116 gboolean res = FALSE;
1118 GstPadMode old, new;
1119 GstPadDirection dir;
1122 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1124 GST_OBJECT_LOCK (pad);
1125 old = GST_PAD_MODE (pad);
1126 dir = GST_PAD_DIRECTION (pad);
1127 ACQUIRE_PARENT (pad, parent, no_parent);
1128 GST_OBJECT_UNLOCK (pad);
1130 new = active ? mode : GST_PAD_MODE_NONE;
1135 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1136 /* pad was activate in the wrong direction, deactivate it
1137 * and reactivate it in the requested mode */
1138 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1139 gst_pad_mode_get_name (old));
1141 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
1142 goto deactivate_failed;
1146 case GST_PAD_MODE_PULL:
1148 if (dir == GST_PAD_SINK) {
1149 if ((peer = gst_pad_get_peer (pad))) {
1150 GST_DEBUG_OBJECT (pad, "calling peer");
1151 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1153 gst_object_unref (peer);
1155 /* there is no peer, this is only fatal when we activate. When we
1156 * deactivate, we must assume the application has unlinked the peer and
1157 * will deactivate it eventually. */
1161 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1164 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1165 goto failure; /* Can't activate pull on a src without a
1166 getrange function */
1174 /* Mark pad as needing reconfiguration */
1176 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1177 pre_activate (pad, new);
1179 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1180 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1184 /* can happen for sinks of passthrough elements */
1187 post_activate (pad, new);
1189 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1190 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1195 /* Clear sticky flags on deactivation */
1197 GST_OBJECT_LOCK (pad);
1198 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1199 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1200 GST_OBJECT_UNLOCK (pad);
1204 RELEASE_PARENT (parent);
1210 GST_DEBUG_OBJECT (pad, "no parent");
1211 GST_OBJECT_UNLOCK (pad);
1216 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1217 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1222 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1223 "failed to %s in switch to %s mode from %s mode",
1224 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1225 gst_pad_mode_get_name (old));
1230 GST_OBJECT_LOCK (peer);
1231 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1232 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1233 GST_OBJECT_UNLOCK (peer);
1234 gst_object_unref (peer);
1239 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1240 "pad in pull mode");
1245 GST_OBJECT_LOCK (pad);
1246 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1247 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1248 GST_PAD_SET_FLUSHING (pad);
1249 GST_PAD_MODE (pad) = old;
1250 GST_OBJECT_UNLOCK (pad);
1256 * gst_pad_is_active:
1257 * @pad: the #GstPad to query
1259 * Query if a pad is active
1261 * Returns: %TRUE if the pad is active.
1266 gst_pad_is_active (GstPad * pad)
1268 gboolean result = FALSE;
1270 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1272 GST_OBJECT_LOCK (pad);
1273 result = GST_PAD_IS_ACTIVE (pad);
1274 GST_OBJECT_UNLOCK (pad);
1280 cleanup_hook (GstPad * pad, GHook * hook)
1282 GstPadProbeType type;
1284 if (!G_HOOK_IS_VALID (hook))
1287 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1289 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1290 /* unblock when we remove the last blocking probe */
1292 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1295 /* Might have new probes now that want to be called */
1296 GST_PAD_BLOCK_BROADCAST (pad);
1298 if (pad->num_blocked == 0) {
1299 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1300 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1303 g_hook_destroy_link (&pad->probes, hook);
1308 * gst_pad_add_probe:
1309 * @pad: the #GstPad to add the probe to
1310 * @mask: the probe mask
1311 * @callback: #GstPadProbeCallback that will be called with notifications of
1313 * @user_data: (closure): user data passed to the callback
1314 * @destroy_data: #GDestroyNotify for user_data
1316 * Be notified of different states of pads. The provided callback is called for
1317 * every state that matches @mask.
1319 * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1320 * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1321 * happen that the probe can be run immediately and if the probe returns
1322 * GST_PAD_PROBE_REMOVE this functions returns 0.
1327 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1328 GstPadProbeCallback callback, gpointer user_data,
1329 GDestroyNotify destroy_data)
1334 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1335 g_return_val_if_fail (mask != 0, 0);
1337 GST_OBJECT_LOCK (pad);
1339 /* make a new probe */
1340 hook = g_hook_alloc (&pad->probes);
1342 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1345 /* when no contraints are given for the types, assume all types are
1347 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1348 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1349 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1350 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1352 /* store our flags and other fields */
1353 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1354 hook->func = callback;
1355 hook->data = user_data;
1356 hook->destroy = destroy_data;
1357 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1360 g_hook_prepend (&pad->probes, hook);
1362 /* incremenent cookie so that the new hook get's called */
1363 pad->priv->probe_list_cookie++;
1365 /* get the id of the hook, we return this and it can be used to remove the
1367 res = hook->hook_id;
1369 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1371 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1372 /* we have a block probe */
1374 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1375 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1376 "now %d blocking probes", pad->num_blocked);
1378 /* Might have new probes now that want to be called */
1379 GST_PAD_BLOCK_BROADCAST (pad);
1382 /* call the callback if we need to be called for idle callbacks */
1383 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1384 if (pad->priv->using > 0) {
1385 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1386 * flag above, the last thread to leave the push will do the callback. New
1387 * threads going into the push will block. */
1388 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1389 "pad is in use, delay idle callback");
1390 GST_OBJECT_UNLOCK (pad);
1392 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1393 GstPadProbeReturn ret;
1395 /* Keep another ref, the callback could destroy the pad */
1396 gst_object_ref (pad);
1397 pad->priv->idle_running++;
1399 /* the pad is idle now, we can signal the idle callback now */
1400 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1401 "pad is idle, trigger idle callback");
1402 GST_OBJECT_UNLOCK (pad);
1404 ret = callback (pad, &info, user_data);
1406 GST_OBJECT_LOCK (pad);
1408 case GST_PAD_PROBE_REMOVE:
1409 /* remove the probe */
1410 GST_DEBUG_OBJECT (pad, "asked to remove hook");
1411 cleanup_hook (pad, hook);
1414 case GST_PAD_PROBE_DROP:
1415 GST_DEBUG_OBJECT (pad, "asked to drop item");
1417 case GST_PAD_PROBE_PASS:
1418 GST_DEBUG_OBJECT (pad, "asked to pass item");
1420 case GST_PAD_PROBE_OK:
1421 GST_DEBUG_OBJECT (pad, "probe returned OK");
1424 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1427 pad->priv->idle_running--;
1428 if (pad->priv->idle_running == 0) {
1429 GST_PAD_BLOCK_BROADCAST (pad);
1431 GST_OBJECT_UNLOCK (pad);
1433 gst_object_unref (pad);
1436 GST_OBJECT_UNLOCK (pad);
1442 * gst_pad_remove_probe:
1443 * @pad: the #GstPad with the probe
1444 * @id: the probe id to remove
1446 * Remove the probe with @id from @pad.
1451 gst_pad_remove_probe (GstPad * pad, gulong id)
1455 g_return_if_fail (GST_IS_PAD (pad));
1457 GST_OBJECT_LOCK (pad);
1459 hook = g_hook_get (&pad->probes, id);
1463 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1465 cleanup_hook (pad, hook);
1466 GST_OBJECT_UNLOCK (pad);
1472 GST_OBJECT_UNLOCK (pad);
1473 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1479 * gst_pad_is_blocked:
1480 * @pad: the #GstPad to query
1482 * Checks if the pad is blocked or not. This function returns the
1483 * last requested state of the pad. It is not certain that the pad
1484 * is actually blocking at this point (see gst_pad_is_blocking()).
1486 * Returns: %TRUE if the pad is blocked.
1491 gst_pad_is_blocked (GstPad * pad)
1493 gboolean result = FALSE;
1495 g_return_val_if_fail (GST_IS_PAD (pad), result);
1497 GST_OBJECT_LOCK (pad);
1498 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1499 GST_OBJECT_UNLOCK (pad);
1505 * gst_pad_is_blocking:
1506 * @pad: the #GstPad to query
1508 * Checks if the pad is blocking or not. This is a guaranteed state
1509 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1511 * Returns: %TRUE if the pad is blocking.
1516 gst_pad_is_blocking (GstPad * pad)
1518 gboolean result = FALSE;
1520 g_return_val_if_fail (GST_IS_PAD (pad), result);
1522 GST_OBJECT_LOCK (pad);
1523 /* the blocking flag is only valid if the pad is not flushing */
1524 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1525 GST_OBJECT_UNLOCK (pad);
1531 * gst_pad_needs_reconfigure:
1532 * @pad: the #GstPad to check
1534 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1535 * if the flag was set.
1537 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1540 gst_pad_needs_reconfigure (GstPad * pad)
1542 gboolean reconfigure;
1544 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1546 GST_OBJECT_LOCK (pad);
1547 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1548 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1549 GST_OBJECT_UNLOCK (pad);
1555 * gst_pad_check_reconfigure:
1556 * @pad: the #GstPad to check
1558 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1559 * if the flag was set.
1561 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1564 gst_pad_check_reconfigure (GstPad * pad)
1566 gboolean reconfigure;
1568 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1570 GST_OBJECT_LOCK (pad);
1571 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1573 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1574 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1576 GST_OBJECT_UNLOCK (pad);
1582 * gst_pad_mark_reconfigure:
1583 * @pad: the #GstPad to mark
1585 * Mark a pad for needing reconfiguration. The next call to
1586 * gst_pad_check_reconfigure() will return %TRUE after this call.
1589 gst_pad_mark_reconfigure (GstPad * pad)
1591 g_return_if_fail (GST_IS_PAD (pad));
1593 GST_OBJECT_LOCK (pad);
1594 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1595 GST_OBJECT_UNLOCK (pad);
1599 * gst_pad_set_activate_function:
1601 * @f: the #GstPadActivateFunction to set.
1603 * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1607 * gst_pad_set_activate_function_full:
1609 * @activate: the #GstPadActivateFunction to set.
1610 * @user_data: user_data passed to @notify
1611 * @notify: notify called when @activate will not be used anymore.
1613 * Sets the given activate function for @pad. The activate function will
1614 * dispatch to gst_pad_activate_mode() to perform the actual activation.
1615 * Only makes sense to set on sink pads.
1617 * Call this function if your sink pad can start a pull-based task.
1620 gst_pad_set_activate_function_full (GstPad * pad,
1621 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1623 g_return_if_fail (GST_IS_PAD (pad));
1625 if (pad->activatenotify)
1626 pad->activatenotify (pad->activatedata);
1627 GST_PAD_ACTIVATEFUNC (pad) = activate;
1628 pad->activatedata = user_data;
1629 pad->activatenotify = notify;
1631 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1632 GST_DEBUG_FUNCPTR_NAME (activate));
1636 * gst_pad_set_activatemode_function:
1638 * @f: the #GstPadActivateModeFunction to set.
1640 * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1644 * gst_pad_set_activatemode_function_full:
1646 * @activatemode: the #GstPadActivateModeFunction to set.
1647 * @user_data: user_data passed to @notify
1648 * @notify: notify called when @activatemode will not be used anymore.
1650 * Sets the given activate_mode function for the pad. An activate_mode function
1651 * prepares the element for data passing.
1654 gst_pad_set_activatemode_function_full (GstPad * pad,
1655 GstPadActivateModeFunction activatemode, gpointer user_data,
1656 GDestroyNotify notify)
1658 g_return_if_fail (GST_IS_PAD (pad));
1660 if (pad->activatemodenotify)
1661 pad->activatemodenotify (pad->activatemodedata);
1662 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1663 pad->activatemodedata = user_data;
1664 pad->activatemodenotify = notify;
1666 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1667 GST_DEBUG_FUNCPTR_NAME (activatemode));
1671 * gst_pad_set_chain_function:
1672 * @p: a sink #GstPad.
1673 * @f: the #GstPadChainFunction to set.
1675 * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1679 * gst_pad_set_chain_function_full:
1680 * @pad: a sink #GstPad.
1681 * @chain: the #GstPadChainFunction to set.
1682 * @user_data: user_data passed to @notify
1683 * @notify: notify called when @chain will not be used anymore.
1685 * Sets the given chain function for the pad. The chain function is called to
1686 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1689 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1690 gpointer user_data, GDestroyNotify notify)
1692 g_return_if_fail (GST_IS_PAD (pad));
1693 g_return_if_fail (GST_PAD_IS_SINK (pad));
1695 if (pad->chainnotify)
1696 pad->chainnotify (pad->chaindata);
1697 GST_PAD_CHAINFUNC (pad) = chain;
1698 pad->chaindata = user_data;
1699 pad->chainnotify = notify;
1701 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1702 GST_DEBUG_FUNCPTR_NAME (chain));
1706 * gst_pad_set_chain_list_function:
1707 * @p: a sink #GstPad.
1708 * @f: the #GstPadChainListFunction to set.
1710 * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1714 * gst_pad_set_chain_list_function_full:
1715 * @pad: a sink #GstPad.
1716 * @chainlist: the #GstPadChainListFunction to set.
1717 * @user_data: user_data passed to @notify
1718 * @notify: notify called when @chainlist will not be used anymore.
1720 * Sets the given chain list function for the pad. The chainlist function is
1721 * called to process a #GstBufferList input buffer list. See
1722 * #GstPadChainListFunction for more details.
1725 gst_pad_set_chain_list_function_full (GstPad * pad,
1726 GstPadChainListFunction chainlist, gpointer user_data,
1727 GDestroyNotify notify)
1729 g_return_if_fail (GST_IS_PAD (pad));
1730 g_return_if_fail (GST_PAD_IS_SINK (pad));
1732 if (pad->chainlistnotify)
1733 pad->chainlistnotify (pad->chainlistdata);
1734 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1735 pad->chainlistdata = user_data;
1736 pad->chainlistnotify = notify;
1738 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1739 GST_DEBUG_FUNCPTR_NAME (chainlist));
1743 * gst_pad_set_getrange_function:
1744 * @p: a source #GstPad.
1745 * @f: the #GstPadGetRangeFunction to set.
1747 * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1751 * gst_pad_set_getrange_function_full:
1752 * @pad: a source #GstPad.
1753 * @get: the #GstPadGetRangeFunction to set.
1754 * @user_data: user_data passed to @notify
1755 * @notify: notify called when @get will not be used anymore.
1757 * Sets the given getrange function for the pad. The getrange function is
1758 * called to produce a new #GstBuffer to start the processing pipeline. see
1759 * #GstPadGetRangeFunction for a description of the getrange function.
1762 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1763 gpointer user_data, GDestroyNotify notify)
1765 g_return_if_fail (GST_IS_PAD (pad));
1766 g_return_if_fail (GST_PAD_IS_SRC (pad));
1768 if (pad->getrangenotify)
1769 pad->getrangenotify (pad->getrangedata);
1770 GST_PAD_GETRANGEFUNC (pad) = get;
1771 pad->getrangedata = user_data;
1772 pad->getrangenotify = notify;
1774 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1775 GST_DEBUG_FUNCPTR_NAME (get));
1779 * gst_pad_set_event_function:
1780 * @p: a #GstPad of either direction.
1781 * @f: the #GstPadEventFunction to set.
1783 * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1787 * gst_pad_set_event_function_full:
1788 * @pad: a #GstPad of either direction.
1789 * @event: the #GstPadEventFunction to set.
1790 * @user_data: user_data passed to @notify
1791 * @notify: notify called when @event will not be used anymore.
1793 * Sets the given event handler for the pad.
1796 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1797 gpointer user_data, GDestroyNotify notify)
1799 g_return_if_fail (GST_IS_PAD (pad));
1801 if (pad->eventnotify)
1802 pad->eventnotify (pad->eventdata);
1803 GST_PAD_EVENTFUNC (pad) = event;
1804 pad->eventdata = user_data;
1805 pad->eventnotify = notify;
1807 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1808 GST_DEBUG_FUNCPTR_NAME (event));
1812 * gst_pad_set_query_function:
1813 * @p: a #GstPad of either direction.
1814 * @f: the #GstPadQueryFunction to set.
1816 * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1820 * gst_pad_set_query_function_full:
1821 * @pad: a #GstPad of either direction.
1822 * @query: the #GstPadQueryFunction to set.
1823 * @user_data: user_data passed to @notify
1824 * @notify: notify called when @query will not be used anymore.
1826 * Set the given query function for the pad.
1829 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1830 gpointer user_data, GDestroyNotify notify)
1832 g_return_if_fail (GST_IS_PAD (pad));
1834 if (pad->querynotify)
1835 pad->querynotify (pad->querydata);
1836 GST_PAD_QUERYFUNC (pad) = query;
1837 pad->querydata = user_data;
1838 pad->querynotify = notify;
1840 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1841 GST_DEBUG_FUNCPTR_NAME (query));
1845 * gst_pad_set_iterate_internal_links_function:
1846 * @p: a #GstPad of either direction.
1847 * @f: the #GstPadIterIntLinkFunction to set.
1849 * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1850 * for the user_data and notify.
1853 * gst_pad_set_iterate_internal_links_function_full:
1854 * @pad: a #GstPad of either direction.
1855 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1856 * @user_data: user_data passed to @notify
1857 * @notify: notify called when @iterintlink will not be used anymore.
1859 * Sets the given internal link iterator function for the pad.
1862 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1863 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1864 GDestroyNotify notify)
1866 g_return_if_fail (GST_IS_PAD (pad));
1868 if (pad->iterintlinknotify)
1869 pad->iterintlinknotify (pad->iterintlinkdata);
1870 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1871 pad->iterintlinkdata = user_data;
1872 pad->iterintlinknotify = notify;
1874 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1875 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1879 * gst_pad_set_link_function:
1881 * @f: the #GstPadLinkFunction to set.
1883 * Calls gst_pad_set_link_function_full() with %NULL
1884 * for the user_data and notify.
1887 * gst_pad_set_link_function_full:
1889 * @link: the #GstPadLinkFunction to set.
1890 * @user_data: user_data passed to @notify
1891 * @notify: notify called when @link will not be used anymore.
1893 * Sets the given link function for the pad. It will be called when
1894 * the pad is linked with another pad.
1896 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1899 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1900 * cannot be made for some reason.
1902 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1903 * of the peer sink pad, if present.
1906 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1907 gpointer user_data, GDestroyNotify notify)
1909 g_return_if_fail (GST_IS_PAD (pad));
1911 if (pad->linknotify)
1912 pad->linknotify (pad->linkdata);
1913 GST_PAD_LINKFUNC (pad) = link;
1914 pad->linkdata = user_data;
1915 pad->linknotify = notify;
1917 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1918 GST_DEBUG_FUNCPTR_NAME (link));
1922 * gst_pad_set_unlink_function:
1924 * @f: the #GstPadUnlinkFunction to set.
1926 * Calls gst_pad_set_unlink_function_full() with %NULL
1927 * for the user_data and notify.
1930 * gst_pad_set_unlink_function_full:
1932 * @unlink: the #GstPadUnlinkFunction to set.
1933 * @user_data: user_data passed to @notify
1934 * @notify: notify called when @unlink will not be used anymore.
1936 * Sets the given unlink function for the pad. It will be called
1937 * when the pad is unlinked.
1940 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1941 gpointer user_data, GDestroyNotify notify)
1943 g_return_if_fail (GST_IS_PAD (pad));
1945 if (pad->unlinknotify)
1946 pad->unlinknotify (pad->unlinkdata);
1947 GST_PAD_UNLINKFUNC (pad) = unlink;
1948 pad->unlinkdata = user_data;
1949 pad->unlinknotify = notify;
1951 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1952 GST_DEBUG_FUNCPTR_NAME (unlink));
1957 * @srcpad: the source #GstPad to unlink.
1958 * @sinkpad: the sink #GstPad to unlink.
1960 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1961 * signal on both pads.
1963 * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
1964 * the pads were not linked together.
1969 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1971 gboolean result = FALSE;
1972 GstElement *parent = NULL;
1974 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1975 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1976 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1977 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1979 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1980 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1981 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1983 /* We need to notify the parent before taking any pad locks as the bin in
1984 * question might be waiting for a lock on the pad while holding its lock
1985 * that our message will try to take. */
1986 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1987 if (GST_IS_ELEMENT (parent)) {
1988 gst_element_post_message (parent,
1989 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1990 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1992 gst_object_unref (parent);
1997 GST_OBJECT_LOCK (srcpad);
1998 GST_OBJECT_LOCK (sinkpad);
2000 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2001 goto not_linked_together;
2003 if (GST_PAD_UNLINKFUNC (srcpad)) {
2004 GstObject *tmpparent;
2006 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2008 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2009 RELEASE_PARENT (tmpparent);
2012 if (GST_PAD_UNLINKFUNC (sinkpad)) {
2013 GstObject *tmpparent;
2015 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2017 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2018 RELEASE_PARENT (tmpparent);
2022 /* first clear peers */
2023 GST_PAD_PEER (srcpad) = NULL;
2024 GST_PAD_PEER (sinkpad) = NULL;
2026 GST_OBJECT_UNLOCK (sinkpad);
2027 GST_OBJECT_UNLOCK (srcpad);
2029 /* fire off a signal to each of the pads telling them
2030 * that they've been unlinked */
2031 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2032 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2034 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2035 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2040 if (parent != NULL) {
2041 gst_element_post_message (parent,
2042 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2043 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2044 gst_object_unref (parent);
2049 not_linked_together:
2051 /* we do not emit a warning in this case because unlinking cannot
2052 * be made MT safe.*/
2053 GST_OBJECT_UNLOCK (sinkpad);
2054 GST_OBJECT_UNLOCK (srcpad);
2060 * gst_pad_is_linked:
2061 * @pad: pad to check
2063 * Checks if a @pad is linked to another pad or not.
2065 * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2070 gst_pad_is_linked (GstPad * pad)
2074 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2076 GST_OBJECT_LOCK (pad);
2077 result = (GST_PAD_PEER (pad) != NULL);
2078 GST_OBJECT_UNLOCK (pad);
2083 /* get the caps from both pads and see if the intersection
2086 * This function should be called with the pad LOCK on both
2090 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2091 GstPadLinkCheck flags)
2093 GstCaps *srccaps = NULL;
2094 GstCaps *sinkcaps = NULL;
2095 gboolean compatible = FALSE;
2097 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2100 /* Doing the expensive caps checking takes priority over only checking the template caps */
2101 if (flags & GST_PAD_LINK_CHECK_CAPS) {
2102 GST_OBJECT_UNLOCK (sink);
2103 GST_OBJECT_UNLOCK (src);
2105 srccaps = gst_pad_query_caps (src, NULL);
2106 sinkcaps = gst_pad_query_caps (sink, NULL);
2108 GST_OBJECT_LOCK (src);
2109 GST_OBJECT_LOCK (sink);
2111 /* If one of the two pads doesn't have a template, consider the intersection
2113 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2114 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2118 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2120 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2123 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2125 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2128 /* if we have caps on both pads we can check the intersection. If one
2129 * of the caps is %NULL, we return %TRUE. */
2130 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2132 gst_caps_unref (srccaps);
2134 gst_caps_unref (sinkcaps);
2138 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2139 gst_caps_unref (srccaps);
2140 gst_caps_unref (sinkcaps);
2143 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2144 (compatible ? "" : "not "));
2149 /* check if the grandparents of both pads are the same.
2150 * This check is required so that we don't try to link
2151 * pads from elements in different bins without ghostpads.
2153 * The LOCK should be held on both pads
2156 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2158 GstObject *psrc, *psink;
2160 psrc = GST_OBJECT_PARENT (src);
2161 psink = GST_OBJECT_PARENT (sink);
2163 /* if one of the pads has no parent, we allow the link */
2164 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2167 /* only care about parents that are elements */
2168 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2169 goto no_element_parent;
2171 /* if the parents are the same, we have a loop */
2172 if (G_UNLIKELY (psrc == psink))
2175 /* if they both have a parent, we check the grandparents. We can not lock
2176 * the parent because we hold on the child (pad) and the locking order is
2177 * parent >> child. */
2178 psrc = GST_OBJECT_PARENT (psrc);
2179 psink = GST_OBJECT_PARENT (psink);
2181 /* if they have grandparents but they are not the same */
2182 if (G_UNLIKELY (psrc != psink))
2183 goto wrong_grandparents;
2190 GST_CAT_DEBUG (GST_CAT_CAPS,
2191 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2192 GST_PTR_FORMAT, psrc, psink);
2197 GST_CAT_DEBUG (GST_CAT_CAPS,
2198 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2199 GST_PTR_FORMAT, psrc, psink);
2204 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2210 GST_CAT_DEBUG (GST_CAT_CAPS,
2211 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2212 GST_PTR_FORMAT, psrc, psink);
2217 /* FIXME leftover from an attempt at refactoring... */
2218 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2219 * the two pads will be locked in the srcpad, sinkpad order. */
2220 static GstPadLinkReturn
2221 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2223 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2224 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2226 GST_OBJECT_LOCK (srcpad);
2228 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2229 goto src_was_linked;
2231 GST_OBJECT_LOCK (sinkpad);
2233 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2234 goto sink_was_linked;
2236 /* check hierarchy, pads can only be linked if the grandparents
2238 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2239 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2240 goto wrong_hierarchy;
2242 /* check pad caps for non-empty intersection */
2243 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2246 /* FIXME check pad scheduling for non-empty intersection */
2248 return GST_PAD_LINK_OK;
2252 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2253 GST_DEBUG_PAD_NAME (srcpad),
2254 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2255 /* we do not emit a warning in this case because unlinking cannot
2256 * be made MT safe.*/
2257 GST_OBJECT_UNLOCK (srcpad);
2258 return GST_PAD_LINK_WAS_LINKED;
2262 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2263 GST_DEBUG_PAD_NAME (sinkpad),
2264 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2265 /* we do not emit a warning in this case because unlinking cannot
2266 * be made MT safe.*/
2267 GST_OBJECT_UNLOCK (sinkpad);
2268 GST_OBJECT_UNLOCK (srcpad);
2269 return GST_PAD_LINK_WAS_LINKED;
2273 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2274 GST_OBJECT_UNLOCK (sinkpad);
2275 GST_OBJECT_UNLOCK (srcpad);
2276 return GST_PAD_LINK_WRONG_HIERARCHY;
2280 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2281 GST_OBJECT_UNLOCK (sinkpad);
2282 GST_OBJECT_UNLOCK (srcpad);
2283 return GST_PAD_LINK_NOFORMAT;
2289 * @srcpad: the source #GstPad.
2290 * @sinkpad: the sink #GstPad.
2292 * Checks if the source pad and the sink pad are compatible so they can be
2295 * Returns: %TRUE if the pads can be linked.
2298 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2300 GstPadLinkReturn result;
2302 /* generic checks */
2303 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2304 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2306 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2307 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2309 /* gst_pad_link_prepare does everything for us, we only release the locks
2310 * on the pads that it gets us. If this function returns !OK the locks are not
2312 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2313 if (result != GST_PAD_LINK_OK)
2316 GST_OBJECT_UNLOCK (srcpad);
2317 GST_OBJECT_UNLOCK (sinkpad);
2320 return result == GST_PAD_LINK_OK;
2324 * gst_pad_link_full:
2325 * @srcpad: the source #GstPad to link.
2326 * @sinkpad: the sink #GstPad to link.
2327 * @flags: the checks to validate when linking
2329 * Links the source pad and the sink pad.
2331 * This variant of #gst_pad_link provides a more granular control on the
2332 * checks being done when linking. While providing some considerable speedups
2333 * the caller of this method must be aware that wrong usage of those flags
2334 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2335 * for more information.
2339 * Returns: A result code indicating if the connection worked or
2343 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2345 GstPadLinkReturn result;
2347 GstPadLinkFunction srcfunc, sinkfunc;
2349 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2350 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2351 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2352 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2353 GST_PAD_LINK_WRONG_DIRECTION);
2355 /* Notify the parent early. See gst_pad_unlink for details. */
2356 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2357 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2358 gst_element_post_message (parent,
2359 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2360 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2362 gst_object_unref (parent);
2367 /* prepare will also lock the two pads */
2368 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2370 if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2371 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2372 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2373 gst_pad_link_get_name (result));
2377 /* must set peers before calling the link function */
2378 GST_PAD_PEER (srcpad) = sinkpad;
2379 GST_PAD_PEER (sinkpad) = srcpad;
2381 /* check events, when something is different, mark pending */
2382 schedule_events (srcpad, sinkpad);
2384 /* get the link functions */
2385 srcfunc = GST_PAD_LINKFUNC (srcpad);
2386 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2388 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2389 /* custom link functions, execute them */
2390 GST_OBJECT_UNLOCK (sinkpad);
2391 GST_OBJECT_UNLOCK (srcpad);
2394 GstObject *tmpparent;
2396 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2397 /* this one will call the peer link function */
2398 result = srcfunc (srcpad, tmpparent, sinkpad);
2399 RELEASE_PARENT (tmpparent);
2400 } else if (sinkfunc) {
2401 GstObject *tmpparent;
2403 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2404 /* if no source link function, we need to call the sink link
2405 * function ourselves. */
2406 result = sinkfunc (sinkpad, tmpparent, srcpad);
2407 RELEASE_PARENT (tmpparent);
2411 GST_OBJECT_LOCK (srcpad);
2412 GST_OBJECT_LOCK (sinkpad);
2414 /* we released the lock, check if the same pads are linked still */
2415 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2416 goto concurrent_link;
2418 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2421 GST_OBJECT_UNLOCK (sinkpad);
2422 GST_OBJECT_UNLOCK (srcpad);
2424 /* fire off a signal to each of the pads telling them
2425 * that they've been linked */
2426 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2427 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2429 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2430 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2432 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2435 if (G_LIKELY (parent)) {
2436 gst_element_post_message (parent,
2437 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2438 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2439 gst_object_unref (parent);
2447 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2448 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2449 GST_OBJECT_UNLOCK (sinkpad);
2450 GST_OBJECT_UNLOCK (srcpad);
2452 /* The other link operation succeeded first */
2453 result = GST_PAD_LINK_WAS_LINKED;
2458 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2459 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2460 gst_pad_link_get_name (result));
2462 GST_PAD_PEER (srcpad) = NULL;
2463 GST_PAD_PEER (sinkpad) = NULL;
2465 GST_OBJECT_UNLOCK (sinkpad);
2466 GST_OBJECT_UNLOCK (srcpad);
2474 * @srcpad: the source #GstPad to link.
2475 * @sinkpad: the sink #GstPad to link.
2477 * Links the source pad and the sink pad.
2479 * Returns: A result code indicating if the connection worked or
2485 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2487 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2491 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2493 GstPadTemplate **template_p;
2495 /* this function would need checks if it weren't static */
2497 GST_OBJECT_LOCK (pad);
2498 template_p = &pad->padtemplate;
2499 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2500 GST_OBJECT_UNLOCK (pad);
2503 gst_pad_template_pad_created (templ, pad);
2507 * gst_pad_get_pad_template:
2510 * Gets the template for @pad.
2512 * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2513 * this pad was instantiated, or %NULL if this pad has no
2514 * template. Unref after usage.
2517 gst_pad_get_pad_template (GstPad * pad)
2519 GstPadTemplate *templ;
2521 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2523 templ = GST_PAD_PAD_TEMPLATE (pad);
2525 return (templ ? gst_object_ref (templ) : NULL);
2529 * gst_pad_has_current_caps:
2530 * @pad: a #GstPad to check
2532 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2534 * Returns: %TRUE when @pad has caps associated with it.
2537 gst_pad_has_current_caps (GstPad * pad)
2542 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2544 GST_OBJECT_LOCK (pad);
2545 caps = get_pad_caps (pad);
2546 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2547 "check current pad caps %" GST_PTR_FORMAT, caps);
2548 result = (caps != NULL);
2549 GST_OBJECT_UNLOCK (pad);
2555 * gst_pad_get_current_caps:
2556 * @pad: a #GstPad to get the current capabilities of.
2558 * Gets the capabilities currently configured on @pad with the last
2559 * #GST_EVENT_CAPS event.
2561 * Returns: the current caps of the pad with incremented ref-count.
2564 gst_pad_get_current_caps (GstPad * pad)
2568 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2570 GST_OBJECT_LOCK (pad);
2571 if ((result = get_pad_caps (pad)))
2572 gst_caps_ref (result);
2573 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2574 "get current pad caps %" GST_PTR_FORMAT, result);
2575 GST_OBJECT_UNLOCK (pad);
2581 * gst_pad_get_pad_template_caps:
2582 * @pad: a #GstPad to get the template capabilities from.
2584 * Gets the capabilities for @pad's template.
2586 * Returns: (transfer full): the #GstCaps of this pad template.
2587 * Unref after usage.
2590 gst_pad_get_pad_template_caps (GstPad * pad)
2592 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2594 if (GST_PAD_PAD_TEMPLATE (pad))
2595 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2597 return gst_caps_ref (GST_CAPS_ANY);
2602 * @pad: a #GstPad to get the peer of.
2604 * Gets the peer of @pad. This function refs the peer pad so
2605 * you need to unref it after use.
2607 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2612 gst_pad_get_peer (GstPad * pad)
2616 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2618 GST_OBJECT_LOCK (pad);
2619 result = GST_PAD_PEER (pad);
2621 gst_object_ref (result);
2622 GST_OBJECT_UNLOCK (pad);
2628 * gst_pad_get_allowed_caps:
2631 * Gets the capabilities of the allowed media types that can flow through
2632 * @pad and its peer.
2634 * The allowed capabilities is calculated as the intersection of the results of
2635 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2636 * on the resulting caps.
2638 * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2639 * pad link. Unref the caps when you no longer need it. This
2640 * function returns %NULL when @pad has no peer.
2645 gst_pad_get_allowed_caps (GstPad * pad)
2648 GstCaps *caps = NULL;
2651 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2653 GST_OBJECT_LOCK (pad);
2654 if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2656 GST_OBJECT_UNLOCK (pad);
2658 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2660 mycaps = gst_pad_query_caps (pad, NULL);
2662 /* Query peer caps */
2663 query = gst_query_new_caps (mycaps);
2664 gst_pad_peer_query (pad, query);
2665 gst_query_parse_caps (query, &caps);
2666 gst_query_unref (query);
2668 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2675 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2676 GST_OBJECT_UNLOCK (pad);
2683 * gst_pad_iterate_internal_links_default:
2684 * @pad: the #GstPad to get the internal links of.
2685 * @parent: (allow-none): the parent of @pad or %NULL
2687 * Iterate the list of pads to which the given pad is linked to inside of
2688 * the parent element.
2689 * This is the default handler, and thus returns an iterator of all of the
2690 * pads inside the parent element with opposite direction.
2692 * The caller must free this iterator after use with gst_iterator_free().
2694 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2695 * has no parent. Unref each returned pad with gst_object_unref().
2698 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2705 GstElement *eparent;
2707 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2709 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2710 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2712 GST_OBJECT_LOCK (pad);
2713 eparent = GST_PAD_PARENT (pad);
2714 if (!eparent || !GST_IS_ELEMENT (eparent))
2717 gst_object_ref (eparent);
2718 GST_OBJECT_UNLOCK (pad);
2721 if (pad->direction == GST_PAD_SRC)
2722 padlist = &eparent->sinkpads;
2724 padlist = &eparent->srcpads;
2726 GST_DEBUG_OBJECT (pad, "Making iterator");
2728 cookie = &eparent->pads_cookie;
2730 lock = GST_OBJECT_GET_LOCK (eparent);
2732 res = gst_iterator_new_list (GST_TYPE_PAD,
2733 lock, cookie, padlist, (GObject *) owner, NULL);
2735 gst_object_unref (owner);
2742 GST_OBJECT_UNLOCK (pad);
2743 GST_DEBUG_OBJECT (pad, "no parent element");
2749 * gst_pad_iterate_internal_links:
2750 * @pad: the GstPad to get the internal links of.
2752 * Gets an iterator for the pads to which the given pad is linked to inside
2753 * of the parent element.
2755 * Each #GstPad element yielded by the iterator will have its refcount increased,
2756 * so unref after use.
2758 * Free-function: gst_iterator_free
2760 * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2761 * or %NULL when the pad does not have an iterator function
2762 * configured. Use gst_iterator_free() after usage.
2765 gst_pad_iterate_internal_links (GstPad * pad)
2767 GstIterator *res = NULL;
2770 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2772 GST_OBJECT_LOCK (pad);
2773 ACQUIRE_PARENT (pad, parent, no_parent);
2774 GST_OBJECT_UNLOCK (pad);
2776 if (GST_PAD_ITERINTLINKFUNC (pad))
2777 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2779 RELEASE_PARENT (parent);
2786 GST_DEBUG_OBJECT (pad, "no parent");
2787 GST_OBJECT_UNLOCK (pad);
2795 * @forward: (scope call): a #GstPadForwardFunction
2796 * @user_data: user data passed to @forward
2798 * Calls @forward for all internally linked pads of @pad. This function deals with
2799 * dynamically changing internal pads and will make sure that the @forward
2800 * function is only called once for each pad.
2802 * When @forward returns %TRUE, no further pads will be processed.
2804 * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2807 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2810 gboolean result = FALSE;
2812 gboolean done = FALSE;
2813 GValue item = { 0, };
2814 GList *pushed_pads = NULL;
2816 iter = gst_pad_iterate_internal_links (pad);
2822 switch (gst_iterator_next (iter, &item)) {
2823 case GST_ITERATOR_OK:
2827 intpad = g_value_get_object (&item);
2829 /* if already pushed, skip. FIXME, find something faster to tag pads */
2830 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2831 g_value_reset (&item);
2835 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2836 GST_DEBUG_PAD_NAME (intpad));
2837 done = result = forward (intpad, user_data);
2839 pushed_pads = g_list_prepend (pushed_pads, intpad);
2841 g_value_reset (&item);
2844 case GST_ITERATOR_RESYNC:
2845 /* We don't reset the result here because we don't push the event
2846 * again on pads that got the event already and because we need
2847 * to consider the result of the previous pushes */
2848 gst_iterator_resync (iter);
2850 case GST_ITERATOR_ERROR:
2851 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2854 case GST_ITERATOR_DONE:
2859 g_value_unset (&item);
2860 gst_iterator_free (iter);
2862 g_list_free (pushed_pads);
2872 gboolean dispatched;
2876 event_forward_func (GstPad * pad, EventData * data)
2878 /* for each pad we send to, we should ref the event; it's up
2879 * to downstream to unref again when handled. */
2880 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2881 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2883 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2885 data->dispatched = TRUE;
2892 * gst_pad_event_default:
2893 * @pad: a #GstPad to call the default event handler on.
2894 * @parent: (allow-none): the parent of @pad or %NULL
2895 * @event: (transfer full): the #GstEvent to handle.
2897 * Invokes the default event handler for the given pad.
2899 * The EOS event will pause the task associated with @pad before it is forwarded
2900 * to all internally linked pads,
2902 * The event is sent to all pads internally linked to @pad. This function
2903 * takes ownership of @event.
2905 * Returns: %TRUE if the event was sent successfully.
2908 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2910 gboolean result, forward = TRUE;
2912 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2913 g_return_val_if_fail (event != NULL, FALSE);
2915 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2918 switch (GST_EVENT_TYPE (event)) {
2919 case GST_EVENT_CAPS:
2920 forward = GST_PAD_IS_PROXY_CAPS (pad);
2931 data.dispatched = FALSE;
2932 data.result = FALSE;
2934 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2936 /* for sinkpads without a parent element or without internal links, nothing
2937 * will be dispatched but we still want to return TRUE. */
2938 if (data.dispatched)
2939 result = data.result;
2944 gst_event_unref (event);
2949 /* Default accept caps implementation just checks against
2950 * the allowed caps for the pad */
2952 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2954 /* get the caps and see if it intersects to something not empty */
2955 GstCaps *caps, *allowed;
2958 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2960 /* first forward the query to internally linked pads when we are dealing with
2962 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2963 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2968 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2969 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2971 gst_query_parse_accept_caps (query, &caps);
2972 allowed = gst_pad_query_caps (pad, caps);
2975 if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
2976 GST_DEBUG_OBJECT (pad,
2977 "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
2979 result = gst_caps_can_intersect (caps, allowed);
2981 GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
2982 GST_PTR_FORMAT, allowed, caps);
2983 result = gst_caps_is_subset (caps, allowed);
2985 gst_caps_unref (allowed);
2987 GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
2990 gst_query_set_accept_caps_result (query, result);
2996 /* Default caps implementation */
2998 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3000 GstCaps *result = NULL, *filter;
3001 GstPadTemplate *templ;
3002 gboolean fixed_caps;
3004 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3007 /* first try to proxy if we must */
3008 if (GST_PAD_IS_PROXY_CAPS (pad)) {
3009 if ((gst_pad_proxy_query_caps (pad, query))) {
3014 gst_query_parse_caps (query, &filter);
3016 /* no proxy or it failed, do default handling */
3017 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3019 GST_OBJECT_LOCK (pad);
3021 /* fixed caps, try the negotiated caps first */
3022 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3023 if ((result = get_pad_caps (pad)))
3024 goto filter_done_unlock;
3027 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3028 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3029 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3030 goto filter_done_unlock;
3034 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3035 "non-fixed pad caps: trying pad caps");
3036 /* non fixed caps, try the negotiated caps */
3037 if ((result = get_pad_caps (pad)))
3038 goto filter_done_unlock;
3041 /* this almost never happens */
3042 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3043 result = GST_CAPS_ANY;
3046 GST_OBJECT_UNLOCK (pad);
3048 /* run the filter on the result */
3050 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3051 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3052 GST_PTR_FORMAT, result, result, filter, filter);
3053 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3054 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3057 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3058 "using caps %p %" GST_PTR_FORMAT, result, result);
3059 result = gst_caps_ref (result);
3061 gst_query_set_caps_result (query, result);
3062 gst_caps_unref (result);
3068 /* Default latency implementation */
3072 GstClockTime min, max;
3076 query_latency_default_fold (const GValue * item, GValue * ret,
3079 GstPad *pad = g_value_get_object (item), *peer;
3080 LatencyFoldData *fold_data = user_data;
3082 gboolean res = FALSE;
3084 query = gst_query_new_latency ();
3086 peer = gst_pad_get_peer (pad);
3088 res = gst_pad_peer_query (pad, query);
3090 GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3095 GstClockTime min, max;
3097 gst_query_parse_latency (query, &live, &min, &max);
3099 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3100 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3103 if (min > fold_data->min)
3104 fold_data->min = min;
3106 if (fold_data->max == GST_CLOCK_TIME_NONE)
3107 fold_data->max = max;
3108 else if (max < fold_data->max)
3109 fold_data->max = max;
3111 fold_data->live = TRUE;
3114 GST_DEBUG_OBJECT (pad, "latency query failed");
3115 g_value_set_boolean (ret, FALSE);
3118 gst_query_unref (query);
3120 gst_object_unref (peer);
3126 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3129 GstIteratorResult res;
3130 GValue ret = G_VALUE_INIT;
3132 LatencyFoldData fold_data;
3134 it = gst_pad_iterate_internal_links (pad);
3136 GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3140 g_value_init (&ret, G_TYPE_BOOLEAN);
3143 fold_data.live = FALSE;
3145 fold_data.max = GST_CLOCK_TIME_NONE;
3147 g_value_set_boolean (&ret, TRUE);
3148 res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3150 case GST_ITERATOR_OK:
3151 g_assert_not_reached ();
3153 case GST_ITERATOR_DONE:
3155 case GST_ITERATOR_ERROR:
3156 g_value_set_boolean (&ret, FALSE);
3158 case GST_ITERATOR_RESYNC:
3159 gst_iterator_resync (it);
3162 g_assert_not_reached ();
3165 gst_iterator_free (it);
3167 query_ret = g_value_get_boolean (&ret);
3169 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3170 " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3171 fold_data.min, fold_data.max);
3173 if (fold_data.min > fold_data.max) {
3174 GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3177 gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3179 GST_LOG_OBJECT (pad, "latency query failed");
3189 gboolean dispatched;
3193 query_forward_func (GstPad * pad, QueryData * data)
3195 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3196 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3198 data->result |= gst_pad_peer_query (pad, data->query);
3200 data->dispatched = TRUE;
3202 /* stop on first successful reply */
3203 return data->result;
3207 * gst_pad_query_default:
3208 * @pad: a #GstPad to call the default query handler on.
3209 * @parent: (allow-none): the parent of @pad or %NULL
3210 * @query: (transfer none): the #GstQuery to handle.
3212 * Invokes the default query handler for the given pad.
3213 * The query is sent to all pads internally linked to @pad. Note that
3214 * if there are many possible sink pads that are internally linked to
3215 * @pad, only one will be sent the query.
3216 * Multi-sinkpad elements should implement custom query handlers.
3218 * Returns: %TRUE if the query was performed successfully.
3221 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3223 gboolean forward, ret = FALSE;
3225 switch (GST_QUERY_TYPE (query)) {
3226 case GST_QUERY_SCHEDULING:
3227 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3229 case GST_QUERY_ALLOCATION:
3230 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3232 case GST_QUERY_ACCEPT_CAPS:
3233 ret = gst_pad_query_accept_caps_default (pad, query);
3236 case GST_QUERY_CAPS:
3237 ret = gst_pad_query_caps_default (pad, query);
3240 case GST_QUERY_LATENCY:
3241 ret = gst_pad_query_latency_default (pad, query);
3244 case GST_QUERY_POSITION:
3245 case GST_QUERY_SEEKING:
3246 case GST_QUERY_FORMATS:
3247 case GST_QUERY_JITTER:
3248 case GST_QUERY_RATE:
3249 case GST_QUERY_CONVERT:
3255 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3256 query, GST_QUERY_TYPE_NAME (query));
3262 data.dispatched = FALSE;
3263 data.result = FALSE;
3265 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3267 if (data.dispatched) {
3270 /* nothing dispatched, assume drained */
3271 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3281 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3283 GstPad *pad = data->pad;
3284 GstPadProbeInfo *info = data->info;
3285 GstPadProbeType type, flags;
3286 GstPadProbeCallback callback;
3287 GstPadProbeReturn ret;
3288 gpointer original_data;
3290 /* if we have called this callback, do nothing */
3291 if (PROBE_COOKIE (hook) == data->cookie) {
3292 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3293 "hook %lu, cookie %u already called", hook->hook_id,
3294 PROBE_COOKIE (hook));
3298 PROBE_COOKIE (hook) = data->cookie;
3300 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3302 original_data = info->data;
3304 /* one of the data types for non-idle probes */
3305 if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3306 && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3308 /* one of the scheduling types */
3309 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3311 /* one of the blocking types must match */
3312 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3313 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3315 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3316 (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3318 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3319 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3320 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3323 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3324 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3325 PROBE_COOKIE (hook), flags);
3327 data->marshalled = TRUE;
3329 callback = (GstPadProbeCallback) hook->func;
3330 if (callback == NULL)
3333 info->id = hook->hook_id;
3335 GST_OBJECT_UNLOCK (pad);
3337 ret = callback (pad, info, hook->data);
3339 GST_OBJECT_LOCK (pad);
3341 if (original_data != NULL && info->data == NULL) {
3342 GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3343 info->type = GST_PAD_PROBE_TYPE_INVALID;
3344 data->dropped = TRUE;
3348 case GST_PAD_PROBE_REMOVE:
3349 /* remove the probe */
3350 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3351 cleanup_hook (pad, hook);
3353 case GST_PAD_PROBE_DROP:
3354 /* need to drop the data, make sure other probes don't get called
3356 GST_DEBUG_OBJECT (pad, "asked to drop item");
3357 info->type = GST_PAD_PROBE_TYPE_INVALID;
3358 data->dropped = TRUE;
3360 case GST_PAD_PROBE_PASS:
3361 /* inform the pad block to let things pass */
3362 GST_DEBUG_OBJECT (pad, "asked to pass item");
3365 case GST_PAD_PROBE_OK:
3366 GST_DEBUG_OBJECT (pad, "probe returned OK");
3369 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3376 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3377 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3378 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3383 /* a probe that does not take or return any data */
3384 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3386 if (G_UNLIKELY (pad->num_probes)) { \
3387 GstFlowReturn pval = defaultval; \
3388 /* pass NULL as the data item */ \
3389 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3390 ret = do_probe_callbacks (pad, &info, defaultval); \
3391 if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK)) \
3396 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3398 if (G_UNLIKELY (pad->num_probes)) { \
3399 /* pass the data item */ \
3400 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3401 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3402 /* store the possibly updated data item */ \
3403 data = GST_PAD_PROBE_INFO_DATA (&info); \
3404 /* if something went wrong, exit */ \
3405 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3410 #define PROBE_PUSH(pad,mask,data,label) \
3411 PROBE_FULL(pad, mask, data, -1, -1, label);
3412 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3413 PROBE_FULL(pad, mask, data, offs, size, label);
3415 static GstFlowReturn
3416 do_pad_idle_probe_wait (GstPad * pad)
3418 while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3419 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3420 "waiting idle probe to be removed");
3421 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3422 GST_PAD_BLOCK_WAIT (pad);
3423 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3424 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3426 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3427 return GST_FLOW_FLUSHING;
3432 #define PROBE_TYPE_IS_SERIALIZED(i) \
3435 (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3436 GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3437 GST_EVENT_IS_SERIALIZED ((i)->data)) \
3439 (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3440 GST_QUERY_IS_SERIALIZED ((i)->data)) \
3442 ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3443 GST_PAD_PROBE_TYPE_BUFFER_LIST)) \
3447 static GstFlowReturn
3448 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3449 GstFlowReturn defaultval)
3458 data.marshalled = FALSE;
3459 data.dropped = FALSE;
3460 data.cookie = ++pad->priv->probe_cookie;
3463 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3465 if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3466 if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3471 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3472 "do probes cookie %u", data.cookie);
3473 cookie = pad->priv->probe_list_cookie;
3475 g_hook_list_marshal (&pad->probes, TRUE,
3476 (GHookMarshaller) probe_hook_marshal, &data);
3478 /* if the list changed, call the new callbacks (they will not have their
3479 * cookie set to data.cookie */
3480 if (cookie != pad->priv->probe_list_cookie) {
3481 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3482 "probe list changed, restarting");
3486 /* the first item that dropped will stop the hooks and then we drop here */
3490 /* if no handler matched and we are blocking, let the item pass */
3491 if (!data.marshalled && is_block)
3494 /* At this point, all handlers returned either OK or PASS. If one handler
3495 * returned PASS, let the item pass */
3500 while (GST_PAD_IS_BLOCKED (pad)) {
3501 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3502 "we are blocked %d times", pad->num_blocked);
3504 /* we might have released the lock */
3505 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3508 /* now we block the streaming thread. It can be unlocked when we
3509 * deactivate the pad (which will also set the FLUSHING flag) or
3510 * when the pad is unblocked. A flushing event will also unblock
3511 * the pad after setting the FLUSHING flag. */
3512 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3513 "Waiting to be unblocked or set flushing");
3514 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3515 GST_PAD_BLOCK_WAIT (pad);
3516 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3517 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3519 /* if the list changed, call the new callbacks (they will not have their
3520 * cookie set to data.cookie */
3521 if (cookie != pad->priv->probe_list_cookie) {
3522 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3523 "probe list changed, restarting");
3527 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3537 GST_DEBUG_OBJECT (pad, "pad is flushing");
3538 return GST_FLOW_FLUSHING;
3542 GST_DEBUG_OBJECT (pad, "data is dropped");
3543 return GST_FLOW_CUSTOM_SUCCESS;
3547 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3548 GST_DEBUG_OBJECT (pad, "data is passed");
3556 * gst_pad_get_offset:
3559 * Get the offset applied to the running time of @pad. @pad has to be a source
3562 * Returns: the offset.
3565 gst_pad_get_offset (GstPad * pad)
3569 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3571 GST_OBJECT_LOCK (pad);
3572 result = pad->offset;
3573 GST_OBJECT_UNLOCK (pad);
3579 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3581 ev->received = FALSE;
3586 * gst_pad_set_offset:
3588 * @offset: the offset
3590 * Set the offset that will be applied to the running time of @pad.
3593 gst_pad_set_offset (GstPad * pad, gint64 offset)
3595 g_return_if_fail (GST_IS_PAD (pad));
3597 GST_OBJECT_LOCK (pad);
3598 /* if nothing changed, do nothing */
3599 if (pad->offset == offset)
3602 pad->offset = offset;
3603 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3605 /* resend all sticky events with updated offset on next buffer push */
3606 events_foreach (pad, mark_event_not_received, NULL);
3607 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3610 GST_OBJECT_UNLOCK (pad);
3617 /* If TRUE and ret is not OK this means
3618 * that pushing the EOS event failed
3622 /* If called for an event this is
3623 * the event that would be pushed
3624 * next. Don't forward sticky events
3625 * that would come after that */
3629 /* should be called with pad LOCK */
3631 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3633 PushStickyData *data = user_data;
3634 GstEvent *event = ev->event;
3637 GST_DEBUG_OBJECT (pad, "event %s was already received",
3638 GST_EVENT_TYPE_NAME (event));
3642 /* If we're called because of an sticky event, only forward
3643 * events that would come before this new event and the
3645 if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3646 GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3647 GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3648 data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3650 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3651 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3654 switch (data->ret) {
3656 ev->received = TRUE;
3657 GST_DEBUG_OBJECT (pad, "event %s marked received",
3658 GST_EVENT_TYPE_NAME (event));
3660 case GST_FLOW_CUSTOM_SUCCESS:
3661 /* we can't assume the event is received when it was dropped */
3662 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3663 GST_EVENT_TYPE_NAME (event));
3664 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3665 data->ret = GST_FLOW_OK;
3667 case GST_FLOW_CUSTOM_SUCCESS_1:
3668 /* event was ignored and should be sent later */
3669 GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3670 GST_EVENT_TYPE_NAME (event));
3671 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3672 data->ret = GST_FLOW_OK;
3674 case GST_FLOW_NOT_LINKED:
3675 /* not linked is not a problem, we are sticky so the event will be
3676 * sent later but only for non-EOS events */
3677 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3678 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3679 data->ret = GST_FLOW_OK;
3680 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3683 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3684 gst_flow_get_name (data->ret));
3685 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3689 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3690 data->was_eos = TRUE;
3692 return data->ret == GST_FLOW_OK;
3695 /* check sticky events and push them when needed. should be called
3697 static inline GstFlowReturn
3698 check_sticky (GstPad * pad, GstEvent * event)
3700 PushStickyData data = { GST_FLOW_OK, FALSE, event };
3702 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3703 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3705 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3706 events_foreach (pad, push_sticky, &data);
3708 /* If there's an EOS event we must push it downstream
3709 * even if sending a previous sticky event failed.
3710 * Otherwise the pipeline might wait forever for EOS.
3712 * Only do this if pushing another event than the EOS
3715 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3716 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3718 if (ev && !ev->received) {
3719 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3720 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3721 /* the event could have been dropped. Because this can only
3722 * happen if the user asked for it, it's not an error */
3723 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3724 data.ret = GST_FLOW_OK;
3734 * @pad: a #GstPad to invoke the default query on.
3735 * @query: (transfer none): the #GstQuery to perform.
3737 * Dispatches a query to a pad. The query should have been allocated by the
3738 * caller via one of the type-specific allocation functions. The element that
3739 * the pad belongs to is responsible for filling the query with an appropriate
3740 * response, which should then be parsed with a type-specific query parsing
3743 * Again, the caller is responsible for both the allocation and deallocation of
3744 * the query structure.
3746 * Please also note that some queries might need a running pipeline to work.
3748 * Returns: %TRUE if the query could be performed.
3751 gst_pad_query (GstPad * pad, GstQuery * query)
3754 gboolean res, serialized;
3755 GstPadQueryFunction func;
3756 GstPadProbeType type;
3759 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3760 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3762 if (GST_PAD_IS_SRC (pad)) {
3763 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3764 goto wrong_direction;
3765 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3766 } else if (GST_PAD_IS_SINK (pad)) {
3767 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3768 goto wrong_direction;
3769 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3771 goto unknown_direction;
3773 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3774 GST_QUERY_TYPE_NAME (query));
3776 serialized = GST_QUERY_IS_SERIALIZED (query);
3777 if (G_UNLIKELY (serialized))
3778 GST_PAD_STREAM_LOCK (pad);
3780 GST_OBJECT_LOCK (pad);
3781 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3782 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3783 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3785 ACQUIRE_PARENT (pad, parent, no_parent);
3786 GST_OBJECT_UNLOCK (pad);
3788 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3791 res = func (pad, parent, query);
3793 RELEASE_PARENT (parent);
3795 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3796 GST_QUERY_TYPE_NAME (query), res);
3801 GST_OBJECT_LOCK (pad);
3802 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3803 GST_OBJECT_UNLOCK (pad);
3805 if (G_UNLIKELY (serialized))
3806 GST_PAD_STREAM_UNLOCK (pad);
3813 g_warning ("pad %s:%s query %s in wrong direction",
3814 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3819 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3824 GST_DEBUG_OBJECT (pad, "had no parent");
3825 GST_OBJECT_UNLOCK (pad);
3826 if (G_UNLIKELY (serialized))
3827 GST_PAD_STREAM_UNLOCK (pad);
3832 GST_DEBUG_OBJECT (pad, "had no query function");
3833 RELEASE_PARENT (parent);
3834 if (G_UNLIKELY (serialized))
3835 GST_PAD_STREAM_UNLOCK (pad);
3840 GST_DEBUG_OBJECT (pad, "query failed");
3841 if (G_UNLIKELY (serialized))
3842 GST_PAD_STREAM_UNLOCK (pad);
3847 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3848 GST_OBJECT_UNLOCK (pad);
3849 if (G_UNLIKELY (serialized))
3850 GST_PAD_STREAM_UNLOCK (pad);
3852 /* if a probe dropped, we don't sent it further but assume that the probe
3853 * did not answer the query and return FALSE */
3861 * gst_pad_peer_query:
3862 * @pad: a #GstPad to invoke the peer query on.
3863 * @query: (transfer none): the #GstQuery to perform.
3865 * Performs gst_pad_query() on the peer of @pad.
3867 * The caller is responsible for both the allocation and deallocation of
3868 * the query structure.
3870 * Returns: %TRUE if the query could be performed. This function returns %FALSE
3871 * if @pad has no peer.
3874 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3877 GstPadProbeType type;
3878 gboolean res, serialized;
3881 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3882 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3884 if (GST_PAD_IS_SRC (pad)) {
3885 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3886 goto wrong_direction;
3887 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3888 } else if (GST_PAD_IS_SINK (pad)) {
3889 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3890 goto wrong_direction;
3891 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3893 goto unknown_direction;
3895 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3896 GST_QUERY_TYPE_NAME (query));
3898 serialized = GST_QUERY_IS_SERIALIZED (query);
3900 GST_OBJECT_LOCK (pad);
3901 if (GST_PAD_IS_SRC (pad) && serialized) {
3902 /* all serialized queries on the srcpad trigger push of
3904 if (check_sticky (pad, NULL) != GST_FLOW_OK)
3908 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3909 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3910 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3912 peerpad = GST_PAD_PEER (pad);
3913 if (G_UNLIKELY (peerpad == NULL))
3916 gst_object_ref (peerpad);
3917 GST_OBJECT_UNLOCK (pad);
3919 res = gst_pad_query (peerpad, query);
3921 gst_object_unref (peerpad);
3926 GST_OBJECT_LOCK (pad);
3927 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3928 GST_OBJECT_UNLOCK (pad);
3935 g_warning ("pad %s:%s query %s in wrong direction",
3936 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3941 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3946 GST_WARNING_OBJECT (pad, "could not send sticky events");
3947 GST_OBJECT_UNLOCK (pad);
3952 GST_INFO_OBJECT (pad, "pad has no peer");
3953 GST_OBJECT_UNLOCK (pad);
3958 GST_DEBUG_OBJECT (pad, "query failed");
3963 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3964 GST_OBJECT_UNLOCK (pad);
3966 /* if a probe dropped, we don't sent it further but assume that the probe
3967 * did not answer the query and return FALSE */
3974 /**********************************************************************
3975 * Data passing functions
3978 /* this is the chain function that does not perform the additional argument
3979 * checking for that little extra speed.
3981 static inline GstFlowReturn
3982 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3987 GST_PAD_STREAM_LOCK (pad);
3989 GST_OBJECT_LOCK (pad);
3990 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3993 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3996 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3999 #ifndef G_DISABLE_ASSERT
4000 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4001 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4003 ":%s:<%s:%s> Got data flow before stream-start event",
4004 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4006 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4008 ":%s:<%s:%s> Got data flow before segment event",
4009 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4011 pad->priv->last_cookie = pad->priv->events_cookie;
4015 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4017 PROBE_PUSH (pad, type, data, probe_stopped);
4019 ACQUIRE_PARENT (pad, parent, no_parent);
4020 GST_OBJECT_UNLOCK (pad);
4022 /* NOTE: we read the chainfunc unlocked.
4023 * we cannot hold the lock for the pad so we might send
4024 * the data to the wrong function. This is not really a
4025 * problem since functions are assigned at creation time
4026 * and don't change that often... */
4027 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4028 GstPadChainFunction chainfunc;
4030 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4033 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4034 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4035 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4037 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4039 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4040 "called chainfunction &%s with buffer %p, returned %s",
4041 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4043 GstPadChainListFunction chainlistfunc;
4045 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4048 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4049 "calling chainlistfunction &%s",
4050 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4052 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4054 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4055 "called chainlistfunction &%s, returned %s",
4056 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4059 RELEASE_PARENT (parent);
4061 GST_PAD_STREAM_UNLOCK (pad);
4068 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4069 "chaining, but pad was flushing");
4070 GST_OBJECT_UNLOCK (pad);
4071 GST_PAD_STREAM_UNLOCK (pad);
4072 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4073 return GST_FLOW_FLUSHING;
4077 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4078 GST_OBJECT_UNLOCK (pad);
4079 GST_PAD_STREAM_UNLOCK (pad);
4080 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4081 return GST_FLOW_EOS;
4085 g_critical ("chain on pad %s:%s but it was not in push mode",
4086 GST_DEBUG_PAD_NAME (pad));
4087 GST_OBJECT_UNLOCK (pad);
4088 GST_PAD_STREAM_UNLOCK (pad);
4089 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4090 return GST_FLOW_ERROR;
4094 GST_OBJECT_UNLOCK (pad);
4095 GST_PAD_STREAM_UNLOCK (pad);
4096 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4099 case GST_FLOW_CUSTOM_SUCCESS:
4100 GST_DEBUG_OBJECT (pad, "dropped buffer");
4104 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4111 GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4112 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4113 GST_OBJECT_UNLOCK (pad);
4114 GST_PAD_STREAM_UNLOCK (pad);
4115 return GST_FLOW_FLUSHING;
4119 RELEASE_PARENT (parent);
4120 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4121 g_critical ("chain on pad %s:%s but it has no chainfunction",
4122 GST_DEBUG_PAD_NAME (pad));
4123 GST_PAD_STREAM_UNLOCK (pad);
4124 return GST_FLOW_NOT_SUPPORTED;
4130 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4131 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4134 * Chain a buffer to @pad.
4136 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4138 * If the buffer type is not acceptable for @pad (as negotiated with a
4139 * preceding GST_EVENT_CAPS event), this function returns
4140 * #GST_FLOW_NOT_NEGOTIATED.
4142 * The function proceeds calling the chain function installed on @pad (see
4143 * gst_pad_set_chain_function()) and the return value of that function is
4144 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4147 * In all cases, success or failure, the caller loses its reference to @buffer
4148 * after calling this function.
4150 * Returns: a #GstFlowReturn from the pad.
4155 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4157 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4158 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4159 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4161 return gst_pad_chain_data_unchecked (pad,
4162 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4165 static GstFlowReturn
4166 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4167 GstBufferList * list)
4173 GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4175 len = gst_buffer_list_length (list);
4178 for (i = 0; i < len; i++) {
4179 buffer = gst_buffer_list_get (list, i);
4181 gst_pad_chain_data_unchecked (pad,
4182 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4183 gst_buffer_ref (buffer));
4184 if (ret != GST_FLOW_OK)
4187 gst_buffer_list_unref (list);
4193 * gst_pad_chain_list:
4194 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4195 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4198 * Chain a bufferlist to @pad.
4200 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4202 * If @pad was not negotiated properly with a CAPS event, this function
4203 * returns #GST_FLOW_NOT_NEGOTIATED.
4205 * The function proceeds calling the chainlist function installed on @pad (see
4206 * gst_pad_set_chain_list_function()) and the return value of that function is
4207 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4208 * chainlist function.
4210 * In all cases, success or failure, the caller loses its reference to @list
4211 * after calling this function.
4215 * Returns: a #GstFlowReturn from the pad.
4218 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4220 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4221 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4222 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4224 return gst_pad_chain_data_unchecked (pad,
4225 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4228 static GstFlowReturn
4229 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4234 GST_OBJECT_LOCK (pad);
4235 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4238 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4241 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4244 #ifndef G_DISABLE_ASSERT
4245 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4246 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4248 ":%s:<%s:%s> Got data flow before stream-start event",
4249 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4251 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4253 ":%s:<%s:%s> Got data flow before segment event",
4254 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4256 pad->priv->last_cookie = pad->priv->events_cookie;
4260 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4263 /* do block probes */
4264 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4266 /* recheck sticky events because the probe might have cause a relink */
4267 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4270 /* do post-blocking probes */
4271 PROBE_PUSH (pad, type, data, probe_stopped);
4273 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4276 /* take ref to peer pad before releasing the lock */
4277 gst_object_ref (peer);
4279 GST_OBJECT_UNLOCK (pad);
4281 ret = gst_pad_chain_data_unchecked (peer, type, data);
4284 gst_object_unref (peer);
4286 GST_OBJECT_LOCK (pad);
4287 pad->ABI.abi.last_flowret = ret;
4289 if (pad->priv->using == 0) {
4290 /* pad is not active anymore, trigger idle callbacks */
4291 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4292 probe_stopped, ret);
4294 GST_OBJECT_UNLOCK (pad);
4298 /* ERROR recovery here */
4302 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4303 "pushing, but pad was flushing");
4304 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4305 GST_OBJECT_UNLOCK (pad);
4306 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4307 return GST_FLOW_FLUSHING;
4311 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4312 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4313 GST_OBJECT_UNLOCK (pad);
4314 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4315 return GST_FLOW_EOS;
4319 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4320 GST_DEBUG_PAD_NAME (pad));
4321 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4322 GST_OBJECT_UNLOCK (pad);
4323 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4324 return GST_FLOW_ERROR;
4328 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4329 "error pushing events, return %s", gst_flow_get_name (ret));
4330 pad->ABI.abi.last_flowret = ret;
4331 GST_OBJECT_UNLOCK (pad);
4332 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4337 GST_OBJECT_UNLOCK (pad);
4338 pad->ABI.abi.last_flowret =
4339 ret == GST_FLOW_CUSTOM_SUCCESS ? GST_FLOW_OK : ret;
4341 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4344 case GST_FLOW_CUSTOM_SUCCESS:
4345 GST_DEBUG_OBJECT (pad, "dropped buffer");
4349 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4356 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4357 "pushing, but it was not linked");
4358 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4359 GST_OBJECT_UNLOCK (pad);
4360 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4361 return GST_FLOW_NOT_LINKED;
4367 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4368 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4371 * Pushes a buffer to the peer of @pad.
4373 * This function will call installed block probes before triggering any
4374 * installed data probes.
4376 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4377 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4380 * In all cases, success or failure, the caller loses its reference to @buffer
4381 * after calling this function.
4383 * Returns: a #GstFlowReturn from the peer pad.
4388 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4390 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4391 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4392 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4394 return gst_pad_push_data (pad,
4395 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4399 * gst_pad_push_list:
4400 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4401 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4404 * Pushes a buffer list to the peer of @pad.
4406 * This function will call installed block probes before triggering any
4407 * installed data probes.
4409 * The function proceeds calling the chain function on the peer pad and returns
4410 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4411 * be returned. If the peer pad does not have any installed chainlist function
4412 * every group buffer of the list will be merged into a normal #GstBuffer and
4413 * chained via gst_pad_chain().
4415 * In all cases, success or failure, the caller loses its reference to @list
4416 * after calling this function.
4418 * Returns: a #GstFlowReturn from the peer pad.
4423 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4425 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4426 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4427 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4429 return gst_pad_push_data (pad,
4430 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4433 static GstFlowReturn
4434 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4435 GstBuffer ** buffer)
4438 GstPadGetRangeFunction getrangefunc;
4442 GST_PAD_STREAM_LOCK (pad);
4444 GST_OBJECT_LOCK (pad);
4445 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4448 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4451 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4456 /* when one of the probes returns DROPPED, probe_stopped will be called
4457 * and we skip calling the getrange function, res_buf should then contain a
4458 * valid result buffer */
4459 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4460 res_buf, offset, size, probe_stopped);
4462 /* recheck sticky events because the probe might have cause a relink */
4463 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4466 ACQUIRE_PARENT (pad, parent, no_parent);
4467 GST_OBJECT_UNLOCK (pad);
4469 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4472 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4473 "calling getrangefunc %s, offset %"
4474 G_GUINT64_FORMAT ", size %u",
4475 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4477 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4479 RELEASE_PARENT (parent);
4481 GST_OBJECT_LOCK (pad);
4482 if (G_UNLIKELY (ret != GST_FLOW_OK))
4483 goto get_range_failed;
4485 /* can only fire the signal if we have a valid buffer */
4487 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4488 res_buf, offset, size, probe_stopped_unref);
4489 pad->ABI.abi.last_flowret = ret;
4490 GST_OBJECT_UNLOCK (pad);
4492 GST_PAD_STREAM_UNLOCK (pad);
4501 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4502 "getrange, but pad was flushing");
4503 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4504 GST_OBJECT_UNLOCK (pad);
4505 GST_PAD_STREAM_UNLOCK (pad);
4506 return GST_FLOW_FLUSHING;
4510 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4511 GST_DEBUG_PAD_NAME (pad));
4512 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4513 GST_OBJECT_UNLOCK (pad);
4514 GST_PAD_STREAM_UNLOCK (pad);
4515 return GST_FLOW_ERROR;
4519 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4520 pad->ABI.abi.last_flowret = ret;
4521 GST_OBJECT_UNLOCK (pad);
4522 GST_PAD_STREAM_UNLOCK (pad);
4527 GST_DEBUG_OBJECT (pad, "no parent");
4528 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4529 GST_OBJECT_UNLOCK (pad);
4530 GST_PAD_STREAM_UNLOCK (pad);
4531 return GST_FLOW_FLUSHING;
4535 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4536 GST_DEBUG_PAD_NAME (pad));
4537 RELEASE_PARENT (parent);
4538 GST_PAD_STREAM_UNLOCK (pad);
4539 return GST_FLOW_NOT_SUPPORTED;
4543 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4544 "probe returned %s", gst_flow_get_name (ret));
4545 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4547 /* the probe filled the buffer and asks us to not call the getrange
4548 * anymore, we continue with the post probes then. */
4549 GST_DEBUG_OBJECT (pad, "handled buffer");
4553 /* no buffer, we are EOS */
4554 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4558 pad->ABI.abi.last_flowret = ret;
4559 GST_OBJECT_UNLOCK (pad);
4560 GST_PAD_STREAM_UNLOCK (pad);
4564 probe_stopped_unref:
4566 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4567 "probe returned %s", gst_flow_get_name (ret));
4568 /* if we drop here, it signals EOS */
4569 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4571 pad->ABI.abi.last_flowret = ret;
4572 GST_OBJECT_UNLOCK (pad);
4573 GST_PAD_STREAM_UNLOCK (pad);
4574 if (*buffer == NULL)
4575 gst_buffer_unref (res_buf);
4580 pad->ABI.abi.last_flowret = ret;
4581 GST_OBJECT_UNLOCK (pad);
4582 GST_PAD_STREAM_UNLOCK (pad);
4583 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4584 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4585 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4591 * gst_pad_get_range:
4592 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4593 * @offset: The start offset of the buffer
4594 * @size: The length of the buffer
4595 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4596 * returns #GST_FLOW_ERROR if %NULL.
4598 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4599 * immediately and @buffer is %NULL.
4601 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4602 * description of a getrange function. If @pad has no getrange function
4603 * installed (see gst_pad_set_getrange_function()) this function returns
4604 * #GST_FLOW_NOT_SUPPORTED.
4606 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4607 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4608 * must be freed with gst_buffer_unref() after usage.
4610 * When @buffer points to a variable that points to a valid #GstBuffer, the
4611 * buffer will be filled with the result data when this function returns
4612 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4613 * @size bytes will be filled in the result buffer and its size will be updated
4616 * Note that less than @size bytes can be returned in @buffer when, for example,
4617 * an EOS condition is near or when @buffer is not large enough to hold @size
4618 * bytes. The caller should check the result buffer size to get the result size.
4620 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4621 * will be unchanged.
4623 * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4625 * Returns: a #GstFlowReturn from the pad.
4630 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4631 GstBuffer ** buffer)
4633 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4634 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4635 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4636 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4637 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4639 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4643 * gst_pad_pull_range:
4644 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4645 * @offset: The start offset of the buffer
4646 * @size: The length of the buffer
4647 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4648 * GST_FLOW_ERROR if %NULL.
4650 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4652 * This function will first trigger the pad block signal if it was
4655 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4656 * function returns the result of gst_pad_get_range() on the peer pad.
4657 * See gst_pad_get_range() for a list of return values and for the
4658 * semantics of the arguments of this function.
4660 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4661 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4662 * must be freed with gst_buffer_unref() after usage. When this function
4663 * returns any other result value, @buffer will still point to %NULL.
4665 * When @buffer points to a variable that points to a valid #GstBuffer, the
4666 * buffer will be filled with the result data when this function returns
4667 * #GST_FLOW_OK. When this function returns any other result value,
4668 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4669 * @size bytes will be filled in the result buffer and its size will be updated
4672 * Note that less than @size bytes can be returned in @buffer when, for example,
4673 * an EOS condition is near or when @buffer is not large enough to hold @size
4674 * bytes. The caller should check the result buffer size to get the result size.
4676 * Returns: a #GstFlowReturn from the peer pad.
4681 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4682 GstBuffer ** buffer)
4688 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4689 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4690 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4691 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4692 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4694 GST_OBJECT_LOCK (pad);
4695 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4698 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4703 /* when one of the probes returns DROPPED, probe_stopped will be
4704 * called and we skip calling the peer getrange function. *buffer should then
4705 * contain a valid buffer */
4706 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4707 res_buf, offset, size, probe_stopped);
4709 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4712 gst_object_ref (peer);
4714 GST_OBJECT_UNLOCK (pad);
4716 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4718 gst_object_unref (peer);
4720 GST_OBJECT_LOCK (pad);
4722 pad->ABI.abi.last_flowret = ret;
4723 if (pad->priv->using == 0) {
4724 /* pad is not active anymore, trigger idle callbacks */
4725 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4726 probe_stopped_unref, ret);
4729 if (G_UNLIKELY (ret != GST_FLOW_OK))
4730 goto pull_range_failed;
4733 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4734 res_buf, offset, size, probe_stopped_unref);
4736 GST_OBJECT_UNLOCK (pad);
4742 /* ERROR recovery here */
4745 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4746 "pullrange, but pad was flushing");
4747 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4748 GST_OBJECT_UNLOCK (pad);
4749 return GST_FLOW_FLUSHING;
4753 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4754 GST_DEBUG_PAD_NAME (pad));
4755 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4756 GST_OBJECT_UNLOCK (pad);
4757 return GST_FLOW_ERROR;
4761 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4762 gst_flow_get_name (ret));
4763 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4765 /* the probe filled the buffer and asks us to not forward to the peer
4766 * anymore, we continue with the post probes then */
4767 GST_DEBUG_OBJECT (pad, "handled buffer");
4771 /* no buffer, we are EOS then */
4772 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4776 pad->ABI.abi.last_flowret = ret;
4777 GST_OBJECT_UNLOCK (pad);
4782 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4783 "pulling range, but it was not linked");
4784 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4785 GST_OBJECT_UNLOCK (pad);
4786 return GST_FLOW_NOT_LINKED;
4790 pad->ABI.abi.last_flowret = ret;
4791 GST_OBJECT_UNLOCK (pad);
4792 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4793 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4794 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4797 probe_stopped_unref:
4799 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4800 "post probe returned %s", gst_flow_get_name (ret));
4802 /* if we drop here, it signals EOS */
4803 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4806 pad->ABI.abi.last_flowret = ret;
4807 GST_OBJECT_UNLOCK (pad);
4809 if (*buffer == NULL)
4810 gst_buffer_unref (res_buf);
4815 /* must be called with pad object lock */
4816 static GstFlowReturn
4817 store_sticky_event (GstPad * pad, GstEvent * event)
4822 gboolean res = FALSE;
4823 const gchar *name = NULL;
4824 gboolean insert = TRUE;
4826 type = GST_EVENT_TYPE (event);
4828 /* Store all sticky events except SEGMENT/EOS when we're flushing,
4829 * otherwise they can be dropped and nothing would ever resend them.
4830 * Only do that for activated pads though, everything else is a bug!
4832 if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4833 || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4834 || type == GST_EVENT_EOS))))
4837 /* Unset the EOS flag when received STREAM_START event, so pad can
4838 * store sticky event and then push it later */
4839 if (type == GST_EVENT_STREAM_START) {
4840 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4841 remove_event_by_type (pad, GST_EVENT_EOS);
4842 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4845 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4848 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4849 name = gst_structure_get_name (gst_event_get_structure (event));
4851 events = pad->priv->events;
4854 for (i = 0; i < len; i++) {
4855 PadEvent *ev = &g_array_index (events, PadEvent, i);
4857 if (ev->event == NULL)
4860 if (type == GST_EVENT_TYPE (ev->event)) {
4861 /* matching types, check matching name if needed */
4862 if (name && !gst_event_has_name (ev->event, name))
4866 if ((res = gst_event_replace (&ev->event, event)))
4867 ev->received = FALSE;
4873 if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4874 && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4875 /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4876 * storing the sticky ordered we can check that this is respected. */
4877 if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4878 || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4880 ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4881 G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4882 gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4883 gst_event_type_get_name (type));
4889 ev.event = gst_event_ref (event);
4890 ev.received = FALSE;
4891 g_array_insert_val (events, i, ev);
4896 pad->priv->events_cookie++;
4897 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4899 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4901 switch (GST_EVENT_TYPE (event)) {
4902 case GST_EVENT_CAPS:
4903 GST_OBJECT_UNLOCK (pad);
4905 GST_DEBUG_OBJECT (pad, "notify caps");
4906 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4908 GST_OBJECT_LOCK (pad);
4914 if (type == GST_EVENT_EOS) {
4915 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4916 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4919 return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4924 GST_DEBUG_OBJECT (pad, "pad is flushing");
4925 return GST_FLOW_FLUSHING;
4929 GST_DEBUG_OBJECT (pad, "pad is EOS");
4930 return GST_FLOW_EOS;
4935 * gst_pad_store_sticky_event:
4937 * @event: a #GstEvent
4939 * Store the sticky @event on @pad
4941 * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
4942 * was flushing or #GST_FLOW_EOS when the pad was EOS.
4947 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4951 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4952 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4954 GST_OBJECT_LOCK (pad);
4955 ret = store_sticky_event (pad, event);
4956 GST_OBJECT_UNLOCK (pad);
4962 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
4964 PushStickyData *data = user_data;
4966 /* Forward all sticky events before our current one that are pending */
4967 if (ev->event != data->event
4968 && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
4969 return push_sticky (pad, ev, data);
4974 /* should be called with pad LOCK */
4975 static GstFlowReturn
4976 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4977 GstPadProbeType type)
4981 GstEventType event_type;
4983 /* pass the adjusted event on. We need to do this even if
4984 * there is no peer pad because of the probes. */
4985 event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
4987 /* Two checks to be made:
4988 * . (un)set the FLUSHING flag for flushing events,
4989 * . handle pad blocking */
4990 event_type = GST_EVENT_TYPE (event);
4991 switch (event_type) {
4992 case GST_EVENT_FLUSH_START:
4993 GST_PAD_SET_FLUSHING (pad);
4995 GST_PAD_BLOCK_BROADCAST (pad);
4996 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4998 case GST_EVENT_FLUSH_STOP:
4999 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5002 GST_PAD_UNSET_FLUSHING (pad);
5004 /* Remove sticky EOS events */
5005 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5006 remove_event_by_type (pad, GST_EVENT_EOS);
5007 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5008 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5009 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5011 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5015 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5018 /* No need to check for EOS here as either the caller (gst_pad_push_event())
5019 * checked already or this is called as part of pushing sticky events,
5020 * in which case we still want to forward the EOS event downstream.
5023 switch (GST_EVENT_TYPE (event)) {
5024 case GST_EVENT_RECONFIGURE:
5025 if (GST_PAD_IS_SINK (pad))
5026 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5031 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5032 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5037 /* send probes after modifying the events above */
5038 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5040 /* recheck sticky events because the probe might have cause a relink */
5041 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5042 && (GST_EVENT_IS_SERIALIZED (event)
5043 || GST_EVENT_IS_STICKY (event))) {
5044 PushStickyData data = { GST_FLOW_OK, FALSE, event };
5045 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5047 /* Push all sticky events before our current one
5048 * that have changed */
5049 events_foreach (pad, sticky_changed, &data);
5052 /* now check the peer pad */
5053 peerpad = GST_PAD_PEER (pad);
5054 if (peerpad == NULL)
5057 gst_object_ref (peerpad);
5059 GST_OBJECT_UNLOCK (pad);
5061 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5062 event, gst_event_type_get_name (event_type), peerpad);
5064 ret = gst_pad_send_event_unchecked (peerpad, event, type);
5066 /* Note: we gave away ownership of the event at this point but we can still
5067 * print the old pointer */
5068 GST_LOG_OBJECT (pad,
5069 "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5070 gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5072 gst_object_unref (peerpad);
5074 GST_OBJECT_LOCK (pad);
5076 if (pad->priv->using == 0) {
5077 /* pad is not active anymore, trigger idle callbacks */
5078 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5079 idle_probe_stopped, ret);
5083 /* ERROR handling */
5086 GST_DEBUG_OBJECT (pad, "We're flushing");
5087 gst_event_unref (event);
5088 return GST_FLOW_FLUSHING;
5092 GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5093 gst_event_unref (event);
5094 return GST_FLOW_FLUSHING;
5098 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5099 gst_event_unref (event);
5102 case GST_FLOW_CUSTOM_SUCCESS:
5103 GST_DEBUG_OBJECT (pad, "dropped event");
5106 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5113 GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5114 gst_event_type_get_name (GST_EVENT_TYPE (event)));
5115 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5116 gst_event_unref (event);
5118 /* unlinked pads should not influence latency configuration */
5119 if (event_type == GST_EVENT_LATENCY)
5122 return GST_FLOW_NOT_LINKED;
5126 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5132 * gst_pad_push_event:
5133 * @pad: a #GstPad to push the event to.
5134 * @event: (transfer full): the #GstEvent to send to the pad.
5136 * Sends the event to the peer of the given pad. This function is
5137 * mainly used by elements to send events to their peer
5140 * This function takes ownership of the provided event so you should
5141 * gst_event_ref() it if you want to reuse the event after this call.
5143 * Returns: %TRUE if the event was handled.
5148 gst_pad_push_event (GstPad * pad, GstEvent * event)
5150 gboolean res = FALSE;
5151 GstPadProbeType type;
5152 gboolean sticky, serialized;
5154 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5155 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5157 if (GST_PAD_IS_SRC (pad)) {
5158 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5159 goto wrong_direction;
5160 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5161 } else if (GST_PAD_IS_SINK (pad)) {
5162 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5163 goto wrong_direction;
5164 /* events pushed on sinkpad never are sticky */
5165 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5167 goto unknown_direction;
5169 GST_OBJECT_LOCK (pad);
5170 sticky = GST_EVENT_IS_STICKY (event);
5171 serialized = GST_EVENT_IS_SERIALIZED (event);
5174 /* srcpad sticky events are stored immediately, the received flag is set
5175 * to FALSE and will be set to TRUE when we can successfully push the
5176 * event to the peer pad */
5177 switch (store_sticky_event (pad, event)) {
5178 case GST_FLOW_FLUSHING:
5186 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5187 /* all serialized or sticky events on the srcpad trigger push of
5189 res = (check_sticky (pad, event) == GST_FLOW_OK);
5194 /* other events are pushed right away */
5195 ret = gst_pad_push_event_unchecked (pad, event, type);
5196 /* dropped events by a probe are not an error */
5197 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
5199 /* Errors in sticky event pushing are no problem and ignored here
5200 * as they will cause more meaningful errors during data flow.
5201 * For EOS events, that are not followed by data flow, we still
5202 * return FALSE here though.
5204 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5206 gst_event_unref (event);
5208 GST_OBJECT_UNLOCK (pad);
5212 /* ERROR handling */
5215 g_warning ("pad %s:%s pushing %s event in wrong direction",
5216 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5217 gst_event_unref (event);
5222 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5223 gst_event_unref (event);
5228 GST_DEBUG_OBJECT (pad, "We're flushing");
5229 GST_OBJECT_UNLOCK (pad);
5230 gst_event_unref (event);
5235 GST_DEBUG_OBJECT (pad, "We're EOS");
5236 GST_OBJECT_UNLOCK (pad);
5237 gst_event_unref (event);
5242 /* Check if we can call the event function with the given event */
5243 static GstFlowReturn
5244 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5248 switch (GST_EVENT_TYPE (event)) {
5249 case GST_EVENT_CAPS:
5251 /* backwards compatibility mode for caps */
5252 gst_event_parse_caps (event, &caps);
5254 if (!gst_pad_query_accept_caps (pad, caps))
5266 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5267 "caps %" GST_PTR_FORMAT " not accepted", caps);
5268 return GST_FLOW_NOT_NEGOTIATED;
5272 static GstFlowReturn
5273 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5274 GstPadProbeType type)
5277 GstEventType event_type;
5278 gboolean serialized, need_unlock = FALSE, sticky;
5279 GstPadEventFunction eventfunc;
5282 GST_OBJECT_LOCK (pad);
5284 event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5286 if (GST_PAD_IS_SINK (pad))
5287 serialized = GST_EVENT_IS_SERIALIZED (event);
5290 sticky = GST_EVENT_IS_STICKY (event);
5291 event_type = GST_EVENT_TYPE (event);
5292 switch (event_type) {
5293 case GST_EVENT_FLUSH_START:
5294 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5295 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5297 /* can't even accept a flush begin event when flushing */
5298 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5301 GST_PAD_SET_FLUSHING (pad);
5302 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5304 case GST_EVENT_FLUSH_STOP:
5305 /* we can't accept flush-stop on inactive pads else the flushing flag
5306 * would be cleared and it would look like the pad can accept data.
5307 * Also, some elements restart a streaming thread in flush-stop which we
5308 * can't allow on inactive pads */
5309 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5312 GST_PAD_UNSET_FLUSHING (pad);
5313 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5314 /* Remove pending EOS events */
5315 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5316 remove_event_by_type (pad, GST_EVENT_EOS);
5317 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5318 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5319 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5321 GST_OBJECT_UNLOCK (pad);
5322 /* grab stream lock */
5323 GST_PAD_STREAM_LOCK (pad);
5325 GST_OBJECT_LOCK (pad);
5326 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5329 case GST_EVENT_RECONFIGURE:
5330 if (GST_PAD_IS_SRC (pad))
5331 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5333 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5334 "have event type %" GST_PTR_FORMAT, event);
5336 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5339 switch (event_type) {
5340 case GST_EVENT_STREAM_START:
5341 /* Remove sticky EOS events */
5342 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5343 remove_event_by_type (pad, GST_EVENT_EOS);
5344 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5351 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5354 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5355 GST_OBJECT_UNLOCK (pad);
5356 GST_PAD_STREAM_LOCK (pad);
5358 GST_OBJECT_LOCK (pad);
5359 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5362 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5368 /* now do the probe */
5370 type | GST_PAD_PROBE_TYPE_PUSH |
5371 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5373 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5375 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5378 ACQUIRE_PARENT (pad, parent, no_parent);
5379 GST_OBJECT_UNLOCK (pad);
5381 ret = pre_eventfunc_check (pad, event);
5382 if (G_UNLIKELY (ret != GST_FLOW_OK))
5383 goto precheck_failed;
5386 gst_event_ref (event);
5388 if (eventfunc (pad, parent, event)) {
5391 /* something went wrong */
5392 switch (event_type) {
5393 case GST_EVENT_CAPS:
5394 ret = GST_FLOW_NOT_NEGOTIATED;
5397 ret = GST_FLOW_ERROR;
5401 RELEASE_PARENT (parent);
5403 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5406 if (ret == GST_FLOW_OK) {
5407 GST_OBJECT_LOCK (pad);
5408 /* after the event function accepted the event, we can store the sticky
5409 * event on the pad */
5410 switch (store_sticky_event (pad, event)) {
5411 case GST_FLOW_FLUSHING:
5418 GST_OBJECT_UNLOCK (pad);
5420 gst_event_unref (event);
5424 GST_PAD_STREAM_UNLOCK (pad);
5428 /* ERROR handling */
5431 GST_OBJECT_UNLOCK (pad);
5433 GST_PAD_STREAM_UNLOCK (pad);
5434 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5435 "Received event on flushing pad. Discarding");
5436 gst_event_unref (event);
5437 return GST_FLOW_FLUSHING;
5441 GST_OBJECT_UNLOCK (pad);
5443 GST_PAD_STREAM_UNLOCK (pad);
5444 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5445 "Received flush-stop on inactive pad. Discarding");
5446 gst_event_unref (event);
5447 return GST_FLOW_FLUSHING;
5451 GST_OBJECT_UNLOCK (pad);
5453 GST_PAD_STREAM_UNLOCK (pad);
5454 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5455 "Received event on EOS pad. Discarding");
5456 gst_event_unref (event);
5457 return GST_FLOW_EOS;
5461 GST_OBJECT_UNLOCK (pad);
5463 GST_PAD_STREAM_UNLOCK (pad);
5464 gst_event_unref (event);
5467 case GST_FLOW_CUSTOM_SUCCESS:
5468 GST_DEBUG_OBJECT (pad, "dropped event");
5472 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5479 g_warning ("pad %s:%s has no event handler, file a bug.",
5480 GST_DEBUG_PAD_NAME (pad));
5481 GST_OBJECT_UNLOCK (pad);
5483 GST_PAD_STREAM_UNLOCK (pad);
5484 gst_event_unref (event);
5485 return GST_FLOW_NOT_SUPPORTED;
5489 GST_DEBUG_OBJECT (pad, "no parent");
5490 GST_OBJECT_UNLOCK (pad);
5492 GST_PAD_STREAM_UNLOCK (pad);
5493 gst_event_unref (event);
5494 return GST_FLOW_FLUSHING;
5498 GST_DEBUG_OBJECT (pad, "pre event check failed");
5499 RELEASE_PARENT (parent);
5501 GST_PAD_STREAM_UNLOCK (pad);
5502 gst_event_unref (event);
5508 * gst_pad_send_event:
5509 * @pad: a #GstPad to send the event to.
5510 * @event: (transfer full): the #GstEvent to send to the pad.
5512 * Sends the event to the pad. This function can be used
5513 * by applications to send events in the pipeline.
5515 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5516 * sink pad, @event should be a downstream event. For example, you would not
5517 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5518 * Furthermore, some downstream events have to be serialized with data flow,
5519 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5520 * the event needs to be serialized with data flow, this function will take the
5521 * pad's stream lock while calling its event function.
5523 * To find out whether an event type is upstream, downstream, or downstream and
5524 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5525 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5526 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5527 * plugin doesn't need to bother itself with this information; the core handles
5528 * all necessary locks and checks.
5530 * This function takes ownership of the provided event so you should
5531 * gst_event_ref() it if you want to reuse the event after this call.
5533 * Returns: %TRUE if the event was handled.
5536 gst_pad_send_event (GstPad * pad, GstEvent * event)
5539 GstPadProbeType type;
5541 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5542 g_return_val_if_fail (event != NULL, FALSE);
5544 if (GST_PAD_IS_SINK (pad)) {
5545 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5546 goto wrong_direction;
5547 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5548 } else if (GST_PAD_IS_SRC (pad)) {
5549 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5550 goto wrong_direction;
5551 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5553 goto unknown_direction;
5555 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5562 /* ERROR handling */
5565 g_warning ("pad %s:%s sending %s event in wrong direction",
5566 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5567 gst_event_unref (event);
5572 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5573 gst_event_unref (event);
5579 * gst_pad_set_element_private:
5580 * @pad: the #GstPad to set the private data of.
5581 * @priv: The private data to attach to the pad.
5583 * Set the given private data gpointer on the pad.
5584 * This function can only be used by the element that owns the pad.
5585 * No locking is performed in this function.
5588 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5590 pad->element_private = priv;
5594 * gst_pad_get_element_private:
5595 * @pad: the #GstPad to get the private data of.
5597 * Gets the private data of a pad.
5598 * No locking is performed in this function.
5600 * Returns: (transfer none): a #gpointer to the private data.
5603 gst_pad_get_element_private (GstPad * pad)
5605 return pad->element_private;
5609 * gst_pad_get_sticky_event:
5610 * @pad: the #GstPad to get the event from.
5611 * @event_type: the #GstEventType that should be retrieved.
5612 * @idx: the index of the event
5614 * Returns a new reference of the sticky event of type @event_type
5617 * Returns: (transfer full) (nullable): a #GstEvent of type
5618 * @event_type or %NULL when no event of @event_type was on
5619 * @pad. Unref after usage.
5622 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5624 GstEvent *event = NULL;
5627 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5628 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5630 GST_OBJECT_LOCK (pad);
5631 ev = find_event_by_type (pad, event_type, idx);
5632 if (ev && (event = ev->event))
5633 gst_event_ref (event);
5634 GST_OBJECT_UNLOCK (pad);
5641 GstPadStickyEventsForeachFunction func;
5646 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5648 ForeachDispatch *data = user_data;
5649 gboolean ret = TRUE;
5652 GST_OBJECT_UNLOCK (pad);
5654 ret = data->func (pad, &ev->event, data->user_data);
5656 GST_OBJECT_LOCK (pad);
5663 * gst_pad_sticky_events_foreach:
5664 * @pad: the #GstPad that should be used for iteration.
5665 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5666 * should be called for every event.
5667 * @user_data: (closure): the optional user data.
5669 * Iterates all sticky events on @pad and calls @foreach_func for every
5670 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5673 gst_pad_sticky_events_foreach (GstPad * pad,
5674 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5676 ForeachDispatch data;
5678 g_return_if_fail (GST_IS_PAD (pad));
5679 g_return_if_fail (foreach_func != NULL);
5681 data.func = foreach_func;
5682 data.user_data = user_data;
5684 GST_OBJECT_LOCK (pad);
5685 events_foreach (pad, foreach_dispatch_function, &data);
5686 GST_OBJECT_UNLOCK (pad);
5690 do_stream_status (GstPad * pad, GstStreamStatusType type,
5691 GThread * thread, GstTask * task)
5695 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5697 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5698 if (GST_IS_ELEMENT (parent)) {
5699 GstMessage *message;
5700 GValue value = { 0 };
5702 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5703 gchar *tname, *ename, *pname;
5705 /* create a good task name */
5706 ename = gst_element_get_name (parent);
5707 pname = gst_pad_get_name (pad);
5708 tname = g_strdup_printf ("%s:%s", ename, pname);
5712 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5716 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5719 g_value_init (&value, GST_TYPE_TASK);
5720 g_value_set_object (&value, task);
5721 gst_message_set_stream_status_object (message, &value);
5722 g_value_unset (&value);
5724 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5725 gst_element_post_message (parent, message);
5727 gst_object_unref (parent);
5732 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5734 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5739 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5741 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5746 * gst_pad_start_task:
5747 * @pad: the #GstPad to start the task of
5748 * @func: the task function to call
5749 * @user_data: user data passed to the task function
5750 * @notify: called when @user_data is no longer referenced
5752 * Starts a task that repeatedly calls @func with @user_data. This function
5753 * is mostly used in pad activation functions to start the dataflow.
5754 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5755 * before @func is called.
5757 * Returns: a %TRUE if the task could be started.
5760 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5761 GDestroyNotify notify)
5766 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5767 g_return_val_if_fail (func != NULL, FALSE);
5769 GST_DEBUG_OBJECT (pad, "start task");
5771 GST_OBJECT_LOCK (pad);
5772 task = GST_PAD_TASK (pad);
5774 task = gst_task_new (func, user_data, notify);
5775 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5776 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5777 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5778 GST_INFO_OBJECT (pad, "created task %p", task);
5779 GST_PAD_TASK (pad) = task;
5780 gst_object_ref (task);
5781 /* release lock to post the message */
5782 GST_OBJECT_UNLOCK (pad);
5784 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5786 gst_object_unref (task);
5788 GST_OBJECT_LOCK (pad);
5789 /* nobody else is supposed to have changed the pad now */
5790 if (GST_PAD_TASK (pad) != task)
5791 goto concurrent_stop;
5793 res = gst_task_set_state (task, GST_TASK_STARTED);
5794 GST_OBJECT_UNLOCK (pad);
5801 GST_OBJECT_UNLOCK (pad);
5807 * gst_pad_pause_task:
5808 * @pad: the #GstPad to pause the task of
5810 * Pause the task of @pad. This function will also wait until the
5811 * function executed by the task is finished if this function is not
5812 * called from the task function.
5814 * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5818 gst_pad_pause_task (GstPad * pad)
5823 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5825 GST_DEBUG_OBJECT (pad, "pause task");
5827 GST_OBJECT_LOCK (pad);
5828 task = GST_PAD_TASK (pad);
5831 res = gst_task_set_state (task, GST_TASK_PAUSED);
5832 GST_OBJECT_UNLOCK (pad);
5834 /* wait for task function to finish, this lock is recursive so it does nothing
5835 * when the pause is called from the task itself */
5836 GST_PAD_STREAM_LOCK (pad);
5837 GST_PAD_STREAM_UNLOCK (pad);
5843 GST_DEBUG_OBJECT (pad, "pad has no task");
5844 GST_OBJECT_UNLOCK (pad);
5850 * gst_pad_stop_task:
5851 * @pad: the #GstPad to stop the task of
5853 * Stop the task of @pad. This function will also make sure that the
5854 * function executed by the task will effectively stop if not called
5855 * from the GstTaskFunction.
5857 * This function will deadlock if called from the GstTaskFunction of
5858 * the task. Use gst_task_pause() instead.
5860 * Regardless of whether the pad has a task, the stream lock is acquired and
5861 * released so as to ensure that streaming through this pad has finished.
5863 * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5866 gst_pad_stop_task (GstPad * pad)
5871 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5873 GST_DEBUG_OBJECT (pad, "stop task");
5875 GST_OBJECT_LOCK (pad);
5876 task = GST_PAD_TASK (pad);
5879 GST_PAD_TASK (pad) = NULL;
5880 res = gst_task_set_state (task, GST_TASK_STOPPED);
5881 GST_OBJECT_UNLOCK (pad);
5883 GST_PAD_STREAM_LOCK (pad);
5884 GST_PAD_STREAM_UNLOCK (pad);
5886 if (!gst_task_join (task))
5889 gst_object_unref (task);
5895 GST_DEBUG_OBJECT (pad, "no task");
5896 GST_OBJECT_UNLOCK (pad);
5898 GST_PAD_STREAM_LOCK (pad);
5899 GST_PAD_STREAM_UNLOCK (pad);
5901 /* this is not an error */
5906 /* this is bad, possibly the application tried to join the task from
5907 * the task's thread. We install the task again so that it will be stopped
5908 * again from the right thread next time hopefully. */
5909 GST_OBJECT_LOCK (pad);
5910 GST_DEBUG_OBJECT (pad, "join failed");
5911 /* we can only install this task if there was no other task */
5912 if (GST_PAD_TASK (pad) == NULL)
5913 GST_PAD_TASK (pad) = task;
5914 GST_OBJECT_UNLOCK (pad);
5921 * gst_pad_probe_info_get_event:
5922 * @info: a #GstPadProbeInfo
5924 * Returns: (transfer none): The #GstEvent from the probe
5928 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5930 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5931 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5933 return GST_PAD_PROBE_INFO_EVENT (info);
5938 * gst_pad_probe_info_get_query:
5939 * @info: a #GstPadProbeInfo
5941 * Returns: (transfer none): The #GstQuery from the probe
5945 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5947 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5948 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5950 return GST_PAD_PROBE_INFO_QUERY (info);
5954 * gst_pad_probe_info_get_buffer:
5955 * @info: a #GstPadProbeInfo
5957 * Returns: (transfer none): The #GstBuffer from the probe
5961 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5963 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5965 return GST_PAD_PROBE_INFO_BUFFER (info);
5969 * gst_pad_probe_info_get_bufferlist:
5970 * @info: a #GstPadProbeInfo
5972 * Returns: (transfer none): The #GstBufferlist from the probe
5976 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5978 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5980 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5984 * gst_pad_get_last_flow_return:
5987 * Gets the #GstFlowReturn return from the last data passed by this pad.
5992 gst_pad_get_last_flow_return (GstPad * pad)
5996 GST_OBJECT_LOCK (pad);
5997 ret = GST_PAD_LAST_FLOW_RETURN (pad);
5998 GST_OBJECT_UNLOCK (pad);