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