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