pad: Log pad offsets as signed times
[platform/upstream/gstreamer.git] / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpad.c: Pads for linking elements together
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:gstpad
24  * @short_description: Object contained by elements that allows links to
25  *                     other elements
26  * @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
27  *
28  * A #GstElement is linked to other elements via "pads", which are extremely
29  * light-weight generic link points.
30  *
31  * Pads have a #GstPadDirection, source pads produce data, sink pads consume
32  * data.
33  *
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.
39  *
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.
43  *
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.
47  *
48  * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
49  *
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
55  * linked to.
56  *
57  * Before dataflow is possible on the pads, they need to be activated with
58  * gst_pad_set_active().
59  *
60  * gst_pad_query() and gst_pad_peer_query() can be used to query various
61  * properties of the pad and the stream.
62  *
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.
69  *
70  * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
71  * or pull in a buffer.
72  *
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.
79  *
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.
83  *
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()
86  * respectively.
87  */
88
89 #include "gst_private.h"
90
91 #include "gstpad.h"
92 #include "gstpadtemplate.h"
93 #include "gstenumtypes.h"
94 #include "gstutils.h"
95 #include "gstinfo.h"
96 #include "gsterror.h"
97 #include "gsttracerutils.h"
98 #include "gstvalue.h"
99 #include "glib-compat-private.h"
100
101 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
102 #define GST_CAT_DEFAULT GST_CAT_PADS
103
104 /* Pad signals and args */
105 enum
106 {
107   PAD_LINKED,
108   PAD_UNLINKED,
109   /* FILL ME */
110   LAST_SIGNAL
111 };
112
113 enum
114 {
115   PAD_PROP_0,
116   PAD_PROP_CAPS,
117   PAD_PROP_DIRECTION,
118   PAD_PROP_TEMPLATE,
119   PAD_PROP_OFFSET
120       /* FILL ME */
121 };
122
123 #define GST_PAD_GET_PRIVATE(obj)  \
124    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
125
126 #define _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH (GST_PAD_PROBE_TYPE_ALL_BOTH | GST_PAD_PROBE_TYPE_EVENT_FLUSH)
127
128 /* we have a pending and an active event on the pad. On source pads only the
129  * active event is used. On sinkpads, events are copied to the pending entry and
130  * moved to the active event when the eventfunc returned %TRUE. */
131 typedef struct
132 {
133   gboolean received;
134   GstEvent *event;
135 } PadEvent;
136
137 struct _GstPadPrivate
138 {
139   guint events_cookie;
140   GArray *events;
141   guint last_cookie;
142
143   gint using;
144   guint probe_list_cookie;
145   guint probe_cookie;
146
147   /* counter of how many idle probes are running directly from the add_probe
148    * call. Used to block any data flowing in the pad while the idle callback
149    * Doesn't finish its work */
150   gint idle_running;
151 };
152
153 typedef struct
154 {
155   GHook hook;
156   guint cookie;
157 } GstProbe;
158
159 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
160 #define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
161     (((GstPad *)(p))->priv->idle_running > 0)
162
163 typedef struct
164 {
165   GstPad *pad;
166   GstPadProbeInfo *info;
167   gboolean dropped;
168   gboolean pass;
169   gboolean handled;
170   gboolean marshalled;
171   guint cookie;
172 } ProbeMarshall;
173
174 static void gst_pad_dispose (GObject * object);
175 static void gst_pad_finalize (GObject * object);
176 static void gst_pad_set_property (GObject * object, guint prop_id,
177     const GValue * value, GParamSpec * pspec);
178 static void gst_pad_get_property (GObject * object, guint prop_id,
179     GValue * value, GParamSpec * pspec);
180
181 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
182 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
183 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
184     GstObject * parent, GstBufferList * list);
185
186 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
187     GstEvent * event, GstPadProbeType type);
188 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
189     GstEvent * event, GstPadProbeType type);
190
191 static gboolean activate_mode_internal (GstPad * pad, GstObject * parent,
192     GstPadMode mode, gboolean active);
193
194 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
195
196 static GParamSpec *pspec_caps = NULL;
197
198 /* quarks for probe signals */
199 static GQuark buffer_quark;
200 static GQuark buffer_list_quark;
201 static GQuark event_quark;
202
203 typedef struct
204 {
205   const gint ret;
206   const gchar *name;
207   GQuark quark;
208 } GstFlowQuarks;
209
210 static GstFlowQuarks flow_quarks[] = {
211   {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
212   {GST_FLOW_OK, "ok", 0},
213   {GST_FLOW_NOT_LINKED, "not-linked", 0},
214   {GST_FLOW_FLUSHING, "flushing", 0},
215   {GST_FLOW_EOS, "eos", 0},
216   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
217   {GST_FLOW_ERROR, "error", 0},
218   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
219   {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
220 };
221
222 /**
223  * gst_flow_get_name:
224  * @ret: a #GstFlowReturn to get the name of.
225  *
226  * Gets a string representing the given flow return.
227  *
228  * Returns: a static string with the name of the flow return.
229  */
230 const gchar *
231 gst_flow_get_name (GstFlowReturn ret)
232 {
233   gint i;
234
235   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
236
237   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
238     if (ret == flow_quarks[i].ret)
239       return flow_quarks[i].name;
240   }
241   return "unknown";
242 }
243
244 /**
245  * gst_flow_to_quark:
246  * @ret: a #GstFlowReturn to get the quark of.
247  *
248  * Get the unique quark for the given GstFlowReturn.
249  *
250  * Returns: the quark associated with the flow return or 0 if an
251  * invalid return was specified.
252  */
253 GQuark
254 gst_flow_to_quark (GstFlowReturn ret)
255 {
256   gint i;
257
258   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
259
260   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
261     if (ret == flow_quarks[i].ret)
262       return flow_quarks[i].quark;
263   }
264   return 0;
265 }
266
267 /**
268  * gst_pad_link_get_name:
269  * @ret: a #GstPadLinkReturn to get the name of.
270  *
271  * Gets a string representing the given pad-link return.
272  *
273  * Returns: a static string with the name of the pad-link return.
274  *
275  * Since: 1.4
276  */
277 const gchar *
278 gst_pad_link_get_name (GstPadLinkReturn ret)
279 {
280   switch (ret) {
281     case GST_PAD_LINK_OK:
282       return "ok";
283     case GST_PAD_LINK_WRONG_HIERARCHY:
284       return "wrong hierarchy";
285     case GST_PAD_LINK_WAS_LINKED:
286       return "was linked";
287     case GST_PAD_LINK_WRONG_DIRECTION:
288       return "wrong direction";
289     case GST_PAD_LINK_NOFORMAT:
290       return "no common format";
291     case GST_PAD_LINK_NOSCHED:
292       return "incompatible scheduling";
293     case GST_PAD_LINK_REFUSED:
294       return "refused";
295   }
296   g_return_val_if_reached ("unknown");
297 }
298
299 #define _do_init \
300 { \
301   gint i; \
302   \
303   buffer_quark = g_quark_from_static_string ("buffer"); \
304   buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
305   event_quark = g_quark_from_static_string ("event"); \
306   \
307   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {                    \
308     flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
309   } \
310   \
311   GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
312       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
313 }
314
315 #define gst_pad_parent_class parent_class
316 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
317
318 static void
319 gst_pad_class_init (GstPadClass * klass)
320 {
321   GObjectClass *gobject_class;
322   GstObjectClass *gstobject_class;
323
324   gobject_class = G_OBJECT_CLASS (klass);
325   gstobject_class = GST_OBJECT_CLASS (klass);
326
327   g_type_class_add_private (klass, sizeof (GstPadPrivate));
328
329   gobject_class->dispose = gst_pad_dispose;
330   gobject_class->finalize = gst_pad_finalize;
331   gobject_class->set_property = gst_pad_set_property;
332   gobject_class->get_property = gst_pad_get_property;
333
334   /**
335    * GstPad::linked:
336    * @pad: the pad that emitted the signal
337    * @peer: the peer pad that has been connected
338    *
339    * Signals that a pad has been linked to the peer pad.
340    */
341   gst_pad_signals[PAD_LINKED] =
342       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
343       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
344       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
345   /**
346    * GstPad::unlinked:
347    * @pad: the pad that emitted the signal
348    * @peer: the peer pad that has been disconnected
349    *
350    * Signals that a pad has been unlinked from the peer pad.
351    */
352   gst_pad_signals[PAD_UNLINKED] =
353       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
354       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
355       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
356
357   pspec_caps = g_param_spec_boxed ("caps", "Caps",
358       "The capabilities of the pad", GST_TYPE_CAPS,
359       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
360   g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
361
362   g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
363       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
364           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
365           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
366
367   /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
368   g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
369       g_param_spec_object ("template", "Template",
370           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
371           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372
373   /**
374    * GstPad:offset:
375    *
376    * The offset that will be applied to the running time of the pad.
377    *
378    * Since: 1.6
379    */
380   g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
381       g_param_spec_int64 ("offset", "Offset",
382           "The running time offset of the pad", 0, G_MAXINT64, 0,
383           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384
385   gstobject_class->path_string_separator = ".";
386
387   /* Register common function pointer descriptions */
388   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
389   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
390   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
391   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
392   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
393 }
394
395 static void
396 gst_pad_init (GstPad * pad)
397 {
398   pad->priv = GST_PAD_GET_PRIVATE (pad);
399
400   GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
401
402   GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
403   GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
404   GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
405   GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
406   GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
407
408   GST_PAD_SET_FLUSHING (pad);
409
410   g_rec_mutex_init (&pad->stream_rec_lock);
411
412   g_cond_init (&pad->block_cond);
413
414   g_hook_list_init (&pad->probes, sizeof (GstProbe));
415
416   pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
417   pad->priv->events_cookie = 0;
418   pad->priv->last_cookie = -1;
419   pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
420 }
421
422 /* called when setting the pad inactive. It removes all sticky events from
423  * the pad. must be called with object lock */
424 static void
425 remove_events (GstPad * pad)
426 {
427   guint i, len;
428   GArray *events;
429   gboolean notify = FALSE;
430
431   events = pad->priv->events;
432
433   len = events->len;
434   for (i = 0; i < len; i++) {
435     PadEvent *ev = &g_array_index (events, PadEvent, i);
436     GstEvent *event = ev->event;
437
438     ev->event = NULL;
439
440     if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
441       notify = TRUE;
442
443     gst_event_unref (event);
444   }
445
446   GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
447   g_array_set_size (events, 0);
448   pad->priv->events_cookie++;
449
450   if (notify) {
451     GST_OBJECT_UNLOCK (pad);
452
453     GST_DEBUG_OBJECT (pad, "notify caps");
454     g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
455
456     GST_OBJECT_LOCK (pad);
457   }
458 }
459
460 /* should be called with object lock */
461 static PadEvent *
462 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
463 {
464   guint i, len;
465   GArray *events;
466   PadEvent *ev;
467
468   events = pad->priv->events;
469   len = events->len;
470
471   for (i = 0; i < len; i++) {
472     ev = &g_array_index (events, PadEvent, i);
473     if (ev->event == NULL)
474       continue;
475
476     if (GST_EVENT_TYPE (ev->event) == type) {
477       if (idx == 0)
478         goto found;
479       idx--;
480     } else if (GST_EVENT_TYPE (ev->event) > type) {
481       break;
482     }
483   }
484   ev = NULL;
485 found:
486   return ev;
487 }
488
489 /* should be called with OBJECT lock */
490 static PadEvent *
491 find_event (GstPad * pad, GstEvent * event)
492 {
493   guint i, len;
494   GArray *events;
495   PadEvent *ev;
496
497   events = pad->priv->events;
498   len = events->len;
499
500   for (i = 0; i < len; i++) {
501     ev = &g_array_index (events, PadEvent, i);
502     if (event == ev->event)
503       goto found;
504     else if (GST_EVENT_TYPE (ev->event) > GST_EVENT_TYPE (event))
505       break;
506   }
507   ev = NULL;
508 found:
509   return ev;
510 }
511
512 /* should be called with OBJECT lock */
513 static void
514 remove_event_by_type (GstPad * pad, GstEventType type)
515 {
516   guint i, len;
517   GArray *events;
518   PadEvent *ev;
519
520   events = pad->priv->events;
521   len = events->len;
522
523   i = 0;
524   while (i < len) {
525     ev = &g_array_index (events, PadEvent, i);
526     if (ev->event == NULL)
527       goto next;
528
529     if (GST_EVENT_TYPE (ev->event) > type)
530       break;
531     else if (GST_EVENT_TYPE (ev->event) != type)
532       goto next;
533
534     gst_event_unref (ev->event);
535     g_array_remove_index (events, i);
536     len--;
537     pad->priv->events_cookie++;
538     continue;
539
540   next:
541     i++;
542   }
543 }
544
545 /* check all events on srcpad against those on sinkpad. All events that are not
546  * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
547  * srcpad so that the events will be sent next time */
548 /* should be called with srcpad and sinkpad LOCKS */
549 static void
550 schedule_events (GstPad * srcpad, GstPad * sinkpad)
551 {
552   gint i, len;
553   GArray *events;
554   PadEvent *ev;
555   gboolean pending = FALSE;
556
557   events = srcpad->priv->events;
558   len = events->len;
559
560   for (i = 0; i < len; i++) {
561     ev = &g_array_index (events, PadEvent, i);
562     if (ev->event == NULL)
563       continue;
564
565     if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
566       ev->received = FALSE;
567       pending = TRUE;
568     }
569   }
570   if (pending)
571     GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
572 }
573
574 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
575     gpointer user_data);
576
577 /* should be called with pad LOCK */
578 static void
579 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
580 {
581   guint i, len;
582   GArray *events;
583   gboolean ret;
584   guint cookie;
585
586   events = pad->priv->events;
587
588 restart:
589   cookie = pad->priv->events_cookie;
590   i = 0;
591   len = events->len;
592   while (i < len) {
593     PadEvent *ev, ev_ret;
594
595     ev = &g_array_index (events, PadEvent, i);
596     if (G_UNLIKELY (ev->event == NULL))
597       goto next;
598
599     /* take aditional ref, func might release the lock */
600     ev_ret.event = gst_event_ref (ev->event);
601     ev_ret.received = ev->received;
602
603     ret = func (pad, &ev_ret, user_data);
604
605     /* recheck the cookie, lock might have been released and the list could have
606      * changed */
607     if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
608       if (G_LIKELY (ev_ret.event))
609         gst_event_unref (ev_ret.event);
610       goto restart;
611     }
612
613     /* store the received state */
614     ev->received = ev_ret.received;
615
616     /* if the event changed, we need to do something */
617     if (G_UNLIKELY (ev->event != ev_ret.event)) {
618       if (G_UNLIKELY (ev_ret.event == NULL)) {
619         /* function unreffed and set the event to NULL, remove it */
620         gst_event_unref (ev->event);
621         g_array_remove_index (events, i);
622         len--;
623         cookie = ++pad->priv->events_cookie;
624         continue;
625       } else {
626         /* function gave a new event for us */
627         gst_event_take (&ev->event, ev_ret.event);
628       }
629     } else {
630       /* just unref, nothing changed */
631       gst_event_unref (ev_ret.event);
632     }
633     if (!ret)
634       break;
635   next:
636     i++;
637   }
638 }
639
640 /* should be called with LOCK */
641 static GstEvent *
642 _apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
643 {
644   gint64 offset;
645
646   GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_STIME_FORMAT,
647       GST_STIME_ARGS (pad->offset));
648
649   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
650     GstSegment segment;
651
652     g_assert (!upstream);
653
654     /* copy segment values */
655     gst_event_copy_segment (event, &segment);
656     gst_event_unref (event);
657
658     gst_segment_offset_running_time (&segment, segment.format, pad->offset);
659     event = gst_event_new_segment (&segment);
660   }
661
662   event = gst_event_make_writable (event);
663   offset = gst_event_get_running_time_offset (event);
664   if (upstream)
665     offset -= pad->offset;
666   else
667     offset += pad->offset;
668   gst_event_set_running_time_offset (event, offset);
669
670   return event;
671 }
672
673 static inline GstEvent *
674 apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
675 {
676   if (G_UNLIKELY (pad->offset != 0))
677     return _apply_pad_offset (pad, event, upstream);
678   return event;
679 }
680
681
682 /* should be called with the OBJECT_LOCK */
683 static GstCaps *
684 get_pad_caps (GstPad * pad)
685 {
686   GstCaps *caps = NULL;
687   PadEvent *ev;
688
689   ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
690   if (ev && ev->event)
691     gst_event_parse_caps (ev->event, &caps);
692
693   return caps;
694 }
695
696 static void
697 gst_pad_dispose (GObject * object)
698 {
699   GstPad *pad = GST_PAD_CAST (object);
700   GstPad *peer;
701
702   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "%p dispose", pad);
703
704   /* unlink the peer pad */
705   if ((peer = gst_pad_get_peer (pad))) {
706     /* window for MT unsafeness, someone else could unlink here
707      * and then we call unlink with wrong pads. The unlink
708      * function would catch this and safely return failed. */
709     if (GST_PAD_IS_SRC (pad))
710       gst_pad_unlink (pad, peer);
711     else
712       gst_pad_unlink (peer, pad);
713
714     gst_object_unref (peer);
715   }
716
717   gst_pad_set_pad_template (pad, NULL);
718
719   GST_OBJECT_LOCK (pad);
720   remove_events (pad);
721   GST_OBJECT_UNLOCK (pad);
722
723   g_hook_list_clear (&pad->probes);
724
725   G_OBJECT_CLASS (parent_class)->dispose (object);
726 }
727
728 static void
729 gst_pad_finalize (GObject * object)
730 {
731   GstPad *pad = GST_PAD_CAST (object);
732   GstTask *task;
733
734   /* in case the task is still around, clean it up */
735   if ((task = GST_PAD_TASK (pad))) {
736     gst_task_join (task);
737     GST_PAD_TASK (pad) = NULL;
738     gst_object_unref (task);
739   }
740
741   if (pad->activatenotify)
742     pad->activatenotify (pad->activatedata);
743   if (pad->activatemodenotify)
744     pad->activatemodenotify (pad->activatemodedata);
745   if (pad->linknotify)
746     pad->linknotify (pad->linkdata);
747   if (pad->unlinknotify)
748     pad->unlinknotify (pad->unlinkdata);
749   if (pad->chainnotify)
750     pad->chainnotify (pad->chaindata);
751   if (pad->chainlistnotify)
752     pad->chainlistnotify (pad->chainlistdata);
753   if (pad->getrangenotify)
754     pad->getrangenotify (pad->getrangedata);
755   if (pad->eventnotify)
756     pad->eventnotify (pad->eventdata);
757   if (pad->querynotify)
758     pad->querynotify (pad->querydata);
759   if (pad->iterintlinknotify)
760     pad->iterintlinknotify (pad->iterintlinkdata);
761
762   g_rec_mutex_clear (&pad->stream_rec_lock);
763   g_cond_clear (&pad->block_cond);
764   g_array_free (pad->priv->events, TRUE);
765
766   G_OBJECT_CLASS (parent_class)->finalize (object);
767 }
768
769 static void
770 gst_pad_set_property (GObject * object, guint prop_id,
771     const GValue * value, GParamSpec * pspec)
772 {
773   g_return_if_fail (GST_IS_PAD (object));
774
775   switch (prop_id) {
776     case PAD_PROP_DIRECTION:
777       GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
778       break;
779     case PAD_PROP_TEMPLATE:
780       gst_pad_set_pad_template (GST_PAD_CAST (object),
781           (GstPadTemplate *) g_value_get_object (value));
782       break;
783     case PAD_PROP_OFFSET:
784       gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
785       break;
786     default:
787       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
788       break;
789   }
790 }
791
792 static void
793 gst_pad_get_property (GObject * object, guint prop_id,
794     GValue * value, GParamSpec * pspec)
795 {
796   g_return_if_fail (GST_IS_PAD (object));
797
798   switch (prop_id) {
799     case PAD_PROP_CAPS:
800       GST_OBJECT_LOCK (object);
801       g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
802       GST_OBJECT_UNLOCK (object);
803       break;
804     case PAD_PROP_DIRECTION:
805       g_value_set_enum (value, GST_PAD_DIRECTION (object));
806       break;
807     case PAD_PROP_TEMPLATE:
808       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
809       break;
810     case PAD_PROP_OFFSET:
811       g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
812       break;
813     default:
814       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
815       break;
816   }
817 }
818
819 /**
820  * gst_pad_new:
821  * @name: (allow-none): the name of the new pad.
822  * @direction: the #GstPadDirection of the pad.
823  *
824  * Creates a new pad with the given name in the given direction.
825  * If name is %NULL, a guaranteed unique name (across all pads)
826  * will be assigned.
827  * This function makes a copy of the name so you can safely free the name.
828  *
829  * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
830  * case of an error.
831  *
832  * MT safe.
833  */
834 GstPad *
835 gst_pad_new (const gchar * name, GstPadDirection direction)
836 {
837   return g_object_new (GST_TYPE_PAD,
838       "name", name, "direction", direction, NULL);
839 }
840
841 /**
842  * gst_pad_new_from_template:
843  * @templ: the pad template to use
844  * @name: (allow-none): the name of the pad
845  *
846  * Creates a new pad with the given name from the given template.
847  * If name is %NULL, a guaranteed unique name (across all pads)
848  * will be assigned.
849  * This function makes a copy of the name so you can safely free the name.
850  *
851  * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
852  * case of an error.
853  */
854 GstPad *
855 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
856 {
857   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
858
859   return g_object_new (GST_TYPE_PAD,
860       "name", name, "direction", templ->direction, "template", templ, NULL);
861 }
862
863 /**
864  * gst_pad_new_from_static_template:
865  * @templ: the #GstStaticPadTemplate to use
866  * @name: the name of the pad
867  *
868  * Creates a new pad with the given name from the given static template.
869  * If name is %NULL, a guaranteed unique name (across all pads)
870  * will be assigned.
871  * This function makes a copy of the name so you can safely free the name.
872  *
873  * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
874  * case of an error.
875  */
876 GstPad *
877 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
878     const gchar * name)
879 {
880   GstPad *pad;
881   GstPadTemplate *template;
882
883   template = gst_static_pad_template_get (templ);
884   pad = gst_pad_new_from_template (template, name);
885   gst_object_unref (template);
886   return pad;
887 }
888
889 #define ACQUIRE_PARENT(pad, parent, label)                      \
890   G_STMT_START {                                                \
891     if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad))))          \
892       gst_object_ref (parent);                                  \
893     else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad)))             \
894       goto label;                                               \
895   } G_STMT_END
896
897 #define RELEASE_PARENT(parent)                                  \
898   G_STMT_START {                                                \
899     if (G_LIKELY (parent))                                      \
900       gst_object_unref (parent);                                \
901   } G_STMT_END
902
903 /**
904  * gst_pad_get_direction:
905  * @pad: a #GstPad to get the direction of.
906  *
907  * Gets the direction of the pad. The direction of the pad is
908  * decided at construction time so this function does not take
909  * the LOCK.
910  *
911  * Returns: the #GstPadDirection of the pad.
912  *
913  * MT safe.
914  */
915 GstPadDirection
916 gst_pad_get_direction (GstPad * pad)
917 {
918   GstPadDirection result;
919
920   /* PAD_UNKNOWN is a little silly but we need some sort of
921    * error return value */
922   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
923
924   result = GST_PAD_DIRECTION (pad);
925
926   return result;
927 }
928
929 static gboolean
930 gst_pad_activate_default (GstPad * pad, GstObject * parent)
931 {
932   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
933
934   return activate_mode_internal (pad, parent, GST_PAD_MODE_PUSH, TRUE);
935 }
936
937 /**
938  * gst_pad_mode_get_name:
939  * @mode: the pad mode
940  *
941  * Return the name of a pad mode, for use in debug messages mostly.
942  *
943  * Returns: short mnemonic for pad mode @mode
944  */
945 const gchar *
946 gst_pad_mode_get_name (GstPadMode mode)
947 {
948   switch (mode) {
949     case GST_PAD_MODE_NONE:
950       return "none";
951     case GST_PAD_MODE_PUSH:
952       return "push";
953     case GST_PAD_MODE_PULL:
954       return "pull";
955     default:
956       break;
957   }
958   return "unknown";
959 }
960
961 static void
962 pre_activate (GstPad * pad, GstPadMode new_mode)
963 {
964   switch (new_mode) {
965     case GST_PAD_MODE_NONE:
966       GST_OBJECT_LOCK (pad);
967       GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
968       GST_PAD_SET_FLUSHING (pad);
969       pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
970       GST_PAD_MODE (pad) = new_mode;
971       /* unlock blocked pads so element can resume and stop */
972       GST_PAD_BLOCK_BROADCAST (pad);
973       GST_OBJECT_UNLOCK (pad);
974       break;
975     case GST_PAD_MODE_PUSH:
976     case GST_PAD_MODE_PULL:
977       GST_OBJECT_LOCK (pad);
978       GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
979           gst_pad_mode_get_name (new_mode));
980       GST_PAD_UNSET_FLUSHING (pad);
981       pad->ABI.abi.last_flowret = GST_FLOW_OK;
982       GST_PAD_MODE (pad) = new_mode;
983       if (GST_PAD_IS_SINK (pad)) {
984         GstPad *peer;
985         /* make sure the peer src pad sends us all events */
986         if ((peer = GST_PAD_PEER (pad))) {
987           gst_object_ref (peer);
988           GST_OBJECT_UNLOCK (pad);
989
990           GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
991               GST_DEBUG_PAD_NAME (peer));
992
993           GST_OBJECT_LOCK (peer);
994           schedule_events (peer, NULL);
995           GST_OBJECT_UNLOCK (peer);
996
997           gst_object_unref (peer);
998         } else {
999           GST_OBJECT_UNLOCK (pad);
1000         }
1001       } else {
1002         GST_OBJECT_UNLOCK (pad);
1003       }
1004       break;
1005   }
1006 }
1007
1008 static void
1009 post_activate (GstPad * pad, GstPadMode new_mode)
1010 {
1011   switch (new_mode) {
1012     case GST_PAD_MODE_NONE:
1013       /* ensures that streaming stops */
1014       GST_PAD_STREAM_LOCK (pad);
1015       GST_DEBUG_OBJECT (pad, "stopped streaming");
1016       GST_OBJECT_LOCK (pad);
1017       remove_events (pad);
1018       GST_OBJECT_UNLOCK (pad);
1019       GST_PAD_STREAM_UNLOCK (pad);
1020       break;
1021     case GST_PAD_MODE_PUSH:
1022     case GST_PAD_MODE_PULL:
1023       /* NOP */
1024       break;
1025   }
1026 }
1027
1028 /**
1029  * gst_pad_set_active:
1030  * @pad: the #GstPad to activate or deactivate.
1031  * @active: whether or not the pad should be active.
1032  *
1033  * Activates or deactivates the given pad.
1034  * Normally called from within core state change functions.
1035  *
1036  * If @active, makes sure the pad is active. If it is already active, either in
1037  * push or pull mode, just return. Otherwise dispatches to the pad's activate
1038  * function to perform the actual activation.
1039  *
1040  * If not @active, calls gst_pad_activate_mode() with the pad's current mode
1041  * and a %FALSE argument.
1042  *
1043  * Returns: %TRUE if the operation was successful.
1044  *
1045  * MT safe.
1046  */
1047 gboolean
1048 gst_pad_set_active (GstPad * pad, gboolean active)
1049 {
1050   GstObject *parent;
1051   GstPadMode old;
1052   gboolean ret = FALSE;
1053
1054   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1055
1056   GST_OBJECT_LOCK (pad);
1057   old = GST_PAD_MODE (pad);
1058   ACQUIRE_PARENT (pad, parent, no_parent);
1059   GST_OBJECT_UNLOCK (pad);
1060
1061   if (active) {
1062     if (old == GST_PAD_MODE_NONE) {
1063       GST_DEBUG_OBJECT (pad, "activating pad from none");
1064       ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
1065       if (ret)
1066         pad->ABI.abi.last_flowret = GST_FLOW_OK;
1067     } else {
1068       GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
1069           gst_pad_mode_get_name (old));
1070       ret = TRUE;
1071     }
1072   } else {
1073     if (old == GST_PAD_MODE_NONE) {
1074       GST_DEBUG_OBJECT (pad, "pad was inactive");
1075       ret = TRUE;
1076     } else {
1077       GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1078           gst_pad_mode_get_name (old));
1079       ret = activate_mode_internal (pad, parent, old, FALSE);
1080       if (ret)
1081         pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
1082     }
1083   }
1084
1085   RELEASE_PARENT (parent);
1086
1087   if (G_UNLIKELY (!ret))
1088     goto failed;
1089
1090   return ret;
1091
1092   /* ERRORS */
1093 no_parent:
1094   {
1095     GST_DEBUG_OBJECT (pad, "no parent");
1096     GST_OBJECT_UNLOCK (pad);
1097     return FALSE;
1098   }
1099 failed:
1100   {
1101     GST_OBJECT_LOCK (pad);
1102     if (!active) {
1103       g_critical ("Failed to deactivate pad %s:%s, very bad",
1104           GST_DEBUG_PAD_NAME (pad));
1105     } else {
1106       GST_WARNING_OBJECT (pad, "Failed to activate pad");
1107     }
1108     GST_OBJECT_UNLOCK (pad);
1109     return FALSE;
1110   }
1111 }
1112
1113 static gboolean
1114 activate_mode_internal (GstPad * pad, GstObject * parent, GstPadMode mode,
1115     gboolean active)
1116 {
1117   gboolean res = FALSE;
1118   GstPadMode old, new;
1119   GstPadDirection dir;
1120   GstPad *peer;
1121
1122   GST_OBJECT_LOCK (pad);
1123   old = GST_PAD_MODE (pad);
1124   dir = GST_PAD_DIRECTION (pad);
1125   GST_OBJECT_UNLOCK (pad);
1126
1127   new = active ? mode : GST_PAD_MODE_NONE;
1128
1129   if (old == new)
1130     goto was_ok;
1131
1132   if (active && old != mode && old != GST_PAD_MODE_NONE) {
1133     /* pad was activate in the wrong direction, deactivate it
1134      * and reactivate it in the requested mode */
1135     GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1136         gst_pad_mode_get_name (old));
1137
1138     if (G_UNLIKELY (!activate_mode_internal (pad, parent, old, FALSE)))
1139       goto deactivate_failed;
1140     old = GST_PAD_MODE_NONE;
1141   }
1142
1143   switch (mode) {
1144     case GST_PAD_MODE_PULL:
1145     {
1146       if (dir == GST_PAD_SINK) {
1147         if ((peer = gst_pad_get_peer (pad))) {
1148           GST_DEBUG_OBJECT (pad, "calling peer");
1149           if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1150             goto peer_failed;
1151           gst_object_unref (peer);
1152         } else {
1153           /* there is no peer, this is only fatal when we activate. When we
1154            * deactivate, we must assume the application has unlinked the peer and
1155            * will deactivate it eventually. */
1156           if (active)
1157             goto not_linked;
1158           else
1159             GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1160         }
1161       } else {
1162         if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1163           goto failure;         /* Can't activate pull on a src without a
1164                                    getrange function */
1165       }
1166       break;
1167     }
1168     default:
1169       break;
1170   }
1171
1172   /* Mark pad as needing reconfiguration */
1173   if (active)
1174     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1175   pre_activate (pad, new);
1176
1177   if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1178     if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1179                 active)))
1180       goto failure;
1181   } else {
1182     /* can happen for sinks of passthrough elements */
1183   }
1184
1185   post_activate (pad, new);
1186
1187   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1188       active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1189
1190 exit_success:
1191   res = TRUE;
1192
1193   /* Clear sticky flags on deactivation */
1194   if (!active) {
1195     GST_OBJECT_LOCK (pad);
1196     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1197     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1198     GST_OBJECT_UNLOCK (pad);
1199   }
1200
1201 exit:
1202   return res;
1203
1204 was_ok:
1205   {
1206     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1207         active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1208     goto exit_success;
1209   }
1210 deactivate_failed:
1211   {
1212     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1213         "failed to %s in switch to %s mode from %s mode",
1214         (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1215         gst_pad_mode_get_name (old));
1216     goto exit;
1217   }
1218 peer_failed:
1219   {
1220     GST_OBJECT_LOCK (peer);
1221     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1222         "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1223     GST_OBJECT_UNLOCK (peer);
1224     gst_object_unref (peer);
1225     goto exit;
1226   }
1227 not_linked:
1228   {
1229     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1230         "pad in pull mode");
1231     goto exit;
1232   }
1233 failure:
1234   {
1235     GST_OBJECT_LOCK (pad);
1236     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1237         active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1238     GST_PAD_SET_FLUSHING (pad);
1239     GST_PAD_MODE (pad) = old;
1240     GST_OBJECT_UNLOCK (pad);
1241     goto exit;
1242   }
1243 }
1244
1245 /**
1246  * gst_pad_activate_mode:
1247  * @pad: the #GstPad to activate or deactivate.
1248  * @mode: the requested activation mode
1249  * @active: whether or not the pad should be active.
1250  *
1251  * Activates or deactivates the given pad in @mode via dispatching to the
1252  * pad's activatemodefunc. For use from within pad activation functions only.
1253  *
1254  * If you don't know what this is, you probably don't want to call it.
1255  *
1256  * Returns: %TRUE if the operation was successful.
1257  *
1258  * MT safe.
1259  */
1260 gboolean
1261 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1262 {
1263   GstObject *parent;
1264   gboolean res;
1265
1266   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1267
1268   GST_OBJECT_LOCK (pad);
1269   ACQUIRE_PARENT (pad, parent, no_parent);
1270   GST_OBJECT_UNLOCK (pad);
1271
1272   res = activate_mode_internal (pad, parent, mode, active);
1273
1274   RELEASE_PARENT (parent);
1275
1276   return res;
1277
1278 no_parent:
1279   {
1280     GST_WARNING_OBJECT (pad, "no parent");
1281     GST_OBJECT_UNLOCK (pad);
1282     return FALSE;
1283   }
1284 }
1285
1286 /**
1287  * gst_pad_is_active:
1288  * @pad: the #GstPad to query
1289  *
1290  * Query if a pad is active
1291  *
1292  * Returns: %TRUE if the pad is active.
1293  *
1294  * MT safe.
1295  */
1296 gboolean
1297 gst_pad_is_active (GstPad * pad)
1298 {
1299   gboolean result = FALSE;
1300
1301   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1302
1303   GST_OBJECT_LOCK (pad);
1304   result = GST_PAD_IS_ACTIVE (pad);
1305   GST_OBJECT_UNLOCK (pad);
1306
1307   return result;
1308 }
1309
1310 static void
1311 cleanup_hook (GstPad * pad, GHook * hook)
1312 {
1313   GstPadProbeType type;
1314
1315   if (!G_HOOK_IS_VALID (hook))
1316     return;
1317
1318   type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1319
1320   if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1321     /* unblock when we remove the last blocking probe */
1322     pad->num_blocked--;
1323     GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1324         pad->num_blocked);
1325
1326     /* Might have new probes now that want to be called */
1327     GST_PAD_BLOCK_BROADCAST (pad);
1328
1329     if (pad->num_blocked == 0) {
1330       GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1331       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1332     }
1333   }
1334   g_hook_destroy_link (&pad->probes, hook);
1335   pad->num_probes--;
1336 }
1337
1338 /**
1339  * gst_pad_add_probe:
1340  * @pad: the #GstPad to add the probe to
1341  * @mask: the probe mask
1342  * @callback: #GstPadProbeCallback that will be called with notifications of
1343  *           the pad state
1344  * @user_data: (closure): user data passed to the callback
1345  * @destroy_data: #GDestroyNotify for user_data
1346  *
1347  * Be notified of different states of pads. The provided callback is called for
1348  * every state that matches @mask.
1349  *
1350  * Probes are called in groups: First GST_PAD_PROBE_TYPE_BLOCK probes are
1351  * called, then others, then finally GST_PAD_PROBE_TYPE_IDLE. The only
1352  * exception here are GST_PAD_PROBE_TYPE_IDLE probes that are called
1353  * immediately if the pad is already idle while calling gst_pad_add_probe().
1354  * In each of the groups, probes are called in the order in which they were
1355  * added.
1356  *
1357  * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1358  * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1359  * happen that the probe can be run immediately and if the probe returns
1360  * GST_PAD_PROBE_REMOVE this functions returns 0.
1361  *
1362  * MT safe.
1363  */
1364 gulong
1365 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1366     GstPadProbeCallback callback, gpointer user_data,
1367     GDestroyNotify destroy_data)
1368 {
1369   GHook *hook;
1370   gulong res;
1371
1372   g_return_val_if_fail (GST_IS_PAD (pad), 0);
1373   g_return_val_if_fail (mask != 0, 0);
1374
1375   GST_OBJECT_LOCK (pad);
1376
1377   /* make a new probe */
1378   hook = g_hook_alloc (&pad->probes);
1379
1380   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1381       mask);
1382
1383   /* when no contraints are given for the types, assume all types are
1384    * acceptable */
1385   if ((mask & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH) == 0)
1386     mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1387   if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1388     mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1389
1390   /* store our flags and other fields */
1391   hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1392   hook->func = callback;
1393   hook->data = user_data;
1394   hook->destroy = destroy_data;
1395   PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1396
1397   /* add the probe */
1398   g_hook_append (&pad->probes, hook);
1399   pad->num_probes++;
1400   /* incremenent cookie so that the new hook get's called */
1401   pad->priv->probe_list_cookie++;
1402
1403   /* get the id of the hook, we return this and it can be used to remove the
1404    * probe later */
1405   res = hook->hook_id;
1406
1407   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1408
1409   if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1410     /* we have a block probe */
1411     pad->num_blocked++;
1412     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1413     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1414         "now %d blocking probes", pad->num_blocked);
1415
1416     /* Might have new probes now that want to be called */
1417     GST_PAD_BLOCK_BROADCAST (pad);
1418   }
1419
1420   /* call the callback if we need to be called for idle callbacks */
1421   if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1422     if (pad->priv->using > 0) {
1423       /* the pad is in use, we can't signal the idle callback yet. Since we set the
1424        * flag above, the last thread to leave the push will do the callback. New
1425        * threads going into the push will block. */
1426       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1427           "pad is in use, delay idle callback");
1428       GST_OBJECT_UNLOCK (pad);
1429     } else {
1430       GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1431       GstPadProbeReturn ret;
1432
1433       /* Keep another ref, the callback could destroy the pad */
1434       gst_object_ref (pad);
1435       pad->priv->idle_running++;
1436
1437       /* the pad is idle now, we can signal the idle callback now */
1438       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1439           "pad is idle, trigger idle callback");
1440       GST_OBJECT_UNLOCK (pad);
1441
1442       ret = callback (pad, &info, user_data);
1443
1444       GST_OBJECT_LOCK (pad);
1445       switch (ret) {
1446         case GST_PAD_PROBE_REMOVE:
1447           /* remove the probe */
1448           GST_DEBUG_OBJECT (pad, "asked to remove hook");
1449           cleanup_hook (pad, hook);
1450           res = 0;
1451           break;
1452         case GST_PAD_PROBE_DROP:
1453           GST_DEBUG_OBJECT (pad, "asked to drop item");
1454           break;
1455         case GST_PAD_PROBE_PASS:
1456           GST_DEBUG_OBJECT (pad, "asked to pass item");
1457           break;
1458         case GST_PAD_PROBE_OK:
1459           GST_DEBUG_OBJECT (pad, "probe returned OK");
1460           break;
1461         case GST_PAD_PROBE_HANDLED:
1462           GST_DEBUG_OBJECT (pad, "probe handled the data");
1463           break;
1464         default:
1465           GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1466           break;
1467       }
1468       pad->priv->idle_running--;
1469       if (pad->priv->idle_running == 0) {
1470         GST_PAD_BLOCK_BROADCAST (pad);
1471       }
1472       GST_OBJECT_UNLOCK (pad);
1473
1474       gst_object_unref (pad);
1475     }
1476   } else {
1477     GST_OBJECT_UNLOCK (pad);
1478   }
1479   return res;
1480 }
1481
1482 /**
1483  * gst_pad_remove_probe:
1484  * @pad: the #GstPad with the probe
1485  * @id: the probe id to remove
1486  *
1487  * Remove the probe with @id from @pad.
1488  *
1489  * MT safe.
1490  */
1491 void
1492 gst_pad_remove_probe (GstPad * pad, gulong id)
1493 {
1494   GHook *hook;
1495
1496   g_return_if_fail (GST_IS_PAD (pad));
1497
1498   GST_OBJECT_LOCK (pad);
1499
1500   hook = g_hook_get (&pad->probes, id);
1501   if (hook == NULL)
1502     goto not_found;
1503
1504   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1505       hook->hook_id);
1506   cleanup_hook (pad, hook);
1507   GST_OBJECT_UNLOCK (pad);
1508
1509   return;
1510
1511 not_found:
1512   {
1513     GST_OBJECT_UNLOCK (pad);
1514     g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1515     return;
1516   }
1517 }
1518
1519 /**
1520  * gst_pad_is_blocked:
1521  * @pad: the #GstPad to query
1522  *
1523  * Checks if the pad is blocked or not. This function returns the
1524  * last requested state of the pad. It is not certain that the pad
1525  * is actually blocking at this point (see gst_pad_is_blocking()).
1526  *
1527  * Returns: %TRUE if the pad is blocked.
1528  *
1529  * MT safe.
1530  */
1531 gboolean
1532 gst_pad_is_blocked (GstPad * pad)
1533 {
1534   gboolean result = FALSE;
1535
1536   g_return_val_if_fail (GST_IS_PAD (pad), result);
1537
1538   GST_OBJECT_LOCK (pad);
1539   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1540   GST_OBJECT_UNLOCK (pad);
1541
1542   return result;
1543 }
1544
1545 /**
1546  * gst_pad_is_blocking:
1547  * @pad: the #GstPad to query
1548  *
1549  * Checks if the pad is blocking or not. This is a guaranteed state
1550  * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1551  *
1552  * Returns: %TRUE if the pad is blocking.
1553  *
1554  * MT safe.
1555  */
1556 gboolean
1557 gst_pad_is_blocking (GstPad * pad)
1558 {
1559   gboolean result = FALSE;
1560
1561   g_return_val_if_fail (GST_IS_PAD (pad), result);
1562
1563   GST_OBJECT_LOCK (pad);
1564   /* the blocking flag is only valid if the pad is not flushing */
1565   result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1566   GST_OBJECT_UNLOCK (pad);
1567
1568   return result;
1569 }
1570
1571 /**
1572  * gst_pad_needs_reconfigure:
1573  * @pad: the #GstPad to check
1574  *
1575  * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1576  * if the flag was set.
1577  *
1578  * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1579  */
1580 gboolean
1581 gst_pad_needs_reconfigure (GstPad * pad)
1582 {
1583   gboolean reconfigure;
1584
1585   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1586
1587   GST_OBJECT_LOCK (pad);
1588   reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1589   GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1590   GST_OBJECT_UNLOCK (pad);
1591
1592   return reconfigure;
1593 }
1594
1595 /**
1596  * gst_pad_check_reconfigure:
1597  * @pad: the #GstPad to check
1598  *
1599  * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1600  * if the flag was set.
1601  *
1602  * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1603  */
1604 gboolean
1605 gst_pad_check_reconfigure (GstPad * pad)
1606 {
1607   gboolean reconfigure;
1608
1609   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1610
1611   GST_OBJECT_LOCK (pad);
1612   reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1613   if (reconfigure) {
1614     GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1615     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1616   }
1617   GST_OBJECT_UNLOCK (pad);
1618
1619   return reconfigure;
1620 }
1621
1622 /**
1623  * gst_pad_mark_reconfigure:
1624  * @pad: the #GstPad to mark
1625  *
1626  * Mark a pad for needing reconfiguration. The next call to
1627  * gst_pad_check_reconfigure() will return %TRUE after this call.
1628  */
1629 void
1630 gst_pad_mark_reconfigure (GstPad * pad)
1631 {
1632   g_return_if_fail (GST_IS_PAD (pad));
1633
1634   GST_OBJECT_LOCK (pad);
1635   GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1636   GST_OBJECT_UNLOCK (pad);
1637 }
1638
1639 /**
1640  * gst_pad_set_activate_function:
1641  * @p: a #GstPad.
1642  * @f: the #GstPadActivateFunction to set.
1643  *
1644  * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1645  * notify.
1646  */
1647 /**
1648  * gst_pad_set_activate_function_full:
1649  * @pad: a #GstPad.
1650  * @activate: the #GstPadActivateFunction to set.
1651  * @user_data: user_data passed to @notify
1652  * @notify: notify called when @activate will not be used anymore.
1653  *
1654  * Sets the given activate function for @pad. The activate function will
1655  * dispatch to gst_pad_activate_mode() to perform the actual activation.
1656  * Only makes sense to set on sink pads.
1657  *
1658  * Call this function if your sink pad can start a pull-based task.
1659  */
1660 void
1661 gst_pad_set_activate_function_full (GstPad * pad,
1662     GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1663 {
1664   g_return_if_fail (GST_IS_PAD (pad));
1665
1666   if (pad->activatenotify)
1667     pad->activatenotify (pad->activatedata);
1668   GST_PAD_ACTIVATEFUNC (pad) = activate;
1669   pad->activatedata = user_data;
1670   pad->activatenotify = notify;
1671
1672   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1673       GST_DEBUG_FUNCPTR_NAME (activate));
1674 }
1675
1676 /**
1677  * gst_pad_set_activatemode_function:
1678  * @p: a #GstPad.
1679  * @f: the #GstPadActivateModeFunction to set.
1680  *
1681  * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1682  * notify.
1683  */
1684 /**
1685  * gst_pad_set_activatemode_function_full:
1686  * @pad: a #GstPad.
1687  * @activatemode: the #GstPadActivateModeFunction to set.
1688  * @user_data: user_data passed to @notify
1689  * @notify: notify called when @activatemode will not be used anymore.
1690  *
1691  * Sets the given activate_mode function for the pad. An activate_mode function
1692  * prepares the element for data passing.
1693  */
1694 void
1695 gst_pad_set_activatemode_function_full (GstPad * pad,
1696     GstPadActivateModeFunction activatemode, gpointer user_data,
1697     GDestroyNotify notify)
1698 {
1699   g_return_if_fail (GST_IS_PAD (pad));
1700
1701   if (pad->activatemodenotify)
1702     pad->activatemodenotify (pad->activatemodedata);
1703   GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1704   pad->activatemodedata = user_data;
1705   pad->activatemodenotify = notify;
1706
1707   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1708       GST_DEBUG_FUNCPTR_NAME (activatemode));
1709 }
1710
1711 /**
1712  * gst_pad_set_chain_function:
1713  * @p: a sink #GstPad.
1714  * @f: the #GstPadChainFunction to set.
1715  *
1716  * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1717  * notify.
1718  */
1719 /**
1720  * gst_pad_set_chain_function_full:
1721  * @pad: a sink #GstPad.
1722  * @chain: the #GstPadChainFunction to set.
1723  * @user_data: user_data passed to @notify
1724  * @notify: notify called when @chain will not be used anymore.
1725  *
1726  * Sets the given chain function for the pad. The chain function is called to
1727  * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1728  */
1729 void
1730 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1731     gpointer user_data, GDestroyNotify notify)
1732 {
1733   g_return_if_fail (GST_IS_PAD (pad));
1734   g_return_if_fail (GST_PAD_IS_SINK (pad));
1735
1736   if (pad->chainnotify)
1737     pad->chainnotify (pad->chaindata);
1738   GST_PAD_CHAINFUNC (pad) = chain;
1739   pad->chaindata = user_data;
1740   pad->chainnotify = notify;
1741
1742   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1743       GST_DEBUG_FUNCPTR_NAME (chain));
1744 }
1745
1746 /**
1747  * gst_pad_set_chain_list_function:
1748  * @p: a sink #GstPad.
1749  * @f: the #GstPadChainListFunction to set.
1750  *
1751  * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1752  * notify.
1753  */
1754 /**
1755  * gst_pad_set_chain_list_function_full:
1756  * @pad: a sink #GstPad.
1757  * @chainlist: the #GstPadChainListFunction to set.
1758  * @user_data: user_data passed to @notify
1759  * @notify: notify called when @chainlist will not be used anymore.
1760  *
1761  * Sets the given chain list function for the pad. The chainlist function is
1762  * called to process a #GstBufferList input buffer list. See
1763  * #GstPadChainListFunction for more details.
1764  */
1765 void
1766 gst_pad_set_chain_list_function_full (GstPad * pad,
1767     GstPadChainListFunction chainlist, gpointer user_data,
1768     GDestroyNotify notify)
1769 {
1770   g_return_if_fail (GST_IS_PAD (pad));
1771   g_return_if_fail (GST_PAD_IS_SINK (pad));
1772
1773   if (pad->chainlistnotify)
1774     pad->chainlistnotify (pad->chainlistdata);
1775   GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1776   pad->chainlistdata = user_data;
1777   pad->chainlistnotify = notify;
1778
1779   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1780       GST_DEBUG_FUNCPTR_NAME (chainlist));
1781 }
1782
1783 /**
1784  * gst_pad_set_getrange_function:
1785  * @p: a source #GstPad.
1786  * @f: the #GstPadGetRangeFunction to set.
1787  *
1788  * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1789  * notify.
1790  */
1791 /**
1792  * gst_pad_set_getrange_function_full:
1793  * @pad: a source #GstPad.
1794  * @get: the #GstPadGetRangeFunction to set.
1795  * @user_data: user_data passed to @notify
1796  * @notify: notify called when @get will not be used anymore.
1797  *
1798  * Sets the given getrange function for the pad. The getrange function is
1799  * called to produce a new #GstBuffer to start the processing pipeline. see
1800  * #GstPadGetRangeFunction for a description of the getrange function.
1801  */
1802 void
1803 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1804     gpointer user_data, GDestroyNotify notify)
1805 {
1806   g_return_if_fail (GST_IS_PAD (pad));
1807   g_return_if_fail (GST_PAD_IS_SRC (pad));
1808
1809   if (pad->getrangenotify)
1810     pad->getrangenotify (pad->getrangedata);
1811   GST_PAD_GETRANGEFUNC (pad) = get;
1812   pad->getrangedata = user_data;
1813   pad->getrangenotify = notify;
1814
1815   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1816       GST_DEBUG_FUNCPTR_NAME (get));
1817 }
1818
1819 /**
1820  * gst_pad_set_event_function:
1821  * @p: a #GstPad of either direction.
1822  * @f: the #GstPadEventFunction to set.
1823  *
1824  * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1825  * notify.
1826  */
1827 /**
1828  * gst_pad_set_event_function_full:
1829  * @pad: a #GstPad of either direction.
1830  * @event: the #GstPadEventFunction to set.
1831  * @user_data: user_data passed to @notify
1832  * @notify: notify called when @event will not be used anymore.
1833  *
1834  * Sets the given event handler for the pad.
1835  */
1836 void
1837 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1838     gpointer user_data, GDestroyNotify notify)
1839 {
1840   g_return_if_fail (GST_IS_PAD (pad));
1841
1842   if (pad->eventnotify)
1843     pad->eventnotify (pad->eventdata);
1844   GST_PAD_EVENTFUNC (pad) = event;
1845   pad->eventdata = user_data;
1846   pad->eventnotify = notify;
1847
1848   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1849       GST_DEBUG_FUNCPTR_NAME (event));
1850 }
1851
1852 static gboolean
1853 event_wrap (GstPad * pad, GstObject * object, GstEvent * event)
1854 {
1855   GstFlowReturn ret;
1856
1857   ret = GST_PAD_EVENTFULLFUNC (pad) (pad, object, event);
1858   if (ret == GST_FLOW_OK)
1859     return TRUE;
1860   return FALSE;
1861 }
1862
1863 /**
1864  * gst_pad_set_event_full_function:
1865  * @p: a #GstPad of either direction.
1866  * @f: the #GstPadEventFullFunction to set.
1867  *
1868  * Calls gst_pad_set_event_full_function_full() with %NULL for the user_data and
1869  * notify.
1870  */
1871 /**
1872  * gst_pad_set_event_full_function_full:
1873  * @pad: a #GstPad of either direction.
1874  * @event: the #GstPadEventFullFunction to set.
1875  * @user_data: user_data passed to @notify
1876  * @notify: notify called when @event will not be used anymore.
1877  *
1878  * Sets the given event handler for the pad.
1879  *
1880  * Since: 1.8
1881  */
1882 void
1883 gst_pad_set_event_full_function_full (GstPad * pad,
1884     GstPadEventFullFunction event, gpointer user_data, GDestroyNotify notify)
1885 {
1886   g_return_if_fail (GST_IS_PAD (pad));
1887
1888   if (pad->eventnotify)
1889     pad->eventnotify (pad->eventdata);
1890   GST_PAD_EVENTFULLFUNC (pad) = event;
1891   GST_PAD_EVENTFUNC (pad) = event_wrap;
1892   pad->eventdata = user_data;
1893   pad->eventnotify = notify;
1894
1895   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfullfunc for set to %s",
1896       GST_DEBUG_FUNCPTR_NAME (event));
1897 }
1898
1899 /**
1900  * gst_pad_set_query_function:
1901  * @p: a #GstPad of either direction.
1902  * @f: the #GstPadQueryFunction to set.
1903  *
1904  * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1905  * notify.
1906  */
1907 /**
1908  * gst_pad_set_query_function_full:
1909  * @pad: a #GstPad of either direction.
1910  * @query: the #GstPadQueryFunction to set.
1911  * @user_data: user_data passed to @notify
1912  * @notify: notify called when @query will not be used anymore.
1913  *
1914  * Set the given query function for the pad.
1915  */
1916 void
1917 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1918     gpointer user_data, GDestroyNotify notify)
1919 {
1920   g_return_if_fail (GST_IS_PAD (pad));
1921
1922   if (pad->querynotify)
1923     pad->querynotify (pad->querydata);
1924   GST_PAD_QUERYFUNC (pad) = query;
1925   pad->querydata = user_data;
1926   pad->querynotify = notify;
1927
1928   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1929       GST_DEBUG_FUNCPTR_NAME (query));
1930 }
1931
1932 /**
1933  * gst_pad_set_iterate_internal_links_function:
1934  * @p: a #GstPad of either direction.
1935  * @f: the #GstPadIterIntLinkFunction to set.
1936  *
1937  * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1938  * for the user_data and notify.
1939  */
1940 /**
1941  * gst_pad_set_iterate_internal_links_function_full:
1942  * @pad: a #GstPad of either direction.
1943  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1944  * @user_data: user_data passed to @notify
1945  * @notify: notify called when @iterintlink will not be used anymore.
1946  *
1947  * Sets the given internal link iterator function for the pad.
1948  */
1949 void
1950 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1951     GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1952     GDestroyNotify notify)
1953 {
1954   g_return_if_fail (GST_IS_PAD (pad));
1955
1956   if (pad->iterintlinknotify)
1957     pad->iterintlinknotify (pad->iterintlinkdata);
1958   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1959   pad->iterintlinkdata = user_data;
1960   pad->iterintlinknotify = notify;
1961
1962   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1963       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1964 }
1965
1966 /**
1967  * gst_pad_set_link_function:
1968  * @p: a #GstPad.
1969  * @f: the #GstPadLinkFunction to set.
1970  *
1971  * Calls gst_pad_set_link_function_full() with %NULL
1972  * for the user_data and notify.
1973  */
1974 /**
1975  * gst_pad_set_link_function_full:
1976  * @pad: a #GstPad.
1977  * @link: the #GstPadLinkFunction to set.
1978  * @user_data: user_data passed to @notify
1979  * @notify: notify called when @link will not be used anymore.
1980  *
1981  * Sets the given link function for the pad. It will be called when
1982  * the pad is linked with another pad.
1983  *
1984  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1985  * made.
1986  *
1987  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1988  * cannot be made for some reason.
1989  *
1990  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1991  * of the peer sink pad, if present.
1992  */
1993 void
1994 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1995     gpointer user_data, GDestroyNotify notify)
1996 {
1997   g_return_if_fail (GST_IS_PAD (pad));
1998
1999   if (pad->linknotify)
2000     pad->linknotify (pad->linkdata);
2001   GST_PAD_LINKFUNC (pad) = link;
2002   pad->linkdata = user_data;
2003   pad->linknotify = notify;
2004
2005   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
2006       GST_DEBUG_FUNCPTR_NAME (link));
2007 }
2008
2009 /**
2010  * gst_pad_set_unlink_function:
2011  * @p: a #GstPad.
2012  * @f: the #GstPadUnlinkFunction to set.
2013  *
2014  * Calls gst_pad_set_unlink_function_full() with %NULL
2015  * for the user_data and notify.
2016  */
2017 /**
2018  * gst_pad_set_unlink_function_full:
2019  * @pad: a #GstPad.
2020  * @unlink: the #GstPadUnlinkFunction to set.
2021  * @user_data: user_data passed to @notify
2022  * @notify: notify called when @unlink will not be used anymore.
2023  *
2024  * Sets the given unlink function for the pad. It will be called
2025  * when the pad is unlinked.
2026  */
2027 void
2028 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
2029     gpointer user_data, GDestroyNotify notify)
2030 {
2031   g_return_if_fail (GST_IS_PAD (pad));
2032
2033   if (pad->unlinknotify)
2034     pad->unlinknotify (pad->unlinkdata);
2035   GST_PAD_UNLINKFUNC (pad) = unlink;
2036   pad->unlinkdata = user_data;
2037   pad->unlinknotify = notify;
2038
2039   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
2040       GST_DEBUG_FUNCPTR_NAME (unlink));
2041 }
2042
2043 /**
2044  * gst_pad_unlink:
2045  * @srcpad: the source #GstPad to unlink.
2046  * @sinkpad: the sink #GstPad to unlink.
2047  *
2048  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
2049  * signal on both pads.
2050  *
2051  * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
2052  * the pads were not linked together.
2053  *
2054  * MT safe.
2055  */
2056 gboolean
2057 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
2058 {
2059   gboolean result = FALSE;
2060   GstElement *parent = NULL;
2061
2062   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2063   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
2064   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2065   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
2066
2067   GST_TRACER_PAD_UNLINK_PRE (srcpad, sinkpad);
2068
2069   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
2070       GST_DEBUG_PAD_NAME (srcpad), srcpad,
2071       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
2072
2073   /* We need to notify the parent before taking any pad locks as the bin in
2074    * question might be waiting for a lock on the pad while holding its lock
2075    * that our message will try to take. */
2076   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2077     if (GST_IS_ELEMENT (parent)) {
2078       gst_element_post_message (parent,
2079           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2080               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
2081     } else {
2082       gst_object_unref (parent);
2083       parent = NULL;
2084     }
2085   }
2086
2087   GST_OBJECT_LOCK (srcpad);
2088   GST_OBJECT_LOCK (sinkpad);
2089
2090   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2091     goto not_linked_together;
2092
2093   if (GST_PAD_UNLINKFUNC (srcpad)) {
2094     GstObject *tmpparent;
2095
2096     ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2097
2098     GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2099     RELEASE_PARENT (tmpparent);
2100   }
2101 no_src_parent:
2102   if (GST_PAD_UNLINKFUNC (sinkpad)) {
2103     GstObject *tmpparent;
2104
2105     ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2106
2107     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2108     RELEASE_PARENT (tmpparent);
2109   }
2110 no_sink_parent:
2111
2112   /* first clear peers */
2113   GST_PAD_PEER (srcpad) = NULL;
2114   GST_PAD_PEER (sinkpad) = NULL;
2115
2116   GST_OBJECT_UNLOCK (sinkpad);
2117   GST_OBJECT_UNLOCK (srcpad);
2118
2119   /* fire off a signal to each of the pads telling them
2120    * that they've been unlinked */
2121   g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2122   g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2123
2124   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2125       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2126
2127   result = TRUE;
2128
2129 done:
2130   if (parent != NULL) {
2131     gst_element_post_message (parent,
2132         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2133             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2134     gst_object_unref (parent);
2135   }
2136   GST_TRACER_PAD_UNLINK_POST (srcpad, sinkpad, result);
2137   return result;
2138
2139   /* ERRORS */
2140 not_linked_together:
2141   {
2142     /* we do not emit a warning in this case because unlinking cannot
2143      * be made MT safe.*/
2144     GST_OBJECT_UNLOCK (sinkpad);
2145     GST_OBJECT_UNLOCK (srcpad);
2146     goto done;
2147   }
2148 }
2149
2150 /**
2151  * gst_pad_is_linked:
2152  * @pad: pad to check
2153  *
2154  * Checks if a @pad is linked to another pad or not.
2155  *
2156  * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2157  *
2158  * MT safe.
2159  */
2160 gboolean
2161 gst_pad_is_linked (GstPad * pad)
2162 {
2163   gboolean result;
2164
2165   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2166
2167   GST_OBJECT_LOCK (pad);
2168   result = (GST_PAD_PEER (pad) != NULL);
2169   GST_OBJECT_UNLOCK (pad);
2170
2171   return result;
2172 }
2173
2174 /* get the caps from both pads and see if the intersection
2175  * is not empty.
2176  *
2177  * This function should be called with the pad LOCK on both
2178  * pads
2179  */
2180 static gboolean
2181 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2182     GstPadLinkCheck flags)
2183 {
2184   GstCaps *srccaps = NULL;
2185   GstCaps *sinkcaps = NULL;
2186   gboolean compatible = FALSE;
2187
2188   if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2189     return TRUE;
2190
2191   /* Doing the expensive caps checking takes priority over only checking the template caps */
2192   if (flags & GST_PAD_LINK_CHECK_CAPS) {
2193     GST_OBJECT_UNLOCK (sink);
2194     GST_OBJECT_UNLOCK (src);
2195
2196     srccaps = gst_pad_query_caps (src, NULL);
2197     sinkcaps = gst_pad_query_caps (sink, NULL);
2198
2199     GST_OBJECT_LOCK (src);
2200     GST_OBJECT_LOCK (sink);
2201   } else {
2202     /* If one of the two pads doesn't have a template, consider the intersection
2203      * as valid.*/
2204     if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2205             || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2206       compatible = TRUE;
2207       goto done;
2208     }
2209     srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2210     sinkcaps =
2211         gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2212   }
2213
2214   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2215       srccaps);
2216   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2217       sinkcaps);
2218
2219   /* if we have caps on both pads we can check the intersection. If one
2220    * of the caps is %NULL, we return %TRUE. */
2221   if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2222     if (srccaps)
2223       gst_caps_unref (srccaps);
2224     if (sinkcaps)
2225       gst_caps_unref (sinkcaps);
2226     goto done;
2227   }
2228
2229   compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2230   gst_caps_unref (srccaps);
2231   gst_caps_unref (sinkcaps);
2232
2233 done:
2234   GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2235       (compatible ? "" : "not "));
2236
2237   return compatible;
2238 }
2239
2240 /* check if the grandparents of both pads are the same.
2241  * This check is required so that we don't try to link
2242  * pads from elements in different bins without ghostpads.
2243  *
2244  * The LOCK should be held on both pads
2245  */
2246 static gboolean
2247 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2248 {
2249   GstObject *psrc, *psink;
2250
2251   psrc = GST_OBJECT_PARENT (src);
2252   psink = GST_OBJECT_PARENT (sink);
2253
2254   /* if one of the pads has no parent, we allow the link */
2255   if (G_UNLIKELY (psrc == NULL || psink == NULL))
2256     goto no_parent;
2257
2258   /* only care about parents that are elements */
2259   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2260     goto no_element_parent;
2261
2262   /* if the parents are the same, we have a loop */
2263   if (G_UNLIKELY (psrc == psink))
2264     goto same_parents;
2265
2266   /* if they both have a parent, we check the grandparents. We can not lock
2267    * the parent because we hold on the child (pad) and the locking order is
2268    * parent >> child. */
2269   psrc = GST_OBJECT_PARENT (psrc);
2270   psink = GST_OBJECT_PARENT (psink);
2271
2272   /* if they have grandparents but they are not the same */
2273   if (G_UNLIKELY (psrc != psink))
2274     goto wrong_grandparents;
2275
2276   return TRUE;
2277
2278   /* ERRORS */
2279 no_parent:
2280   {
2281     GST_CAT_DEBUG (GST_CAT_CAPS,
2282         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2283         GST_PTR_FORMAT, psrc, psink);
2284     return TRUE;
2285   }
2286 no_element_parent:
2287   {
2288     GST_CAT_DEBUG (GST_CAT_CAPS,
2289         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2290         GST_PTR_FORMAT, psrc, psink);
2291     return TRUE;
2292   }
2293 same_parents:
2294   {
2295     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2296         psrc);
2297     return FALSE;
2298   }
2299 wrong_grandparents:
2300   {
2301     GST_CAT_DEBUG (GST_CAT_CAPS,
2302         "pads have different grandparents %" GST_PTR_FORMAT " and %"
2303         GST_PTR_FORMAT, psrc, psink);
2304     return FALSE;
2305   }
2306 }
2307
2308 /* FIXME leftover from an attempt at refactoring... */
2309 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2310  * the two pads will be locked in the srcpad, sinkpad order. */
2311 static GstPadLinkReturn
2312 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2313 {
2314   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2315       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2316
2317   GST_OBJECT_LOCK (srcpad);
2318
2319   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2320     goto src_was_linked;
2321
2322   GST_OBJECT_LOCK (sinkpad);
2323
2324   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2325     goto sink_was_linked;
2326
2327   /* check hierarchy, pads can only be linked if the grandparents
2328    * are the same. */
2329   if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2330       && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2331     goto wrong_hierarchy;
2332
2333   /* check pad caps for non-empty intersection */
2334   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2335     goto no_format;
2336
2337   /* FIXME check pad scheduling for non-empty intersection */
2338
2339   return GST_PAD_LINK_OK;
2340
2341 src_was_linked:
2342   {
2343     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2344         GST_DEBUG_PAD_NAME (srcpad),
2345         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2346     /* we do not emit a warning in this case because unlinking cannot
2347      * be made MT safe.*/
2348     GST_OBJECT_UNLOCK (srcpad);
2349     return GST_PAD_LINK_WAS_LINKED;
2350   }
2351 sink_was_linked:
2352   {
2353     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2354         GST_DEBUG_PAD_NAME (sinkpad),
2355         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2356     /* we do not emit a warning in this case because unlinking cannot
2357      * be made MT safe.*/
2358     GST_OBJECT_UNLOCK (sinkpad);
2359     GST_OBJECT_UNLOCK (srcpad);
2360     return GST_PAD_LINK_WAS_LINKED;
2361   }
2362 wrong_hierarchy:
2363   {
2364     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2365     GST_OBJECT_UNLOCK (sinkpad);
2366     GST_OBJECT_UNLOCK (srcpad);
2367     return GST_PAD_LINK_WRONG_HIERARCHY;
2368   }
2369 no_format:
2370   {
2371     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2372     GST_OBJECT_UNLOCK (sinkpad);
2373     GST_OBJECT_UNLOCK (srcpad);
2374     return GST_PAD_LINK_NOFORMAT;
2375   }
2376 }
2377
2378 /**
2379  * gst_pad_can_link:
2380  * @srcpad: the source #GstPad.
2381  * @sinkpad: the sink #GstPad.
2382  *
2383  * Checks if the source pad and the sink pad are compatible so they can be
2384  * linked.
2385  *
2386  * Returns: %TRUE if the pads can be linked.
2387  */
2388 gboolean
2389 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2390 {
2391   GstPadLinkReturn result;
2392
2393   /* generic checks */
2394   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2395   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2396
2397   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2398       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2399
2400   /* gst_pad_link_prepare does everything for us, we only release the locks
2401    * on the pads that it gets us. If this function returns !OK the locks are not
2402    * taken anymore. */
2403   result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2404   if (result != GST_PAD_LINK_OK)
2405     goto done;
2406
2407   GST_OBJECT_UNLOCK (srcpad);
2408   GST_OBJECT_UNLOCK (sinkpad);
2409
2410 done:
2411   return result == GST_PAD_LINK_OK;
2412 }
2413
2414 /**
2415  * gst_pad_link_full:
2416  * @srcpad: the source #GstPad to link.
2417  * @sinkpad: the sink #GstPad to link.
2418  * @flags: the checks to validate when linking
2419  *
2420  * Links the source pad and the sink pad.
2421  *
2422  * This variant of #gst_pad_link provides a more granular control on the
2423  * checks being done when linking. While providing some considerable speedups
2424  * the caller of this method must be aware that wrong usage of those flags
2425  * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2426  * for more information.
2427  *
2428  * MT Safe.
2429  *
2430  * Returns: A result code indicating if the connection worked or
2431  *          what went wrong.
2432  */
2433 GstPadLinkReturn
2434 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2435 {
2436   GstPadLinkReturn result;
2437   GstElement *parent;
2438   GstPadLinkFunction srcfunc, sinkfunc;
2439
2440   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2441   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2442   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2443   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2444       GST_PAD_LINK_WRONG_DIRECTION);
2445
2446   /* Notify the parent early. See gst_pad_unlink for details. */
2447   if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2448     if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2449       gst_element_post_message (parent,
2450           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2451               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2452     } else {
2453       gst_object_unref (parent);
2454       parent = NULL;
2455     }
2456   }
2457
2458   /* prepare will also lock the two pads */
2459   result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2460
2461   if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2462     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2463         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2464         gst_pad_link_get_name (result));
2465     goto done;
2466   }
2467
2468   /* must set peers before calling the link function */
2469   GST_PAD_PEER (srcpad) = sinkpad;
2470   GST_PAD_PEER (sinkpad) = srcpad;
2471
2472   /* check events, when something is different, mark pending */
2473   schedule_events (srcpad, sinkpad);
2474
2475   /* get the link functions */
2476   srcfunc = GST_PAD_LINKFUNC (srcpad);
2477   sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2478
2479   if (G_UNLIKELY (srcfunc || sinkfunc)) {
2480     /* custom link functions, execute them */
2481     GST_OBJECT_UNLOCK (sinkpad);
2482     GST_OBJECT_UNLOCK (srcpad);
2483
2484     if (srcfunc) {
2485       GstObject *tmpparent;
2486
2487       ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2488       /* this one will call the peer link function */
2489       result = srcfunc (srcpad, tmpparent, sinkpad);
2490       RELEASE_PARENT (tmpparent);
2491     } else if (sinkfunc) {
2492       GstObject *tmpparent;
2493
2494       ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2495       /* if no source link function, we need to call the sink link
2496        * function ourselves. */
2497       result = sinkfunc (sinkpad, tmpparent, srcpad);
2498       RELEASE_PARENT (tmpparent);
2499     }
2500   no_parent:
2501
2502     GST_OBJECT_LOCK (srcpad);
2503     GST_OBJECT_LOCK (sinkpad);
2504
2505     /* we released the lock, check if the same pads are linked still */
2506     if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2507       goto concurrent_link;
2508
2509     if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2510       goto link_failed;
2511   }
2512   GST_OBJECT_UNLOCK (sinkpad);
2513   GST_OBJECT_UNLOCK (srcpad);
2514
2515   /* fire off a signal to each of the pads telling them
2516    * that they've been linked */
2517   g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2518   g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2519
2520   GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2521       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2522
2523   gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2524
2525 done:
2526   if (G_LIKELY (parent)) {
2527     gst_element_post_message (parent,
2528         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2529             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2530     gst_object_unref (parent);
2531   }
2532
2533   return result;
2534
2535   /* ERRORS */
2536 concurrent_link:
2537   {
2538     GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2539         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2540     GST_OBJECT_UNLOCK (sinkpad);
2541     GST_OBJECT_UNLOCK (srcpad);
2542
2543     /* The other link operation succeeded first */
2544     result = GST_PAD_LINK_WAS_LINKED;
2545     goto done;
2546   }
2547 link_failed:
2548   {
2549     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2550         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2551         gst_pad_link_get_name (result));
2552
2553     GST_PAD_PEER (srcpad) = NULL;
2554     GST_PAD_PEER (sinkpad) = NULL;
2555
2556     GST_OBJECT_UNLOCK (sinkpad);
2557     GST_OBJECT_UNLOCK (srcpad);
2558
2559     goto done;
2560   }
2561 }
2562
2563 /**
2564  * gst_pad_link:
2565  * @srcpad: the source #GstPad to link.
2566  * @sinkpad: the sink #GstPad to link.
2567  *
2568  * Links the source pad and the sink pad.
2569  *
2570  * Returns: A result code indicating if the connection worked or
2571  *          what went wrong.
2572  *
2573  * MT Safe.
2574  */
2575 GstPadLinkReturn
2576 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2577 {
2578   GstPadLinkReturn ret;
2579
2580   GST_TRACER_PAD_LINK_PRE (srcpad, sinkpad);
2581   ret = gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2582   GST_TRACER_PAD_LINK_POST (srcpad, sinkpad, ret);
2583
2584   return ret;
2585 }
2586
2587 static void
2588 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2589 {
2590   GstPadTemplate **template_p;
2591
2592   /* this function would need checks if it weren't static */
2593
2594   GST_OBJECT_LOCK (pad);
2595   template_p = &pad->padtemplate;
2596   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2597   GST_OBJECT_UNLOCK (pad);
2598
2599   if (templ)
2600     gst_pad_template_pad_created (templ, pad);
2601 }
2602
2603 /**
2604  * gst_pad_get_pad_template:
2605  * @pad: a #GstPad.
2606  *
2607  * Gets the template for @pad.
2608  *
2609  * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2610  *     this pad was instantiated, or %NULL if this pad has no
2611  *     template. Unref after usage.
2612  */
2613 GstPadTemplate *
2614 gst_pad_get_pad_template (GstPad * pad)
2615 {
2616   GstPadTemplate *templ;
2617
2618   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2619
2620   templ = GST_PAD_PAD_TEMPLATE (pad);
2621
2622   return (templ ? gst_object_ref (templ) : NULL);
2623 }
2624
2625 /**
2626  * gst_pad_has_current_caps:
2627  * @pad: a  #GstPad to check
2628  *
2629  * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2630  *
2631  * Returns: %TRUE when @pad has caps associated with it.
2632  */
2633 gboolean
2634 gst_pad_has_current_caps (GstPad * pad)
2635 {
2636   gboolean result;
2637   GstCaps *caps;
2638
2639   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2640
2641   GST_OBJECT_LOCK (pad);
2642   caps = get_pad_caps (pad);
2643   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2644       "check current pad caps %" GST_PTR_FORMAT, caps);
2645   result = (caps != NULL);
2646   GST_OBJECT_UNLOCK (pad);
2647
2648   return result;
2649 }
2650
2651 /**
2652  * gst_pad_get_current_caps:
2653  * @pad: a  #GstPad to get the current capabilities of.
2654  *
2655  * Gets the capabilities currently configured on @pad with the last
2656  * #GST_EVENT_CAPS event.
2657  *
2658  * Returns: (transfer full) (nullable): the current caps of the pad with
2659  * incremented ref-count or %NULL when pad has no caps. Unref after usage.
2660  */
2661 GstCaps *
2662 gst_pad_get_current_caps (GstPad * pad)
2663 {
2664   GstCaps *result;
2665
2666   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2667
2668   GST_OBJECT_LOCK (pad);
2669   if ((result = get_pad_caps (pad)))
2670     gst_caps_ref (result);
2671   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2672       "get current pad caps %" GST_PTR_FORMAT, result);
2673   GST_OBJECT_UNLOCK (pad);
2674
2675   return result;
2676 }
2677
2678 /**
2679  * gst_pad_get_pad_template_caps:
2680  * @pad: a #GstPad to get the template capabilities from.
2681  *
2682  * Gets the capabilities for @pad's template.
2683  *
2684  * Returns: (transfer full): the #GstCaps of this pad template.
2685  * Unref after usage.
2686  */
2687 GstCaps *
2688 gst_pad_get_pad_template_caps (GstPad * pad)
2689 {
2690   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2691
2692   if (GST_PAD_PAD_TEMPLATE (pad))
2693     return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2694
2695   return gst_caps_ref (GST_CAPS_ANY);
2696 }
2697
2698 /**
2699  * gst_pad_get_peer:
2700  * @pad: a #GstPad to get the peer of.
2701  *
2702  * Gets the peer of @pad. This function refs the peer pad so
2703  * you need to unref it after use.
2704  *
2705  * Returns: (transfer full): the peer #GstPad. Unref after usage.
2706  *
2707  * MT safe.
2708  */
2709 GstPad *
2710 gst_pad_get_peer (GstPad * pad)
2711 {
2712   GstPad *result;
2713
2714   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2715
2716   GST_OBJECT_LOCK (pad);
2717   result = GST_PAD_PEER (pad);
2718   if (result)
2719     gst_object_ref (result);
2720   GST_OBJECT_UNLOCK (pad);
2721
2722   return result;
2723 }
2724
2725 /**
2726  * gst_pad_get_allowed_caps:
2727  * @pad: a #GstPad.
2728  *
2729  * Gets the capabilities of the allowed media types that can flow through
2730  * @pad and its peer.
2731  *
2732  * The allowed capabilities is calculated as the intersection of the results of
2733  * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2734  * on the resulting caps.
2735  *
2736  * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2737  *     pad link. Unref the caps when you no longer need it. This
2738  *     function returns %NULL when @pad has no peer.
2739  *
2740  * MT safe.
2741  */
2742 GstCaps *
2743 gst_pad_get_allowed_caps (GstPad * pad)
2744 {
2745   GstCaps *mycaps;
2746   GstCaps *caps = NULL;
2747   GstQuery *query;
2748
2749   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2750
2751   GST_OBJECT_LOCK (pad);
2752   if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2753     goto no_peer;
2754   GST_OBJECT_UNLOCK (pad);
2755
2756   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2757
2758   mycaps = gst_pad_query_caps (pad, NULL);
2759
2760   /* Query peer caps */
2761   query = gst_query_new_caps (mycaps);
2762   gst_pad_peer_query (pad, query);
2763   gst_query_parse_caps_result (query, &caps);
2764   gst_caps_ref (caps);
2765   gst_query_unref (query);
2766
2767   gst_caps_unref (mycaps);
2768
2769   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2770       caps);
2771
2772   return caps;
2773
2774 no_peer:
2775   {
2776     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2777     GST_OBJECT_UNLOCK (pad);
2778
2779     return NULL;
2780   }
2781 }
2782
2783 /**
2784  * gst_pad_iterate_internal_links_default:
2785  * @pad: the #GstPad to get the internal links of.
2786  * @parent: (allow-none): the parent of @pad or %NULL
2787  *
2788  * Iterate the list of pads to which the given pad is linked to inside of
2789  * the parent element.
2790  * This is the default handler, and thus returns an iterator of all of the
2791  * pads inside the parent element with opposite direction.
2792  *
2793  * The caller must free this iterator after use with gst_iterator_free().
2794  *
2795  * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2796  * has no parent. Unref each returned pad with gst_object_unref().
2797  */
2798 GstIterator *
2799 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2800 {
2801   GstIterator *res;
2802   GList **padlist;
2803   guint32 *cookie;
2804   GMutex *lock;
2805   gpointer owner;
2806   GstElement *eparent;
2807
2808   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2809
2810   if (parent != NULL && GST_IS_ELEMENT (parent)) {
2811     eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2812   } else {
2813     GST_OBJECT_LOCK (pad);
2814     eparent = GST_PAD_PARENT (pad);
2815     if (!eparent || !GST_IS_ELEMENT (eparent))
2816       goto no_parent;
2817
2818     gst_object_ref (eparent);
2819     GST_OBJECT_UNLOCK (pad);
2820   }
2821
2822   if (pad->direction == GST_PAD_SRC)
2823     padlist = &eparent->sinkpads;
2824   else
2825     padlist = &eparent->srcpads;
2826
2827   GST_DEBUG_OBJECT (pad, "Making iterator");
2828
2829   cookie = &eparent->pads_cookie;
2830   owner = eparent;
2831   lock = GST_OBJECT_GET_LOCK (eparent);
2832
2833   res = gst_iterator_new_list (GST_TYPE_PAD,
2834       lock, cookie, padlist, (GObject *) owner, NULL);
2835
2836   gst_object_unref (owner);
2837
2838   return res;
2839
2840   /* ERRORS */
2841 no_parent:
2842   {
2843     GST_OBJECT_UNLOCK (pad);
2844     GST_DEBUG_OBJECT (pad, "no parent element");
2845     return NULL;
2846   }
2847 }
2848
2849 /**
2850  * gst_pad_iterate_internal_links:
2851  * @pad: the GstPad to get the internal links of.
2852  *
2853  * Gets an iterator for the pads to which the given pad is linked to inside
2854  * of the parent element.
2855  *
2856  * Each #GstPad element yielded by the iterator will have its refcount increased,
2857  * so unref after use.
2858  *
2859  * Free-function: gst_iterator_free
2860  *
2861  * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2862  *     or %NULL when the pad does not have an iterator function
2863  *     configured. Use gst_iterator_free() after usage.
2864  */
2865 GstIterator *
2866 gst_pad_iterate_internal_links (GstPad * pad)
2867 {
2868   GstIterator *res = NULL;
2869   GstObject *parent;
2870
2871   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2872
2873   GST_OBJECT_LOCK (pad);
2874   ACQUIRE_PARENT (pad, parent, no_parent);
2875   GST_OBJECT_UNLOCK (pad);
2876
2877   if (GST_PAD_ITERINTLINKFUNC (pad))
2878     res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2879
2880   RELEASE_PARENT (parent);
2881
2882   return res;
2883
2884   /* ERRORS */
2885 no_parent:
2886   {
2887     GST_DEBUG_OBJECT (pad, "no parent");
2888     GST_OBJECT_UNLOCK (pad);
2889     return NULL;
2890   }
2891 }
2892
2893 /**
2894  * gst_pad_forward:
2895  * @pad: a #GstPad
2896  * @forward: (scope call): a #GstPadForwardFunction
2897  * @user_data: user data passed to @forward
2898  *
2899  * Calls @forward for all internally linked pads of @pad. This function deals with
2900  * dynamically changing internal pads and will make sure that the @forward
2901  * function is only called once for each pad.
2902  *
2903  * When @forward returns %TRUE, no further pads will be processed.
2904  *
2905  * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2906  */
2907 gboolean
2908 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2909     gpointer user_data)
2910 {
2911   gboolean result = FALSE;
2912   GstIterator *iter;
2913   gboolean done = FALSE;
2914   GValue item = { 0, };
2915   GList *pushed_pads = NULL;
2916
2917   iter = gst_pad_iterate_internal_links (pad);
2918
2919   if (!iter)
2920     goto no_iter;
2921
2922   while (!done) {
2923     switch (gst_iterator_next (iter, &item)) {
2924       case GST_ITERATOR_OK:
2925       {
2926         GstPad *intpad;
2927
2928         intpad = g_value_get_object (&item);
2929
2930         /* if already pushed, skip. FIXME, find something faster to tag pads */
2931         if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2932           g_value_reset (&item);
2933           break;
2934         }
2935
2936         GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2937             GST_DEBUG_PAD_NAME (intpad));
2938         done = result = forward (intpad, user_data);
2939
2940         pushed_pads = g_list_prepend (pushed_pads, intpad);
2941
2942         g_value_reset (&item);
2943         break;
2944       }
2945       case GST_ITERATOR_RESYNC:
2946         /* We don't reset the result here because we don't push the event
2947          * again on pads that got the event already and because we need
2948          * to consider the result of the previous pushes */
2949         gst_iterator_resync (iter);
2950         break;
2951       case GST_ITERATOR_ERROR:
2952         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2953         done = TRUE;
2954         break;
2955       case GST_ITERATOR_DONE:
2956         done = TRUE;
2957         break;
2958     }
2959   }
2960   g_value_unset (&item);
2961   gst_iterator_free (iter);
2962
2963   g_list_free (pushed_pads);
2964
2965 no_iter:
2966   return result;
2967 }
2968
2969 typedef struct
2970 {
2971   GstEvent *event;
2972   gboolean result;
2973   gboolean dispatched;
2974 } EventData;
2975
2976 static gboolean
2977 event_forward_func (GstPad * pad, EventData * data)
2978 {
2979   /* for each pad we send to, we should ref the event; it's up
2980    * to downstream to unref again when handled. */
2981   GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2982       data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2983
2984   data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2985
2986   data->dispatched = TRUE;
2987
2988   /* don't stop */
2989   return FALSE;
2990 }
2991
2992 /**
2993  * gst_pad_event_default:
2994  * @pad: a #GstPad to call the default event handler on.
2995  * @parent: (allow-none): the parent of @pad or %NULL
2996  * @event: (transfer full): the #GstEvent to handle.
2997  *
2998  * Invokes the default event handler for the given pad.
2999  *
3000  * The EOS event will pause the task associated with @pad before it is forwarded
3001  * to all internally linked pads,
3002  *
3003  * The event is sent to all pads internally linked to @pad. This function
3004  * takes ownership of @event.
3005  *
3006  * Returns: %TRUE if the event was sent successfully.
3007  */
3008 gboolean
3009 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
3010 {
3011   gboolean result, forward = TRUE;
3012
3013   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3014   g_return_val_if_fail (event != NULL, FALSE);
3015
3016   GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
3017       event);
3018
3019   switch (GST_EVENT_TYPE (event)) {
3020     case GST_EVENT_CAPS:
3021       forward = GST_PAD_IS_PROXY_CAPS (pad);
3022       result = TRUE;
3023       break;
3024     default:
3025       break;
3026   }
3027
3028   if (forward) {
3029     EventData data;
3030
3031     data.event = event;
3032     data.dispatched = FALSE;
3033     data.result = FALSE;
3034
3035     gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3036
3037     /* for sinkpads without a parent element or without internal links, nothing
3038      * will be dispatched but we still want to return TRUE. */
3039     if (data.dispatched)
3040       result = data.result;
3041     else
3042       result = TRUE;
3043   }
3044
3045   gst_event_unref (event);
3046
3047   return result;
3048 }
3049
3050 /* Default accept caps implementation just checks against
3051  * the allowed caps for the pad */
3052 static gboolean
3053 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
3054 {
3055   /* get the caps and see if it intersects to something not empty */
3056   GstCaps *caps, *allowed = NULL;
3057   gboolean result;
3058
3059   GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
3060
3061   /* first forward the query to internally linked pads when we are dealing with
3062    * a PROXY CAPS */
3063   if (GST_PAD_IS_PROXY_CAPS (pad)) {
3064     result = gst_pad_proxy_query_accept_caps (pad, query);
3065     if (result)
3066       allowed = gst_pad_get_pad_template_caps (pad);
3067     else
3068       goto done;
3069   }
3070
3071   gst_query_parse_accept_caps (query, &caps);
3072   if (!allowed) {
3073     GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
3074         "fallback ACCEPT_CAPS query, consider implementing a specialized version");
3075     if (GST_PAD_IS_ACCEPT_TEMPLATE (pad))
3076       allowed = gst_pad_get_pad_template_caps (pad);
3077     else
3078       allowed = gst_pad_query_caps (pad, caps);
3079   }
3080
3081   if (allowed) {
3082     if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
3083       GST_DEBUG_OBJECT (pad,
3084           "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
3085           allowed, caps);
3086       result = gst_caps_can_intersect (caps, allowed);
3087     } else {
3088       GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
3089           GST_PTR_FORMAT, allowed, caps);
3090       result = gst_caps_is_subset (caps, allowed);
3091     }
3092     gst_caps_unref (allowed);
3093   } else {
3094     GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
3095     result = FALSE;
3096   }
3097   gst_query_set_accept_caps_result (query, result);
3098
3099 done:
3100   return TRUE;
3101 }
3102
3103 /* Default caps implementation */
3104 static gboolean
3105 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3106 {
3107   GstCaps *result = NULL, *filter;
3108   GstPadTemplate *templ;
3109   gboolean fixed_caps;
3110
3111   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3112       query);
3113
3114   /* first try to proxy if we must */
3115   if (GST_PAD_IS_PROXY_CAPS (pad)) {
3116     if ((gst_pad_proxy_query_caps (pad, query))) {
3117       goto done;
3118     }
3119   }
3120
3121   gst_query_parse_caps (query, &filter);
3122
3123   /* no proxy or it failed, do default handling */
3124   fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3125
3126   GST_OBJECT_LOCK (pad);
3127   if (fixed_caps) {
3128     /* fixed caps, try the negotiated caps first */
3129     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3130     if ((result = get_pad_caps (pad)))
3131       goto filter_done_unlock;
3132   }
3133
3134   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3135     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3136     if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3137       goto filter_done_unlock;
3138   }
3139
3140   if (!fixed_caps) {
3141     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3142         "non-fixed pad caps: trying pad caps");
3143     /* non fixed caps, try the negotiated caps */
3144     if ((result = get_pad_caps (pad)))
3145       goto filter_done_unlock;
3146   }
3147
3148   /* this almost never happens */
3149   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3150   result = GST_CAPS_ANY;
3151
3152 filter_done_unlock:
3153   GST_OBJECT_UNLOCK (pad);
3154
3155   /* run the filter on the result */
3156   if (filter) {
3157     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3158         "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3159         GST_PTR_FORMAT, result, result, filter, filter);
3160     result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3161     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3162         result, result);
3163   } else {
3164     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3165         "using caps %p %" GST_PTR_FORMAT, result, result);
3166     result = gst_caps_ref (result);
3167   }
3168   gst_query_set_caps_result (query, result);
3169   gst_caps_unref (result);
3170
3171 done:
3172   return TRUE;
3173 }
3174
3175 /* Default latency implementation */
3176 typedef struct
3177 {
3178   guint count;
3179   gboolean live;
3180   GstClockTime min, max;
3181 } LatencyFoldData;
3182
3183 static gboolean
3184 query_latency_default_fold (const GValue * item, GValue * ret,
3185     gpointer user_data)
3186 {
3187   GstPad *pad = g_value_get_object (item), *peer;
3188   LatencyFoldData *fold_data = user_data;
3189   GstQuery *query;
3190   gboolean res = FALSE;
3191
3192   query = gst_query_new_latency ();
3193
3194   peer = gst_pad_get_peer (pad);
3195   if (peer) {
3196     res = gst_pad_peer_query (pad, query);
3197   } else {
3198     GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3199   }
3200
3201   if (res) {
3202     gboolean live;
3203     GstClockTime min, max;
3204
3205     gst_query_parse_latency (query, &live, &min, &max);
3206
3207     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3208         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3209
3210     /* FIXME : Why do we only take values into account if it's live ? */
3211     if (live || fold_data->count == 0) {
3212       if (min > fold_data->min)
3213         fold_data->min = min;
3214
3215       if (fold_data->max == GST_CLOCK_TIME_NONE)
3216         fold_data->max = max;
3217       else if (max < fold_data->max)
3218         fold_data->max = max;
3219
3220       fold_data->live = live;
3221     }
3222     fold_data->count += 1;
3223   } else if (peer) {
3224     GST_DEBUG_OBJECT (pad, "latency query failed");
3225     g_value_set_boolean (ret, FALSE);
3226   }
3227
3228   gst_query_unref (query);
3229   if (peer)
3230     gst_object_unref (peer);
3231
3232   return TRUE;
3233 }
3234
3235 static gboolean
3236 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3237 {
3238   GstIterator *it;
3239   GstIteratorResult res;
3240   GValue ret = G_VALUE_INIT;
3241   gboolean query_ret;
3242   LatencyFoldData fold_data;
3243
3244   it = gst_pad_iterate_internal_links (pad);
3245   if (!it) {
3246     GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3247     return FALSE;
3248   }
3249
3250   g_value_init (&ret, G_TYPE_BOOLEAN);
3251
3252 retry:
3253   fold_data.count = 0;
3254   fold_data.live = FALSE;
3255   fold_data.min = 0;
3256   fold_data.max = GST_CLOCK_TIME_NONE;
3257
3258   g_value_set_boolean (&ret, TRUE);
3259   res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3260   switch (res) {
3261     case GST_ITERATOR_OK:
3262       g_assert_not_reached ();
3263       break;
3264     case GST_ITERATOR_DONE:
3265       break;
3266     case GST_ITERATOR_ERROR:
3267       g_value_set_boolean (&ret, FALSE);
3268       break;
3269     case GST_ITERATOR_RESYNC:
3270       gst_iterator_resync (it);
3271       goto retry;
3272     default:
3273       g_assert_not_reached ();
3274       break;
3275   }
3276   gst_iterator_free (it);
3277
3278   query_ret = g_value_get_boolean (&ret);
3279   if (query_ret) {
3280     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3281         " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3282         fold_data.min, fold_data.max);
3283
3284     if (fold_data.min > fold_data.max) {
3285       GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3286     }
3287
3288     gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3289   } else {
3290     GST_LOG_OBJECT (pad, "latency query failed");
3291   }
3292
3293   return query_ret;
3294 }
3295
3296 typedef struct
3297 {
3298   GstQuery *query;
3299   gboolean result;
3300   gboolean dispatched;
3301 } QueryData;
3302
3303 static gboolean
3304 query_forward_func (GstPad * pad, QueryData * data)
3305 {
3306   GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3307       data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3308
3309   data->result |= gst_pad_peer_query (pad, data->query);
3310
3311   data->dispatched = TRUE;
3312
3313   /* stop on first successful reply */
3314   return data->result;
3315 }
3316
3317 /**
3318  * gst_pad_query_default:
3319  * @pad: a #GstPad to call the default query handler on.
3320  * @parent: (allow-none): the parent of @pad or %NULL
3321  * @query: (transfer none): the #GstQuery to handle.
3322  *
3323  * Invokes the default query handler for the given pad.
3324  * The query is sent to all pads internally linked to @pad. Note that
3325  * if there are many possible sink pads that are internally linked to
3326  * @pad, only one will be sent the query.
3327  * Multi-sinkpad elements should implement custom query handlers.
3328  *
3329  * Returns: %TRUE if the query was performed successfully.
3330  */
3331 gboolean
3332 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3333 {
3334   gboolean forward, ret = FALSE;
3335
3336   switch (GST_QUERY_TYPE (query)) {
3337     case GST_QUERY_SCHEDULING:
3338       forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3339       break;
3340     case GST_QUERY_ALLOCATION:
3341       forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3342       break;
3343     case GST_QUERY_ACCEPT_CAPS:
3344       ret = gst_pad_query_accept_caps_default (pad, query);
3345       forward = FALSE;
3346       break;
3347     case GST_QUERY_CAPS:
3348       ret = gst_pad_query_caps_default (pad, query);
3349       forward = FALSE;
3350       break;
3351     case GST_QUERY_LATENCY:
3352       ret = gst_pad_query_latency_default (pad, query);
3353       forward = FALSE;
3354       break;
3355     case GST_QUERY_POSITION:
3356     case GST_QUERY_SEEKING:
3357     case GST_QUERY_FORMATS:
3358     case GST_QUERY_JITTER:
3359     case GST_QUERY_RATE:
3360     case GST_QUERY_CONVERT:
3361     default:
3362       forward = TRUE;
3363       break;
3364   }
3365
3366   GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3367       query, GST_QUERY_TYPE_NAME (query));
3368
3369   if (forward) {
3370     QueryData data;
3371
3372     data.query = query;
3373     data.dispatched = FALSE;
3374     data.result = FALSE;
3375
3376     gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3377
3378     if (data.dispatched) {
3379       ret = data.result;
3380     } else {
3381       /* nothing dispatched, assume drained */
3382       if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3383         ret = TRUE;
3384       else
3385         ret = FALSE;
3386     }
3387   }
3388   return ret;
3389 }
3390
3391 static void
3392 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3393 {
3394   GstPad *pad = data->pad;
3395   GstPadProbeInfo *info = data->info;
3396   GstPadProbeType type, flags;
3397   GstPadProbeCallback callback;
3398   GstPadProbeReturn ret;
3399   gpointer original_data;
3400
3401   /* if we have called this callback, do nothing */
3402   if (PROBE_COOKIE (hook) == data->cookie) {
3403     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3404         "hook %lu, cookie %u already called", hook->hook_id,
3405         PROBE_COOKIE (hook));
3406     return;
3407   }
3408
3409   PROBE_COOKIE (hook) = data->cookie;
3410
3411   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3412   type = info->type;
3413   original_data = info->data;
3414
3415   /* one of the scheduling types */
3416   if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3417     goto no_match;
3418
3419   if (type & GST_PAD_PROBE_TYPE_PUSH) {
3420     /* one of the data types for non-idle probes */
3421     if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3422         && (flags & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH & type) == 0)
3423       goto no_match;
3424   } else if (type & GST_PAD_PROBE_TYPE_PULL) {
3425     /* one of the data types for non-idle probes */
3426     if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0
3427         && (flags & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH & type) == 0)
3428       goto no_match;
3429   } else {
3430     /* Type must have PULL or PUSH probe types */
3431     g_assert_not_reached ();
3432   }
3433
3434   /* one of the blocking types must match */
3435   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3436       (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3437     goto no_match;
3438   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3439       (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3440     goto no_match;
3441   /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3442   if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3443       (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3444     goto no_match;
3445
3446   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3447       "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3448       PROBE_COOKIE (hook), flags);
3449
3450   data->marshalled = TRUE;
3451
3452   callback = (GstPadProbeCallback) hook->func;
3453   if (callback == NULL)
3454     return;
3455
3456   info->id = hook->hook_id;
3457
3458   GST_OBJECT_UNLOCK (pad);
3459
3460   ret = callback (pad, info, hook->data);
3461
3462   GST_OBJECT_LOCK (pad);
3463
3464   if (original_data != NULL && info->data == NULL) {
3465     GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3466     info->type = GST_PAD_PROBE_TYPE_INVALID;
3467     data->dropped = TRUE;
3468   }
3469
3470   switch (ret) {
3471     case GST_PAD_PROBE_REMOVE:
3472       /* remove the probe */
3473       GST_DEBUG_OBJECT (pad, "asked to remove hook");
3474       cleanup_hook (pad, hook);
3475       break;
3476     case GST_PAD_PROBE_DROP:
3477       /* need to drop the data, make sure other probes don't get called
3478        * anymore */
3479       GST_DEBUG_OBJECT (pad, "asked to drop item");
3480       info->type = GST_PAD_PROBE_TYPE_INVALID;
3481       data->dropped = TRUE;
3482       break;
3483     case GST_PAD_PROBE_HANDLED:
3484       GST_DEBUG_OBJECT (pad, "probe handled data");
3485       data->handled = TRUE;
3486       break;
3487     case GST_PAD_PROBE_PASS:
3488       /* inform the pad block to let things pass */
3489       GST_DEBUG_OBJECT (pad, "asked to pass item");
3490       data->pass = TRUE;
3491       break;
3492     case GST_PAD_PROBE_OK:
3493       GST_DEBUG_OBJECT (pad, "probe returned OK");
3494       break;
3495     default:
3496       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3497       break;
3498   }
3499   return;
3500
3501 no_match:
3502   {
3503     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3504         "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3505         hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3506     return;
3507   }
3508 }
3509
3510 /* a probe that does not take or return any data */
3511 #define PROBE_NO_DATA(pad,mask,label,defaultval)                \
3512   G_STMT_START {                                                \
3513     if (G_UNLIKELY (pad->num_probes)) {                         \
3514       GstFlowReturn pval = defaultval;                          \
3515       /* pass NULL as the data item */                          \
3516       GstPadProbeInfo info = { mask, 0, NULL, 0, 0 };           \
3517       info.ABI.abi.flow_ret = defaultval;                       \
3518       ret = do_probe_callbacks (pad, &info, defaultval);        \
3519       if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK))       \
3520         goto label;                                             \
3521     }                                                           \
3522   } G_STMT_END
3523
3524 #define PROBE_FULL(pad,mask,data,offs,size,label,handleable,handle_label) \
3525   G_STMT_START {                                                        \
3526     if (G_UNLIKELY (pad->num_probes)) {                                 \
3527       /* pass the data item */                                          \
3528       GstPadProbeInfo info = { mask, 0, data, offs, size };             \
3529       info.ABI.abi.flow_ret = GST_FLOW_OK;                              \
3530       ret = do_probe_callbacks (pad, &info, GST_FLOW_OK);               \
3531       /* store the possibly updated data item */                        \
3532       data = GST_PAD_PROBE_INFO_DATA (&info);                           \
3533       /* if something went wrong, exit */                               \
3534       if (G_UNLIKELY (ret != GST_FLOW_OK)) {                            \
3535         if (handleable && ret == GST_FLOW_CUSTOM_SUCCESS_1) {           \
3536           ret = info.ABI.abi.flow_ret;                                          \
3537           goto handle_label;                                            \
3538         }                                                               \
3539         goto label;                                                     \
3540       }                                                                 \
3541     }                                                                   \
3542   } G_STMT_END
3543
3544 #define PROBE_PUSH(pad,mask,data,label)         \
3545   PROBE_FULL(pad, mask, data, -1, -1, label, FALSE, label);
3546 #define PROBE_HANDLE(pad,mask,data,label,handle_label)  \
3547   PROBE_FULL(pad, mask, data, -1, -1, label, TRUE, handle_label);
3548 #define PROBE_PULL(pad,mask,data,offs,size,label)               \
3549   PROBE_FULL(pad, mask, data, offs, size, label, FALSE, label);
3550
3551 static GstFlowReturn
3552 do_pad_idle_probe_wait (GstPad * pad)
3553 {
3554   while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3555     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3556         "waiting idle probe to be removed");
3557     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3558     GST_PAD_BLOCK_WAIT (pad);
3559     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3560     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3561
3562     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3563       return GST_FLOW_FLUSHING;
3564   }
3565   return GST_FLOW_OK;
3566 }
3567
3568 #define PROBE_TYPE_IS_SERIALIZED(i) \
3569     ( \
3570       ( \
3571         (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3572         GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3573         GST_EVENT_IS_SERIALIZED ((i)->data)) \
3574       ) || ( \
3575         (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3576         GST_QUERY_IS_SERIALIZED ((i)->data)) \
3577       ) || ( \
3578         ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3579         GST_PAD_PROBE_TYPE_BUFFER_LIST))  \
3580       ) \
3581     )
3582
3583 static GstFlowReturn
3584 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3585     GstFlowReturn defaultval)
3586 {
3587   ProbeMarshall data;
3588   guint cookie;
3589   gboolean is_block;
3590
3591   data.pad = pad;
3592   data.info = info;
3593   data.pass = FALSE;
3594   data.handled = FALSE;
3595   data.marshalled = FALSE;
3596   data.dropped = FALSE;
3597   data.cookie = ++pad->priv->probe_cookie;
3598
3599   is_block =
3600       (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3601
3602   if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3603     if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3604       goto flushing;
3605   }
3606
3607 again:
3608   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3609       "do probes cookie %u", data.cookie);
3610   cookie = pad->priv->probe_list_cookie;
3611
3612   g_hook_list_marshal (&pad->probes, TRUE,
3613       (GHookMarshaller) probe_hook_marshal, &data);
3614
3615   /* if the list changed, call the new callbacks (they will not have their
3616    * cookie set to data.cookie */
3617   if (cookie != pad->priv->probe_list_cookie) {
3618     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3619         "probe list changed, restarting");
3620     goto again;
3621   }
3622
3623   /* the first item that dropped will stop the hooks and then we drop here */
3624   if (data.dropped)
3625     goto dropped;
3626
3627   /* If one handler took care of it, let the the item pass */
3628   if (data.handled) {
3629     goto handled;
3630   }
3631
3632   /* if no handler matched and we are blocking, let the item pass */
3633   if (!data.marshalled && is_block)
3634     goto passed;
3635
3636   /* At this point, all handlers returned either OK or PASS. If one handler
3637    * returned PASS, let the item pass */
3638   if (data.pass)
3639     goto passed;
3640
3641   if (is_block) {
3642     while (GST_PAD_IS_BLOCKED (pad)) {
3643       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3644           "we are blocked %d times", pad->num_blocked);
3645
3646       /* we might have released the lock */
3647       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3648         goto flushing;
3649
3650       /* now we block the streaming thread. It can be unlocked when we
3651        * deactivate the pad (which will also set the FLUSHING flag) or
3652        * when the pad is unblocked. A flushing event will also unblock
3653        * the pad after setting the FLUSHING flag. */
3654       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3655           "Waiting to be unblocked or set flushing");
3656       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3657       GST_PAD_BLOCK_WAIT (pad);
3658       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3659       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3660
3661       /* if the list changed, call the new callbacks (they will not have their
3662        * cookie set to data.cookie */
3663       if (cookie != pad->priv->probe_list_cookie) {
3664         GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3665             "probe list changed, restarting");
3666         goto again;
3667       }
3668
3669       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3670         goto flushing;
3671     }
3672   }
3673
3674   return defaultval;
3675
3676   /* ERRORS */
3677 flushing:
3678   {
3679     GST_DEBUG_OBJECT (pad, "pad is flushing");
3680     return GST_FLOW_FLUSHING;
3681   }
3682 dropped:
3683   {
3684     GST_DEBUG_OBJECT (pad, "data is dropped");
3685     return GST_FLOW_CUSTOM_SUCCESS;
3686   }
3687 passed:
3688   {
3689     /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3690     GST_DEBUG_OBJECT (pad, "data is passed");
3691     return GST_FLOW_OK;
3692   }
3693 handled:
3694   {
3695     GST_DEBUG_OBJECT (pad, "data was handled");
3696     return GST_FLOW_CUSTOM_SUCCESS_1;
3697   }
3698 }
3699
3700 /* pad offsets */
3701
3702 /**
3703  * gst_pad_get_offset:
3704  * @pad: a #GstPad
3705  *
3706  * Get the offset applied to the running time of @pad. @pad has to be a source
3707  * pad.
3708  *
3709  * Returns: the offset.
3710  */
3711 gint64
3712 gst_pad_get_offset (GstPad * pad)
3713 {
3714   gint64 result;
3715
3716   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3717
3718   GST_OBJECT_LOCK (pad);
3719   result = pad->offset;
3720   GST_OBJECT_UNLOCK (pad);
3721
3722   return result;
3723 }
3724
3725 static gboolean
3726 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3727 {
3728   ev->received = FALSE;
3729   return TRUE;
3730 }
3731
3732 /**
3733  * gst_pad_set_offset:
3734  * @pad: a #GstPad
3735  * @offset: the offset
3736  *
3737  * Set the offset that will be applied to the running time of @pad.
3738  */
3739 void
3740 gst_pad_set_offset (GstPad * pad, gint64 offset)
3741 {
3742   g_return_if_fail (GST_IS_PAD (pad));
3743
3744   GST_OBJECT_LOCK (pad);
3745   /* if nothing changed, do nothing */
3746   if (pad->offset == offset)
3747     goto done;
3748
3749   pad->offset = offset;
3750   GST_DEBUG_OBJECT (pad, "changed offset to %" GST_STIME_FORMAT,
3751       GST_STIME_ARGS (offset));
3752
3753   /* resend all sticky events with updated offset on next buffer push */
3754   events_foreach (pad, mark_event_not_received, NULL);
3755   GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3756
3757 done:
3758   GST_OBJECT_UNLOCK (pad);
3759 }
3760
3761 typedef struct
3762 {
3763   GstFlowReturn ret;
3764
3765   /* If TRUE and ret is not OK this means
3766    * that pushing the EOS event failed
3767    */
3768   gboolean was_eos;
3769
3770   /* If called for an event this is
3771    * the event that would be pushed
3772    * next. Don't forward sticky events
3773    * that would come after that */
3774   GstEvent *event;
3775 } PushStickyData;
3776
3777 /* should be called with pad LOCK */
3778 static gboolean
3779 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3780 {
3781   PushStickyData *data = user_data;
3782   GstEvent *event = ev->event;
3783
3784   if (ev->received) {
3785     GST_DEBUG_OBJECT (pad, "event %s was already received",
3786         GST_EVENT_TYPE_NAME (event));
3787     return TRUE;
3788   }
3789
3790   /* If we're called because of an sticky event, only forward
3791    * events that would come before this new event and the
3792    * event itself */
3793   if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3794       GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3795       GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3796     data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3797   } else {
3798     data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3799         GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3800   }
3801
3802   switch (data->ret) {
3803     case GST_FLOW_OK:
3804       ev->received = TRUE;
3805       GST_DEBUG_OBJECT (pad, "event %s marked received",
3806           GST_EVENT_TYPE_NAME (event));
3807       break;
3808     case GST_FLOW_CUSTOM_SUCCESS:
3809       /* we can't assume the event is received when it was dropped */
3810       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3811           GST_EVENT_TYPE_NAME (event));
3812       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3813       data->ret = GST_FLOW_OK;
3814       break;
3815     case GST_FLOW_CUSTOM_SUCCESS_1:
3816       /* event was ignored and should be sent later */
3817       GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3818           GST_EVENT_TYPE_NAME (event));
3819       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3820       data->ret = GST_FLOW_OK;
3821       break;
3822     case GST_FLOW_NOT_LINKED:
3823       /* not linked is not a problem, we are sticky so the event will be
3824        * rescheduled to be sent later on re-link, but only for non-EOS events */
3825       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3826       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
3827         data->ret = GST_FLOW_OK;
3828         ev->received = TRUE;
3829       }
3830       break;
3831     default:
3832       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3833           gst_flow_get_name (data->ret));
3834       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3835       break;
3836   }
3837
3838   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3839     data->was_eos = TRUE;
3840
3841   return data->ret == GST_FLOW_OK;
3842 }
3843
3844 /* check sticky events and push them when needed. should be called
3845  * with pad LOCK */
3846 static inline GstFlowReturn
3847 check_sticky (GstPad * pad, GstEvent * event)
3848 {
3849   PushStickyData data = { GST_FLOW_OK, FALSE, event };
3850
3851   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3852     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3853
3854     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3855     events_foreach (pad, push_sticky, &data);
3856
3857     /* If there's an EOS event we must push it downstream
3858      * even if sending a previous sticky event failed.
3859      * Otherwise the pipeline might wait forever for EOS.
3860      *
3861      * Only do this if pushing another event than the EOS
3862      * event failed.
3863      */
3864     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3865       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3866
3867       if (ev && !ev->received) {
3868         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3869             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3870         /* the event could have been dropped. Because this can only
3871          * happen if the user asked for it, it's not an error */
3872         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3873           data.ret = GST_FLOW_OK;
3874       }
3875     }
3876   }
3877   return data.ret;
3878 }
3879
3880
3881 /**
3882  * gst_pad_query:
3883  * @pad: a #GstPad to invoke the default query on.
3884  * @query: (transfer none): the #GstQuery to perform.
3885  *
3886  * Dispatches a query to a pad. The query should have been allocated by the
3887  * caller via one of the type-specific allocation functions. The element that
3888  * the pad belongs to is responsible for filling the query with an appropriate
3889  * response, which should then be parsed with a type-specific query parsing
3890  * function.
3891  *
3892  * Again, the caller is responsible for both the allocation and deallocation of
3893  * the query structure.
3894  *
3895  * Please also note that some queries might need a running pipeline to work.
3896  *
3897  * Returns: %TRUE if the query could be performed.
3898  */
3899 gboolean
3900 gst_pad_query (GstPad * pad, GstQuery * query)
3901 {
3902   GstObject *parent;
3903   gboolean res, serialized;
3904   GstPadQueryFunction func;
3905   GstPadProbeType type;
3906   GstFlowReturn ret;
3907
3908   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3909   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3910
3911   if (GST_PAD_IS_SRC (pad)) {
3912     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3913       goto wrong_direction;
3914     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3915   } else if (GST_PAD_IS_SINK (pad)) {
3916     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3917       goto wrong_direction;
3918     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3919   } else
3920     goto unknown_direction;
3921
3922   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3923       GST_QUERY_TYPE_NAME (query));
3924   GST_TRACER_PAD_QUERY_PRE (pad, query);
3925
3926   serialized = GST_QUERY_IS_SERIALIZED (query);
3927   if (G_UNLIKELY (serialized))
3928     GST_PAD_STREAM_LOCK (pad);
3929
3930   GST_OBJECT_LOCK (pad);
3931   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3932       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3933   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3934
3935   ACQUIRE_PARENT (pad, parent, no_parent);
3936   GST_OBJECT_UNLOCK (pad);
3937
3938   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3939     goto no_func;
3940
3941   res = func (pad, parent, query);
3942
3943   RELEASE_PARENT (parent);
3944
3945   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3946       GST_QUERY_TYPE_NAME (query), res);
3947   GST_TRACER_PAD_QUERY_POST (pad, query, res);
3948
3949   if (res != TRUE)
3950     goto query_failed;
3951
3952   GST_OBJECT_LOCK (pad);
3953   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3954   GST_OBJECT_UNLOCK (pad);
3955
3956   if (G_UNLIKELY (serialized))
3957     GST_PAD_STREAM_UNLOCK (pad);
3958
3959   return res;
3960
3961   /* ERRORS */
3962 wrong_direction:
3963   {
3964     g_warning ("pad %s:%s query %s in wrong direction",
3965         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3966     return FALSE;
3967   }
3968 unknown_direction:
3969   {
3970     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3971     return FALSE;
3972   }
3973 no_parent:
3974   {
3975     GST_DEBUG_OBJECT (pad, "had no parent");
3976     GST_OBJECT_UNLOCK (pad);
3977     if (G_UNLIKELY (serialized))
3978       GST_PAD_STREAM_UNLOCK (pad);
3979     return FALSE;
3980   }
3981 no_func:
3982   {
3983     GST_DEBUG_OBJECT (pad, "had no query function");
3984     RELEASE_PARENT (parent);
3985     if (G_UNLIKELY (serialized))
3986       GST_PAD_STREAM_UNLOCK (pad);
3987     return FALSE;
3988   }
3989 query_failed:
3990   {
3991     GST_DEBUG_OBJECT (pad, "query failed");
3992     if (G_UNLIKELY (serialized))
3993       GST_PAD_STREAM_UNLOCK (pad);
3994     return FALSE;
3995   }
3996 probe_stopped:
3997   {
3998     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3999     GST_OBJECT_UNLOCK (pad);
4000     if (G_UNLIKELY (serialized))
4001       GST_PAD_STREAM_UNLOCK (pad);
4002
4003     /* if a probe dropped without handling, we don't sent it further but assume
4004      * that the probe did not answer the query and return FALSE */
4005     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4006       res = FALSE;
4007     else
4008       res = TRUE;
4009
4010     return res;
4011   }
4012 }
4013
4014 /**
4015  * gst_pad_peer_query:
4016  * @pad: a #GstPad to invoke the peer query on.
4017  * @query: (transfer none): the #GstQuery to perform.
4018  *
4019  * Performs gst_pad_query() on the peer of @pad.
4020  *
4021  * The caller is responsible for both the allocation and deallocation of
4022  * the query structure.
4023  *
4024  * Returns: %TRUE if the query could be performed. This function returns %FALSE
4025  * if @pad has no peer.
4026  */
4027 gboolean
4028 gst_pad_peer_query (GstPad * pad, GstQuery * query)
4029 {
4030   GstPad *peerpad;
4031   GstPadProbeType type;
4032   gboolean res, serialized;
4033   GstFlowReturn ret;
4034
4035   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4036   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
4037
4038   if (GST_PAD_IS_SRC (pad)) {
4039     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
4040       goto wrong_direction;
4041     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
4042   } else if (GST_PAD_IS_SINK (pad)) {
4043     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
4044       goto wrong_direction;
4045     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
4046   } else
4047     goto unknown_direction;
4048
4049   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
4050       GST_QUERY_TYPE_NAME (query));
4051
4052   serialized = GST_QUERY_IS_SERIALIZED (query);
4053
4054   GST_OBJECT_LOCK (pad);
4055   if (GST_PAD_IS_SRC (pad) && serialized) {
4056     /* all serialized queries on the srcpad trigger push of
4057      * sticky events */
4058     if (check_sticky (pad, NULL) != GST_FLOW_OK)
4059       goto sticky_failed;
4060   }
4061
4062   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4063       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4064   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4065
4066   peerpad = GST_PAD_PEER (pad);
4067   if (G_UNLIKELY (peerpad == NULL))
4068     goto no_peer;
4069
4070   gst_object_ref (peerpad);
4071   GST_OBJECT_UNLOCK (pad);
4072
4073   res = gst_pad_query (peerpad, query);
4074
4075   gst_object_unref (peerpad);
4076
4077   if (res != TRUE)
4078     goto query_failed;
4079
4080   GST_OBJECT_LOCK (pad);
4081   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4082   GST_OBJECT_UNLOCK (pad);
4083
4084   return res;
4085
4086   /* ERRORS */
4087 wrong_direction:
4088   {
4089     g_warning ("pad %s:%s query %s in wrong direction",
4090         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4091     return FALSE;
4092   }
4093 unknown_direction:
4094   {
4095     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4096     return FALSE;
4097   }
4098 sticky_failed:
4099   {
4100     GST_WARNING_OBJECT (pad, "could not send sticky events");
4101     GST_OBJECT_UNLOCK (pad);
4102     return FALSE;
4103   }
4104 no_peer:
4105   {
4106     GST_INFO_OBJECT (pad, "pad has no peer");
4107     GST_OBJECT_UNLOCK (pad);
4108     return FALSE;
4109   }
4110 query_failed:
4111   {
4112     GST_DEBUG_OBJECT (pad, "query failed");
4113     return FALSE;
4114   }
4115 probe_stopped:
4116   {
4117     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4118     GST_OBJECT_UNLOCK (pad);
4119
4120     /* if a probe dropped without handling, we don't sent it further but
4121      * assume that the probe did not answer the query and return FALSE */
4122     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4123       res = FALSE;
4124     else
4125       res = TRUE;
4126
4127     return res;
4128   }
4129 }
4130
4131 /**********************************************************************
4132  * Data passing functions
4133  */
4134
4135 /* this is the chain function that does not perform the additional argument
4136  * checking for that little extra speed.
4137  */
4138 static inline GstFlowReturn
4139 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4140 {
4141   GstFlowReturn ret;
4142   GstObject *parent;
4143   gboolean handled = FALSE;
4144
4145   GST_PAD_STREAM_LOCK (pad);
4146
4147   GST_OBJECT_LOCK (pad);
4148   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4149     goto flushing;
4150
4151   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4152     goto eos;
4153
4154   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4155     goto wrong_mode;
4156
4157 #ifdef GST_ENABLE_EXTRA_CHECKS
4158   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4159     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4160       g_warning (G_STRLOC
4161           ":%s:<%s:%s> Got data flow before stream-start event",
4162           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4163     }
4164     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4165       g_warning (G_STRLOC
4166           ":%s:<%s:%s> Got data flow before segment event",
4167           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4168     }
4169     pad->priv->last_cookie = pad->priv->events_cookie;
4170   }
4171 #endif
4172
4173   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4174       probe_handled);
4175
4176   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4177
4178   ACQUIRE_PARENT (pad, parent, no_parent);
4179   GST_OBJECT_UNLOCK (pad);
4180
4181   /* NOTE: we read the chainfunc unlocked.
4182    * we cannot hold the lock for the pad so we might send
4183    * the data to the wrong function. This is not really a
4184    * problem since functions are assigned at creation time
4185    * and don't change that often... */
4186   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4187     GstPadChainFunction chainfunc;
4188
4189     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4190       goto no_function;
4191
4192     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4193         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4194         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4195
4196     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4197
4198     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4199         "called chainfunction &%s with buffer %p, returned %s",
4200         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4201   } else {
4202     GstPadChainListFunction chainlistfunc;
4203
4204     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4205       goto no_function;
4206
4207     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4208         "calling chainlistfunction &%s",
4209         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4210
4211     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4212
4213     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4214         "called chainlistfunction &%s, returned %s",
4215         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4216   }
4217
4218   RELEASE_PARENT (parent);
4219
4220   GST_PAD_STREAM_UNLOCK (pad);
4221
4222   return ret;
4223
4224   /* ERRORS */
4225 flushing:
4226   {
4227     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4228         "chaining, but pad was flushing");
4229     GST_OBJECT_UNLOCK (pad);
4230     GST_PAD_STREAM_UNLOCK (pad);
4231     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4232     return GST_FLOW_FLUSHING;
4233   }
4234 eos:
4235   {
4236     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4237     GST_OBJECT_UNLOCK (pad);
4238     GST_PAD_STREAM_UNLOCK (pad);
4239     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4240     return GST_FLOW_EOS;
4241   }
4242 wrong_mode:
4243   {
4244     g_critical ("chain on pad %s:%s but it was not in push mode",
4245         GST_DEBUG_PAD_NAME (pad));
4246     GST_OBJECT_UNLOCK (pad);
4247     GST_PAD_STREAM_UNLOCK (pad);
4248     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4249     return GST_FLOW_ERROR;
4250   }
4251 probe_handled:
4252   handled = TRUE;
4253   /* PASSTHROUGH */
4254 probe_stopped:
4255   {
4256     GST_OBJECT_UNLOCK (pad);
4257     GST_PAD_STREAM_UNLOCK (pad);
4258     /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4259     if (!handled)
4260       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4261
4262     switch (ret) {
4263       case GST_FLOW_CUSTOM_SUCCESS:
4264       case GST_FLOW_CUSTOM_SUCCESS_1:
4265         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4266         ret = GST_FLOW_OK;
4267         break;
4268       default:
4269         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4270         break;
4271     }
4272     return ret;
4273   }
4274 no_parent:
4275   {
4276     GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4277     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4278     GST_OBJECT_UNLOCK (pad);
4279     GST_PAD_STREAM_UNLOCK (pad);
4280     return GST_FLOW_FLUSHING;
4281   }
4282 no_function:
4283   {
4284     RELEASE_PARENT (parent);
4285     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4286     g_critical ("chain on pad %s:%s but it has no chainfunction",
4287         GST_DEBUG_PAD_NAME (pad));
4288     GST_PAD_STREAM_UNLOCK (pad);
4289     return GST_FLOW_NOT_SUPPORTED;
4290   }
4291 }
4292
4293 /**
4294  * gst_pad_chain:
4295  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4296  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4297  *     if not.
4298  *
4299  * Chain a buffer to @pad.
4300  *
4301  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4302  *
4303  * If the buffer type is not acceptable for @pad (as negotiated with a
4304  * preceding GST_EVENT_CAPS event), this function returns
4305  * #GST_FLOW_NOT_NEGOTIATED.
4306  *
4307  * The function proceeds calling the chain function installed on @pad (see
4308  * gst_pad_set_chain_function()) and the return value of that function is
4309  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4310  * chain function.
4311  *
4312  * In all cases, success or failure, the caller loses its reference to @buffer
4313  * after calling this function.
4314  *
4315  * Returns: a #GstFlowReturn from the pad.
4316  *
4317  * MT safe.
4318  */
4319 GstFlowReturn
4320 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4321 {
4322   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4323   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4324   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4325
4326   return gst_pad_chain_data_unchecked (pad,
4327       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4328 }
4329
4330 static GstFlowReturn
4331 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4332     GstBufferList * list)
4333 {
4334   guint i, len;
4335   GstBuffer *buffer;
4336   GstFlowReturn ret;
4337
4338   GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4339
4340   len = gst_buffer_list_length (list);
4341
4342   ret = GST_FLOW_OK;
4343   for (i = 0; i < len; i++) {
4344     buffer = gst_buffer_list_get (list, i);
4345     ret =
4346         gst_pad_chain_data_unchecked (pad,
4347         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4348         gst_buffer_ref (buffer));
4349     if (ret != GST_FLOW_OK)
4350       break;
4351   }
4352   gst_buffer_list_unref (list);
4353
4354   return ret;
4355 }
4356
4357 /**
4358  * gst_pad_chain_list:
4359  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4360  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4361  *     if not.
4362  *
4363  * Chain a bufferlist to @pad.
4364  *
4365  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4366  *
4367  * If @pad was not negotiated properly with a CAPS event, this function
4368  * returns #GST_FLOW_NOT_NEGOTIATED.
4369  *
4370  * The function proceeds calling the chainlist function installed on @pad (see
4371  * gst_pad_set_chain_list_function()) and the return value of that function is
4372  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4373  * chainlist function.
4374  *
4375  * In all cases, success or failure, the caller loses its reference to @list
4376  * after calling this function.
4377  *
4378  * MT safe.
4379  *
4380  * Returns: a #GstFlowReturn from the pad.
4381  */
4382 GstFlowReturn
4383 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4384 {
4385   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4386   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4387   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4388
4389   return gst_pad_chain_data_unchecked (pad,
4390       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4391 }
4392
4393 static GstFlowReturn
4394 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4395 {
4396   GstPad *peer;
4397   GstFlowReturn ret;
4398   gboolean handled = FALSE;
4399
4400   GST_OBJECT_LOCK (pad);
4401   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4402     goto flushing;
4403
4404   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4405     goto eos;
4406
4407   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4408     goto wrong_mode;
4409
4410 #ifdef GST_ENABLE_EXTRA_CHECKS
4411   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4412     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4413       g_warning (G_STRLOC
4414           ":%s:<%s:%s> Got data flow before stream-start event",
4415           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4416     }
4417     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4418       g_warning (G_STRLOC
4419           ":%s:<%s:%s> Got data flow before segment event",
4420           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4421     }
4422     pad->priv->last_cookie = pad->priv->events_cookie;
4423   }
4424 #endif
4425
4426   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4427     goto events_error;
4428
4429   /* do block probes */
4430   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4431       probe_handled);
4432
4433   /* recheck sticky events because the probe might have cause a relink */
4434   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4435     goto events_error;
4436
4437   /* do post-blocking probes */
4438   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4439
4440   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4441     goto not_linked;
4442
4443   /* take ref to peer pad before releasing the lock */
4444   gst_object_ref (peer);
4445   pad->priv->using++;
4446   GST_OBJECT_UNLOCK (pad);
4447
4448   ret = gst_pad_chain_data_unchecked (peer, type, data);
4449   data = NULL;
4450
4451   gst_object_unref (peer);
4452
4453   GST_OBJECT_LOCK (pad);
4454   pad->ABI.abi.last_flowret = ret;
4455   pad->priv->using--;
4456   if (pad->priv->using == 0) {
4457     /* pad is not active anymore, trigger idle callbacks */
4458     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4459         probe_stopped, ret);
4460   }
4461   GST_OBJECT_UNLOCK (pad);
4462
4463   return ret;
4464
4465   /* ERROR recovery here */
4466   /* ERRORS */
4467 flushing:
4468   {
4469     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4470         "pushing, but pad was flushing");
4471     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4472     GST_OBJECT_UNLOCK (pad);
4473     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4474     return GST_FLOW_FLUSHING;
4475   }
4476 eos:
4477   {
4478     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4479     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4480     GST_OBJECT_UNLOCK (pad);
4481     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4482     return GST_FLOW_EOS;
4483   }
4484 wrong_mode:
4485   {
4486     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4487         GST_DEBUG_PAD_NAME (pad));
4488     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4489     GST_OBJECT_UNLOCK (pad);
4490     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4491     return GST_FLOW_ERROR;
4492   }
4493 events_error:
4494   {
4495     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4496         "error pushing events, return %s", gst_flow_get_name (ret));
4497     pad->ABI.abi.last_flowret = ret;
4498     GST_OBJECT_UNLOCK (pad);
4499     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4500     return ret;
4501   }
4502 probe_handled:
4503   handled = TRUE;
4504   /* PASSTHROUGH */
4505 probe_stopped:
4506   {
4507     GST_OBJECT_UNLOCK (pad);
4508     if (data != NULL && !handled)
4509       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4510
4511     switch (ret) {
4512       case GST_FLOW_CUSTOM_SUCCESS:
4513       case GST_FLOW_CUSTOM_SUCCESS_1:
4514         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4515         ret = GST_FLOW_OK;
4516         break;
4517       default:
4518         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4519         break;
4520     }
4521     pad->ABI.abi.last_flowret = ret;
4522     return ret;
4523   }
4524 not_linked:
4525   {
4526     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4527         "pushing, but it was not linked");
4528     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4529     GST_OBJECT_UNLOCK (pad);
4530     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4531     return GST_FLOW_NOT_LINKED;
4532   }
4533 }
4534
4535 /**
4536  * gst_pad_push:
4537  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4538  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4539  *     if not.
4540  *
4541  * Pushes a buffer to the peer of @pad.
4542  *
4543  * This function will call installed block probes before triggering any
4544  * installed data probes.
4545  *
4546  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4547  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4548  * be returned.
4549  *
4550  * In all cases, success or failure, the caller loses its reference to @buffer
4551  * after calling this function.
4552  *
4553  * Returns: a #GstFlowReturn from the peer pad.
4554  *
4555  * MT safe.
4556  */
4557 GstFlowReturn
4558 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4559 {
4560   GstFlowReturn res;
4561
4562   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4563   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4564   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4565
4566   GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4567   res = gst_pad_push_data (pad,
4568       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4569   GST_TRACER_PAD_PUSH_POST (pad, res);
4570   return res;
4571 }
4572
4573 /**
4574  * gst_pad_push_list:
4575  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4576  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4577  *     if not.
4578  *
4579  * Pushes a buffer list to the peer of @pad.
4580  *
4581  * This function will call installed block probes before triggering any
4582  * installed data probes.
4583  *
4584  * The function proceeds calling the chain function on the peer pad and returns
4585  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4586  * be returned. If the peer pad does not have any installed chainlist function
4587  * every group buffer of the list will be merged into a normal #GstBuffer and
4588  * chained via gst_pad_chain().
4589  *
4590  * In all cases, success or failure, the caller loses its reference to @list
4591  * after calling this function.
4592  *
4593  * Returns: a #GstFlowReturn from the peer pad.
4594  *
4595  * MT safe.
4596  */
4597 GstFlowReturn
4598 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4599 {
4600   GstFlowReturn res;
4601
4602   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4603   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4604   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4605
4606   GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4607   res = gst_pad_push_data (pad,
4608       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4609   GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4610   return res;
4611 }
4612
4613 static GstFlowReturn
4614 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4615     GstBuffer ** buffer)
4616 {
4617   GstFlowReturn ret;
4618   GstPadGetRangeFunction getrangefunc;
4619   GstObject *parent;
4620   GstBuffer *res_buf;
4621
4622   GST_PAD_STREAM_LOCK (pad);
4623
4624   GST_OBJECT_LOCK (pad);
4625   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4626     goto flushing;
4627
4628   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4629     goto wrong_mode;
4630
4631   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4632     goto events_error;
4633
4634   res_buf = *buffer;
4635
4636   /* when one of the probes returns DROPPED, probe_stopped will be called
4637    * and we skip calling the getrange function, res_buf should then contain a
4638    * valid result buffer */
4639   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4640       res_buf, offset, size, probe_stopped);
4641
4642   /* recheck sticky events because the probe might have cause a relink */
4643   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4644     goto events_error;
4645
4646   ACQUIRE_PARENT (pad, parent, no_parent);
4647   GST_OBJECT_UNLOCK (pad);
4648
4649   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4650     goto no_function;
4651
4652   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4653       "calling getrangefunc %s, offset %"
4654       G_GUINT64_FORMAT ", size %u",
4655       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4656
4657   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4658
4659   RELEASE_PARENT (parent);
4660
4661   GST_OBJECT_LOCK (pad);
4662   if (G_UNLIKELY (ret != GST_FLOW_OK))
4663     goto get_range_failed;
4664
4665   /* can only fire the signal if we have a valid buffer */
4666 probed_data:
4667   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4668       res_buf, offset, size, probe_stopped_unref);
4669   pad->ABI.abi.last_flowret = ret;
4670   GST_OBJECT_UNLOCK (pad);
4671
4672   GST_PAD_STREAM_UNLOCK (pad);
4673
4674   *buffer = res_buf;
4675
4676   return ret;
4677
4678   /* ERRORS */
4679 flushing:
4680   {
4681     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4682         "getrange, but pad was flushing");
4683     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4684     GST_OBJECT_UNLOCK (pad);
4685     GST_PAD_STREAM_UNLOCK (pad);
4686     return GST_FLOW_FLUSHING;
4687   }
4688 wrong_mode:
4689   {
4690     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4691         GST_DEBUG_PAD_NAME (pad));
4692     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4693     GST_OBJECT_UNLOCK (pad);
4694     GST_PAD_STREAM_UNLOCK (pad);
4695     return GST_FLOW_ERROR;
4696   }
4697 events_error:
4698   {
4699     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4700     pad->ABI.abi.last_flowret = ret;
4701     GST_OBJECT_UNLOCK (pad);
4702     GST_PAD_STREAM_UNLOCK (pad);
4703     return ret;
4704   }
4705 no_parent:
4706   {
4707     GST_DEBUG_OBJECT (pad, "no parent");
4708     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4709     GST_OBJECT_UNLOCK (pad);
4710     GST_PAD_STREAM_UNLOCK (pad);
4711     return GST_FLOW_FLUSHING;
4712   }
4713 no_function:
4714   {
4715     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4716         GST_DEBUG_PAD_NAME (pad));
4717     RELEASE_PARENT (parent);
4718     GST_PAD_STREAM_UNLOCK (pad);
4719     return GST_FLOW_NOT_SUPPORTED;
4720   }
4721 probe_stopped:
4722   {
4723     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4724         "probe returned %s", gst_flow_get_name (ret));
4725     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4726       if (res_buf) {
4727         /* the probe filled the buffer and asks us to not call the getrange
4728          * anymore, we continue with the post probes then. */
4729         GST_DEBUG_OBJECT (pad, "handled buffer");
4730         ret = GST_FLOW_OK;
4731         goto probed_data;
4732       } else {
4733         /* no buffer, we are EOS */
4734         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4735         ret = GST_FLOW_EOS;
4736       }
4737     }
4738     pad->ABI.abi.last_flowret = ret;
4739     GST_OBJECT_UNLOCK (pad);
4740     GST_PAD_STREAM_UNLOCK (pad);
4741
4742     return ret;
4743   }
4744 probe_stopped_unref:
4745   {
4746     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4747         "probe returned %s", gst_flow_get_name (ret));
4748     /* if we drop here, it signals EOS */
4749     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4750       ret = GST_FLOW_EOS;
4751     pad->ABI.abi.last_flowret = ret;
4752     GST_OBJECT_UNLOCK (pad);
4753     GST_PAD_STREAM_UNLOCK (pad);
4754     if (*buffer == NULL)
4755       gst_buffer_unref (res_buf);
4756     return ret;
4757   }
4758 get_range_failed:
4759   {
4760     pad->ABI.abi.last_flowret = ret;
4761     GST_OBJECT_UNLOCK (pad);
4762     GST_PAD_STREAM_UNLOCK (pad);
4763     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4764         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4765         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4766     return ret;
4767   }
4768 }
4769
4770 /**
4771  * gst_pad_get_range:
4772  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4773  * @offset: The start offset of the buffer
4774  * @size: The length of the buffer
4775  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4776  *     returns #GST_FLOW_ERROR if %NULL.
4777  *
4778  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4779  * immediately and @buffer is %NULL.
4780  *
4781  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4782  * description of a getrange function. If @pad has no getrange function
4783  * installed (see gst_pad_set_getrange_function()) this function returns
4784  * #GST_FLOW_NOT_SUPPORTED.
4785  *
4786  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4787  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4788  * must be freed with gst_buffer_unref() after usage.
4789  *
4790  * When @buffer points to a variable that points to a valid #GstBuffer, the
4791  * buffer will be filled with the result data when this function returns
4792  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4793  * @size bytes will be filled in the result buffer and its size will be updated
4794  * accordingly.
4795  *
4796  * Note that less than @size bytes can be returned in @buffer when, for example,
4797  * an EOS condition is near or when @buffer is not large enough to hold @size
4798  * bytes. The caller should check the result buffer size to get the result size.
4799  *
4800  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4801  * will be unchanged.
4802  *
4803  * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4804  *
4805  * Returns: a #GstFlowReturn from the pad.
4806  *
4807  * MT safe.
4808  */
4809 GstFlowReturn
4810 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4811     GstBuffer ** buffer)
4812 {
4813   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4814   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4815   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4816   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4817           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4818
4819   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4820 }
4821
4822 /**
4823  * gst_pad_pull_range:
4824  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4825  * @offset: The start offset of the buffer
4826  * @size: The length of the buffer
4827  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4828  *     GST_FLOW_ERROR if %NULL.
4829  *
4830  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4831  *
4832  * This function will first trigger the pad block signal if it was
4833  * installed.
4834  *
4835  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4836  * function returns the result of gst_pad_get_range() on the peer pad.
4837  * See gst_pad_get_range() for a list of return values and for the
4838  * semantics of the arguments of this function.
4839  *
4840  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4841  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4842  * must be freed with gst_buffer_unref() after usage. When this function
4843  * returns any other result value, @buffer will still point to %NULL.
4844  *
4845  * When @buffer points to a variable that points to a valid #GstBuffer, the
4846  * buffer will be filled with the result data when this function returns
4847  * #GST_FLOW_OK. When this function returns any other result value,
4848  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4849  * @size bytes will be filled in the result buffer and its size will be updated
4850  * accordingly.
4851  *
4852  * Note that less than @size bytes can be returned in @buffer when, for example,
4853  * an EOS condition is near or when @buffer is not large enough to hold @size
4854  * bytes. The caller should check the result buffer size to get the result size.
4855  *
4856  * Returns: a #GstFlowReturn from the peer pad.
4857  *
4858  * MT safe.
4859  */
4860 GstFlowReturn
4861 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4862     GstBuffer ** buffer)
4863 {
4864   GstPad *peer;
4865   GstFlowReturn ret;
4866   GstBuffer *res_buf;
4867
4868   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4869   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4870   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4871   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4872           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4873
4874   GST_TRACER_PAD_PULL_RANGE_PRE (pad, offset, size);
4875
4876   GST_OBJECT_LOCK (pad);
4877   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4878     goto flushing;
4879
4880   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4881     goto wrong_mode;
4882
4883   res_buf = *buffer;
4884
4885   /* when one of the probes returns DROPPED, probe_stopped will be
4886    * called and we skip calling the peer getrange function. *buffer should then
4887    * contain a valid buffer */
4888   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4889       res_buf, offset, size, probe_stopped);
4890
4891   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4892     goto not_linked;
4893
4894   gst_object_ref (peer);
4895   pad->priv->using++;
4896   GST_OBJECT_UNLOCK (pad);
4897
4898   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4899
4900   gst_object_unref (peer);
4901
4902   GST_OBJECT_LOCK (pad);
4903   pad->priv->using--;
4904   pad->ABI.abi.last_flowret = ret;
4905   if (pad->priv->using == 0) {
4906     /* pad is not active anymore, trigger idle callbacks */
4907     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4908         probe_stopped_unref, ret);
4909   }
4910
4911   if (G_UNLIKELY (ret != GST_FLOW_OK))
4912     goto pull_range_failed;
4913
4914 probed_data:
4915   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4916       res_buf, offset, size, probe_stopped_unref);
4917
4918   GST_OBJECT_UNLOCK (pad);
4919
4920   *buffer = res_buf;
4921
4922   GST_TRACER_PAD_PULL_RANGE_POST (pad, *buffer, ret);
4923   return ret;
4924
4925   /* ERROR recovery here */
4926 flushing:
4927   {
4928     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4929         "pullrange, but pad was flushing");
4930     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4931     GST_OBJECT_UNLOCK (pad);
4932     ret = GST_FLOW_FLUSHING;
4933     goto done;
4934   }
4935 wrong_mode:
4936   {
4937     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4938         GST_DEBUG_PAD_NAME (pad));
4939     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4940     GST_OBJECT_UNLOCK (pad);
4941     ret = GST_FLOW_ERROR;
4942     goto done;
4943   }
4944 probe_stopped:
4945   {
4946     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4947         gst_flow_get_name (ret));
4948     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4949       if (res_buf) {
4950         /* the probe filled the buffer and asks us to not forward to the peer
4951          * anymore, we continue with the post probes then */
4952         GST_DEBUG_OBJECT (pad, "handled buffer");
4953         ret = GST_FLOW_OK;
4954         goto probed_data;
4955       } else {
4956         /* no buffer, we are EOS then */
4957         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4958         ret = GST_FLOW_EOS;
4959       }
4960     }
4961     pad->ABI.abi.last_flowret = ret;
4962     GST_OBJECT_UNLOCK (pad);
4963     goto done;
4964   }
4965 not_linked:
4966   {
4967     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4968         "pulling range, but it was not linked");
4969     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4970     GST_OBJECT_UNLOCK (pad);
4971     ret = GST_FLOW_NOT_LINKED;
4972     goto done;
4973   }
4974 pull_range_failed:
4975   {
4976     pad->ABI.abi.last_flowret = ret;
4977     GST_OBJECT_UNLOCK (pad);
4978     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4979         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4980         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4981     goto done;
4982   }
4983 probe_stopped_unref:
4984   {
4985     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4986         "post probe returned %s", gst_flow_get_name (ret));
4987
4988     /* if we drop here, it signals EOS */
4989     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4990       ret = GST_FLOW_EOS;
4991
4992     pad->ABI.abi.last_flowret = ret;
4993     GST_OBJECT_UNLOCK (pad);
4994
4995     if (*buffer == NULL)
4996       gst_buffer_unref (res_buf);
4997     goto done;
4998   }
4999 done:
5000   GST_TRACER_PAD_PULL_RANGE_POST (pad, NULL, ret);
5001   return ret;
5002 }
5003
5004 /* must be called with pad object lock */
5005 static GstFlowReturn
5006 store_sticky_event (GstPad * pad, GstEvent * event)
5007 {
5008   guint i, len;
5009   GstEventType type;
5010   GArray *events;
5011   gboolean res = FALSE;
5012   const gchar *name = NULL;
5013   gboolean insert = TRUE;
5014
5015   type = GST_EVENT_TYPE (event);
5016
5017   /* Store all sticky events except SEGMENT/EOS when we're flushing,
5018    * otherwise they can be dropped and nothing would ever resend them.
5019    * Only do that for activated pads though, everything else is a bug!
5020    */
5021   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
5022           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
5023                   || type == GST_EVENT_EOS))))
5024     goto flushed;
5025
5026   /* Unset the EOS flag when received STREAM_START event, so pad can
5027    * store sticky event and then push it later */
5028   if (type == GST_EVENT_STREAM_START) {
5029     GST_LOG_OBJECT (pad, "Removing pending EOS events");
5030     remove_event_by_type (pad, GST_EVENT_EOS);
5031     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5032   }
5033
5034   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5035     goto eos;
5036
5037   if (type & GST_EVENT_TYPE_STICKY_MULTI)
5038     name = gst_structure_get_name (gst_event_get_structure (event));
5039
5040   events = pad->priv->events;
5041   len = events->len;
5042
5043   for (i = 0; i < len; i++) {
5044     PadEvent *ev = &g_array_index (events, PadEvent, i);
5045
5046     if (ev->event == NULL)
5047       continue;
5048
5049     if (type == GST_EVENT_TYPE (ev->event)) {
5050       /* matching types, check matching name if needed */
5051       if (name && !gst_event_has_name (ev->event, name))
5052         continue;
5053
5054       /* overwrite */
5055       if ((res = gst_event_replace (&ev->event, event)))
5056         ev->received = FALSE;
5057
5058       insert = FALSE;
5059       break;
5060     }
5061
5062     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
5063             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
5064       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
5065        * storing the sticky ordered we can check that this is respected. */
5066       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
5067               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
5068         g_warning (G_STRLOC
5069             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
5070             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
5071             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
5072             gst_event_type_get_name (type));
5073       break;
5074     }
5075   }
5076   if (insert) {
5077     PadEvent ev;
5078     ev.event = gst_event_ref (event);
5079     ev.received = FALSE;
5080     g_array_insert_val (events, i, ev);
5081     res = TRUE;
5082   }
5083
5084   if (res) {
5085     pad->priv->events_cookie++;
5086     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5087
5088     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
5089
5090     switch (GST_EVENT_TYPE (event)) {
5091       case GST_EVENT_CAPS:
5092         GST_OBJECT_UNLOCK (pad);
5093
5094         GST_DEBUG_OBJECT (pad, "notify caps");
5095         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
5096
5097         GST_OBJECT_LOCK (pad);
5098         break;
5099       default:
5100         break;
5101     }
5102   }
5103   if (type == GST_EVENT_EOS) {
5104     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
5105     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
5106   }
5107
5108   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
5109
5110   /* ERRORS */
5111 flushed:
5112   {
5113     GST_DEBUG_OBJECT (pad, "pad is flushing");
5114     return GST_FLOW_FLUSHING;
5115   }
5116 eos:
5117   {
5118     GST_DEBUG_OBJECT (pad, "pad is EOS");
5119     return GST_FLOW_EOS;
5120   }
5121 }
5122
5123 /**
5124  * gst_pad_store_sticky_event:
5125  * @pad: a #GstPad
5126  * @event: a #GstEvent
5127  *
5128  * Store the sticky @event on @pad
5129  *
5130  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5131  * was flushing or #GST_FLOW_EOS when the pad was EOS.
5132  *
5133  * Since: 1.2
5134  */
5135 GstFlowReturn
5136 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5137 {
5138   GstFlowReturn ret;
5139
5140   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5141   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5142
5143   GST_OBJECT_LOCK (pad);
5144   ret = store_sticky_event (pad, event);
5145   GST_OBJECT_UNLOCK (pad);
5146
5147   return ret;
5148 }
5149
5150 static gboolean
5151 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5152 {
5153   PushStickyData *data = user_data;
5154
5155   /* Forward all sticky events before our current one that are pending */
5156   if (ev->event != data->event
5157       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5158     return push_sticky (pad, ev, data);
5159
5160   return TRUE;
5161 }
5162
5163 /* should be called with pad LOCK */
5164 static GstFlowReturn
5165 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5166     GstPadProbeType type)
5167 {
5168   GstFlowReturn ret;
5169   GstPad *peerpad;
5170   GstEventType event_type;
5171
5172   /* pass the adjusted event on. We need to do this even if
5173    * there is no peer pad because of the probes. */
5174   event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5175
5176   /* Two checks to be made:
5177    * . (un)set the FLUSHING flag for flushing events,
5178    * . handle pad blocking */
5179   event_type = GST_EVENT_TYPE (event);
5180   switch (event_type) {
5181     case GST_EVENT_FLUSH_START:
5182       GST_PAD_SET_FLUSHING (pad);
5183
5184       GST_PAD_BLOCK_BROADCAST (pad);
5185       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5186       break;
5187     case GST_EVENT_FLUSH_STOP:
5188       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5189         goto inactive;
5190
5191       GST_PAD_UNSET_FLUSHING (pad);
5192
5193       /* Remove sticky EOS events */
5194       GST_LOG_OBJECT (pad, "Removing pending EOS events");
5195       remove_event_by_type (pad, GST_EVENT_EOS);
5196       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5197       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5198       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5199
5200       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5201       break;
5202     default:
5203     {
5204       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5205         goto flushed;
5206
5207       /* No need to check for EOS here as either the caller (gst_pad_push_event())
5208        * checked already or this is called as part of pushing sticky events,
5209        * in which case we still want to forward the EOS event downstream.
5210        */
5211
5212       switch (GST_EVENT_TYPE (event)) {
5213         case GST_EVENT_RECONFIGURE:
5214           if (GST_PAD_IS_SINK (pad))
5215             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5216           break;
5217         default:
5218           break;
5219       }
5220       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5221           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5222       break;
5223     }
5224   }
5225
5226   /* send probes after modifying the events above */
5227   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5228
5229   /* recheck sticky events because the probe might have cause a relink */
5230   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5231       && (GST_EVENT_IS_SERIALIZED (event)
5232           || GST_EVENT_IS_STICKY (event))) {
5233     PushStickyData data = { GST_FLOW_OK, FALSE, event };
5234     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5235
5236     /* Push all sticky events before our current one
5237      * that have changed */
5238     events_foreach (pad, sticky_changed, &data);
5239   }
5240
5241   /* now check the peer pad */
5242   peerpad = GST_PAD_PEER (pad);
5243   if (peerpad == NULL)
5244     goto not_linked;
5245
5246   gst_object_ref (peerpad);
5247   pad->priv->using++;
5248   GST_OBJECT_UNLOCK (pad);
5249
5250   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5251       event, gst_event_type_get_name (event_type), peerpad);
5252
5253   ret = gst_pad_send_event_unchecked (peerpad, event, type);
5254
5255   /* Note: we gave away ownership of the event at this point but we can still
5256    * print the old pointer */
5257   GST_LOG_OBJECT (pad,
5258       "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5259       gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5260
5261   gst_object_unref (peerpad);
5262
5263   GST_OBJECT_LOCK (pad);
5264   pad->priv->using--;
5265   if (pad->priv->using == 0) {
5266     /* pad is not active anymore, trigger idle callbacks */
5267     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5268         idle_probe_stopped, ret);
5269   }
5270   return ret;
5271
5272   /* ERROR handling */
5273 flushed:
5274   {
5275     GST_DEBUG_OBJECT (pad, "We're flushing");
5276     gst_event_unref (event);
5277     return GST_FLOW_FLUSHING;
5278   }
5279 inactive:
5280   {
5281     GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5282     gst_event_unref (event);
5283     return GST_FLOW_FLUSHING;
5284   }
5285 probe_stopped:
5286   {
5287     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5288     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5289       gst_event_unref (event);
5290
5291     switch (ret) {
5292       case GST_FLOW_CUSTOM_SUCCESS_1:
5293         GST_DEBUG_OBJECT (pad, "handled event");
5294         break;
5295       case GST_FLOW_CUSTOM_SUCCESS:
5296         GST_DEBUG_OBJECT (pad, "dropped event");
5297         break;
5298       default:
5299         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5300         break;
5301     }
5302     return ret;
5303   }
5304 not_linked:
5305   {
5306     GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5307         gst_event_type_get_name (GST_EVENT_TYPE (event)));
5308     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5309     gst_event_unref (event);
5310
5311     /* unlinked pads should not influence latency configuration */
5312     if (event_type == GST_EVENT_LATENCY)
5313       return GST_FLOW_OK;
5314
5315     return GST_FLOW_NOT_LINKED;
5316   }
5317 idle_probe_stopped:
5318   {
5319     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5320     return ret;
5321   }
5322 }
5323
5324 /**
5325  * gst_pad_push_event:
5326  * @pad: a #GstPad to push the event to.
5327  * @event: (transfer full): the #GstEvent to send to the pad.
5328  *
5329  * Sends the event to the peer of the given pad. This function is
5330  * mainly used by elements to send events to their peer
5331  * elements.
5332  *
5333  * This function takes ownership of the provided event so you should
5334  * gst_event_ref() it if you want to reuse the event after this call.
5335  *
5336  * Returns: %TRUE if the event was handled.
5337  *
5338  * MT safe.
5339  */
5340 gboolean
5341 gst_pad_push_event (GstPad * pad, GstEvent * event)
5342 {
5343   gboolean res = FALSE;
5344   GstPadProbeType type;
5345   gboolean sticky, serialized;
5346
5347   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5348   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5349
5350   GST_TRACER_PAD_PUSH_EVENT_PRE (pad, event);
5351
5352   if (GST_PAD_IS_SRC (pad)) {
5353     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5354       goto wrong_direction;
5355     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5356   } else if (GST_PAD_IS_SINK (pad)) {
5357     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5358       goto wrong_direction;
5359     /* events pushed on sinkpad never are sticky */
5360     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5361   } else
5362     goto unknown_direction;
5363
5364   GST_OBJECT_LOCK (pad);
5365   sticky = GST_EVENT_IS_STICKY (event);
5366   serialized = GST_EVENT_IS_SERIALIZED (event);
5367
5368   if (sticky) {
5369     /* srcpad sticky events are stored immediately, the received flag is set
5370      * to FALSE and will be set to TRUE when we can successfully push the
5371      * event to the peer pad */
5372     switch (store_sticky_event (pad, event)) {
5373       case GST_FLOW_FLUSHING:
5374         goto flushed;
5375       case GST_FLOW_EOS:
5376         goto eos;
5377       default:
5378         break;
5379     }
5380   }
5381   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5382     /* all serialized or sticky events on the srcpad trigger push of
5383      * sticky events */
5384     res = (check_sticky (pad, event) == GST_FLOW_OK);
5385   }
5386   if (!sticky) {
5387     GstFlowReturn ret;
5388
5389     /* other events are pushed right away */
5390     ret = gst_pad_push_event_unchecked (pad, event, type);
5391     /* dropped events by a probe are not an error */
5392     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5393         || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5394   } else {
5395     /* Errors in sticky event pushing are no problem and ignored here
5396      * as they will cause more meaningful errors during data flow.
5397      * For EOS events, that are not followed by data flow, we still
5398      * return FALSE here though.
5399      */
5400     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5401       res = TRUE;
5402     gst_event_unref (event);
5403   }
5404   GST_OBJECT_UNLOCK (pad);
5405
5406   GST_TRACER_PAD_PUSH_EVENT_POST (pad, res);
5407   return res;
5408
5409   /* ERROR handling */
5410 wrong_direction:
5411   {
5412     g_warning ("pad %s:%s pushing %s event in wrong direction",
5413         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5414     gst_event_unref (event);
5415     goto done;
5416   }
5417 unknown_direction:
5418   {
5419     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5420     gst_event_unref (event);
5421     goto done;
5422   }
5423 flushed:
5424   {
5425     GST_DEBUG_OBJECT (pad, "We're flushing");
5426     GST_OBJECT_UNLOCK (pad);
5427     gst_event_unref (event);
5428     goto done;
5429   }
5430 eos:
5431   {
5432     GST_DEBUG_OBJECT (pad, "We're EOS");
5433     GST_OBJECT_UNLOCK (pad);
5434     gst_event_unref (event);
5435     goto done;
5436   }
5437 done:
5438   GST_TRACER_PAD_PUSH_EVENT_POST (pad, FALSE);
5439   return FALSE;
5440 }
5441
5442 /* Check if we can call the event function with the given event */
5443 static GstFlowReturn
5444 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5445 {
5446   GstCaps *caps;
5447
5448   switch (GST_EVENT_TYPE (event)) {
5449     case GST_EVENT_CAPS:
5450     {
5451       /* backwards compatibility mode for caps */
5452       gst_event_parse_caps (event, &caps);
5453
5454       if (!gst_pad_query_accept_caps (pad, caps))
5455         goto not_accepted;
5456       break;
5457     }
5458     default:
5459       break;
5460   }
5461   return GST_FLOW_OK;
5462
5463   /* ERRORS */
5464 not_accepted:
5465   {
5466     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5467         "caps %" GST_PTR_FORMAT " not accepted", caps);
5468     return GST_FLOW_NOT_NEGOTIATED;
5469   }
5470 }
5471
5472 static GstFlowReturn
5473 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5474     GstPadProbeType type)
5475 {
5476   GstFlowReturn ret;
5477   GstEventType event_type;
5478   gboolean serialized, need_unlock = FALSE, sticky;
5479   GstPadEventFunction eventfunc;
5480   GstPadEventFullFunction eventfullfunc = NULL;
5481   GstObject *parent;
5482
5483   GST_OBJECT_LOCK (pad);
5484
5485   event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5486
5487   if (GST_PAD_IS_SINK (pad))
5488     serialized = GST_EVENT_IS_SERIALIZED (event);
5489   else
5490     serialized = FALSE;
5491   sticky = GST_EVENT_IS_STICKY (event);
5492   event_type = GST_EVENT_TYPE (event);
5493   switch (event_type) {
5494     case GST_EVENT_FLUSH_START:
5495       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5496           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5497
5498       /* can't even accept a flush begin event when flushing */
5499       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5500         goto flushing;
5501
5502       GST_PAD_SET_FLUSHING (pad);
5503       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5504       GST_PAD_BLOCK_BROADCAST (pad);
5505       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5506       break;
5507     case GST_EVENT_FLUSH_STOP:
5508       /* we can't accept flush-stop on inactive pads else the flushing flag
5509        * would be cleared and it would look like the pad can accept data.
5510        * Also, some elements restart a streaming thread in flush-stop which we
5511        * can't allow on inactive pads */
5512       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5513         goto inactive;
5514
5515       GST_PAD_UNSET_FLUSHING (pad);
5516       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5517       /* Remove pending EOS events */
5518       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5519       remove_event_by_type (pad, GST_EVENT_EOS);
5520       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5521       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5522       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5523
5524       GST_OBJECT_UNLOCK (pad);
5525       /* grab stream lock */
5526       GST_PAD_STREAM_LOCK (pad);
5527       need_unlock = TRUE;
5528       GST_OBJECT_LOCK (pad);
5529       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5530         goto flushing;
5531       break;
5532     case GST_EVENT_RECONFIGURE:
5533       if (GST_PAD_IS_SRC (pad))
5534         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5535     default:
5536       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5537           "have event type %" GST_PTR_FORMAT, event);
5538
5539       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5540         goto flushing;
5541
5542       switch (event_type) {
5543         case GST_EVENT_STREAM_START:
5544           /* Remove sticky EOS events */
5545           GST_LOG_OBJECT (pad, "Removing pending EOS events");
5546           remove_event_by_type (pad, GST_EVENT_EOS);
5547           GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5548           break;
5549         default:
5550           break;
5551       }
5552
5553       if (serialized) {
5554         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5555           goto eos;
5556
5557         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5558         GST_OBJECT_UNLOCK (pad);
5559         GST_PAD_STREAM_LOCK (pad);
5560         need_unlock = TRUE;
5561         GST_OBJECT_LOCK (pad);
5562         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5563           goto flushing;
5564
5565         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5566           goto eos;
5567       }
5568       break;
5569   }
5570
5571   /* now do the probe */
5572   PROBE_PUSH (pad,
5573       type | GST_PAD_PROBE_TYPE_PUSH |
5574       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5575
5576   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5577
5578   eventfullfunc = GST_PAD_EVENTFULLFUNC (pad);
5579   eventfunc = GST_PAD_EVENTFUNC (pad);
5580   if (G_UNLIKELY (eventfunc == NULL && eventfullfunc == NULL))
5581     goto no_function;
5582
5583   ACQUIRE_PARENT (pad, parent, no_parent);
5584   GST_OBJECT_UNLOCK (pad);
5585
5586   ret = pre_eventfunc_check (pad, event);
5587   if (G_UNLIKELY (ret != GST_FLOW_OK))
5588     goto precheck_failed;
5589
5590   if (sticky)
5591     gst_event_ref (event);
5592
5593   if (eventfullfunc) {
5594     ret = eventfullfunc (pad, parent, event);
5595   } else if (eventfunc (pad, parent, event)) {
5596     ret = GST_FLOW_OK;
5597   } else {
5598     /* something went wrong */
5599     switch (event_type) {
5600       case GST_EVENT_CAPS:
5601         ret = GST_FLOW_NOT_NEGOTIATED;
5602         break;
5603       default:
5604         ret = GST_FLOW_ERROR;
5605         break;
5606     }
5607   }
5608   RELEASE_PARENT (parent);
5609
5610   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5611
5612   if (sticky) {
5613     if (ret == GST_FLOW_OK) {
5614       GST_OBJECT_LOCK (pad);
5615       /* after the event function accepted the event, we can store the sticky
5616        * event on the pad */
5617       switch (store_sticky_event (pad, event)) {
5618         case GST_FLOW_FLUSHING:
5619           goto flushing;
5620         case GST_FLOW_EOS:
5621           goto eos;
5622         default:
5623           break;
5624       }
5625       GST_OBJECT_UNLOCK (pad);
5626     }
5627     gst_event_unref (event);
5628   }
5629
5630   if (need_unlock)
5631     GST_PAD_STREAM_UNLOCK (pad);
5632
5633   return ret;
5634
5635   /* ERROR handling */
5636 flushing:
5637   {
5638     GST_OBJECT_UNLOCK (pad);
5639     if (need_unlock)
5640       GST_PAD_STREAM_UNLOCK (pad);
5641     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5642         "Received event on flushing pad. Discarding");
5643     gst_event_unref (event);
5644     return GST_FLOW_FLUSHING;
5645   }
5646 inactive:
5647   {
5648     GST_OBJECT_UNLOCK (pad);
5649     if (need_unlock)
5650       GST_PAD_STREAM_UNLOCK (pad);
5651     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5652         "Received flush-stop on inactive pad. Discarding");
5653     gst_event_unref (event);
5654     return GST_FLOW_FLUSHING;
5655   }
5656 eos:
5657   {
5658     GST_OBJECT_UNLOCK (pad);
5659     if (need_unlock)
5660       GST_PAD_STREAM_UNLOCK (pad);
5661     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5662         "Received event on EOS pad. Discarding");
5663     gst_event_unref (event);
5664     return GST_FLOW_EOS;
5665   }
5666 probe_stopped:
5667   {
5668     GST_OBJECT_UNLOCK (pad);
5669     if (need_unlock)
5670       GST_PAD_STREAM_UNLOCK (pad);
5671     /* Only unref if unhandled */
5672     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5673       gst_event_unref (event);
5674
5675     switch (ret) {
5676       case GST_FLOW_CUSTOM_SUCCESS_1:
5677       case GST_FLOW_CUSTOM_SUCCESS:
5678         GST_DEBUG_OBJECT (pad, "dropped or handled event");
5679         ret = GST_FLOW_OK;
5680         break;
5681       default:
5682         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5683         break;
5684     }
5685     return ret;
5686   }
5687 no_function:
5688   {
5689     g_warning ("pad %s:%s has no event handler, file a bug.",
5690         GST_DEBUG_PAD_NAME (pad));
5691     GST_OBJECT_UNLOCK (pad);
5692     if (need_unlock)
5693       GST_PAD_STREAM_UNLOCK (pad);
5694     gst_event_unref (event);
5695     return GST_FLOW_NOT_SUPPORTED;
5696   }
5697 no_parent:
5698   {
5699     GST_DEBUG_OBJECT (pad, "no parent");
5700     GST_OBJECT_UNLOCK (pad);
5701     if (need_unlock)
5702       GST_PAD_STREAM_UNLOCK (pad);
5703     gst_event_unref (event);
5704     return GST_FLOW_FLUSHING;
5705   }
5706 precheck_failed:
5707   {
5708     GST_DEBUG_OBJECT (pad, "pre event check failed");
5709     RELEASE_PARENT (parent);
5710     if (need_unlock)
5711       GST_PAD_STREAM_UNLOCK (pad);
5712     gst_event_unref (event);
5713     return ret;
5714   }
5715 }
5716
5717 /**
5718  * gst_pad_send_event:
5719  * @pad: a #GstPad to send the event to.
5720  * @event: (transfer full): the #GstEvent to send to the pad.
5721  *
5722  * Sends the event to the pad. This function can be used
5723  * by applications to send events in the pipeline.
5724  *
5725  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5726  * sink pad, @event should be a downstream event. For example, you would not
5727  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5728  * Furthermore, some downstream events have to be serialized with data flow,
5729  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5730  * the event needs to be serialized with data flow, this function will take the
5731  * pad's stream lock while calling its event function.
5732  *
5733  * To find out whether an event type is upstream, downstream, or downstream and
5734  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5735  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5736  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5737  * plugin doesn't need to bother itself with this information; the core handles
5738  * all necessary locks and checks.
5739  *
5740  * This function takes ownership of the provided event so you should
5741  * gst_event_ref() it if you want to reuse the event after this call.
5742  *
5743  * Returns: %TRUE if the event was handled.
5744  */
5745 gboolean
5746 gst_pad_send_event (GstPad * pad, GstEvent * event)
5747 {
5748   gboolean result;
5749   GstPadProbeType type;
5750
5751   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5752   g_return_val_if_fail (event != NULL, FALSE);
5753
5754   if (GST_PAD_IS_SINK (pad)) {
5755     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5756       goto wrong_direction;
5757     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5758   } else if (GST_PAD_IS_SRC (pad)) {
5759     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5760       goto wrong_direction;
5761     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5762   } else
5763     goto unknown_direction;
5764
5765   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5766     result = FALSE;
5767   else
5768     result = TRUE;
5769
5770   return result;
5771
5772   /* ERROR handling */
5773 wrong_direction:
5774   {
5775     g_warning ("pad %s:%s sending %s event in wrong direction",
5776         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5777     gst_event_unref (event);
5778     return FALSE;
5779   }
5780 unknown_direction:
5781   {
5782     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5783     gst_event_unref (event);
5784     return FALSE;
5785   }
5786 }
5787
5788 /**
5789  * gst_pad_set_element_private:
5790  * @pad: the #GstPad to set the private data of.
5791  * @priv: The private data to attach to the pad.
5792  *
5793  * Set the given private data gpointer on the pad.
5794  * This function can only be used by the element that owns the pad.
5795  * No locking is performed in this function.
5796  */
5797 void
5798 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5799 {
5800   pad->element_private = priv;
5801 }
5802
5803 /**
5804  * gst_pad_get_element_private:
5805  * @pad: the #GstPad to get the private data of.
5806  *
5807  * Gets the private data of a pad.
5808  * No locking is performed in this function.
5809  *
5810  * Returns: (transfer none): a #gpointer to the private data.
5811  */
5812 gpointer
5813 gst_pad_get_element_private (GstPad * pad)
5814 {
5815   return pad->element_private;
5816 }
5817
5818 /**
5819  * gst_pad_get_sticky_event:
5820  * @pad: the #GstPad to get the event from.
5821  * @event_type: the #GstEventType that should be retrieved.
5822  * @idx: the index of the event
5823  *
5824  * Returns a new reference of the sticky event of type @event_type
5825  * from the event.
5826  *
5827  * Returns: (transfer full) (nullable): a #GstEvent of type
5828  * @event_type or %NULL when no event of @event_type was on
5829  * @pad. Unref after usage.
5830  */
5831 GstEvent *
5832 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5833 {
5834   GstEvent *event = NULL;
5835   PadEvent *ev;
5836
5837   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5838   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5839
5840   GST_OBJECT_LOCK (pad);
5841   ev = find_event_by_type (pad, event_type, idx);
5842   if (ev && (event = ev->event))
5843     gst_event_ref (event);
5844   GST_OBJECT_UNLOCK (pad);
5845
5846   return event;
5847 }
5848
5849 typedef struct
5850 {
5851   GstPadStickyEventsForeachFunction func;
5852   gpointer user_data;
5853 } ForeachDispatch;
5854
5855 static gboolean
5856 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5857 {
5858   ForeachDispatch *data = user_data;
5859   gboolean ret = TRUE;
5860
5861   if (ev->event) {
5862     GST_OBJECT_UNLOCK (pad);
5863
5864     ret = data->func (pad, &ev->event, data->user_data);
5865
5866     GST_OBJECT_LOCK (pad);
5867   }
5868
5869   return ret;
5870 }
5871
5872 /**
5873  * gst_pad_sticky_events_foreach:
5874  * @pad: the #GstPad that should be used for iteration.
5875  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5876  *                should be called for every event.
5877  * @user_data: (closure): the optional user data.
5878  *
5879  * Iterates all sticky events on @pad and calls @foreach_func for every
5880  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5881  */
5882 void
5883 gst_pad_sticky_events_foreach (GstPad * pad,
5884     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5885 {
5886   ForeachDispatch data;
5887
5888   g_return_if_fail (GST_IS_PAD (pad));
5889   g_return_if_fail (foreach_func != NULL);
5890
5891   data.func = foreach_func;
5892   data.user_data = user_data;
5893
5894   GST_OBJECT_LOCK (pad);
5895   events_foreach (pad, foreach_dispatch_function, &data);
5896   GST_OBJECT_UNLOCK (pad);
5897 }
5898
5899 static void
5900 do_stream_status (GstPad * pad, GstStreamStatusType type,
5901     GThread * thread, GstTask * task)
5902 {
5903   GstElement *parent;
5904
5905   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5906
5907   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5908     if (GST_IS_ELEMENT (parent)) {
5909       GstMessage *message;
5910       GValue value = { 0 };
5911
5912       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5913         gchar *tname, *ename, *pname;
5914
5915         /* create a good task name */
5916         ename = gst_element_get_name (parent);
5917         pname = gst_pad_get_name (pad);
5918         tname = g_strdup_printf ("%s:%s", ename, pname);
5919         g_free (ename);
5920         g_free (pname);
5921
5922         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5923         g_free (tname);
5924       }
5925
5926       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5927           type, parent);
5928
5929       g_value_init (&value, GST_TYPE_TASK);
5930       g_value_set_object (&value, task);
5931       gst_message_set_stream_status_object (message, &value);
5932       g_value_unset (&value);
5933
5934       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5935       gst_element_post_message (parent, message);
5936     }
5937     gst_object_unref (parent);
5938   }
5939 }
5940
5941 static void
5942 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5943 {
5944   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5945       thread, task);
5946 }
5947
5948 static void
5949 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5950 {
5951   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5952       thread, task);
5953 }
5954
5955 /**
5956  * gst_pad_start_task:
5957  * @pad: the #GstPad to start the task of
5958  * @func: the task function to call
5959  * @user_data: user data passed to the task function
5960  * @notify: called when @user_data is no longer referenced
5961  *
5962  * Starts a task that repeatedly calls @func with @user_data. This function
5963  * is mostly used in pad activation functions to start the dataflow.
5964  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5965  * before @func is called.
5966  *
5967  * Returns: a %TRUE if the task could be started.
5968  */
5969 gboolean
5970 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5971     GDestroyNotify notify)
5972 {
5973   GstTask *task;
5974   gboolean res;
5975
5976   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5977   g_return_val_if_fail (func != NULL, FALSE);
5978
5979   GST_DEBUG_OBJECT (pad, "start task");
5980
5981   GST_OBJECT_LOCK (pad);
5982   task = GST_PAD_TASK (pad);
5983   if (task == NULL) {
5984     task = gst_task_new (func, user_data, notify);
5985     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5986     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5987     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5988     GST_INFO_OBJECT (pad, "created task %p", task);
5989     GST_PAD_TASK (pad) = task;
5990     gst_object_ref (task);
5991     /* release lock to post the message */
5992     GST_OBJECT_UNLOCK (pad);
5993
5994     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5995
5996     gst_object_unref (task);
5997
5998     GST_OBJECT_LOCK (pad);
5999     /* nobody else is supposed to have changed the pad now */
6000     if (GST_PAD_TASK (pad) != task)
6001       goto concurrent_stop;
6002   }
6003   res = gst_task_set_state (task, GST_TASK_STARTED);
6004   GST_OBJECT_UNLOCK (pad);
6005
6006   return res;
6007
6008   /* ERRORS */
6009 concurrent_stop:
6010   {
6011     GST_OBJECT_UNLOCK (pad);
6012     return TRUE;
6013   }
6014 }
6015
6016 /**
6017  * gst_pad_pause_task:
6018  * @pad: the #GstPad to pause the task of
6019  *
6020  * Pause the task of @pad. This function will also wait until the
6021  * function executed by the task is finished if this function is not
6022  * called from the task function.
6023  *
6024  * Returns: a %TRUE if the task could be paused or %FALSE when the pad
6025  * has no task.
6026  */
6027 gboolean
6028 gst_pad_pause_task (GstPad * pad)
6029 {
6030   GstTask *task;
6031   gboolean res;
6032
6033   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6034
6035   GST_DEBUG_OBJECT (pad, "pause task");
6036
6037   GST_OBJECT_LOCK (pad);
6038   task = GST_PAD_TASK (pad);
6039   if (task == NULL)
6040     goto no_task;
6041   res = gst_task_set_state (task, GST_TASK_PAUSED);
6042   GST_OBJECT_UNLOCK (pad);
6043
6044   /* wait for task function to finish, this lock is recursive so it does nothing
6045    * when the pause is called from the task itself */
6046   GST_PAD_STREAM_LOCK (pad);
6047   GST_PAD_STREAM_UNLOCK (pad);
6048
6049   return res;
6050
6051 no_task:
6052   {
6053     GST_DEBUG_OBJECT (pad, "pad has no task");
6054     GST_OBJECT_UNLOCK (pad);
6055     return FALSE;
6056   }
6057 }
6058
6059 /**
6060  * gst_pad_stop_task:
6061  * @pad: the #GstPad to stop the task of
6062  *
6063  * Stop the task of @pad. This function will also make sure that the
6064  * function executed by the task will effectively stop if not called
6065  * from the GstTaskFunction.
6066  *
6067  * This function will deadlock if called from the GstTaskFunction of
6068  * the task. Use gst_task_pause() instead.
6069  *
6070  * Regardless of whether the pad has a task, the stream lock is acquired and
6071  * released so as to ensure that streaming through this pad has finished.
6072  *
6073  * Returns: a %TRUE if the task could be stopped or %FALSE on error.
6074  */
6075 gboolean
6076 gst_pad_stop_task (GstPad * pad)
6077 {
6078   GstTask *task;
6079   gboolean res;
6080
6081   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6082
6083   GST_DEBUG_OBJECT (pad, "stop task");
6084
6085   GST_OBJECT_LOCK (pad);
6086   task = GST_PAD_TASK (pad);
6087   if (task == NULL)
6088     goto no_task;
6089   GST_PAD_TASK (pad) = NULL;
6090   res = gst_task_set_state (task, GST_TASK_STOPPED);
6091   GST_OBJECT_UNLOCK (pad);
6092
6093   GST_PAD_STREAM_LOCK (pad);
6094   GST_PAD_STREAM_UNLOCK (pad);
6095
6096   if (!gst_task_join (task))
6097     goto join_failed;
6098
6099   gst_object_unref (task);
6100
6101   return res;
6102
6103 no_task:
6104   {
6105     GST_DEBUG_OBJECT (pad, "no task");
6106     GST_OBJECT_UNLOCK (pad);
6107
6108     GST_PAD_STREAM_LOCK (pad);
6109     GST_PAD_STREAM_UNLOCK (pad);
6110
6111     /* this is not an error */
6112     return TRUE;
6113   }
6114 join_failed:
6115   {
6116     /* this is bad, possibly the application tried to join the task from
6117      * the task's thread. We install the task again so that it will be stopped
6118      * again from the right thread next time hopefully. */
6119     GST_OBJECT_LOCK (pad);
6120     GST_DEBUG_OBJECT (pad, "join failed");
6121     /* we can only install this task if there was no other task */
6122     if (GST_PAD_TASK (pad) == NULL)
6123       GST_PAD_TASK (pad) = task;
6124     GST_OBJECT_UNLOCK (pad);
6125
6126     return FALSE;
6127   }
6128 }
6129
6130 /**
6131  * gst_pad_probe_info_get_event:
6132  * @info: a #GstPadProbeInfo
6133  *
6134  * Returns: (transfer none): The #GstEvent from the probe
6135  */
6136
6137 GstEvent *
6138 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6139 {
6140   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6141           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6142
6143   return GST_PAD_PROBE_INFO_EVENT (info);
6144 }
6145
6146
6147 /**
6148  * gst_pad_probe_info_get_query:
6149  * @info: a #GstPadProbeInfo
6150  *
6151  * Returns: (transfer none): The #GstQuery from the probe
6152  */
6153
6154 GstQuery *
6155 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6156 {
6157   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6158           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6159
6160   return GST_PAD_PROBE_INFO_QUERY (info);
6161 }
6162
6163 /**
6164  * gst_pad_probe_info_get_buffer:
6165  * @info: a #GstPadProbeInfo
6166  *
6167  * Returns: (transfer none): The #GstBuffer from the probe
6168  */
6169
6170 GstBuffer *
6171 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6172 {
6173   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6174
6175   return GST_PAD_PROBE_INFO_BUFFER (info);
6176 }
6177
6178 /**
6179  * gst_pad_probe_info_get_buffer_list:
6180  * @info: a #GstPadProbeInfo
6181  *
6182  * Returns: (transfer none): The #GstBufferList from the probe
6183  */
6184
6185 GstBufferList *
6186 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6187 {
6188   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6189
6190   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6191 }
6192
6193 /**
6194  * gst_pad_get_last_flow_return:
6195  * @pad: the #GstPad
6196  *
6197  * Gets the #GstFlowReturn return from the last data passed by this pad.
6198  *
6199  * Since: 1.4
6200  */
6201 GstFlowReturn
6202 gst_pad_get_last_flow_return (GstPad * pad)
6203 {
6204   GstFlowReturn ret;
6205
6206   GST_OBJECT_LOCK (pad);
6207   ret = GST_PAD_LAST_FLOW_RETURN (pad);
6208   GST_OBJECT_UNLOCK (pad);
6209
6210   return ret;
6211 }