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