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