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