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