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