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