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