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