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)
2651 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2653 GST_OBJECT_LOCK (pad);
2654 peer = GST_PAD_PEER (pad);
2655 if (G_UNLIKELY (peer == NULL))
2658 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2660 gst_object_ref (peer);
2661 GST_OBJECT_UNLOCK (pad);
2662 mycaps = gst_pad_query_caps (pad, NULL);
2664 caps = gst_pad_query_caps (peer, mycaps);
2665 gst_object_unref (peer);
2667 gst_caps_unref (mycaps);
2669 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2676 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2677 GST_OBJECT_UNLOCK (pad);
2684 * gst_pad_iterate_internal_links_default:
2685 * @pad: the #GstPad to get the internal links of.
2686 * @parent: (allow-none): the parent of @pad or %NULL
2688 * Iterate the list of pads to which the given pad is linked to inside of
2689 * the parent element.
2690 * This is the default handler, and thus returns an iterator of all of the
2691 * pads inside the parent element with opposite direction.
2693 * The caller must free this iterator after use with gst_iterator_free().
2695 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2696 * has no parent. Unref each returned pad with gst_object_unref().
2699 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2706 GstElement *eparent;
2708 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2710 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2711 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2713 GST_OBJECT_LOCK (pad);
2714 eparent = GST_PAD_PARENT (pad);
2715 if (!eparent || !GST_IS_ELEMENT (eparent))
2718 gst_object_ref (eparent);
2719 GST_OBJECT_UNLOCK (pad);
2722 if (pad->direction == GST_PAD_SRC)
2723 padlist = &eparent->sinkpads;
2725 padlist = &eparent->srcpads;
2727 GST_DEBUG_OBJECT (pad, "Making iterator");
2729 cookie = &eparent->pads_cookie;
2731 lock = GST_OBJECT_GET_LOCK (eparent);
2733 res = gst_iterator_new_list (GST_TYPE_PAD,
2734 lock, cookie, padlist, (GObject *) owner, NULL);
2736 gst_object_unref (owner);
2743 GST_OBJECT_UNLOCK (pad);
2744 GST_DEBUG_OBJECT (pad, "no parent element");
2750 * gst_pad_iterate_internal_links:
2751 * @pad: the GstPad to get the internal links of.
2753 * Gets an iterator for the pads to which the given pad is linked to inside
2754 * of the parent element.
2756 * Each #GstPad element yielded by the iterator will have its refcount increased,
2757 * so unref after use.
2759 * Free-function: gst_iterator_free
2761 * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2762 * or %NULL when the pad does not have an iterator function
2763 * configured. Use gst_iterator_free() after usage.
2766 gst_pad_iterate_internal_links (GstPad * pad)
2768 GstIterator *res = NULL;
2771 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2773 GST_OBJECT_LOCK (pad);
2774 ACQUIRE_PARENT (pad, parent, no_parent);
2775 GST_OBJECT_UNLOCK (pad);
2777 if (GST_PAD_ITERINTLINKFUNC (pad))
2778 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2780 RELEASE_PARENT (parent);
2787 GST_DEBUG_OBJECT (pad, "no parent");
2788 GST_OBJECT_UNLOCK (pad);
2796 * @forward: (scope call): a #GstPadForwardFunction
2797 * @user_data: user data passed to @forward
2799 * Calls @forward for all internally linked pads of @pad. This function deals with
2800 * dynamically changing internal pads and will make sure that the @forward
2801 * function is only called once for each pad.
2803 * When @forward returns %TRUE, no further pads will be processed.
2805 * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2808 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2811 gboolean result = FALSE;
2813 gboolean done = FALSE;
2814 GValue item = { 0, };
2815 GList *pushed_pads = NULL;
2817 iter = gst_pad_iterate_internal_links (pad);
2823 switch (gst_iterator_next (iter, &item)) {
2824 case GST_ITERATOR_OK:
2828 intpad = g_value_get_object (&item);
2830 /* if already pushed, skip. FIXME, find something faster to tag pads */
2831 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2832 g_value_reset (&item);
2836 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2837 GST_DEBUG_PAD_NAME (intpad));
2838 done = result = forward (intpad, user_data);
2840 pushed_pads = g_list_prepend (pushed_pads, intpad);
2842 g_value_reset (&item);
2845 case GST_ITERATOR_RESYNC:
2846 /* We don't reset the result here because we don't push the event
2847 * again on pads that got the event already and because we need
2848 * to consider the result of the previous pushes */
2849 gst_iterator_resync (iter);
2851 case GST_ITERATOR_ERROR:
2852 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2855 case GST_ITERATOR_DONE:
2860 g_value_unset (&item);
2861 gst_iterator_free (iter);
2863 g_list_free (pushed_pads);
2873 gboolean dispatched;
2877 event_forward_func (GstPad * pad, EventData * data)
2879 /* for each pad we send to, we should ref the event; it's up
2880 * to downstream to unref again when handled. */
2881 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2882 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2884 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2886 data->dispatched = TRUE;
2893 * gst_pad_event_default:
2894 * @pad: a #GstPad to call the default event handler on.
2895 * @parent: (allow-none): the parent of @pad or %NULL
2896 * @event: (transfer full): the #GstEvent to handle.
2898 * Invokes the default event handler for the given pad.
2900 * The EOS event will pause the task associated with @pad before it is forwarded
2901 * to all internally linked pads,
2903 * The event is sent to all pads internally linked to @pad. This function
2904 * takes ownership of @event.
2906 * Returns: %TRUE if the event was sent successfully.
2909 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2911 gboolean result, forward = TRUE;
2913 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2914 g_return_val_if_fail (event != NULL, FALSE);
2916 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2919 switch (GST_EVENT_TYPE (event)) {
2920 case GST_EVENT_CAPS:
2921 forward = GST_PAD_IS_PROXY_CAPS (pad);
2932 data.dispatched = FALSE;
2933 data.result = FALSE;
2935 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2937 /* for sinkpads without a parent element or without internal links, nothing
2938 * will be dispatched but we still want to return TRUE. */
2939 if (data.dispatched)
2940 result = data.result;
2945 gst_event_unref (event);
2950 /* Default accept caps implementation just checks against
2951 * the allowed caps for the pad */
2953 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2955 /* get the caps and see if it intersects to something not empty */
2956 GstCaps *caps, *allowed;
2959 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2961 /* first forward the query to internally linked pads when we are dealing with
2963 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2964 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2969 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2970 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2972 gst_query_parse_accept_caps (query, &caps);
2973 allowed = gst_pad_query_caps (pad, caps);
2976 if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
2977 GST_DEBUG_OBJECT (pad,
2978 "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
2980 result = gst_caps_can_intersect (caps, allowed);
2982 GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
2983 GST_PTR_FORMAT, allowed, caps);
2984 result = gst_caps_is_subset (caps, allowed);
2986 gst_caps_unref (allowed);
2988 GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
2991 gst_query_set_accept_caps_result (query, result);
2997 /* Default caps implementation */
2999 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3001 GstCaps *result = NULL, *filter;
3002 GstPadTemplate *templ;
3003 gboolean fixed_caps;
3005 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3008 /* first try to proxy if we must */
3009 if (GST_PAD_IS_PROXY_CAPS (pad)) {
3010 if ((gst_pad_proxy_query_caps (pad, query))) {
3015 gst_query_parse_caps (query, &filter);
3017 /* no proxy or it failed, do default handling */
3018 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3020 GST_OBJECT_LOCK (pad);
3022 /* fixed caps, try the negotiated caps first */
3023 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3024 if ((result = get_pad_caps (pad)))
3025 goto filter_done_unlock;
3028 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3029 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3030 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3031 goto filter_done_unlock;
3035 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3036 "non-fixed pad caps: trying pad caps");
3037 /* non fixed caps, try the negotiated caps */
3038 if ((result = get_pad_caps (pad)))
3039 goto filter_done_unlock;
3042 /* this almost never happens */
3043 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3044 result = GST_CAPS_ANY;
3047 GST_OBJECT_UNLOCK (pad);
3049 /* run the filter on the result */
3051 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3052 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3053 GST_PTR_FORMAT, result, result, filter, filter);
3054 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3055 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3058 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3059 "using caps %p %" GST_PTR_FORMAT, result, result);
3060 result = gst_caps_ref (result);
3062 gst_query_set_caps_result (query, result);
3063 gst_caps_unref (result);
3069 /* Default latency implementation */
3073 GstClockTime min, max;
3077 query_latency_default_fold (const GValue * item, GValue * ret,
3080 GstPad *pad = g_value_get_object (item), *peer;
3081 LatencyFoldData *fold_data = user_data;
3083 gboolean res = FALSE;
3085 query = gst_query_new_latency ();
3087 peer = gst_pad_get_peer (pad);
3089 res = gst_pad_peer_query (pad, query);
3091 GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3096 GstClockTime min, max;
3098 gst_query_parse_latency (query, &live, &min, &max);
3100 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3101 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3104 if (min > fold_data->min)
3105 fold_data->min = min;
3107 if (fold_data->max == GST_CLOCK_TIME_NONE)
3108 fold_data->max = max;
3109 else if (max < fold_data->max)
3110 fold_data->max = max;
3112 fold_data->live = TRUE;
3115 GST_DEBUG_OBJECT (pad, "latency query failed");
3116 g_value_set_boolean (ret, FALSE);
3119 gst_query_unref (query);
3121 gst_object_unref (peer);
3127 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3130 GstIteratorResult res;
3131 GValue ret = G_VALUE_INIT;
3133 LatencyFoldData fold_data;
3135 it = gst_pad_iterate_internal_links (pad);
3137 GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3141 g_value_init (&ret, G_TYPE_BOOLEAN);
3144 fold_data.live = FALSE;
3146 fold_data.max = GST_CLOCK_TIME_NONE;
3148 g_value_set_boolean (&ret, TRUE);
3149 res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3151 case GST_ITERATOR_OK:
3152 g_assert_not_reached ();
3154 case GST_ITERATOR_DONE:
3156 case GST_ITERATOR_ERROR:
3157 g_value_set_boolean (&ret, FALSE);
3159 case GST_ITERATOR_RESYNC:
3160 gst_iterator_resync (it);
3163 g_assert_not_reached ();
3166 gst_iterator_free (it);
3168 query_ret = g_value_get_boolean (&ret);
3170 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3171 " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3172 fold_data.min, fold_data.max);
3174 if (fold_data.min > fold_data.max) {
3175 GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3178 gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3180 GST_LOG_OBJECT (pad, "latency query failed");
3190 gboolean dispatched;
3194 query_forward_func (GstPad * pad, QueryData * data)
3196 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3197 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3199 data->result |= gst_pad_peer_query (pad, data->query);
3201 data->dispatched = TRUE;
3203 /* stop on first successful reply */
3204 return data->result;
3208 * gst_pad_query_default:
3209 * @pad: a #GstPad to call the default query handler on.
3210 * @parent: (allow-none): the parent of @pad or %NULL
3211 * @query: (transfer none): the #GstQuery to handle.
3213 * Invokes the default query handler for the given pad.
3214 * The query is sent to all pads internally linked to @pad. Note that
3215 * if there are many possible sink pads that are internally linked to
3216 * @pad, only one will be sent the query.
3217 * Multi-sinkpad elements should implement custom query handlers.
3219 * Returns: %TRUE if the query was performed successfully.
3222 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3224 gboolean forward, ret = FALSE;
3226 switch (GST_QUERY_TYPE (query)) {
3227 case GST_QUERY_SCHEDULING:
3228 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3230 case GST_QUERY_ALLOCATION:
3231 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3233 case GST_QUERY_ACCEPT_CAPS:
3234 ret = gst_pad_query_accept_caps_default (pad, query);
3237 case GST_QUERY_CAPS:
3238 ret = gst_pad_query_caps_default (pad, query);
3241 case GST_QUERY_LATENCY:
3242 ret = gst_pad_query_latency_default (pad, query);
3245 case GST_QUERY_POSITION:
3246 case GST_QUERY_SEEKING:
3247 case GST_QUERY_FORMATS:
3248 case GST_QUERY_JITTER:
3249 case GST_QUERY_RATE:
3250 case GST_QUERY_CONVERT:
3256 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3257 query, GST_QUERY_TYPE_NAME (query));
3263 data.dispatched = FALSE;
3264 data.result = FALSE;
3266 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3268 if (data.dispatched) {
3271 /* nothing dispatched, assume drained */
3272 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3282 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3284 GstPad *pad = data->pad;
3285 GstPadProbeInfo *info = data->info;
3286 GstPadProbeType type, flags;
3287 GstPadProbeCallback callback;
3288 GstPadProbeReturn ret;
3289 gpointer original_data;
3291 /* if we have called this callback, do nothing */
3292 if (PROBE_COOKIE (hook) == data->cookie) {
3293 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3294 "hook %lu, cookie %u already called", hook->hook_id,
3295 PROBE_COOKIE (hook));
3299 PROBE_COOKIE (hook) = data->cookie;
3301 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3303 original_data = info->data;
3305 /* one of the data types for non-idle probes */
3306 if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3307 && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3309 /* one of the scheduling types */
3310 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3312 /* one of the blocking types must match */
3313 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3314 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3316 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3317 (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3319 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3320 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3321 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3324 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3325 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3326 PROBE_COOKIE (hook), flags);
3328 data->marshalled = TRUE;
3330 callback = (GstPadProbeCallback) hook->func;
3331 if (callback == NULL)
3334 info->id = hook->hook_id;
3336 GST_OBJECT_UNLOCK (pad);
3338 ret = callback (pad, info, hook->data);
3340 GST_OBJECT_LOCK (pad);
3342 if (original_data != NULL && info->data == NULL) {
3343 GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3344 info->type = GST_PAD_PROBE_TYPE_INVALID;
3345 data->dropped = TRUE;
3349 case GST_PAD_PROBE_REMOVE:
3350 /* remove the probe */
3351 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3352 cleanup_hook (pad, hook);
3354 case GST_PAD_PROBE_DROP:
3355 /* need to drop the data, make sure other probes don't get called
3357 GST_DEBUG_OBJECT (pad, "asked to drop item");
3358 info->type = GST_PAD_PROBE_TYPE_INVALID;
3359 data->dropped = TRUE;
3361 case GST_PAD_PROBE_PASS:
3362 /* inform the pad block to let things pass */
3363 GST_DEBUG_OBJECT (pad, "asked to pass item");
3366 case GST_PAD_PROBE_OK:
3367 GST_DEBUG_OBJECT (pad, "probe returned OK");
3370 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3377 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3378 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3379 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3384 /* a probe that does not take or return any data */
3385 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3387 if (G_UNLIKELY (pad->num_probes)) { \
3388 GstFlowReturn pval = defaultval; \
3389 /* pass NULL as the data item */ \
3390 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3391 ret = do_probe_callbacks (pad, &info, defaultval); \
3392 if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK)) \
3397 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3399 if (G_UNLIKELY (pad->num_probes)) { \
3400 /* pass the data item */ \
3401 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3402 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3403 /* store the possibly updated data item */ \
3404 data = GST_PAD_PROBE_INFO_DATA (&info); \
3405 /* if something went wrong, exit */ \
3406 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3411 #define PROBE_PUSH(pad,mask,data,label) \
3412 PROBE_FULL(pad, mask, data, -1, -1, label);
3413 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3414 PROBE_FULL(pad, mask, data, offs, size, label);
3416 static GstFlowReturn
3417 do_pad_idle_probe_wait (GstPad * pad)
3419 while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3420 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3421 "waiting idle probe to be removed");
3422 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3423 GST_PAD_BLOCK_WAIT (pad);
3424 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3425 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3427 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3428 return GST_FLOW_FLUSHING;
3433 #define PROBE_TYPE_IS_SERIALIZED(i) \
3436 (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3437 GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3438 GST_EVENT_IS_SERIALIZED ((i)->data)) \
3440 (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3441 GST_QUERY_IS_SERIALIZED ((i)->data)) \
3443 ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3444 GST_PAD_PROBE_TYPE_BUFFER_LIST)) \
3448 static GstFlowReturn
3449 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3450 GstFlowReturn defaultval)
3459 data.marshalled = FALSE;
3460 data.dropped = FALSE;
3461 data.cookie = ++pad->priv->probe_cookie;
3464 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3466 if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3467 if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3472 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3473 "do probes cookie %u", data.cookie);
3474 cookie = pad->priv->probe_list_cookie;
3476 g_hook_list_marshal (&pad->probes, TRUE,
3477 (GHookMarshaller) probe_hook_marshal, &data);
3479 /* if the list changed, call the new callbacks (they will not have their
3480 * cookie set to data.cookie */
3481 if (cookie != pad->priv->probe_list_cookie) {
3482 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3483 "probe list changed, restarting");
3487 /* the first item that dropped will stop the hooks and then we drop here */
3491 /* if no handler matched and we are blocking, let the item pass */
3492 if (!data.marshalled && is_block)
3495 /* At this point, all handlers returned either OK or PASS. If one handler
3496 * returned PASS, let the item pass */
3501 while (GST_PAD_IS_BLOCKED (pad)) {
3502 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3503 "we are blocked %d times", pad->num_blocked);
3505 /* we might have released the lock */
3506 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3509 /* now we block the streaming thread. It can be unlocked when we
3510 * deactivate the pad (which will also set the FLUSHING flag) or
3511 * when the pad is unblocked. A flushing event will also unblock
3512 * the pad after setting the FLUSHING flag. */
3513 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3514 "Waiting to be unblocked or set flushing");
3515 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3516 GST_PAD_BLOCK_WAIT (pad);
3517 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3518 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3520 /* if the list changed, call the new callbacks (they will not have their
3521 * cookie set to data.cookie */
3522 if (cookie != pad->priv->probe_list_cookie) {
3523 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3524 "probe list changed, restarting");
3528 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3538 GST_DEBUG_OBJECT (pad, "pad is flushing");
3539 return GST_FLOW_FLUSHING;
3543 GST_DEBUG_OBJECT (pad, "data is dropped");
3544 return GST_FLOW_CUSTOM_SUCCESS;
3548 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3549 GST_DEBUG_OBJECT (pad, "data is passed");
3557 * gst_pad_get_offset:
3560 * Get the offset applied to the running time of @pad. @pad has to be a source
3563 * Returns: the offset.
3566 gst_pad_get_offset (GstPad * pad)
3570 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3572 GST_OBJECT_LOCK (pad);
3573 result = pad->offset;
3574 GST_OBJECT_UNLOCK (pad);
3580 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3582 ev->received = FALSE;
3587 * gst_pad_set_offset:
3589 * @offset: the offset
3591 * Set the offset that will be applied to the running time of @pad.
3594 gst_pad_set_offset (GstPad * pad, gint64 offset)
3596 g_return_if_fail (GST_IS_PAD (pad));
3598 GST_OBJECT_LOCK (pad);
3599 /* if nothing changed, do nothing */
3600 if (pad->offset == offset)
3603 pad->offset = offset;
3604 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3606 /* resend all sticky events with updated offset on next buffer push */
3607 events_foreach (pad, mark_event_not_received, NULL);
3608 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3611 GST_OBJECT_UNLOCK (pad);
3618 /* If TRUE and ret is not OK this means
3619 * that pushing the EOS event failed
3623 /* If called for an event this is
3624 * the event that would be pushed
3625 * next. Don't forward sticky events
3626 * that would come after that */
3630 /* should be called with pad LOCK */
3632 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3634 PushStickyData *data = user_data;
3635 GstEvent *event = ev->event;
3638 GST_DEBUG_OBJECT (pad, "event %s was already received",
3639 GST_EVENT_TYPE_NAME (event));
3643 /* If we're called because of an sticky event, only forward
3644 * events that would come before this new event and the
3646 if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3647 GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3648 GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3649 data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3651 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3652 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3655 switch (data->ret) {
3657 ev->received = TRUE;
3658 GST_DEBUG_OBJECT (pad, "event %s marked received",
3659 GST_EVENT_TYPE_NAME (event));
3661 case GST_FLOW_CUSTOM_SUCCESS:
3662 /* we can't assume the event is received when it was dropped */
3663 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3664 GST_EVENT_TYPE_NAME (event));
3665 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3666 data->ret = GST_FLOW_OK;
3668 case GST_FLOW_CUSTOM_SUCCESS_1:
3669 /* event was ignored and should be sent later */
3670 GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3671 GST_EVENT_TYPE_NAME (event));
3672 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3673 data->ret = GST_FLOW_OK;
3675 case GST_FLOW_NOT_LINKED:
3676 /* not linked is not a problem, we are sticky so the event will be
3677 * sent later but only for non-EOS events */
3678 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3679 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3680 data->ret = GST_FLOW_OK;
3681 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3684 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3685 gst_flow_get_name (data->ret));
3686 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3690 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3691 data->was_eos = TRUE;
3693 return data->ret == GST_FLOW_OK;
3696 /* check sticky events and push them when needed. should be called
3698 static inline GstFlowReturn
3699 check_sticky (GstPad * pad, GstEvent * event)
3701 PushStickyData data = { GST_FLOW_OK, FALSE, event };
3703 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3704 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3706 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3707 events_foreach (pad, push_sticky, &data);
3709 /* If there's an EOS event we must push it downstream
3710 * even if sending a previous sticky event failed.
3711 * Otherwise the pipeline might wait forever for EOS.
3713 * Only do this if pushing another event than the EOS
3716 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3717 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3719 if (ev && !ev->received) {
3720 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3721 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3722 /* the event could have been dropped. Because this can only
3723 * happen if the user asked for it, it's not an error */
3724 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3725 data.ret = GST_FLOW_OK;
3735 * @pad: a #GstPad to invoke the default query on.
3736 * @query: (transfer none): the #GstQuery to perform.
3738 * Dispatches a query to a pad. The query should have been allocated by the
3739 * caller via one of the type-specific allocation functions. The element that
3740 * the pad belongs to is responsible for filling the query with an appropriate
3741 * response, which should then be parsed with a type-specific query parsing
3744 * Again, the caller is responsible for both the allocation and deallocation of
3745 * the query structure.
3747 * Please also note that some queries might need a running pipeline to work.
3749 * Returns: %TRUE if the query could be performed.
3752 gst_pad_query (GstPad * pad, GstQuery * query)
3755 gboolean res, serialized;
3756 GstPadQueryFunction func;
3757 GstPadProbeType type;
3760 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3761 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3763 if (GST_PAD_IS_SRC (pad)) {
3764 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3765 goto wrong_direction;
3766 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3767 } else if (GST_PAD_IS_SINK (pad)) {
3768 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3769 goto wrong_direction;
3770 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3772 goto unknown_direction;
3774 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3775 GST_QUERY_TYPE_NAME (query));
3777 serialized = GST_QUERY_IS_SERIALIZED (query);
3778 if (G_UNLIKELY (serialized))
3779 GST_PAD_STREAM_LOCK (pad);
3781 GST_OBJECT_LOCK (pad);
3782 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3783 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3784 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3786 ACQUIRE_PARENT (pad, parent, no_parent);
3787 GST_OBJECT_UNLOCK (pad);
3789 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3792 res = func (pad, parent, query);
3794 RELEASE_PARENT (parent);
3796 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3797 GST_QUERY_TYPE_NAME (query), res);
3802 GST_OBJECT_LOCK (pad);
3803 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3804 GST_OBJECT_UNLOCK (pad);
3806 if (G_UNLIKELY (serialized))
3807 GST_PAD_STREAM_UNLOCK (pad);
3814 g_warning ("pad %s:%s query %s in wrong direction",
3815 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3820 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3825 GST_DEBUG_OBJECT (pad, "had no parent");
3826 GST_OBJECT_UNLOCK (pad);
3827 if (G_UNLIKELY (serialized))
3828 GST_PAD_STREAM_UNLOCK (pad);
3833 GST_DEBUG_OBJECT (pad, "had no query function");
3834 RELEASE_PARENT (parent);
3835 if (G_UNLIKELY (serialized))
3836 GST_PAD_STREAM_UNLOCK (pad);
3841 GST_DEBUG_OBJECT (pad, "query failed");
3842 if (G_UNLIKELY (serialized))
3843 GST_PAD_STREAM_UNLOCK (pad);
3848 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3849 GST_OBJECT_UNLOCK (pad);
3850 if (G_UNLIKELY (serialized))
3851 GST_PAD_STREAM_UNLOCK (pad);
3853 /* if a probe dropped, we don't sent it further but assume that the probe
3854 * did not answer the query and return FALSE */
3862 * gst_pad_peer_query:
3863 * @pad: a #GstPad to invoke the peer query on.
3864 * @query: (transfer none): the #GstQuery to perform.
3866 * Performs gst_pad_query() on the peer of @pad.
3868 * The caller is responsible for both the allocation and deallocation of
3869 * the query structure.
3871 * Returns: %TRUE if the query could be performed. This function returns %FALSE
3872 * if @pad has no peer.
3875 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3878 GstPadProbeType type;
3879 gboolean res, serialized;
3882 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3883 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3885 if (GST_PAD_IS_SRC (pad)) {
3886 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3887 goto wrong_direction;
3888 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3889 } else if (GST_PAD_IS_SINK (pad)) {
3890 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3891 goto wrong_direction;
3892 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3894 goto unknown_direction;
3896 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3897 GST_QUERY_TYPE_NAME (query));
3899 serialized = GST_QUERY_IS_SERIALIZED (query);
3901 GST_OBJECT_LOCK (pad);
3902 if (GST_PAD_IS_SRC (pad) && serialized) {
3903 /* all serialized queries on the srcpad trigger push of
3905 if (check_sticky (pad, NULL) != GST_FLOW_OK)
3909 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3910 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3911 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3913 peerpad = GST_PAD_PEER (pad);
3914 if (G_UNLIKELY (peerpad == NULL))
3917 gst_object_ref (peerpad);
3918 GST_OBJECT_UNLOCK (pad);
3920 res = gst_pad_query (peerpad, query);
3922 gst_object_unref (peerpad);
3927 GST_OBJECT_LOCK (pad);
3928 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3929 GST_OBJECT_UNLOCK (pad);
3936 g_warning ("pad %s:%s query %s in wrong direction",
3937 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3942 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3947 GST_WARNING_OBJECT (pad, "could not send sticky events");
3948 GST_OBJECT_UNLOCK (pad);
3953 GST_INFO_OBJECT (pad, "pad has no peer");
3954 GST_OBJECT_UNLOCK (pad);
3959 GST_DEBUG_OBJECT (pad, "query failed");
3964 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3965 GST_OBJECT_UNLOCK (pad);
3967 /* if a probe dropped, we don't sent it further but assume that the probe
3968 * did not answer the query and return FALSE */
3975 /**********************************************************************
3976 * Data passing functions
3979 /* this is the chain function that does not perform the additional argument
3980 * checking for that little extra speed.
3982 static inline GstFlowReturn
3983 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3988 GST_PAD_STREAM_LOCK (pad);
3990 GST_OBJECT_LOCK (pad);
3991 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3994 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3997 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4000 #ifndef G_DISABLE_ASSERT
4001 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4002 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4004 ":%s:<%s:%s> Got data flow before stream-start event",
4005 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4007 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4009 ":%s:<%s:%s> Got data flow before segment event",
4010 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4012 pad->priv->last_cookie = pad->priv->events_cookie;
4016 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4018 PROBE_PUSH (pad, type, data, probe_stopped);
4020 parent = GST_OBJECT_PARENT (pad);
4021 GST_OBJECT_UNLOCK (pad);
4023 /* NOTE: we read the chainfunc unlocked.
4024 * we cannot hold the lock for the pad so we might send
4025 * the data to the wrong function. This is not really a
4026 * problem since functions are assigned at creation time
4027 * and don't change that often... */
4028 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4029 GstPadChainFunction chainfunc;
4031 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4034 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4035 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4036 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4038 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4040 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4041 "called chainfunction &%s with buffer %p, returned %s",
4042 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4044 GstPadChainListFunction chainlistfunc;
4046 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4049 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4050 "calling chainlistfunction &%s",
4051 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4053 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4055 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4056 "called chainlistfunction &%s, returned %s",
4057 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4060 GST_PAD_STREAM_UNLOCK (pad);
4067 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4068 "chaining, but pad was flushing");
4069 GST_OBJECT_UNLOCK (pad);
4070 GST_PAD_STREAM_UNLOCK (pad);
4071 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4072 return GST_FLOW_FLUSHING;
4076 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4077 GST_OBJECT_UNLOCK (pad);
4078 GST_PAD_STREAM_UNLOCK (pad);
4079 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4080 return GST_FLOW_EOS;
4084 g_critical ("chain on pad %s:%s but it was not in push mode",
4085 GST_DEBUG_PAD_NAME (pad));
4086 GST_OBJECT_UNLOCK (pad);
4087 GST_PAD_STREAM_UNLOCK (pad);
4088 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4089 return GST_FLOW_ERROR;
4093 GST_OBJECT_UNLOCK (pad);
4094 GST_PAD_STREAM_UNLOCK (pad);
4095 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4098 case GST_FLOW_CUSTOM_SUCCESS:
4099 GST_DEBUG_OBJECT (pad, "dropped buffer");
4103 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4110 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4111 g_critical ("chain on pad %s:%s but it has no chainfunction",
4112 GST_DEBUG_PAD_NAME (pad));
4113 GST_PAD_STREAM_UNLOCK (pad);
4114 return GST_FLOW_NOT_SUPPORTED;
4120 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4121 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4124 * Chain a buffer to @pad.
4126 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4128 * If the buffer type is not acceptable for @pad (as negotiated with a
4129 * preceding GST_EVENT_CAPS event), this function returns
4130 * #GST_FLOW_NOT_NEGOTIATED.
4132 * The function proceeds calling the chain function installed on @pad (see
4133 * gst_pad_set_chain_function()) and the return value of that function is
4134 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4137 * In all cases, success or failure, the caller loses its reference to @buffer
4138 * after calling this function.
4140 * Returns: a #GstFlowReturn from the pad.
4145 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4147 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4148 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4149 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4151 return gst_pad_chain_data_unchecked (pad,
4152 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4155 static GstFlowReturn
4156 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4157 GstBufferList * list)
4163 GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4165 len = gst_buffer_list_length (list);
4168 for (i = 0; i < len; i++) {
4169 buffer = gst_buffer_list_get (list, i);
4171 gst_pad_chain_data_unchecked (pad,
4172 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4173 gst_buffer_ref (buffer));
4174 if (ret != GST_FLOW_OK)
4177 gst_buffer_list_unref (list);
4183 * gst_pad_chain_list:
4184 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4185 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4188 * Chain a bufferlist to @pad.
4190 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4192 * If @pad was not negotiated properly with a CAPS event, this function
4193 * returns #GST_FLOW_NOT_NEGOTIATED.
4195 * The function proceeds calling the chainlist function installed on @pad (see
4196 * gst_pad_set_chain_list_function()) and the return value of that function is
4197 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4198 * chainlist function.
4200 * In all cases, success or failure, the caller loses its reference to @list
4201 * after calling this function.
4205 * Returns: a #GstFlowReturn from the pad.
4208 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4210 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4211 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4212 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4214 return gst_pad_chain_data_unchecked (pad,
4215 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4218 static GstFlowReturn
4219 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4224 GST_OBJECT_LOCK (pad);
4225 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4228 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4231 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4234 #ifndef G_DISABLE_ASSERT
4235 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4236 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4238 ":%s:<%s:%s> Got data flow before stream-start event",
4239 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4241 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4243 ":%s:<%s:%s> Got data flow before segment event",
4244 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4246 pad->priv->last_cookie = pad->priv->events_cookie;
4250 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4253 /* do block probes */
4254 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4256 /* recheck sticky events because the probe might have cause a relink */
4257 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4260 /* do post-blocking probes */
4261 PROBE_PUSH (pad, type, data, probe_stopped);
4263 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4266 /* take ref to peer pad before releasing the lock */
4267 gst_object_ref (peer);
4269 GST_OBJECT_UNLOCK (pad);
4271 ret = gst_pad_chain_data_unchecked (peer, type, data);
4273 gst_object_unref (peer);
4275 GST_OBJECT_LOCK (pad);
4276 pad->ABI.abi.last_flowret = ret;
4278 if (pad->priv->using == 0) {
4279 /* pad is not active anymore, trigger idle callbacks */
4280 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4281 probe_stopped, ret);
4283 GST_OBJECT_UNLOCK (pad);
4287 /* ERROR recovery here */
4291 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4292 "pushing, but pad was flushing");
4293 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4294 GST_OBJECT_UNLOCK (pad);
4295 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4296 return GST_FLOW_FLUSHING;
4300 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4301 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4302 GST_OBJECT_UNLOCK (pad);
4303 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4304 return GST_FLOW_EOS;
4308 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4309 GST_DEBUG_PAD_NAME (pad));
4310 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4311 GST_OBJECT_UNLOCK (pad);
4312 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4313 return GST_FLOW_ERROR;
4317 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4318 "error pushing events, return %s", gst_flow_get_name (ret));
4319 pad->ABI.abi.last_flowret = ret;
4320 GST_OBJECT_UNLOCK (pad);
4321 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4326 GST_OBJECT_UNLOCK (pad);
4327 pad->ABI.abi.last_flowret =
4328 ret == GST_FLOW_CUSTOM_SUCCESS ? GST_FLOW_OK : ret;
4330 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4333 case GST_FLOW_CUSTOM_SUCCESS:
4334 GST_DEBUG_OBJECT (pad, "dropped buffer");
4338 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4345 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4346 "pushing, but it was not linked");
4347 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4348 GST_OBJECT_UNLOCK (pad);
4349 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4350 return GST_FLOW_NOT_LINKED;
4356 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4357 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4360 * Pushes a buffer to the peer of @pad.
4362 * This function will call installed block probes before triggering any
4363 * installed data probes.
4365 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4366 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4369 * In all cases, success or failure, the caller loses its reference to @buffer
4370 * after calling this function.
4372 * Returns: a #GstFlowReturn from the peer pad.
4377 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4379 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4380 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4381 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4383 return gst_pad_push_data (pad,
4384 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4388 * gst_pad_push_list:
4389 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4390 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4393 * Pushes a buffer list to the peer of @pad.
4395 * This function will call installed block probes before triggering any
4396 * installed data probes.
4398 * The function proceeds calling the chain function on the peer pad and returns
4399 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4400 * be returned. If the peer pad does not have any installed chainlist function
4401 * every group buffer of the list will be merged into a normal #GstBuffer and
4402 * chained via gst_pad_chain().
4404 * In all cases, success or failure, the caller loses its reference to @list
4405 * after calling this function.
4407 * Returns: a #GstFlowReturn from the peer pad.
4412 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4414 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4415 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4416 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4418 return gst_pad_push_data (pad,
4419 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4422 static GstFlowReturn
4423 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4424 GstBuffer ** buffer)
4427 GstPadGetRangeFunction getrangefunc;
4431 GST_PAD_STREAM_LOCK (pad);
4433 GST_OBJECT_LOCK (pad);
4434 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4437 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4440 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4445 /* when one of the probes returns DROPPED, probe_stopped will be called
4446 * and we skip calling the getrange function, res_buf should then contain a
4447 * valid result buffer */
4448 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4449 res_buf, offset, size, probe_stopped);
4451 /* recheck sticky events because the probe might have cause a relink */
4452 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4455 ACQUIRE_PARENT (pad, parent, no_parent);
4456 GST_OBJECT_UNLOCK (pad);
4458 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4461 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4462 "calling getrangefunc %s, offset %"
4463 G_GUINT64_FORMAT ", size %u",
4464 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4466 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4468 RELEASE_PARENT (parent);
4470 GST_OBJECT_LOCK (pad);
4471 if (G_UNLIKELY (ret != GST_FLOW_OK))
4472 goto get_range_failed;
4474 /* can only fire the signal if we have a valid buffer */
4476 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4477 res_buf, offset, size, probe_stopped_unref);
4478 pad->ABI.abi.last_flowret = ret;
4479 GST_OBJECT_UNLOCK (pad);
4481 GST_PAD_STREAM_UNLOCK (pad);
4490 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4491 "getrange, but pad was flushing");
4492 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4493 GST_OBJECT_UNLOCK (pad);
4494 GST_PAD_STREAM_UNLOCK (pad);
4495 return GST_FLOW_FLUSHING;
4499 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4500 GST_DEBUG_PAD_NAME (pad));
4501 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4502 GST_OBJECT_UNLOCK (pad);
4503 GST_PAD_STREAM_UNLOCK (pad);
4504 return GST_FLOW_ERROR;
4508 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4509 pad->ABI.abi.last_flowret = ret;
4510 GST_OBJECT_UNLOCK (pad);
4511 GST_PAD_STREAM_UNLOCK (pad);
4516 GST_DEBUG_OBJECT (pad, "no parent");
4517 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4518 GST_OBJECT_UNLOCK (pad);
4519 GST_PAD_STREAM_UNLOCK (pad);
4520 return GST_FLOW_FLUSHING;
4524 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4525 GST_DEBUG_PAD_NAME (pad));
4526 RELEASE_PARENT (parent);
4527 GST_PAD_STREAM_UNLOCK (pad);
4528 return GST_FLOW_NOT_SUPPORTED;
4532 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4533 "probe returned %s", gst_flow_get_name (ret));
4534 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4536 /* the probe filled the buffer and asks us to not call the getrange
4537 * anymore, we continue with the post probes then. */
4538 GST_DEBUG_OBJECT (pad, "handled buffer");
4542 /* no buffer, we are EOS */
4543 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4547 pad->ABI.abi.last_flowret = ret;
4548 GST_OBJECT_UNLOCK (pad);
4549 GST_PAD_STREAM_UNLOCK (pad);
4553 probe_stopped_unref:
4555 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4556 "probe returned %s", gst_flow_get_name (ret));
4557 /* if we drop here, it signals EOS */
4558 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4560 pad->ABI.abi.last_flowret = ret;
4561 GST_OBJECT_UNLOCK (pad);
4562 GST_PAD_STREAM_UNLOCK (pad);
4563 if (*buffer == NULL)
4564 gst_buffer_unref (res_buf);
4569 pad->ABI.abi.last_flowret = ret;
4570 GST_OBJECT_UNLOCK (pad);
4571 GST_PAD_STREAM_UNLOCK (pad);
4572 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4573 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4574 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4580 * gst_pad_get_range:
4581 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4582 * @offset: The start offset of the buffer
4583 * @size: The length of the buffer
4584 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4585 * returns #GST_FLOW_ERROR if %NULL.
4587 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4588 * immediately and @buffer is %NULL.
4590 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4591 * description of a getrange function. If @pad has no getrange function
4592 * installed (see gst_pad_set_getrange_function()) this function returns
4593 * #GST_FLOW_NOT_SUPPORTED.
4595 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4596 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4597 * must be freed with gst_buffer_unref() after usage.
4599 * When @buffer points to a variable that points to a valid #GstBuffer, the
4600 * buffer will be filled with the result data when this function returns
4601 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4602 * @size bytes will be filled in the result buffer and its size will be updated
4605 * Note that less than @size bytes can be returned in @buffer when, for example,
4606 * an EOS condition is near or when @buffer is not large enough to hold @size
4607 * bytes. The caller should check the result buffer size to get the result size.
4609 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4610 * will be unchanged.
4612 * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4614 * Returns: a #GstFlowReturn from the pad.
4619 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4620 GstBuffer ** buffer)
4622 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4623 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4624 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4625 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4626 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4628 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4632 * gst_pad_pull_range:
4633 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4634 * @offset: The start offset of the buffer
4635 * @size: The length of the buffer
4636 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4637 * GST_FLOW_ERROR if %NULL.
4639 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4641 * This function will first trigger the pad block signal if it was
4644 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4645 * function returns the result of gst_pad_get_range() on the peer pad.
4646 * See gst_pad_get_range() for a list of return values and for the
4647 * semantics of the arguments of this function.
4649 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4650 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4651 * must be freed with gst_buffer_unref() after usage. When this function
4652 * returns any other result value, @buffer will still point to %NULL.
4654 * When @buffer points to a variable that points to a valid #GstBuffer, the
4655 * buffer will be filled with the result data when this function returns
4656 * #GST_FLOW_OK. When this function returns any other result value,
4657 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4658 * @size bytes will be filled in the result buffer and its size will be updated
4661 * Note that less than @size bytes can be returned in @buffer when, for example,
4662 * an EOS condition is near or when @buffer is not large enough to hold @size
4663 * bytes. The caller should check the result buffer size to get the result size.
4665 * Returns: a #GstFlowReturn from the peer pad.
4670 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4671 GstBuffer ** buffer)
4677 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4678 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4679 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4680 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4681 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4683 GST_OBJECT_LOCK (pad);
4684 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4687 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4692 /* when one of the probes returns DROPPED, probe_stopped will be
4693 * called and we skip calling the peer getrange function. *buffer should then
4694 * contain a valid buffer */
4695 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4696 res_buf, offset, size, probe_stopped);
4698 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4701 gst_object_ref (peer);
4703 GST_OBJECT_UNLOCK (pad);
4705 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4707 gst_object_unref (peer);
4709 GST_OBJECT_LOCK (pad);
4711 pad->ABI.abi.last_flowret = ret;
4712 if (pad->priv->using == 0) {
4713 /* pad is not active anymore, trigger idle callbacks */
4714 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4715 probe_stopped_unref, ret);
4718 if (G_UNLIKELY (ret != GST_FLOW_OK))
4719 goto pull_range_failed;
4722 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4723 res_buf, offset, size, probe_stopped_unref);
4725 GST_OBJECT_UNLOCK (pad);
4731 /* ERROR recovery here */
4734 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4735 "pullrange, but pad was flushing");
4736 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4737 GST_OBJECT_UNLOCK (pad);
4738 return GST_FLOW_FLUSHING;
4742 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4743 GST_DEBUG_PAD_NAME (pad));
4744 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4745 GST_OBJECT_UNLOCK (pad);
4746 return GST_FLOW_ERROR;
4750 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4751 gst_flow_get_name (ret));
4752 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4754 /* the probe filled the buffer and asks us to not forward to the peer
4755 * anymore, we continue with the post probes then */
4756 GST_DEBUG_OBJECT (pad, "handled buffer");
4760 /* no buffer, we are EOS then */
4761 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4765 pad->ABI.abi.last_flowret = ret;
4766 GST_OBJECT_UNLOCK (pad);
4771 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4772 "pulling range, but it was not linked");
4773 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4774 GST_OBJECT_UNLOCK (pad);
4775 return GST_FLOW_NOT_LINKED;
4779 pad->ABI.abi.last_flowret = ret;
4780 GST_OBJECT_UNLOCK (pad);
4781 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4782 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4783 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4786 probe_stopped_unref:
4788 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4789 "post probe returned %s", gst_flow_get_name (ret));
4791 /* if we drop here, it signals EOS */
4792 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4795 pad->ABI.abi.last_flowret = ret;
4796 GST_OBJECT_UNLOCK (pad);
4798 if (*buffer == NULL)
4799 gst_buffer_unref (res_buf);
4804 /* must be called with pad object lock */
4805 static GstFlowReturn
4806 store_sticky_event (GstPad * pad, GstEvent * event)
4811 gboolean res = FALSE;
4812 const gchar *name = NULL;
4813 gboolean insert = TRUE;
4815 type = GST_EVENT_TYPE (event);
4817 /* Store all sticky events except SEGMENT/EOS when we're flushing,
4818 * otherwise they can be dropped and nothing would ever resend them.
4819 * Only do that for activated pads though, everything else is a bug!
4821 if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4822 || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4823 || type == GST_EVENT_EOS))))
4826 /* Unset the EOS flag when received STREAM_START event, so pad can
4827 * store sticky event and then push it later */
4828 if (type == GST_EVENT_STREAM_START) {
4829 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4830 remove_event_by_type (pad, GST_EVENT_EOS);
4831 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4834 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4837 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4838 name = gst_structure_get_name (gst_event_get_structure (event));
4840 events = pad->priv->events;
4843 for (i = 0; i < len; i++) {
4844 PadEvent *ev = &g_array_index (events, PadEvent, i);
4846 if (ev->event == NULL)
4849 if (type == GST_EVENT_TYPE (ev->event)) {
4850 /* matching types, check matching name if needed */
4851 if (name && !gst_event_has_name (ev->event, name))
4855 if ((res = gst_event_replace (&ev->event, event)))
4856 ev->received = FALSE;
4862 if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4863 && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4864 /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4865 * storing the sticky ordered we can check that this is respected. */
4866 if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4867 || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4869 ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4870 G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4871 gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4872 gst_event_type_get_name (type));
4878 ev.event = gst_event_ref (event);
4879 ev.received = FALSE;
4880 g_array_insert_val (events, i, ev);
4885 pad->priv->events_cookie++;
4886 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4888 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4890 switch (GST_EVENT_TYPE (event)) {
4891 case GST_EVENT_CAPS:
4892 GST_OBJECT_UNLOCK (pad);
4894 GST_DEBUG_OBJECT (pad, "notify caps");
4895 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4897 GST_OBJECT_LOCK (pad);
4903 if (type == GST_EVENT_EOS) {
4904 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4905 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4908 return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4913 GST_DEBUG_OBJECT (pad, "pad is flushing");
4914 return GST_FLOW_FLUSHING;
4918 GST_DEBUG_OBJECT (pad, "pad is EOS");
4919 return GST_FLOW_EOS;
4924 * gst_pad_store_sticky_event:
4926 * @event: a #GstEvent
4928 * Store the sticky @event on @pad
4930 * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
4931 * was flushing or #GST_FLOW_EOS when the pad was EOS.
4936 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4940 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4941 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4943 GST_OBJECT_LOCK (pad);
4944 ret = store_sticky_event (pad, event);
4945 GST_OBJECT_UNLOCK (pad);
4951 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
4953 PushStickyData *data = user_data;
4955 /* Forward all sticky events before our current one that are pending */
4956 if (ev->event != data->event
4957 && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
4958 return push_sticky (pad, ev, data);
4963 /* should be called with pad LOCK */
4964 static GstFlowReturn
4965 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4966 GstPadProbeType type)
4970 GstEventType event_type;
4972 /* pass the adjusted event on. We need to do this even if
4973 * there is no peer pad because of the probes. */
4974 event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
4976 /* Two checks to be made:
4977 * . (un)set the FLUSHING flag for flushing events,
4978 * . handle pad blocking */
4979 event_type = GST_EVENT_TYPE (event);
4980 switch (event_type) {
4981 case GST_EVENT_FLUSH_START:
4982 GST_PAD_SET_FLUSHING (pad);
4984 GST_PAD_BLOCK_BROADCAST (pad);
4985 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4987 case GST_EVENT_FLUSH_STOP:
4988 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
4991 GST_PAD_UNSET_FLUSHING (pad);
4993 /* Remove sticky EOS events */
4994 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4995 remove_event_by_type (pad, GST_EVENT_EOS);
4996 remove_event_by_type (pad, GST_EVENT_SEGMENT);
4997 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4998 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5000 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5004 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5007 /* No need to check for EOS here as either the caller (gst_pad_push_event())
5008 * checked already or this is called as part of pushing sticky events,
5009 * in which case we still want to forward the EOS event downstream.
5012 switch (GST_EVENT_TYPE (event)) {
5013 case GST_EVENT_RECONFIGURE:
5014 if (GST_PAD_IS_SINK (pad))
5015 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5020 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5021 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5026 /* send probes after modifying the events above */
5027 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5029 /* recheck sticky events because the probe might have cause a relink */
5030 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5031 && (GST_EVENT_IS_SERIALIZED (event)
5032 || GST_EVENT_IS_STICKY (event))) {
5033 PushStickyData data = { GST_FLOW_OK, FALSE, event };
5034 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5036 /* Push all sticky events before our current one
5037 * that have changed */
5038 events_foreach (pad, sticky_changed, &data);
5041 /* now check the peer pad */
5042 peerpad = GST_PAD_PEER (pad);
5043 if (peerpad == NULL)
5046 gst_object_ref (peerpad);
5048 GST_OBJECT_UNLOCK (pad);
5050 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5051 event, gst_event_type_get_name (event_type), peerpad);
5053 ret = gst_pad_send_event_unchecked (peerpad, event, type);
5055 /* Note: we gave away ownership of the event at this point but we can still
5056 * print the old pointer */
5057 GST_LOG_OBJECT (pad,
5058 "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5059 gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5061 gst_object_unref (peerpad);
5063 GST_OBJECT_LOCK (pad);
5065 if (pad->priv->using == 0) {
5066 /* pad is not active anymore, trigger idle callbacks */
5067 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5068 idle_probe_stopped, ret);
5072 /* ERROR handling */
5075 GST_DEBUG_OBJECT (pad, "We're flushing");
5076 gst_event_unref (event);
5077 return GST_FLOW_FLUSHING;
5081 GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5082 gst_event_unref (event);
5083 return GST_FLOW_FLUSHING;
5087 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5088 gst_event_unref (event);
5091 case GST_FLOW_CUSTOM_SUCCESS:
5092 GST_DEBUG_OBJECT (pad, "dropped event");
5095 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5102 GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5103 gst_event_type_get_name (GST_EVENT_TYPE (event)));
5104 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5105 gst_event_unref (event);
5107 /* unlinked pads should not influence latency configuration */
5108 if (event_type == GST_EVENT_LATENCY)
5111 return GST_FLOW_NOT_LINKED;
5115 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5121 * gst_pad_push_event:
5122 * @pad: a #GstPad to push the event to.
5123 * @event: (transfer full): the #GstEvent to send to the pad.
5125 * Sends the event to the peer of the given pad. This function is
5126 * mainly used by elements to send events to their peer
5129 * This function takes ownership of the provided event so you should
5130 * gst_event_ref() it if you want to reuse the event after this call.
5132 * Returns: %TRUE if the event was handled.
5137 gst_pad_push_event (GstPad * pad, GstEvent * event)
5139 gboolean res = FALSE;
5140 GstPadProbeType type;
5141 gboolean sticky, serialized;
5143 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5144 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5146 if (GST_PAD_IS_SRC (pad)) {
5147 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5148 goto wrong_direction;
5149 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5150 } else if (GST_PAD_IS_SINK (pad)) {
5151 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5152 goto wrong_direction;
5153 /* events pushed on sinkpad never are sticky */
5154 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5156 goto unknown_direction;
5158 GST_OBJECT_LOCK (pad);
5159 sticky = GST_EVENT_IS_STICKY (event);
5160 serialized = GST_EVENT_IS_SERIALIZED (event);
5163 /* srcpad sticky events are stored immediately, the received flag is set
5164 * to FALSE and will be set to TRUE when we can successfully push the
5165 * event to the peer pad */
5166 switch (store_sticky_event (pad, event)) {
5167 case GST_FLOW_FLUSHING:
5175 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5176 /* all serialized or sticky events on the srcpad trigger push of
5178 res = (check_sticky (pad, event) == GST_FLOW_OK);
5183 /* other events are pushed right away */
5184 ret = gst_pad_push_event_unchecked (pad, event, type);
5185 /* dropped events by a probe are not an error */
5186 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
5188 /* Errors in sticky event pushing are no problem and ignored here
5189 * as they will cause more meaningful errors during data flow.
5190 * For EOS events, that are not followed by data flow, we still
5191 * return FALSE here though.
5193 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5195 gst_event_unref (event);
5197 GST_OBJECT_UNLOCK (pad);
5201 /* ERROR handling */
5204 g_warning ("pad %s:%s pushing %s event in wrong direction",
5205 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5206 gst_event_unref (event);
5211 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5212 gst_event_unref (event);
5217 GST_DEBUG_OBJECT (pad, "We're flushing");
5218 GST_OBJECT_UNLOCK (pad);
5219 gst_event_unref (event);
5224 GST_DEBUG_OBJECT (pad, "We're EOS");
5225 GST_OBJECT_UNLOCK (pad);
5226 gst_event_unref (event);
5231 /* Check if we can call the event function with the given event */
5232 static GstFlowReturn
5233 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5237 switch (GST_EVENT_TYPE (event)) {
5238 case GST_EVENT_CAPS:
5240 /* backwards compatibility mode for caps */
5241 gst_event_parse_caps (event, &caps);
5243 if (!gst_pad_query_accept_caps (pad, caps))
5255 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5256 "caps %" GST_PTR_FORMAT " not accepted", caps);
5257 return GST_FLOW_NOT_NEGOTIATED;
5261 static GstFlowReturn
5262 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5263 GstPadProbeType type)
5266 GstEventType event_type;
5267 gboolean serialized, need_unlock = FALSE, sticky;
5268 GstPadEventFunction eventfunc;
5271 GST_OBJECT_LOCK (pad);
5273 event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5275 if (GST_PAD_IS_SINK (pad))
5276 serialized = GST_EVENT_IS_SERIALIZED (event);
5279 sticky = GST_EVENT_IS_STICKY (event);
5280 event_type = GST_EVENT_TYPE (event);
5281 switch (event_type) {
5282 case GST_EVENT_FLUSH_START:
5283 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5284 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5286 /* can't even accept a flush begin event when flushing */
5287 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5290 GST_PAD_SET_FLUSHING (pad);
5291 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5293 case GST_EVENT_FLUSH_STOP:
5294 /* we can't accept flush-stop on inactive pads else the flushing flag
5295 * would be cleared and it would look like the pad can accept data.
5296 * Also, some elements restart a streaming thread in flush-stop which we
5297 * can't allow on inactive pads */
5298 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5301 GST_PAD_UNSET_FLUSHING (pad);
5302 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5303 /* Remove pending EOS events */
5304 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5305 remove_event_by_type (pad, GST_EVENT_EOS);
5306 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5307 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5308 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5310 GST_OBJECT_UNLOCK (pad);
5311 /* grab stream lock */
5312 GST_PAD_STREAM_LOCK (pad);
5314 GST_OBJECT_LOCK (pad);
5315 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5318 case GST_EVENT_RECONFIGURE:
5319 if (GST_PAD_IS_SRC (pad))
5320 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5322 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5323 "have event type %" GST_PTR_FORMAT, event);
5325 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5328 switch (event_type) {
5329 case GST_EVENT_STREAM_START:
5330 /* Remove sticky EOS events */
5331 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5332 remove_event_by_type (pad, GST_EVENT_EOS);
5333 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5340 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5343 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5344 GST_OBJECT_UNLOCK (pad);
5345 GST_PAD_STREAM_LOCK (pad);
5347 GST_OBJECT_LOCK (pad);
5348 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5351 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5357 /* now do the probe */
5359 type | GST_PAD_PROBE_TYPE_PUSH |
5360 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5362 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5364 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5367 ACQUIRE_PARENT (pad, parent, no_parent);
5368 GST_OBJECT_UNLOCK (pad);
5370 ret = pre_eventfunc_check (pad, event);
5371 if (G_UNLIKELY (ret != GST_FLOW_OK))
5372 goto precheck_failed;
5375 gst_event_ref (event);
5377 if (eventfunc (pad, parent, event)) {
5380 /* something went wrong */
5381 switch (event_type) {
5382 case GST_EVENT_CAPS:
5383 ret = GST_FLOW_NOT_NEGOTIATED;
5386 ret = GST_FLOW_ERROR;
5390 RELEASE_PARENT (parent);
5392 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5395 if (ret == GST_FLOW_OK) {
5396 GST_OBJECT_LOCK (pad);
5397 /* after the event function accepted the event, we can store the sticky
5398 * event on the pad */
5399 switch (store_sticky_event (pad, event)) {
5400 case GST_FLOW_FLUSHING:
5407 GST_OBJECT_UNLOCK (pad);
5409 gst_event_unref (event);
5413 GST_PAD_STREAM_UNLOCK (pad);
5417 /* ERROR handling */
5420 GST_OBJECT_UNLOCK (pad);
5422 GST_PAD_STREAM_UNLOCK (pad);
5423 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5424 "Received event on flushing pad. Discarding");
5425 gst_event_unref (event);
5426 return GST_FLOW_FLUSHING;
5430 GST_OBJECT_UNLOCK (pad);
5432 GST_PAD_STREAM_UNLOCK (pad);
5433 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5434 "Received flush-stop on inactive pad. Discarding");
5435 gst_event_unref (event);
5436 return GST_FLOW_FLUSHING;
5440 GST_OBJECT_UNLOCK (pad);
5442 GST_PAD_STREAM_UNLOCK (pad);
5443 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5444 "Received event on EOS pad. Discarding");
5445 gst_event_unref (event);
5446 return GST_FLOW_EOS;
5450 GST_OBJECT_UNLOCK (pad);
5452 GST_PAD_STREAM_UNLOCK (pad);
5453 gst_event_unref (event);
5456 case GST_FLOW_CUSTOM_SUCCESS:
5457 GST_DEBUG_OBJECT (pad, "dropped event");
5461 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5468 g_warning ("pad %s:%s has no event handler, file a bug.",
5469 GST_DEBUG_PAD_NAME (pad));
5470 GST_OBJECT_UNLOCK (pad);
5472 GST_PAD_STREAM_UNLOCK (pad);
5473 gst_event_unref (event);
5474 return GST_FLOW_NOT_SUPPORTED;
5478 GST_DEBUG_OBJECT (pad, "no parent");
5479 GST_OBJECT_UNLOCK (pad);
5481 GST_PAD_STREAM_UNLOCK (pad);
5482 gst_event_unref (event);
5483 return GST_FLOW_FLUSHING;
5487 GST_DEBUG_OBJECT (pad, "pre event check failed");
5488 RELEASE_PARENT (parent);
5490 GST_PAD_STREAM_UNLOCK (pad);
5491 gst_event_unref (event);
5497 * gst_pad_send_event:
5498 * @pad: a #GstPad to send the event to.
5499 * @event: (transfer full): the #GstEvent to send to the pad.
5501 * Sends the event to the pad. This function can be used
5502 * by applications to send events in the pipeline.
5504 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5505 * sink pad, @event should be a downstream event. For example, you would not
5506 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5507 * Furthermore, some downstream events have to be serialized with data flow,
5508 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5509 * the event needs to be serialized with data flow, this function will take the
5510 * pad's stream lock while calling its event function.
5512 * To find out whether an event type is upstream, downstream, or downstream and
5513 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5514 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5515 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5516 * plugin doesn't need to bother itself with this information; the core handles
5517 * all necessary locks and checks.
5519 * This function takes ownership of the provided event so you should
5520 * gst_event_ref() it if you want to reuse the event after this call.
5522 * Returns: %TRUE if the event was handled.
5525 gst_pad_send_event (GstPad * pad, GstEvent * event)
5528 GstPadProbeType type;
5530 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5531 g_return_val_if_fail (event != NULL, FALSE);
5533 if (GST_PAD_IS_SINK (pad)) {
5534 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5535 goto wrong_direction;
5536 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5537 } else if (GST_PAD_IS_SRC (pad)) {
5538 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5539 goto wrong_direction;
5540 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5542 goto unknown_direction;
5544 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5551 /* ERROR handling */
5554 g_warning ("pad %s:%s sending %s event in wrong direction",
5555 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5556 gst_event_unref (event);
5561 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5562 gst_event_unref (event);
5568 * gst_pad_set_element_private:
5569 * @pad: the #GstPad to set the private data of.
5570 * @priv: The private data to attach to the pad.
5572 * Set the given private data gpointer on the pad.
5573 * This function can only be used by the element that owns the pad.
5574 * No locking is performed in this function.
5577 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5579 pad->element_private = priv;
5583 * gst_pad_get_element_private:
5584 * @pad: the #GstPad to get the private data of.
5586 * Gets the private data of a pad.
5587 * No locking is performed in this function.
5589 * Returns: (transfer none): a #gpointer to the private data.
5592 gst_pad_get_element_private (GstPad * pad)
5594 return pad->element_private;
5598 * gst_pad_get_sticky_event:
5599 * @pad: the #GstPad to get the event from.
5600 * @event_type: the #GstEventType that should be retrieved.
5601 * @idx: the index of the event
5603 * Returns a new reference of the sticky event of type @event_type
5606 * Returns: (transfer full) (nullable): a #GstEvent of type
5607 * @event_type or %NULL when no event of @event_type was on
5608 * @pad. Unref after usage.
5611 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5613 GstEvent *event = NULL;
5616 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5617 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5619 GST_OBJECT_LOCK (pad);
5620 ev = find_event_by_type (pad, event_type, idx);
5621 if (ev && (event = ev->event))
5622 gst_event_ref (event);
5623 GST_OBJECT_UNLOCK (pad);
5630 GstPadStickyEventsForeachFunction func;
5635 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5637 ForeachDispatch *data = user_data;
5638 gboolean ret = TRUE;
5641 GST_OBJECT_UNLOCK (pad);
5643 ret = data->func (pad, &ev->event, data->user_data);
5645 GST_OBJECT_LOCK (pad);
5652 * gst_pad_sticky_events_foreach:
5653 * @pad: the #GstPad that should be used for iteration.
5654 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5655 * should be called for every event.
5656 * @user_data: (closure): the optional user data.
5658 * Iterates all sticky events on @pad and calls @foreach_func for every
5659 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5662 gst_pad_sticky_events_foreach (GstPad * pad,
5663 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5665 ForeachDispatch data;
5667 g_return_if_fail (GST_IS_PAD (pad));
5668 g_return_if_fail (foreach_func != NULL);
5670 data.func = foreach_func;
5671 data.user_data = user_data;
5673 GST_OBJECT_LOCK (pad);
5674 events_foreach (pad, foreach_dispatch_function, &data);
5675 GST_OBJECT_UNLOCK (pad);
5679 do_stream_status (GstPad * pad, GstStreamStatusType type,
5680 GThread * thread, GstTask * task)
5684 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5686 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5687 if (GST_IS_ELEMENT (parent)) {
5688 GstMessage *message;
5689 GValue value = { 0 };
5691 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5692 gchar *tname, *ename, *pname;
5694 /* create a good task name */
5695 ename = gst_element_get_name (parent);
5696 pname = gst_pad_get_name (pad);
5697 tname = g_strdup_printf ("%s:%s", ename, pname);
5701 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5705 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5708 g_value_init (&value, GST_TYPE_TASK);
5709 g_value_set_object (&value, task);
5710 gst_message_set_stream_status_object (message, &value);
5711 g_value_unset (&value);
5713 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5714 gst_element_post_message (parent, message);
5716 gst_object_unref (parent);
5721 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5723 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5728 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5730 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5735 * gst_pad_start_task:
5736 * @pad: the #GstPad to start the task of
5737 * @func: the task function to call
5738 * @user_data: user data passed to the task function
5739 * @notify: called when @user_data is no longer referenced
5741 * Starts a task that repeatedly calls @func with @user_data. This function
5742 * is mostly used in pad activation functions to start the dataflow.
5743 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5744 * before @func is called.
5746 * Returns: a %TRUE if the task could be started.
5749 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5750 GDestroyNotify notify)
5755 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5756 g_return_val_if_fail (func != NULL, FALSE);
5758 GST_DEBUG_OBJECT (pad, "start task");
5760 GST_OBJECT_LOCK (pad);
5761 task = GST_PAD_TASK (pad);
5763 task = gst_task_new (func, user_data, notify);
5764 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5765 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5766 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5767 GST_INFO_OBJECT (pad, "created task %p", task);
5768 GST_PAD_TASK (pad) = task;
5769 gst_object_ref (task);
5770 /* release lock to post the message */
5771 GST_OBJECT_UNLOCK (pad);
5773 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5775 gst_object_unref (task);
5777 GST_OBJECT_LOCK (pad);
5778 /* nobody else is supposed to have changed the pad now */
5779 if (GST_PAD_TASK (pad) != task)
5780 goto concurrent_stop;
5782 res = gst_task_set_state (task, GST_TASK_STARTED);
5783 GST_OBJECT_UNLOCK (pad);
5790 GST_OBJECT_UNLOCK (pad);
5796 * gst_pad_pause_task:
5797 * @pad: the #GstPad to pause the task of
5799 * Pause the task of @pad. This function will also wait until the
5800 * function executed by the task is finished if this function is not
5801 * called from the task function.
5803 * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5807 gst_pad_pause_task (GstPad * pad)
5812 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5814 GST_DEBUG_OBJECT (pad, "pause task");
5816 GST_OBJECT_LOCK (pad);
5817 task = GST_PAD_TASK (pad);
5820 res = gst_task_set_state (task, GST_TASK_PAUSED);
5821 GST_OBJECT_UNLOCK (pad);
5823 /* wait for task function to finish, this lock is recursive so it does nothing
5824 * when the pause is called from the task itself */
5825 GST_PAD_STREAM_LOCK (pad);
5826 GST_PAD_STREAM_UNLOCK (pad);
5832 GST_DEBUG_OBJECT (pad, "pad has no task");
5833 GST_OBJECT_UNLOCK (pad);
5839 * gst_pad_stop_task:
5840 * @pad: the #GstPad to stop the task of
5842 * Stop the task of @pad. This function will also make sure that the
5843 * function executed by the task will effectively stop if not called
5844 * from the GstTaskFunction.
5846 * This function will deadlock if called from the GstTaskFunction of
5847 * the task. Use gst_task_pause() instead.
5849 * Regardless of whether the pad has a task, the stream lock is acquired and
5850 * released so as to ensure that streaming through this pad has finished.
5852 * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5855 gst_pad_stop_task (GstPad * pad)
5860 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5862 GST_DEBUG_OBJECT (pad, "stop task");
5864 GST_OBJECT_LOCK (pad);
5865 task = GST_PAD_TASK (pad);
5868 GST_PAD_TASK (pad) = NULL;
5869 res = gst_task_set_state (task, GST_TASK_STOPPED);
5870 GST_OBJECT_UNLOCK (pad);
5872 GST_PAD_STREAM_LOCK (pad);
5873 GST_PAD_STREAM_UNLOCK (pad);
5875 if (!gst_task_join (task))
5878 gst_object_unref (task);
5884 GST_DEBUG_OBJECT (pad, "no task");
5885 GST_OBJECT_UNLOCK (pad);
5887 GST_PAD_STREAM_LOCK (pad);
5888 GST_PAD_STREAM_UNLOCK (pad);
5890 /* this is not an error */
5895 /* this is bad, possibly the application tried to join the task from
5896 * the task's thread. We install the task again so that it will be stopped
5897 * again from the right thread next time hopefully. */
5898 GST_OBJECT_LOCK (pad);
5899 GST_DEBUG_OBJECT (pad, "join failed");
5900 /* we can only install this task if there was no other task */
5901 if (GST_PAD_TASK (pad) == NULL)
5902 GST_PAD_TASK (pad) = task;
5903 GST_OBJECT_UNLOCK (pad);
5910 * gst_pad_probe_info_get_event:
5911 * @info: a #GstPadProbeInfo
5913 * Returns: (transfer none): The #GstEvent from the probe
5917 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5919 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5920 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5922 return GST_PAD_PROBE_INFO_EVENT (info);
5927 * gst_pad_probe_info_get_query:
5928 * @info: a #GstPadProbeInfo
5930 * Returns: (transfer none): The #GstQuery from the probe
5934 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5936 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5937 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5939 return GST_PAD_PROBE_INFO_QUERY (info);
5943 * gst_pad_probe_info_get_buffer:
5944 * @info: a #GstPadProbeInfo
5946 * Returns: (transfer none): The #GstBuffer from the probe
5950 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5952 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5954 return GST_PAD_PROBE_INFO_BUFFER (info);
5958 * gst_pad_probe_info_get_bufferlist:
5959 * @info: a #GstPadProbeInfo
5961 * Returns: (transfer none): The #GstBufferlist from the probe
5965 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5967 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5969 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5973 * gst_pad_get_last_flow_return:
5976 * Gets the #GstFlowReturn return from the last data passed by this pad.
5981 gst_pad_get_last_flow_return (GstPad * pad)
5985 GST_OBJECT_LOCK (pad);
5986 ret = GST_PAD_LAST_FLOW_RETURN (pad);
5987 GST_OBJECT_UNLOCK (pad);