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