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