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