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