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