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