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