pad: also push sticky events on new event
[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 /**
2761  * gst_pad_query_default:
2762  * @pad: a #GstPad to call the default query handler on.
2763  * @parent: the parent of @pad or NULL
2764  * @query: (transfer none): the #GstQuery to handle.
2765  *
2766  * Invokes the default query handler for the given pad.
2767  * The query is sent to all pads internally linked to @pad. Note that
2768  * if there are many possible sink pads that are internally linked to
2769  * @pad, only one will be sent the query.
2770  * Multi-sinkpad elements should implement custom query handlers.
2771  *
2772  * Returns: TRUE if the query was performed successfully.
2773  */
2774 gboolean
2775 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2776 {
2777   gboolean forward, ret = FALSE;
2778
2779   switch (GST_QUERY_TYPE (query)) {
2780     case GST_QUERY_SCHEDULING:
2781       forward = FALSE;
2782       break;
2783     case GST_QUERY_ALLOCATION:
2784       forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2785       break;
2786     case GST_QUERY_ACCEPT_CAPS:
2787       ret = gst_pad_query_accept_caps_default (pad, query);
2788       forward = FALSE;
2789       break;
2790     case GST_QUERY_CAPS:
2791       ret = gst_pad_query_caps_default (pad, query);
2792       forward = FALSE;
2793       break;
2794     case GST_QUERY_POSITION:
2795     case GST_QUERY_SEEKING:
2796     case GST_QUERY_FORMATS:
2797     case GST_QUERY_LATENCY:
2798     case GST_QUERY_JITTER:
2799     case GST_QUERY_RATE:
2800     case GST_QUERY_CONVERT:
2801     default:
2802       forward = TRUE;
2803       break;
2804   }
2805
2806   if (forward) {
2807     ret = gst_pad_forward
2808         (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2809   }
2810   return ret;
2811 }
2812
2813 static void
2814 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2815 {
2816   GstPad *pad = data->pad;
2817   GstPadProbeInfo *info = data->info;
2818   GstPadProbeType type, flags;
2819   GstPadProbeCallback callback;
2820   GstPadProbeReturn ret;
2821
2822   /* if we have called this callback, do nothing */
2823   if (PROBE_COOKIE (hook) == data->cookie) {
2824     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2825         "hook %lu, cookie %u already called", hook->hook_id,
2826         PROBE_COOKIE (hook));
2827     return;
2828   }
2829
2830   PROBE_COOKIE (hook) = data->cookie;
2831
2832   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2833   type = info->type;
2834
2835   /* one of the data types */
2836   if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2837     goto no_match;
2838   /* one of the scheduling types */
2839   if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2840     goto no_match;
2841   /* one of the blocking types must match */
2842   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2843       (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2844     goto no_match;
2845   /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2846   if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2847       (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
2848     goto no_match;
2849
2850   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2851       "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
2852       PROBE_COOKIE (hook), flags);
2853
2854   data->marshalled = TRUE;
2855
2856   callback = (GstPadProbeCallback) hook->func;
2857   if (callback == NULL)
2858     return;
2859
2860   info->id = hook->hook_id;
2861
2862   GST_OBJECT_UNLOCK (pad);
2863
2864   ret = callback (pad, info, hook->data);
2865
2866   GST_OBJECT_LOCK (pad);
2867
2868   switch (ret) {
2869     case GST_PAD_PROBE_REMOVE:
2870       /* remove the probe */
2871       GST_DEBUG_OBJECT (pad, "asked to remove hook");
2872       cleanup_hook (pad, hook);
2873       break;
2874     case GST_PAD_PROBE_DROP:
2875       /* need to drop the data, make sure other probes don't get called
2876        * anymore */
2877       GST_DEBUG_OBJECT (pad, "asked to drop item");
2878       info->type = GST_PAD_PROBE_TYPE_INVALID;
2879       data->dropped = TRUE;
2880       break;
2881     case GST_PAD_PROBE_PASS:
2882       /* inform the pad block to let things pass */
2883       GST_DEBUG_OBJECT (pad, "asked to pass item");
2884       data->pass = TRUE;
2885       break;
2886     default:
2887       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2888       break;
2889   }
2890   return;
2891
2892 no_match:
2893   {
2894     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2895         "hook %lu, cookie %u with flags 0x%08x does not match %08x",
2896         hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
2897     return;
2898   }
2899 }
2900
2901 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval)    \
2902   G_STMT_START {                                                \
2903     if (G_UNLIKELY (pad->num_probes)) {                         \
2904       /* we start with passing NULL as the data item */         \
2905       GstPadProbeInfo info = { mask, 0, NULL, offs, size };     \
2906       ret = do_probe_callbacks (pad, &info, defaultval);        \
2907       /* store the possibly updated data item */                \
2908       data = GST_PAD_PROBE_INFO_DATA (&info);                   \
2909       /* if something went wrong, exit */                       \
2910       if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2911         goto label;                                             \
2912       /* otherwise check if the probe retured data */           \
2913       if (G_UNLIKELY (data != NULL))                            \
2914         goto probed;                                            \
2915     }                                                           \
2916   } G_STMT_END
2917
2918
2919 /* a probe that does not take or return any data */
2920 #define PROBE_NO_DATA(pad,mask,label,defaultval)                \
2921   G_STMT_START {                                                \
2922     if (G_UNLIKELY (pad->num_probes)) {                         \
2923       /* pass NULL as the data item */                          \
2924       GstPadProbeInfo info = { mask, 0, NULL, 0, 0 };           \
2925       ret = do_probe_callbacks (pad, &info, defaultval);        \
2926       if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2927         goto label;                                             \
2928     }                                                           \
2929   } G_STMT_END
2930
2931 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval)    \
2932   G_STMT_START {                                                \
2933     if (G_UNLIKELY (pad->num_probes)) {                         \
2934       GstPadProbeInfo info = { mask, 0, data, offs, size };     \
2935       ret = do_probe_callbacks (pad, &info, defaultval);        \
2936       data = GST_PAD_PROBE_INFO_DATA (&info);                   \
2937       if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2938         goto label;                                             \
2939     }                                                           \
2940   } G_STMT_END
2941
2942 #define PROBE_PUSH(pad,mask,data,label)                         \
2943   PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2944 #define PROBE_PULL(pad,mask,data,offs,size,label)               \
2945   PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2946
2947 static GstFlowReturn
2948 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2949     GstFlowReturn defaultval)
2950 {
2951   ProbeMarshall data;
2952   guint cookie;
2953   gboolean is_block;
2954
2955   data.pad = pad;
2956   data.info = info;
2957   data.pass = FALSE;
2958   data.marshalled = FALSE;
2959   data.dropped = FALSE;
2960   data.cookie = ++pad->priv->probe_cookie;
2961
2962   is_block =
2963       (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2964
2965 again:
2966   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2967       "do probes cookie %u", data.cookie);
2968   cookie = pad->priv->probe_list_cookie;
2969
2970   g_hook_list_marshal (&pad->probes, TRUE,
2971       (GHookMarshaller) probe_hook_marshal, &data);
2972
2973   /* if the list changed, call the new callbacks (they will not have their
2974    * cookie set to data.cookie */
2975   if (cookie != pad->priv->probe_list_cookie) {
2976     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2977         "probe list changed, restarting");
2978     goto again;
2979   }
2980
2981   /* the first item that dropped will stop the hooks and then we drop here */
2982   if (data.dropped)
2983     goto dropped;
2984
2985   /* if no handler matched and we are blocking, let the item pass */
2986   if (!data.marshalled && is_block)
2987     goto passed;
2988
2989   /* At this point, all handlers returned either OK or PASS. If one handler
2990    * returned PASS, let the item pass */
2991   if (data.pass)
2992     goto passed;
2993
2994   if (is_block) {
2995     while (GST_PAD_IS_BLOCKED (pad)) {
2996       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2997           "we are blocked %d times", pad->num_blocked);
2998
2999       /* we might have released the lock */
3000       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3001         goto flushing;
3002
3003       /* now we block the streaming thread. It can be unlocked when we
3004        * deactivate the pad (which will also set the FLUSHING flag) or
3005        * when the pad is unblocked. A flushing event will also unblock
3006        * the pad after setting the FLUSHING flag. */
3007       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3008           "Waiting to be unblocked or set flushing");
3009       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3010       GST_PAD_BLOCK_WAIT (pad);
3011       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3012       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3013
3014       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3015         goto flushing;
3016     }
3017   }
3018
3019   return defaultval;
3020
3021   /* ERRORS */
3022 flushing:
3023   {
3024     GST_DEBUG_OBJECT (pad, "pad is flushing");
3025     return GST_FLOW_FLUSHING;
3026   }
3027 dropped:
3028   {
3029     GST_DEBUG_OBJECT (pad, "data is dropped");
3030     return GST_FLOW_CUSTOM_SUCCESS;
3031   }
3032 passed:
3033   {
3034     /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3035     GST_DEBUG_OBJECT (pad, "data is passed");
3036     return GST_FLOW_OK;
3037   }
3038 }
3039
3040 /* pad offsets */
3041
3042 /**
3043  * gst_pad_get_offset:
3044  * @pad: a #GstPad
3045  *
3046  * Get the offset applied to the running time of @pad. @pad has to be a source
3047  * pad.
3048  *
3049  * Returns: the offset.
3050  */
3051 gint64
3052 gst_pad_get_offset (GstPad * pad)
3053 {
3054   gint64 result;
3055
3056   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3057
3058   GST_OBJECT_LOCK (pad);
3059   result = pad->offset;
3060   GST_OBJECT_UNLOCK (pad);
3061
3062   return result;
3063 }
3064
3065 /**
3066  * gst_pad_set_offset:
3067  * @pad: a #GstPad
3068  * @offset: the offset
3069  *
3070  * Set the offset that will be applied to the running time of @pad.
3071  */
3072 void
3073 gst_pad_set_offset (GstPad * pad, gint64 offset)
3074 {
3075   PadEvent *ev;
3076
3077   g_return_if_fail (GST_IS_PAD (pad));
3078
3079   GST_OBJECT_LOCK (pad);
3080   /* if nothing changed, do nothing */
3081   if (pad->offset == offset)
3082     goto done;
3083
3084   pad->offset = offset;
3085   GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3086
3087   /* sinkpads will apply their offset the next time a segment
3088    * event is received. */
3089   if (GST_PAD_IS_SINK (pad))
3090     goto done;
3091
3092   /* resend the last segment event on next buffer push */
3093   if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3094     ev->received = FALSE;
3095     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3096   }
3097
3098 done:
3099   GST_OBJECT_UNLOCK (pad);
3100 }
3101
3102
3103 /**
3104  * gst_pad_query:
3105  * @pad: a #GstPad to invoke the default query on.
3106  * @query: (transfer none): the #GstQuery to perform.
3107  *
3108  * Dispatches a query to a pad. The query should have been allocated by the
3109  * caller via one of the type-specific allocation functions. The element that
3110  * the pad belongs to is responsible for filling the query with an appropriate
3111  * response, which should then be parsed with a type-specific query parsing
3112  * function.
3113  *
3114  * Again, the caller is responsible for both the allocation and deallocation of
3115  * the query structure.
3116  *
3117  * Please also note that some queries might need a running pipeline to work.
3118  *
3119  * Returns: TRUE if the query could be performed.
3120  */
3121 gboolean
3122 gst_pad_query (GstPad * pad, GstQuery * query)
3123 {
3124   GstObject *parent;
3125   gboolean res;
3126   GstPadQueryFunction func;
3127   GstPadProbeType type;
3128   GstFlowReturn ret;
3129
3130   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3131   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3132
3133   GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3134       GST_QUERY_TYPE_NAME (query));
3135
3136   if (GST_PAD_IS_SRC (pad))
3137     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3138   else
3139     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3140
3141   GST_OBJECT_LOCK (pad);
3142   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3143       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3144   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3145
3146   ACQUIRE_PARENT (pad, parent, no_parent);
3147   GST_OBJECT_UNLOCK (pad);
3148
3149   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3150     goto no_func;
3151
3152   res = func (pad, parent, query);
3153
3154   RELEASE_PARENT (parent);
3155
3156   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3157       GST_QUERY_TYPE_NAME (query), res);
3158
3159   if (res != TRUE)
3160     goto query_failed;
3161
3162   GST_OBJECT_LOCK (pad);
3163   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3164   GST_OBJECT_UNLOCK (pad);
3165
3166   return res;
3167
3168   /* ERRORS */
3169 no_parent:
3170   {
3171     GST_DEBUG_OBJECT (pad, "had no parent");
3172     GST_OBJECT_UNLOCK (pad);
3173     return FALSE;
3174   }
3175 no_func:
3176   {
3177     GST_DEBUG_OBJECT (pad, "had no query function");
3178     RELEASE_PARENT (parent);
3179     return FALSE;
3180   }
3181 query_failed:
3182   {
3183     GST_DEBUG_OBJECT (pad, "query failed");
3184     return FALSE;
3185   }
3186 probe_stopped:
3187   {
3188     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3189     GST_OBJECT_UNLOCK (pad);
3190     return FALSE;
3191   }
3192 }
3193
3194 /**
3195  * gst_pad_peer_query:
3196  * @pad: a #GstPad to invoke the peer query on.
3197  * @query: (transfer none): the #GstQuery to perform.
3198  *
3199  * Performs gst_pad_query() on the peer of @pad.
3200  *
3201  * The caller is responsible for both the allocation and deallocation of
3202  * the query structure.
3203  *
3204  * Returns: TRUE if the query could be performed. This function returns %FALSE
3205  * if @pad has no peer.
3206  *
3207  * Since: 0.10.15
3208  */
3209 gboolean
3210 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3211 {
3212   GstPad *peerpad;
3213   GstPadProbeType type;
3214   gboolean res;
3215   GstFlowReturn ret;
3216
3217   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3218   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3219
3220   if (GST_PAD_IS_SRC (pad))
3221     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3222   else
3223     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3224
3225   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3226       GST_QUERY_TYPE_NAME (query));
3227
3228   GST_OBJECT_LOCK (pad);
3229   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3230       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3231   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3232
3233   peerpad = GST_PAD_PEER (pad);
3234   if (G_UNLIKELY (peerpad == NULL))
3235     goto no_peer;
3236
3237   gst_object_ref (peerpad);
3238   GST_OBJECT_UNLOCK (pad);
3239
3240   res = gst_pad_query (peerpad, query);
3241
3242   gst_object_unref (peerpad);
3243
3244   if (res != TRUE)
3245     goto query_failed;
3246
3247   GST_OBJECT_LOCK (pad);
3248   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3249   GST_OBJECT_UNLOCK (pad);
3250
3251   return res;
3252
3253   /* ERRORS */
3254 no_peer:
3255   {
3256     GST_WARNING_OBJECT (pad, "pad has no peer");
3257     GST_OBJECT_UNLOCK (pad);
3258     return FALSE;
3259   }
3260 query_failed:
3261   {
3262     GST_DEBUG_OBJECT (pad, "query failed");
3263     return FALSE;
3264   }
3265 probe_stopped:
3266   {
3267     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3268     GST_OBJECT_UNLOCK (pad);
3269     return FALSE;
3270   }
3271 }
3272
3273 /**********************************************************************
3274  * Data passing functions
3275  */
3276
3277 /* should be called with pad LOCK */
3278 static gboolean
3279 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3280 {
3281   GstFlowReturn *data = user_data;
3282   GstEvent *event = ev->event;
3283
3284   if (ev->received) {
3285     GST_DEBUG_OBJECT (pad, "event %s was already received",
3286         GST_EVENT_TYPE_NAME (event));
3287     return TRUE;
3288   }
3289
3290   *data = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3291       GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3292
3293   switch (*data) {
3294     case GST_FLOW_OK:
3295       ev->received = TRUE;
3296       GST_DEBUG_OBJECT (pad, "event %s marked received",
3297           GST_EVENT_TYPE_NAME (event));
3298       break;
3299     case GST_FLOW_NOT_LINKED:
3300       /* not linked is not a problem, we are sticky so the event will be
3301        * sent later */
3302       GST_DEBUG_OBJECT (pad, "pad was not linked");
3303       *data = GST_FLOW_OK;
3304       /* fallthrough */
3305     default:
3306       GST_DEBUG_OBJECT (pad, "mark pending events");
3307       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3308       break;
3309   }
3310   return *data == GST_FLOW_OK;
3311 }
3312
3313 /* check sticky events and push them when needed. should be called
3314  * with pad LOCK */
3315 static inline GstFlowReturn
3316 check_sticky (GstPad * pad)
3317 {
3318   GstFlowReturn ret = GST_FLOW_OK;
3319
3320   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3321     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3322
3323     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3324     events_foreach (pad, push_sticky, &ret);
3325   }
3326   return ret;
3327 }
3328
3329 /* this is the chain function that does not perform the additional argument
3330  * checking for that little extra speed.
3331  */
3332 static inline GstFlowReturn
3333 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3334 {
3335   GstFlowReturn ret;
3336   GstObject *parent;
3337
3338   GST_PAD_STREAM_LOCK (pad);
3339
3340   GST_OBJECT_LOCK (pad);
3341   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3342     goto flushing;
3343
3344   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3345     goto wrong_mode;
3346
3347   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3348
3349   PROBE_PUSH (pad, type, data, probe_stopped);
3350
3351   parent = GST_OBJECT_PARENT (pad);
3352   GST_OBJECT_UNLOCK (pad);
3353
3354   /* NOTE: we read the chainfunc unlocked.
3355    * we cannot hold the lock for the pad so we might send
3356    * the data to the wrong function. This is not really a
3357    * problem since functions are assigned at creation time
3358    * and don't change that often... */
3359   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3360     GstPadChainFunction chainfunc;
3361
3362     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3363       goto no_function;
3364
3365     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3366         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3367         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3368
3369     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3370
3371     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3372         "called chainfunction &%s with buffer %p, returned %s",
3373         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3374   } else {
3375     GstPadChainListFunction chainlistfunc;
3376
3377     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3378       goto no_function;
3379
3380     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3381         "calling chainlistfunction &%s",
3382         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3383
3384     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3385
3386     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3387         "called chainlistfunction &%s, returned %s",
3388         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3389   }
3390
3391   GST_PAD_STREAM_UNLOCK (pad);
3392
3393   return ret;
3394
3395   /* ERRORS */
3396 flushing:
3397   {
3398     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3399         "chaining, but pad was flushing");
3400     GST_OBJECT_UNLOCK (pad);
3401     GST_PAD_STREAM_UNLOCK (pad);
3402     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3403     return GST_FLOW_FLUSHING;
3404   }
3405 wrong_mode:
3406   {
3407     g_critical ("chain on pad %s:%s but it was not in push mode",
3408         GST_DEBUG_PAD_NAME (pad));
3409     GST_OBJECT_UNLOCK (pad);
3410     GST_PAD_STREAM_UNLOCK (pad);
3411     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3412     return GST_FLOW_ERROR;
3413   }
3414 probe_stopped:
3415   {
3416     GST_OBJECT_UNLOCK (pad);
3417     GST_PAD_STREAM_UNLOCK (pad);
3418     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3419
3420     switch (ret) {
3421       case GST_FLOW_CUSTOM_SUCCESS:
3422         GST_DEBUG_OBJECT (pad, "dropped buffer");
3423         ret = GST_FLOW_OK;
3424         break;
3425       default:
3426         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3427         break;
3428     }
3429     return ret;
3430   }
3431 no_function:
3432   {
3433     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3434     g_critical ("chain on pad %s:%s but it has no chainfunction",
3435         GST_DEBUG_PAD_NAME (pad));
3436     GST_PAD_STREAM_UNLOCK (pad);
3437     return GST_FLOW_NOT_SUPPORTED;
3438   }
3439 }
3440
3441 /**
3442  * gst_pad_chain:
3443  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3444  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3445  *     if not.
3446  *
3447  * Chain a buffer to @pad.
3448  *
3449  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3450  *
3451  * If the buffer type is not acceptable for @pad (as negotiated with a
3452  * preceeding GST_EVENT_CAPS event), this function returns
3453  * #GST_FLOW_NOT_NEGOTIATED.
3454  *
3455  * The function proceeds calling the chain function installed on @pad (see
3456  * gst_pad_set_chain_function()) and the return value of that function is
3457  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3458  * chain function.
3459  *
3460  * In all cases, success or failure, the caller loses its reference to @buffer
3461  * after calling this function.
3462  *
3463  * Returns: a #GstFlowReturn from the pad.
3464  *
3465  * MT safe.
3466  */
3467 GstFlowReturn
3468 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3469 {
3470   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3471   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3472   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3473
3474   return gst_pad_chain_data_unchecked (pad,
3475       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3476 }
3477
3478 static GstFlowReturn
3479 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3480     GstBufferList * list)
3481 {
3482   guint i, len;
3483   GstBuffer *buffer;
3484   GstFlowReturn ret;
3485
3486   GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3487
3488   len = gst_buffer_list_length (list);
3489
3490   ret = GST_FLOW_OK;
3491   for (i = 0; i < len; i++) {
3492     buffer = gst_buffer_list_get (list, i);
3493     ret =
3494         gst_pad_chain_data_unchecked (pad,
3495         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3496         gst_buffer_ref (buffer));
3497     if (ret != GST_FLOW_OK)
3498       break;
3499   }
3500   gst_buffer_list_unref (list);
3501
3502   return ret;
3503 }
3504
3505 /**
3506  * gst_pad_chain_list:
3507  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3508  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3509  *     if not.
3510  *
3511  * Chain a bufferlist to @pad.
3512  *
3513  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3514  *
3515  * If @pad was not negotiated properly with a CAPS event, this function
3516  * returns #GST_FLOW_NOT_NEGOTIATED.
3517  *
3518  * The function proceeds calling the chainlist function installed on @pad (see
3519  * gst_pad_set_chain_list_function()) and the return value of that function is
3520  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3521  * chainlist function.
3522  *
3523  * In all cases, success or failure, the caller loses its reference to @list
3524  * after calling this function.
3525  *
3526  * MT safe.
3527  *
3528  * Returns: a #GstFlowReturn from the pad.
3529  *
3530  * Since: 0.10.24
3531  */
3532 GstFlowReturn
3533 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3534 {
3535   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3536   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3537   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3538
3539   return gst_pad_chain_data_unchecked (pad,
3540       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3541 }
3542
3543 static GstFlowReturn
3544 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3545 {
3546   GstPad *peer;
3547   GstFlowReturn ret;
3548
3549   GST_OBJECT_LOCK (pad);
3550   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3551     goto flushing;
3552
3553   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3554     goto wrong_mode;
3555
3556   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3557     goto events_error;
3558
3559   /* do block probes */
3560   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3561
3562   /* do post-blocking probes */
3563   PROBE_PUSH (pad, type, data, probe_stopped);
3564
3565   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3566     goto not_linked;
3567
3568   /* take ref to peer pad before releasing the lock */
3569   gst_object_ref (peer);
3570   pad->priv->using++;
3571   GST_OBJECT_UNLOCK (pad);
3572
3573   ret = gst_pad_chain_data_unchecked (peer, type, data);
3574
3575   gst_object_unref (peer);
3576
3577   GST_OBJECT_LOCK (pad);
3578   pad->priv->using--;
3579   if (pad->priv->using == 0) {
3580     /* pad is not active anymore, trigger idle callbacks */
3581     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3582         probe_stopped, ret);
3583   }
3584   GST_OBJECT_UNLOCK (pad);
3585
3586   return ret;
3587
3588   /* ERROR recovery here */
3589   /* ERRORS */
3590 flushing:
3591   {
3592     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3593         "pushing, but pad was flushing");
3594     GST_OBJECT_UNLOCK (pad);
3595     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3596     return GST_FLOW_FLUSHING;
3597   }
3598 wrong_mode:
3599   {
3600     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3601         GST_DEBUG_PAD_NAME (pad));
3602     GST_OBJECT_UNLOCK (pad);
3603     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3604     return GST_FLOW_ERROR;
3605   }
3606 events_error:
3607   {
3608     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3609         "error pushing events, return %s", gst_flow_get_name (ret));
3610     GST_OBJECT_UNLOCK (pad);
3611     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3612     return ret;
3613   }
3614 probe_stopped:
3615   {
3616     GST_OBJECT_UNLOCK (pad);
3617     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3618
3619     switch (ret) {
3620       case GST_FLOW_CUSTOM_SUCCESS:
3621         GST_DEBUG_OBJECT (pad, "dropped buffer");
3622         ret = GST_FLOW_OK;
3623         break;
3624       default:
3625         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3626         break;
3627     }
3628     return ret;
3629   }
3630 not_linked:
3631   {
3632     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3633         "pushing, but it was not linked");
3634     GST_OBJECT_UNLOCK (pad);
3635     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3636     return GST_FLOW_NOT_LINKED;
3637   }
3638 }
3639
3640 /**
3641  * gst_pad_push:
3642  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3643  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3644  *     if not.
3645  *
3646  * Pushes a buffer to the peer of @pad.
3647  *
3648  * This function will call installed block probes before triggering any
3649  * installed data probes.
3650  *
3651  * The function proceeds calling gst_pad_chain() on the peer pad and returns
3652  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3653  * be returned.
3654  *
3655  * In all cases, success or failure, the caller loses its reference to @buffer
3656  * after calling this function.
3657  *
3658  * Returns: a #GstFlowReturn from the peer pad.
3659  *
3660  * MT safe.
3661  */
3662 GstFlowReturn
3663 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3664 {
3665   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3666   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3667   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3668
3669   return gst_pad_push_data (pad,
3670       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3671 }
3672
3673 /**
3674  * gst_pad_push_list:
3675  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3676  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3677  *     if not.
3678  *
3679  * Pushes a buffer list to the peer of @pad.
3680  *
3681  * This function will call installed block probes before triggering any
3682  * installed data probes.
3683  *
3684  * The function proceeds calling the chain function on the peer pad and returns
3685  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3686  * be returned. If the peer pad does not have any installed chainlist function
3687  * every group buffer of the list will be merged into a normal #GstBuffer and
3688  * chained via gst_pad_chain().
3689  *
3690  * In all cases, success or failure, the caller loses its reference to @list
3691  * after calling this function.
3692  *
3693  * Returns: a #GstFlowReturn from the peer pad.
3694  *
3695  * MT safe.
3696  *
3697  * Since: 0.10.24
3698  */
3699 GstFlowReturn
3700 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3701 {
3702   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3703   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3704   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3705
3706   return gst_pad_push_data (pad,
3707       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3708 }
3709
3710 static GstFlowReturn
3711 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3712     GstBuffer ** buffer)
3713 {
3714   GstFlowReturn ret;
3715   GstPadGetRangeFunction getrangefunc;
3716   GstObject *parent;
3717
3718   GST_PAD_STREAM_LOCK (pad);
3719
3720   GST_OBJECT_LOCK (pad);
3721   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3722     goto flushing;
3723
3724   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3725     goto wrong_mode;
3726
3727   if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3728     goto events_error;
3729
3730   /* when one of the probes returns a buffer, probed_data will be called and we
3731    * skip calling the getrange function */
3732   PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3733       *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3734
3735   ACQUIRE_PARENT (pad, parent, no_parent);
3736   GST_OBJECT_UNLOCK (pad);
3737
3738   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3739     goto no_function;
3740
3741   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3742       "calling getrangefunc %s, offset %"
3743       G_GUINT64_FORMAT ", size %u",
3744       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3745
3746   ret = getrangefunc (pad, parent, offset, size, buffer);
3747
3748   RELEASE_PARENT (parent);
3749
3750   if (G_UNLIKELY (ret != GST_FLOW_OK))
3751     goto get_range_failed;
3752
3753   /* can only fire the signal if we have a valid buffer */
3754   GST_OBJECT_LOCK (pad);
3755 probed_data:
3756   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3757       *buffer, offset, size, probe_stopped_unref);
3758   GST_OBJECT_UNLOCK (pad);
3759
3760   GST_PAD_STREAM_UNLOCK (pad);
3761
3762   return ret;
3763
3764   /* ERRORS */
3765 flushing:
3766   {
3767     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3768         "getrange, but pad was flushing");
3769     GST_OBJECT_UNLOCK (pad);
3770     GST_PAD_STREAM_UNLOCK (pad);
3771     return GST_FLOW_FLUSHING;
3772   }
3773 wrong_mode:
3774   {
3775     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
3776         GST_DEBUG_PAD_NAME (pad));
3777     GST_OBJECT_UNLOCK (pad);
3778     GST_PAD_STREAM_UNLOCK (pad);
3779     return GST_FLOW_ERROR;
3780   }
3781 events_error:
3782   {
3783     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3784     GST_OBJECT_UNLOCK (pad);
3785     GST_PAD_STREAM_UNLOCK (pad);
3786     return ret;
3787   }
3788 no_parent:
3789   {
3790     GST_DEBUG_OBJECT (pad, "no parent");
3791     GST_OBJECT_UNLOCK (pad);
3792     GST_PAD_STREAM_UNLOCK (pad);
3793     return GST_FLOW_FLUSHING;
3794   }
3795 no_function:
3796   {
3797     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
3798         GST_DEBUG_PAD_NAME (pad));
3799     RELEASE_PARENT (parent);
3800     GST_PAD_STREAM_UNLOCK (pad);
3801     return GST_FLOW_NOT_SUPPORTED;
3802   }
3803 probe_stopped:
3804   {
3805     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3806         "probe returned %s", gst_flow_get_name (ret));
3807     GST_OBJECT_UNLOCK (pad);
3808     GST_PAD_STREAM_UNLOCK (pad);
3809     return ret;
3810   }
3811 probe_stopped_unref:
3812   {
3813     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3814         "probe returned %s", gst_flow_get_name (ret));
3815     GST_OBJECT_UNLOCK (pad);
3816     GST_PAD_STREAM_UNLOCK (pad);
3817     gst_buffer_unref (*buffer);
3818     *buffer = NULL;
3819     return ret;
3820   }
3821 get_range_failed:
3822   {
3823     GST_PAD_STREAM_UNLOCK (pad);
3824     *buffer = NULL;
3825     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3826         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3827         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3828     return ret;
3829   }
3830 }
3831
3832 /**
3833  * gst_pad_get_range:
3834  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3835  * @offset: The start offset of the buffer
3836  * @size: The length of the buffer
3837  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3838  *     returns #GST_FLOW_ERROR if %NULL.
3839  *
3840  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
3841  * immediately and @buffer is %NULL.
3842  *
3843  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3844  * description of a getrange function. If @pad has no getrange function
3845  * installed (see gst_pad_set_getrange_function()) this function returns
3846  * #GST_FLOW_NOT_SUPPORTED.
3847  *
3848  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3849  *
3850  * Returns: a #GstFlowReturn from the pad.
3851  *
3852  * MT safe.
3853  */
3854 GstFlowReturn
3855 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3856     GstBuffer ** buffer)
3857 {
3858   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3859   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3860   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3861
3862   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3863 }
3864
3865 /**
3866  * gst_pad_pull_range:
3867  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3868  * @offset: The start offset of the buffer
3869  * @size: The length of the buffer
3870  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3871  *     GST_FLOW_ERROR if %NULL.
3872  *
3873  * Pulls a @buffer from the peer pad.
3874  *
3875  * This function will first trigger the pad block signal if it was
3876  * installed.
3877  *
3878  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3879  * function returns the result of gst_pad_get_range() on the peer pad.
3880  * See gst_pad_get_range() for a list of return values and for the
3881  * semantics of the arguments of this function.
3882  *
3883  * Returns: a #GstFlowReturn from the peer pad.
3884  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3885  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3886  * @buffer may not be used or freed when any other return value than
3887  * #GST_FLOW_OK is returned.
3888  *
3889  * MT safe.
3890  */
3891 GstFlowReturn
3892 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3893     GstBuffer ** buffer)
3894 {
3895   GstPad *peer;
3896   GstFlowReturn ret;
3897
3898   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3899   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3900   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3901
3902   GST_OBJECT_LOCK (pad);
3903   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3904     goto flushing;
3905
3906   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3907     goto wrong_mode;
3908
3909   /* when one of the probes returns a buffer, probed_data will be called and we
3910    * skip calling the peer getrange function */
3911   PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3912       *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3913
3914   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3915     goto not_linked;
3916
3917   gst_object_ref (peer);
3918   pad->priv->using++;
3919   GST_OBJECT_UNLOCK (pad);
3920
3921   ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3922
3923   gst_object_unref (peer);
3924
3925   GST_OBJECT_LOCK (pad);
3926   pad->priv->using--;
3927   if (pad->priv->using == 0) {
3928     /* pad is not active anymore, trigger idle callbacks */
3929     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3930         post_probe_stopped, ret);
3931   }
3932
3933   if (G_UNLIKELY (ret != GST_FLOW_OK))
3934     goto pull_range_failed;
3935
3936 probed_data:
3937   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3938       *buffer, offset, size, post_probe_stopped);
3939
3940   GST_OBJECT_UNLOCK (pad);
3941
3942   return ret;
3943
3944   /* ERROR recovery here */
3945 flushing:
3946   {
3947     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3948         "pullrange, but pad was flushing");
3949     GST_OBJECT_UNLOCK (pad);
3950     return GST_FLOW_FLUSHING;
3951   }
3952 wrong_mode:
3953   {
3954     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
3955         GST_DEBUG_PAD_NAME (pad));
3956     GST_OBJECT_UNLOCK (pad);
3957     return GST_FLOW_ERROR;
3958   }
3959 pre_probe_stopped:
3960   {
3961     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3962         gst_flow_get_name (ret));
3963     GST_OBJECT_UNLOCK (pad);
3964     return ret;
3965   }
3966 not_linked:
3967   {
3968     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3969         "pulling range, but it was not linked");
3970     GST_OBJECT_UNLOCK (pad);
3971     return GST_FLOW_NOT_LINKED;
3972   }
3973 pull_range_failed:
3974   {
3975     *buffer = NULL;
3976     GST_OBJECT_UNLOCK (pad);
3977     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3978         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3979         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3980     return ret;
3981   }
3982 post_probe_stopped:
3983   {
3984     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3985         "post probe returned %s", gst_flow_get_name (ret));
3986     GST_OBJECT_UNLOCK (pad);
3987     if (ret == GST_FLOW_OK)
3988       gst_buffer_unref (*buffer);
3989     *buffer = NULL;
3990     return ret;
3991   }
3992 }
3993
3994 static gboolean
3995 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
3996 {
3997   guint i, len;
3998   GstEventType type;
3999   GArray *events;
4000   gboolean res = FALSE;
4001   const gchar *name = NULL;
4002
4003   type = GST_EVENT_TYPE (event);
4004   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4005     name = gst_structure_get_name (gst_event_get_structure (event));
4006
4007   events = pad->priv->events;
4008   len = events->len;
4009
4010   for (i = 0; i < len; i++) {
4011     PadEvent *ev = &g_array_index (events, PadEvent, i);
4012
4013     if (ev->event == NULL)
4014       continue;
4015
4016     if (type == GST_EVENT_TYPE (ev->event)) {
4017       /* matching types, check matching name if needed */
4018       if (name && !gst_event_has_name (ev->event, name))
4019         continue;
4020
4021       /* overwrite */
4022       if ((res = gst_event_replace (&ev->event, event)))
4023         ev->received = FALSE;
4024       break;
4025     }
4026   }
4027   if (i == len) {
4028     PadEvent ev;
4029     ev.event = gst_event_ref (event);
4030     ev.received = FALSE;
4031     g_array_append_val (events, ev);
4032     res = TRUE;
4033   }
4034
4035   if (res) {
4036     pad->priv->events_cookie++;
4037     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4038
4039     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4040
4041     switch (GST_EVENT_TYPE (event)) {
4042       case GST_EVENT_CAPS:
4043         if (locked)
4044           GST_OBJECT_UNLOCK (pad);
4045
4046         GST_DEBUG_OBJECT (pad, "notify caps");
4047         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4048
4049         if (locked)
4050           GST_OBJECT_LOCK (pad);
4051         break;
4052       default:
4053         break;
4054     }
4055   }
4056   return res;
4057 }
4058
4059 /* should be called with pad LOCK */
4060 static GstFlowReturn
4061 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4062     GstPadProbeType type)
4063 {
4064   GstFlowReturn ret;
4065   GstPad *peerpad;
4066   GstEventType event_type;
4067
4068   /* Two checks to be made:
4069    * . (un)set the FLUSHING flag for flushing events,
4070    * . handle pad blocking */
4071   event_type = GST_EVENT_TYPE (event);
4072   switch (event_type) {
4073     case GST_EVENT_FLUSH_START:
4074       GST_PAD_SET_FLUSHING (pad);
4075
4076       GST_PAD_BLOCK_BROADCAST (pad);
4077       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4078       break;
4079     case GST_EVENT_FLUSH_STOP:
4080       GST_PAD_UNSET_FLUSHING (pad);
4081
4082       /* Remove sticky EOS events */
4083       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4084       remove_event_by_type (pad, GST_EVENT_EOS);
4085
4086       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4087       break;
4088     default:
4089     {
4090       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4091         goto flushed;
4092
4093       switch (GST_EVENT_TYPE (event)) {
4094         case GST_EVENT_SEGMENT:
4095           /* pass the adjusted segment event on. We need to do this even if
4096            * there is no peer pad because of the probes. */
4097           event = apply_pad_offset (pad, event);
4098           break;
4099         case GST_EVENT_RECONFIGURE:
4100           if (GST_PAD_IS_SINK (pad))
4101             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4102           break;
4103         default:
4104           break;
4105       }
4106       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4107           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4108       break;
4109     }
4110   }
4111
4112   /* send probes after modifying the events above */
4113   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4114
4115   /* now check the peer pad */
4116   peerpad = GST_PAD_PEER (pad);
4117   if (peerpad == NULL)
4118     goto not_linked;
4119
4120   gst_object_ref (peerpad);
4121   pad->priv->using++;
4122   GST_OBJECT_UNLOCK (pad);
4123
4124   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4125       event, GST_EVENT_TYPE_NAME (event), peerpad);
4126
4127   ret = gst_pad_send_event_unchecked (peerpad, event, type);
4128
4129   /* Note: we gave away ownership of the event at this point but we can still
4130    * print the old pointer */
4131   GST_LOG_OBJECT (pad,
4132       "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4133       gst_flow_get_name (ret));
4134
4135   gst_object_unref (peerpad);
4136
4137   GST_OBJECT_LOCK (pad);
4138   pad->priv->using--;
4139   if (pad->priv->using == 0) {
4140     /* pad is not active anymore, trigger idle callbacks */
4141     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4142         idle_probe_stopped, ret);
4143   }
4144   return ret;
4145
4146   /* ERROR handling */
4147 flushed:
4148   {
4149     GST_DEBUG_OBJECT (pad, "We're flushing");
4150     gst_event_unref (event);
4151     return GST_FLOW_FLUSHING;
4152   }
4153 probe_stopped:
4154   {
4155     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4156     gst_event_unref (event);
4157
4158     switch (ret) {
4159       case GST_FLOW_CUSTOM_SUCCESS:
4160         GST_DEBUG_OBJECT (pad, "dropped event");
4161         ret = GST_FLOW_OK;
4162         break;
4163       default:
4164         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4165         break;
4166     }
4167     return ret;
4168   }
4169 not_linked:
4170   {
4171     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4172     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4173     gst_event_unref (event);
4174     return GST_FLOW_NOT_LINKED;
4175   }
4176 idle_probe_stopped:
4177   {
4178     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4179     return ret;
4180   }
4181 }
4182
4183 /**
4184  * gst_pad_push_event:
4185  * @pad: a #GstPad to push the event to.
4186  * @event: (transfer full): the #GstEvent to send to the pad.
4187  *
4188  * Sends the event to the peer of the given pad. This function is
4189  * mainly used by elements to send events to their peer
4190  * elements.
4191  *
4192  * This function takes owership of the provided event so you should
4193  * gst_event_ref() it if you want to reuse the event after this call.
4194  *
4195  * Returns: TRUE if the event was handled.
4196  *
4197  * MT safe.
4198  */
4199 gboolean
4200 gst_pad_push_event (GstPad * pad, GstEvent * event)
4201 {
4202   gboolean res;
4203   GstPadProbeType type;
4204   gboolean sticky, serialized;
4205
4206   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4207   g_return_val_if_fail (event != NULL, FALSE);
4208   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4209
4210   if (GST_PAD_IS_SRC (pad)) {
4211     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4212       goto wrong_direction;
4213     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4214   } else if (GST_PAD_IS_SINK (pad)) {
4215     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4216       goto wrong_direction;
4217     /* events pushed on sinkpad never are sticky */
4218     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4219   } else
4220     goto unknown_direction;
4221
4222   GST_OBJECT_LOCK (pad);
4223   sticky = GST_EVENT_IS_STICKY (event);
4224   serialized = GST_EVENT_IS_SERIALIZED (event);
4225
4226   if (sticky) {
4227     /* can't store on flushing pads */
4228     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4229       goto flushed;
4230
4231     /* srcpad sticky events are store immediately, the received flag is set
4232      * to FALSE and will be set to TRUE when we can successfully push the
4233      * event to the peer pad */
4234     if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4235       GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4236     }
4237   }
4238   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4239     /* all serialized or sticky events on the srcpad trigger push of
4240      * sticky events */
4241     res = (check_sticky (pad) == GST_FLOW_OK);
4242   }
4243   if (!sticky) {
4244     /* other events are pushed right away */
4245     res = (gst_pad_push_event_unchecked (pad, event, type) == GST_FLOW_OK);
4246   } else {
4247     gst_event_unref (event);
4248     res = TRUE;
4249   }
4250   GST_OBJECT_UNLOCK (pad);
4251
4252   return res;
4253
4254   /* ERROR handling */
4255 wrong_direction:
4256   {
4257     g_warning ("pad %s:%s pushing %s event in wrong direction",
4258         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4259     gst_event_unref (event);
4260     return FALSE;
4261   }
4262 unknown_direction:
4263   {
4264     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4265     gst_event_unref (event);
4266     return FALSE;
4267   }
4268 flushed:
4269   {
4270     GST_DEBUG_OBJECT (pad, "We're flushing");
4271     GST_OBJECT_UNLOCK (pad);
4272     gst_event_unref (event);
4273     return FALSE;
4274   }
4275 }
4276
4277 /* Check if we can call the event function with the given event */
4278 static GstFlowReturn
4279 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4280 {
4281   GstCaps *caps, *templ;
4282
4283   switch (GST_EVENT_TYPE (event)) {
4284     case GST_EVENT_CAPS:
4285     {
4286       /* backwards compatibility mode for caps */
4287       gst_event_parse_caps (event, &caps);
4288
4289       /* See if pad accepts the caps */
4290       templ = gst_pad_get_pad_template_caps (pad);
4291       if (!gst_caps_is_subset (caps, templ))
4292         goto not_accepted;
4293
4294       gst_caps_unref (templ);
4295       break;
4296     }
4297     default:
4298       break;
4299   }
4300   return GST_FLOW_OK;
4301
4302   /* ERRORS */
4303 not_accepted:
4304   {
4305     gst_caps_unref (templ);
4306     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4307         "caps %" GST_PTR_FORMAT " not accepted", caps);
4308     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4309         "no intersection with template %" GST_PTR_FORMAT, templ);
4310     return GST_FLOW_NOT_NEGOTIATED;
4311   }
4312 }
4313
4314 static GstFlowReturn
4315 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4316     GstPadProbeType type)
4317 {
4318   GstFlowReturn ret;
4319   GstEventType event_type;
4320   gboolean serialized, need_unlock = FALSE, sticky;
4321   GstPadEventFunction eventfunc;
4322   GstObject *parent;
4323
4324   GST_OBJECT_LOCK (pad);
4325   if (GST_PAD_IS_SINK (pad))
4326     serialized = GST_EVENT_IS_SERIALIZED (event);
4327   else
4328     serialized = FALSE;
4329   sticky = GST_EVENT_IS_STICKY (event);
4330   event_type = GST_EVENT_TYPE (event);
4331   switch (event_type) {
4332     case GST_EVENT_FLUSH_START:
4333       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4334           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4335
4336       /* can't even accept a flush begin event when flushing */
4337       if (GST_PAD_IS_FLUSHING (pad))
4338         goto flushing;
4339
4340       GST_PAD_SET_FLUSHING (pad);
4341       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4342       break;
4343     case GST_EVENT_FLUSH_STOP:
4344       if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4345         GST_PAD_UNSET_FLUSHING (pad);
4346         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4347       }
4348       /* Remove pending EOS events */
4349       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4350       remove_event_by_type (pad, GST_EVENT_EOS);
4351
4352       GST_OBJECT_UNLOCK (pad);
4353       /* grab stream lock */
4354       GST_PAD_STREAM_LOCK (pad);
4355       need_unlock = TRUE;
4356       GST_OBJECT_LOCK (pad);
4357       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4358         goto flushing;
4359       break;
4360     case GST_EVENT_RECONFIGURE:
4361       if (GST_PAD_IS_SRC (pad))
4362         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4363     default:
4364       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4365           "have event type %" GST_PTR_FORMAT, event);
4366
4367       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4368         goto flushing;
4369
4370       if (serialized) {
4371         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4372         GST_OBJECT_UNLOCK (pad);
4373         GST_PAD_STREAM_LOCK (pad);
4374         need_unlock = TRUE;
4375         GST_OBJECT_LOCK (pad);
4376         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4377           goto flushing;
4378       }
4379
4380       switch (GST_EVENT_TYPE (event)) {
4381         case GST_EVENT_SEGMENT:
4382           event = apply_pad_offset (pad, event);
4383           break;
4384         default:
4385           break;
4386       }
4387
4388       /* now do the probe */
4389       PROBE_PUSH (pad,
4390           type | GST_PAD_PROBE_TYPE_PUSH |
4391           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4392
4393       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4394       break;
4395   }
4396
4397   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4398     goto no_function;
4399
4400   ACQUIRE_PARENT (pad, parent, no_parent);
4401   GST_OBJECT_UNLOCK (pad);
4402
4403   ret = pre_eventfunc_check (pad, event);
4404   if (G_UNLIKELY (ret != GST_FLOW_OK))
4405     goto precheck_failed;
4406
4407   if (sticky)
4408     gst_event_ref (event);
4409
4410   if (eventfunc (pad, parent, event)) {
4411     ret = GST_FLOW_OK;
4412   } else {
4413     /* something went wrong */
4414     switch (event_type) {
4415       case GST_EVENT_CAPS:
4416         ret = GST_FLOW_NOT_NEGOTIATED;
4417         break;
4418       default:
4419         ret = GST_FLOW_ERROR;
4420         break;
4421     }
4422   }
4423   RELEASE_PARENT (parent);
4424
4425   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4426
4427   if (sticky) {
4428     if (ret == GST_FLOW_OK) {
4429       /* after the event function accepted the event, we can store the sticky
4430        * event on the pad */
4431       gst_pad_store_sticky_event (pad, event, FALSE);
4432     }
4433     gst_event_unref (event);
4434   }
4435
4436   if (need_unlock)
4437     GST_PAD_STREAM_UNLOCK (pad);
4438
4439   return ret;
4440
4441   /* ERROR handling */
4442 flushing:
4443   {
4444     GST_OBJECT_UNLOCK (pad);
4445     if (need_unlock)
4446       GST_PAD_STREAM_UNLOCK (pad);
4447     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4448         "Received event on flushing pad. Discarding");
4449     gst_event_unref (event);
4450     return GST_FLOW_FLUSHING;
4451   }
4452 probe_stopped:
4453   {
4454     GST_OBJECT_UNLOCK (pad);
4455     if (need_unlock)
4456       GST_PAD_STREAM_UNLOCK (pad);
4457     gst_event_unref (event);
4458
4459     switch (ret) {
4460       case GST_FLOW_CUSTOM_SUCCESS:
4461         GST_DEBUG_OBJECT (pad, "dropped event");
4462         ret = GST_FLOW_OK;
4463         break;
4464       default:
4465         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4466         break;
4467     }
4468     return ret;
4469   }
4470 no_function:
4471   {
4472     g_warning ("pad %s:%s has no event handler, file a bug.",
4473         GST_DEBUG_PAD_NAME (pad));
4474     GST_OBJECT_UNLOCK (pad);
4475     if (need_unlock)
4476       GST_PAD_STREAM_UNLOCK (pad);
4477     gst_event_unref (event);
4478     return GST_FLOW_NOT_SUPPORTED;
4479   }
4480 no_parent:
4481   {
4482     GST_DEBUG_OBJECT (pad, "no parent");
4483     GST_OBJECT_UNLOCK (pad);
4484     if (need_unlock)
4485       GST_PAD_STREAM_UNLOCK (pad);
4486     gst_event_unref (event);
4487     return GST_FLOW_FLUSHING;
4488   }
4489 precheck_failed:
4490   {
4491     GST_DEBUG_OBJECT (pad, "pre event check failed");
4492     RELEASE_PARENT (parent);
4493     if (need_unlock)
4494       GST_PAD_STREAM_UNLOCK (pad);
4495     gst_event_unref (event);
4496     return ret;
4497   }
4498 }
4499
4500 /**
4501  * gst_pad_send_event:
4502  * @pad: a #GstPad to send the event to.
4503  * @event: (transfer full): the #GstEvent to send to the pad.
4504  *
4505  * Sends the event to the pad. This function can be used
4506  * by applications to send events in the pipeline.
4507  *
4508  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4509  * sink pad, @event should be a downstream event. For example, you would not
4510  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4511  * Furthermore, some downstream events have to be serialized with data flow,
4512  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4513  * the event needs to be serialized with data flow, this function will take the
4514  * pad's stream lock while calling its event function.
4515  *
4516  * To find out whether an event type is upstream, downstream, or downstream and
4517  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4518  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4519  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4520  * plugin doesn't need to bother itself with this information; the core handles
4521  * all necessary locks and checks.
4522  *
4523  * This function takes owership of the provided event so you should
4524  * gst_event_ref() it if you want to reuse the event after this call.
4525  *
4526  * Returns: TRUE if the event was handled.
4527  */
4528 gboolean
4529 gst_pad_send_event (GstPad * pad, GstEvent * event)
4530 {
4531   gboolean result;
4532   GstPadProbeType type;
4533
4534   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4535   g_return_val_if_fail (event != NULL, FALSE);
4536
4537   if (GST_PAD_IS_SINK (pad)) {
4538     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4539       goto wrong_direction;
4540     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4541   } else if (GST_PAD_IS_SRC (pad)) {
4542     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4543       goto wrong_direction;
4544     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4545   } else
4546     goto unknown_direction;
4547
4548   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4549     result = FALSE;
4550   else
4551     result = TRUE;
4552
4553   return result;
4554
4555   /* ERROR handling */
4556 wrong_direction:
4557   {
4558     g_warning ("pad %s:%s sending %s event in wrong direction",
4559         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4560     gst_event_unref (event);
4561     return FALSE;
4562   }
4563 unknown_direction:
4564   {
4565     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4566     gst_event_unref (event);
4567     return FALSE;
4568   }
4569 }
4570
4571 /**
4572  * gst_pad_set_element_private:
4573  * @pad: the #GstPad to set the private data of.
4574  * @priv: The private data to attach to the pad.
4575  *
4576  * Set the given private data gpointer on the pad.
4577  * This function can only be used by the element that owns the pad.
4578  * No locking is performed in this function.
4579  */
4580 void
4581 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4582 {
4583   pad->element_private = priv;
4584 }
4585
4586 /**
4587  * gst_pad_get_element_private:
4588  * @pad: the #GstPad to get the private data of.
4589  *
4590  * Gets the private data of a pad.
4591  * No locking is performed in this function.
4592  *
4593  * Returns: (transfer none): a #gpointer to the private data.
4594  */
4595 gpointer
4596 gst_pad_get_element_private (GstPad * pad)
4597 {
4598   return pad->element_private;
4599 }
4600
4601 /**
4602  * gst_pad_get_sticky_event:
4603  * @pad: the #GstPad to get the event from.
4604  * @event_type: the #GstEventType that should be retrieved.
4605  * @idx: the index of the event
4606  *
4607  * Returns a new reference of the sticky event of type @event_type
4608  * from the event.
4609  *
4610  * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4611  * event of @event_type was on @pad. Unref after usage.
4612  */
4613 GstEvent *
4614 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4615 {
4616   GstEvent *event = NULL;
4617   PadEvent *ev;
4618
4619   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4620   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4621
4622   GST_OBJECT_LOCK (pad);
4623   ev = find_event_by_type (pad, event_type, idx);
4624   if (ev && (event = ev->event))
4625     gst_event_ref (event);
4626   GST_OBJECT_UNLOCK (pad);
4627
4628   return event;
4629 }
4630
4631 typedef struct
4632 {
4633   GstPadStickyEventsForeachFunction func;
4634   gpointer user_data;
4635 } ForeachDispatch;
4636
4637 static gboolean
4638 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4639 {
4640   ForeachDispatch *data = user_data;
4641   gboolean ret;
4642
4643   GST_OBJECT_UNLOCK (pad);
4644
4645   ret = data->func (pad, &ev->event, data->user_data);
4646
4647   GST_OBJECT_LOCK (pad);
4648
4649   return ret;
4650 }
4651
4652 /**
4653  * gst_pad_sticky_events_foreach:
4654  * @pad: the #GstPad that should be used for iteration.
4655  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4656  *                should be called for every event.
4657  * @user_data: (closure): the optional user data.
4658  *
4659  * Iterates all sticky events on @pad and calls @foreach_func for every
4660  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4661  */
4662 void
4663 gst_pad_sticky_events_foreach (GstPad * pad,
4664     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4665 {
4666   ForeachDispatch data;
4667
4668   g_return_if_fail (GST_IS_PAD (pad));
4669   g_return_if_fail (foreach_func != NULL);
4670
4671   data.func = foreach_func;
4672   data.user_data = user_data;
4673
4674   GST_OBJECT_LOCK (pad);
4675   events_foreach (pad, foreach_dispatch_function, &data);
4676   GST_OBJECT_UNLOCK (pad);
4677 }
4678
4679 static void
4680 do_stream_status (GstPad * pad, GstStreamStatusType type,
4681     GThread * thread, GstTask * task)
4682 {
4683   GstElement *parent;
4684
4685   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4686
4687   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4688     if (GST_IS_ELEMENT (parent)) {
4689       GstMessage *message;
4690       GValue value = { 0 };
4691
4692       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4693         gchar *tname, *ename, *pname;
4694
4695         /* create a good task name */
4696         ename = gst_element_get_name (parent);
4697         pname = gst_pad_get_name (pad);
4698         tname = g_strdup_printf ("%s:%s", ename, pname);
4699         g_free (ename);
4700         g_free (pname);
4701
4702         gst_object_set_name (GST_OBJECT_CAST (task), tname);
4703         g_free (tname);
4704       }
4705
4706       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4707           type, parent);
4708
4709       g_value_init (&value, GST_TYPE_TASK);
4710       g_value_set_object (&value, task);
4711       gst_message_set_stream_status_object (message, &value);
4712       g_value_unset (&value);
4713
4714       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4715       gst_element_post_message (parent, message);
4716     }
4717     gst_object_unref (parent);
4718   }
4719 }
4720
4721 static void
4722 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4723 {
4724   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4725       thread, task);
4726 }
4727
4728 static void
4729 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4730 {
4731   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4732       thread, task);
4733 }
4734
4735 static GstTaskThreadCallbacks thr_callbacks = {
4736   pad_enter_thread,
4737   pad_leave_thread,
4738 };
4739
4740 /**
4741  * gst_pad_start_task:
4742  * @pad: the #GstPad to start the task of
4743  * @func: the task function to call
4744  * @data: data passed to the task function
4745  *
4746  * Starts a task that repeatedly calls @func with @data. This function
4747  * is mostly used in pad activation functions to start the dataflow.
4748  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4749  * before @func is called.
4750  *
4751  * Returns: a %TRUE if the task could be started.
4752  */
4753 gboolean
4754 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4755 {
4756   GstTask *task;
4757   gboolean res;
4758
4759   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4760   g_return_val_if_fail (func != NULL, FALSE);
4761
4762   GST_DEBUG_OBJECT (pad, "start task");
4763
4764   GST_OBJECT_LOCK (pad);
4765   task = GST_PAD_TASK (pad);
4766   if (task == NULL) {
4767     task = gst_task_new (func, data);
4768     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4769     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4770     GST_INFO_OBJECT (pad, "created task %p", task);
4771     GST_PAD_TASK (pad) = task;
4772     gst_object_ref (task);
4773     /* release lock to post the message */
4774     GST_OBJECT_UNLOCK (pad);
4775
4776     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4777
4778     gst_object_unref (task);
4779
4780     GST_OBJECT_LOCK (pad);
4781     /* nobody else is supposed to have changed the pad now */
4782     if (GST_PAD_TASK (pad) != task)
4783       goto concurrent_stop;
4784   }
4785   res = gst_task_set_state (task, GST_TASK_STARTED);
4786   GST_OBJECT_UNLOCK (pad);
4787
4788   return res;
4789
4790   /* ERRORS */
4791 concurrent_stop:
4792   {
4793     GST_OBJECT_UNLOCK (pad);
4794     return TRUE;
4795   }
4796 }
4797
4798 /**
4799  * gst_pad_pause_task:
4800  * @pad: the #GstPad to pause the task of
4801  *
4802  * Pause the task of @pad. This function will also wait until the
4803  * function executed by the task is finished if this function is not
4804  * called from the task function.
4805  *
4806  * Returns: a TRUE if the task could be paused or FALSE when the pad
4807  * has no task.
4808  */
4809 gboolean
4810 gst_pad_pause_task (GstPad * pad)
4811 {
4812   GstTask *task;
4813   gboolean res;
4814
4815   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4816
4817   GST_DEBUG_OBJECT (pad, "pause task");
4818
4819   GST_OBJECT_LOCK (pad);
4820   task = GST_PAD_TASK (pad);
4821   if (task == NULL)
4822     goto no_task;
4823   res = gst_task_set_state (task, GST_TASK_PAUSED);
4824   GST_OBJECT_UNLOCK (pad);
4825
4826   /* wait for task function to finish, this lock is recursive so it does nothing
4827    * when the pause is called from the task itself */
4828   GST_PAD_STREAM_LOCK (pad);
4829   GST_PAD_STREAM_UNLOCK (pad);
4830
4831   return res;
4832
4833 no_task:
4834   {
4835     GST_DEBUG_OBJECT (pad, "pad has no task");
4836     GST_OBJECT_UNLOCK (pad);
4837     return FALSE;
4838   }
4839 }
4840
4841 /**
4842  * gst_pad_stop_task:
4843  * @pad: the #GstPad to stop the task of
4844  *
4845  * Stop the task of @pad. This function will also make sure that the
4846  * function executed by the task will effectively stop if not called
4847  * from the GstTaskFunction.
4848  *
4849  * This function will deadlock if called from the GstTaskFunction of
4850  * the task. Use gst_task_pause() instead.
4851  *
4852  * Regardless of whether the pad has a task, the stream lock is acquired and
4853  * released so as to ensure that streaming through this pad has finished.
4854  *
4855  * Returns: a TRUE if the task could be stopped or FALSE on error.
4856  */
4857 gboolean
4858 gst_pad_stop_task (GstPad * pad)
4859 {
4860   GstTask *task;
4861   gboolean res;
4862
4863   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4864
4865   GST_DEBUG_OBJECT (pad, "stop task");
4866
4867   GST_OBJECT_LOCK (pad);
4868   task = GST_PAD_TASK (pad);
4869   if (task == NULL)
4870     goto no_task;
4871   GST_PAD_TASK (pad) = NULL;
4872   res = gst_task_set_state (task, GST_TASK_STOPPED);
4873   GST_OBJECT_UNLOCK (pad);
4874
4875   GST_PAD_STREAM_LOCK (pad);
4876   GST_PAD_STREAM_UNLOCK (pad);
4877
4878   if (!gst_task_join (task))
4879     goto join_failed;
4880
4881   gst_object_unref (task);
4882
4883   return res;
4884
4885 no_task:
4886   {
4887     GST_DEBUG_OBJECT (pad, "no task");
4888     GST_OBJECT_UNLOCK (pad);
4889
4890     GST_PAD_STREAM_LOCK (pad);
4891     GST_PAD_STREAM_UNLOCK (pad);
4892
4893     /* this is not an error */
4894     return TRUE;
4895   }
4896 join_failed:
4897   {
4898     /* this is bad, possibly the application tried to join the task from
4899      * the task's thread. We install the task again so that it will be stopped
4900      * again from the right thread next time hopefully. */
4901     GST_OBJECT_LOCK (pad);
4902     GST_DEBUG_OBJECT (pad, "join failed");
4903     /* we can only install this task if there was no other task */
4904     if (GST_PAD_TASK (pad) == NULL)
4905       GST_PAD_TASK (pad) = task;
4906     GST_OBJECT_UNLOCK (pad);
4907
4908     return FALSE;
4909   }
4910 }