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