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