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