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