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