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