gstpad: Release pending g_cond_wait() when stopping/pausing task
[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     if (data->ret == GST_FLOW_CUSTOM_SUCCESS_1)
3875       data->ret = GST_FLOW_OK;
3876   }
3877
3878   switch (data->ret) {
3879     case GST_FLOW_OK:
3880       ev->received = TRUE;
3881       GST_DEBUG_OBJECT (pad, "event %s marked received",
3882           GST_EVENT_TYPE_NAME (event));
3883       break;
3884     case GST_FLOW_CUSTOM_SUCCESS:
3885       /* we can't assume the event is received when it was dropped */
3886       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3887           GST_EVENT_TYPE_NAME (event));
3888       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3889       data->ret = GST_FLOW_OK;
3890       break;
3891     case GST_FLOW_CUSTOM_SUCCESS_1:
3892       /* event was ignored and should be sent later */
3893       GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3894           GST_EVENT_TYPE_NAME (event));
3895       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3896       data->ret = GST_FLOW_OK;
3897       break;
3898     case GST_FLOW_NOT_LINKED:
3899       /* not linked is not a problem, we are sticky so the event will be
3900        * rescheduled to be sent later on re-link, but only for non-EOS events */
3901       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3902       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
3903         data->ret = GST_FLOW_OK;
3904         ev->received = TRUE;
3905       }
3906       break;
3907     default:
3908       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3909           gst_flow_get_name (data->ret));
3910       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3911       break;
3912   }
3913
3914   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3915     data->was_eos = TRUE;
3916
3917   return data->ret == GST_FLOW_OK;
3918 }
3919
3920 /* check sticky events and push them when needed. should be called
3921  * with pad LOCK */
3922 static inline GstFlowReturn
3923 check_sticky (GstPad * pad, GstEvent * event)
3924 {
3925   PushStickyData data = { GST_FLOW_OK, FALSE, event };
3926
3927   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3928     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3929
3930     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3931     events_foreach (pad, push_sticky, &data);
3932
3933     /* If there's an EOS event we must push it downstream
3934      * even if sending a previous sticky event failed.
3935      * Otherwise the pipeline might wait forever for EOS.
3936      *
3937      * Only do this if pushing another event than the EOS
3938      * event failed.
3939      */
3940     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3941       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3942
3943       if (ev && !ev->received) {
3944         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3945             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3946         /* the event could have been dropped. Because this can only
3947          * happen if the user asked for it, it's not an error */
3948         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3949           data.ret = GST_FLOW_OK;
3950       }
3951     }
3952   }
3953   return data.ret;
3954 }
3955
3956
3957 /**
3958  * gst_pad_query:
3959  * @pad: a #GstPad to invoke the default query on.
3960  * @query: (transfer none): the #GstQuery to perform.
3961  *
3962  * Dispatches a query to a pad. The query should have been allocated by the
3963  * caller via one of the type-specific allocation functions. The element that
3964  * the pad belongs to is responsible for filling the query with an appropriate
3965  * response, which should then be parsed with a type-specific query parsing
3966  * function.
3967  *
3968  * Again, the caller is responsible for both the allocation and deallocation of
3969  * the query structure.
3970  *
3971  * Please also note that some queries might need a running pipeline to work.
3972  *
3973  * Returns: %TRUE if the query could be performed.
3974  */
3975 gboolean
3976 gst_pad_query (GstPad * pad, GstQuery * query)
3977 {
3978   GstObject *parent;
3979   gboolean res, serialized;
3980   GstPadQueryFunction func;
3981   GstPadProbeType type;
3982   GstFlowReturn ret;
3983
3984   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3985   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3986
3987   if (GST_PAD_IS_SRC (pad)) {
3988     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3989       goto wrong_direction;
3990     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3991   } else if (GST_PAD_IS_SINK (pad)) {
3992     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3993       goto wrong_direction;
3994     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3995   } else
3996     goto unknown_direction;
3997
3998   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3999       GST_QUERY_TYPE_NAME (query));
4000   GST_TRACER_PAD_QUERY_PRE (pad, query);
4001
4002   serialized = GST_QUERY_IS_SERIALIZED (query);
4003   if (G_UNLIKELY (serialized))
4004     GST_PAD_STREAM_LOCK (pad);
4005
4006   GST_OBJECT_LOCK (pad);
4007   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4008       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4009   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4010
4011   ACQUIRE_PARENT (pad, parent, no_parent);
4012   GST_OBJECT_UNLOCK (pad);
4013
4014   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
4015     goto no_func;
4016
4017   res = func (pad, parent, query);
4018
4019   RELEASE_PARENT (parent);
4020
4021   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
4022       GST_QUERY_TYPE_NAME (query), res);
4023   GST_TRACER_PAD_QUERY_POST (pad, query, res);
4024
4025   if (res != TRUE)
4026     goto query_failed;
4027
4028   GST_OBJECT_LOCK (pad);
4029   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4030   GST_OBJECT_UNLOCK (pad);
4031
4032   if (G_UNLIKELY (serialized))
4033     GST_PAD_STREAM_UNLOCK (pad);
4034
4035   return res;
4036
4037   /* ERRORS */
4038 wrong_direction:
4039   {
4040     g_warning ("pad %s:%s query %s in wrong direction",
4041         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4042     return FALSE;
4043   }
4044 unknown_direction:
4045   {
4046     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4047     return FALSE;
4048   }
4049 no_parent:
4050   {
4051     GST_DEBUG_OBJECT (pad, "had no parent");
4052     GST_OBJECT_UNLOCK (pad);
4053     if (G_UNLIKELY (serialized))
4054       GST_PAD_STREAM_UNLOCK (pad);
4055     return FALSE;
4056   }
4057 no_func:
4058   {
4059     GST_DEBUG_OBJECT (pad, "had no query function");
4060     RELEASE_PARENT (parent);
4061     if (G_UNLIKELY (serialized))
4062       GST_PAD_STREAM_UNLOCK (pad);
4063     return FALSE;
4064   }
4065 query_failed:
4066   {
4067     GST_DEBUG_OBJECT (pad, "query failed");
4068     if (G_UNLIKELY (serialized))
4069       GST_PAD_STREAM_UNLOCK (pad);
4070     return FALSE;
4071   }
4072 probe_stopped:
4073   {
4074     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4075     GST_OBJECT_UNLOCK (pad);
4076     if (G_UNLIKELY (serialized))
4077       GST_PAD_STREAM_UNLOCK (pad);
4078
4079     /* if a probe dropped without handling, we don't sent it further but assume
4080      * that the probe did not answer the query and return FALSE */
4081     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4082       res = FALSE;
4083     else
4084       res = TRUE;
4085
4086     return res;
4087   }
4088 }
4089
4090 /**
4091  * gst_pad_peer_query:
4092  * @pad: a #GstPad to invoke the peer query on.
4093  * @query: (transfer none): the #GstQuery to perform.
4094  *
4095  * Performs gst_pad_query() on the peer of @pad.
4096  *
4097  * The caller is responsible for both the allocation and deallocation of
4098  * the query structure.
4099  *
4100  * Returns: %TRUE if the query could be performed. This function returns %FALSE
4101  * if @pad has no peer.
4102  */
4103 gboolean
4104 gst_pad_peer_query (GstPad * pad, GstQuery * query)
4105 {
4106   GstPad *peerpad;
4107   GstPadProbeType type;
4108   gboolean res, serialized;
4109   GstFlowReturn ret;
4110
4111   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4112   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
4113
4114   if (GST_PAD_IS_SRC (pad)) {
4115     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
4116       goto wrong_direction;
4117     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
4118   } else if (GST_PAD_IS_SINK (pad)) {
4119     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
4120       goto wrong_direction;
4121     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
4122   } else
4123     goto unknown_direction;
4124
4125   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
4126       GST_QUERY_TYPE_NAME (query));
4127
4128   serialized = GST_QUERY_IS_SERIALIZED (query);
4129
4130   GST_OBJECT_LOCK (pad);
4131   if (GST_PAD_IS_SRC (pad) && serialized) {
4132     /* all serialized queries on the srcpad trigger push of
4133      * sticky events */
4134     if (check_sticky (pad, NULL) != GST_FLOW_OK)
4135       goto sticky_failed;
4136   }
4137
4138   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4139       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4140   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4141
4142   peerpad = GST_PAD_PEER (pad);
4143   if (G_UNLIKELY (peerpad == NULL))
4144     goto no_peer;
4145
4146   gst_object_ref (peerpad);
4147   GST_OBJECT_UNLOCK (pad);
4148
4149   res = gst_pad_query (peerpad, query);
4150
4151   gst_object_unref (peerpad);
4152
4153   if (res != TRUE)
4154     goto query_failed;
4155
4156   GST_OBJECT_LOCK (pad);
4157   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4158   GST_OBJECT_UNLOCK (pad);
4159
4160   return res;
4161
4162   /* ERRORS */
4163 wrong_direction:
4164   {
4165     g_warning ("pad %s:%s query %s in wrong direction",
4166         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4167     return FALSE;
4168   }
4169 unknown_direction:
4170   {
4171     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4172     return FALSE;
4173   }
4174 sticky_failed:
4175   {
4176     GST_WARNING_OBJECT (pad, "could not send sticky events");
4177     GST_OBJECT_UNLOCK (pad);
4178     return FALSE;
4179   }
4180 no_peer:
4181   {
4182     GST_INFO_OBJECT (pad, "pad has no peer");
4183     GST_OBJECT_UNLOCK (pad);
4184     return FALSE;
4185   }
4186 query_failed:
4187   {
4188     GST_DEBUG_OBJECT (pad, "query failed");
4189     return FALSE;
4190   }
4191 probe_stopped:
4192   {
4193     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4194     GST_OBJECT_UNLOCK (pad);
4195
4196     /* if a probe dropped without handling, we don't sent it further but
4197      * assume that the probe did not answer the query and return FALSE */
4198     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4199       res = FALSE;
4200     else
4201       res = TRUE;
4202
4203     return res;
4204   }
4205 }
4206
4207 /**********************************************************************
4208  * Data passing functions
4209  */
4210
4211 /* this is the chain function that does not perform the additional argument
4212  * checking for that little extra speed.
4213  */
4214 static inline GstFlowReturn
4215 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4216 {
4217   GstFlowReturn ret;
4218   GstObject *parent;
4219   gboolean handled = FALSE;
4220
4221   GST_PAD_STREAM_LOCK (pad);
4222
4223   GST_OBJECT_LOCK (pad);
4224   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4225     goto flushing;
4226
4227   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4228     goto eos;
4229
4230   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4231     goto wrong_mode;
4232
4233 #ifdef GST_ENABLE_EXTRA_CHECKS
4234   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4235     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4236       g_warning (G_STRLOC
4237           ":%s:<%s:%s> Got data flow before stream-start event",
4238           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4239     }
4240     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4241       g_warning (G_STRLOC
4242           ":%s:<%s:%s> Got data flow before segment event",
4243           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4244     }
4245     pad->priv->last_cookie = pad->priv->events_cookie;
4246   }
4247 #endif
4248
4249   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4250       probe_handled);
4251
4252   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4253
4254   ACQUIRE_PARENT (pad, parent, no_parent);
4255   GST_OBJECT_UNLOCK (pad);
4256
4257   /* NOTE: we read the chainfunc unlocked.
4258    * we cannot hold the lock for the pad so we might send
4259    * the data to the wrong function. This is not really a
4260    * problem since functions are assigned at creation time
4261    * and don't change that often... */
4262   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4263     GstPadChainFunction chainfunc;
4264
4265     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4266       goto no_function;
4267
4268     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4269         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4270         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4271
4272     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4273
4274     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4275         "called chainfunction &%s with buffer %p, returned %s",
4276         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4277   } else {
4278     GstPadChainListFunction chainlistfunc;
4279
4280     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4281       goto no_function;
4282
4283     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4284         "calling chainlistfunction &%s",
4285         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4286
4287     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4288
4289     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4290         "called chainlistfunction &%s, returned %s",
4291         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4292   }
4293
4294   RELEASE_PARENT (parent);
4295
4296   GST_PAD_STREAM_UNLOCK (pad);
4297
4298   return ret;
4299
4300   /* ERRORS */
4301 flushing:
4302   {
4303     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4304         "chaining, but pad was flushing");
4305     GST_OBJECT_UNLOCK (pad);
4306     GST_PAD_STREAM_UNLOCK (pad);
4307     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4308     return GST_FLOW_FLUSHING;
4309   }
4310 eos:
4311   {
4312     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4313     GST_OBJECT_UNLOCK (pad);
4314     GST_PAD_STREAM_UNLOCK (pad);
4315     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4316     return GST_FLOW_EOS;
4317   }
4318 wrong_mode:
4319   {
4320     g_critical ("chain on pad %s:%s but it was not in push mode",
4321         GST_DEBUG_PAD_NAME (pad));
4322     GST_OBJECT_UNLOCK (pad);
4323     GST_PAD_STREAM_UNLOCK (pad);
4324     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4325     return GST_FLOW_ERROR;
4326   }
4327 probe_handled:
4328   handled = TRUE;
4329   /* PASSTHROUGH */
4330 probe_stopped:
4331   {
4332     GST_OBJECT_UNLOCK (pad);
4333     GST_PAD_STREAM_UNLOCK (pad);
4334     /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4335     if (!handled)
4336       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4337
4338     switch (ret) {
4339       case GST_FLOW_CUSTOM_SUCCESS:
4340       case GST_FLOW_CUSTOM_SUCCESS_1:
4341         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4342         ret = GST_FLOW_OK;
4343         break;
4344       default:
4345         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4346         break;
4347     }
4348     return ret;
4349   }
4350 no_parent:
4351   {
4352     GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4353     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4354     GST_OBJECT_UNLOCK (pad);
4355     GST_PAD_STREAM_UNLOCK (pad);
4356     return GST_FLOW_FLUSHING;
4357   }
4358 no_function:
4359   {
4360     RELEASE_PARENT (parent);
4361     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4362     g_critical ("chain on pad %s:%s but it has no chainfunction",
4363         GST_DEBUG_PAD_NAME (pad));
4364     GST_PAD_STREAM_UNLOCK (pad);
4365     return GST_FLOW_NOT_SUPPORTED;
4366   }
4367 }
4368
4369 /**
4370  * gst_pad_chain:
4371  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4372  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4373  *     if not.
4374  *
4375  * Chain a buffer to @pad.
4376  *
4377  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4378  *
4379  * If the buffer type is not acceptable for @pad (as negotiated with a
4380  * preceding GST_EVENT_CAPS event), this function returns
4381  * #GST_FLOW_NOT_NEGOTIATED.
4382  *
4383  * The function proceeds calling the chain function installed on @pad (see
4384  * gst_pad_set_chain_function()) and the return value of that function is
4385  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4386  * chain function.
4387  *
4388  * In all cases, success or failure, the caller loses its reference to @buffer
4389  * after calling this function.
4390  *
4391  * Returns: a #GstFlowReturn from the pad.
4392  *
4393  * MT safe.
4394  */
4395 GstFlowReturn
4396 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4397 {
4398   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4399   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4400   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4401
4402   return gst_pad_chain_data_unchecked (pad,
4403       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4404 }
4405
4406 static GstFlowReturn
4407 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4408     GstBufferList * list)
4409 {
4410   guint i, len;
4411   GstBuffer *buffer;
4412   GstFlowReturn ret;
4413
4414   GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4415
4416   len = gst_buffer_list_length (list);
4417
4418   ret = GST_FLOW_OK;
4419   for (i = 0; i < len; i++) {
4420     buffer = gst_buffer_list_get (list, i);
4421     ret =
4422         gst_pad_chain_data_unchecked (pad,
4423         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4424         gst_buffer_ref (buffer));
4425     if (ret != GST_FLOW_OK)
4426       break;
4427   }
4428   gst_buffer_list_unref (list);
4429
4430   return ret;
4431 }
4432
4433 /**
4434  * gst_pad_chain_list:
4435  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4436  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4437  *     if not.
4438  *
4439  * Chain a bufferlist to @pad.
4440  *
4441  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4442  *
4443  * If @pad was not negotiated properly with a CAPS event, this function
4444  * returns #GST_FLOW_NOT_NEGOTIATED.
4445  *
4446  * The function proceeds calling the chainlist function installed on @pad (see
4447  * gst_pad_set_chain_list_function()) and the return value of that function is
4448  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4449  * chainlist function.
4450  *
4451  * In all cases, success or failure, the caller loses its reference to @list
4452  * after calling this function.
4453  *
4454  * MT safe.
4455  *
4456  * Returns: a #GstFlowReturn from the pad.
4457  */
4458 GstFlowReturn
4459 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4460 {
4461   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4462   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4463   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4464
4465   return gst_pad_chain_data_unchecked (pad,
4466       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4467 }
4468
4469 static GstFlowReturn
4470 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4471 {
4472   GstPad *peer;
4473   GstFlowReturn ret;
4474   gboolean handled = FALSE;
4475
4476   GST_OBJECT_LOCK (pad);
4477   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4478     goto flushing;
4479
4480   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4481     goto eos;
4482
4483   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4484     goto wrong_mode;
4485
4486 #ifdef GST_ENABLE_EXTRA_CHECKS
4487   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4488     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4489       g_warning (G_STRLOC
4490           ":%s:<%s:%s> Got data flow before stream-start event",
4491           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4492     }
4493     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4494       g_warning (G_STRLOC
4495           ":%s:<%s:%s> Got data flow before segment event",
4496           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4497     }
4498     pad->priv->last_cookie = pad->priv->events_cookie;
4499   }
4500 #endif
4501
4502   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4503     goto events_error;
4504
4505   /* do block probes */
4506   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4507       probe_handled);
4508
4509   /* recheck sticky events because the probe might have cause a relink */
4510   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4511     goto events_error;
4512
4513   /* do post-blocking probes */
4514   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4515
4516   /* recheck sticky events because the probe might have cause a relink */
4517   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4518     goto events_error;
4519
4520   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4521     goto not_linked;
4522
4523   /* take ref to peer pad before releasing the lock */
4524   gst_object_ref (peer);
4525   pad->priv->using++;
4526   GST_OBJECT_UNLOCK (pad);
4527
4528   ret = gst_pad_chain_data_unchecked (peer, type, data);
4529   data = NULL;
4530
4531   gst_object_unref (peer);
4532
4533   GST_OBJECT_LOCK (pad);
4534   pad->ABI.abi.last_flowret = ret;
4535   pad->priv->using--;
4536   if (pad->priv->using == 0) {
4537     /* pad is not active anymore, trigger idle callbacks */
4538     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4539         probe_stopped, ret);
4540   }
4541   GST_OBJECT_UNLOCK (pad);
4542
4543   return ret;
4544
4545   /* ERROR recovery here */
4546   /* ERRORS */
4547 flushing:
4548   {
4549     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4550         "pushing, but pad was flushing");
4551     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4552     GST_OBJECT_UNLOCK (pad);
4553     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4554     return GST_FLOW_FLUSHING;
4555   }
4556 eos:
4557   {
4558     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4559     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4560     GST_OBJECT_UNLOCK (pad);
4561     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4562     return GST_FLOW_EOS;
4563   }
4564 wrong_mode:
4565   {
4566     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4567         GST_DEBUG_PAD_NAME (pad));
4568     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4569     GST_OBJECT_UNLOCK (pad);
4570     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4571     return GST_FLOW_ERROR;
4572   }
4573 events_error:
4574   {
4575     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4576         "error pushing events, return %s", gst_flow_get_name (ret));
4577     pad->ABI.abi.last_flowret = ret;
4578     GST_OBJECT_UNLOCK (pad);
4579     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4580     return ret;
4581   }
4582 probe_handled:
4583   handled = TRUE;
4584   /* PASSTHROUGH */
4585 probe_stopped:
4586   {
4587     GST_OBJECT_UNLOCK (pad);
4588     if (data != NULL && !handled)
4589       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4590
4591     switch (ret) {
4592       case GST_FLOW_CUSTOM_SUCCESS:
4593       case GST_FLOW_CUSTOM_SUCCESS_1:
4594         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4595         ret = GST_FLOW_OK;
4596         break;
4597       default:
4598         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4599         break;
4600     }
4601     pad->ABI.abi.last_flowret = ret;
4602     return ret;
4603   }
4604 not_linked:
4605   {
4606     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4607         "pushing, but it was not linked");
4608     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4609     GST_OBJECT_UNLOCK (pad);
4610     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4611     return GST_FLOW_NOT_LINKED;
4612   }
4613 }
4614
4615 /**
4616  * gst_pad_push:
4617  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4618  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4619  *     if not.
4620  *
4621  * Pushes a buffer to the peer of @pad.
4622  *
4623  * This function will call installed block probes before triggering any
4624  * installed data probes.
4625  *
4626  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4627  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4628  * be returned.
4629  *
4630  * In all cases, success or failure, the caller loses its reference to @buffer
4631  * after calling this function.
4632  *
4633  * Returns: a #GstFlowReturn from the peer pad.
4634  *
4635  * MT safe.
4636  */
4637 GstFlowReturn
4638 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4639 {
4640   GstFlowReturn res;
4641
4642   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4643   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4644   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4645
4646   GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4647   res = gst_pad_push_data (pad,
4648       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4649   GST_TRACER_PAD_PUSH_POST (pad, res);
4650   return res;
4651 }
4652
4653 /**
4654  * gst_pad_push_list:
4655  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4656  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4657  *     if not.
4658  *
4659  * Pushes a buffer list to the peer of @pad.
4660  *
4661  * This function will call installed block probes before triggering any
4662  * installed data probes.
4663  *
4664  * The function proceeds calling the chain function on the peer pad and returns
4665  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4666  * be returned. If the peer pad does not have any installed chainlist function
4667  * every group buffer of the list will be merged into a normal #GstBuffer and
4668  * chained via gst_pad_chain().
4669  *
4670  * In all cases, success or failure, the caller loses its reference to @list
4671  * after calling this function.
4672  *
4673  * Returns: a #GstFlowReturn from the peer pad.
4674  *
4675  * MT safe.
4676  */
4677 GstFlowReturn
4678 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4679 {
4680   GstFlowReturn res;
4681
4682   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4683   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4684   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4685
4686   GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4687   res = gst_pad_push_data (pad,
4688       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4689   GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4690   return res;
4691 }
4692
4693 static GstFlowReturn
4694 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4695     GstBuffer ** buffer)
4696 {
4697   GstFlowReturn ret;
4698   GstPadGetRangeFunction getrangefunc;
4699   GstObject *parent;
4700   GstBuffer *res_buf;
4701
4702   GST_PAD_STREAM_LOCK (pad);
4703
4704   GST_OBJECT_LOCK (pad);
4705   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4706     goto flushing;
4707
4708   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4709     goto wrong_mode;
4710
4711   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4712     goto events_error;
4713
4714   res_buf = *buffer;
4715
4716   /* when one of the probes returns DROPPED, probe_stopped will be called
4717    * and we skip calling the getrange function, res_buf should then contain a
4718    * valid result buffer */
4719   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4720       res_buf, offset, size, probe_stopped);
4721
4722   /* recheck sticky events because the probe might have cause a relink */
4723   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4724     goto events_error;
4725
4726   ACQUIRE_PARENT (pad, parent, no_parent);
4727   GST_OBJECT_UNLOCK (pad);
4728
4729   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4730     goto no_function;
4731
4732   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4733       "calling getrangefunc %s, offset %"
4734       G_GUINT64_FORMAT ", size %u",
4735       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4736
4737   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4738
4739   RELEASE_PARENT (parent);
4740
4741   GST_OBJECT_LOCK (pad);
4742   if (G_UNLIKELY (ret != GST_FLOW_OK))
4743     goto get_range_failed;
4744
4745   /* can only fire the signal if we have a valid buffer */
4746 probed_data:
4747   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4748       res_buf, offset, size, probe_stopped_unref);
4749   pad->ABI.abi.last_flowret = ret;
4750   GST_OBJECT_UNLOCK (pad);
4751
4752   GST_PAD_STREAM_UNLOCK (pad);
4753
4754   *buffer = res_buf;
4755
4756   return ret;
4757
4758   /* ERRORS */
4759 flushing:
4760   {
4761     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4762         "getrange, but pad was flushing");
4763     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4764     GST_OBJECT_UNLOCK (pad);
4765     GST_PAD_STREAM_UNLOCK (pad);
4766     return GST_FLOW_FLUSHING;
4767   }
4768 wrong_mode:
4769   {
4770     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4771         GST_DEBUG_PAD_NAME (pad));
4772     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4773     GST_OBJECT_UNLOCK (pad);
4774     GST_PAD_STREAM_UNLOCK (pad);
4775     return GST_FLOW_ERROR;
4776   }
4777 events_error:
4778   {
4779     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4780     pad->ABI.abi.last_flowret = ret;
4781     GST_OBJECT_UNLOCK (pad);
4782     GST_PAD_STREAM_UNLOCK (pad);
4783     return ret;
4784   }
4785 no_parent:
4786   {
4787     GST_DEBUG_OBJECT (pad, "no parent");
4788     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4789     GST_OBJECT_UNLOCK (pad);
4790     GST_PAD_STREAM_UNLOCK (pad);
4791     return GST_FLOW_FLUSHING;
4792   }
4793 no_function:
4794   {
4795     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4796         GST_DEBUG_PAD_NAME (pad));
4797     RELEASE_PARENT (parent);
4798     GST_PAD_STREAM_UNLOCK (pad);
4799     return GST_FLOW_NOT_SUPPORTED;
4800   }
4801 probe_stopped:
4802   {
4803     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4804         "probe returned %s", gst_flow_get_name (ret));
4805     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4806       if (res_buf) {
4807         /* the probe filled the buffer and asks us to not call the getrange
4808          * anymore, we continue with the post probes then. */
4809         GST_DEBUG_OBJECT (pad, "handled buffer");
4810         ret = GST_FLOW_OK;
4811         goto probed_data;
4812       } else {
4813         /* no buffer, we are EOS */
4814         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4815         ret = GST_FLOW_EOS;
4816       }
4817     }
4818     pad->ABI.abi.last_flowret = ret;
4819     GST_OBJECT_UNLOCK (pad);
4820     GST_PAD_STREAM_UNLOCK (pad);
4821
4822     return ret;
4823   }
4824 probe_stopped_unref:
4825   {
4826     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4827         "probe returned %s", gst_flow_get_name (ret));
4828     /* if we drop here, it signals EOS */
4829     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4830       ret = GST_FLOW_EOS;
4831     pad->ABI.abi.last_flowret = ret;
4832     GST_OBJECT_UNLOCK (pad);
4833     GST_PAD_STREAM_UNLOCK (pad);
4834     if (*buffer == NULL)
4835       gst_buffer_unref (res_buf);
4836     return ret;
4837   }
4838 get_range_failed:
4839   {
4840     pad->ABI.abi.last_flowret = ret;
4841     GST_OBJECT_UNLOCK (pad);
4842     GST_PAD_STREAM_UNLOCK (pad);
4843     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4844         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4845         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4846     return ret;
4847   }
4848 }
4849
4850 /**
4851  * gst_pad_get_range:
4852  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4853  * @offset: The start offset of the buffer
4854  * @size: The length of the buffer
4855  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4856  *     returns #GST_FLOW_ERROR if %NULL.
4857  *
4858  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4859  * immediately and @buffer is %NULL.
4860  *
4861  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4862  * description of a getrange function. If @pad has no getrange function
4863  * installed (see gst_pad_set_getrange_function()) this function returns
4864  * #GST_FLOW_NOT_SUPPORTED.
4865  *
4866  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4867  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4868  * must be freed with gst_buffer_unref() after usage.
4869  *
4870  * When @buffer points to a variable that points to a valid #GstBuffer, the
4871  * buffer will be filled with the result data when this function returns
4872  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4873  * @size bytes will be filled in the result buffer and its size will be updated
4874  * accordingly.
4875  *
4876  * Note that less than @size bytes can be returned in @buffer when, for example,
4877  * an EOS condition is near or when @buffer is not large enough to hold @size
4878  * bytes. The caller should check the result buffer size to get the result size.
4879  *
4880  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4881  * will be unchanged.
4882  *
4883  * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4884  *
4885  * Returns: a #GstFlowReturn from the pad.
4886  *
4887  * MT safe.
4888  */
4889 GstFlowReturn
4890 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4891     GstBuffer ** buffer)
4892 {
4893   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4894   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4895   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4896   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4897           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4898
4899   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4900 }
4901
4902 /**
4903  * gst_pad_pull_range:
4904  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4905  * @offset: The start offset of the buffer
4906  * @size: The length of the buffer
4907  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4908  *     GST_FLOW_ERROR if %NULL.
4909  *
4910  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4911  *
4912  * This function will first trigger the pad block signal if it was
4913  * installed.
4914  *
4915  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4916  * function returns the result of gst_pad_get_range() on the peer pad.
4917  * See gst_pad_get_range() for a list of return values and for the
4918  * semantics of the arguments of this function.
4919  *
4920  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4921  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4922  * must be freed with gst_buffer_unref() after usage. When this function
4923  * returns any other result value, @buffer will still point to %NULL.
4924  *
4925  * When @buffer points to a variable that points to a valid #GstBuffer, the
4926  * buffer will be filled with the result data when this function returns
4927  * #GST_FLOW_OK. When this function returns any other result value,
4928  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4929  * @size bytes will be filled in the result buffer and its size will be updated
4930  * accordingly.
4931  *
4932  * Note that less than @size bytes can be returned in @buffer when, for example,
4933  * an EOS condition is near or when @buffer is not large enough to hold @size
4934  * bytes. The caller should check the result buffer size to get the result size.
4935  *
4936  * Returns: a #GstFlowReturn from the peer pad.
4937  *
4938  * MT safe.
4939  */
4940 GstFlowReturn
4941 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4942     GstBuffer ** buffer)
4943 {
4944   GstPad *peer;
4945   GstFlowReturn ret;
4946   GstBuffer *res_buf;
4947
4948   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4949   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4950   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4951   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4952           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4953
4954   GST_TRACER_PAD_PULL_RANGE_PRE (pad, offset, size);
4955
4956   GST_OBJECT_LOCK (pad);
4957   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4958     goto flushing;
4959
4960   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4961     goto wrong_mode;
4962
4963   res_buf = *buffer;
4964
4965   /* when one of the probes returns DROPPED, probe_stopped will be
4966    * called and we skip calling the peer getrange function. *buffer should then
4967    * contain a valid buffer */
4968   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4969       res_buf, offset, size, probe_stopped);
4970
4971   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4972     goto not_linked;
4973
4974   gst_object_ref (peer);
4975   pad->priv->using++;
4976   GST_OBJECT_UNLOCK (pad);
4977
4978   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4979
4980   gst_object_unref (peer);
4981
4982   GST_OBJECT_LOCK (pad);
4983   pad->priv->using--;
4984   pad->ABI.abi.last_flowret = ret;
4985   if (pad->priv->using == 0) {
4986     /* pad is not active anymore, trigger idle callbacks */
4987     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4988         probe_stopped_unref, ret);
4989   }
4990
4991   if (G_UNLIKELY (ret != GST_FLOW_OK))
4992     goto pull_range_failed;
4993
4994 probed_data:
4995   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4996       res_buf, offset, size, probe_stopped_unref);
4997
4998   GST_OBJECT_UNLOCK (pad);
4999
5000   *buffer = res_buf;
5001
5002   GST_TRACER_PAD_PULL_RANGE_POST (pad, *buffer, ret);
5003   return ret;
5004
5005   /* ERROR recovery here */
5006 flushing:
5007   {
5008     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5009         "pullrange, but pad was flushing");
5010     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
5011     GST_OBJECT_UNLOCK (pad);
5012     ret = GST_FLOW_FLUSHING;
5013     goto done;
5014   }
5015 wrong_mode:
5016   {
5017     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
5018         GST_DEBUG_PAD_NAME (pad));
5019     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
5020     GST_OBJECT_UNLOCK (pad);
5021     ret = GST_FLOW_ERROR;
5022     goto done;
5023   }
5024 probe_stopped:
5025   {
5026     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
5027         gst_flow_get_name (ret));
5028     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
5029       if (res_buf) {
5030         /* the probe filled the buffer and asks us to not forward to the peer
5031          * anymore, we continue with the post probes then */
5032         GST_DEBUG_OBJECT (pad, "handled buffer");
5033         ret = GST_FLOW_OK;
5034         goto probed_data;
5035       } else {
5036         /* no buffer, we are EOS then */
5037         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
5038         ret = GST_FLOW_EOS;
5039       }
5040     }
5041     pad->ABI.abi.last_flowret = ret;
5042     GST_OBJECT_UNLOCK (pad);
5043     goto done;
5044   }
5045 not_linked:
5046   {
5047     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5048         "pulling range, but it was not linked");
5049     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
5050     GST_OBJECT_UNLOCK (pad);
5051     ret = GST_FLOW_NOT_LINKED;
5052     goto done;
5053   }
5054 pull_range_failed:
5055   {
5056     pad->ABI.abi.last_flowret = ret;
5057     GST_OBJECT_UNLOCK (pad);
5058     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5059         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5060         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
5061     goto done;
5062   }
5063 probe_stopped_unref:
5064   {
5065     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5066         "post probe returned %s", gst_flow_get_name (ret));
5067
5068     /* if we drop here, it signals EOS */
5069     if (ret == GST_FLOW_CUSTOM_SUCCESS)
5070       ret = GST_FLOW_EOS;
5071
5072     pad->ABI.abi.last_flowret = ret;
5073     GST_OBJECT_UNLOCK (pad);
5074
5075     if (*buffer == NULL)
5076       gst_buffer_unref (res_buf);
5077     goto done;
5078   }
5079 done:
5080   GST_TRACER_PAD_PULL_RANGE_POST (pad, NULL, ret);
5081   return ret;
5082 }
5083
5084 /* must be called with pad object lock */
5085 static GstFlowReturn
5086 store_sticky_event (GstPad * pad, GstEvent * event)
5087 {
5088   guint i, len;
5089   GstEventType type;
5090   GArray *events;
5091   gboolean res = FALSE;
5092   const gchar *name = NULL;
5093   gboolean insert = TRUE;
5094
5095   type = GST_EVENT_TYPE (event);
5096
5097   /* Store all sticky events except SEGMENT/EOS when we're flushing,
5098    * otherwise they can be dropped and nothing would ever resend them.
5099    * Only do that for activated pads though, everything else is a bug!
5100    */
5101   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
5102           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
5103                   || type == GST_EVENT_EOS))))
5104     goto flushed;
5105
5106   /* Unset the EOS flag when received STREAM_START event, so pad can
5107    * store sticky event and then push it later */
5108   if (type == GST_EVENT_STREAM_START) {
5109     GST_LOG_OBJECT (pad, "Removing pending EOS and StreamGroupDone events");
5110     remove_event_by_type (pad, GST_EVENT_EOS);
5111     remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5112     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5113   }
5114
5115   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5116     goto eos;
5117
5118   if (type & GST_EVENT_TYPE_STICKY_MULTI)
5119     name = gst_structure_get_name (gst_event_get_structure (event));
5120
5121   events = pad->priv->events;
5122   len = events->len;
5123
5124   for (i = 0; i < len; i++) {
5125     PadEvent *ev = &g_array_index (events, PadEvent, i);
5126
5127     if (ev->event == NULL)
5128       continue;
5129
5130     if (type == GST_EVENT_TYPE (ev->event)) {
5131       /* matching types, check matching name if needed */
5132       if (name && !gst_event_has_name (ev->event, name))
5133         continue;
5134
5135       /* overwrite */
5136       if ((res = gst_event_replace (&ev->event, event)))
5137         ev->received = FALSE;
5138
5139       insert = FALSE;
5140       break;
5141     }
5142
5143     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
5144             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
5145       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
5146        * storing the sticky ordered we can check that this is respected. */
5147       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
5148               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
5149         g_warning (G_STRLOC
5150             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
5151             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
5152             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
5153             gst_event_type_get_name (type));
5154       break;
5155     }
5156   }
5157   if (insert) {
5158     PadEvent ev;
5159     ev.event = gst_event_ref (event);
5160     ev.received = FALSE;
5161     g_array_insert_val (events, i, ev);
5162     res = TRUE;
5163   }
5164
5165   if (res) {
5166     pad->priv->events_cookie++;
5167     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5168
5169     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
5170
5171     switch (GST_EVENT_TYPE (event)) {
5172       case GST_EVENT_CAPS:
5173         GST_OBJECT_UNLOCK (pad);
5174
5175         GST_DEBUG_OBJECT (pad, "notify caps");
5176         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
5177
5178         GST_OBJECT_LOCK (pad);
5179         break;
5180       default:
5181         break;
5182     }
5183   }
5184   if (type == GST_EVENT_EOS) {
5185     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
5186     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
5187   }
5188
5189   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
5190
5191   /* ERRORS */
5192 flushed:
5193   {
5194     GST_DEBUG_OBJECT (pad, "pad is flushing");
5195     return GST_FLOW_FLUSHING;
5196   }
5197 eos:
5198   {
5199     GST_DEBUG_OBJECT (pad, "pad is EOS");
5200     return GST_FLOW_EOS;
5201   }
5202 }
5203
5204 /**
5205  * gst_pad_store_sticky_event:
5206  * @pad: a #GstPad
5207  * @event: (transfer none): a #GstEvent
5208  *
5209  * Store the sticky @event on @pad
5210  *
5211  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5212  * was flushing or #GST_FLOW_EOS when the pad was EOS.
5213  *
5214  * Since: 1.2
5215  */
5216 GstFlowReturn
5217 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5218 {
5219   GstFlowReturn ret;
5220
5221   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5222   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5223
5224   GST_OBJECT_LOCK (pad);
5225   ret = store_sticky_event (pad, event);
5226   GST_OBJECT_UNLOCK (pad);
5227
5228   return ret;
5229 }
5230
5231 static gboolean
5232 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5233 {
5234   PushStickyData *data = user_data;
5235
5236   /* Forward all sticky events before our current one that are pending */
5237   if (ev->event != data->event
5238       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5239     return push_sticky (pad, ev, data);
5240
5241   return TRUE;
5242 }
5243
5244 /* should be called with pad LOCK */
5245 static GstFlowReturn
5246 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5247     GstPadProbeType type)
5248 {
5249   GstFlowReturn ret;
5250   GstPad *peerpad;
5251   GstEventType event_type;
5252
5253   /* pass the adjusted event on. We need to do this even if
5254    * there is no peer pad because of the probes. */
5255   event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5256
5257   /* Two checks to be made:
5258    * . (un)set the FLUSHING flag for flushing events,
5259    * . handle pad blocking */
5260   event_type = GST_EVENT_TYPE (event);
5261   switch (event_type) {
5262     case GST_EVENT_FLUSH_START:
5263       GST_PAD_SET_FLUSHING (pad);
5264
5265       GST_PAD_BLOCK_BROADCAST (pad);
5266       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5267       break;
5268     case GST_EVENT_FLUSH_STOP:
5269       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5270         goto inactive;
5271
5272       GST_PAD_UNSET_FLUSHING (pad);
5273
5274       /* Remove sticky EOS events */
5275       GST_LOG_OBJECT (pad, "Removing pending EOS events");
5276       remove_event_by_type (pad, GST_EVENT_EOS);
5277       remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5278       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5279       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5280       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5281
5282       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5283       break;
5284     default:
5285     {
5286       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5287         goto flushed;
5288
5289       /* No need to check for EOS here as either the caller (gst_pad_push_event())
5290        * checked already or this is called as part of pushing sticky events,
5291        * in which case we still want to forward the EOS event downstream.
5292        */
5293
5294       switch (GST_EVENT_TYPE (event)) {
5295         case GST_EVENT_RECONFIGURE:
5296           if (GST_PAD_IS_SINK (pad))
5297             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5298           break;
5299         default:
5300           break;
5301       }
5302       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5303           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5304       /* recheck sticky events because the probe might have cause a relink */
5305       if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5306           && (GST_EVENT_IS_SERIALIZED (event)
5307               || GST_EVENT_IS_STICKY (event))) {
5308         PushStickyData data = { GST_FLOW_OK, FALSE, event };
5309         GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5310
5311         /* Push all sticky events before our current one
5312          * that have changed */
5313         events_foreach (pad, sticky_changed, &data);
5314       }
5315       break;
5316     }
5317   }
5318
5319   /* send probes after modifying the events above */
5320   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5321
5322   /* recheck sticky events because the probe might have cause a relink */
5323   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5324       && (GST_EVENT_IS_SERIALIZED (event)
5325           || GST_EVENT_IS_STICKY (event))) {
5326     PushStickyData data = { GST_FLOW_OK, FALSE, event };
5327     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5328
5329     /* Push all sticky events before our current one
5330      * that have changed */
5331     events_foreach (pad, sticky_changed, &data);
5332   }
5333
5334   /* now check the peer pad */
5335   peerpad = GST_PAD_PEER (pad);
5336   if (peerpad == NULL)
5337     goto not_linked;
5338
5339   gst_object_ref (peerpad);
5340   pad->priv->using++;
5341   GST_OBJECT_UNLOCK (pad);
5342
5343   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5344       event, gst_event_type_get_name (event_type), peerpad);
5345
5346   ret = gst_pad_send_event_unchecked (peerpad, event, type);
5347
5348   /* Note: we gave away ownership of the event at this point but we can still
5349    * print the old pointer */
5350   GST_LOG_OBJECT (pad,
5351       "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5352       gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5353
5354   gst_object_unref (peerpad);
5355
5356   GST_OBJECT_LOCK (pad);
5357   pad->priv->using--;
5358   if (pad->priv->using == 0) {
5359     /* pad is not active anymore, trigger idle callbacks */
5360     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5361         idle_probe_stopped, ret);
5362   }
5363   return ret;
5364
5365   /* ERROR handling */
5366 flushed:
5367   {
5368     GST_DEBUG_OBJECT (pad, "We're flushing");
5369     gst_event_unref (event);
5370     return GST_FLOW_FLUSHING;
5371   }
5372 inactive:
5373   {
5374     GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5375     gst_event_unref (event);
5376     return GST_FLOW_FLUSHING;
5377   }
5378 probe_stopped:
5379   {
5380     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5381     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5382       gst_event_unref (event);
5383
5384     switch (ret) {
5385       case GST_FLOW_CUSTOM_SUCCESS_1:
5386         GST_DEBUG_OBJECT (pad, "handled event");
5387         break;
5388       case GST_FLOW_CUSTOM_SUCCESS:
5389         GST_DEBUG_OBJECT (pad, "dropped event");
5390         break;
5391       default:
5392         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5393         break;
5394     }
5395     return ret;
5396   }
5397 not_linked:
5398   {
5399     GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5400         gst_event_type_get_name (GST_EVENT_TYPE (event)));
5401     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5402     gst_event_unref (event);
5403
5404     /* unlinked pads should not influence latency configuration */
5405     if (event_type == GST_EVENT_LATENCY)
5406       return GST_FLOW_OK;
5407
5408     return GST_FLOW_NOT_LINKED;
5409   }
5410 idle_probe_stopped:
5411   {
5412     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5413     return ret;
5414   }
5415 }
5416
5417 /**
5418  * gst_pad_push_event:
5419  * @pad: a #GstPad to push the event to.
5420  * @event: (transfer full): the #GstEvent to send to the pad.
5421  *
5422  * Sends the event to the peer of the given pad. This function is
5423  * mainly used by elements to send events to their peer
5424  * elements.
5425  *
5426  * This function takes ownership of the provided event so you should
5427  * gst_event_ref() it if you want to reuse the event after this call.
5428  *
5429  * Returns: %TRUE if the event was handled.
5430  *
5431  * MT safe.
5432  */
5433 gboolean
5434 gst_pad_push_event (GstPad * pad, GstEvent * event)
5435 {
5436   gboolean res = FALSE;
5437   GstPadProbeType type;
5438   gboolean sticky, serialized;
5439
5440   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5441   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5442
5443   GST_TRACER_PAD_PUSH_EVENT_PRE (pad, event);
5444
5445   if (GST_PAD_IS_SRC (pad)) {
5446     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5447       goto wrong_direction;
5448     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5449   } else if (GST_PAD_IS_SINK (pad)) {
5450     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5451       goto wrong_direction;
5452     /* events pushed on sinkpad never are sticky */
5453     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5454   } else
5455     goto unknown_direction;
5456
5457   GST_OBJECT_LOCK (pad);
5458   sticky = GST_EVENT_IS_STICKY (event);
5459   serialized = GST_EVENT_IS_SERIALIZED (event);
5460
5461   if (sticky) {
5462     /* srcpad sticky events are stored immediately, the received flag is set
5463      * to FALSE and will be set to TRUE when we can successfully push the
5464      * event to the peer pad */
5465     switch (store_sticky_event (pad, event)) {
5466       case GST_FLOW_FLUSHING:
5467         goto flushed;
5468       case GST_FLOW_EOS:
5469         goto eos;
5470       default:
5471         break;
5472     }
5473   }
5474   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5475     /* all serialized or sticky events on the srcpad trigger push of
5476      * sticky events */
5477     res = (check_sticky (pad, event) == GST_FLOW_OK);
5478   }
5479   if (!sticky) {
5480     GstFlowReturn ret;
5481
5482     /* other events are pushed right away */
5483     ret = gst_pad_push_event_unchecked (pad, event, type);
5484     /* dropped events by a probe are not an error */
5485     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5486         || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5487   } else {
5488     /* Errors in sticky event pushing are no problem and ignored here
5489      * as they will cause more meaningful errors during data flow.
5490      * For EOS events, that are not followed by data flow, we still
5491      * return FALSE here though.
5492      */
5493     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5494       res = TRUE;
5495     gst_event_unref (event);
5496   }
5497   GST_OBJECT_UNLOCK (pad);
5498
5499   GST_TRACER_PAD_PUSH_EVENT_POST (pad, res);
5500   return res;
5501
5502   /* ERROR handling */
5503 wrong_direction:
5504   {
5505     g_warning ("pad %s:%s pushing %s event in wrong direction",
5506         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5507     gst_event_unref (event);
5508     goto done;
5509   }
5510 unknown_direction:
5511   {
5512     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5513     gst_event_unref (event);
5514     goto done;
5515   }
5516 flushed:
5517   {
5518     GST_DEBUG_OBJECT (pad, "We're flushing");
5519     GST_OBJECT_UNLOCK (pad);
5520     gst_event_unref (event);
5521     goto done;
5522   }
5523 eos:
5524   {
5525     GST_DEBUG_OBJECT (pad, "We're EOS");
5526     GST_OBJECT_UNLOCK (pad);
5527     gst_event_unref (event);
5528     goto done;
5529   }
5530 done:
5531   GST_TRACER_PAD_PUSH_EVENT_POST (pad, FALSE);
5532   return FALSE;
5533 }
5534
5535 /* Check if we can call the event function with the given event */
5536 static GstFlowReturn
5537 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5538 {
5539   GstCaps *caps;
5540
5541   switch (GST_EVENT_TYPE (event)) {
5542     case GST_EVENT_CAPS:
5543     {
5544       /* backwards compatibility mode for caps */
5545       gst_event_parse_caps (event, &caps);
5546
5547       if (!gst_pad_query_accept_caps (pad, caps))
5548         goto not_accepted;
5549       break;
5550     }
5551     default:
5552       break;
5553   }
5554   return GST_FLOW_OK;
5555
5556   /* ERRORS */
5557 not_accepted:
5558   {
5559     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5560         "caps %" GST_PTR_FORMAT " not accepted", caps);
5561     return GST_FLOW_NOT_NEGOTIATED;
5562   }
5563 }
5564
5565 static GstFlowReturn
5566 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5567     GstPadProbeType type)
5568 {
5569   GstFlowReturn ret;
5570   GstEventType event_type;
5571   gboolean serialized, need_unlock = FALSE, sticky;
5572   GstPadEventFunction eventfunc;
5573   GstPadEventFullFunction eventfullfunc = NULL;
5574   GstObject *parent;
5575
5576   GST_OBJECT_LOCK (pad);
5577
5578   event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5579
5580   if (GST_PAD_IS_SINK (pad))
5581     serialized = GST_EVENT_IS_SERIALIZED (event);
5582   else
5583     serialized = FALSE;
5584   sticky = GST_EVENT_IS_STICKY (event);
5585   event_type = GST_EVENT_TYPE (event);
5586   switch (event_type) {
5587     case GST_EVENT_FLUSH_START:
5588       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5589           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5590
5591       /* can't even accept a flush begin event when flushing */
5592       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5593         goto flushing;
5594
5595       GST_PAD_SET_FLUSHING (pad);
5596       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5597       GST_PAD_BLOCK_BROADCAST (pad);
5598       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5599       break;
5600     case GST_EVENT_FLUSH_STOP:
5601       /* we can't accept flush-stop on inactive pads else the flushing flag
5602        * would be cleared and it would look like the pad can accept data.
5603        * Also, some elements restart a streaming thread in flush-stop which we
5604        * can't allow on inactive pads */
5605       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5606         goto inactive;
5607
5608       GST_PAD_UNSET_FLUSHING (pad);
5609       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5610       /* Remove pending EOS events */
5611       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5612       remove_event_by_type (pad, GST_EVENT_EOS);
5613       remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5614       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5615       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5616       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5617
5618       GST_OBJECT_UNLOCK (pad);
5619       /* grab stream lock */
5620       GST_PAD_STREAM_LOCK (pad);
5621       need_unlock = TRUE;
5622       GST_OBJECT_LOCK (pad);
5623       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5624         goto flushing;
5625       break;
5626     case GST_EVENT_RECONFIGURE:
5627       if (GST_PAD_IS_SRC (pad))
5628         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5629     default:
5630       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5631           "have event type %" GST_PTR_FORMAT, event);
5632
5633       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5634         goto flushing;
5635
5636       switch (event_type) {
5637         case GST_EVENT_STREAM_START:
5638           /* Remove sticky EOS events */
5639           GST_LOG_OBJECT (pad, "Removing pending EOS events");
5640           remove_event_by_type (pad, GST_EVENT_EOS);
5641           remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5642           GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5643           break;
5644         default:
5645           break;
5646       }
5647
5648       if (serialized) {
5649         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5650           goto eos;
5651
5652         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5653         GST_OBJECT_UNLOCK (pad);
5654         GST_PAD_STREAM_LOCK (pad);
5655         need_unlock = TRUE;
5656         GST_OBJECT_LOCK (pad);
5657         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5658           goto flushing;
5659
5660         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5661           goto eos;
5662       }
5663       break;
5664   }
5665
5666   /* now do the probe */
5667   PROBE_PUSH (pad,
5668       type | GST_PAD_PROBE_TYPE_PUSH |
5669       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5670
5671   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5672
5673   eventfullfunc = GST_PAD_EVENTFULLFUNC (pad);
5674   eventfunc = GST_PAD_EVENTFUNC (pad);
5675   if (G_UNLIKELY (eventfunc == NULL && eventfullfunc == NULL))
5676     goto no_function;
5677
5678   ACQUIRE_PARENT (pad, parent, no_parent);
5679   GST_OBJECT_UNLOCK (pad);
5680
5681   ret = pre_eventfunc_check (pad, event);
5682   if (G_UNLIKELY (ret != GST_FLOW_OK))
5683     goto precheck_failed;
5684
5685   if (sticky)
5686     gst_event_ref (event);
5687
5688   if (eventfullfunc) {
5689     ret = eventfullfunc (pad, parent, event);
5690   } else if (eventfunc (pad, parent, event)) {
5691     ret = GST_FLOW_OK;
5692   } else {
5693     /* something went wrong */
5694     switch (event_type) {
5695       case GST_EVENT_CAPS:
5696         ret = GST_FLOW_NOT_NEGOTIATED;
5697         break;
5698       default:
5699         ret = GST_FLOW_ERROR;
5700         break;
5701     }
5702   }
5703   RELEASE_PARENT (parent);
5704
5705   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5706
5707   if (sticky) {
5708     if (ret == GST_FLOW_OK) {
5709       GST_OBJECT_LOCK (pad);
5710       /* after the event function accepted the event, we can store the sticky
5711        * event on the pad */
5712       switch (store_sticky_event (pad, event)) {
5713         case GST_FLOW_FLUSHING:
5714           goto flushing;
5715         case GST_FLOW_EOS:
5716           goto eos;
5717         default:
5718           break;
5719       }
5720       GST_OBJECT_UNLOCK (pad);
5721     }
5722     gst_event_unref (event);
5723   }
5724
5725   if (need_unlock)
5726     GST_PAD_STREAM_UNLOCK (pad);
5727
5728   return ret;
5729
5730   /* ERROR handling */
5731 flushing:
5732   {
5733     GST_OBJECT_UNLOCK (pad);
5734     if (need_unlock)
5735       GST_PAD_STREAM_UNLOCK (pad);
5736     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5737         "Received event on flushing pad. Discarding");
5738     gst_event_unref (event);
5739     return GST_FLOW_FLUSHING;
5740   }
5741 inactive:
5742   {
5743     GST_OBJECT_UNLOCK (pad);
5744     if (need_unlock)
5745       GST_PAD_STREAM_UNLOCK (pad);
5746     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5747         "Received flush-stop on inactive pad. Discarding");
5748     gst_event_unref (event);
5749     return GST_FLOW_FLUSHING;
5750   }
5751 eos:
5752   {
5753     GST_OBJECT_UNLOCK (pad);
5754     if (need_unlock)
5755       GST_PAD_STREAM_UNLOCK (pad);
5756     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5757         "Received event on EOS pad. Discarding");
5758     gst_event_unref (event);
5759     return GST_FLOW_EOS;
5760   }
5761 probe_stopped:
5762   {
5763     GST_OBJECT_UNLOCK (pad);
5764     if (need_unlock)
5765       GST_PAD_STREAM_UNLOCK (pad);
5766     /* Only unref if unhandled */
5767     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5768       gst_event_unref (event);
5769
5770     switch (ret) {
5771       case GST_FLOW_CUSTOM_SUCCESS_1:
5772       case GST_FLOW_CUSTOM_SUCCESS:
5773         GST_DEBUG_OBJECT (pad, "dropped or handled event");
5774         ret = GST_FLOW_OK;
5775         break;
5776       default:
5777         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5778         break;
5779     }
5780     return ret;
5781   }
5782 no_function:
5783   {
5784     g_warning ("pad %s:%s has no event handler, file a bug.",
5785         GST_DEBUG_PAD_NAME (pad));
5786     GST_OBJECT_UNLOCK (pad);
5787     if (need_unlock)
5788       GST_PAD_STREAM_UNLOCK (pad);
5789     gst_event_unref (event);
5790     return GST_FLOW_NOT_SUPPORTED;
5791   }
5792 no_parent:
5793   {
5794     GST_DEBUG_OBJECT (pad, "no parent");
5795     GST_OBJECT_UNLOCK (pad);
5796     if (need_unlock)
5797       GST_PAD_STREAM_UNLOCK (pad);
5798     gst_event_unref (event);
5799     return GST_FLOW_FLUSHING;
5800   }
5801 precheck_failed:
5802   {
5803     GST_DEBUG_OBJECT (pad, "pre event check failed");
5804     RELEASE_PARENT (parent);
5805     if (need_unlock)
5806       GST_PAD_STREAM_UNLOCK (pad);
5807     gst_event_unref (event);
5808     return ret;
5809   }
5810 }
5811
5812 /**
5813  * gst_pad_send_event:
5814  * @pad: a #GstPad to send the event to.
5815  * @event: (transfer full): the #GstEvent to send to the pad.
5816  *
5817  * Sends the event to the pad. This function can be used
5818  * by applications to send events in the pipeline.
5819  *
5820  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5821  * sink pad, @event should be a downstream event. For example, you would not
5822  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5823  * Furthermore, some downstream events have to be serialized with data flow,
5824  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5825  * the event needs to be serialized with data flow, this function will take the
5826  * pad's stream lock while calling its event function.
5827  *
5828  * To find out whether an event type is upstream, downstream, or downstream and
5829  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5830  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5831  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5832  * plugin doesn't need to bother itself with this information; the core handles
5833  * all necessary locks and checks.
5834  *
5835  * This function takes ownership of the provided event so you should
5836  * gst_event_ref() it if you want to reuse the event after this call.
5837  *
5838  * Returns: %TRUE if the event was handled.
5839  */
5840 gboolean
5841 gst_pad_send_event (GstPad * pad, GstEvent * event)
5842 {
5843   gboolean result;
5844   GstPadProbeType type;
5845
5846   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5847   g_return_val_if_fail (event != NULL, FALSE);
5848
5849   if (GST_PAD_IS_SINK (pad)) {
5850     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5851       goto wrong_direction;
5852     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5853   } else if (GST_PAD_IS_SRC (pad)) {
5854     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5855       goto wrong_direction;
5856     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5857   } else
5858     goto unknown_direction;
5859
5860   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5861     result = FALSE;
5862   else
5863     result = TRUE;
5864
5865   return result;
5866
5867   /* ERROR handling */
5868 wrong_direction:
5869   {
5870     g_warning ("pad %s:%s sending %s event in wrong direction",
5871         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5872     gst_event_unref (event);
5873     return FALSE;
5874   }
5875 unknown_direction:
5876   {
5877     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5878     gst_event_unref (event);
5879     return FALSE;
5880   }
5881 }
5882
5883 /**
5884  * gst_pad_set_element_private:
5885  * @pad: the #GstPad to set the private data of.
5886  * @priv: The private data to attach to the pad.
5887  *
5888  * Set the given private data gpointer on the pad.
5889  * This function can only be used by the element that owns the pad.
5890  * No locking is performed in this function.
5891  */
5892 void
5893 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5894 {
5895   pad->element_private = priv;
5896 }
5897
5898 /**
5899  * gst_pad_get_element_private:
5900  * @pad: the #GstPad to get the private data of.
5901  *
5902  * Gets the private data of a pad.
5903  * No locking is performed in this function.
5904  *
5905  * Returns: (transfer none): a #gpointer to the private data.
5906  */
5907 gpointer
5908 gst_pad_get_element_private (GstPad * pad)
5909 {
5910   return pad->element_private;
5911 }
5912
5913 /**
5914  * gst_pad_get_sticky_event:
5915  * @pad: the #GstPad to get the event from.
5916  * @event_type: the #GstEventType that should be retrieved.
5917  * @idx: the index of the event
5918  *
5919  * Returns a new reference of the sticky event of type @event_type
5920  * from the event.
5921  *
5922  * Returns: (transfer full) (nullable): a #GstEvent of type
5923  * @event_type or %NULL when no event of @event_type was on
5924  * @pad. Unref after usage.
5925  */
5926 GstEvent *
5927 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5928 {
5929   GstEvent *event = NULL;
5930   PadEvent *ev;
5931
5932   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5933   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5934
5935   GST_OBJECT_LOCK (pad);
5936   ev = find_event_by_type (pad, event_type, idx);
5937   if (ev && (event = ev->event))
5938     gst_event_ref (event);
5939   GST_OBJECT_UNLOCK (pad);
5940
5941   return event;
5942 }
5943
5944 typedef struct
5945 {
5946   GstPadStickyEventsForeachFunction func;
5947   gpointer user_data;
5948 } ForeachDispatch;
5949
5950 static gboolean
5951 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5952 {
5953   ForeachDispatch *data = user_data;
5954   gboolean ret = TRUE;
5955
5956   if (ev->event) {
5957     GST_OBJECT_UNLOCK (pad);
5958
5959     ret = data->func (pad, &ev->event, data->user_data);
5960
5961     GST_OBJECT_LOCK (pad);
5962   }
5963
5964   return ret;
5965 }
5966
5967 /**
5968  * gst_pad_sticky_events_foreach:
5969  * @pad: the #GstPad that should be used for iteration.
5970  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5971  *                should be called for every event.
5972  * @user_data: (closure): the optional user data.
5973  *
5974  * Iterates all sticky events on @pad and calls @foreach_func for every
5975  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5976  */
5977 void
5978 gst_pad_sticky_events_foreach (GstPad * pad,
5979     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5980 {
5981   ForeachDispatch data;
5982
5983   g_return_if_fail (GST_IS_PAD (pad));
5984   g_return_if_fail (foreach_func != NULL);
5985
5986   data.func = foreach_func;
5987   data.user_data = user_data;
5988
5989   GST_OBJECT_LOCK (pad);
5990   events_foreach (pad, foreach_dispatch_function, &data);
5991   GST_OBJECT_UNLOCK (pad);
5992 }
5993
5994 static void
5995 do_stream_status (GstPad * pad, GstStreamStatusType type,
5996     GThread * thread, GstTask * task)
5997 {
5998   GstElement *parent;
5999
6000   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
6001
6002   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
6003     if (GST_IS_ELEMENT (parent)) {
6004       GstMessage *message;
6005       GValue value = { 0 };
6006
6007       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
6008         gchar *tname, *ename, *pname;
6009
6010         /* create a good task name */
6011         ename = gst_element_get_name (parent);
6012         pname = gst_pad_get_name (pad);
6013         tname = g_strdup_printf ("%s:%s", ename, pname);
6014         g_free (ename);
6015         g_free (pname);
6016
6017         gst_object_set_name (GST_OBJECT_CAST (task), tname);
6018         g_free (tname);
6019       }
6020
6021       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
6022           type, parent);
6023
6024       g_value_init (&value, GST_TYPE_TASK);
6025       g_value_set_object (&value, task);
6026       gst_message_set_stream_status_object (message, &value);
6027       g_value_unset (&value);
6028
6029       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
6030       gst_element_post_message (parent, message);
6031     }
6032     gst_object_unref (parent);
6033   }
6034 }
6035
6036 static void
6037 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
6038 {
6039   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
6040       thread, task);
6041 }
6042
6043 static void
6044 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
6045 {
6046   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
6047       thread, task);
6048 }
6049
6050 /**
6051  * gst_pad_start_task:
6052  * @pad: the #GstPad to start the task of
6053  * @func: the task function to call
6054  * @user_data: user data passed to the task function
6055  * @notify: called when @user_data is no longer referenced
6056  *
6057  * Starts a task that repeatedly calls @func with @user_data. This function
6058  * is mostly used in pad activation functions to start the dataflow.
6059  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
6060  * before @func is called.
6061  *
6062  * Returns: a %TRUE if the task could be started.
6063  */
6064 gboolean
6065 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
6066     GDestroyNotify notify)
6067 {
6068   GstTask *task;
6069   gboolean res;
6070
6071   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6072   g_return_val_if_fail (func != NULL, FALSE);
6073
6074   GST_DEBUG_OBJECT (pad, "start task");
6075
6076   GST_OBJECT_LOCK (pad);
6077   task = GST_PAD_TASK (pad);
6078   if (task == NULL) {
6079     task = gst_task_new (func, user_data, notify);
6080     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
6081     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
6082     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
6083     GST_INFO_OBJECT (pad, "created task %p", task);
6084     GST_PAD_TASK (pad) = task;
6085     gst_object_ref (task);
6086     /* release lock to post the message */
6087     GST_OBJECT_UNLOCK (pad);
6088
6089     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
6090
6091     gst_object_unref (task);
6092
6093     GST_OBJECT_LOCK (pad);
6094     /* nobody else is supposed to have changed the pad now */
6095     if (GST_PAD_TASK (pad) != task)
6096       goto concurrent_stop;
6097   }
6098   res = gst_task_set_state (task, GST_TASK_STARTED);
6099   GST_OBJECT_UNLOCK (pad);
6100
6101   return res;
6102
6103   /* ERRORS */
6104 concurrent_stop:
6105   {
6106     GST_OBJECT_UNLOCK (pad);
6107     return TRUE;
6108   }
6109 }
6110
6111 /**
6112  * gst_pad_pause_task:
6113  * @pad: the #GstPad to pause the task of
6114  *
6115  * Pause the task of @pad. This function will also wait until the
6116  * function executed by the task is finished if this function is not
6117  * called from the task function.
6118  *
6119  * Returns: a %TRUE if the task could be paused or %FALSE when the pad
6120  * has no task.
6121  */
6122 gboolean
6123 gst_pad_pause_task (GstPad * pad)
6124 {
6125   GstTask *task;
6126   gboolean res;
6127
6128   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6129
6130   GST_DEBUG_OBJECT (pad, "pause task");
6131
6132   GST_OBJECT_LOCK (pad);
6133   task = GST_PAD_TASK (pad);
6134   if (task == NULL)
6135     goto no_task;
6136   res = gst_task_set_state (task, GST_TASK_PAUSED);
6137   /* unblock activation waits if any */
6138   pad->priv->in_activation = FALSE;
6139   g_cond_broadcast (&pad->priv->activation_cond);
6140   GST_OBJECT_UNLOCK (pad);
6141
6142   /* wait for task function to finish, this lock is recursive so it does nothing
6143    * when the pause is called from the task itself */
6144   GST_PAD_STREAM_LOCK (pad);
6145   GST_PAD_STREAM_UNLOCK (pad);
6146
6147   return res;
6148
6149 no_task:
6150   {
6151     GST_DEBUG_OBJECT (pad, "pad has no task");
6152     GST_OBJECT_UNLOCK (pad);
6153     return FALSE;
6154   }
6155 }
6156
6157 /**
6158  * gst_pad_get_task_state:
6159  * @pad: the #GstPad to get task state from
6160  *
6161  * Get @pad task state. If no task is currently
6162  * set, #GST_TASK_STOPPED is returned.
6163  *
6164  * Returns: The current state of @pad's task.
6165  *
6166  * Since: 1.12
6167  */
6168 GstTaskState
6169 gst_pad_get_task_state (GstPad * pad)
6170 {
6171   GstTask *task;
6172   GstTaskState res;
6173
6174   g_return_val_if_fail (GST_IS_PAD (pad), GST_TASK_STOPPED);
6175
6176   GST_OBJECT_LOCK (pad);
6177   task = GST_PAD_TASK (pad);
6178   if (task == NULL)
6179     goto no_task;
6180   res = gst_task_get_state (task);
6181   GST_OBJECT_UNLOCK (pad);
6182
6183   return res;
6184
6185 no_task:
6186   {
6187     GST_DEBUG_OBJECT (pad, "pad has no task");
6188     GST_OBJECT_UNLOCK (pad);
6189     return GST_TASK_STOPPED;
6190   }
6191 }
6192
6193 /**
6194  * gst_pad_stop_task:
6195  * @pad: the #GstPad to stop the task of
6196  *
6197  * Stop the task of @pad. This function will also make sure that the
6198  * function executed by the task will effectively stop if not called
6199  * from the GstTaskFunction.
6200  *
6201  * This function will deadlock if called from the GstTaskFunction of
6202  * the task. Use gst_task_pause() instead.
6203  *
6204  * Regardless of whether the pad has a task, the stream lock is acquired and
6205  * released so as to ensure that streaming through this pad has finished.
6206  *
6207  * Returns: a %TRUE if the task could be stopped or %FALSE on error.
6208  */
6209 gboolean
6210 gst_pad_stop_task (GstPad * pad)
6211 {
6212   GstTask *task;
6213   gboolean res;
6214
6215   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6216
6217   GST_DEBUG_OBJECT (pad, "stop task");
6218
6219   GST_OBJECT_LOCK (pad);
6220   task = GST_PAD_TASK (pad);
6221   if (task == NULL)
6222     goto no_task;
6223   GST_PAD_TASK (pad) = NULL;
6224   res = gst_task_set_state (task, GST_TASK_STOPPED);
6225   /* unblock activation waits if any */
6226   pad->priv->in_activation = FALSE;
6227   g_cond_broadcast (&pad->priv->activation_cond);
6228   GST_OBJECT_UNLOCK (pad);
6229
6230   GST_PAD_STREAM_LOCK (pad);
6231   GST_PAD_STREAM_UNLOCK (pad);
6232
6233   if (!gst_task_join (task))
6234     goto join_failed;
6235
6236   gst_object_unref (task);
6237
6238   return res;
6239
6240 no_task:
6241   {
6242     GST_DEBUG_OBJECT (pad, "no task");
6243     GST_OBJECT_UNLOCK (pad);
6244
6245     GST_PAD_STREAM_LOCK (pad);
6246     GST_PAD_STREAM_UNLOCK (pad);
6247
6248     /* this is not an error */
6249     return TRUE;
6250   }
6251 join_failed:
6252   {
6253     /* this is bad, possibly the application tried to join the task from
6254      * the task's thread. We install the task again so that it will be stopped
6255      * again from the right thread next time hopefully. */
6256     GST_OBJECT_LOCK (pad);
6257     GST_DEBUG_OBJECT (pad, "join failed");
6258     /* we can only install this task if there was no other task */
6259     if (GST_PAD_TASK (pad) == NULL)
6260       GST_PAD_TASK (pad) = task;
6261     GST_OBJECT_UNLOCK (pad);
6262
6263     return FALSE;
6264   }
6265 }
6266
6267 /**
6268  * gst_pad_probe_info_get_event:
6269  * @info: a #GstPadProbeInfo
6270  *
6271  * Returns: (transfer none): The #GstEvent from the probe
6272  */
6273
6274 GstEvent *
6275 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6276 {
6277   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6278           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6279
6280   return GST_PAD_PROBE_INFO_EVENT (info);
6281 }
6282
6283
6284 /**
6285  * gst_pad_probe_info_get_query:
6286  * @info: a #GstPadProbeInfo
6287  *
6288  * Returns: (transfer none): The #GstQuery from the probe
6289  */
6290
6291 GstQuery *
6292 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6293 {
6294   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6295           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6296
6297   return GST_PAD_PROBE_INFO_QUERY (info);
6298 }
6299
6300 /**
6301  * gst_pad_probe_info_get_buffer:
6302  * @info: a #GstPadProbeInfo
6303  *
6304  * Returns: (transfer none): The #GstBuffer from the probe
6305  */
6306
6307 GstBuffer *
6308 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6309 {
6310   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6311
6312   return GST_PAD_PROBE_INFO_BUFFER (info);
6313 }
6314
6315 /**
6316  * gst_pad_probe_info_get_buffer_list:
6317  * @info: a #GstPadProbeInfo
6318  *
6319  * Returns: (transfer none): The #GstBufferList from the probe
6320  */
6321
6322 GstBufferList *
6323 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6324 {
6325   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6326
6327   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6328 }
6329
6330 /**
6331  * gst_pad_get_last_flow_return:
6332  * @pad: the #GstPad
6333  *
6334  * Gets the #GstFlowReturn return from the last data passed by this pad.
6335  *
6336  * Since: 1.4
6337  */
6338 GstFlowReturn
6339 gst_pad_get_last_flow_return (GstPad * pad)
6340 {
6341   GstFlowReturn ret;
6342
6343   GST_OBJECT_LOCK (pad);
6344   ret = GST_PAD_LAST_FLOW_RETURN (pad);
6345   GST_OBJECT_UNLOCK (pad);
6346
6347   return ret;
6348 }