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