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