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