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