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