pad: resend dropped events
[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_CUSTOM_SUCCESS:
3295       /* we can't assume the event is received when it was dropped */
3296       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3297           GST_EVENT_TYPE_NAME (event));
3298       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3299       data->ret = GST_FLOW_OK;
3300       break;
3301     case GST_FLOW_NOT_LINKED:
3302       /* not linked is not a problem, we are sticky so the event will be
3303        * sent later but only for non-EOS events */
3304       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3305       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3306         data->ret = GST_FLOW_OK;
3307       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3308       break;
3309     default:
3310       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3311           gst_flow_get_name (data->ret));
3312       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3313       break;
3314   }
3315
3316   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3317     data->was_eos = TRUE;
3318
3319   return data->ret == GST_FLOW_OK;
3320 }
3321
3322 /* check sticky events and push them when needed. should be called
3323  * with pad LOCK */
3324 static inline GstFlowReturn
3325 check_sticky (GstPad * pad)
3326 {
3327   PushStickyData data = { GST_FLOW_OK, FALSE };
3328
3329   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3330     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3331
3332     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3333     events_foreach (pad, push_sticky, &data);
3334
3335     /* If there's an EOS event we must push it downstream
3336      * even if sending a previous sticky event failed.
3337      * Otherwise the pipeline might wait forever for EOS.
3338      *
3339      * Only do this if pushing another event than the EOS
3340      * event failed.
3341      */
3342     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3343       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3344
3345       if (ev && !ev->received) {
3346         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3347             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3348         /* the event could have been dropped. Because this can only
3349          * happen if the user asked for it, it's not an error */
3350         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3351           data.ret = GST_FLOW_OK;
3352       }
3353     }
3354   }
3355   return data.ret;
3356 }
3357
3358
3359 /**
3360  * gst_pad_query:
3361  * @pad: a #GstPad to invoke the default query on.
3362  * @query: (transfer none): the #GstQuery to perform.
3363  *
3364  * Dispatches a query to a pad. The query should have been allocated by the
3365  * caller via one of the type-specific allocation functions. The element that
3366  * the pad belongs to is responsible for filling the query with an appropriate
3367  * response, which should then be parsed with a type-specific query parsing
3368  * function.
3369  *
3370  * Again, the caller is responsible for both the allocation and deallocation of
3371  * the query structure.
3372  *
3373  * Please also note that some queries might need a running pipeline to work.
3374  *
3375  * Returns: TRUE if the query could be performed.
3376  */
3377 gboolean
3378 gst_pad_query (GstPad * pad, GstQuery * query)
3379 {
3380   GstObject *parent;
3381   gboolean res, serialized;
3382   GstPadQueryFunction func;
3383   GstPadProbeType type;
3384   GstFlowReturn ret;
3385
3386   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3387   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3388
3389   if (GST_PAD_IS_SRC (pad)) {
3390     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3391       goto wrong_direction;
3392     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3393   } else if (GST_PAD_IS_SINK (pad)) {
3394     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3395       goto wrong_direction;
3396     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3397   } else
3398     goto unknown_direction;
3399
3400   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3401       GST_QUERY_TYPE_NAME (query));
3402
3403   serialized = GST_QUERY_IS_SERIALIZED (query);
3404   if (G_UNLIKELY (serialized))
3405     GST_PAD_STREAM_LOCK (pad);
3406
3407   GST_OBJECT_LOCK (pad);
3408   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3409       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3410   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3411
3412   ACQUIRE_PARENT (pad, parent, no_parent);
3413   GST_OBJECT_UNLOCK (pad);
3414
3415   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3416     goto no_func;
3417
3418   res = func (pad, parent, query);
3419
3420   RELEASE_PARENT (parent);
3421
3422   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3423       GST_QUERY_TYPE_NAME (query), res);
3424
3425   if (res != TRUE)
3426     goto query_failed;
3427
3428   GST_OBJECT_LOCK (pad);
3429   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3430   GST_OBJECT_UNLOCK (pad);
3431
3432   if (G_UNLIKELY (serialized))
3433     GST_PAD_STREAM_UNLOCK (pad);
3434
3435   return res;
3436
3437   /* ERRORS */
3438 wrong_direction:
3439   {
3440     g_warning ("pad %s:%s query %s in wrong direction",
3441         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3442     return FALSE;
3443   }
3444 unknown_direction:
3445   {
3446     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3447     return FALSE;
3448   }
3449 no_parent:
3450   {
3451     GST_DEBUG_OBJECT (pad, "had no parent");
3452     GST_OBJECT_UNLOCK (pad);
3453     if (G_UNLIKELY (serialized))
3454       GST_PAD_STREAM_UNLOCK (pad);
3455     return FALSE;
3456   }
3457 no_func:
3458   {
3459     GST_DEBUG_OBJECT (pad, "had no query function");
3460     RELEASE_PARENT (parent);
3461     if (G_UNLIKELY (serialized))
3462       GST_PAD_STREAM_UNLOCK (pad);
3463     return FALSE;
3464   }
3465 query_failed:
3466   {
3467     GST_DEBUG_OBJECT (pad, "query failed");
3468     if (G_UNLIKELY (serialized))
3469       GST_PAD_STREAM_UNLOCK (pad);
3470     return FALSE;
3471   }
3472 probe_stopped:
3473   {
3474     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3475     GST_OBJECT_UNLOCK (pad);
3476     if (G_UNLIKELY (serialized))
3477       GST_PAD_STREAM_UNLOCK (pad);
3478
3479     /* if a probe dropped, we don't sent it further but assume that the probe
3480      * answered the query and return TRUE */
3481     if (ret == GST_FLOW_CUSTOM_SUCCESS)
3482       res = TRUE;
3483     else
3484       res = FALSE;
3485
3486     return res;
3487   }
3488 }
3489
3490 /**
3491  * gst_pad_peer_query:
3492  * @pad: a #GstPad to invoke the peer query on.
3493  * @query: (transfer none): the #GstQuery to perform.
3494  *
3495  * Performs gst_pad_query() on the peer of @pad.
3496  *
3497  * The caller is responsible for both the allocation and deallocation of
3498  * the query structure.
3499  *
3500  * Returns: TRUE if the query could be performed. This function returns %FALSE
3501  * if @pad has no peer.
3502  */
3503 gboolean
3504 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3505 {
3506   GstPad *peerpad;
3507   GstPadProbeType type;
3508   gboolean res, serialized;
3509   GstFlowReturn ret;
3510
3511   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3512   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3513
3514   if (GST_PAD_IS_SRC (pad)) {
3515     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3516       goto wrong_direction;
3517     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3518   } else if (GST_PAD_IS_SINK (pad)) {
3519     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3520       goto wrong_direction;
3521     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3522   } else
3523     goto unknown_direction;
3524
3525   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3526       GST_QUERY_TYPE_NAME (query));
3527
3528   serialized = GST_QUERY_IS_SERIALIZED (query);
3529
3530   GST_OBJECT_LOCK (pad);
3531   if (GST_PAD_IS_SRC (pad) && serialized) {
3532     /* all serialized queries on the srcpad trigger push of
3533      * sticky events */
3534     if (!check_sticky (pad) == GST_FLOW_OK)
3535       goto sticky_failed;
3536   }
3537
3538   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3539       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3540   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3541
3542   peerpad = GST_PAD_PEER (pad);
3543   if (G_UNLIKELY (peerpad == NULL))
3544     goto no_peer;
3545
3546   gst_object_ref (peerpad);
3547   GST_OBJECT_UNLOCK (pad);
3548
3549   res = gst_pad_query (peerpad, query);
3550
3551   gst_object_unref (peerpad);
3552
3553   if (res != TRUE)
3554     goto query_failed;
3555
3556   GST_OBJECT_LOCK (pad);
3557   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3558   GST_OBJECT_UNLOCK (pad);
3559
3560   return res;
3561
3562   /* ERRORS */
3563 wrong_direction:
3564   {
3565     g_warning ("pad %s:%s query %s in wrong direction",
3566         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3567     return FALSE;
3568   }
3569 unknown_direction:
3570   {
3571     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3572     return FALSE;
3573   }
3574 sticky_failed:
3575   {
3576     GST_WARNING_OBJECT (pad, "could not send sticky events");
3577     GST_OBJECT_UNLOCK (pad);
3578     return FALSE;
3579   }
3580 no_peer:
3581   {
3582     GST_WARNING_OBJECT (pad, "pad has no peer");
3583     GST_OBJECT_UNLOCK (pad);
3584     return FALSE;
3585   }
3586 query_failed:
3587   {
3588     GST_DEBUG_OBJECT (pad, "query failed");
3589     return FALSE;
3590   }
3591 probe_stopped:
3592   {
3593     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3594     GST_OBJECT_UNLOCK (pad);
3595
3596     /* if a probe dropped, we don't sent it further but assume that the probe
3597      * answered the query and return TRUE */
3598     if (ret == GST_FLOW_CUSTOM_SUCCESS)
3599       res = TRUE;
3600     else
3601       res = FALSE;
3602
3603     return res;
3604   }
3605 }
3606
3607 /**********************************************************************
3608  * Data passing functions
3609  */
3610
3611 /* this is the chain function that does not perform the additional argument
3612  * checking for that little extra speed.
3613  */
3614 static inline GstFlowReturn
3615 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3616 {
3617   GstFlowReturn ret;
3618   GstObject *parent;
3619
3620   GST_PAD_STREAM_LOCK (pad);
3621
3622   GST_OBJECT_LOCK (pad);
3623   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3624     goto flushing;
3625
3626   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3627     goto eos;
3628
3629   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3630     goto wrong_mode;
3631
3632   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3633
3634   PROBE_PUSH (pad, type, data, probe_stopped);
3635
3636   parent = GST_OBJECT_PARENT (pad);
3637   GST_OBJECT_UNLOCK (pad);
3638
3639   /* NOTE: we read the chainfunc unlocked.
3640    * we cannot hold the lock for the pad so we might send
3641    * the data to the wrong function. This is not really a
3642    * problem since functions are assigned at creation time
3643    * and don't change that often... */
3644   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3645     GstPadChainFunction chainfunc;
3646
3647     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3648       goto no_function;
3649
3650     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3651         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3652         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3653
3654     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3655
3656     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3657         "called chainfunction &%s with buffer %p, returned %s",
3658         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3659   } else {
3660     GstPadChainListFunction chainlistfunc;
3661
3662     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3663       goto no_function;
3664
3665     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3666         "calling chainlistfunction &%s",
3667         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3668
3669     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3670
3671     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3672         "called chainlistfunction &%s, returned %s",
3673         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3674   }
3675
3676   GST_PAD_STREAM_UNLOCK (pad);
3677
3678   return ret;
3679
3680   /* ERRORS */
3681 flushing:
3682   {
3683     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3684         "chaining, but pad was flushing");
3685     GST_OBJECT_UNLOCK (pad);
3686     GST_PAD_STREAM_UNLOCK (pad);
3687     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3688     return GST_FLOW_FLUSHING;
3689   }
3690 eos:
3691   {
3692     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
3693     GST_OBJECT_UNLOCK (pad);
3694     GST_PAD_STREAM_UNLOCK (pad);
3695     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3696     return GST_FLOW_EOS;
3697   }
3698 wrong_mode:
3699   {
3700     g_critical ("chain on pad %s:%s but it was not in push mode",
3701         GST_DEBUG_PAD_NAME (pad));
3702     GST_OBJECT_UNLOCK (pad);
3703     GST_PAD_STREAM_UNLOCK (pad);
3704     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3705     return GST_FLOW_ERROR;
3706   }
3707 probe_stopped:
3708   {
3709     GST_OBJECT_UNLOCK (pad);
3710     GST_PAD_STREAM_UNLOCK (pad);
3711     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3712
3713     switch (ret) {
3714       case GST_FLOW_CUSTOM_SUCCESS:
3715         GST_DEBUG_OBJECT (pad, "dropped buffer");
3716         ret = GST_FLOW_OK;
3717         break;
3718       default:
3719         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3720         break;
3721     }
3722     return ret;
3723   }
3724 no_function:
3725   {
3726     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3727     g_critical ("chain on pad %s:%s but it has no chainfunction",
3728         GST_DEBUG_PAD_NAME (pad));
3729     GST_PAD_STREAM_UNLOCK (pad);
3730     return GST_FLOW_NOT_SUPPORTED;
3731   }
3732 }
3733
3734 /**
3735  * gst_pad_chain:
3736  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3737  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3738  *     if not.
3739  *
3740  * Chain a buffer to @pad.
3741  *
3742  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3743  *
3744  * If the buffer type is not acceptable for @pad (as negotiated with a
3745  * preceeding GST_EVENT_CAPS event), this function returns
3746  * #GST_FLOW_NOT_NEGOTIATED.
3747  *
3748  * The function proceeds calling the chain function installed on @pad (see
3749  * gst_pad_set_chain_function()) and the return value of that function is
3750  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3751  * chain function.
3752  *
3753  * In all cases, success or failure, the caller loses its reference to @buffer
3754  * after calling this function.
3755  *
3756  * Returns: a #GstFlowReturn from the pad.
3757  *
3758  * MT safe.
3759  */
3760 GstFlowReturn
3761 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3762 {
3763   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3764   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3765   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3766
3767   return gst_pad_chain_data_unchecked (pad,
3768       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3769 }
3770
3771 static GstFlowReturn
3772 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3773     GstBufferList * list)
3774 {
3775   guint i, len;
3776   GstBuffer *buffer;
3777   GstFlowReturn ret;
3778
3779   GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3780
3781   len = gst_buffer_list_length (list);
3782
3783   ret = GST_FLOW_OK;
3784   for (i = 0; i < len; i++) {
3785     buffer = gst_buffer_list_get (list, i);
3786     ret =
3787         gst_pad_chain_data_unchecked (pad,
3788         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3789         gst_buffer_ref (buffer));
3790     if (ret != GST_FLOW_OK)
3791       break;
3792   }
3793   gst_buffer_list_unref (list);
3794
3795   return ret;
3796 }
3797
3798 /**
3799  * gst_pad_chain_list:
3800  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3801  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3802  *     if not.
3803  *
3804  * Chain a bufferlist to @pad.
3805  *
3806  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3807  *
3808  * If @pad was not negotiated properly with a CAPS event, this function
3809  * returns #GST_FLOW_NOT_NEGOTIATED.
3810  *
3811  * The function proceeds calling the chainlist function installed on @pad (see
3812  * gst_pad_set_chain_list_function()) and the return value of that function is
3813  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3814  * chainlist function.
3815  *
3816  * In all cases, success or failure, the caller loses its reference to @list
3817  * after calling this function.
3818  *
3819  * MT safe.
3820  *
3821  * Returns: a #GstFlowReturn from the pad.
3822  */
3823 GstFlowReturn
3824 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3825 {
3826   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3827   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3828   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3829
3830   return gst_pad_chain_data_unchecked (pad,
3831       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3832 }
3833
3834 static GstFlowReturn
3835 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3836 {
3837   GstPad *peer;
3838   GstFlowReturn ret;
3839
3840   GST_OBJECT_LOCK (pad);
3841   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3842     goto flushing;
3843
3844   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3845     goto eos;
3846
3847   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3848     goto wrong_mode;
3849
3850   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3851     goto events_error;
3852
3853   /* do block probes */
3854   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3855
3856   /* recheck sticky events because the probe might have cause a relink */
3857   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3858     goto events_error;
3859
3860   /* do post-blocking probes */
3861   PROBE_PUSH (pad, type, data, probe_stopped);
3862
3863   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3864     goto not_linked;
3865
3866   /* take ref to peer pad before releasing the lock */
3867   gst_object_ref (peer);
3868   pad->priv->using++;
3869   GST_OBJECT_UNLOCK (pad);
3870
3871   ret = gst_pad_chain_data_unchecked (peer, type, data);
3872
3873   gst_object_unref (peer);
3874
3875   GST_OBJECT_LOCK (pad);
3876   pad->priv->using--;
3877   if (pad->priv->using == 0) {
3878     /* pad is not active anymore, trigger idle callbacks */
3879     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3880         probe_stopped, ret);
3881   }
3882   GST_OBJECT_UNLOCK (pad);
3883
3884   return ret;
3885
3886   /* ERROR recovery here */
3887   /* ERRORS */
3888 flushing:
3889   {
3890     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3891         "pushing, but pad was flushing");
3892     GST_OBJECT_UNLOCK (pad);
3893     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3894     return GST_FLOW_FLUSHING;
3895   }
3896 eos:
3897   {
3898     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
3899     GST_OBJECT_UNLOCK (pad);
3900     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3901     return GST_FLOW_EOS;
3902   }
3903 wrong_mode:
3904   {
3905     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3906         GST_DEBUG_PAD_NAME (pad));
3907     GST_OBJECT_UNLOCK (pad);
3908     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3909     return GST_FLOW_ERROR;
3910   }
3911 events_error:
3912   {
3913     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3914         "error pushing events, return %s", gst_flow_get_name (ret));
3915     GST_OBJECT_UNLOCK (pad);
3916     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3917     return ret;
3918   }
3919 probe_stopped:
3920   {
3921     GST_OBJECT_UNLOCK (pad);
3922     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3923
3924     switch (ret) {
3925       case GST_FLOW_CUSTOM_SUCCESS:
3926         GST_DEBUG_OBJECT (pad, "dropped buffer");
3927         ret = GST_FLOW_OK;
3928         break;
3929       default:
3930         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3931         break;
3932     }
3933     return ret;
3934   }
3935 not_linked:
3936   {
3937     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3938         "pushing, but it was not linked");
3939     GST_OBJECT_UNLOCK (pad);
3940     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3941     return GST_FLOW_NOT_LINKED;
3942   }
3943 }
3944
3945 /**
3946  * gst_pad_push:
3947  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3948  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3949  *     if not.
3950  *
3951  * Pushes a buffer to the peer of @pad.
3952  *
3953  * This function will call installed block probes before triggering any
3954  * installed data probes.
3955  *
3956  * The function proceeds calling gst_pad_chain() on the peer pad and returns
3957  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3958  * be returned.
3959  *
3960  * In all cases, success or failure, the caller loses its reference to @buffer
3961  * after calling this function.
3962  *
3963  * Returns: a #GstFlowReturn from the peer pad.
3964  *
3965  * MT safe.
3966  */
3967 GstFlowReturn
3968 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3969 {
3970   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3971   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3972   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3973
3974   return gst_pad_push_data (pad,
3975       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3976 }
3977
3978 /**
3979  * gst_pad_push_list:
3980  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3981  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3982  *     if not.
3983  *
3984  * Pushes a buffer list to the peer of @pad.
3985  *
3986  * This function will call installed block probes before triggering any
3987  * installed data probes.
3988  *
3989  * The function proceeds calling the chain function on the peer pad and returns
3990  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3991  * be returned. If the peer pad does not have any installed chainlist function
3992  * every group buffer of the list will be merged into a normal #GstBuffer and
3993  * chained via gst_pad_chain().
3994  *
3995  * In all cases, success or failure, the caller loses its reference to @list
3996  * after calling this function.
3997  *
3998  * Returns: a #GstFlowReturn from the peer pad.
3999  *
4000  * MT safe.
4001  */
4002 GstFlowReturn
4003 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4004 {
4005   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4006   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4007   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4008
4009   return gst_pad_push_data (pad,
4010       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4011 }
4012
4013 static GstFlowReturn
4014 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4015     GstBuffer ** buffer)
4016 {
4017   GstFlowReturn ret;
4018   GstPadGetRangeFunction getrangefunc;
4019   GstObject *parent;
4020   GstBuffer *res_buf;
4021
4022   GST_PAD_STREAM_LOCK (pad);
4023
4024   GST_OBJECT_LOCK (pad);
4025   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4026     goto flushing;
4027
4028   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4029     goto wrong_mode;
4030
4031   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4032     goto events_error;
4033
4034   res_buf = *buffer;
4035
4036   /* when one of the probes returns DROPPED, probe_stopped will be called
4037    * and we skip calling the getrange function, res_buf should then contain a
4038    * valid result buffer */
4039   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4040       res_buf, offset, size, probe_stopped);
4041
4042   /* recheck sticky events because the probe might have cause a relink */
4043   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4044     goto events_error;
4045
4046   ACQUIRE_PARENT (pad, parent, no_parent);
4047   GST_OBJECT_UNLOCK (pad);
4048
4049   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4050     goto no_function;
4051
4052   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4053       "calling getrangefunc %s, offset %"
4054       G_GUINT64_FORMAT ", size %u",
4055       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4056
4057   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4058
4059   RELEASE_PARENT (parent);
4060
4061   if (G_UNLIKELY (ret != GST_FLOW_OK))
4062     goto get_range_failed;
4063
4064   /* can only fire the signal if we have a valid buffer */
4065   GST_OBJECT_LOCK (pad);
4066 probed_data:
4067   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4068       res_buf, offset, size, probe_stopped_unref);
4069   GST_OBJECT_UNLOCK (pad);
4070
4071   GST_PAD_STREAM_UNLOCK (pad);
4072
4073   *buffer = res_buf;
4074
4075   return ret;
4076
4077   /* ERRORS */
4078 flushing:
4079   {
4080     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4081         "getrange, but pad was flushing");
4082     GST_OBJECT_UNLOCK (pad);
4083     GST_PAD_STREAM_UNLOCK (pad);
4084     return GST_FLOW_FLUSHING;
4085   }
4086 wrong_mode:
4087   {
4088     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4089         GST_DEBUG_PAD_NAME (pad));
4090     GST_OBJECT_UNLOCK (pad);
4091     GST_PAD_STREAM_UNLOCK (pad);
4092     return GST_FLOW_ERROR;
4093   }
4094 events_error:
4095   {
4096     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4097     GST_OBJECT_UNLOCK (pad);
4098     GST_PAD_STREAM_UNLOCK (pad);
4099     return ret;
4100   }
4101 no_parent:
4102   {
4103     GST_DEBUG_OBJECT (pad, "no parent");
4104     GST_OBJECT_UNLOCK (pad);
4105     GST_PAD_STREAM_UNLOCK (pad);
4106     return GST_FLOW_FLUSHING;
4107   }
4108 no_function:
4109   {
4110     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4111         GST_DEBUG_PAD_NAME (pad));
4112     RELEASE_PARENT (parent);
4113     GST_PAD_STREAM_UNLOCK (pad);
4114     return GST_FLOW_NOT_SUPPORTED;
4115   }
4116 probe_stopped:
4117   {
4118     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4119         "probe returned %s", gst_flow_get_name (ret));
4120     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4121       if (res_buf) {
4122         /* the probe filled the buffer and asks us to not call the getrange
4123          * anymore, we continue with the post probes then. */
4124         GST_DEBUG_OBJECT (pad, "handled buffer");
4125         ret = GST_FLOW_OK;
4126         goto probed_data;
4127       } else {
4128         /* no buffer, we are EOS */
4129         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4130         ret = GST_FLOW_EOS;
4131       }
4132     }
4133     GST_OBJECT_UNLOCK (pad);
4134     GST_PAD_STREAM_UNLOCK (pad);
4135
4136     return ret;
4137   }
4138 probe_stopped_unref:
4139   {
4140     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4141         "probe returned %s", gst_flow_get_name (ret));
4142     /* if we drop here, it signals EOS */
4143     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4144       ret = GST_FLOW_EOS;
4145     GST_OBJECT_UNLOCK (pad);
4146     GST_PAD_STREAM_UNLOCK (pad);
4147     if (*buffer == NULL)
4148       gst_buffer_unref (res_buf);
4149     return ret;
4150   }
4151 get_range_failed:
4152   {
4153     GST_PAD_STREAM_UNLOCK (pad);
4154     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4155         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4156         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4157     return ret;
4158   }
4159 }
4160
4161 /**
4162  * gst_pad_get_range:
4163  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4164  * @offset: The start offset of the buffer
4165  * @size: The length of the buffer
4166  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4167  *     returns #GST_FLOW_ERROR if %NULL.
4168  *
4169  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4170  * immediately and @buffer is %NULL.
4171  *
4172  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4173  * description of a getrange function. If @pad has no getrange function
4174  * installed (see gst_pad_set_getrange_function()) this function returns
4175  * #GST_FLOW_NOT_SUPPORTED.
4176  *
4177  * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4178  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4179  * must be freed with gst_buffer_unref() after usage.
4180  *
4181  * When @buffer points to a variable that points to a valid #GstBuffer, the
4182  * buffer will be filled with the result data when this function returns
4183  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4184  * @size bytes will be filled in the result buffer and its size will be updated
4185  * accordingly.
4186  *
4187  * Note that less than @size bytes can be returned in @buffer when, for example,
4188  * an EOS condition is near or when @buffer is not large enough to hold @size
4189  * bytes. The caller should check the result buffer size to get the result size.
4190  *
4191  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4192  * will be unchanged.
4193  *
4194  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4195  *
4196  * Returns: a #GstFlowReturn from the pad.
4197  *
4198  * MT safe.
4199  */
4200 GstFlowReturn
4201 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4202     GstBuffer ** buffer)
4203 {
4204   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4205   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4206   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4207   g_return_val_if_fail (*buffer == NULL
4208       || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4209
4210   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4211 }
4212
4213 /**
4214  * gst_pad_pull_range:
4215  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4216  * @offset: The start offset of the buffer
4217  * @size: The length of the buffer
4218  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4219  *     GST_FLOW_ERROR if %NULL.
4220  *
4221  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4222  *
4223  * This function will first trigger the pad block signal if it was
4224  * installed.
4225  *
4226  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4227  * function returns the result of gst_pad_get_range() on the peer pad.
4228  * See gst_pad_get_range() for a list of return values and for the
4229  * semantics of the arguments of this function.
4230  *
4231  * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4232  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4233  * must be freed with gst_buffer_unref() after usage. When this function
4234  * returns any other result value, @buffer will still point to NULL.
4235  *
4236  * When @buffer points to a variable that points to a valid #GstBuffer, the
4237  * buffer will be filled with the result data when this function returns
4238  * #GST_FLOW_OK. When this function returns any other result value,
4239  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4240  * @size bytes will be filled in the result buffer and its size will be updated
4241  * accordingly.
4242  *
4243  * Note that less than @size bytes can be returned in @buffer when, for example,
4244  * an EOS condition is near or when @buffer is not large enough to hold @size
4245  * bytes. The caller should check the result buffer size to get the result size.
4246  *
4247  * Returns: a #GstFlowReturn from the peer pad.
4248  *
4249  * MT safe.
4250  */
4251 GstFlowReturn
4252 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4253     GstBuffer ** buffer)
4254 {
4255   GstPad *peer;
4256   GstFlowReturn ret;
4257   GstBuffer *res_buf;
4258
4259   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4260   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4261   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4262   g_return_val_if_fail (*buffer == NULL
4263       || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4264
4265   GST_OBJECT_LOCK (pad);
4266   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4267     goto flushing;
4268
4269   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4270     goto wrong_mode;
4271
4272   res_buf = *buffer;
4273
4274   /* when one of the probes returns DROPPED, probe_stopped will be
4275    * called and we skip calling the peer getrange function. *buffer should then
4276    * contain a valid buffer */
4277   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4278       res_buf, offset, size, probe_stopped);
4279
4280   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4281     goto not_linked;
4282
4283   gst_object_ref (peer);
4284   pad->priv->using++;
4285   GST_OBJECT_UNLOCK (pad);
4286
4287   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4288
4289   gst_object_unref (peer);
4290
4291   GST_OBJECT_LOCK (pad);
4292   pad->priv->using--;
4293   if (pad->priv->using == 0) {
4294     /* pad is not active anymore, trigger idle callbacks */
4295     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4296         probe_stopped_unref, ret);
4297   }
4298
4299   if (G_UNLIKELY (ret != GST_FLOW_OK))
4300     goto pull_range_failed;
4301
4302 probed_data:
4303   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4304       res_buf, offset, size, probe_stopped_unref);
4305
4306   GST_OBJECT_UNLOCK (pad);
4307
4308   *buffer = res_buf;
4309
4310   return ret;
4311
4312   /* ERROR recovery here */
4313 flushing:
4314   {
4315     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4316         "pullrange, but pad was flushing");
4317     GST_OBJECT_UNLOCK (pad);
4318     return GST_FLOW_FLUSHING;
4319   }
4320 wrong_mode:
4321   {
4322     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4323         GST_DEBUG_PAD_NAME (pad));
4324     GST_OBJECT_UNLOCK (pad);
4325     return GST_FLOW_ERROR;
4326   }
4327 probe_stopped:
4328   {
4329     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4330         gst_flow_get_name (ret));
4331     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4332       if (res_buf) {
4333         /* the probe filled the buffer and asks us to not forward to the peer
4334          * anymore, we continue with the post probes then */
4335         GST_DEBUG_OBJECT (pad, "handled buffer");
4336         ret = GST_FLOW_OK;
4337         goto probed_data;
4338       } else {
4339         /* no buffer, we are EOS then */
4340         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4341         ret = GST_FLOW_EOS;
4342       }
4343     }
4344     GST_OBJECT_UNLOCK (pad);
4345     return ret;
4346   }
4347 not_linked:
4348   {
4349     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4350         "pulling range, but it was not linked");
4351     GST_OBJECT_UNLOCK (pad);
4352     return GST_FLOW_NOT_LINKED;
4353   }
4354 pull_range_failed:
4355   {
4356     GST_OBJECT_UNLOCK (pad);
4357     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4358         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4359         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4360     return ret;
4361   }
4362 probe_stopped_unref:
4363   {
4364     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4365         "post probe returned %s", gst_flow_get_name (ret));
4366     GST_OBJECT_UNLOCK (pad);
4367     /* if we drop here, it signals EOS */
4368     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4369       ret = GST_FLOW_EOS;
4370     if (*buffer == NULL)
4371       gst_buffer_unref (res_buf);
4372     return ret;
4373   }
4374 }
4375
4376 /* must be called with pad object lock */
4377 static gboolean
4378 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4379 {
4380   guint i, len;
4381   GstEventType type;
4382   GArray *events;
4383   gboolean res = FALSE;
4384   const gchar *name = NULL;
4385
4386   type = GST_EVENT_TYPE (event);
4387   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4388     name = gst_structure_get_name (gst_event_get_structure (event));
4389
4390   events = pad->priv->events;
4391   len = events->len;
4392
4393   for (i = 0; i < len; i++) {
4394     PadEvent *ev = &g_array_index (events, PadEvent, i);
4395
4396     if (ev->event == NULL)
4397       continue;
4398
4399     if (type == GST_EVENT_TYPE (ev->event)) {
4400       /* matching types, check matching name if needed */
4401       if (name && !gst_event_has_name (ev->event, name))
4402         continue;
4403
4404       /* overwrite */
4405       if ((res = gst_event_replace (&ev->event, event)))
4406         ev->received = FALSE;
4407       break;
4408     }
4409   }
4410   if (i == len) {
4411     PadEvent ev;
4412     ev.event = gst_event_ref (event);
4413     ev.received = FALSE;
4414     g_array_append_val (events, ev);
4415     res = TRUE;
4416   }
4417
4418   if (res) {
4419     pad->priv->events_cookie++;
4420     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4421
4422     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4423
4424     switch (GST_EVENT_TYPE (event)) {
4425       case GST_EVENT_CAPS:
4426         GST_OBJECT_UNLOCK (pad);
4427
4428         GST_DEBUG_OBJECT (pad, "notify caps");
4429         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4430
4431         GST_OBJECT_LOCK (pad);
4432         break;
4433       default:
4434         break;
4435     }
4436   }
4437   return res;
4438 }
4439
4440 /* should be called with pad LOCK */
4441 static GstFlowReturn
4442 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4443     GstPadProbeType type)
4444 {
4445   GstFlowReturn ret;
4446   GstPad *peerpad;
4447   GstEventType event_type;
4448
4449   /* Two checks to be made:
4450    * . (un)set the FLUSHING flag for flushing events,
4451    * . handle pad blocking */
4452   event_type = GST_EVENT_TYPE (event);
4453   switch (event_type) {
4454     case GST_EVENT_FLUSH_START:
4455       GST_PAD_SET_FLUSHING (pad);
4456
4457       GST_PAD_BLOCK_BROADCAST (pad);
4458       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4459       break;
4460     case GST_EVENT_FLUSH_STOP:
4461       GST_PAD_UNSET_FLUSHING (pad);
4462
4463       /* Remove sticky EOS events */
4464       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4465       remove_event_by_type (pad, GST_EVENT_EOS);
4466       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4467
4468       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4469       break;
4470     default:
4471     {
4472       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4473         goto flushed;
4474
4475       /* No need to check for EOS here as either the caller (gst_pad_push_event())
4476        * checked already or this is called as part of pushing sticky events,
4477        * in which case we still want to forward the EOS event downstream.
4478        */
4479
4480       switch (GST_EVENT_TYPE (event)) {
4481         case GST_EVENT_SEGMENT:
4482           /* pass the adjusted segment event on. We need to do this even if
4483            * there is no peer pad because of the probes. */
4484           event = apply_pad_offset (pad, event);
4485           break;
4486         case GST_EVENT_RECONFIGURE:
4487           if (GST_PAD_IS_SINK (pad))
4488             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4489           break;
4490         default:
4491           break;
4492       }
4493       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4494           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4495       break;
4496     }
4497   }
4498
4499   /* send probes after modifying the events above */
4500   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4501
4502   /* now check the peer pad */
4503   peerpad = GST_PAD_PEER (pad);
4504   if (peerpad == NULL)
4505     goto not_linked;
4506
4507   gst_object_ref (peerpad);
4508   pad->priv->using++;
4509   GST_OBJECT_UNLOCK (pad);
4510
4511   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4512       event, GST_EVENT_TYPE_NAME (event), peerpad);
4513
4514   ret = gst_pad_send_event_unchecked (peerpad, event, type);
4515
4516   /* Note: we gave away ownership of the event at this point but we can still
4517    * print the old pointer */
4518   GST_LOG_OBJECT (pad, "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s",
4519       event, peerpad, gst_flow_get_name (ret));
4520
4521   gst_object_unref (peerpad);
4522
4523   GST_OBJECT_LOCK (pad);
4524   pad->priv->using--;
4525   if (pad->priv->using == 0) {
4526     /* pad is not active anymore, trigger idle callbacks */
4527     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4528         idle_probe_stopped, ret);
4529   }
4530   return ret;
4531
4532   /* ERROR handling */
4533 flushed:
4534   {
4535     GST_DEBUG_OBJECT (pad, "We're flushing");
4536     gst_event_unref (event);
4537     return GST_FLOW_FLUSHING;
4538   }
4539 probe_stopped:
4540   {
4541     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4542     gst_event_unref (event);
4543
4544     switch (ret) {
4545       case GST_FLOW_CUSTOM_SUCCESS:
4546         GST_DEBUG_OBJECT (pad, "dropped event");
4547         break;
4548       default:
4549         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4550         break;
4551     }
4552     return ret;
4553   }
4554 not_linked:
4555   {
4556     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4557     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4558     gst_event_unref (event);
4559
4560     /* unlinked pads should not influence latency configuration */
4561     if (event_type == GST_EVENT_LATENCY)
4562       return GST_FLOW_OK;
4563
4564     return GST_FLOW_NOT_LINKED;
4565   }
4566 idle_probe_stopped:
4567   {
4568     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4569     return ret;
4570   }
4571 }
4572
4573 /**
4574  * gst_pad_push_event:
4575  * @pad: a #GstPad to push the event to.
4576  * @event: (transfer full): the #GstEvent to send to the pad.
4577  *
4578  * Sends the event to the peer of the given pad. This function is
4579  * mainly used by elements to send events to their peer
4580  * elements.
4581  *
4582  * This function takes owership of the provided event so you should
4583  * gst_event_ref() it if you want to reuse the event after this call.
4584  *
4585  * Returns: TRUE if the event was handled.
4586  *
4587  * MT safe.
4588  */
4589 gboolean
4590 gst_pad_push_event (GstPad * pad, GstEvent * event)
4591 {
4592   gboolean res = FALSE;
4593   GstPadProbeType type;
4594   gboolean sticky, serialized;
4595
4596   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4597   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4598
4599   if (GST_PAD_IS_SRC (pad)) {
4600     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4601       goto wrong_direction;
4602     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4603   } else if (GST_PAD_IS_SINK (pad)) {
4604     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4605       goto wrong_direction;
4606     /* events pushed on sinkpad never are sticky */
4607     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4608   } else
4609     goto unknown_direction;
4610
4611   GST_OBJECT_LOCK (pad);
4612   sticky = GST_EVENT_IS_STICKY (event);
4613   serialized = GST_EVENT_IS_SERIALIZED (event);
4614
4615   if (sticky) {
4616     /* can't store on flushing pads */
4617     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4618       goto flushed;
4619
4620     if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4621       goto eos;
4622
4623     /* srcpad sticky events are stored immediately, the received flag is set
4624      * to FALSE and will be set to TRUE when we can successfully push the
4625      * event to the peer pad */
4626     if (gst_pad_store_sticky_event (pad, event)) {
4627       GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4628     }
4629     if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
4630       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4631   }
4632   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4633     /* all serialized or sticky events on the srcpad trigger push of
4634      * sticky events */
4635     res = (check_sticky (pad) == GST_FLOW_OK);
4636   }
4637   if (!sticky) {
4638     GstFlowReturn ret;
4639
4640     /* other events are pushed right away */
4641     ret = gst_pad_push_event_unchecked (pad, event, type);
4642     /* dropped events by a probe are not an error */
4643     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
4644   } else {
4645     /* Errors in sticky event pushing are no problem and ignored here
4646      * as they will cause more meaningful errors during data flow.
4647      * For EOS events, that are not followed by data flow, we still
4648      * return FALSE here though.
4649      */
4650     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
4651       res = TRUE;
4652     gst_event_unref (event);
4653   }
4654   GST_OBJECT_UNLOCK (pad);
4655
4656   return res;
4657
4658   /* ERROR handling */
4659 wrong_direction:
4660   {
4661     g_warning ("pad %s:%s pushing %s event in wrong direction",
4662         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4663     gst_event_unref (event);
4664     return FALSE;
4665   }
4666 unknown_direction:
4667   {
4668     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4669     gst_event_unref (event);
4670     return FALSE;
4671   }
4672 flushed:
4673   {
4674     GST_DEBUG_OBJECT (pad, "We're flushing");
4675     GST_OBJECT_UNLOCK (pad);
4676     gst_event_unref (event);
4677     return FALSE;
4678   }
4679 eos:
4680   {
4681     GST_DEBUG_OBJECT (pad, "We're EOS");
4682     GST_OBJECT_UNLOCK (pad);
4683     gst_event_unref (event);
4684     return FALSE;
4685   }
4686 }
4687
4688 /* Check if we can call the event function with the given event */
4689 static GstFlowReturn
4690 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4691 {
4692   GstCaps *caps;
4693
4694   switch (GST_EVENT_TYPE (event)) {
4695     case GST_EVENT_CAPS:
4696     {
4697       /* backwards compatibility mode for caps */
4698       gst_event_parse_caps (event, &caps);
4699
4700       if (!gst_pad_query_accept_caps (pad, caps))
4701         goto not_accepted;
4702       break;
4703     }
4704     default:
4705       break;
4706   }
4707   return GST_FLOW_OK;
4708
4709   /* ERRORS */
4710 not_accepted:
4711   {
4712     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4713         "caps %" GST_PTR_FORMAT " not accepted", caps);
4714     return GST_FLOW_NOT_NEGOTIATED;
4715   }
4716 }
4717
4718 static GstFlowReturn
4719 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4720     GstPadProbeType type)
4721 {
4722   GstFlowReturn ret;
4723   GstEventType event_type;
4724   gboolean serialized, need_unlock = FALSE, sticky;
4725   GstPadEventFunction eventfunc;
4726   GstObject *parent;
4727
4728   GST_OBJECT_LOCK (pad);
4729   if (GST_PAD_IS_SINK (pad))
4730     serialized = GST_EVENT_IS_SERIALIZED (event);
4731   else
4732     serialized = FALSE;
4733   sticky = GST_EVENT_IS_STICKY (event);
4734   event_type = GST_EVENT_TYPE (event);
4735   switch (event_type) {
4736     case GST_EVENT_FLUSH_START:
4737       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4738           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4739
4740       /* can't even accept a flush begin event when flushing */
4741       if (GST_PAD_IS_FLUSHING (pad))
4742         goto flushing;
4743
4744       GST_PAD_SET_FLUSHING (pad);
4745       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4746       break;
4747     case GST_EVENT_FLUSH_STOP:
4748       if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4749         GST_PAD_UNSET_FLUSHING (pad);
4750         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4751       }
4752       /* Remove pending EOS events */
4753       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4754       remove_event_by_type (pad, GST_EVENT_EOS);
4755       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4756
4757       GST_OBJECT_UNLOCK (pad);
4758       /* grab stream lock */
4759       GST_PAD_STREAM_LOCK (pad);
4760       need_unlock = TRUE;
4761       GST_OBJECT_LOCK (pad);
4762       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4763         goto flushing;
4764       break;
4765     case GST_EVENT_RECONFIGURE:
4766       if (GST_PAD_IS_SRC (pad))
4767         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4768     default:
4769       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4770           "have event type %" GST_PTR_FORMAT, event);
4771
4772       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4773         goto flushing;
4774
4775       if (serialized) {
4776         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4777           goto eos;
4778
4779         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4780         GST_OBJECT_UNLOCK (pad);
4781         GST_PAD_STREAM_LOCK (pad);
4782         need_unlock = TRUE;
4783         GST_OBJECT_LOCK (pad);
4784         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4785           goto flushing;
4786
4787         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4788           goto eos;
4789       }
4790
4791       switch (GST_EVENT_TYPE (event)) {
4792         case GST_EVENT_SEGMENT:
4793           event = apply_pad_offset (pad, event);
4794           break;
4795         default:
4796           break;
4797       }
4798
4799       /* now do the probe */
4800       PROBE_PUSH (pad,
4801           type | GST_PAD_PROBE_TYPE_PUSH |
4802           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4803
4804       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4805       break;
4806   }
4807
4808   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4809     goto no_function;
4810
4811   ACQUIRE_PARENT (pad, parent, no_parent);
4812   GST_OBJECT_UNLOCK (pad);
4813
4814   ret = pre_eventfunc_check (pad, event);
4815   if (G_UNLIKELY (ret != GST_FLOW_OK))
4816     goto precheck_failed;
4817
4818   if (sticky)
4819     gst_event_ref (event);
4820
4821   if (eventfunc (pad, parent, event)) {
4822     ret = GST_FLOW_OK;
4823   } else {
4824     /* something went wrong */
4825     switch (event_type) {
4826       case GST_EVENT_CAPS:
4827         ret = GST_FLOW_NOT_NEGOTIATED;
4828         break;
4829       default:
4830         ret = GST_FLOW_ERROR;
4831         break;
4832     }
4833   }
4834   RELEASE_PARENT (parent);
4835
4836   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4837
4838   if (sticky) {
4839     if (ret == GST_FLOW_OK) {
4840       GST_OBJECT_LOCK (pad);
4841       /* can't store on flushing pads */
4842       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4843         goto flushing;
4844
4845       if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4846         goto eos;
4847
4848       /* after the event function accepted the event, we can store the sticky
4849        * event on the pad */
4850       if (gst_pad_store_sticky_event (pad, event)) {
4851         GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4852       }
4853       if (event_type == GST_EVENT_EOS)
4854         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4855
4856       GST_OBJECT_UNLOCK (pad);
4857     }
4858     gst_event_unref (event);
4859   }
4860
4861   if (need_unlock)
4862     GST_PAD_STREAM_UNLOCK (pad);
4863
4864   return ret;
4865
4866   /* ERROR handling */
4867 flushing:
4868   {
4869     GST_OBJECT_UNLOCK (pad);
4870     if (need_unlock)
4871       GST_PAD_STREAM_UNLOCK (pad);
4872     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4873         "Received event on flushing pad. Discarding");
4874     gst_event_unref (event);
4875     return GST_FLOW_FLUSHING;
4876   }
4877 eos:
4878   {
4879     GST_OBJECT_UNLOCK (pad);
4880     if (need_unlock)
4881       GST_PAD_STREAM_UNLOCK (pad);
4882     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4883         "Received event on EOS pad. Discarding");
4884     gst_event_unref (event);
4885     return GST_FLOW_EOS;
4886   }
4887 probe_stopped:
4888   {
4889     GST_OBJECT_UNLOCK (pad);
4890     if (need_unlock)
4891       GST_PAD_STREAM_UNLOCK (pad);
4892     gst_event_unref (event);
4893
4894     switch (ret) {
4895       case GST_FLOW_CUSTOM_SUCCESS:
4896         GST_DEBUG_OBJECT (pad, "dropped event");
4897         ret = GST_FLOW_OK;
4898         break;
4899       default:
4900         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4901         break;
4902     }
4903     return ret;
4904   }
4905 no_function:
4906   {
4907     g_warning ("pad %s:%s has no event handler, file a bug.",
4908         GST_DEBUG_PAD_NAME (pad));
4909     GST_OBJECT_UNLOCK (pad);
4910     if (need_unlock)
4911       GST_PAD_STREAM_UNLOCK (pad);
4912     gst_event_unref (event);
4913     return GST_FLOW_NOT_SUPPORTED;
4914   }
4915 no_parent:
4916   {
4917     GST_DEBUG_OBJECT (pad, "no parent");
4918     GST_OBJECT_UNLOCK (pad);
4919     if (need_unlock)
4920       GST_PAD_STREAM_UNLOCK (pad);
4921     gst_event_unref (event);
4922     return GST_FLOW_FLUSHING;
4923   }
4924 precheck_failed:
4925   {
4926     GST_DEBUG_OBJECT (pad, "pre event check failed");
4927     RELEASE_PARENT (parent);
4928     if (need_unlock)
4929       GST_PAD_STREAM_UNLOCK (pad);
4930     gst_event_unref (event);
4931     return ret;
4932   }
4933 }
4934
4935 /**
4936  * gst_pad_send_event:
4937  * @pad: a #GstPad to send the event to.
4938  * @event: (transfer full): the #GstEvent to send to the pad.
4939  *
4940  * Sends the event to the pad. This function can be used
4941  * by applications to send events in the pipeline.
4942  *
4943  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4944  * sink pad, @event should be a downstream event. For example, you would not
4945  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4946  * Furthermore, some downstream events have to be serialized with data flow,
4947  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4948  * the event needs to be serialized with data flow, this function will take the
4949  * pad's stream lock while calling its event function.
4950  *
4951  * To find out whether an event type is upstream, downstream, or downstream and
4952  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4953  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4954  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4955  * plugin doesn't need to bother itself with this information; the core handles
4956  * all necessary locks and checks.
4957  *
4958  * This function takes owership of the provided event so you should
4959  * gst_event_ref() it if you want to reuse the event after this call.
4960  *
4961  * Returns: TRUE if the event was handled.
4962  */
4963 gboolean
4964 gst_pad_send_event (GstPad * pad, GstEvent * event)
4965 {
4966   gboolean result;
4967   GstPadProbeType type;
4968
4969   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4970   g_return_val_if_fail (event != NULL, FALSE);
4971
4972   if (GST_PAD_IS_SINK (pad)) {
4973     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4974       goto wrong_direction;
4975     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4976   } else if (GST_PAD_IS_SRC (pad)) {
4977     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4978       goto wrong_direction;
4979     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4980   } else
4981     goto unknown_direction;
4982
4983   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4984     result = FALSE;
4985   else
4986     result = TRUE;
4987
4988   return result;
4989
4990   /* ERROR handling */
4991 wrong_direction:
4992   {
4993     g_warning ("pad %s:%s sending %s event in wrong direction",
4994         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4995     gst_event_unref (event);
4996     return FALSE;
4997   }
4998 unknown_direction:
4999   {
5000     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5001     gst_event_unref (event);
5002     return FALSE;
5003   }
5004 }
5005
5006 /**
5007  * gst_pad_set_element_private:
5008  * @pad: the #GstPad to set the private data of.
5009  * @priv: The private data to attach to the pad.
5010  *
5011  * Set the given private data gpointer on the pad.
5012  * This function can only be used by the element that owns the pad.
5013  * No locking is performed in this function.
5014  */
5015 void
5016 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5017 {
5018   pad->element_private = priv;
5019 }
5020
5021 /**
5022  * gst_pad_get_element_private:
5023  * @pad: the #GstPad to get the private data of.
5024  *
5025  * Gets the private data of a pad.
5026  * No locking is performed in this function.
5027  *
5028  * Returns: (transfer none): a #gpointer to the private data.
5029  */
5030 gpointer
5031 gst_pad_get_element_private (GstPad * pad)
5032 {
5033   return pad->element_private;
5034 }
5035
5036 /**
5037  * gst_pad_get_sticky_event:
5038  * @pad: the #GstPad to get the event from.
5039  * @event_type: the #GstEventType that should be retrieved.
5040  * @idx: the index of the event
5041  *
5042  * Returns a new reference of the sticky event of type @event_type
5043  * from the event.
5044  *
5045  * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
5046  * event of @event_type was on @pad. Unref after usage.
5047  */
5048 GstEvent *
5049 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5050 {
5051   GstEvent *event = NULL;
5052   PadEvent *ev;
5053
5054   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5055   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5056
5057   GST_OBJECT_LOCK (pad);
5058   ev = find_event_by_type (pad, event_type, idx);
5059   if (ev && (event = ev->event))
5060     gst_event_ref (event);
5061   GST_OBJECT_UNLOCK (pad);
5062
5063   return event;
5064 }
5065
5066 typedef struct
5067 {
5068   GstPadStickyEventsForeachFunction func;
5069   gpointer user_data;
5070 } ForeachDispatch;
5071
5072 static gboolean
5073 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5074 {
5075   ForeachDispatch *data = user_data;
5076   gboolean ret;
5077
5078   GST_OBJECT_UNLOCK (pad);
5079
5080   ret = data->func (pad, &ev->event, data->user_data);
5081
5082   GST_OBJECT_LOCK (pad);
5083
5084   return ret;
5085 }
5086
5087 /**
5088  * gst_pad_sticky_events_foreach:
5089  * @pad: the #GstPad that should be used for iteration.
5090  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5091  *                should be called for every event.
5092  * @user_data: (closure): the optional user data.
5093  *
5094  * Iterates all sticky events on @pad and calls @foreach_func for every
5095  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5096  */
5097 void
5098 gst_pad_sticky_events_foreach (GstPad * pad,
5099     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5100 {
5101   ForeachDispatch data;
5102
5103   g_return_if_fail (GST_IS_PAD (pad));
5104   g_return_if_fail (foreach_func != NULL);
5105
5106   data.func = foreach_func;
5107   data.user_data = user_data;
5108
5109   GST_OBJECT_LOCK (pad);
5110   events_foreach (pad, foreach_dispatch_function, &data);
5111   GST_OBJECT_UNLOCK (pad);
5112 }
5113
5114 static void
5115 do_stream_status (GstPad * pad, GstStreamStatusType type,
5116     GThread * thread, GstTask * task)
5117 {
5118   GstElement *parent;
5119
5120   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5121
5122   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5123     if (GST_IS_ELEMENT (parent)) {
5124       GstMessage *message;
5125       GValue value = { 0 };
5126
5127       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5128         gchar *tname, *ename, *pname;
5129
5130         /* create a good task name */
5131         ename = gst_element_get_name (parent);
5132         pname = gst_pad_get_name (pad);
5133         tname = g_strdup_printf ("%s:%s", ename, pname);
5134         g_free (ename);
5135         g_free (pname);
5136
5137         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5138         g_free (tname);
5139       }
5140
5141       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5142           type, parent);
5143
5144       g_value_init (&value, GST_TYPE_TASK);
5145       g_value_set_object (&value, task);
5146       gst_message_set_stream_status_object (message, &value);
5147       g_value_unset (&value);
5148
5149       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5150       gst_element_post_message (parent, message);
5151     }
5152     gst_object_unref (parent);
5153   }
5154 }
5155
5156 static void
5157 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5158 {
5159   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5160       thread, task);
5161 }
5162
5163 static void
5164 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5165 {
5166   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5167       thread, task);
5168 }
5169
5170 /**
5171  * gst_pad_start_task:
5172  * @pad: the #GstPad to start the task of
5173  * @func: the task function to call
5174  * @user_data: user data passed to the task function
5175  * @notify: called when @user_data is no longer referenced
5176  *
5177  * Starts a task that repeatedly calls @func with @user_data. This function
5178  * is mostly used in pad activation functions to start the dataflow.
5179  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5180  * before @func is called.
5181  *
5182  * Returns: a %TRUE if the task could be started.
5183  */
5184 gboolean
5185 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5186     GDestroyNotify notify)
5187 {
5188   GstTask *task;
5189   gboolean res;
5190
5191   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5192   g_return_val_if_fail (func != NULL, FALSE);
5193
5194   GST_DEBUG_OBJECT (pad, "start task");
5195
5196   GST_OBJECT_LOCK (pad);
5197   task = GST_PAD_TASK (pad);
5198   if (task == NULL) {
5199     task = gst_task_new (func, user_data, notify);
5200     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5201     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5202     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5203     GST_INFO_OBJECT (pad, "created task %p", task);
5204     GST_PAD_TASK (pad) = task;
5205     gst_object_ref (task);
5206     /* release lock to post the message */
5207     GST_OBJECT_UNLOCK (pad);
5208
5209     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5210
5211     gst_object_unref (task);
5212
5213     GST_OBJECT_LOCK (pad);
5214     /* nobody else is supposed to have changed the pad now */
5215     if (GST_PAD_TASK (pad) != task)
5216       goto concurrent_stop;
5217   }
5218   res = gst_task_set_state (task, GST_TASK_STARTED);
5219   GST_OBJECT_UNLOCK (pad);
5220
5221   return res;
5222
5223   /* ERRORS */
5224 concurrent_stop:
5225   {
5226     GST_OBJECT_UNLOCK (pad);
5227     return TRUE;
5228   }
5229 }
5230
5231 /**
5232  * gst_pad_pause_task:
5233  * @pad: the #GstPad to pause the task of
5234  *
5235  * Pause the task of @pad. This function will also wait until the
5236  * function executed by the task is finished if this function is not
5237  * called from the task function.
5238  *
5239  * Returns: a TRUE if the task could be paused or FALSE when the pad
5240  * has no task.
5241  */
5242 gboolean
5243 gst_pad_pause_task (GstPad * pad)
5244 {
5245   GstTask *task;
5246   gboolean res;
5247
5248   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5249
5250   GST_DEBUG_OBJECT (pad, "pause task");
5251
5252   GST_OBJECT_LOCK (pad);
5253   task = GST_PAD_TASK (pad);
5254   if (task == NULL)
5255     goto no_task;
5256   res = gst_task_set_state (task, GST_TASK_PAUSED);
5257   GST_OBJECT_UNLOCK (pad);
5258
5259   /* wait for task function to finish, this lock is recursive so it does nothing
5260    * when the pause is called from the task itself */
5261   GST_PAD_STREAM_LOCK (pad);
5262   GST_PAD_STREAM_UNLOCK (pad);
5263
5264   return res;
5265
5266 no_task:
5267   {
5268     GST_DEBUG_OBJECT (pad, "pad has no task");
5269     GST_OBJECT_UNLOCK (pad);
5270     return FALSE;
5271   }
5272 }
5273
5274 /**
5275  * gst_pad_stop_task:
5276  * @pad: the #GstPad to stop the task of
5277  *
5278  * Stop the task of @pad. This function will also make sure that the
5279  * function executed by the task will effectively stop if not called
5280  * from the GstTaskFunction.
5281  *
5282  * This function will deadlock if called from the GstTaskFunction of
5283  * the task. Use gst_task_pause() instead.
5284  *
5285  * Regardless of whether the pad has a task, the stream lock is acquired and
5286  * released so as to ensure that streaming through this pad has finished.
5287  *
5288  * Returns: a TRUE if the task could be stopped or FALSE on error.
5289  */
5290 gboolean
5291 gst_pad_stop_task (GstPad * pad)
5292 {
5293   GstTask *task;
5294   gboolean res;
5295
5296   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5297
5298   GST_DEBUG_OBJECT (pad, "stop task");
5299
5300   GST_OBJECT_LOCK (pad);
5301   task = GST_PAD_TASK (pad);
5302   if (task == NULL)
5303     goto no_task;
5304   GST_PAD_TASK (pad) = NULL;
5305   res = gst_task_set_state (task, GST_TASK_STOPPED);
5306   GST_OBJECT_UNLOCK (pad);
5307
5308   GST_PAD_STREAM_LOCK (pad);
5309   GST_PAD_STREAM_UNLOCK (pad);
5310
5311   if (!gst_task_join (task))
5312     goto join_failed;
5313
5314   gst_object_unref (task);
5315
5316   return res;
5317
5318 no_task:
5319   {
5320     GST_DEBUG_OBJECT (pad, "no task");
5321     GST_OBJECT_UNLOCK (pad);
5322
5323     GST_PAD_STREAM_LOCK (pad);
5324     GST_PAD_STREAM_UNLOCK (pad);
5325
5326     /* this is not an error */
5327     return TRUE;
5328   }
5329 join_failed:
5330   {
5331     /* this is bad, possibly the application tried to join the task from
5332      * the task's thread. We install the task again so that it will be stopped
5333      * again from the right thread next time hopefully. */
5334     GST_OBJECT_LOCK (pad);
5335     GST_DEBUG_OBJECT (pad, "join failed");
5336     /* we can only install this task if there was no other task */
5337     if (GST_PAD_TASK (pad) == NULL)
5338       GST_PAD_TASK (pad) = task;
5339     GST_OBJECT_UNLOCK (pad);
5340
5341     return FALSE;
5342   }
5343 }
5344
5345 /**
5346  * gst_pad_probe_info_get_event:
5347  * @info: a #GstPadProbeInfo
5348  *
5349  * Returns: (transfer none): The #GstEvent from the probe
5350  */
5351
5352 GstEvent *
5353 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5354 {
5355   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5356           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5357
5358   return GST_PAD_PROBE_INFO_EVENT (info);
5359 }
5360
5361
5362 /**
5363  * gst_pad_probe_info_get_query:
5364  * @info: a #GstPadProbeInfo
5365  *
5366  * Returns: (transfer none): The #GstQuery from the probe
5367  */
5368
5369 GstQuery *
5370 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5371 {
5372   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5373           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5374
5375   return GST_PAD_PROBE_INFO_QUERY (info);
5376 }
5377
5378 /**
5379  * gst_pad_probe_info_get_buffer:
5380  * @info: a #GstPadProbeInfo
5381  *
5382  * Returns: (transfer none): The #GstBuffer from the probe
5383  */
5384
5385 GstBuffer *
5386 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5387 {
5388   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5389
5390   return GST_PAD_PROBE_INFO_BUFFER (info);
5391 }
5392
5393 /**
5394  * gst_pad_probe_info_get_bufferlist:
5395  * @info: a #GstPadProbeInfo
5396  *
5397  * Returns: (transfer none): The #GstBufferlist from the probe
5398  */
5399
5400 GstBufferList *
5401 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5402 {
5403   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5404
5405   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5406 }