docs: fix some warnings and add some since markers
[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  * Since: 1.8
1858  */
1859 void
1860 gst_pad_set_event_full_function_full (GstPad * pad,
1861     GstPadEventFullFunction event, gpointer user_data, GDestroyNotify notify)
1862 {
1863   g_return_if_fail (GST_IS_PAD (pad));
1864
1865   if (pad->eventnotify)
1866     pad->eventnotify (pad->eventdata);
1867   GST_PAD_EVENTFULLFUNC (pad) = event;
1868   GST_PAD_EVENTFUNC (pad) = event_wrap;
1869   pad->eventdata = user_data;
1870   pad->eventnotify = notify;
1871
1872   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfullfunc for set to %s",
1873       GST_DEBUG_FUNCPTR_NAME (event));
1874 }
1875
1876 /**
1877  * gst_pad_set_query_function:
1878  * @p: a #GstPad of either direction.
1879  * @f: the #GstPadQueryFunction to set.
1880  *
1881  * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1882  * notify.
1883  */
1884 /**
1885  * gst_pad_set_query_function_full:
1886  * @pad: a #GstPad of either direction.
1887  * @query: the #GstPadQueryFunction to set.
1888  * @user_data: user_data passed to @notify
1889  * @notify: notify called when @query will not be used anymore.
1890  *
1891  * Set the given query function for the pad.
1892  */
1893 void
1894 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1895     gpointer user_data, GDestroyNotify notify)
1896 {
1897   g_return_if_fail (GST_IS_PAD (pad));
1898
1899   if (pad->querynotify)
1900     pad->querynotify (pad->querydata);
1901   GST_PAD_QUERYFUNC (pad) = query;
1902   pad->querydata = user_data;
1903   pad->querynotify = notify;
1904
1905   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1906       GST_DEBUG_FUNCPTR_NAME (query));
1907 }
1908
1909 /**
1910  * gst_pad_set_iterate_internal_links_function:
1911  * @p: a #GstPad of either direction.
1912  * @f: the #GstPadIterIntLinkFunction to set.
1913  *
1914  * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1915  * for the user_data and notify.
1916  */
1917 /**
1918  * gst_pad_set_iterate_internal_links_function_full:
1919  * @pad: a #GstPad of either direction.
1920  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1921  * @user_data: user_data passed to @notify
1922  * @notify: notify called when @iterintlink will not be used anymore.
1923  *
1924  * Sets the given internal link iterator function for the pad.
1925  */
1926 void
1927 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1928     GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1929     GDestroyNotify notify)
1930 {
1931   g_return_if_fail (GST_IS_PAD (pad));
1932
1933   if (pad->iterintlinknotify)
1934     pad->iterintlinknotify (pad->iterintlinkdata);
1935   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1936   pad->iterintlinkdata = user_data;
1937   pad->iterintlinknotify = notify;
1938
1939   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1940       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1941 }
1942
1943 /**
1944  * gst_pad_set_link_function:
1945  * @p: a #GstPad.
1946  * @f: the #GstPadLinkFunction to set.
1947  *
1948  * Calls gst_pad_set_link_function_full() with %NULL
1949  * for the user_data and notify.
1950  */
1951 /**
1952  * gst_pad_set_link_function_full:
1953  * @pad: a #GstPad.
1954  * @link: the #GstPadLinkFunction to set.
1955  * @user_data: user_data passed to @notify
1956  * @notify: notify called when @link will not be used anymore.
1957  *
1958  * Sets the given link function for the pad. It will be called when
1959  * the pad is linked with another pad.
1960  *
1961  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1962  * made.
1963  *
1964  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1965  * cannot be made for some reason.
1966  *
1967  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1968  * of the peer sink pad, if present.
1969  */
1970 void
1971 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1972     gpointer user_data, GDestroyNotify notify)
1973 {
1974   g_return_if_fail (GST_IS_PAD (pad));
1975
1976   if (pad->linknotify)
1977     pad->linknotify (pad->linkdata);
1978   GST_PAD_LINKFUNC (pad) = link;
1979   pad->linkdata = user_data;
1980   pad->linknotify = notify;
1981
1982   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1983       GST_DEBUG_FUNCPTR_NAME (link));
1984 }
1985
1986 /**
1987  * gst_pad_set_unlink_function:
1988  * @p: a #GstPad.
1989  * @f: the #GstPadUnlinkFunction to set.
1990  *
1991  * Calls gst_pad_set_unlink_function_full() with %NULL
1992  * for the user_data and notify.
1993  */
1994 /**
1995  * gst_pad_set_unlink_function_full:
1996  * @pad: a #GstPad.
1997  * @unlink: the #GstPadUnlinkFunction to set.
1998  * @user_data: user_data passed to @notify
1999  * @notify: notify called when @unlink will not be used anymore.
2000  *
2001  * Sets the given unlink function for the pad. It will be called
2002  * when the pad is unlinked.
2003  */
2004 void
2005 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
2006     gpointer user_data, GDestroyNotify notify)
2007 {
2008   g_return_if_fail (GST_IS_PAD (pad));
2009
2010   if (pad->unlinknotify)
2011     pad->unlinknotify (pad->unlinkdata);
2012   GST_PAD_UNLINKFUNC (pad) = unlink;
2013   pad->unlinkdata = user_data;
2014   pad->unlinknotify = notify;
2015
2016   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
2017       GST_DEBUG_FUNCPTR_NAME (unlink));
2018 }
2019
2020 /**
2021  * gst_pad_unlink:
2022  * @srcpad: the source #GstPad to unlink.
2023  * @sinkpad: the sink #GstPad to unlink.
2024  *
2025  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
2026  * signal on both pads.
2027  *
2028  * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
2029  * the pads were not linked together.
2030  *
2031  * MT safe.
2032  */
2033 gboolean
2034 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
2035 {
2036   gboolean result = FALSE;
2037   GstElement *parent = NULL;
2038
2039   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2040   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
2041   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2042   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
2043
2044   GST_TRACER_PAD_UNLINK_PRE (srcpad, sinkpad);
2045
2046   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
2047       GST_DEBUG_PAD_NAME (srcpad), srcpad,
2048       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
2049
2050   /* We need to notify the parent before taking any pad locks as the bin in
2051    * question might be waiting for a lock on the pad while holding its lock
2052    * that our message will try to take. */
2053   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2054     if (GST_IS_ELEMENT (parent)) {
2055       gst_element_post_message (parent,
2056           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2057               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
2058     } else {
2059       gst_object_unref (parent);
2060       parent = NULL;
2061     }
2062   }
2063
2064   GST_OBJECT_LOCK (srcpad);
2065   GST_OBJECT_LOCK (sinkpad);
2066
2067   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2068     goto not_linked_together;
2069
2070   if (GST_PAD_UNLINKFUNC (srcpad)) {
2071     GstObject *tmpparent;
2072
2073     ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2074
2075     GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2076     RELEASE_PARENT (tmpparent);
2077   }
2078 no_src_parent:
2079   if (GST_PAD_UNLINKFUNC (sinkpad)) {
2080     GstObject *tmpparent;
2081
2082     ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2083
2084     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2085     RELEASE_PARENT (tmpparent);
2086   }
2087 no_sink_parent:
2088
2089   /* first clear peers */
2090   GST_PAD_PEER (srcpad) = NULL;
2091   GST_PAD_PEER (sinkpad) = NULL;
2092
2093   GST_OBJECT_UNLOCK (sinkpad);
2094   GST_OBJECT_UNLOCK (srcpad);
2095
2096   /* fire off a signal to each of the pads telling them
2097    * that they've been unlinked */
2098   g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2099   g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2100
2101   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2102       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2103
2104   result = TRUE;
2105
2106 done:
2107   if (parent != NULL) {
2108     gst_element_post_message (parent,
2109         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2110             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2111     gst_object_unref (parent);
2112   }
2113   GST_TRACER_PAD_UNLINK_POST (srcpad, sinkpad, result);
2114   return result;
2115
2116   /* ERRORS */
2117 not_linked_together:
2118   {
2119     /* we do not emit a warning in this case because unlinking cannot
2120      * be made MT safe.*/
2121     GST_OBJECT_UNLOCK (sinkpad);
2122     GST_OBJECT_UNLOCK (srcpad);
2123     goto done;
2124   }
2125 }
2126
2127 /**
2128  * gst_pad_is_linked:
2129  * @pad: pad to check
2130  *
2131  * Checks if a @pad is linked to another pad or not.
2132  *
2133  * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2134  *
2135  * MT safe.
2136  */
2137 gboolean
2138 gst_pad_is_linked (GstPad * pad)
2139 {
2140   gboolean result;
2141
2142   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2143
2144   GST_OBJECT_LOCK (pad);
2145   result = (GST_PAD_PEER (pad) != NULL);
2146   GST_OBJECT_UNLOCK (pad);
2147
2148   return result;
2149 }
2150
2151 /* get the caps from both pads and see if the intersection
2152  * is not empty.
2153  *
2154  * This function should be called with the pad LOCK on both
2155  * pads
2156  */
2157 static gboolean
2158 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2159     GstPadLinkCheck flags)
2160 {
2161   GstCaps *srccaps = NULL;
2162   GstCaps *sinkcaps = NULL;
2163   gboolean compatible = FALSE;
2164
2165   if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2166     return TRUE;
2167
2168   /* Doing the expensive caps checking takes priority over only checking the template caps */
2169   if (flags & GST_PAD_LINK_CHECK_CAPS) {
2170     GST_OBJECT_UNLOCK (sink);
2171     GST_OBJECT_UNLOCK (src);
2172
2173     srccaps = gst_pad_query_caps (src, NULL);
2174     sinkcaps = gst_pad_query_caps (sink, NULL);
2175
2176     GST_OBJECT_LOCK (src);
2177     GST_OBJECT_LOCK (sink);
2178   } else {
2179     /* If one of the two pads doesn't have a template, consider the intersection
2180      * as valid.*/
2181     if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2182             || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2183       compatible = TRUE;
2184       goto done;
2185     }
2186     srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2187     sinkcaps =
2188         gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2189   }
2190
2191   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2192       srccaps);
2193   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2194       sinkcaps);
2195
2196   /* if we have caps on both pads we can check the intersection. If one
2197    * of the caps is %NULL, we return %TRUE. */
2198   if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2199     if (srccaps)
2200       gst_caps_unref (srccaps);
2201     if (sinkcaps)
2202       gst_caps_unref (sinkcaps);
2203     goto done;
2204   }
2205
2206   compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2207   gst_caps_unref (srccaps);
2208   gst_caps_unref (sinkcaps);
2209
2210 done:
2211   GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2212       (compatible ? "" : "not "));
2213
2214   return compatible;
2215 }
2216
2217 /* check if the grandparents of both pads are the same.
2218  * This check is required so that we don't try to link
2219  * pads from elements in different bins without ghostpads.
2220  *
2221  * The LOCK should be held on both pads
2222  */
2223 static gboolean
2224 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2225 {
2226   GstObject *psrc, *psink;
2227
2228   psrc = GST_OBJECT_PARENT (src);
2229   psink = GST_OBJECT_PARENT (sink);
2230
2231   /* if one of the pads has no parent, we allow the link */
2232   if (G_UNLIKELY (psrc == NULL || psink == NULL))
2233     goto no_parent;
2234
2235   /* only care about parents that are elements */
2236   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2237     goto no_element_parent;
2238
2239   /* if the parents are the same, we have a loop */
2240   if (G_UNLIKELY (psrc == psink))
2241     goto same_parents;
2242
2243   /* if they both have a parent, we check the grandparents. We can not lock
2244    * the parent because we hold on the child (pad) and the locking order is
2245    * parent >> child. */
2246   psrc = GST_OBJECT_PARENT (psrc);
2247   psink = GST_OBJECT_PARENT (psink);
2248
2249   /* if they have grandparents but they are not the same */
2250   if (G_UNLIKELY (psrc != psink))
2251     goto wrong_grandparents;
2252
2253   return TRUE;
2254
2255   /* ERRORS */
2256 no_parent:
2257   {
2258     GST_CAT_DEBUG (GST_CAT_CAPS,
2259         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2260         GST_PTR_FORMAT, psrc, psink);
2261     return TRUE;
2262   }
2263 no_element_parent:
2264   {
2265     GST_CAT_DEBUG (GST_CAT_CAPS,
2266         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2267         GST_PTR_FORMAT, psrc, psink);
2268     return TRUE;
2269   }
2270 same_parents:
2271   {
2272     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2273         psrc);
2274     return FALSE;
2275   }
2276 wrong_grandparents:
2277   {
2278     GST_CAT_DEBUG (GST_CAT_CAPS,
2279         "pads have different grandparents %" GST_PTR_FORMAT " and %"
2280         GST_PTR_FORMAT, psrc, psink);
2281     return FALSE;
2282   }
2283 }
2284
2285 /* FIXME leftover from an attempt at refactoring... */
2286 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2287  * the two pads will be locked in the srcpad, sinkpad order. */
2288 static GstPadLinkReturn
2289 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2290 {
2291   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2292       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2293
2294   GST_OBJECT_LOCK (srcpad);
2295
2296   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2297     goto src_was_linked;
2298
2299   GST_OBJECT_LOCK (sinkpad);
2300
2301   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2302     goto sink_was_linked;
2303
2304   /* check hierarchy, pads can only be linked if the grandparents
2305    * are the same. */
2306   if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2307       && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2308     goto wrong_hierarchy;
2309
2310   /* check pad caps for non-empty intersection */
2311   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2312     goto no_format;
2313
2314   /* FIXME check pad scheduling for non-empty intersection */
2315
2316   return GST_PAD_LINK_OK;
2317
2318 src_was_linked:
2319   {
2320     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2321         GST_DEBUG_PAD_NAME (srcpad),
2322         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2323     /* we do not emit a warning in this case because unlinking cannot
2324      * be made MT safe.*/
2325     GST_OBJECT_UNLOCK (srcpad);
2326     return GST_PAD_LINK_WAS_LINKED;
2327   }
2328 sink_was_linked:
2329   {
2330     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2331         GST_DEBUG_PAD_NAME (sinkpad),
2332         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2333     /* we do not emit a warning in this case because unlinking cannot
2334      * be made MT safe.*/
2335     GST_OBJECT_UNLOCK (sinkpad);
2336     GST_OBJECT_UNLOCK (srcpad);
2337     return GST_PAD_LINK_WAS_LINKED;
2338   }
2339 wrong_hierarchy:
2340   {
2341     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2342     GST_OBJECT_UNLOCK (sinkpad);
2343     GST_OBJECT_UNLOCK (srcpad);
2344     return GST_PAD_LINK_WRONG_HIERARCHY;
2345   }
2346 no_format:
2347   {
2348     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2349     GST_OBJECT_UNLOCK (sinkpad);
2350     GST_OBJECT_UNLOCK (srcpad);
2351     return GST_PAD_LINK_NOFORMAT;
2352   }
2353 }
2354
2355 /**
2356  * gst_pad_can_link:
2357  * @srcpad: the source #GstPad.
2358  * @sinkpad: the sink #GstPad.
2359  *
2360  * Checks if the source pad and the sink pad are compatible so they can be
2361  * linked.
2362  *
2363  * Returns: %TRUE if the pads can be linked.
2364  */
2365 gboolean
2366 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2367 {
2368   GstPadLinkReturn result;
2369
2370   /* generic checks */
2371   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2372   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2373
2374   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2375       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2376
2377   /* gst_pad_link_prepare does everything for us, we only release the locks
2378    * on the pads that it gets us. If this function returns !OK the locks are not
2379    * taken anymore. */
2380   result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2381   if (result != GST_PAD_LINK_OK)
2382     goto done;
2383
2384   GST_OBJECT_UNLOCK (srcpad);
2385   GST_OBJECT_UNLOCK (sinkpad);
2386
2387 done:
2388   return result == GST_PAD_LINK_OK;
2389 }
2390
2391 /**
2392  * gst_pad_link_full:
2393  * @srcpad: the source #GstPad to link.
2394  * @sinkpad: the sink #GstPad to link.
2395  * @flags: the checks to validate when linking
2396  *
2397  * Links the source pad and the sink pad.
2398  *
2399  * This variant of #gst_pad_link provides a more granular control on the
2400  * checks being done when linking. While providing some considerable speedups
2401  * the caller of this method must be aware that wrong usage of those flags
2402  * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2403  * for more information.
2404  *
2405  * MT Safe.
2406  *
2407  * Returns: A result code indicating if the connection worked or
2408  *          what went wrong.
2409  */
2410 GstPadLinkReturn
2411 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2412 {
2413   GstPadLinkReturn result;
2414   GstElement *parent;
2415   GstPadLinkFunction srcfunc, sinkfunc;
2416
2417   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2418   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2419   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2420   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2421       GST_PAD_LINK_WRONG_DIRECTION);
2422
2423   /* Notify the parent early. See gst_pad_unlink for details. */
2424   if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2425     if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2426       gst_element_post_message (parent,
2427           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2428               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2429     } else {
2430       gst_object_unref (parent);
2431       parent = NULL;
2432     }
2433   }
2434
2435   /* prepare will also lock the two pads */
2436   result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2437
2438   if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2439     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2440         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2441         gst_pad_link_get_name (result));
2442     goto done;
2443   }
2444
2445   /* must set peers before calling the link function */
2446   GST_PAD_PEER (srcpad) = sinkpad;
2447   GST_PAD_PEER (sinkpad) = srcpad;
2448
2449   /* check events, when something is different, mark pending */
2450   schedule_events (srcpad, sinkpad);
2451
2452   /* get the link functions */
2453   srcfunc = GST_PAD_LINKFUNC (srcpad);
2454   sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2455
2456   if (G_UNLIKELY (srcfunc || sinkfunc)) {
2457     /* custom link functions, execute them */
2458     GST_OBJECT_UNLOCK (sinkpad);
2459     GST_OBJECT_UNLOCK (srcpad);
2460
2461     if (srcfunc) {
2462       GstObject *tmpparent;
2463
2464       ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2465       /* this one will call the peer link function */
2466       result = srcfunc (srcpad, tmpparent, sinkpad);
2467       RELEASE_PARENT (tmpparent);
2468     } else if (sinkfunc) {
2469       GstObject *tmpparent;
2470
2471       ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2472       /* if no source link function, we need to call the sink link
2473        * function ourselves. */
2474       result = sinkfunc (sinkpad, tmpparent, srcpad);
2475       RELEASE_PARENT (tmpparent);
2476     }
2477   no_parent:
2478
2479     GST_OBJECT_LOCK (srcpad);
2480     GST_OBJECT_LOCK (sinkpad);
2481
2482     /* we released the lock, check if the same pads are linked still */
2483     if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2484       goto concurrent_link;
2485
2486     if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2487       goto link_failed;
2488   }
2489   GST_OBJECT_UNLOCK (sinkpad);
2490   GST_OBJECT_UNLOCK (srcpad);
2491
2492   /* fire off a signal to each of the pads telling them
2493    * that they've been linked */
2494   g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2495   g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2496
2497   GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2498       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2499
2500   gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2501
2502 done:
2503   if (G_LIKELY (parent)) {
2504     gst_element_post_message (parent,
2505         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2506             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2507     gst_object_unref (parent);
2508   }
2509
2510   return result;
2511
2512   /* ERRORS */
2513 concurrent_link:
2514   {
2515     GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2516         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2517     GST_OBJECT_UNLOCK (sinkpad);
2518     GST_OBJECT_UNLOCK (srcpad);
2519
2520     /* The other link operation succeeded first */
2521     result = GST_PAD_LINK_WAS_LINKED;
2522     goto done;
2523   }
2524 link_failed:
2525   {
2526     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2527         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2528         gst_pad_link_get_name (result));
2529
2530     GST_PAD_PEER (srcpad) = NULL;
2531     GST_PAD_PEER (sinkpad) = NULL;
2532
2533     GST_OBJECT_UNLOCK (sinkpad);
2534     GST_OBJECT_UNLOCK (srcpad);
2535
2536     goto done;
2537   }
2538 }
2539
2540 /**
2541  * gst_pad_link:
2542  * @srcpad: the source #GstPad to link.
2543  * @sinkpad: the sink #GstPad to link.
2544  *
2545  * Links the source pad and the sink pad.
2546  *
2547  * Returns: A result code indicating if the connection worked or
2548  *          what went wrong.
2549  *
2550  * MT Safe.
2551  */
2552 GstPadLinkReturn
2553 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2554 {
2555   GstPadLinkReturn ret;
2556
2557   GST_TRACER_PAD_LINK_PRE (srcpad, sinkpad);
2558   ret = gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2559   GST_TRACER_PAD_LINK_POST (srcpad, sinkpad, ret);
2560
2561   return ret;
2562 }
2563
2564 static void
2565 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2566 {
2567   GstPadTemplate **template_p;
2568
2569   /* this function would need checks if it weren't static */
2570
2571   GST_OBJECT_LOCK (pad);
2572   template_p = &pad->padtemplate;
2573   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2574   GST_OBJECT_UNLOCK (pad);
2575
2576   if (templ)
2577     gst_pad_template_pad_created (templ, pad);
2578 }
2579
2580 /**
2581  * gst_pad_get_pad_template:
2582  * @pad: a #GstPad.
2583  *
2584  * Gets the template for @pad.
2585  *
2586  * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2587  *     this pad was instantiated, or %NULL if this pad has no
2588  *     template. Unref after usage.
2589  */
2590 GstPadTemplate *
2591 gst_pad_get_pad_template (GstPad * pad)
2592 {
2593   GstPadTemplate *templ;
2594
2595   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2596
2597   templ = GST_PAD_PAD_TEMPLATE (pad);
2598
2599   return (templ ? gst_object_ref (templ) : NULL);
2600 }
2601
2602 /**
2603  * gst_pad_has_current_caps:
2604  * @pad: a  #GstPad to check
2605  *
2606  * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2607  *
2608  * Returns: %TRUE when @pad has caps associated with it.
2609  */
2610 gboolean
2611 gst_pad_has_current_caps (GstPad * pad)
2612 {
2613   gboolean result;
2614   GstCaps *caps;
2615
2616   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2617
2618   GST_OBJECT_LOCK (pad);
2619   caps = get_pad_caps (pad);
2620   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2621       "check current pad caps %" GST_PTR_FORMAT, caps);
2622   result = (caps != NULL);
2623   GST_OBJECT_UNLOCK (pad);
2624
2625   return result;
2626 }
2627
2628 /**
2629  * gst_pad_get_current_caps:
2630  * @pad: a  #GstPad to get the current capabilities of.
2631  *
2632  * Gets the capabilities currently configured on @pad with the last
2633  * #GST_EVENT_CAPS event.
2634  *
2635  * Returns: the current caps of the pad with incremented ref-count.
2636  */
2637 GstCaps *
2638 gst_pad_get_current_caps (GstPad * pad)
2639 {
2640   GstCaps *result;
2641
2642   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2643
2644   GST_OBJECT_LOCK (pad);
2645   if ((result = get_pad_caps (pad)))
2646     gst_caps_ref (result);
2647   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2648       "get current pad caps %" GST_PTR_FORMAT, result);
2649   GST_OBJECT_UNLOCK (pad);
2650
2651   return result;
2652 }
2653
2654 /**
2655  * gst_pad_get_pad_template_caps:
2656  * @pad: a #GstPad to get the template capabilities from.
2657  *
2658  * Gets the capabilities for @pad's template.
2659  *
2660  * Returns: (transfer full): the #GstCaps of this pad template.
2661  * Unref after usage.
2662  */
2663 GstCaps *
2664 gst_pad_get_pad_template_caps (GstPad * pad)
2665 {
2666   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2667
2668   if (GST_PAD_PAD_TEMPLATE (pad))
2669     return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2670
2671   return gst_caps_ref (GST_CAPS_ANY);
2672 }
2673
2674 /**
2675  * gst_pad_get_peer:
2676  * @pad: a #GstPad to get the peer of.
2677  *
2678  * Gets the peer of @pad. This function refs the peer pad so
2679  * you need to unref it after use.
2680  *
2681  * Returns: (transfer full): the peer #GstPad. Unref after usage.
2682  *
2683  * MT safe.
2684  */
2685 GstPad *
2686 gst_pad_get_peer (GstPad * pad)
2687 {
2688   GstPad *result;
2689
2690   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2691
2692   GST_OBJECT_LOCK (pad);
2693   result = GST_PAD_PEER (pad);
2694   if (result)
2695     gst_object_ref (result);
2696   GST_OBJECT_UNLOCK (pad);
2697
2698   return result;
2699 }
2700
2701 /**
2702  * gst_pad_get_allowed_caps:
2703  * @pad: a #GstPad.
2704  *
2705  * Gets the capabilities of the allowed media types that can flow through
2706  * @pad and its peer.
2707  *
2708  * The allowed capabilities is calculated as the intersection of the results of
2709  * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2710  * on the resulting caps.
2711  *
2712  * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2713  *     pad link. Unref the caps when you no longer need it. This
2714  *     function returns %NULL when @pad has no peer.
2715  *
2716  * MT safe.
2717  */
2718 GstCaps *
2719 gst_pad_get_allowed_caps (GstPad * pad)
2720 {
2721   GstCaps *mycaps;
2722   GstCaps *caps = NULL;
2723   GstQuery *query;
2724
2725   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2726
2727   GST_OBJECT_LOCK (pad);
2728   if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2729     goto no_peer;
2730   GST_OBJECT_UNLOCK (pad);
2731
2732   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2733
2734   mycaps = gst_pad_query_caps (pad, NULL);
2735
2736   /* Query peer caps */
2737   query = gst_query_new_caps (mycaps);
2738   gst_pad_peer_query (pad, query);
2739   gst_query_parse_caps_result (query, &caps);
2740   gst_caps_ref (caps);
2741   gst_query_unref (query);
2742
2743   gst_caps_unref (mycaps);
2744
2745   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2746       caps);
2747
2748   return caps;
2749
2750 no_peer:
2751   {
2752     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2753     GST_OBJECT_UNLOCK (pad);
2754
2755     return NULL;
2756   }
2757 }
2758
2759 /**
2760  * gst_pad_iterate_internal_links_default:
2761  * @pad: the #GstPad to get the internal links of.
2762  * @parent: (allow-none): the parent of @pad or %NULL
2763  *
2764  * Iterate the list of pads to which the given pad is linked to inside of
2765  * the parent element.
2766  * This is the default handler, and thus returns an iterator of all of the
2767  * pads inside the parent element with opposite direction.
2768  *
2769  * The caller must free this iterator after use with gst_iterator_free().
2770  *
2771  * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2772  * has no parent. Unref each returned pad with gst_object_unref().
2773  */
2774 GstIterator *
2775 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2776 {
2777   GstIterator *res;
2778   GList **padlist;
2779   guint32 *cookie;
2780   GMutex *lock;
2781   gpointer owner;
2782   GstElement *eparent;
2783
2784   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2785
2786   if (parent != NULL && GST_IS_ELEMENT (parent)) {
2787     eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2788   } else {
2789     GST_OBJECT_LOCK (pad);
2790     eparent = GST_PAD_PARENT (pad);
2791     if (!eparent || !GST_IS_ELEMENT (eparent))
2792       goto no_parent;
2793
2794     gst_object_ref (eparent);
2795     GST_OBJECT_UNLOCK (pad);
2796   }
2797
2798   if (pad->direction == GST_PAD_SRC)
2799     padlist = &eparent->sinkpads;
2800   else
2801     padlist = &eparent->srcpads;
2802
2803   GST_DEBUG_OBJECT (pad, "Making iterator");
2804
2805   cookie = &eparent->pads_cookie;
2806   owner = eparent;
2807   lock = GST_OBJECT_GET_LOCK (eparent);
2808
2809   res = gst_iterator_new_list (GST_TYPE_PAD,
2810       lock, cookie, padlist, (GObject *) owner, NULL);
2811
2812   gst_object_unref (owner);
2813
2814   return res;
2815
2816   /* ERRORS */
2817 no_parent:
2818   {
2819     GST_OBJECT_UNLOCK (pad);
2820     GST_DEBUG_OBJECT (pad, "no parent element");
2821     return NULL;
2822   }
2823 }
2824
2825 /**
2826  * gst_pad_iterate_internal_links:
2827  * @pad: the GstPad to get the internal links of.
2828  *
2829  * Gets an iterator for the pads to which the given pad is linked to inside
2830  * of the parent element.
2831  *
2832  * Each #GstPad element yielded by the iterator will have its refcount increased,
2833  * so unref after use.
2834  *
2835  * Free-function: gst_iterator_free
2836  *
2837  * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2838  *     or %NULL when the pad does not have an iterator function
2839  *     configured. Use gst_iterator_free() after usage.
2840  */
2841 GstIterator *
2842 gst_pad_iterate_internal_links (GstPad * pad)
2843 {
2844   GstIterator *res = NULL;
2845   GstObject *parent;
2846
2847   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2848
2849   GST_OBJECT_LOCK (pad);
2850   ACQUIRE_PARENT (pad, parent, no_parent);
2851   GST_OBJECT_UNLOCK (pad);
2852
2853   if (GST_PAD_ITERINTLINKFUNC (pad))
2854     res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2855
2856   RELEASE_PARENT (parent);
2857
2858   return res;
2859
2860   /* ERRORS */
2861 no_parent:
2862   {
2863     GST_DEBUG_OBJECT (pad, "no parent");
2864     GST_OBJECT_UNLOCK (pad);
2865     return NULL;
2866   }
2867 }
2868
2869 /**
2870  * gst_pad_forward:
2871  * @pad: a #GstPad
2872  * @forward: (scope call): a #GstPadForwardFunction
2873  * @user_data: user data passed to @forward
2874  *
2875  * Calls @forward for all internally linked pads of @pad. This function deals with
2876  * dynamically changing internal pads and will make sure that the @forward
2877  * function is only called once for each pad.
2878  *
2879  * When @forward returns %TRUE, no further pads will be processed.
2880  *
2881  * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2882  */
2883 gboolean
2884 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2885     gpointer user_data)
2886 {
2887   gboolean result = FALSE;
2888   GstIterator *iter;
2889   gboolean done = FALSE;
2890   GValue item = { 0, };
2891   GList *pushed_pads = NULL;
2892
2893   iter = gst_pad_iterate_internal_links (pad);
2894
2895   if (!iter)
2896     goto no_iter;
2897
2898   while (!done) {
2899     switch (gst_iterator_next (iter, &item)) {
2900       case GST_ITERATOR_OK:
2901       {
2902         GstPad *intpad;
2903
2904         intpad = g_value_get_object (&item);
2905
2906         /* if already pushed, skip. FIXME, find something faster to tag pads */
2907         if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2908           g_value_reset (&item);
2909           break;
2910         }
2911
2912         GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2913             GST_DEBUG_PAD_NAME (intpad));
2914         done = result = forward (intpad, user_data);
2915
2916         pushed_pads = g_list_prepend (pushed_pads, intpad);
2917
2918         g_value_reset (&item);
2919         break;
2920       }
2921       case GST_ITERATOR_RESYNC:
2922         /* We don't reset the result here because we don't push the event
2923          * again on pads that got the event already and because we need
2924          * to consider the result of the previous pushes */
2925         gst_iterator_resync (iter);
2926         break;
2927       case GST_ITERATOR_ERROR:
2928         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2929         done = TRUE;
2930         break;
2931       case GST_ITERATOR_DONE:
2932         done = TRUE;
2933         break;
2934     }
2935   }
2936   g_value_unset (&item);
2937   gst_iterator_free (iter);
2938
2939   g_list_free (pushed_pads);
2940
2941 no_iter:
2942   return result;
2943 }
2944
2945 typedef struct
2946 {
2947   GstEvent *event;
2948   gboolean result;
2949   gboolean dispatched;
2950 } EventData;
2951
2952 static gboolean
2953 event_forward_func (GstPad * pad, EventData * data)
2954 {
2955   /* for each pad we send to, we should ref the event; it's up
2956    * to downstream to unref again when handled. */
2957   GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2958       data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2959
2960   data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2961
2962   data->dispatched = TRUE;
2963
2964   /* don't stop */
2965   return FALSE;
2966 }
2967
2968 /**
2969  * gst_pad_event_default:
2970  * @pad: a #GstPad to call the default event handler on.
2971  * @parent: (allow-none): the parent of @pad or %NULL
2972  * @event: (transfer full): the #GstEvent to handle.
2973  *
2974  * Invokes the default event handler for the given pad.
2975  *
2976  * The EOS event will pause the task associated with @pad before it is forwarded
2977  * to all internally linked pads,
2978  *
2979  * The event is sent to all pads internally linked to @pad. This function
2980  * takes ownership of @event.
2981  *
2982  * Returns: %TRUE if the event was sent successfully.
2983  */
2984 gboolean
2985 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2986 {
2987   gboolean result, forward = TRUE;
2988
2989   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2990   g_return_val_if_fail (event != NULL, FALSE);
2991
2992   GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2993       event);
2994
2995   switch (GST_EVENT_TYPE (event)) {
2996     case GST_EVENT_CAPS:
2997       forward = GST_PAD_IS_PROXY_CAPS (pad);
2998       result = TRUE;
2999       break;
3000     default:
3001       break;
3002   }
3003
3004   if (forward) {
3005     EventData data;
3006
3007     data.event = event;
3008     data.dispatched = FALSE;
3009     data.result = FALSE;
3010
3011     gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3012
3013     /* for sinkpads without a parent element or without internal links, nothing
3014      * will be dispatched but we still want to return TRUE. */
3015     if (data.dispatched)
3016       result = data.result;
3017     else
3018       result = TRUE;
3019   }
3020
3021   gst_event_unref (event);
3022
3023   return result;
3024 }
3025
3026 /* Default accept caps implementation just checks against
3027  * the allowed caps for the pad */
3028 static gboolean
3029 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
3030 {
3031   /* get the caps and see if it intersects to something not empty */
3032   GstCaps *caps, *allowed = NULL;
3033   gboolean result;
3034
3035   GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
3036
3037   /* first forward the query to internally linked pads when we are dealing with
3038    * a PROXY CAPS */
3039   if (GST_PAD_IS_PROXY_CAPS (pad)) {
3040     result = gst_pad_proxy_query_accept_caps (pad, query);
3041     if (result)
3042       allowed = gst_pad_get_pad_template_caps (pad);
3043     else
3044       goto done;
3045   }
3046
3047   gst_query_parse_accept_caps (query, &caps);
3048   if (!allowed) {
3049     GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
3050         "fallback ACCEPT_CAPS query, consider implementing a specialized version");
3051     if (GST_PAD_IS_ACCEPT_TEMPLATE (pad))
3052       allowed = gst_pad_get_pad_template_caps (pad);
3053     else
3054       allowed = gst_pad_query_caps (pad, caps);
3055   }
3056
3057   if (allowed) {
3058     if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
3059       GST_DEBUG_OBJECT (pad,
3060           "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
3061           allowed, caps);
3062       result = gst_caps_can_intersect (caps, allowed);
3063     } else {
3064       GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
3065           GST_PTR_FORMAT, allowed, caps);
3066       result = gst_caps_is_subset (caps, allowed);
3067     }
3068     gst_caps_unref (allowed);
3069   } else {
3070     GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
3071     result = FALSE;
3072   }
3073   gst_query_set_accept_caps_result (query, result);
3074
3075 done:
3076   return TRUE;
3077 }
3078
3079 /* Default caps implementation */
3080 static gboolean
3081 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3082 {
3083   GstCaps *result = NULL, *filter;
3084   GstPadTemplate *templ;
3085   gboolean fixed_caps;
3086
3087   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3088       query);
3089
3090   /* first try to proxy if we must */
3091   if (GST_PAD_IS_PROXY_CAPS (pad)) {
3092     if ((gst_pad_proxy_query_caps (pad, query))) {
3093       goto done;
3094     }
3095   }
3096
3097   gst_query_parse_caps (query, &filter);
3098
3099   /* no proxy or it failed, do default handling */
3100   fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3101
3102   GST_OBJECT_LOCK (pad);
3103   if (fixed_caps) {
3104     /* fixed caps, try the negotiated caps first */
3105     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3106     if ((result = get_pad_caps (pad)))
3107       goto filter_done_unlock;
3108   }
3109
3110   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3111     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3112     if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3113       goto filter_done_unlock;
3114   }
3115
3116   if (!fixed_caps) {
3117     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3118         "non-fixed pad caps: trying pad caps");
3119     /* non fixed caps, try the negotiated caps */
3120     if ((result = get_pad_caps (pad)))
3121       goto filter_done_unlock;
3122   }
3123
3124   /* this almost never happens */
3125   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3126   result = GST_CAPS_ANY;
3127
3128 filter_done_unlock:
3129   GST_OBJECT_UNLOCK (pad);
3130
3131   /* run the filter on the result */
3132   if (filter) {
3133     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3134         "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3135         GST_PTR_FORMAT, result, result, filter, filter);
3136     result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3137     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3138         result, result);
3139   } else {
3140     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3141         "using caps %p %" GST_PTR_FORMAT, result, result);
3142     result = gst_caps_ref (result);
3143   }
3144   gst_query_set_caps_result (query, result);
3145   gst_caps_unref (result);
3146
3147 done:
3148   return TRUE;
3149 }
3150
3151 /* Default latency implementation */
3152 typedef struct
3153 {
3154   gboolean live;
3155   GstClockTime min, max;
3156 } LatencyFoldData;
3157
3158 static gboolean
3159 query_latency_default_fold (const GValue * item, GValue * ret,
3160     gpointer user_data)
3161 {
3162   GstPad *pad = g_value_get_object (item), *peer;
3163   LatencyFoldData *fold_data = user_data;
3164   GstQuery *query;
3165   gboolean res = FALSE;
3166
3167   query = gst_query_new_latency ();
3168
3169   peer = gst_pad_get_peer (pad);
3170   if (peer) {
3171     res = gst_pad_peer_query (pad, query);
3172   } else {
3173     GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3174   }
3175
3176   if (res) {
3177     gboolean live;
3178     GstClockTime min, max;
3179
3180     gst_query_parse_latency (query, &live, &min, &max);
3181
3182     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3183         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3184
3185     if (live) {
3186       if (min > fold_data->min)
3187         fold_data->min = min;
3188
3189       if (fold_data->max == GST_CLOCK_TIME_NONE)
3190         fold_data->max = max;
3191       else if (max < fold_data->max)
3192         fold_data->max = max;
3193
3194       fold_data->live = TRUE;
3195     }
3196   } else if (peer) {
3197     GST_DEBUG_OBJECT (pad, "latency query failed");
3198     g_value_set_boolean (ret, FALSE);
3199   }
3200
3201   gst_query_unref (query);
3202   if (peer)
3203     gst_object_unref (peer);
3204
3205   return TRUE;
3206 }
3207
3208 static gboolean
3209 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3210 {
3211   GstIterator *it;
3212   GstIteratorResult res;
3213   GValue ret = G_VALUE_INIT;
3214   gboolean query_ret;
3215   LatencyFoldData fold_data;
3216
3217   it = gst_pad_iterate_internal_links (pad);
3218   if (!it) {
3219     GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3220     return FALSE;
3221   }
3222
3223   g_value_init (&ret, G_TYPE_BOOLEAN);
3224
3225 retry:
3226   fold_data.live = FALSE;
3227   fold_data.min = 0;
3228   fold_data.max = GST_CLOCK_TIME_NONE;
3229
3230   g_value_set_boolean (&ret, TRUE);
3231   res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3232   switch (res) {
3233     case GST_ITERATOR_OK:
3234       g_assert_not_reached ();
3235       break;
3236     case GST_ITERATOR_DONE:
3237       break;
3238     case GST_ITERATOR_ERROR:
3239       g_value_set_boolean (&ret, FALSE);
3240       break;
3241     case GST_ITERATOR_RESYNC:
3242       gst_iterator_resync (it);
3243       goto retry;
3244     default:
3245       g_assert_not_reached ();
3246       break;
3247   }
3248   gst_iterator_free (it);
3249
3250   query_ret = g_value_get_boolean (&ret);
3251   if (query_ret) {
3252     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3253         " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3254         fold_data.min, fold_data.max);
3255
3256     if (fold_data.min > fold_data.max) {
3257       GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3258     }
3259
3260     gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3261   } else {
3262     GST_LOG_OBJECT (pad, "latency query failed");
3263   }
3264
3265   return query_ret;
3266 }
3267
3268 typedef struct
3269 {
3270   GstQuery *query;
3271   gboolean result;
3272   gboolean dispatched;
3273 } QueryData;
3274
3275 static gboolean
3276 query_forward_func (GstPad * pad, QueryData * data)
3277 {
3278   GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3279       data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3280
3281   data->result |= gst_pad_peer_query (pad, data->query);
3282
3283   data->dispatched = TRUE;
3284
3285   /* stop on first successful reply */
3286   return data->result;
3287 }
3288
3289 /**
3290  * gst_pad_query_default:
3291  * @pad: a #GstPad to call the default query handler on.
3292  * @parent: (allow-none): the parent of @pad or %NULL
3293  * @query: (transfer none): the #GstQuery to handle.
3294  *
3295  * Invokes the default query handler for the given pad.
3296  * The query is sent to all pads internally linked to @pad. Note that
3297  * if there are many possible sink pads that are internally linked to
3298  * @pad, only one will be sent the query.
3299  * Multi-sinkpad elements should implement custom query handlers.
3300  *
3301  * Returns: %TRUE if the query was performed successfully.
3302  */
3303 gboolean
3304 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3305 {
3306   gboolean forward, ret = FALSE;
3307
3308   switch (GST_QUERY_TYPE (query)) {
3309     case GST_QUERY_SCHEDULING:
3310       forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3311       break;
3312     case GST_QUERY_ALLOCATION:
3313       forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3314       break;
3315     case GST_QUERY_ACCEPT_CAPS:
3316       ret = gst_pad_query_accept_caps_default (pad, query);
3317       forward = FALSE;
3318       break;
3319     case GST_QUERY_CAPS:
3320       ret = gst_pad_query_caps_default (pad, query);
3321       forward = FALSE;
3322       break;
3323     case GST_QUERY_LATENCY:
3324       ret = gst_pad_query_latency_default (pad, query);
3325       forward = FALSE;
3326       break;
3327     case GST_QUERY_POSITION:
3328     case GST_QUERY_SEEKING:
3329     case GST_QUERY_FORMATS:
3330     case GST_QUERY_JITTER:
3331     case GST_QUERY_RATE:
3332     case GST_QUERY_CONVERT:
3333     default:
3334       forward = TRUE;
3335       break;
3336   }
3337
3338   GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3339       query, GST_QUERY_TYPE_NAME (query));
3340
3341   if (forward) {
3342     QueryData data;
3343
3344     data.query = query;
3345     data.dispatched = FALSE;
3346     data.result = FALSE;
3347
3348     gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3349
3350     if (data.dispatched) {
3351       ret = data.result;
3352     } else {
3353       /* nothing dispatched, assume drained */
3354       if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3355         ret = TRUE;
3356       else
3357         ret = FALSE;
3358     }
3359   }
3360   return ret;
3361 }
3362
3363 static void
3364 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3365 {
3366   GstPad *pad = data->pad;
3367   GstPadProbeInfo *info = data->info;
3368   GstPadProbeType type, flags;
3369   GstPadProbeCallback callback;
3370   GstPadProbeReturn ret;
3371   gpointer original_data;
3372
3373   /* if we have called this callback, do nothing */
3374   if (PROBE_COOKIE (hook) == data->cookie) {
3375     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3376         "hook %lu, cookie %u already called", hook->hook_id,
3377         PROBE_COOKIE (hook));
3378     return;
3379   }
3380
3381   PROBE_COOKIE (hook) = data->cookie;
3382
3383   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3384   type = info->type;
3385   original_data = info->data;
3386
3387   /* one of the data types for non-idle probes */
3388   if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3389       && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3390     goto no_match;
3391   /* one of the scheduling types */
3392   if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3393     goto no_match;
3394   /* one of the blocking types must match */
3395   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3396       (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3397     goto no_match;
3398   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3399       (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3400     goto no_match;
3401   /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3402   if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3403       (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3404     goto no_match;
3405
3406   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3407       "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3408       PROBE_COOKIE (hook), flags);
3409
3410   data->marshalled = TRUE;
3411
3412   callback = (GstPadProbeCallback) hook->func;
3413   if (callback == NULL)
3414     return;
3415
3416   info->id = hook->hook_id;
3417
3418   GST_OBJECT_UNLOCK (pad);
3419
3420   ret = callback (pad, info, hook->data);
3421
3422   GST_OBJECT_LOCK (pad);
3423
3424   if (original_data != NULL && info->data == NULL) {
3425     GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3426     info->type = GST_PAD_PROBE_TYPE_INVALID;
3427     data->dropped = TRUE;
3428   }
3429
3430   switch (ret) {
3431     case GST_PAD_PROBE_REMOVE:
3432       /* remove the probe */
3433       GST_DEBUG_OBJECT (pad, "asked to remove hook");
3434       cleanup_hook (pad, hook);
3435       break;
3436     case GST_PAD_PROBE_DROP:
3437       /* need to drop the data, make sure other probes don't get called
3438        * anymore */
3439       GST_DEBUG_OBJECT (pad, "asked to drop item");
3440       info->type = GST_PAD_PROBE_TYPE_INVALID;
3441       data->dropped = TRUE;
3442       break;
3443     case GST_PAD_PROBE_HANDLED:
3444       GST_DEBUG_OBJECT (pad, "probe handled data");
3445       data->handled = TRUE;
3446       break;
3447     case GST_PAD_PROBE_PASS:
3448       /* inform the pad block to let things pass */
3449       GST_DEBUG_OBJECT (pad, "asked to pass item");
3450       data->pass = TRUE;
3451       break;
3452     case GST_PAD_PROBE_OK:
3453       GST_DEBUG_OBJECT (pad, "probe returned OK");
3454       break;
3455     default:
3456       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3457       break;
3458   }
3459   return;
3460
3461 no_match:
3462   {
3463     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3464         "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3465         hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3466     return;
3467   }
3468 }
3469
3470 /* a probe that does not take or return any data */
3471 #define PROBE_NO_DATA(pad,mask,label,defaultval)                \
3472   G_STMT_START {                                                \
3473     if (G_UNLIKELY (pad->num_probes)) {                         \
3474       GstFlowReturn pval = defaultval;                          \
3475       /* pass NULL as the data item */                          \
3476       GstPadProbeInfo info = { mask, 0, NULL, 0, 0 };           \
3477       info.ABI.abi.flow_ret = defaultval;                       \
3478       ret = do_probe_callbacks (pad, &info, defaultval);        \
3479       if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK))       \
3480         goto label;                                             \
3481     }                                                           \
3482   } G_STMT_END
3483
3484 #define PROBE_FULL(pad,mask,data,offs,size,label,handleable,handle_label) \
3485   G_STMT_START {                                                        \
3486     if (G_UNLIKELY (pad->num_probes)) {                                 \
3487       /* pass the data item */                                          \
3488       GstPadProbeInfo info = { mask, 0, data, offs, size };             \
3489       info.ABI.abi.flow_ret = GST_FLOW_OK;                              \
3490       ret = do_probe_callbacks (pad, &info, GST_FLOW_OK);               \
3491       /* store the possibly updated data item */                        \
3492       data = GST_PAD_PROBE_INFO_DATA (&info);                           \
3493       /* if something went wrong, exit */                               \
3494       if (G_UNLIKELY (ret != GST_FLOW_OK)) {                            \
3495         if (handleable && ret == GST_FLOW_CUSTOM_SUCCESS_1) {           \
3496           ret = info.ABI.abi.flow_ret;                                          \
3497           goto handle_label;                                            \
3498         }                                                               \
3499         goto label;                                                     \
3500       }                                                                 \
3501     }                                                                   \
3502   } G_STMT_END
3503
3504 #define PROBE_PUSH(pad,mask,data,label)         \
3505   PROBE_FULL(pad, mask, data, -1, -1, label, FALSE, label);
3506 #define PROBE_HANDLE(pad,mask,data,label,handle_label)  \
3507   PROBE_FULL(pad, mask, data, -1, -1, label, TRUE, handle_label);
3508 #define PROBE_PULL(pad,mask,data,offs,size,label)               \
3509   PROBE_FULL(pad, mask, data, offs, size, label, FALSE, label);
3510
3511 static GstFlowReturn
3512 do_pad_idle_probe_wait (GstPad * pad)
3513 {
3514   while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3515     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3516         "waiting idle probe to be removed");
3517     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3518     GST_PAD_BLOCK_WAIT (pad);
3519     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3520     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3521
3522     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3523       return GST_FLOW_FLUSHING;
3524   }
3525   return GST_FLOW_OK;
3526 }
3527
3528 #define PROBE_TYPE_IS_SERIALIZED(i) \
3529     ( \
3530       ( \
3531         (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3532         GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3533         GST_EVENT_IS_SERIALIZED ((i)->data)) \
3534       ) || ( \
3535         (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3536         GST_QUERY_IS_SERIALIZED ((i)->data)) \
3537       ) || ( \
3538         ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3539         GST_PAD_PROBE_TYPE_BUFFER_LIST))  \
3540       ) \
3541     )
3542
3543 static GstFlowReturn
3544 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3545     GstFlowReturn defaultval)
3546 {
3547   ProbeMarshall data;
3548   guint cookie;
3549   gboolean is_block;
3550
3551   data.pad = pad;
3552   data.info = info;
3553   data.pass = FALSE;
3554   data.handled = FALSE;
3555   data.marshalled = FALSE;
3556   data.dropped = FALSE;
3557   data.cookie = ++pad->priv->probe_cookie;
3558
3559   is_block =
3560       (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3561
3562   if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3563     if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3564       goto flushing;
3565   }
3566
3567 again:
3568   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3569       "do probes cookie %u", data.cookie);
3570   cookie = pad->priv->probe_list_cookie;
3571
3572   g_hook_list_marshal (&pad->probes, TRUE,
3573       (GHookMarshaller) probe_hook_marshal, &data);
3574
3575   /* if the list changed, call the new callbacks (they will not have their
3576    * cookie set to data.cookie */
3577   if (cookie != pad->priv->probe_list_cookie) {
3578     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3579         "probe list changed, restarting");
3580     goto again;
3581   }
3582
3583   /* the first item that dropped will stop the hooks and then we drop here */
3584   if (data.dropped)
3585     goto dropped;
3586
3587   /* If one handler took care of it, let the the item pass */
3588   if (data.handled) {
3589     goto handled;
3590   }
3591
3592   /* if no handler matched and we are blocking, let the item pass */
3593   if (!data.marshalled && is_block)
3594     goto passed;
3595
3596   /* At this point, all handlers returned either OK or PASS. If one handler
3597    * returned PASS, let the item pass */
3598   if (data.pass)
3599     goto passed;
3600
3601   if (is_block) {
3602     while (GST_PAD_IS_BLOCKED (pad)) {
3603       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3604           "we are blocked %d times", pad->num_blocked);
3605
3606       /* we might have released the lock */
3607       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3608         goto flushing;
3609
3610       /* now we block the streaming thread. It can be unlocked when we
3611        * deactivate the pad (which will also set the FLUSHING flag) or
3612        * when the pad is unblocked. A flushing event will also unblock
3613        * the pad after setting the FLUSHING flag. */
3614       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3615           "Waiting to be unblocked or set flushing");
3616       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3617       GST_PAD_BLOCK_WAIT (pad);
3618       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3619       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3620
3621       /* if the list changed, call the new callbacks (they will not have their
3622        * cookie set to data.cookie */
3623       if (cookie != pad->priv->probe_list_cookie) {
3624         GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3625             "probe list changed, restarting");
3626         goto again;
3627       }
3628
3629       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3630         goto flushing;
3631     }
3632   }
3633
3634   return defaultval;
3635
3636   /* ERRORS */
3637 flushing:
3638   {
3639     GST_DEBUG_OBJECT (pad, "pad is flushing");
3640     return GST_FLOW_FLUSHING;
3641   }
3642 dropped:
3643   {
3644     GST_DEBUG_OBJECT (pad, "data is dropped");
3645     return GST_FLOW_CUSTOM_SUCCESS;
3646   }
3647 passed:
3648   {
3649     /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3650     GST_DEBUG_OBJECT (pad, "data is passed");
3651     return GST_FLOW_OK;
3652   }
3653 handled:
3654   {
3655     GST_DEBUG_OBJECT (pad, "data was handled");
3656     return GST_FLOW_CUSTOM_SUCCESS_1;
3657   }
3658 }
3659
3660 /* pad offsets */
3661
3662 /**
3663  * gst_pad_get_offset:
3664  * @pad: a #GstPad
3665  *
3666  * Get the offset applied to the running time of @pad. @pad has to be a source
3667  * pad.
3668  *
3669  * Returns: the offset.
3670  */
3671 gint64
3672 gst_pad_get_offset (GstPad * pad)
3673 {
3674   gint64 result;
3675
3676   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3677
3678   GST_OBJECT_LOCK (pad);
3679   result = pad->offset;
3680   GST_OBJECT_UNLOCK (pad);
3681
3682   return result;
3683 }
3684
3685 static gboolean
3686 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3687 {
3688   ev->received = FALSE;
3689   return TRUE;
3690 }
3691
3692 /**
3693  * gst_pad_set_offset:
3694  * @pad: a #GstPad
3695  * @offset: the offset
3696  *
3697  * Set the offset that will be applied to the running time of @pad.
3698  */
3699 void
3700 gst_pad_set_offset (GstPad * pad, gint64 offset)
3701 {
3702   g_return_if_fail (GST_IS_PAD (pad));
3703
3704   GST_OBJECT_LOCK (pad);
3705   /* if nothing changed, do nothing */
3706   if (pad->offset == offset)
3707     goto done;
3708
3709   pad->offset = offset;
3710   GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3711
3712   /* resend all sticky events with updated offset on next buffer push */
3713   events_foreach (pad, mark_event_not_received, NULL);
3714   GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3715
3716 done:
3717   GST_OBJECT_UNLOCK (pad);
3718 }
3719
3720 typedef struct
3721 {
3722   GstFlowReturn ret;
3723
3724   /* If TRUE and ret is not OK this means
3725    * that pushing the EOS event failed
3726    */
3727   gboolean was_eos;
3728
3729   /* If called for an event this is
3730    * the event that would be pushed
3731    * next. Don't forward sticky events
3732    * that would come after that */
3733   GstEvent *event;
3734 } PushStickyData;
3735
3736 /* should be called with pad LOCK */
3737 static gboolean
3738 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3739 {
3740   PushStickyData *data = user_data;
3741   GstEvent *event = ev->event;
3742
3743   if (ev->received) {
3744     GST_DEBUG_OBJECT (pad, "event %s was already received",
3745         GST_EVENT_TYPE_NAME (event));
3746     return TRUE;
3747   }
3748
3749   /* If we're called because of an sticky event, only forward
3750    * events that would come before this new event and the
3751    * event itself */
3752   if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3753       GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3754       GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3755     data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3756   } else {
3757     data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3758         GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3759   }
3760
3761   switch (data->ret) {
3762     case GST_FLOW_OK:
3763       ev->received = TRUE;
3764       GST_DEBUG_OBJECT (pad, "event %s marked received",
3765           GST_EVENT_TYPE_NAME (event));
3766       break;
3767     case GST_FLOW_CUSTOM_SUCCESS:
3768       /* we can't assume the event is received when it was dropped */
3769       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3770           GST_EVENT_TYPE_NAME (event));
3771       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3772       data->ret = GST_FLOW_OK;
3773       break;
3774     case GST_FLOW_CUSTOM_SUCCESS_1:
3775       /* event was ignored and should be sent later */
3776       GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3777           GST_EVENT_TYPE_NAME (event));
3778       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3779       data->ret = GST_FLOW_OK;
3780       break;
3781     case GST_FLOW_NOT_LINKED:
3782       /* not linked is not a problem, we are sticky so the event will be
3783        * rescheduled to be sent later on re-link, but only for non-EOS events */
3784       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3785       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
3786         data->ret = GST_FLOW_OK;
3787         ev->received = TRUE;
3788       }
3789       break;
3790     default:
3791       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3792           gst_flow_get_name (data->ret));
3793       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3794       break;
3795   }
3796
3797   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3798     data->was_eos = TRUE;
3799
3800   return data->ret == GST_FLOW_OK;
3801 }
3802
3803 /* check sticky events and push them when needed. should be called
3804  * with pad LOCK */
3805 static inline GstFlowReturn
3806 check_sticky (GstPad * pad, GstEvent * event)
3807 {
3808   PushStickyData data = { GST_FLOW_OK, FALSE, event };
3809
3810   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3811     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3812
3813     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3814     events_foreach (pad, push_sticky, &data);
3815
3816     /* If there's an EOS event we must push it downstream
3817      * even if sending a previous sticky event failed.
3818      * Otherwise the pipeline might wait forever for EOS.
3819      *
3820      * Only do this if pushing another event than the EOS
3821      * event failed.
3822      */
3823     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3824       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3825
3826       if (ev && !ev->received) {
3827         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3828             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3829         /* the event could have been dropped. Because this can only
3830          * happen if the user asked for it, it's not an error */
3831         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3832           data.ret = GST_FLOW_OK;
3833       }
3834     }
3835   }
3836   return data.ret;
3837 }
3838
3839
3840 /**
3841  * gst_pad_query:
3842  * @pad: a #GstPad to invoke the default query on.
3843  * @query: (transfer none): the #GstQuery to perform.
3844  *
3845  * Dispatches a query to a pad. The query should have been allocated by the
3846  * caller via one of the type-specific allocation functions. The element that
3847  * the pad belongs to is responsible for filling the query with an appropriate
3848  * response, which should then be parsed with a type-specific query parsing
3849  * function.
3850  *
3851  * Again, the caller is responsible for both the allocation and deallocation of
3852  * the query structure.
3853  *
3854  * Please also note that some queries might need a running pipeline to work.
3855  *
3856  * Returns: %TRUE if the query could be performed.
3857  */
3858 gboolean
3859 gst_pad_query (GstPad * pad, GstQuery * query)
3860 {
3861   GstObject *parent;
3862   gboolean res, serialized;
3863   GstPadQueryFunction func;
3864   GstPadProbeType type;
3865   GstFlowReturn ret;
3866
3867   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3868   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3869
3870   if (GST_PAD_IS_SRC (pad)) {
3871     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3872       goto wrong_direction;
3873     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3874   } else if (GST_PAD_IS_SINK (pad)) {
3875     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3876       goto wrong_direction;
3877     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3878   } else
3879     goto unknown_direction;
3880
3881   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3882       GST_QUERY_TYPE_NAME (query));
3883   GST_TRACER_PAD_QUERY_PRE (pad, query);
3884
3885   serialized = GST_QUERY_IS_SERIALIZED (query);
3886   if (G_UNLIKELY (serialized))
3887     GST_PAD_STREAM_LOCK (pad);
3888
3889   GST_OBJECT_LOCK (pad);
3890   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3891       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3892   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3893
3894   ACQUIRE_PARENT (pad, parent, no_parent);
3895   GST_OBJECT_UNLOCK (pad);
3896
3897   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3898     goto no_func;
3899
3900   res = func (pad, parent, query);
3901
3902   RELEASE_PARENT (parent);
3903
3904   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3905       GST_QUERY_TYPE_NAME (query), res);
3906   GST_TRACER_PAD_QUERY_POST (pad, res, query);
3907
3908   if (res != TRUE)
3909     goto query_failed;
3910
3911   GST_OBJECT_LOCK (pad);
3912   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3913   GST_OBJECT_UNLOCK (pad);
3914
3915   if (G_UNLIKELY (serialized))
3916     GST_PAD_STREAM_UNLOCK (pad);
3917
3918   return res;
3919
3920   /* ERRORS */
3921 wrong_direction:
3922   {
3923     g_warning ("pad %s:%s query %s in wrong direction",
3924         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3925     return FALSE;
3926   }
3927 unknown_direction:
3928   {
3929     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3930     return FALSE;
3931   }
3932 no_parent:
3933   {
3934     GST_DEBUG_OBJECT (pad, "had no parent");
3935     GST_OBJECT_UNLOCK (pad);
3936     if (G_UNLIKELY (serialized))
3937       GST_PAD_STREAM_UNLOCK (pad);
3938     return FALSE;
3939   }
3940 no_func:
3941   {
3942     GST_DEBUG_OBJECT (pad, "had no query function");
3943     RELEASE_PARENT (parent);
3944     if (G_UNLIKELY (serialized))
3945       GST_PAD_STREAM_UNLOCK (pad);
3946     return FALSE;
3947   }
3948 query_failed:
3949   {
3950     GST_DEBUG_OBJECT (pad, "query failed");
3951     if (G_UNLIKELY (serialized))
3952       GST_PAD_STREAM_UNLOCK (pad);
3953     return FALSE;
3954   }
3955 probe_stopped:
3956   {
3957     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3958     GST_OBJECT_UNLOCK (pad);
3959     if (G_UNLIKELY (serialized))
3960       GST_PAD_STREAM_UNLOCK (pad);
3961
3962     /* if a probe dropped without handling, we don't sent it further but assume
3963      * that the probe did not answer the query and return FALSE */
3964     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
3965       res = FALSE;
3966     else
3967       res = TRUE;
3968
3969     return res;
3970   }
3971 }
3972
3973 /**
3974  * gst_pad_peer_query:
3975  * @pad: a #GstPad to invoke the peer query on.
3976  * @query: (transfer none): the #GstQuery to perform.
3977  *
3978  * Performs gst_pad_query() on the peer of @pad.
3979  *
3980  * The caller is responsible for both the allocation and deallocation of
3981  * the query structure.
3982  *
3983  * Returns: %TRUE if the query could be performed. This function returns %FALSE
3984  * if @pad has no peer.
3985  */
3986 gboolean
3987 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3988 {
3989   GstPad *peerpad;
3990   GstPadProbeType type;
3991   gboolean res, serialized;
3992   GstFlowReturn ret;
3993
3994   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3995   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3996
3997   if (GST_PAD_IS_SRC (pad)) {
3998     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3999       goto wrong_direction;
4000     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
4001   } else if (GST_PAD_IS_SINK (pad)) {
4002     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
4003       goto wrong_direction;
4004     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
4005   } else
4006     goto unknown_direction;
4007
4008   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
4009       GST_QUERY_TYPE_NAME (query));
4010
4011   serialized = GST_QUERY_IS_SERIALIZED (query);
4012
4013   GST_OBJECT_LOCK (pad);
4014   if (GST_PAD_IS_SRC (pad) && serialized) {
4015     /* all serialized queries on the srcpad trigger push of
4016      * sticky events */
4017     if (check_sticky (pad, NULL) != GST_FLOW_OK)
4018       goto sticky_failed;
4019   }
4020
4021   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4022       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4023   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4024
4025   peerpad = GST_PAD_PEER (pad);
4026   if (G_UNLIKELY (peerpad == NULL))
4027     goto no_peer;
4028
4029   gst_object_ref (peerpad);
4030   GST_OBJECT_UNLOCK (pad);
4031
4032   res = gst_pad_query (peerpad, query);
4033
4034   gst_object_unref (peerpad);
4035
4036   if (res != TRUE)
4037     goto query_failed;
4038
4039   GST_OBJECT_LOCK (pad);
4040   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4041   GST_OBJECT_UNLOCK (pad);
4042
4043   return res;
4044
4045   /* ERRORS */
4046 wrong_direction:
4047   {
4048     g_warning ("pad %s:%s query %s in wrong direction",
4049         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4050     return FALSE;
4051   }
4052 unknown_direction:
4053   {
4054     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4055     return FALSE;
4056   }
4057 sticky_failed:
4058   {
4059     GST_WARNING_OBJECT (pad, "could not send sticky events");
4060     GST_OBJECT_UNLOCK (pad);
4061     return FALSE;
4062   }
4063 no_peer:
4064   {
4065     GST_INFO_OBJECT (pad, "pad has no peer");
4066     GST_OBJECT_UNLOCK (pad);
4067     return FALSE;
4068   }
4069 query_failed:
4070   {
4071     GST_DEBUG_OBJECT (pad, "query failed");
4072     return FALSE;
4073   }
4074 probe_stopped:
4075   {
4076     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4077     GST_OBJECT_UNLOCK (pad);
4078
4079     /* if a probe dropped without handling, we don't sent it further but
4080      * assume that the probe did not answer the query and return FALSE */
4081     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4082       res = FALSE;
4083     else
4084       res = TRUE;
4085
4086     return res;
4087   }
4088 }
4089
4090 /**********************************************************************
4091  * Data passing functions
4092  */
4093
4094 /* this is the chain function that does not perform the additional argument
4095  * checking for that little extra speed.
4096  */
4097 static inline GstFlowReturn
4098 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4099 {
4100   GstFlowReturn ret;
4101   GstObject *parent;
4102   gboolean handled = FALSE;
4103
4104   GST_PAD_STREAM_LOCK (pad);
4105
4106   GST_OBJECT_LOCK (pad);
4107   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4108     goto flushing;
4109
4110   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4111     goto eos;
4112
4113   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4114     goto wrong_mode;
4115
4116 #ifdef GST_ENABLE_EXTRA_CHECKS
4117   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4118     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4119       g_warning (G_STRLOC
4120           ":%s:<%s:%s> Got data flow before stream-start event",
4121           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4122     }
4123     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4124       g_warning (G_STRLOC
4125           ":%s:<%s:%s> Got data flow before segment event",
4126           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4127     }
4128     pad->priv->last_cookie = pad->priv->events_cookie;
4129   }
4130 #endif
4131
4132   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4133       probe_handled);
4134
4135   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4136
4137   ACQUIRE_PARENT (pad, parent, no_parent);
4138   GST_OBJECT_UNLOCK (pad);
4139
4140   /* NOTE: we read the chainfunc unlocked.
4141    * we cannot hold the lock for the pad so we might send
4142    * the data to the wrong function. This is not really a
4143    * problem since functions are assigned at creation time
4144    * and don't change that often... */
4145   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4146     GstPadChainFunction chainfunc;
4147
4148     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4149       goto no_function;
4150
4151     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4152         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4153         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4154
4155     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4156
4157     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4158         "called chainfunction &%s with buffer %p, returned %s",
4159         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4160   } else {
4161     GstPadChainListFunction chainlistfunc;
4162
4163     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4164       goto no_function;
4165
4166     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4167         "calling chainlistfunction &%s",
4168         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4169
4170     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4171
4172     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4173         "called chainlistfunction &%s, returned %s",
4174         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4175   }
4176
4177   RELEASE_PARENT (parent);
4178
4179   GST_PAD_STREAM_UNLOCK (pad);
4180
4181   return ret;
4182
4183   /* ERRORS */
4184 flushing:
4185   {
4186     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4187         "chaining, but pad was flushing");
4188     GST_OBJECT_UNLOCK (pad);
4189     GST_PAD_STREAM_UNLOCK (pad);
4190     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4191     return GST_FLOW_FLUSHING;
4192   }
4193 eos:
4194   {
4195     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4196     GST_OBJECT_UNLOCK (pad);
4197     GST_PAD_STREAM_UNLOCK (pad);
4198     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4199     return GST_FLOW_EOS;
4200   }
4201 wrong_mode:
4202   {
4203     g_critical ("chain on pad %s:%s but it was not in push mode",
4204         GST_DEBUG_PAD_NAME (pad));
4205     GST_OBJECT_UNLOCK (pad);
4206     GST_PAD_STREAM_UNLOCK (pad);
4207     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4208     return GST_FLOW_ERROR;
4209   }
4210 probe_handled:
4211   handled = TRUE;
4212   /* PASSTHROUGH */
4213 probe_stopped:
4214   {
4215     GST_OBJECT_UNLOCK (pad);
4216     GST_PAD_STREAM_UNLOCK (pad);
4217     /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4218     if (!handled)
4219       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4220
4221     switch (ret) {
4222       case GST_FLOW_CUSTOM_SUCCESS:
4223       case GST_FLOW_CUSTOM_SUCCESS_1:
4224         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4225         ret = GST_FLOW_OK;
4226         break;
4227       default:
4228         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4229         break;
4230     }
4231     return ret;
4232   }
4233 no_parent:
4234   {
4235     GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4236     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4237     GST_OBJECT_UNLOCK (pad);
4238     GST_PAD_STREAM_UNLOCK (pad);
4239     return GST_FLOW_FLUSHING;
4240   }
4241 no_function:
4242   {
4243     RELEASE_PARENT (parent);
4244     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4245     g_critical ("chain on pad %s:%s but it has no chainfunction",
4246         GST_DEBUG_PAD_NAME (pad));
4247     GST_PAD_STREAM_UNLOCK (pad);
4248     return GST_FLOW_NOT_SUPPORTED;
4249   }
4250 }
4251
4252 /**
4253  * gst_pad_chain:
4254  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4255  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4256  *     if not.
4257  *
4258  * Chain a buffer to @pad.
4259  *
4260  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4261  *
4262  * If the buffer type is not acceptable for @pad (as negotiated with a
4263  * preceding GST_EVENT_CAPS event), this function returns
4264  * #GST_FLOW_NOT_NEGOTIATED.
4265  *
4266  * The function proceeds calling the chain function installed on @pad (see
4267  * gst_pad_set_chain_function()) and the return value of that function is
4268  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4269  * chain function.
4270  *
4271  * In all cases, success or failure, the caller loses its reference to @buffer
4272  * after calling this function.
4273  *
4274  * Returns: a #GstFlowReturn from the pad.
4275  *
4276  * MT safe.
4277  */
4278 GstFlowReturn
4279 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4280 {
4281   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4282   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4283   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4284
4285   return gst_pad_chain_data_unchecked (pad,
4286       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4287 }
4288
4289 static GstFlowReturn
4290 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4291     GstBufferList * list)
4292 {
4293   guint i, len;
4294   GstBuffer *buffer;
4295   GstFlowReturn ret;
4296
4297   GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4298
4299   len = gst_buffer_list_length (list);
4300
4301   ret = GST_FLOW_OK;
4302   for (i = 0; i < len; i++) {
4303     buffer = gst_buffer_list_get (list, i);
4304     ret =
4305         gst_pad_chain_data_unchecked (pad,
4306         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4307         gst_buffer_ref (buffer));
4308     if (ret != GST_FLOW_OK)
4309       break;
4310   }
4311   gst_buffer_list_unref (list);
4312
4313   return ret;
4314 }
4315
4316 /**
4317  * gst_pad_chain_list:
4318  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4319  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4320  *     if not.
4321  *
4322  * Chain a bufferlist to @pad.
4323  *
4324  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4325  *
4326  * If @pad was not negotiated properly with a CAPS event, this function
4327  * returns #GST_FLOW_NOT_NEGOTIATED.
4328  *
4329  * The function proceeds calling the chainlist function installed on @pad (see
4330  * gst_pad_set_chain_list_function()) and the return value of that function is
4331  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4332  * chainlist function.
4333  *
4334  * In all cases, success or failure, the caller loses its reference to @list
4335  * after calling this function.
4336  *
4337  * MT safe.
4338  *
4339  * Returns: a #GstFlowReturn from the pad.
4340  */
4341 GstFlowReturn
4342 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4343 {
4344   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4345   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4346   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4347
4348   return gst_pad_chain_data_unchecked (pad,
4349       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4350 }
4351
4352 static GstFlowReturn
4353 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4354 {
4355   GstPad *peer;
4356   GstFlowReturn ret;
4357   gboolean handled = FALSE;
4358
4359   GST_OBJECT_LOCK (pad);
4360   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4361     goto flushing;
4362
4363   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4364     goto eos;
4365
4366   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4367     goto wrong_mode;
4368
4369 #ifdef GST_ENABLE_EXTRA_CHECKS
4370   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4371     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4372       g_warning (G_STRLOC
4373           ":%s:<%s:%s> Got data flow before stream-start event",
4374           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4375     }
4376     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4377       g_warning (G_STRLOC
4378           ":%s:<%s:%s> Got data flow before segment event",
4379           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4380     }
4381     pad->priv->last_cookie = pad->priv->events_cookie;
4382   }
4383 #endif
4384
4385   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4386     goto events_error;
4387
4388   /* do block probes */
4389   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4390       probe_handled);
4391
4392   /* recheck sticky events because the probe might have cause a relink */
4393   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4394     goto events_error;
4395
4396   /* do post-blocking probes */
4397   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4398
4399   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4400     goto not_linked;
4401
4402   /* take ref to peer pad before releasing the lock */
4403   gst_object_ref (peer);
4404   pad->priv->using++;
4405   GST_OBJECT_UNLOCK (pad);
4406
4407   ret = gst_pad_chain_data_unchecked (peer, type, data);
4408   data = NULL;
4409
4410   gst_object_unref (peer);
4411
4412   GST_OBJECT_LOCK (pad);
4413   pad->ABI.abi.last_flowret = ret;
4414   pad->priv->using--;
4415   if (pad->priv->using == 0) {
4416     /* pad is not active anymore, trigger idle callbacks */
4417     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4418         probe_stopped, ret);
4419   }
4420   GST_OBJECT_UNLOCK (pad);
4421
4422   return ret;
4423
4424   /* ERROR recovery here */
4425   /* ERRORS */
4426 flushing:
4427   {
4428     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4429         "pushing, but pad was flushing");
4430     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4431     GST_OBJECT_UNLOCK (pad);
4432     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4433     return GST_FLOW_FLUSHING;
4434   }
4435 eos:
4436   {
4437     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4438     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4439     GST_OBJECT_UNLOCK (pad);
4440     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4441     return GST_FLOW_EOS;
4442   }
4443 wrong_mode:
4444   {
4445     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4446         GST_DEBUG_PAD_NAME (pad));
4447     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4448     GST_OBJECT_UNLOCK (pad);
4449     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4450     return GST_FLOW_ERROR;
4451   }
4452 events_error:
4453   {
4454     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4455         "error pushing events, return %s", gst_flow_get_name (ret));
4456     pad->ABI.abi.last_flowret = ret;
4457     GST_OBJECT_UNLOCK (pad);
4458     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4459     return ret;
4460   }
4461 probe_handled:
4462   handled = TRUE;
4463   /* PASSTHROUGH */
4464 probe_stopped:
4465   {
4466     GST_OBJECT_UNLOCK (pad);
4467     if (data != NULL && !handled)
4468       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4469
4470     switch (ret) {
4471       case GST_FLOW_CUSTOM_SUCCESS:
4472       case GST_FLOW_CUSTOM_SUCCESS_1:
4473         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4474         ret = GST_FLOW_OK;
4475         break;
4476       default:
4477         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4478         break;
4479     }
4480     pad->ABI.abi.last_flowret = ret;
4481     return ret;
4482   }
4483 not_linked:
4484   {
4485     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4486         "pushing, but it was not linked");
4487     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4488     GST_OBJECT_UNLOCK (pad);
4489     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4490     return GST_FLOW_NOT_LINKED;
4491   }
4492 }
4493
4494 /**
4495  * gst_pad_push:
4496  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4497  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4498  *     if not.
4499  *
4500  * Pushes a buffer to the peer of @pad.
4501  *
4502  * This function will call installed block probes before triggering any
4503  * installed data probes.
4504  *
4505  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4506  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4507  * be returned.
4508  *
4509  * In all cases, success or failure, the caller loses its reference to @buffer
4510  * after calling this function.
4511  *
4512  * Returns: a #GstFlowReturn from the peer pad.
4513  *
4514  * MT safe.
4515  */
4516 GstFlowReturn
4517 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4518 {
4519   GstFlowReturn res;
4520
4521   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4522   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4523   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4524
4525   GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4526   res = gst_pad_push_data (pad,
4527       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4528   GST_TRACER_PAD_PUSH_POST (pad, res);
4529   return res;
4530 }
4531
4532 /**
4533  * gst_pad_push_list:
4534  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4535  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4536  *     if not.
4537  *
4538  * Pushes a buffer list to the peer of @pad.
4539  *
4540  * This function will call installed block probes before triggering any
4541  * installed data probes.
4542  *
4543  * The function proceeds calling the chain function on the peer pad and returns
4544  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4545  * be returned. If the peer pad does not have any installed chainlist function
4546  * every group buffer of the list will be merged into a normal #GstBuffer and
4547  * chained via gst_pad_chain().
4548  *
4549  * In all cases, success or failure, the caller loses its reference to @list
4550  * after calling this function.
4551  *
4552  * Returns: a #GstFlowReturn from the peer pad.
4553  *
4554  * MT safe.
4555  */
4556 GstFlowReturn
4557 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4558 {
4559   GstFlowReturn res;
4560
4561   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4562   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4563   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4564
4565   GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4566   res = gst_pad_push_data (pad,
4567       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4568   GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4569   return res;
4570 }
4571
4572 static GstFlowReturn
4573 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4574     GstBuffer ** buffer)
4575 {
4576   GstFlowReturn ret;
4577   GstPadGetRangeFunction getrangefunc;
4578   GstObject *parent;
4579   GstBuffer *res_buf;
4580
4581   GST_PAD_STREAM_LOCK (pad);
4582
4583   GST_OBJECT_LOCK (pad);
4584   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4585     goto flushing;
4586
4587   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4588     goto wrong_mode;
4589
4590   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4591     goto events_error;
4592
4593   res_buf = *buffer;
4594
4595   /* when one of the probes returns DROPPED, probe_stopped will be called
4596    * and we skip calling the getrange function, res_buf should then contain a
4597    * valid result buffer */
4598   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4599       res_buf, offset, size, probe_stopped);
4600
4601   /* recheck sticky events because the probe might have cause a relink */
4602   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4603     goto events_error;
4604
4605   ACQUIRE_PARENT (pad, parent, no_parent);
4606   GST_OBJECT_UNLOCK (pad);
4607
4608   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4609     goto no_function;
4610
4611   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4612       "calling getrangefunc %s, offset %"
4613       G_GUINT64_FORMAT ", size %u",
4614       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4615
4616   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4617
4618   RELEASE_PARENT (parent);
4619
4620   GST_OBJECT_LOCK (pad);
4621   if (G_UNLIKELY (ret != GST_FLOW_OK))
4622     goto get_range_failed;
4623
4624   /* can only fire the signal if we have a valid buffer */
4625 probed_data:
4626   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4627       res_buf, offset, size, probe_stopped_unref);
4628   pad->ABI.abi.last_flowret = ret;
4629   GST_OBJECT_UNLOCK (pad);
4630
4631   GST_PAD_STREAM_UNLOCK (pad);
4632
4633   *buffer = res_buf;
4634
4635   return ret;
4636
4637   /* ERRORS */
4638 flushing:
4639   {
4640     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4641         "getrange, but pad was flushing");
4642     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4643     GST_OBJECT_UNLOCK (pad);
4644     GST_PAD_STREAM_UNLOCK (pad);
4645     return GST_FLOW_FLUSHING;
4646   }
4647 wrong_mode:
4648   {
4649     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4650         GST_DEBUG_PAD_NAME (pad));
4651     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4652     GST_OBJECT_UNLOCK (pad);
4653     GST_PAD_STREAM_UNLOCK (pad);
4654     return GST_FLOW_ERROR;
4655   }
4656 events_error:
4657   {
4658     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4659     pad->ABI.abi.last_flowret = ret;
4660     GST_OBJECT_UNLOCK (pad);
4661     GST_PAD_STREAM_UNLOCK (pad);
4662     return ret;
4663   }
4664 no_parent:
4665   {
4666     GST_DEBUG_OBJECT (pad, "no parent");
4667     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4668     GST_OBJECT_UNLOCK (pad);
4669     GST_PAD_STREAM_UNLOCK (pad);
4670     return GST_FLOW_FLUSHING;
4671   }
4672 no_function:
4673   {
4674     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4675         GST_DEBUG_PAD_NAME (pad));
4676     RELEASE_PARENT (parent);
4677     GST_PAD_STREAM_UNLOCK (pad);
4678     return GST_FLOW_NOT_SUPPORTED;
4679   }
4680 probe_stopped:
4681   {
4682     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4683         "probe returned %s", gst_flow_get_name (ret));
4684     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4685       if (res_buf) {
4686         /* the probe filled the buffer and asks us to not call the getrange
4687          * anymore, we continue with the post probes then. */
4688         GST_DEBUG_OBJECT (pad, "handled buffer");
4689         ret = GST_FLOW_OK;
4690         goto probed_data;
4691       } else {
4692         /* no buffer, we are EOS */
4693         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4694         ret = GST_FLOW_EOS;
4695       }
4696     }
4697     pad->ABI.abi.last_flowret = ret;
4698     GST_OBJECT_UNLOCK (pad);
4699     GST_PAD_STREAM_UNLOCK (pad);
4700
4701     return ret;
4702   }
4703 probe_stopped_unref:
4704   {
4705     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4706         "probe returned %s", gst_flow_get_name (ret));
4707     /* if we drop here, it signals EOS */
4708     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4709       ret = GST_FLOW_EOS;
4710     pad->ABI.abi.last_flowret = ret;
4711     GST_OBJECT_UNLOCK (pad);
4712     GST_PAD_STREAM_UNLOCK (pad);
4713     if (*buffer == NULL)
4714       gst_buffer_unref (res_buf);
4715     return ret;
4716   }
4717 get_range_failed:
4718   {
4719     pad->ABI.abi.last_flowret = ret;
4720     GST_OBJECT_UNLOCK (pad);
4721     GST_PAD_STREAM_UNLOCK (pad);
4722     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4723         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4724         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4725     return ret;
4726   }
4727 }
4728
4729 /**
4730  * gst_pad_get_range:
4731  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4732  * @offset: The start offset of the buffer
4733  * @size: The length of the buffer
4734  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4735  *     returns #GST_FLOW_ERROR if %NULL.
4736  *
4737  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4738  * immediately and @buffer is %NULL.
4739  *
4740  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4741  * description of a getrange function. If @pad has no getrange function
4742  * installed (see gst_pad_set_getrange_function()) this function returns
4743  * #GST_FLOW_NOT_SUPPORTED.
4744  *
4745  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4746  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4747  * must be freed with gst_buffer_unref() after usage.
4748  *
4749  * When @buffer points to a variable that points to a valid #GstBuffer, the
4750  * buffer will be filled with the result data when this function returns
4751  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4752  * @size bytes will be filled in the result buffer and its size will be updated
4753  * accordingly.
4754  *
4755  * Note that less than @size bytes can be returned in @buffer when, for example,
4756  * an EOS condition is near or when @buffer is not large enough to hold @size
4757  * bytes. The caller should check the result buffer size to get the result size.
4758  *
4759  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4760  * will be unchanged.
4761  *
4762  * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4763  *
4764  * Returns: a #GstFlowReturn from the pad.
4765  *
4766  * MT safe.
4767  */
4768 GstFlowReturn
4769 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4770     GstBuffer ** buffer)
4771 {
4772   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4773   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4774   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4775   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4776           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4777
4778   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4779 }
4780
4781 /**
4782  * gst_pad_pull_range:
4783  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4784  * @offset: The start offset of the buffer
4785  * @size: The length of the buffer
4786  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4787  *     GST_FLOW_ERROR if %NULL.
4788  *
4789  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4790  *
4791  * This function will first trigger the pad block signal if it was
4792  * installed.
4793  *
4794  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4795  * function returns the result of gst_pad_get_range() on the peer pad.
4796  * See gst_pad_get_range() for a list of return values and for the
4797  * semantics of the arguments of this function.
4798  *
4799  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4800  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4801  * must be freed with gst_buffer_unref() after usage. When this function
4802  * returns any other result value, @buffer will still point to %NULL.
4803  *
4804  * When @buffer points to a variable that points to a valid #GstBuffer, the
4805  * buffer will be filled with the result data when this function returns
4806  * #GST_FLOW_OK. When this function returns any other result value,
4807  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4808  * @size bytes will be filled in the result buffer and its size will be updated
4809  * accordingly.
4810  *
4811  * Note that less than @size bytes can be returned in @buffer when, for example,
4812  * an EOS condition is near or when @buffer is not large enough to hold @size
4813  * bytes. The caller should check the result buffer size to get the result size.
4814  *
4815  * Returns: a #GstFlowReturn from the peer pad.
4816  *
4817  * MT safe.
4818  */
4819 GstFlowReturn
4820 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4821     GstBuffer ** buffer)
4822 {
4823   GstPad *peer;
4824   GstFlowReturn ret;
4825   GstBuffer *res_buf;
4826
4827   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4828   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4829   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4830   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4831           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4832
4833   GST_TRACER_PAD_PULL_RANGE_PRE (pad, offset, size);
4834
4835   GST_OBJECT_LOCK (pad);
4836   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4837     goto flushing;
4838
4839   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4840     goto wrong_mode;
4841
4842   res_buf = *buffer;
4843
4844   /* when one of the probes returns DROPPED, probe_stopped will be
4845    * called and we skip calling the peer getrange function. *buffer should then
4846    * contain a valid buffer */
4847   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4848       res_buf, offset, size, probe_stopped);
4849
4850   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4851     goto not_linked;
4852
4853   gst_object_ref (peer);
4854   pad->priv->using++;
4855   GST_OBJECT_UNLOCK (pad);
4856
4857   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4858
4859   gst_object_unref (peer);
4860
4861   GST_OBJECT_LOCK (pad);
4862   pad->priv->using--;
4863   pad->ABI.abi.last_flowret = ret;
4864   if (pad->priv->using == 0) {
4865     /* pad is not active anymore, trigger idle callbacks */
4866     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4867         probe_stopped_unref, ret);
4868   }
4869
4870   if (G_UNLIKELY (ret != GST_FLOW_OK))
4871     goto pull_range_failed;
4872
4873 probed_data:
4874   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4875       res_buf, offset, size, probe_stopped_unref);
4876
4877   GST_OBJECT_UNLOCK (pad);
4878
4879   *buffer = res_buf;
4880
4881   GST_TRACER_PAD_PULL_RANGE_POST (pad, *buffer, ret);
4882   return ret;
4883
4884   /* ERROR recovery here */
4885 flushing:
4886   {
4887     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4888         "pullrange, but pad was flushing");
4889     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4890     GST_OBJECT_UNLOCK (pad);
4891     ret = GST_FLOW_FLUSHING;
4892     goto done;
4893   }
4894 wrong_mode:
4895   {
4896     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4897         GST_DEBUG_PAD_NAME (pad));
4898     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4899     GST_OBJECT_UNLOCK (pad);
4900     ret = GST_FLOW_ERROR;
4901     goto done;
4902   }
4903 probe_stopped:
4904   {
4905     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4906         gst_flow_get_name (ret));
4907     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4908       if (res_buf) {
4909         /* the probe filled the buffer and asks us to not forward to the peer
4910          * anymore, we continue with the post probes then */
4911         GST_DEBUG_OBJECT (pad, "handled buffer");
4912         ret = GST_FLOW_OK;
4913         goto probed_data;
4914       } else {
4915         /* no buffer, we are EOS then */
4916         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4917         ret = GST_FLOW_EOS;
4918       }
4919     }
4920     pad->ABI.abi.last_flowret = ret;
4921     GST_OBJECT_UNLOCK (pad);
4922     goto done;
4923   }
4924 not_linked:
4925   {
4926     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4927         "pulling range, but it was not linked");
4928     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4929     GST_OBJECT_UNLOCK (pad);
4930     ret = GST_FLOW_NOT_LINKED;
4931     goto done;
4932   }
4933 pull_range_failed:
4934   {
4935     pad->ABI.abi.last_flowret = ret;
4936     GST_OBJECT_UNLOCK (pad);
4937     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4938         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4939         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4940     goto done;
4941   }
4942 probe_stopped_unref:
4943   {
4944     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4945         "post probe returned %s", gst_flow_get_name (ret));
4946
4947     /* if we drop here, it signals EOS */
4948     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4949       ret = GST_FLOW_EOS;
4950
4951     pad->ABI.abi.last_flowret = ret;
4952     GST_OBJECT_UNLOCK (pad);
4953
4954     if (*buffer == NULL)
4955       gst_buffer_unref (res_buf);
4956     goto done;
4957   }
4958 done:
4959   GST_TRACER_PAD_PULL_RANGE_POST (pad, NULL, ret);
4960   return ret;
4961 }
4962
4963 /* must be called with pad object lock */
4964 static GstFlowReturn
4965 store_sticky_event (GstPad * pad, GstEvent * event)
4966 {
4967   guint i, len;
4968   GstEventType type;
4969   GArray *events;
4970   gboolean res = FALSE;
4971   const gchar *name = NULL;
4972   gboolean insert = TRUE;
4973
4974   type = GST_EVENT_TYPE (event);
4975
4976   /* Store all sticky events except SEGMENT/EOS when we're flushing,
4977    * otherwise they can be dropped and nothing would ever resend them.
4978    * Only do that for activated pads though, everything else is a bug!
4979    */
4980   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4981           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4982                   || type == GST_EVENT_EOS))))
4983     goto flushed;
4984
4985   /* Unset the EOS flag when received STREAM_START event, so pad can
4986    * store sticky event and then push it later */
4987   if (type == GST_EVENT_STREAM_START) {
4988     GST_LOG_OBJECT (pad, "Removing pending EOS events");
4989     remove_event_by_type (pad, GST_EVENT_EOS);
4990     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4991   }
4992
4993   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4994     goto eos;
4995
4996   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4997     name = gst_structure_get_name (gst_event_get_structure (event));
4998
4999   events = pad->priv->events;
5000   len = events->len;
5001
5002   for (i = 0; i < len; i++) {
5003     PadEvent *ev = &g_array_index (events, PadEvent, i);
5004
5005     if (ev->event == NULL)
5006       continue;
5007
5008     if (type == GST_EVENT_TYPE (ev->event)) {
5009       /* matching types, check matching name if needed */
5010       if (name && !gst_event_has_name (ev->event, name))
5011         continue;
5012
5013       /* overwrite */
5014       if ((res = gst_event_replace (&ev->event, event)))
5015         ev->received = FALSE;
5016
5017       insert = FALSE;
5018       break;
5019     }
5020
5021     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
5022             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
5023       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
5024        * storing the sticky ordered we can check that this is respected. */
5025       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
5026               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
5027         g_warning (G_STRLOC
5028             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
5029             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
5030             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
5031             gst_event_type_get_name (type));
5032       break;
5033     }
5034   }
5035   if (insert) {
5036     PadEvent ev;
5037     ev.event = gst_event_ref (event);
5038     ev.received = FALSE;
5039     g_array_insert_val (events, i, ev);
5040     res = TRUE;
5041   }
5042
5043   if (res) {
5044     pad->priv->events_cookie++;
5045     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5046
5047     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
5048
5049     switch (GST_EVENT_TYPE (event)) {
5050       case GST_EVENT_CAPS:
5051         GST_OBJECT_UNLOCK (pad);
5052
5053         GST_DEBUG_OBJECT (pad, "notify caps");
5054         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
5055
5056         GST_OBJECT_LOCK (pad);
5057         break;
5058       default:
5059         break;
5060     }
5061   }
5062   if (type == GST_EVENT_EOS) {
5063     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
5064     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
5065   }
5066
5067   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
5068
5069   /* ERRORS */
5070 flushed:
5071   {
5072     GST_DEBUG_OBJECT (pad, "pad is flushing");
5073     return GST_FLOW_FLUSHING;
5074   }
5075 eos:
5076   {
5077     GST_DEBUG_OBJECT (pad, "pad is EOS");
5078     return GST_FLOW_EOS;
5079   }
5080 }
5081
5082 /**
5083  * gst_pad_store_sticky_event:
5084  * @pad: a #GstPad
5085  * @event: a #GstEvent
5086  *
5087  * Store the sticky @event on @pad
5088  *
5089  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5090  * was flushing or #GST_FLOW_EOS when the pad was EOS.
5091  *
5092  * Since: 1.2
5093  */
5094 GstFlowReturn
5095 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5096 {
5097   GstFlowReturn ret;
5098
5099   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5100   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5101
5102   GST_OBJECT_LOCK (pad);
5103   ret = store_sticky_event (pad, event);
5104   GST_OBJECT_UNLOCK (pad);
5105
5106   return ret;
5107 }
5108
5109 static gboolean
5110 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5111 {
5112   PushStickyData *data = user_data;
5113
5114   /* Forward all sticky events before our current one that are pending */
5115   if (ev->event != data->event
5116       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5117     return push_sticky (pad, ev, data);
5118
5119   return TRUE;
5120 }
5121
5122 /* should be called with pad LOCK */
5123 static GstFlowReturn
5124 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5125     GstPadProbeType type)
5126 {
5127   GstFlowReturn ret;
5128   GstPad *peerpad;
5129   GstEventType event_type;
5130
5131   /* pass the adjusted event on. We need to do this even if
5132    * there is no peer pad because of the probes. */
5133   event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5134
5135   /* Two checks to be made:
5136    * . (un)set the FLUSHING flag for flushing events,
5137    * . handle pad blocking */
5138   event_type = GST_EVENT_TYPE (event);
5139   switch (event_type) {
5140     case GST_EVENT_FLUSH_START:
5141       GST_PAD_SET_FLUSHING (pad);
5142
5143       GST_PAD_BLOCK_BROADCAST (pad);
5144       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5145       break;
5146     case GST_EVENT_FLUSH_STOP:
5147       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5148         goto inactive;
5149
5150       GST_PAD_UNSET_FLUSHING (pad);
5151
5152       /* Remove sticky EOS events */
5153       GST_LOG_OBJECT (pad, "Removing pending EOS events");
5154       remove_event_by_type (pad, GST_EVENT_EOS);
5155       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5156       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5157       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5158
5159       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5160       break;
5161     default:
5162     {
5163       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5164         goto flushed;
5165
5166       /* No need to check for EOS here as either the caller (gst_pad_push_event())
5167        * checked already or this is called as part of pushing sticky events,
5168        * in which case we still want to forward the EOS event downstream.
5169        */
5170
5171       switch (GST_EVENT_TYPE (event)) {
5172         case GST_EVENT_RECONFIGURE:
5173           if (GST_PAD_IS_SINK (pad))
5174             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5175           break;
5176         default:
5177           break;
5178       }
5179       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5180           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5181       break;
5182     }
5183   }
5184
5185   /* send probes after modifying the events above */
5186   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5187
5188   /* recheck sticky events because the probe might have cause a relink */
5189   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5190       && (GST_EVENT_IS_SERIALIZED (event)
5191           || GST_EVENT_IS_STICKY (event))) {
5192     PushStickyData data = { GST_FLOW_OK, FALSE, event };
5193     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5194
5195     /* Push all sticky events before our current one
5196      * that have changed */
5197     events_foreach (pad, sticky_changed, &data);
5198   }
5199
5200   /* now check the peer pad */
5201   peerpad = GST_PAD_PEER (pad);
5202   if (peerpad == NULL)
5203     goto not_linked;
5204
5205   gst_object_ref (peerpad);
5206   pad->priv->using++;
5207   GST_OBJECT_UNLOCK (pad);
5208
5209   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5210       event, gst_event_type_get_name (event_type), peerpad);
5211
5212   ret = gst_pad_send_event_unchecked (peerpad, event, type);
5213
5214   /* Note: we gave away ownership of the event at this point but we can still
5215    * print the old pointer */
5216   GST_LOG_OBJECT (pad,
5217       "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5218       gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5219
5220   gst_object_unref (peerpad);
5221
5222   GST_OBJECT_LOCK (pad);
5223   pad->priv->using--;
5224   if (pad->priv->using == 0) {
5225     /* pad is not active anymore, trigger idle callbacks */
5226     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5227         idle_probe_stopped, ret);
5228   }
5229   return ret;
5230
5231   /* ERROR handling */
5232 flushed:
5233   {
5234     GST_DEBUG_OBJECT (pad, "We're flushing");
5235     gst_event_unref (event);
5236     return GST_FLOW_FLUSHING;
5237   }
5238 inactive:
5239   {
5240     GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5241     gst_event_unref (event);
5242     return GST_FLOW_FLUSHING;
5243   }
5244 probe_stopped:
5245   {
5246     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5247     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5248       gst_event_unref (event);
5249
5250     switch (ret) {
5251       case GST_FLOW_CUSTOM_SUCCESS_1:
5252         GST_DEBUG_OBJECT (pad, "handled event");
5253         break;
5254       case GST_FLOW_CUSTOM_SUCCESS:
5255         GST_DEBUG_OBJECT (pad, "dropped event");
5256         break;
5257       default:
5258         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5259         break;
5260     }
5261     return ret;
5262   }
5263 not_linked:
5264   {
5265     GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5266         gst_event_type_get_name (GST_EVENT_TYPE (event)));
5267     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5268     gst_event_unref (event);
5269
5270     /* unlinked pads should not influence latency configuration */
5271     if (event_type == GST_EVENT_LATENCY)
5272       return GST_FLOW_OK;
5273
5274     return GST_FLOW_NOT_LINKED;
5275   }
5276 idle_probe_stopped:
5277   {
5278     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5279     return ret;
5280   }
5281 }
5282
5283 /**
5284  * gst_pad_push_event:
5285  * @pad: a #GstPad to push the event to.
5286  * @event: (transfer full): the #GstEvent to send to the pad.
5287  *
5288  * Sends the event to the peer of the given pad. This function is
5289  * mainly used by elements to send events to their peer
5290  * elements.
5291  *
5292  * This function takes ownership of the provided event so you should
5293  * gst_event_ref() it if you want to reuse the event after this call.
5294  *
5295  * Returns: %TRUE if the event was handled.
5296  *
5297  * MT safe.
5298  */
5299 gboolean
5300 gst_pad_push_event (GstPad * pad, GstEvent * event)
5301 {
5302   gboolean res = FALSE;
5303   GstPadProbeType type;
5304   gboolean sticky, serialized;
5305
5306   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5307   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5308
5309   GST_TRACER_PAD_PUSH_EVENT_PRE (pad, event);
5310
5311   if (GST_PAD_IS_SRC (pad)) {
5312     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5313       goto wrong_direction;
5314     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5315   } else if (GST_PAD_IS_SINK (pad)) {
5316     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5317       goto wrong_direction;
5318     /* events pushed on sinkpad never are sticky */
5319     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5320   } else
5321     goto unknown_direction;
5322
5323   GST_OBJECT_LOCK (pad);
5324   sticky = GST_EVENT_IS_STICKY (event);
5325   serialized = GST_EVENT_IS_SERIALIZED (event);
5326
5327   if (sticky) {
5328     /* srcpad sticky events are stored immediately, the received flag is set
5329      * to FALSE and will be set to TRUE when we can successfully push the
5330      * event to the peer pad */
5331     switch (store_sticky_event (pad, event)) {
5332       case GST_FLOW_FLUSHING:
5333         goto flushed;
5334       case GST_FLOW_EOS:
5335         goto eos;
5336       default:
5337         break;
5338     }
5339   }
5340   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5341     /* all serialized or sticky events on the srcpad trigger push of
5342      * sticky events */
5343     res = (check_sticky (pad, event) == GST_FLOW_OK);
5344   }
5345   if (!sticky) {
5346     GstFlowReturn ret;
5347
5348     /* other events are pushed right away */
5349     ret = gst_pad_push_event_unchecked (pad, event, type);
5350     /* dropped events by a probe are not an error */
5351     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5352         || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5353   } else {
5354     /* Errors in sticky event pushing are no problem and ignored here
5355      * as they will cause more meaningful errors during data flow.
5356      * For EOS events, that are not followed by data flow, we still
5357      * return FALSE here though.
5358      */
5359     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5360       res = TRUE;
5361     gst_event_unref (event);
5362   }
5363   GST_OBJECT_UNLOCK (pad);
5364
5365   GST_TRACER_PAD_PUSH_EVENT_POST (pad, res);
5366   return res;
5367
5368   /* ERROR handling */
5369 wrong_direction:
5370   {
5371     g_warning ("pad %s:%s pushing %s event in wrong direction",
5372         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5373     gst_event_unref (event);
5374     goto done;
5375   }
5376 unknown_direction:
5377   {
5378     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5379     gst_event_unref (event);
5380     goto done;
5381   }
5382 flushed:
5383   {
5384     GST_DEBUG_OBJECT (pad, "We're flushing");
5385     GST_OBJECT_UNLOCK (pad);
5386     gst_event_unref (event);
5387     goto done;
5388   }
5389 eos:
5390   {
5391     GST_DEBUG_OBJECT (pad, "We're EOS");
5392     GST_OBJECT_UNLOCK (pad);
5393     gst_event_unref (event);
5394     goto done;
5395   }
5396 done:
5397   GST_TRACER_PAD_PUSH_EVENT_POST (pad, FALSE);
5398   return FALSE;
5399 }
5400
5401 /* Check if we can call the event function with the given event */
5402 static GstFlowReturn
5403 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5404 {
5405   GstCaps *caps;
5406
5407   switch (GST_EVENT_TYPE (event)) {
5408     case GST_EVENT_CAPS:
5409     {
5410       /* backwards compatibility mode for caps */
5411       gst_event_parse_caps (event, &caps);
5412
5413       if (!gst_pad_query_accept_caps (pad, caps))
5414         goto not_accepted;
5415       break;
5416     }
5417     default:
5418       break;
5419   }
5420   return GST_FLOW_OK;
5421
5422   /* ERRORS */
5423 not_accepted:
5424   {
5425     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5426         "caps %" GST_PTR_FORMAT " not accepted", caps);
5427     return GST_FLOW_NOT_NEGOTIATED;
5428   }
5429 }
5430
5431 static GstFlowReturn
5432 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5433     GstPadProbeType type)
5434 {
5435   GstFlowReturn ret;
5436   GstEventType event_type;
5437   gboolean serialized, need_unlock = FALSE, sticky;
5438   GstPadEventFunction eventfunc;
5439   GstPadEventFullFunction eventfullfunc = NULL;
5440   GstObject *parent;
5441
5442   GST_OBJECT_LOCK (pad);
5443
5444   event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5445
5446   if (GST_PAD_IS_SINK (pad))
5447     serialized = GST_EVENT_IS_SERIALIZED (event);
5448   else
5449     serialized = FALSE;
5450   sticky = GST_EVENT_IS_STICKY (event);
5451   event_type = GST_EVENT_TYPE (event);
5452   switch (event_type) {
5453     case GST_EVENT_FLUSH_START:
5454       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5455           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5456
5457       /* can't even accept a flush begin event when flushing */
5458       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5459         goto flushing;
5460
5461       GST_PAD_SET_FLUSHING (pad);
5462       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5463       GST_PAD_BLOCK_BROADCAST (pad);
5464       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5465       break;
5466     case GST_EVENT_FLUSH_STOP:
5467       /* we can't accept flush-stop on inactive pads else the flushing flag
5468        * would be cleared and it would look like the pad can accept data.
5469        * Also, some elements restart a streaming thread in flush-stop which we
5470        * can't allow on inactive pads */
5471       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5472         goto inactive;
5473
5474       GST_PAD_UNSET_FLUSHING (pad);
5475       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5476       /* Remove pending EOS events */
5477       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5478       remove_event_by_type (pad, GST_EVENT_EOS);
5479       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5480       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5481       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5482
5483       GST_OBJECT_UNLOCK (pad);
5484       /* grab stream lock */
5485       GST_PAD_STREAM_LOCK (pad);
5486       need_unlock = TRUE;
5487       GST_OBJECT_LOCK (pad);
5488       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5489         goto flushing;
5490       break;
5491     case GST_EVENT_RECONFIGURE:
5492       if (GST_PAD_IS_SRC (pad))
5493         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5494     default:
5495       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5496           "have event type %" GST_PTR_FORMAT, event);
5497
5498       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5499         goto flushing;
5500
5501       switch (event_type) {
5502         case GST_EVENT_STREAM_START:
5503           /* Remove sticky EOS events */
5504           GST_LOG_OBJECT (pad, "Removing pending EOS events");
5505           remove_event_by_type (pad, GST_EVENT_EOS);
5506           GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5507           break;
5508         default:
5509           break;
5510       }
5511
5512       if (serialized) {
5513         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5514           goto eos;
5515
5516         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5517         GST_OBJECT_UNLOCK (pad);
5518         GST_PAD_STREAM_LOCK (pad);
5519         need_unlock = TRUE;
5520         GST_OBJECT_LOCK (pad);
5521         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5522           goto flushing;
5523
5524         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5525           goto eos;
5526       }
5527       break;
5528   }
5529
5530   /* now do the probe */
5531   PROBE_PUSH (pad,
5532       type | GST_PAD_PROBE_TYPE_PUSH |
5533       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5534
5535   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5536
5537   eventfullfunc = GST_PAD_EVENTFULLFUNC (pad);
5538   eventfunc = GST_PAD_EVENTFUNC (pad);
5539   if (G_UNLIKELY (eventfunc == NULL && eventfullfunc == NULL))
5540     goto no_function;
5541
5542   ACQUIRE_PARENT (pad, parent, no_parent);
5543   GST_OBJECT_UNLOCK (pad);
5544
5545   ret = pre_eventfunc_check (pad, event);
5546   if (G_UNLIKELY (ret != GST_FLOW_OK))
5547     goto precheck_failed;
5548
5549   if (sticky)
5550     gst_event_ref (event);
5551
5552   if (eventfullfunc) {
5553     ret = eventfullfunc (pad, parent, event);
5554   } else if (eventfunc (pad, parent, event)) {
5555     ret = GST_FLOW_OK;
5556   } else {
5557     /* something went wrong */
5558     switch (event_type) {
5559       case GST_EVENT_CAPS:
5560         ret = GST_FLOW_NOT_NEGOTIATED;
5561         break;
5562       default:
5563         ret = GST_FLOW_ERROR;
5564         break;
5565     }
5566   }
5567   RELEASE_PARENT (parent);
5568
5569   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5570
5571   if (sticky) {
5572     if (ret == GST_FLOW_OK) {
5573       GST_OBJECT_LOCK (pad);
5574       /* after the event function accepted the event, we can store the sticky
5575        * event on the pad */
5576       switch (store_sticky_event (pad, event)) {
5577         case GST_FLOW_FLUSHING:
5578           goto flushing;
5579         case GST_FLOW_EOS:
5580           goto eos;
5581         default:
5582           break;
5583       }
5584       GST_OBJECT_UNLOCK (pad);
5585     }
5586     gst_event_unref (event);
5587   }
5588
5589   if (need_unlock)
5590     GST_PAD_STREAM_UNLOCK (pad);
5591
5592   return ret;
5593
5594   /* ERROR handling */
5595 flushing:
5596   {
5597     GST_OBJECT_UNLOCK (pad);
5598     if (need_unlock)
5599       GST_PAD_STREAM_UNLOCK (pad);
5600     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5601         "Received event on flushing pad. Discarding");
5602     gst_event_unref (event);
5603     return GST_FLOW_FLUSHING;
5604   }
5605 inactive:
5606   {
5607     GST_OBJECT_UNLOCK (pad);
5608     if (need_unlock)
5609       GST_PAD_STREAM_UNLOCK (pad);
5610     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5611         "Received flush-stop on inactive pad. Discarding");
5612     gst_event_unref (event);
5613     return GST_FLOW_FLUSHING;
5614   }
5615 eos:
5616   {
5617     GST_OBJECT_UNLOCK (pad);
5618     if (need_unlock)
5619       GST_PAD_STREAM_UNLOCK (pad);
5620     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5621         "Received event on EOS pad. Discarding");
5622     gst_event_unref (event);
5623     return GST_FLOW_EOS;
5624   }
5625 probe_stopped:
5626   {
5627     GST_OBJECT_UNLOCK (pad);
5628     if (need_unlock)
5629       GST_PAD_STREAM_UNLOCK (pad);
5630     /* Only unref if unhandled */
5631     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5632       gst_event_unref (event);
5633
5634     switch (ret) {
5635       case GST_FLOW_CUSTOM_SUCCESS_1:
5636       case GST_FLOW_CUSTOM_SUCCESS:
5637         GST_DEBUG_OBJECT (pad, "dropped or handled event");
5638         ret = GST_FLOW_OK;
5639         break;
5640       default:
5641         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5642         break;
5643     }
5644     return ret;
5645   }
5646 no_function:
5647   {
5648     g_warning ("pad %s:%s has no event handler, file a bug.",
5649         GST_DEBUG_PAD_NAME (pad));
5650     GST_OBJECT_UNLOCK (pad);
5651     if (need_unlock)
5652       GST_PAD_STREAM_UNLOCK (pad);
5653     gst_event_unref (event);
5654     return GST_FLOW_NOT_SUPPORTED;
5655   }
5656 no_parent:
5657   {
5658     GST_DEBUG_OBJECT (pad, "no parent");
5659     GST_OBJECT_UNLOCK (pad);
5660     if (need_unlock)
5661       GST_PAD_STREAM_UNLOCK (pad);
5662     gst_event_unref (event);
5663     return GST_FLOW_FLUSHING;
5664   }
5665 precheck_failed:
5666   {
5667     GST_DEBUG_OBJECT (pad, "pre event check failed");
5668     RELEASE_PARENT (parent);
5669     if (need_unlock)
5670       GST_PAD_STREAM_UNLOCK (pad);
5671     gst_event_unref (event);
5672     return ret;
5673   }
5674 }
5675
5676 /**
5677  * gst_pad_send_event:
5678  * @pad: a #GstPad to send the event to.
5679  * @event: (transfer full): the #GstEvent to send to the pad.
5680  *
5681  * Sends the event to the pad. This function can be used
5682  * by applications to send events in the pipeline.
5683  *
5684  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5685  * sink pad, @event should be a downstream event. For example, you would not
5686  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5687  * Furthermore, some downstream events have to be serialized with data flow,
5688  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5689  * the event needs to be serialized with data flow, this function will take the
5690  * pad's stream lock while calling its event function.
5691  *
5692  * To find out whether an event type is upstream, downstream, or downstream and
5693  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5694  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5695  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5696  * plugin doesn't need to bother itself with this information; the core handles
5697  * all necessary locks and checks.
5698  *
5699  * This function takes ownership of the provided event so you should
5700  * gst_event_ref() it if you want to reuse the event after this call.
5701  *
5702  * Returns: %TRUE if the event was handled.
5703  */
5704 gboolean
5705 gst_pad_send_event (GstPad * pad, GstEvent * event)
5706 {
5707   gboolean result;
5708   GstPadProbeType type;
5709
5710   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5711   g_return_val_if_fail (event != NULL, FALSE);
5712
5713   if (GST_PAD_IS_SINK (pad)) {
5714     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5715       goto wrong_direction;
5716     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5717   } else if (GST_PAD_IS_SRC (pad)) {
5718     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5719       goto wrong_direction;
5720     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5721   } else
5722     goto unknown_direction;
5723
5724   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5725     result = FALSE;
5726   else
5727     result = TRUE;
5728
5729   return result;
5730
5731   /* ERROR handling */
5732 wrong_direction:
5733   {
5734     g_warning ("pad %s:%s sending %s event in wrong direction",
5735         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5736     gst_event_unref (event);
5737     return FALSE;
5738   }
5739 unknown_direction:
5740   {
5741     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5742     gst_event_unref (event);
5743     return FALSE;
5744   }
5745 }
5746
5747 /**
5748  * gst_pad_set_element_private:
5749  * @pad: the #GstPad to set the private data of.
5750  * @priv: The private data to attach to the pad.
5751  *
5752  * Set the given private data gpointer on the pad.
5753  * This function can only be used by the element that owns the pad.
5754  * No locking is performed in this function.
5755  */
5756 void
5757 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5758 {
5759   pad->element_private = priv;
5760 }
5761
5762 /**
5763  * gst_pad_get_element_private:
5764  * @pad: the #GstPad to get the private data of.
5765  *
5766  * Gets the private data of a pad.
5767  * No locking is performed in this function.
5768  *
5769  * Returns: (transfer none): a #gpointer to the private data.
5770  */
5771 gpointer
5772 gst_pad_get_element_private (GstPad * pad)
5773 {
5774   return pad->element_private;
5775 }
5776
5777 /**
5778  * gst_pad_get_sticky_event:
5779  * @pad: the #GstPad to get the event from.
5780  * @event_type: the #GstEventType that should be retrieved.
5781  * @idx: the index of the event
5782  *
5783  * Returns a new reference of the sticky event of type @event_type
5784  * from the event.
5785  *
5786  * Returns: (transfer full) (nullable): a #GstEvent of type
5787  * @event_type or %NULL when no event of @event_type was on
5788  * @pad. Unref after usage.
5789  */
5790 GstEvent *
5791 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5792 {
5793   GstEvent *event = NULL;
5794   PadEvent *ev;
5795
5796   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5797   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5798
5799   GST_OBJECT_LOCK (pad);
5800   ev = find_event_by_type (pad, event_type, idx);
5801   if (ev && (event = ev->event))
5802     gst_event_ref (event);
5803   GST_OBJECT_UNLOCK (pad);
5804
5805   return event;
5806 }
5807
5808 typedef struct
5809 {
5810   GstPadStickyEventsForeachFunction func;
5811   gpointer user_data;
5812 } ForeachDispatch;
5813
5814 static gboolean
5815 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5816 {
5817   ForeachDispatch *data = user_data;
5818   gboolean ret = TRUE;
5819
5820   if (ev->event) {
5821     GST_OBJECT_UNLOCK (pad);
5822
5823     ret = data->func (pad, &ev->event, data->user_data);
5824
5825     GST_OBJECT_LOCK (pad);
5826   }
5827
5828   return ret;
5829 }
5830
5831 /**
5832  * gst_pad_sticky_events_foreach:
5833  * @pad: the #GstPad that should be used for iteration.
5834  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5835  *                should be called for every event.
5836  * @user_data: (closure): the optional user data.
5837  *
5838  * Iterates all sticky events on @pad and calls @foreach_func for every
5839  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5840  */
5841 void
5842 gst_pad_sticky_events_foreach (GstPad * pad,
5843     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5844 {
5845   ForeachDispatch data;
5846
5847   g_return_if_fail (GST_IS_PAD (pad));
5848   g_return_if_fail (foreach_func != NULL);
5849
5850   data.func = foreach_func;
5851   data.user_data = user_data;
5852
5853   GST_OBJECT_LOCK (pad);
5854   events_foreach (pad, foreach_dispatch_function, &data);
5855   GST_OBJECT_UNLOCK (pad);
5856 }
5857
5858 static void
5859 do_stream_status (GstPad * pad, GstStreamStatusType type,
5860     GThread * thread, GstTask * task)
5861 {
5862   GstElement *parent;
5863
5864   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5865
5866   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5867     if (GST_IS_ELEMENT (parent)) {
5868       GstMessage *message;
5869       GValue value = { 0 };
5870
5871       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5872         gchar *tname, *ename, *pname;
5873
5874         /* create a good task name */
5875         ename = gst_element_get_name (parent);
5876         pname = gst_pad_get_name (pad);
5877         tname = g_strdup_printf ("%s:%s", ename, pname);
5878         g_free (ename);
5879         g_free (pname);
5880
5881         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5882         g_free (tname);
5883       }
5884
5885       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5886           type, parent);
5887
5888       g_value_init (&value, GST_TYPE_TASK);
5889       g_value_set_object (&value, task);
5890       gst_message_set_stream_status_object (message, &value);
5891       g_value_unset (&value);
5892
5893       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5894       gst_element_post_message (parent, message);
5895     }
5896     gst_object_unref (parent);
5897   }
5898 }
5899
5900 static void
5901 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5902 {
5903   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5904       thread, task);
5905 }
5906
5907 static void
5908 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5909 {
5910   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5911       thread, task);
5912 }
5913
5914 /**
5915  * gst_pad_start_task:
5916  * @pad: the #GstPad to start the task of
5917  * @func: the task function to call
5918  * @user_data: user data passed to the task function
5919  * @notify: called when @user_data is no longer referenced
5920  *
5921  * Starts a task that repeatedly calls @func with @user_data. This function
5922  * is mostly used in pad activation functions to start the dataflow.
5923  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5924  * before @func is called.
5925  *
5926  * Returns: a %TRUE if the task could be started.
5927  */
5928 gboolean
5929 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5930     GDestroyNotify notify)
5931 {
5932   GstTask *task;
5933   gboolean res;
5934
5935   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5936   g_return_val_if_fail (func != NULL, FALSE);
5937
5938   GST_DEBUG_OBJECT (pad, "start task");
5939
5940   GST_OBJECT_LOCK (pad);
5941   task = GST_PAD_TASK (pad);
5942   if (task == NULL) {
5943     task = gst_task_new (func, user_data, notify);
5944     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5945     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5946     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5947     GST_INFO_OBJECT (pad, "created task %p", task);
5948     GST_PAD_TASK (pad) = task;
5949     gst_object_ref (task);
5950     /* release lock to post the message */
5951     GST_OBJECT_UNLOCK (pad);
5952
5953     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5954
5955     gst_object_unref (task);
5956
5957     GST_OBJECT_LOCK (pad);
5958     /* nobody else is supposed to have changed the pad now */
5959     if (GST_PAD_TASK (pad) != task)
5960       goto concurrent_stop;
5961   }
5962   res = gst_task_set_state (task, GST_TASK_STARTED);
5963   GST_OBJECT_UNLOCK (pad);
5964
5965   return res;
5966
5967   /* ERRORS */
5968 concurrent_stop:
5969   {
5970     GST_OBJECT_UNLOCK (pad);
5971     return TRUE;
5972   }
5973 }
5974
5975 /**
5976  * gst_pad_pause_task:
5977  * @pad: the #GstPad to pause the task of
5978  *
5979  * Pause the task of @pad. This function will also wait until the
5980  * function executed by the task is finished if this function is not
5981  * called from the task function.
5982  *
5983  * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5984  * has no task.
5985  */
5986 gboolean
5987 gst_pad_pause_task (GstPad * pad)
5988 {
5989   GstTask *task;
5990   gboolean res;
5991
5992   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5993
5994   GST_DEBUG_OBJECT (pad, "pause task");
5995
5996   GST_OBJECT_LOCK (pad);
5997   task = GST_PAD_TASK (pad);
5998   if (task == NULL)
5999     goto no_task;
6000   res = gst_task_set_state (task, GST_TASK_PAUSED);
6001   GST_OBJECT_UNLOCK (pad);
6002
6003   /* wait for task function to finish, this lock is recursive so it does nothing
6004    * when the pause is called from the task itself */
6005   GST_PAD_STREAM_LOCK (pad);
6006   GST_PAD_STREAM_UNLOCK (pad);
6007
6008   return res;
6009
6010 no_task:
6011   {
6012     GST_DEBUG_OBJECT (pad, "pad has no task");
6013     GST_OBJECT_UNLOCK (pad);
6014     return FALSE;
6015   }
6016 }
6017
6018 /**
6019  * gst_pad_stop_task:
6020  * @pad: the #GstPad to stop the task of
6021  *
6022  * Stop the task of @pad. This function will also make sure that the
6023  * function executed by the task will effectively stop if not called
6024  * from the GstTaskFunction.
6025  *
6026  * This function will deadlock if called from the GstTaskFunction of
6027  * the task. Use gst_task_pause() instead.
6028  *
6029  * Regardless of whether the pad has a task, the stream lock is acquired and
6030  * released so as to ensure that streaming through this pad has finished.
6031  *
6032  * Returns: a %TRUE if the task could be stopped or %FALSE on error.
6033  */
6034 gboolean
6035 gst_pad_stop_task (GstPad * pad)
6036 {
6037   GstTask *task;
6038   gboolean res;
6039
6040   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6041
6042   GST_DEBUG_OBJECT (pad, "stop task");
6043
6044   GST_OBJECT_LOCK (pad);
6045   task = GST_PAD_TASK (pad);
6046   if (task == NULL)
6047     goto no_task;
6048   GST_PAD_TASK (pad) = NULL;
6049   res = gst_task_set_state (task, GST_TASK_STOPPED);
6050   GST_OBJECT_UNLOCK (pad);
6051
6052   GST_PAD_STREAM_LOCK (pad);
6053   GST_PAD_STREAM_UNLOCK (pad);
6054
6055   if (!gst_task_join (task))
6056     goto join_failed;
6057
6058   gst_object_unref (task);
6059
6060   return res;
6061
6062 no_task:
6063   {
6064     GST_DEBUG_OBJECT (pad, "no task");
6065     GST_OBJECT_UNLOCK (pad);
6066
6067     GST_PAD_STREAM_LOCK (pad);
6068     GST_PAD_STREAM_UNLOCK (pad);
6069
6070     /* this is not an error */
6071     return TRUE;
6072   }
6073 join_failed:
6074   {
6075     /* this is bad, possibly the application tried to join the task from
6076      * the task's thread. We install the task again so that it will be stopped
6077      * again from the right thread next time hopefully. */
6078     GST_OBJECT_LOCK (pad);
6079     GST_DEBUG_OBJECT (pad, "join failed");
6080     /* we can only install this task if there was no other task */
6081     if (GST_PAD_TASK (pad) == NULL)
6082       GST_PAD_TASK (pad) = task;
6083     GST_OBJECT_UNLOCK (pad);
6084
6085     return FALSE;
6086   }
6087 }
6088
6089 /**
6090  * gst_pad_probe_info_get_event:
6091  * @info: a #GstPadProbeInfo
6092  *
6093  * Returns: (transfer none): The #GstEvent from the probe
6094  */
6095
6096 GstEvent *
6097 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6098 {
6099   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6100           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6101
6102   return GST_PAD_PROBE_INFO_EVENT (info);
6103 }
6104
6105
6106 /**
6107  * gst_pad_probe_info_get_query:
6108  * @info: a #GstPadProbeInfo
6109  *
6110  * Returns: (transfer none): The #GstQuery from the probe
6111  */
6112
6113 GstQuery *
6114 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6115 {
6116   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6117           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6118
6119   return GST_PAD_PROBE_INFO_QUERY (info);
6120 }
6121
6122 /**
6123  * gst_pad_probe_info_get_buffer:
6124  * @info: a #GstPadProbeInfo
6125  *
6126  * Returns: (transfer none): The #GstBuffer from the probe
6127  */
6128
6129 GstBuffer *
6130 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6131 {
6132   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6133
6134   return GST_PAD_PROBE_INFO_BUFFER (info);
6135 }
6136
6137 /**
6138  * gst_pad_probe_info_get_buffer_list:
6139  * @info: a #GstPadProbeInfo
6140  *
6141  * Returns: (transfer none): The #GstBufferList from the probe
6142  */
6143
6144 GstBufferList *
6145 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6146 {
6147   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6148
6149   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6150 }
6151
6152 /**
6153  * gst_pad_get_last_flow_return:
6154  * @pad: the #GstPad
6155  *
6156  * Gets the #GstFlowReturn return from the last data passed by this pad.
6157  *
6158  * Since: 1.4
6159  */
6160 GstFlowReturn
6161 gst_pad_get_last_flow_return (GstPad * pad)
6162 {
6163   GstFlowReturn ret;
6164
6165   GST_OBJECT_LOCK (pad);
6166   ret = GST_PAD_LAST_FLOW_RETURN (pad);
6167   GST_OBJECT_UNLOCK (pad);
6168
6169   return ret;
6170 }