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