*.c: Don't cast to GST_OBJECT when reffing or unreffing. Large source-munging commit!!!
[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 #include "gst_private.h"
24
25 #include "gstpad.h"
26 #include "gstenumtypes.h"
27 #include "gstmarshal.h"
28 #include "gstutils.h"
29 #include "gstelement.h"
30 #include "gstbin.h"
31 #include "gstscheduler.h"
32 #include "gstevent.h"
33 #include "gstinfo.h"
34 #include "gsterror.h"
35 #include "gstvalue.h"
36
37 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
38 #define DEBUG_DATA(obj,data,notice) G_STMT_START{\
39   if (!data) { \
40     GST_CAT_DEBUG_OBJECT (debug_dataflow, obj, "NULL data value"); \
41   } else if (GST_IS_EVENT (data)) { \
42     GST_CAT_DEBUG_OBJECT (debug_dataflow, obj, "%s event %p (type %d, refcount %d)", notice, data, \
43         GST_EVENT_TYPE (data), GST_DATA_REFCOUNT_VALUE (data)); \
44   } else { \
45     GST_CAT_LOG_OBJECT (debug_dataflow, obj, "%s buffer %p (size %u, refcount %d)", notice, data, \
46         GST_BUFFER_SIZE (data), GST_BUFFER_REFCOUNT_VALUE (data)); \
47   } \
48 }G_STMT_END
49 #define GST_CAT_DEFAULT GST_CAT_PADS
50
51 enum
52 {
53   TEMPL_PAD_CREATED,
54   /* FILL ME */
55   TEMPL_LAST_SIGNAL
56 };
57
58 static GstObject *padtemplate_parent_class = NULL;
59 static guint gst_pad_template_signals[TEMPL_LAST_SIGNAL] = { 0 };
60
61 /* Pad signals and args */
62 enum
63 {
64   PAD_LINKED,
65   PAD_UNLINKED,
66   PAD_REQUEST_LINK,
67   /* FILL ME */
68   PAD_LAST_SIGNAL
69 };
70
71 enum
72 {
73   PAD_PROP_0,
74   PAD_PROP_CAPS,
75   PAD_PROP_DIRECTION,
76   PAD_PROP_TEMPLATE,
77   /* FILL ME */
78 };
79
80 GType _gst_pad_type = 0;
81
82 static void gst_pad_class_init (GstPadClass * klass);
83 static void gst_pad_init (GstPad * pad);
84 static void gst_pad_dispose (GObject * object);
85 static void gst_pad_finalize (GObject * object);
86 static void gst_pad_set_property (GObject * object, guint prop_id,
87     const GValue * value, GParamSpec * pspec);
88 static void gst_pad_get_property (GObject * object, guint prop_id,
89     GValue * value, GParamSpec * pspec);
90
91 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
92 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
93 static gboolean gst_pad_activate_default (GstPad * pad);
94
95 #ifndef GST_DISABLE_LOADSAVE
96 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
97 #endif
98
99 static GstObjectClass *pad_parent_class = NULL;
100 static guint gst_pad_signals[PAD_LAST_SIGNAL] = { 0 };
101
102 GType
103 gst_pad_get_type (void)
104 {
105   if (!_gst_pad_type) {
106     static const GTypeInfo pad_info = {
107       sizeof (GstPadClass), NULL, NULL,
108       (GClassInitFunc) gst_pad_class_init, NULL, NULL,
109       sizeof (GstPad),
110       0,
111       (GInstanceInitFunc) gst_pad_init, NULL
112     };
113
114     _gst_pad_type = g_type_register_static (GST_TYPE_OBJECT, "GstPad",
115         &pad_info, 0);
116
117     GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW",
118         GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads");
119   }
120   return _gst_pad_type;
121 }
122
123 static void
124 gst_pad_class_init (GstPadClass * klass)
125 {
126   GObjectClass *gobject_class;
127
128
129   GstObjectClass *gstobject_class;
130
131   gobject_class = (GObjectClass *) klass;
132   gstobject_class = (GstObjectClass *) klass;
133
134   pad_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
135
136   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
137   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pad_finalize);
138   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pad_set_property);
139   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pad_get_property);
140
141   gst_pad_signals[PAD_LINKED] =
142       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
143       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
144       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
145   gst_pad_signals[PAD_UNLINKED] =
146       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
147       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
148       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
149   gst_pad_signals[PAD_REQUEST_LINK] =
150       g_signal_new ("request_link", G_TYPE_FROM_CLASS (klass),
151       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
152       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
153
154   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_CAPS,
155       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
156           GST_TYPE_CAPS, G_PARAM_READABLE));
157   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_DIRECTION,
158       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
159           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
160           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
161   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_TEMPLATE,
162       g_param_spec_object ("template", "Template",
163           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
164           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
165
166 #ifndef GST_DISABLE_LOADSAVE
167   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
168 #endif
169   gstobject_class->path_string_separator = ".";
170 }
171
172 static void
173 gst_pad_init (GstPad * pad)
174 {
175   pad->direction = GST_PAD_UNKNOWN;
176   pad->peer = NULL;
177
178   pad->chainfunc = NULL;
179
180   pad->caps = NULL;
181
182   pad->linkfunc = NULL;
183   pad->getcapsfunc = NULL;
184
185   pad->activatefunc = gst_pad_activate_default;
186   pad->eventfunc = gst_pad_event_default;
187   pad->querytypefunc = gst_pad_get_query_types_default;
188   pad->queryfunc = gst_pad_query_default;
189   pad->intlinkfunc = gst_pad_get_internal_links_default;
190
191   GST_PAD_UNSET_FLUSHING (pad);
192
193   pad->preroll_lock = g_mutex_new ();
194   pad->preroll_cond = g_cond_new ();
195
196   pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
197   g_static_rec_mutex_init (pad->stream_rec_lock);
198
199   pad->block_cond = g_cond_new ();
200 }
201
202 static void
203 gst_pad_dispose (GObject * object)
204 {
205   GstPad *pad = GST_PAD (object);
206
207   gst_pad_set_pad_template (pad, NULL);
208   /* FIXME, we have links to many other things like caps
209    * and the peer pad... */
210
211   /* No linked pad can ever be disposed.
212    * It has to have a parent to be linked 
213    * and a parent would hold a reference */
214   /* FIXME: what about if g_object_dispose is explicitly called on the pad? Is
215      that legal? otherwise we could assert GST_OBJECT_PARENT (pad) == NULL as
216      well... */
217   g_assert (GST_PAD_PEER (pad) == NULL);
218
219   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s",
220       GST_DEBUG_PAD_NAME (pad));
221
222   /* clear the caps */
223   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
224
225   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))) {
226     GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'",
227         GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
228
229     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
230   }
231
232   G_OBJECT_CLASS (pad_parent_class)->dispose (object);
233 }
234
235 static void
236 gst_pad_finalize (GObject * object)
237 {
238   GstPad *pad = GST_PAD (object);
239
240   if (pad->stream_rec_lock) {
241     g_static_rec_mutex_free (pad->stream_rec_lock);
242     pad->stream_rec_lock = NULL;
243   }
244   if (pad->preroll_lock) {
245     g_mutex_free (pad->preroll_lock);
246     g_cond_free (pad->preroll_cond);
247     pad->preroll_lock = NULL;
248     pad->preroll_cond = NULL;
249   }
250   if (pad->block_cond) {
251     g_cond_free (pad->block_cond);
252     pad->block_cond = NULL;
253   }
254
255   G_OBJECT_CLASS (pad_parent_class)->finalize (object);
256 }
257
258 static void
259 gst_pad_set_property (GObject * object, guint prop_id,
260     const GValue * value, GParamSpec * pspec)
261 {
262   g_return_if_fail (GST_IS_PAD (object));
263
264   switch (prop_id) {
265     case PAD_PROP_DIRECTION:
266       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
267       break;
268     case PAD_PROP_TEMPLATE:
269       gst_pad_set_pad_template (GST_PAD_CAST (object),
270           (GstPadTemplate *) g_value_dup_object (value));
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275   }
276 }
277
278 static void
279 gst_pad_get_property (GObject * object, guint prop_id,
280     GValue * value, GParamSpec * pspec)
281 {
282   g_return_if_fail (GST_IS_PAD (object));
283
284   switch (prop_id) {
285     case PAD_PROP_CAPS:
286       g_value_set_boxed (value, GST_PAD_CAPS (object));
287       break;
288     case PAD_PROP_DIRECTION:
289       g_value_set_enum (value, GST_PAD_DIRECTION (object));
290       break;
291     case PAD_PROP_TEMPLATE:
292       g_value_set_object (value, GST_PAD_TEMPLATE (object));
293       break;
294     default:
295       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296       break;
297   }
298 }
299
300 /**
301  * gst_pad_new:
302  * @name: the name of the new pad.
303  * @direction: the #GstPadDirection of the pad.
304  *
305  * Creates a new pad with the given name in the given direction.
306  * If name is NULL, a guaranteed unique name (across all pads) 
307  * will be assigned.
308  * This function makes a copy of the name so you can safely free the name.
309  *
310  * Returns: a new #GstPad, or NULL in case of an error.
311  *
312  * MT safe.
313  */
314 GstPad *
315 gst_pad_new (const gchar * name, GstPadDirection direction)
316 {
317   return g_object_new (GST_TYPE_PAD,
318       "name", name, "direction", direction, NULL);
319 }
320
321 /**
322  * gst_pad_new_from_template:
323  * @templ: the pad template to use
324  * @name: the name of the element
325  *
326  * Creates a new pad with the given name from the given template.
327  * If name is NULL, a guaranteed unique name (across all pads) 
328  * will be assigned.
329  * This function makes a copy of the name so you can safely free the name.
330  *
331  * Returns: a new #GstPad, or NULL in case of an error.
332  */
333 GstPad *
334 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
335 {
336   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
337
338   return g_object_new (GST_TYPE_PAD,
339       "name", name, "direction", templ->direction, "template", templ, NULL);
340 }
341
342 /**
343  * gst_pad_get_parent:
344  * @pad: a pad
345  *
346  * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
347  * its parent is not an element, return NULL.
348  *
349  * Returns: The parent of the pad. The caller has a reference on the parent, so
350  * unref when you're finished with it.
351  *
352  * MT safe.
353  */
354 GstElement *
355 gst_pad_get_parent (GstPad * pad)
356 {
357   GstObject *p;
358
359   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
360
361   p = gst_object_get_parent (GST_OBJECT_CAST (pad));
362
363   if (p && !GST_IS_ELEMENT (p)) {
364     gst_object_unref (p);
365     p = NULL;
366   }
367
368   return GST_ELEMENT_CAST (p);
369 }
370
371 /**
372  * gst_pad_get_direction:
373  * @pad: a #GstPad to get the direction of.
374  *
375  * Gets the direction of the pad. The direction of the pad is
376  * decided at construction time so this function does not take 
377  * the LOCK.
378  *
379  * Returns: the #GstPadDirection of the pad.
380  *
381  * MT safe.
382  */
383 GstPadDirection
384 gst_pad_get_direction (GstPad * pad)
385 {
386   GstPadDirection result;
387
388   /* PAD_UNKNOWN is a little silly but we need some sort of
389    * error return value */
390   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
391
392   GST_LOCK (pad);
393   result = GST_PAD_DIRECTION (pad);
394   GST_UNLOCK (pad);
395
396   return result;
397 }
398
399 static gboolean
400 gst_pad_activate_default (GstPad * pad)
401 {
402   return gst_pad_activate_push (pad, TRUE);
403 }
404
405 static void
406 pre_activate_switch (GstPad * pad, gboolean new_active)
407 {
408   if (new_active) {
409     return;
410   } else {
411     GST_LOCK (pad);
412     GST_PAD_SET_FLUSHING (pad);
413     /* unlock blocked pads so element can resume and stop */
414     GST_PAD_BLOCK_SIGNAL (pad);
415     GST_UNLOCK (pad);
416   }
417 }
418
419 static void
420 post_activate_switch (GstPad * pad, gboolean new_active)
421 {
422   if (new_active) {
423     GST_LOCK (pad);
424     GST_PAD_UNSET_FLUSHING (pad);
425     GST_UNLOCK (pad);
426   } else {
427     /* make streaming stop */
428     GST_STREAM_LOCK (pad);
429     GST_STREAM_UNLOCK (pad);
430   }
431 }
432
433 /**
434  * gst_pad_set_active:
435  * @pad: the #GstPad to activate or deactivate.
436  * @active: whether or not the pad should be active.
437  *
438  * Activates or deactivates the given pad. Must be called with the STATE_LOCK.
439  * Normally called from within core state change functions.
440  *
441  * If @active, makes sure the pad is active. If it is already active, either in
442  * push or pull mode, just return. Otherwise dispatches to the pad's activate
443  * function to perform the actual activation.
444  *
445  * If not @active, checks the pad's current mode and calls
446  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
447  * FALSE argument.
448  *
449  * Returns: TRUE if the operation was successfull.
450  *
451  * MT safe. Must be called with STATE_LOCK.
452  */
453 gboolean
454 gst_pad_set_active (GstPad * pad, gboolean active)
455 {
456   GstActivateMode old;
457   gboolean ret = FALSE;
458
459   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
460
461   GST_LOCK (pad);
462   old = GST_PAD_ACTIVATE_MODE (pad);
463   GST_UNLOCK (pad);
464
465   if (active) {
466     switch (old) {
467       case GST_ACTIVATE_PUSH:
468       case GST_ACTIVATE_PULL:
469         ret = TRUE;
470         break;
471       case GST_ACTIVATE_NONE:
472         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
473         break;
474     }
475   } else {
476     switch (old) {
477       case GST_ACTIVATE_PUSH:
478         ret = gst_pad_activate_push (pad, FALSE);
479         break;
480       case GST_ACTIVATE_PULL:
481         ret = gst_pad_activate_pull (pad, FALSE);
482         break;
483       case GST_ACTIVATE_NONE:
484         ret = TRUE;
485         break;
486     }
487   }
488
489   return ret;
490 }
491
492 /**
493  * gst_pad_activate_pull:
494  * @pad: the #GstPad to activate or deactivate.
495  * @active: whether or not the pad should be active.
496  *
497  * Activates or deactivates the given pad in pull mode via dispatching to the
498  * pad's activatepullfunc. For use from within pad activation functions only.
499  * When called on sink pads, will first proxy the call to the peer pad, which is
500  * expected to activate its internally linked pads from within its activate_pull
501  * function.
502  *
503  * If you don't know what this is, you probably don't want to call it.
504  *
505  * Returns: TRUE if the operation was successfull.
506  *
507  * MT safe.
508  */
509 gboolean
510 gst_pad_activate_pull (GstPad * pad, gboolean active)
511 {
512   GstActivateMode old;
513
514   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
515
516   GST_LOCK (pad);
517   old = GST_PAD_ACTIVATE_MODE (pad);
518   GST_UNLOCK (pad);
519
520   if ((active && old == GST_ACTIVATE_PULL)
521       || (!active && old == GST_ACTIVATE_NONE))
522     goto was_ok;
523
524   if (active) {
525     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
526   } else {
527     g_return_val_if_fail (old == GST_ACTIVATE_PULL, FALSE);
528   }
529
530   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
531     GstPad *peer = gst_pad_get_peer (pad);
532
533     if (peer) {
534       if (!gst_pad_activate_pull (peer, active)) {
535         GST_LOCK (peer);
536         GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
537             "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
538         GST_UNLOCK (peer);
539         gst_object_unref (peer);
540         goto failure;
541       }
542     }
543   }
544
545   pre_activate_switch (pad, active);
546
547   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
548     if (GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)) {
549       goto success;
550     } else {
551       goto failure;
552     }
553   } else {
554     /* can happen for sinks of passthrough elements */
555     goto success;
556   }
557
558 was_ok:
559   {
560     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
561         active ? "activated" : "deactivated");
562     return TRUE;
563   }
564
565 success:
566   {
567     GST_LOCK (pad);
568     GST_PAD_ACTIVATE_MODE (pad) =
569         active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
570     GST_UNLOCK (pad);
571     post_activate_switch (pad, active);
572
573     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
574         active ? "activated" : "deactivated");
575     return TRUE;
576   }
577
578 failure:
579   {
580     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
581         active ? "activate" : "deactivate");
582     return FALSE;
583   }
584 }
585
586 /**
587  * gst_pad_activate_push:
588  * @pad: the #GstPad to activate or deactivate.
589  * @active: whether or not the pad should be active.
590  *
591  * Activates or deactivates the given pad in push mode via dispatching to the
592  * pad's activatepushfunc. For use from within pad activation functions only.
593  *
594  * If you don't know what this is, you probably don't want to call it.
595  *
596  * Returns: TRUE if the operation was successfull.
597  *
598  * MT safe.
599  */
600 gboolean
601 gst_pad_activate_push (GstPad * pad, gboolean active)
602 {
603   GstActivateMode old;
604
605   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
606
607   GST_LOCK (pad);
608   old = GST_PAD_ACTIVATE_MODE (pad);
609   GST_UNLOCK (pad);
610
611   if ((active && old == GST_ACTIVATE_PUSH)
612       || (!active && old == GST_ACTIVATE_NONE))
613     goto was_ok;
614
615   if (active) {
616     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
617   } else {
618     g_return_val_if_fail (old == GST_ACTIVATE_PUSH, FALSE);
619   }
620
621   pre_activate_switch (pad, active);
622
623   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
624     if (GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active)) {
625       goto success;
626     } else {
627       goto failure;
628     }
629   } else {
630     /* quite ok, element relies on state change func to prepare itself */
631     goto success;
632   }
633
634 was_ok:
635   {
636     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
637         active ? "activated" : "deactivated");
638     return TRUE;
639   }
640
641 success:
642   {
643     GST_LOCK (pad);
644     GST_PAD_ACTIVATE_MODE (pad) =
645         active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
646     GST_UNLOCK (pad);
647     post_activate_switch (pad, active);
648
649     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
650         active ? "activated" : "deactivated");
651     return TRUE;
652   }
653
654 failure:
655   {
656     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
657         active ? "activate" : "deactivate");
658     return FALSE;
659   }
660 }
661
662 /**
663  * gst_pad_is_active:
664  * @pad: the #GstPad to query
665  *
666  * Query if a pad is active
667  *
668  * Returns: TRUE if the pad is active.
669  *
670  * MT safe.
671  */
672 gboolean
673 gst_pad_is_active (GstPad * pad)
674 {
675   gboolean result = FALSE;
676
677   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
678
679   GST_LOCK (pad);
680   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
681   GST_UNLOCK (pad);
682
683   return result;
684 }
685
686 /**
687  * gst_pad_set_blocked_async:
688  * @pad: the #GstPad to block or unblock
689  * @blocked: boolean indicating we should block or unblock
690  * @callback: #GstPadBlockCallback that will be called when the
691  *            operation succeeds.
692  * @user_data: user data passed to the callback
693  *
694  * Blocks or unblocks the dataflow on a pad. The provided callback
695  * is called when the operation succeeds. This can take a while as
696  * the pad can only become blocked when real dataflow is happening.
697  * When the pipeline is stalled, for example in PAUSED, this can
698  * take an indeterminate amount of time.
699  * You can pass NULL as the callback to make this call block. Be
700  * carefull with this blocking call as it might not return for
701  * reasons stated above.
702  *
703  * Returns: TRUE if the pad could be blocked. This function can fail
704  *   if wrong parameters were passed or the pad was already in the 
705  *   requested state.
706  *
707  * MT safe.
708  */
709 gboolean
710 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
711     GstPadBlockCallback callback, gpointer user_data)
712 {
713   gboolean was_blocked;
714
715   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
716
717   GST_LOCK (pad);
718
719   was_blocked = GST_PAD_IS_BLOCKED (pad);
720
721   if (G_UNLIKELY (was_blocked == blocked))
722     goto had_right_state;
723
724   if (blocked) {
725     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad %s:%s",
726         GST_DEBUG_PAD_NAME (pad));
727
728     GST_FLAG_SET (pad, GST_PAD_BLOCKED);
729     pad->block_callback = callback;
730     pad->block_data = user_data;
731     if (!callback) {
732       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
733       GST_PAD_BLOCK_WAIT (pad);
734       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
735     }
736   } else {
737     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad %s:%s",
738         GST_DEBUG_PAD_NAME (pad));
739
740     GST_FLAG_UNSET (pad, GST_PAD_BLOCKED);
741
742     pad->block_callback = callback;
743     pad->block_data = user_data;
744
745     if (callback) {
746       GST_PAD_BLOCK_SIGNAL (pad);
747     } else {
748       GST_PAD_BLOCK_SIGNAL (pad);
749       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
750       GST_PAD_BLOCK_WAIT (pad);
751       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
752     }
753   }
754   GST_UNLOCK (pad);
755
756   return TRUE;
757
758 had_right_state:
759   {
760     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
761         "pad %s:%s was in right state", GST_DEBUG_PAD_NAME (pad));
762     GST_UNLOCK (pad);
763     return FALSE;
764   }
765 }
766
767 /**
768  * gst_pad_set_blocked:
769  * @pad: the #GstPad to block or unblock
770  * @blocked: boolean indicating we should block or unblock
771  *
772  * Blocks or unblocks the dataflow on a pad. This function is
773  * a shortcut for @gst_pad_set_blocked_async() with a NULL
774  * callback.
775  *
776  * Returns: TRUE if the pad could be blocked. This function can fail
777  *   wrong parameters were passed or the pad was already in the 
778  *   requested state.
779  *
780  * MT safe.
781  */
782 gboolean
783 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
784 {
785   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
786 }
787
788 /**
789  * gst_pad_is_blocked:
790  * @pad: the #GstPad to query 
791  *
792  * Checks if the pad is blocked or not. This function returns the
793  * last requested state of the pad. It is not certain that the pad
794  * is actually blocked at this point.
795  *
796  * Returns: TRUE if the pad is blocked.
797  *
798  * MT safe.
799  */
800 gboolean
801 gst_pad_is_blocked (GstPad * pad)
802 {
803   gboolean result = FALSE;
804
805   g_return_val_if_fail (GST_IS_PAD (pad), result);
806
807   GST_LOCK (pad);
808   result = GST_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
809   GST_UNLOCK (pad);
810
811   return result;
812 }
813
814 /**
815  * gst_pad_set_activate_function:
816  * @pad: a sink #GstPad.
817  * @chain: the #GstPadActivateFunction to set.
818  *
819  * Sets the given activate function for the pad. The activate function will
820  * dispatch to activate_push or activate_pull to perform the actual activation.
821  * Only makes sense to set on sink pads.
822  *
823  * Call this function if your sink pad can start a pull-based task.
824  */
825 void
826 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
827 {
828   g_return_if_fail (GST_IS_PAD (pad));
829
830   GST_PAD_ACTIVATEFUNC (pad) = activate;
831   GST_CAT_DEBUG (GST_CAT_PADS, "activatefunc for %s:%s set to %s",
832       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activate));
833 }
834
835 /**
836  * gst_pad_set_activatepull_function:
837  * @pad: a sink #GstPad.
838  * @chain: the #GstPadActivateModeFunction to set.
839  *
840  * Sets the given activate_pull function for the pad. An activate_pull function
841  * prepares the element and any upstream connections for pulling. See XXX
842  * part-activation.txt for details.
843  */
844 void
845 gst_pad_set_activatepull_function (GstPad * pad,
846     GstPadActivateModeFunction activatepull)
847 {
848   g_return_if_fail (GST_IS_PAD (pad));
849
850   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
851   GST_CAT_DEBUG (GST_CAT_PADS, "activatepullfunc for %s:%s set to %s",
852       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepull));
853 }
854
855 /**
856  * gst_pad_set_activatepush_function:
857  * @pad: a sink #GstPad.
858  * @chain: the #GstPadActivateModeFunction to set.
859  *
860  * Sets the given activate_push function for the pad. An activate_push function
861  * prepares the element for pushing. See XXX part-activation.txt for details.
862  */
863 void
864 gst_pad_set_activatepush_function (GstPad * pad,
865     GstPadActivateModeFunction activatepush)
866 {
867   g_return_if_fail (GST_IS_PAD (pad));
868
869   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
870   GST_CAT_DEBUG (GST_CAT_PADS, "activatepushfunc for %s:%s set to %s",
871       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepush));
872 }
873
874 /**
875  * gst_pad_set_loop_function:
876  * @pad: a sink #GstPad.
877  * @chain: the #GstPadLoopFunction to set.
878  *
879  * Sets the given loop function for the pad. The loop function is called 
880  * repeadedly to pull/push buffers from/to the peer pad.
881  */
882 void
883 gst_pad_set_loop_function (GstPad * pad, GstPadLoopFunction loop)
884 {
885   g_return_if_fail (GST_IS_PAD (pad));
886
887   GST_PAD_LOOPFUNC (pad) = loop;
888   GST_CAT_DEBUG (GST_CAT_PADS, "loopfunc for %s:%s set to %s",
889       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (loop));
890 }
891
892 /**
893  * gst_pad_set_chain_function:
894  * @pad: a sink #GstPad.
895  * @chain: the #GstPadChainFunction to set.
896  *
897  * Sets the given chain function for the pad. The chain function is called to
898  * process a #GstBuffer input buffer.
899  */
900 void
901 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
902 {
903   g_return_if_fail (GST_IS_PAD (pad));
904   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
905
906   GST_PAD_CHAINFUNC (pad) = chain;
907   GST_CAT_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
908       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
909 }
910
911 /**
912  * gst_pad_set_getrange_function:
913  * @pad: a source #GstPad.
914  * @get: the #GstPadGetRangeFunction to set.
915  *
916  * Sets the given getrange function for the pad. The getrange function is called to
917  * produce a new #GstBuffer to start the processing pipeline. Getrange functions cannot
918  * return %NULL.
919  */
920 void
921 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
922 {
923   g_return_if_fail (GST_IS_PAD (pad));
924   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
925
926   GST_PAD_GETRANGEFUNC (pad) = get;
927
928   GST_CAT_DEBUG (GST_CAT_PADS, "getrangefunc for %s:%s  set to %s",
929       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
930 }
931
932 /**
933  * gst_pad_set_checkgetrange_function:
934  * @pad: a source #GstPad.
935  * @check: the #GstPadCheckGetRangeFunction to set.
936  *
937  * Sets the given checkgetrange function for the pad. Implement this function on
938  * a pad if you dynamically support getrange based scheduling on the pad.
939  */
940 void
941 gst_pad_set_checkgetrange_function (GstPad * pad,
942     GstPadCheckGetRangeFunction check)
943 {
944   g_return_if_fail (GST_IS_PAD (pad));
945   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
946
947   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
948
949   GST_CAT_DEBUG (GST_CAT_PADS, "checkgetrangefunc for %s:%s  set to %s",
950       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (check));
951 }
952
953 /**
954  * gst_pad_set_event_function:
955  * @pad: a source #GstPad.
956  * @event: the #GstPadEventFunction to set.
957  *
958  * Sets the given event handler for the pad.
959  */
960 void
961 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
962 {
963   g_return_if_fail (GST_IS_PAD (pad));
964
965   GST_PAD_EVENTFUNC (pad) = event;
966
967   GST_CAT_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
968       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
969 }
970
971 /**
972  * gst_pad_set_query_function:
973  * @pad: a #GstPad of either direction.
974  * @query: the #GstPadQueryFunction to set.
975  *
976  * Set the given query function for the pad.
977  */
978 void
979 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
980 {
981   g_return_if_fail (GST_IS_PAD (pad));
982
983   GST_PAD_QUERYFUNC (pad) = query;
984
985   GST_CAT_DEBUG (GST_CAT_PADS, "queryfunc for %s:%s  set to %s",
986       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (query));
987 }
988
989 /**
990  * gst_pad_set_query_type_function:
991  * @pad: a #GstPad of either direction.
992  * @type_func: the #GstPadQueryTypeFunction to set.
993  *
994  * Set the given query type function for the pad.
995  */
996 void
997 gst_pad_set_query_type_function (GstPad * pad,
998     GstPadQueryTypeFunction type_func)
999 {
1000   g_return_if_fail (GST_IS_PAD (pad));
1001
1002   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1003
1004   GST_CAT_DEBUG (GST_CAT_PADS, "querytypefunc for %s:%s  set to %s",
1005       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (type_func));
1006 }
1007
1008 /**
1009  * gst_pad_get_query_types:
1010  * @pad: a #GstPad.
1011  *
1012  * Get an array of supported queries that can be performed
1013  * on this pad.
1014  *
1015  * Returns: a zero-terminated array of #GstQueryType.
1016  */
1017 const GstQueryType *
1018 gst_pad_get_query_types (GstPad * pad)
1019 {
1020   GstPadQueryTypeFunction func;
1021
1022   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1023
1024   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1025     goto no_func;
1026
1027   return func (pad);
1028
1029 no_func:
1030   {
1031     return NULL;
1032   }
1033 }
1034
1035 static gboolean
1036 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1037 {
1038   *data = gst_pad_get_query_types (pad);
1039
1040   return TRUE;
1041 }
1042
1043 /**
1044  * gst_pad_get_query_types_default:
1045  * @pad: a #GstPad.
1046  *
1047  * Invoke the default dispatcher for the query types on
1048  * the pad.
1049  *
1050  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1051  * internally-linked pads has a query types function.
1052  */
1053 const GstQueryType *
1054 gst_pad_get_query_types_default (GstPad * pad)
1055 {
1056   GstQueryType *result = NULL;
1057
1058   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1059
1060   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1061       gst_pad_get_query_types_dispatcher, &result);
1062
1063   return result;
1064 }
1065
1066 /**
1067  * gst_pad_set_internal_link_function:
1068  * @pad: a #GstPad of either direction.
1069  * @intlink: the #GstPadIntLinkFunction to set.
1070  *
1071  * Sets the given internal link function for the pad.
1072  */
1073 void
1074 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1075 {
1076   g_return_if_fail (GST_IS_PAD (pad));
1077
1078   GST_PAD_INTLINKFUNC (pad) = intlink;
1079   GST_CAT_DEBUG (GST_CAT_PADS, "internal link for %s:%s  set to %s",
1080       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (intlink));
1081 }
1082
1083 /**
1084  * gst_pad_set_link_function:
1085  * @pad: a #GstPad.
1086  * @link: the #GstPadLinkFunction to set.
1087  * 
1088  * Sets the given link function for the pad. It will be called when the pad is
1089  * linked or relinked with caps. The caps passed to the link function is
1090  * the caps for the connnection. It can contain a non fixed caps.
1091  * 
1092  * The return value GST_PAD_LINK_OK should be used when the connection can be
1093  * made.
1094  * 
1095  * The return value GST_PAD_LINK_REFUSED should be used when the connection
1096  * cannot be made for some reason.
1097  */
1098 void
1099 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1100 {
1101   g_return_if_fail (GST_IS_PAD (pad));
1102
1103   GST_PAD_LINKFUNC (pad) = link;
1104   GST_CAT_DEBUG (GST_CAT_PADS, "linkfunc for %s:%s set to %s",
1105       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (link));
1106 }
1107
1108 /**
1109  * gst_pad_set_unlink_function:
1110  * @pad: a #GstPad.
1111  * @unlink: the #GstPadUnlinkFunction to set.
1112  *
1113  * Sets the given unlink function for the pad. It will be called
1114  * when the pad is unlinked.
1115  */
1116 void
1117 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1118 {
1119   g_return_if_fail (GST_IS_PAD (pad));
1120
1121   GST_PAD_UNLINKFUNC (pad) = unlink;
1122   GST_CAT_DEBUG (GST_CAT_PADS, "unlinkfunc for %s:%s set to %s",
1123       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (unlink));
1124 }
1125
1126 /**
1127  * gst_pad_set_getcaps_function:
1128  * @pad: a #GstPad.
1129  * @getcaps: the #GstPadGetCapsFunction to set.
1130  * 
1131  * Sets the given getcaps function for the pad. @getcaps should return the
1132  * allowable caps for a pad in the context of the element's state, its link to
1133  * other elements, and the devices or files it has opened. These caps must be a
1134  * subset of the pad template caps. In the NULL state with no links, @getcaps
1135  * should ideally return the same caps as the pad template. In rare
1136  * circumstances, an object property can affect the caps returned by @getcaps,
1137  * but this is discouraged.
1138  *
1139  * You do not need to call this function if @pad's allowed caps are always the
1140  * same as the pad template caps. This can only be true if the padtemplate 
1141  * has fixed simple caps.
1142  *
1143  * For most filters, the caps returned by @getcaps is directly affected by the
1144  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1145  * the srcpad's getcaps function is directly related to the stream data. Again,
1146  * @getcaps should return the most specific caps it reasonably can, since this
1147  * helps with autoplugging. 
1148  *
1149  * Note that the return value from @getcaps is owned by the caller, so the caller
1150  * should unref the caps after usage.
1151  */
1152 void
1153 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1154 {
1155   g_return_if_fail (GST_IS_PAD (pad));
1156
1157   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1158   GST_CAT_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
1159       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
1160 }
1161
1162 /**
1163  * gst_pad_set_acceptcaps_function:
1164  * @pad: a #GstPad.
1165  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1166  *
1167  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1168  * will be called to check if the pad can accept the given caps.
1169  */
1170 void
1171 gst_pad_set_acceptcaps_function (GstPad * pad,
1172     GstPadAcceptCapsFunction acceptcaps)
1173 {
1174   g_return_if_fail (GST_IS_PAD (pad));
1175
1176   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1177   GST_CAT_DEBUG (GST_CAT_PADS, "acceptcapsfunc for %s:%s set to %s",
1178       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1179 }
1180
1181 /**
1182  * gst_pad_set_fixatecaps_function:
1183  * @pad: a #GstPad.
1184  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1185  *
1186  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1187  * will be called whenever the default values for a GstCaps needs to be
1188  * filled in.
1189  */
1190 void
1191 gst_pad_set_fixatecaps_function (GstPad * pad,
1192     GstPadFixateCapsFunction fixatecaps)
1193 {
1194   g_return_if_fail (GST_IS_PAD (pad));
1195
1196   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1197   GST_CAT_DEBUG (GST_CAT_PADS, "fixatecapsfunc for %s:%s set to %s",
1198       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1199 }
1200
1201 /**
1202  * gst_pad_set_setcaps_function:
1203  * @pad: a #GstPad.
1204  * @setcaps: the #GstPadSetCapsFunction to set.
1205  *
1206  * Sets the given setcaps function for the pad.  The setcaps function
1207  * will be called whenever a buffer with a new media type is pushed or
1208  * pulled from the pad. The pad/element needs to update it's internal
1209  * structures to process the new media type. If this new type is not
1210  * acceptable, the setcaps function should return FALSE.
1211  */
1212 void
1213 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1214 {
1215   g_return_if_fail (GST_IS_PAD (pad));
1216
1217   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1218   GST_CAT_DEBUG (GST_CAT_PADS, "setcapsfunc for %s:%s set to %s",
1219       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (setcaps));
1220 }
1221
1222 /**
1223  * gst_pad_set_bufferalloc_function:
1224  * @pad: a sink #GstPad.
1225  * @bufalloc: the #GstPadBufferAllocFunction to set.
1226  *
1227  * Sets the given bufferalloc function for the pad. Note that the
1228  * bufferalloc function can only be set on sinkpads.
1229  */
1230 void
1231 gst_pad_set_bufferalloc_function (GstPad * pad,
1232     GstPadBufferAllocFunction bufalloc)
1233 {
1234   g_return_if_fail (GST_IS_PAD (pad));
1235   g_return_if_fail (GST_PAD_IS_SINK (pad));
1236
1237   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1238   GST_CAT_DEBUG (GST_CAT_PADS, "bufferallocfunc for %s:%s set to %s",
1239       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufalloc));
1240 }
1241
1242 /**
1243  * gst_pad_unlink:
1244  * @srcpad: the source #GstPad to unlink.
1245  * @sinkpad: the sink #GstPad to unlink.
1246  *
1247  * Unlinks the source pad from the sink pad. Will emit the "unlinked" signal on
1248  * both pads.
1249  *
1250  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1251  * the pads were not linked together.
1252  *
1253  * MT safe.
1254  */
1255 gboolean
1256 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1257 {
1258   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1259   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1260
1261   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1262       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1263       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1264
1265   GST_LOCK (srcpad);
1266
1267   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1268     goto not_srcpad;
1269
1270   GST_LOCK (sinkpad);
1271
1272   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1273     goto not_sinkpad;
1274
1275   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1276     goto not_linked_together;
1277
1278   if (GST_PAD_UNLINKFUNC (srcpad)) {
1279     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1280   }
1281   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1282     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1283   }
1284
1285   /* first clear peers */
1286   GST_PAD_PEER (srcpad) = NULL;
1287   GST_PAD_PEER (sinkpad) = NULL;
1288
1289   GST_UNLOCK (sinkpad);
1290   GST_UNLOCK (srcpad);
1291
1292   /* fire off a signal to each of the pads telling them 
1293    * that they've been unlinked */
1294   g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1295   g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1296
1297   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1298       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1299
1300   return TRUE;
1301
1302 not_srcpad:
1303   {
1304     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1305     GST_UNLOCK (srcpad);
1306     return FALSE;
1307   }
1308 not_sinkpad:
1309   {
1310     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1311     GST_UNLOCK (sinkpad);
1312     GST_UNLOCK (srcpad);
1313     return FALSE;
1314   }
1315 not_linked_together:
1316   {
1317     /* we do not emit a warning in this case because unlinking cannot
1318      * be made MT safe.*/
1319     GST_UNLOCK (sinkpad);
1320     GST_UNLOCK (srcpad);
1321     return FALSE;
1322   }
1323 }
1324
1325 /**
1326  * gst_pad_is_linked:
1327  * @pad: pad to check
1328  *
1329  * Checks if a @pad is linked to another pad or not.
1330  *
1331  * Returns: TRUE if the pad is linked, FALSE otherwise.
1332  *
1333  * MT safe.
1334  */
1335 gboolean
1336 gst_pad_is_linked (GstPad * pad)
1337 {
1338   gboolean result;
1339
1340   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1341
1342   GST_LOCK (pad);
1343   result = (GST_PAD_PEER (pad) != NULL);
1344   GST_UNLOCK (pad);
1345
1346   return result;
1347 }
1348
1349 static gboolean
1350 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1351 {
1352   GstCaps *srccaps;
1353   GstCaps *sinkcaps;
1354
1355   srccaps = gst_pad_get_caps_unlocked (src);
1356   sinkcaps = gst_pad_get_caps_unlocked (sink);
1357   GST_CAT_DEBUG (GST_CAT_CAPS, "got caps %p and %p", srccaps, sinkcaps);
1358
1359   if (srccaps && sinkcaps) {
1360     GstCaps *icaps;
1361
1362     icaps = gst_caps_intersect (srccaps, sinkcaps);
1363     gst_caps_unref (srccaps);
1364     gst_caps_unref (sinkcaps);
1365
1366     GST_CAT_DEBUG (GST_CAT_CAPS,
1367         "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1368
1369     if (!icaps || gst_caps_is_empty (icaps))
1370       return FALSE;
1371   }
1372
1373   return TRUE;
1374 }
1375
1376
1377 /* FIXME leftover from an attempt at refactoring... */
1378 /* call with the two pads unlocked */
1379 static GstPadLinkReturn
1380 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1381 {
1382   /* generic checks */
1383   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1384   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1385
1386   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1387       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1388
1389   GST_LOCK (srcpad);
1390
1391   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1392     goto not_srcpad;
1393
1394   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1395     goto src_was_linked;
1396
1397   GST_LOCK (sinkpad);
1398
1399   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1400     goto not_sinkpad;
1401
1402   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1403     goto sink_was_linked;
1404
1405   /* check pad caps for non-empty intersection */
1406   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad)) {
1407     goto no_format;
1408   }
1409
1410   /* FIXME check pad scheduling for non-empty intersection */
1411
1412   return GST_PAD_LINK_OK;
1413
1414 not_srcpad:
1415   {
1416     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1417     GST_UNLOCK (srcpad);
1418     return GST_PAD_LINK_WRONG_DIRECTION;
1419   }
1420 src_was_linked:
1421   {
1422     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was linked",
1423         GST_DEBUG_PAD_NAME (srcpad));
1424     /* we do not emit a warning in this case because unlinking cannot
1425      * be made MT safe.*/
1426     GST_UNLOCK (srcpad);
1427     return GST_PAD_LINK_WAS_LINKED;
1428   }
1429 not_sinkpad:
1430   {
1431     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1432     GST_UNLOCK (sinkpad);
1433     GST_UNLOCK (srcpad);
1434     return GST_PAD_LINK_WRONG_DIRECTION;
1435   }
1436 sink_was_linked:
1437   {
1438     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was linked",
1439         GST_DEBUG_PAD_NAME (sinkpad));
1440     /* we do not emit a warning in this case because unlinking cannot
1441      * be made MT safe.*/
1442     GST_UNLOCK (sinkpad);
1443     GST_UNLOCK (srcpad);
1444     return GST_PAD_LINK_WAS_LINKED;
1445   }
1446 no_format:
1447   {
1448     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1449     GST_UNLOCK (sinkpad);
1450     GST_UNLOCK (srcpad);
1451     return GST_PAD_LINK_NOFORMAT;
1452   }
1453 }
1454
1455 /**
1456  * gst_pad_link:
1457  * @srcpad: the source #GstPad to link.
1458  * @sinkpad: the sink #GstPad to link.
1459  *
1460  * Links the source pad and the sink pad.
1461  *
1462  * Returns: A result code indicating if the connection worked or
1463  *          what went wrong.
1464  *
1465  * MT Safe.
1466  */
1467 GstPadLinkReturn
1468 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1469 {
1470   GstPadLinkReturn result;
1471
1472   /* prepare will also lock the two pads */
1473   result = gst_pad_link_prepare (srcpad, sinkpad);
1474
1475   if (result != GST_PAD_LINK_OK)
1476     goto prepare_failed;
1477
1478   GST_UNLOCK (sinkpad);
1479   GST_UNLOCK (srcpad);
1480
1481   /* FIXME released the locks here, concurrent thread might link
1482    * something else. */
1483   if (GST_PAD_LINKFUNC (srcpad)) {
1484     /* this one will call the peer link function */
1485     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
1486   } else if (GST_PAD_LINKFUNC (sinkpad)) {
1487     /* if no source link function, we need to call the sink link
1488      * function ourselves. */
1489     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
1490   } else {
1491     result = GST_PAD_LINK_OK;
1492   }
1493
1494   GST_LOCK (srcpad);
1495   GST_LOCK (sinkpad);
1496
1497   if (result == GST_PAD_LINK_OK) {
1498     GST_PAD_PEER (srcpad) = sinkpad;
1499     GST_PAD_PEER (sinkpad) = srcpad;
1500
1501     GST_UNLOCK (sinkpad);
1502     GST_UNLOCK (srcpad);
1503
1504     /* fire off a signal to each of the pads telling them
1505      * that they've been linked */
1506     g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
1507     g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
1508
1509     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1510         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1511   } else {
1512     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1513         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1514
1515     GST_UNLOCK (sinkpad);
1516     GST_UNLOCK (srcpad);
1517   }
1518   return result;
1519
1520 prepare_failed:
1521   {
1522     return result;
1523   }
1524 }
1525
1526 static void
1527 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1528 {
1529   /* this function would need checks if it weren't static */
1530
1531   GST_LOCK (pad);
1532   gst_object_replace ((GstObject **) & pad->padtemplate, (GstObject *) templ);
1533   GST_UNLOCK (pad);
1534
1535   if (templ) {
1536     gst_object_sink (GST_OBJECT (templ));
1537     g_signal_emit (G_OBJECT (templ),
1538         gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
1539   }
1540 }
1541
1542 /**
1543  * gst_pad_get_pad_template:
1544  * @pad: a #GstPad.
1545  *
1546  * Gets the template for @pad.
1547  *
1548  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
1549  * if this pad has no template.
1550  *
1551  * FIXME: currently returns an unrefcounted padtemplate.
1552  */
1553 GstPadTemplate *
1554 gst_pad_get_pad_template (GstPad * pad)
1555 {
1556   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1557
1558   return GST_PAD_PAD_TEMPLATE (pad);
1559 }
1560
1561
1562 /* should be called with the pad LOCK held */
1563 /* refs the caps, so caller is responsible for getting it unreffed */
1564 static GstCaps *
1565 gst_pad_get_caps_unlocked (GstPad * pad)
1566 {
1567   GstCaps *result = NULL;
1568
1569   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1570       GST_DEBUG_PAD_NAME (pad), pad);
1571
1572   if (GST_PAD_GETCAPSFUNC (pad)) {
1573     GST_CAT_DEBUG (GST_CAT_CAPS, "dispatching to pad getcaps function");
1574
1575     GST_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
1576     GST_UNLOCK (pad);
1577     result = GST_PAD_GETCAPSFUNC (pad) (pad);
1578     GST_LOCK (pad);
1579     GST_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
1580
1581     if (result == NULL) {
1582       g_critical ("pad %s:%s returned NULL caps from getcaps function",
1583           GST_DEBUG_PAD_NAME (pad));
1584     } else {
1585 #ifndef G_DISABLE_ASSERT
1586       /* check that the returned caps are a real subset of the template caps */
1587       if (GST_PAD_PAD_TEMPLATE (pad)) {
1588         const GstCaps *templ_caps =
1589             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1590         if (!gst_caps_is_subset (result, templ_caps)) {
1591           GstCaps *temp;
1592
1593           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
1594               "pad returned caps %" GST_PTR_FORMAT
1595               " which are not a real subset of its template caps %"
1596               GST_PTR_FORMAT, result, templ_caps);
1597           g_warning
1598               ("pad %s:%s returned caps that are not a real subset of its template caps",
1599               GST_DEBUG_PAD_NAME (pad));
1600           temp = gst_caps_intersect (templ_caps, result);
1601           gst_caps_unref (result);
1602           result = temp;
1603         }
1604       }
1605 #endif
1606       goto done;
1607     }
1608   }
1609   if (GST_PAD_PAD_TEMPLATE (pad)) {
1610     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1611
1612     result = GST_PAD_TEMPLATE_CAPS (templ);
1613     GST_CAT_DEBUG (GST_CAT_CAPS,
1614         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1615         result);
1616
1617     result = gst_caps_ref (result);
1618     goto done;
1619   }
1620   if (GST_PAD_CAPS (pad)) {
1621     result = GST_PAD_CAPS (pad);
1622
1623     GST_CAT_DEBUG (GST_CAT_CAPS,
1624         "using pad caps %p %" GST_PTR_FORMAT, result, result);
1625
1626     result = gst_caps_ref (result);
1627     goto done;
1628   }
1629
1630   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1631   result = gst_caps_new_empty ();
1632
1633 done:
1634   return result;
1635 }
1636
1637 /**
1638  * gst_pad_get_caps:
1639  * @pad: a  #GstPad to get the capabilities of.
1640  *
1641  * Gets the capabilities of this pad.
1642  *
1643  * Returns: the #GstCaps of this pad. This function returns a new caps, so use
1644  * gst_caps_unref to get rid of it.
1645  *
1646  * MT safe.
1647  */
1648 GstCaps *
1649 gst_pad_get_caps (GstPad * pad)
1650 {
1651   GstCaps *result = NULL;
1652
1653   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1654
1655   GST_LOCK (pad);
1656
1657   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1658       GST_DEBUG_PAD_NAME (pad), pad);
1659
1660   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (pad)))
1661     goto was_dispatching;
1662
1663   result = gst_pad_get_caps_unlocked (pad);
1664   GST_UNLOCK (pad);
1665
1666   return result;
1667
1668 was_dispatching:
1669   {
1670     GST_CAT_DEBUG (GST_CAT_CAPS,
1671         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1672     g_warning ("pad %s:%s recursively called getcaps!",
1673         GST_DEBUG_PAD_NAME (pad));
1674     GST_UNLOCK (pad);
1675     return NULL;
1676   }
1677 }
1678
1679 /**
1680  * gst_pad_peer_get_caps:
1681  * @pad: a  #GstPad to get the peer capabilities of.
1682  *
1683  * Gets the capabilities of the peer connected to this pad.
1684  *
1685  * Returns: the #GstCaps of the peer pad. This function returns a new caps, so use 
1686  * gst_caps_unref to get rid of it. this function returns NULL if there is no
1687  * peer pad or when this function is called recursively from a getcaps function.
1688  */
1689 GstCaps *
1690 gst_pad_peer_get_caps (GstPad * pad)
1691 {
1692   GstPad *peerpad;
1693   GstCaps *result = NULL;
1694
1695   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1696
1697   GST_LOCK (pad);
1698
1699   GST_CAT_DEBUG (GST_CAT_CAPS, "get peer caps of %s:%s (%p)",
1700       GST_DEBUG_PAD_NAME (pad), pad);
1701
1702   peerpad = GST_PAD_PEER (pad);
1703   if (G_UNLIKELY (peerpad == NULL))
1704     goto no_peer;
1705
1706   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (peerpad)))
1707     goto was_dispatching;
1708
1709   gst_object_ref (peerpad);
1710   GST_UNLOCK (pad);
1711
1712   result = gst_pad_get_caps (peerpad);
1713
1714   gst_object_unref (peerpad);
1715
1716   return result;
1717
1718 no_peer:
1719   {
1720     GST_UNLOCK (pad);
1721     return NULL;
1722   }
1723 was_dispatching:
1724   {
1725     GST_CAT_DEBUG (GST_CAT_CAPS,
1726         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1727     g_warning ("pad %s:%s recursively called getcaps!",
1728         GST_DEBUG_PAD_NAME (pad));
1729     GST_UNLOCK (pad);
1730     return NULL;
1731   }
1732 }
1733
1734 /**
1735  * gst_pad_fixate_caps:
1736  * @pad: a  #GstPad to fixate
1737  *
1738  * Fixate a caps on the given pad.
1739  *
1740  * Returns: a fixated #GstCaps.
1741  */
1742 GstCaps *
1743 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
1744 {
1745   /* FIXME, implement me, call the fixate function for the pad */
1746   return caps;
1747 }
1748
1749 /**
1750  * gst_pad_accept_caps:
1751  * @pad: a  #GstPad to check
1752  *
1753  * Check if the given pad accepts the caps.
1754  *
1755  * Returns: TRUE if the pad can accept the caps.
1756  */
1757 gboolean
1758 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
1759 {
1760   gboolean result;
1761
1762   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1763
1764   GST_LOCK (pad);
1765
1766   GST_CAT_DEBUG (GST_CAT_CAPS, "pad accept caps of %s:%s (%p)",
1767       GST_DEBUG_PAD_NAME (pad), pad);
1768
1769   if (GST_PAD_ACCEPTCAPSFUNC (pad)) {
1770     /* we can call the function */
1771     result = GST_PAD_ACCEPTCAPSFUNC (pad) (pad, caps);
1772   } else {
1773     /* else see get the caps and see if it intersects to something
1774      * not empty */
1775     GstCaps *intersect;
1776     GstCaps *allowed;
1777
1778     allowed = gst_pad_get_caps_unlocked (pad);
1779     intersect = gst_caps_intersect (allowed, caps);
1780     if (gst_caps_is_empty (intersect))
1781       result = FALSE;
1782     else
1783       result = TRUE;
1784   }
1785   GST_UNLOCK (pad);
1786
1787   return result;
1788 }
1789
1790 /**
1791  * gst_pad_peer_accept_caps:
1792  * @pad: a  #GstPad to check
1793  *
1794  * Check if the given pad accepts the caps.
1795  *
1796  * Returns: TRUE if the pad can accept the caps.
1797  */
1798 gboolean
1799 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
1800 {
1801   GstPad *peerpad;
1802   gboolean result;
1803
1804   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1805
1806   GST_LOCK (pad);
1807
1808   GST_CAT_DEBUG (GST_CAT_CAPS, "peer accept caps of %s:%s (%p)",
1809       GST_DEBUG_PAD_NAME (pad), pad);
1810
1811   peerpad = GST_PAD_PEER (pad);
1812   if (G_UNLIKELY (peerpad == NULL))
1813     goto no_peer;
1814
1815   result = gst_pad_accept_caps (peerpad, caps);
1816   GST_UNLOCK (pad);
1817
1818   return result;
1819
1820 no_peer:
1821   {
1822     GST_UNLOCK (pad);
1823     return TRUE;
1824   }
1825 }
1826
1827 /**
1828  * gst_pad_set_caps:
1829  * @pad: a  #GstPad to set the capabilities of.
1830  * @caps: a #GstCaps to set.
1831  *
1832  * Sets the capabilities of this pad. The caps must be fixed. Any previous
1833  * caps on the pad will be unreffed. This function refs the caps so you should
1834  * unref if as soon as you don't need it anymore.
1835  * It is possible to set NULL caps, which will make the pad unnegotiated
1836  * again.
1837  *
1838  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
1839  * or bad parameters were provided to this function.
1840  *
1841  * MT safe.
1842  */
1843 gboolean
1844 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
1845 {
1846   GstPadSetCapsFunction setcaps;
1847
1848   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1849
1850   GST_LOCK (pad);
1851   setcaps = GST_PAD_SETCAPSFUNC (pad);
1852
1853   /* call setcaps function to configure the pad */
1854   if (setcaps != NULL) {
1855     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
1856       GST_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
1857       GST_UNLOCK (pad);
1858       if (!setcaps (pad, caps))
1859         goto could_not_set;
1860       GST_LOCK (pad);
1861       GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
1862     } else {
1863       GST_CAT_DEBUG (GST_CAT_CAPS, "pad %s:%s was dispatching",
1864           GST_DEBUG_PAD_NAME (pad));
1865     }
1866   }
1867
1868   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
1869   GST_CAT_DEBUG (GST_CAT_CAPS, "%s:%s caps %" GST_PTR_FORMAT,
1870       GST_DEBUG_PAD_NAME (pad), caps);
1871   GST_UNLOCK (pad);
1872
1873   g_object_notify (G_OBJECT (pad), "caps");
1874
1875   return TRUE;
1876
1877 could_not_set:
1878   {
1879     GST_LOCK (pad);
1880     GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
1881     GST_UNLOCK (pad);
1882     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " could not be set",
1883         caps);
1884     return FALSE;
1885   }
1886 }
1887
1888 static gboolean
1889 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
1890 {
1891   GstPadAcceptCapsFunction acceptcaps;
1892   GstPadSetCapsFunction setcaps;
1893   gboolean res;
1894
1895   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
1896   setcaps = GST_PAD_SETCAPSFUNC (pad);
1897
1898   /* See if pad accepts the caps, by calling acceptcaps, only
1899    * needed if no setcaps function */
1900   if (setcaps == NULL && acceptcaps != NULL) {
1901     if (!acceptcaps (pad, caps))
1902       goto not_accepted;
1903   }
1904   /* set caps on pad if call succeeds */
1905   res = gst_pad_set_caps (pad, caps);
1906   /* no need to unref the caps here, set_caps takes a ref and
1907    * our ref goes away when we leave this function. */
1908
1909   return res;
1910
1911 not_accepted:
1912   {
1913     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
1914     return FALSE;
1915   }
1916 }
1917
1918 static gboolean
1919 gst_pad_configure_src (GstPad * pad, GstCaps * caps)
1920 {
1921   GstPadAcceptCapsFunction acceptcaps;
1922   GstPadSetCapsFunction setcaps;
1923   gboolean res;
1924
1925   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
1926   setcaps = GST_PAD_SETCAPSFUNC (pad);
1927
1928   /* See if pad accepts the caps, by calling acceptcaps, only
1929    * needed if no setcaps function */
1930   if (setcaps == NULL && acceptcaps != NULL) {
1931     if (!acceptcaps (pad, caps))
1932       goto not_accepted;
1933   }
1934   /* set caps on pad if call succeeds */
1935   res = gst_pad_set_caps (pad, caps);
1936   /* no need to unref the caps here, set_caps takes a ref and
1937    * our ref goes away when we leave this function. */
1938
1939   return res;
1940
1941 not_accepted:
1942   {
1943     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
1944     return FALSE;
1945   }
1946 }
1947
1948 /**
1949  * gst_pad_get_pad_template_caps:
1950  * @pad: a #GstPad to get the template capabilities from.
1951  *
1952  * Gets the capabilities for @pad's template.
1953  *
1954  * Returns: the #GstCaps of this pad template. If you intend to keep a reference
1955  * on the caps, make a copy (see gst_caps_copy ()).
1956  */
1957 const GstCaps *
1958 gst_pad_get_pad_template_caps (GstPad * pad)
1959 {
1960   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
1961
1962   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1963
1964   if (GST_PAD_PAD_TEMPLATE (pad))
1965     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1966
1967   return gst_static_caps_get (&anycaps);
1968 }
1969
1970
1971 /**
1972  * gst_pad_get_peer:
1973  * @pad: a #GstPad to get the peer of.
1974  *
1975  * Gets the peer of @pad. This function refs the peer pad so
1976  * you need to unref it after use.
1977  *
1978  * Returns: the peer #GstPad. Unref after usage.
1979  *
1980  * MT safe.
1981  */
1982 GstPad *
1983 gst_pad_get_peer (GstPad * pad)
1984 {
1985   GstPad *result;
1986
1987   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1988
1989   GST_LOCK (pad);
1990   result = GST_PAD_PEER (pad);
1991   if (result)
1992     gst_object_ref (result);
1993   GST_UNLOCK (pad);
1994
1995   return result;
1996 }
1997
1998 /**
1999  * gst_pad_get_allowed_caps:
2000  * @srcpad: a #GstPad, it must a a source pad.
2001  *
2002  * Gets the capabilities of the allowed media types that can flow through
2003  * @srcpad and its peer. The pad must be a source pad.
2004  * The caller must free the resulting caps.
2005  *
2006  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2007  * you no longer need it. This function returns NULL when the @srcpad has no
2008  * peer.
2009  *
2010  * MT safe.
2011  */
2012 GstCaps *
2013 gst_pad_get_allowed_caps (GstPad * srcpad)
2014 {
2015   GstCaps *mycaps;
2016   GstCaps *caps;
2017   GstCaps *peercaps;
2018   GstPad *peer;
2019
2020   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
2021   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
2022
2023   GST_LOCK (srcpad);
2024
2025   peer = GST_PAD_PEER (srcpad);
2026   if (G_UNLIKELY (peer == NULL))
2027     goto no_peer;
2028
2029   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting allowed caps",
2030       GST_DEBUG_PAD_NAME (srcpad));
2031
2032   gst_object_ref (peer);
2033   GST_UNLOCK (srcpad);
2034   mycaps = gst_pad_get_caps (srcpad);
2035
2036   peercaps = gst_pad_get_caps (peer);
2037   gst_object_unref (peer);
2038
2039   caps = gst_caps_intersect (mycaps, peercaps);
2040   gst_caps_unref (peercaps);
2041   gst_caps_unref (mycaps);
2042
2043   GST_CAT_DEBUG (GST_CAT_CAPS, "allowed caps %" GST_PTR_FORMAT, caps);
2044
2045   return caps;
2046
2047 no_peer:
2048   {
2049     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2050         GST_DEBUG_PAD_NAME (srcpad));
2051     GST_UNLOCK (srcpad);
2052
2053     return NULL;
2054   }
2055 }
2056
2057 /**
2058  * gst_pad_get_negotiated_caps:
2059  * @pad: a #GstPad.
2060  *
2061  * Gets the capabilities of the media type that currently flows through @pad
2062  * and its peer.
2063  *
2064  * This function can be used on both src and sinkpads. Note that srcpads are
2065  * always negotiated before sinkpads so it is possible that the negotiated caps
2066  * on the srcpad do not match the negotiated caps of the peer.
2067  *
2068  * Returns: the negotiated #GstCaps of the pad link.  Free the caps when
2069  * you no longer need it. This function returns NULL when the @pad has no
2070  * peer or is not negotiated yet.
2071  *
2072  * MT safe.
2073  */
2074 GstCaps *
2075 gst_pad_get_negotiated_caps (GstPad * pad)
2076 {
2077   GstCaps *caps;
2078   GstPad *peer;
2079
2080   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2081
2082   GST_LOCK (pad);
2083
2084   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2085     goto no_peer;
2086
2087   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting negotiated caps",
2088       GST_DEBUG_PAD_NAME (pad));
2089
2090   caps = GST_PAD_CAPS (pad);
2091   if (caps)
2092     gst_caps_ref (caps);
2093   GST_UNLOCK (pad);
2094
2095   GST_CAT_DEBUG (GST_CAT_CAPS, "negotiated caps %" GST_PTR_FORMAT, caps);
2096
2097   return caps;
2098
2099 no_peer:
2100   {
2101     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2102         GST_DEBUG_PAD_NAME (pad));
2103     GST_UNLOCK (pad);
2104
2105     return NULL;
2106   }
2107 }
2108
2109 /**
2110  * gst_pad_alloc_buffer:
2111  * @pad: a source #GstPad
2112  * @offset: the offset of the new buffer in the stream
2113  * @size: the size of the new buffer
2114  * @caps: the caps of the new buffer
2115  * @buf: a newly allocated buffer
2116  *
2117  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2118  * function only works if @pad is a source pad and has a peer. 
2119  *
2120  * You need to check the caps of the buffer after performing this 
2121  * function and renegotiate to the format if needed.
2122  *
2123  * A new, empty #GstBuffer will be put in the @buf argument.
2124  *
2125  * Returns: a result code indicating success of the operation. Any
2126  * result code other than GST_FLOW_OK is an error and @buf should
2127  * not be used.
2128  * An error can occur if the pad is not connected or when the downstream
2129  * peer elements cannot provide an acceptable buffer.
2130  *
2131  * MT safe.
2132  */
2133 GstFlowReturn
2134 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
2135     GstBuffer ** buf)
2136 {
2137   GstPad *peer;
2138   GstFlowReturn ret;
2139   GstPadBufferAllocFunction bufferallocfunc;
2140   gboolean caps_changed;
2141
2142   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2143   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2144   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2145
2146   GST_LOCK (pad);
2147   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2148     goto no_peer;
2149
2150   gst_object_ref (peer);
2151   GST_UNLOCK (pad);
2152
2153   if (G_LIKELY ((bufferallocfunc = peer->bufferallocfunc) == NULL))
2154     goto fallback;
2155
2156   GST_LOCK (peer);
2157   /* when the peer is flushing we cannot give a buffer */
2158   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (peer)))
2159     goto flushing;
2160
2161   GST_CAT_DEBUG (GST_CAT_PADS,
2162       "calling bufferallocfunc &%s (@%p) of peer pad %s:%s",
2163       GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2164       &bufferallocfunc, GST_DEBUG_PAD_NAME (peer));
2165   GST_UNLOCK (peer);
2166
2167   ret = bufferallocfunc (peer, offset, size, caps, buf);
2168
2169   if (G_UNLIKELY (ret != GST_FLOW_OK))
2170     goto peer_error;
2171   if (G_UNLIKELY (*buf == NULL))
2172     goto fallback;
2173
2174 do_caps:
2175   gst_object_unref (peer);
2176
2177   /* FIXME, move capnego this into a base class? */
2178   caps = GST_BUFFER_CAPS (*buf);
2179   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2180   /* we got a new datatype on the pad, see if it can handle it */
2181   if (G_UNLIKELY (caps_changed)) {
2182     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
2183     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps)))
2184       goto not_negotiated;
2185   }
2186   return ret;
2187
2188 no_peer:
2189   {
2190     /* pad has no peer */
2191     GST_CAT_DEBUG (GST_CAT_PADS,
2192         "%s:%s called bufferallocfunc but had no peer, returning NULL",
2193         GST_DEBUG_PAD_NAME (pad));
2194     GST_UNLOCK (pad);
2195     return GST_FLOW_NOT_CONNECTED;
2196   }
2197 flushing:
2198   {
2199     /* peer was flushing */
2200     GST_UNLOCK (peer);
2201     gst_object_unref (peer);
2202     GST_CAT_DEBUG (GST_CAT_PADS,
2203         "%s:%s called bufferallocfunc but peer was flushing, returning NULL",
2204         GST_DEBUG_PAD_NAME (pad));
2205     return GST_FLOW_WRONG_STATE;
2206   }
2207   /* fallback case, allocate a buffer of our own, add pad caps. */
2208 fallback:
2209   {
2210     GST_CAT_DEBUG (GST_CAT_PADS,
2211         "%s:%s fallback buffer alloc", GST_DEBUG_PAD_NAME (pad));
2212     *buf = gst_buffer_new_and_alloc (size);
2213     gst_buffer_set_caps (*buf, caps);
2214
2215     ret = GST_FLOW_OK;
2216
2217     goto do_caps;
2218   }
2219 not_negotiated:
2220   {
2221     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2222         "alloc function retured unacceptable buffer");
2223     return GST_FLOW_NOT_NEGOTIATED;
2224   }
2225 peer_error:
2226   {
2227     gst_object_unref (peer);
2228     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2229         "alloc function retured error %d", ret);
2230     return ret;
2231   }
2232 }
2233
2234 /**
2235  * gst_pad_get_internal_links_default:
2236  * @pad: the #GstPad to get the internal links of.
2237  *
2238  * Gets a list of pads to which the given pad is linked to
2239  * inside of the parent element.
2240  * This is the default handler, and thus returns a list of all of the
2241  * pads inside the parent element with opposite direction.
2242  * The caller must free this list after use.
2243  *
2244  * Returns: a newly allocated #GList of pads.
2245  *
2246  * Not MT safe.
2247  */
2248 GList *
2249 gst_pad_get_internal_links_default (GstPad * pad)
2250 {
2251   GList *res = NULL;
2252   GstElement *parent;
2253   GList *parent_pads;
2254   GstPadDirection direction;
2255
2256   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2257
2258   direction = pad->direction;
2259
2260   parent = GST_PAD_PARENT (pad);
2261   parent_pads = parent->pads;
2262
2263   while (parent_pads) {
2264     GstPad *parent_pad = GST_PAD_CAST (parent_pads->data);
2265
2266     if (parent_pad->direction != direction) {
2267       res = g_list_prepend (res, parent_pad);
2268     }
2269
2270     parent_pads = g_list_next (parent_pads);
2271   }
2272
2273   return res;
2274 }
2275
2276 /**
2277  * gst_pad_get_internal_links:
2278  * @pad: the #GstPad to get the internal links of.
2279  *
2280  * Gets a list of pads to which the given pad is linked to
2281  * inside of the parent element.
2282  * The caller must free this list after use.
2283  *
2284  * Returns: a newly allocated #GList of pads.
2285  *
2286  * Not MT safe.
2287  */
2288 GList *
2289 gst_pad_get_internal_links (GstPad * pad)
2290 {
2291   GList *res = NULL;
2292
2293   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2294
2295   if (GST_PAD_INTLINKFUNC (pad))
2296     res = GST_PAD_INTLINKFUNC (pad) (pad);
2297
2298   return res;
2299 }
2300
2301
2302 static gboolean
2303 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2304 {
2305   GList *orig, *pads;
2306   gboolean result;
2307
2308   GST_INFO_OBJECT (pad, "Sending event %p to all internally linked pads",
2309       event);
2310
2311   result = (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
2312
2313   orig = pads = gst_pad_get_internal_links (pad);
2314
2315   while (pads) {
2316     GstPad *eventpad = GST_PAD_CAST (pads->data);
2317
2318     pads = g_list_next (pads);
2319
2320     /* for all of the internally-linked pads that are actually linked */
2321     if (GST_PAD_IS_LINKED (eventpad)) {
2322       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2323         /* for each pad we send to, we should ref the event; it's up
2324          * to downstream to unref again when handled. */
2325         GST_LOG_OBJECT (pad, "Reffing and sending event %p to %s:%s", event,
2326             GST_DEBUG_PAD_NAME (eventpad));
2327         gst_event_ref (event);
2328         gst_pad_push_event (eventpad, event);
2329       } else {
2330         /* we only send the event on one pad, multi-sinkpad elements
2331          * should implement a handler */
2332         GST_LOG_OBJECT (pad, "sending event %p to one sink pad %s:%s", event,
2333             GST_DEBUG_PAD_NAME (eventpad));
2334         result = gst_pad_push_event (eventpad, event);
2335         goto done;
2336       }
2337     }
2338   }
2339   /* we handled the incoming event so we unref once */
2340   GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
2341   gst_event_unref (event);
2342
2343 done:
2344   g_list_free (orig);
2345
2346   return result;
2347 }
2348
2349 /**
2350  * gst_pad_event_default:
2351  * @pad: a #GstPad to call the default event handler on.
2352  * @event: the #GstEvent to handle.
2353  *
2354  * Invokes the default event handler for the given pad. End-of-stream and
2355  * discontinuity events are handled specially, and then the event is sent to all
2356  * pads internally linked to @pad. Note that if there are many possible sink
2357  * pads that are internally linked to @pad, only one will be sent an event.
2358  * Multi-sinkpad elements should implement custom event handlers.
2359  *
2360  * Returns: TRUE if the event was sent succesfully.
2361  */
2362 gboolean
2363 gst_pad_event_default (GstPad * pad, GstEvent * event)
2364 {
2365   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2366   g_return_val_if_fail (event != NULL, FALSE);
2367
2368   switch (GST_EVENT_TYPE (event)) {
2369     case GST_EVENT_EOS:
2370     {
2371       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2372       gst_pad_pause_task (pad);
2373     }
2374     default:
2375       break;
2376   }
2377
2378   return gst_pad_event_default_dispatch (pad, event);
2379 }
2380
2381 /**
2382  * gst_pad_dispatcher:
2383  * @pad: a #GstPad to dispatch.
2384  * @dispatch: the #GstDispatcherFunction to call.
2385  * @data: gpointer user data passed to the dispatcher function.
2386  *
2387  * Invokes the given dispatcher function on all pads that are 
2388  * internally linked to the given pad. 
2389  * The GstPadDispatcherFunction should return TRUE when no further pads 
2390  * need to be processed.
2391  *
2392  * Returns: TRUE if one of the dispatcher functions returned TRUE.
2393  */
2394 gboolean
2395 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
2396     gpointer data)
2397 {
2398   gboolean res = FALSE;
2399   GList *int_pads, *orig;
2400
2401   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2402   g_return_val_if_fail (dispatch != NULL, FALSE);
2403
2404   orig = int_pads = gst_pad_get_internal_links (pad);
2405
2406   while (int_pads) {
2407     GstPad *int_pad = GST_PAD_CAST (int_pads->data);
2408     GstPad *int_peer = GST_PAD_PEER (int_pad);
2409
2410     if (int_peer) {
2411       res = dispatch (int_peer, data);
2412       if (res)
2413         break;
2414     }
2415     int_pads = g_list_next (int_pads);
2416   }
2417
2418   g_list_free (orig);
2419
2420   return res;
2421 }
2422
2423 /**
2424  * gst_pad_query:
2425  * @pad: a #GstPad to invoke the default query on.
2426  * @query: the #GstQuery to perform.
2427  *
2428  * Dispatches a query to a pad. The query should have been allocated by the
2429  * caller via one of the type-specific allocation functions in gstquery.h. The
2430  * element is responsible for filling the query with an appropriate response,
2431  * which should then be parsed with a type-specific query parsing function.
2432  *
2433  * Again, the caller is responsible for both the allocation and deallocation of
2434  * the query structure.
2435  *
2436  * Returns: TRUE if the query could be performed.
2437  */
2438 gboolean
2439 gst_pad_query (GstPad * pad, GstQuery * query)
2440 {
2441   GstPadQueryFunction func;
2442
2443   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2444   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2445
2446   GST_DEBUG ("sending query %p to pad %s:%s", query, GST_DEBUG_PAD_NAME (pad));
2447
2448   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
2449     goto no_func;
2450
2451   return func (pad, query);
2452
2453 no_func:
2454   {
2455     GST_DEBUG ("pad had no query function");
2456     return FALSE;
2457   }
2458 }
2459
2460 gboolean
2461 gst_pad_query_default (GstPad * pad, GstQuery * query)
2462 {
2463   switch (GST_QUERY_TYPE (query)) {
2464     case GST_QUERY_POSITION:
2465     case GST_QUERY_SEEKING:
2466     case GST_QUERY_FORMATS:
2467     case GST_QUERY_LATENCY:
2468     case GST_QUERY_JITTER:
2469     case GST_QUERY_RATE:
2470     case GST_QUERY_CONVERT:
2471     default:
2472       return gst_pad_dispatcher
2473           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
2474   }
2475 }
2476
2477 #ifndef GST_DISABLE_LOADSAVE
2478 /* FIXME: why isn't this on a GstElement ? */
2479 /**
2480  * gst_pad_load_and_link:
2481  * @self: an #xmlNodePtr to read the description from.
2482  * @parent: the #GstObject element that owns the pad.
2483  *
2484  * Reads the pad definition from the XML node and links the given pad
2485  * in the element to a pad of an element up in the hierarchy.
2486  */
2487 void
2488 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
2489 {
2490   xmlNodePtr field = self->xmlChildrenNode;
2491   GstPad *pad = NULL, *targetpad;
2492   gchar *peer = NULL;
2493   gchar **split;
2494   GstElement *target;
2495   GstObject *grandparent;
2496   gchar *name = NULL;
2497
2498   while (field) {
2499     if (!strcmp ((char *) field->name, "name")) {
2500       name = (gchar *) xmlNodeGetContent (field);
2501       pad = gst_element_get_pad (GST_ELEMENT (parent), name);
2502       g_free (name);
2503     } else if (!strcmp ((char *) field->name, "peer")) {
2504       peer = (gchar *) xmlNodeGetContent (field);
2505     }
2506     field = field->next;
2507   }
2508   g_return_if_fail (pad != NULL);
2509
2510   if (peer == NULL)
2511     return;
2512
2513   split = g_strsplit (peer, ".", 2);
2514
2515   if (split[0] == NULL || split[1] == NULL) {
2516     GST_CAT_DEBUG (GST_CAT_XML,
2517         "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2518         peer, GST_DEBUG_PAD_NAME (pad));
2519
2520     g_free (peer);
2521     return;
2522   }
2523   g_free (peer);
2524
2525   g_return_if_fail (split[0] != NULL);
2526   g_return_if_fail (split[1] != NULL);
2527
2528   grandparent = gst_object_get_parent (parent);
2529
2530   if (grandparent && GST_IS_BIN (grandparent)) {
2531     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2532   } else
2533     goto cleanup;
2534
2535   if (target == NULL)
2536     goto cleanup;
2537
2538   targetpad = gst_element_get_pad (target, split[1]);
2539
2540   if (targetpad == NULL)
2541     goto cleanup;
2542
2543   gst_pad_link (pad, targetpad);
2544
2545 cleanup:
2546   g_strfreev (split);
2547 }
2548
2549 /**
2550  * gst_pad_save_thyself:
2551  * @pad: a #GstPad to save.
2552  * @parent: the parent #xmlNodePtr to save the description in.
2553  *
2554  * Saves the pad into an xml representation.
2555  *
2556  * Returns: the #xmlNodePtr representation of the pad.
2557  */
2558 static xmlNodePtr
2559 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
2560 {
2561   GstPad *pad;
2562   GstPad *peer;
2563
2564   g_return_val_if_fail (GST_IS_PAD (object), NULL);
2565
2566   pad = GST_PAD (object);
2567
2568   xmlNewChild (parent, NULL, (xmlChar *) "name",
2569       (xmlChar *) GST_PAD_NAME (pad));
2570   if (GST_PAD_PEER (pad) != NULL) {
2571     gchar *content;
2572
2573     peer = GST_PAD_PEER (pad);
2574     /* first check to see if the peer's parent's parent is the same */
2575     /* we just save it off */
2576     content = g_strdup_printf ("%s.%s",
2577         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
2578     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
2579     g_free (content);
2580   } else
2581     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
2582
2583   return parent;
2584 }
2585
2586 #if 0
2587 /**
2588  * gst_ghost_pad_save_thyself:
2589  * @pad: a ghost #GstPad to save.
2590  * @parent: the parent #xmlNodePtr to save the description in.
2591  *
2592  * Saves the ghost pad into an xml representation.
2593  *
2594  * Returns: the #xmlNodePtr representation of the pad.
2595  */
2596 xmlNodePtr
2597 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
2598 {
2599   xmlNodePtr self;
2600
2601   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
2602
2603   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
2604   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
2605   xmlNewChild (self, NULL, (xmlChar *) "parent",
2606       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
2607
2608   /* FIXME FIXME FIXME! */
2609
2610   return self;
2611 }
2612 #endif /* 0 */
2613 #endif /* GST_DISABLE_LOADSAVE */
2614
2615 /* 
2616  * should be called with pad lock held 
2617  *
2618  * MT safe.
2619  */
2620 static void
2621 handle_pad_block (GstPad * pad)
2622 {
2623   GstPadBlockCallback callback;
2624   gpointer user_data;
2625
2626   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2627       "signal block taken on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2628
2629   /* need to grab extra ref for the callbacks */
2630   gst_object_ref (pad);
2631
2632   callback = pad->block_callback;
2633   if (callback) {
2634     user_data = pad->block_data;
2635     GST_UNLOCK (pad);
2636     callback (pad, TRUE, user_data);
2637     GST_LOCK (pad);
2638   } else {
2639     GST_PAD_BLOCK_SIGNAL (pad);
2640   }
2641
2642   while (GST_PAD_IS_BLOCKED (pad))
2643     GST_PAD_BLOCK_WAIT (pad);
2644
2645   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
2646
2647   callback = pad->block_callback;
2648   if (callback) {
2649     user_data = pad->block_data;
2650     GST_UNLOCK (pad);
2651     callback (pad, FALSE, user_data);
2652     GST_LOCK (pad);
2653   } else {
2654     GST_PAD_BLOCK_SIGNAL (pad);
2655   }
2656
2657   gst_object_unref (pad);
2658 }
2659
2660 /**********************************************************************
2661  * Data passing functions
2662  */
2663
2664 /**
2665  * gst_pad_chain:
2666  * @pad: a sink #GstPad.
2667  * @buffer: the #GstBuffer to send.
2668  *
2669  * Chain a buffer to @pad.
2670  *
2671  * Returns: a #GstFlowReturn from the pad.
2672  *
2673  * MT safe.
2674  */
2675 GstFlowReturn
2676 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
2677 {
2678   GstCaps *caps;
2679   gboolean caps_changed;
2680   GstPadChainFunction chainfunc;
2681   GstFlowReturn ret;
2682
2683   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2684   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
2685       GST_FLOW_ERROR);
2686   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2687   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2688
2689   GST_STREAM_LOCK (pad);
2690
2691   GST_LOCK (pad);
2692   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2693     goto flushing;
2694
2695   caps = GST_BUFFER_CAPS (buffer);
2696   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2697   GST_UNLOCK (pad);
2698
2699   /* we got a new datatype on the pad, see if it can handle it */
2700   if (G_UNLIKELY (caps_changed)) {
2701     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
2702     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
2703       goto not_negotiated;
2704   }
2705
2706   /* NOTE: we read the chainfunc unlocked. 
2707    * we cannot hold the lock for the pad so we might send
2708    * the data to the wrong function. This is not really a
2709    * problem since functions are assigned at creation time
2710    * and don't change that often... */
2711   if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
2712     goto no_function;
2713
2714   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2715       "calling chainfunction &%s of pad %s:%s",
2716       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad));
2717
2718   ret = chainfunc (pad, buffer);
2719   GST_STREAM_UNLOCK (pad);
2720
2721   return ret;
2722
2723   /* ERRORS */
2724 flushing:
2725   {
2726     gst_buffer_unref (buffer);
2727     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2728         "pushing, but pad was flushing");
2729     GST_UNLOCK (pad);
2730     GST_STREAM_UNLOCK (pad);
2731     return GST_FLOW_UNEXPECTED;
2732   }
2733 not_negotiated:
2734   {
2735     gst_buffer_unref (buffer);
2736     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2737         "pushing buffer but pad did not accept");
2738     GST_STREAM_UNLOCK (pad);
2739     return GST_FLOW_NOT_NEGOTIATED;
2740   }
2741 no_function:
2742   {
2743     gst_buffer_unref (buffer);
2744     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2745         "pushing, but not chainhandler");
2746     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
2747         ("push on pad %s:%s but it has no chainfunction",
2748             GST_DEBUG_PAD_NAME (pad)));
2749     GST_STREAM_UNLOCK (pad);
2750     return GST_FLOW_ERROR;
2751   }
2752 }
2753
2754 /**
2755  * gst_pad_push:
2756  * @pad: a source #GstPad.
2757  * @buffer: the #GstBuffer to push.
2758  *
2759  * Pushes a buffer to the peer of @pad. @pad must be linked.
2760  *
2761  * Returns: a #GstFlowReturn from the peer pad.
2762  *
2763  * MT safe.
2764  */
2765 GstFlowReturn
2766 gst_pad_push (GstPad * pad, GstBuffer * buffer)
2767 {
2768   GstPad *peer;
2769   GstFlowReturn ret;
2770
2771   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2772   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
2773   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2774   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2775
2776   GST_LOCK (pad);
2777   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2778     handle_pad_block (pad);
2779
2780   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2781     goto not_linked;
2782
2783   gst_object_ref (peer);
2784   GST_UNLOCK (pad);
2785
2786   ret = gst_pad_chain (peer, buffer);
2787
2788   gst_object_unref (peer);
2789
2790   return ret;
2791
2792   /* ERROR recovery here */
2793 not_linked:
2794   {
2795     gst_buffer_unref (buffer);
2796     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2797         "pushing, but it was not linked");
2798     GST_UNLOCK (pad);
2799     return GST_FLOW_NOT_CONNECTED;
2800   }
2801 }
2802
2803 /**
2804  * gst_pad_check_pull_range:
2805  * @pad: a sink #GstPad.
2806  *
2807  * Checks if a #gst_pad_pull_range() can be performed on the peer
2808  * source pad. This function is used by plugins that want to check
2809  * if they can use random access on the peer source pad. 
2810  *
2811  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
2812  * if it needs to perform some logic to determine if pull_range is
2813  * possible.
2814  *
2815  * Returns: a gboolean with the result.
2816  *
2817  * MT safe.
2818  */
2819 gboolean
2820 gst_pad_check_pull_range (GstPad * pad)
2821 {
2822   GstPad *peer;
2823   gboolean ret;
2824   GstPadCheckGetRangeFunction checkgetrangefunc;
2825
2826   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2827
2828   GST_LOCK (pad);
2829   if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
2830     goto wrong_direction;
2831
2832   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2833     goto not_connected;
2834
2835   gst_object_ref (peer);
2836   GST_UNLOCK (pad);
2837
2838   /* see note in above function */
2839   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
2840     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
2841   } else {
2842     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2843         "calling checkgetrangefunc %s of peer pad %s:%s",
2844         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
2845
2846     ret = checkgetrangefunc (peer);
2847   }
2848
2849   gst_object_unref (peer);
2850
2851   return ret;
2852
2853   /* ERROR recovery here */
2854 wrong_direction:
2855   {
2856     GST_UNLOCK (pad);
2857     return FALSE;
2858   }
2859 not_connected:
2860   {
2861     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2862         "checking pull range, but it was not linked");
2863     GST_UNLOCK (pad);
2864     return FALSE;
2865   }
2866 }
2867
2868 /**
2869  * gst_pad_get_range:
2870  * @pad: a src #GstPad.
2871  * @buffer: a pointer to hold the #GstBuffer.
2872  * @offset: The start offset of the buffer
2873  * @length: The length of the buffer
2874  *
2875  * Calls the getrange function of @pad. 
2876  *
2877  * Returns: a #GstFlowReturn from the pad.
2878  *
2879  * MT safe.
2880  */
2881 GstFlowReturn
2882 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
2883     GstBuffer ** buffer)
2884 {
2885   GstFlowReturn ret;
2886   GstPadGetRangeFunction getrangefunc;
2887
2888   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2889   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
2890   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2891
2892   GST_STREAM_LOCK (pad);
2893
2894   GST_LOCK (pad);
2895   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2896     goto flushing;
2897   GST_UNLOCK (pad);
2898
2899   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
2900     goto no_function;
2901
2902   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2903       "calling getrangefunc %s of peer pad %s:%s, offset %"
2904       G_GUINT64_FORMAT ", size %u",
2905       GST_DEBUG_FUNCPTR_NAME (getrangefunc), GST_DEBUG_PAD_NAME (pad),
2906       offset, size);
2907
2908   ret = getrangefunc (pad, offset, size, buffer);
2909
2910   GST_STREAM_UNLOCK (pad);
2911
2912   return ret;
2913
2914   /* ERRORS */
2915 flushing:
2916   {
2917     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2918         "pulling range, but pad was flushing");
2919     GST_UNLOCK (pad);
2920     GST_STREAM_UNLOCK (pad);
2921     return GST_FLOW_UNEXPECTED;
2922   }
2923 no_function:
2924   {
2925     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
2926         ("pullrange on pad %s:%s but it has no getrangefunction",
2927             GST_DEBUG_PAD_NAME (pad)));
2928     GST_STREAM_UNLOCK (pad);
2929     return GST_FLOW_ERROR;
2930   }
2931 }
2932
2933
2934 /**
2935  * gst_pad_pull_range:
2936  * @pad: a sink #GstPad.
2937  * @buffer: a pointer to hold the #GstBuffer.
2938  * @offset: The start offset of the buffer
2939  * @length: The length of the buffer
2940  *
2941  * Pulls a buffer from the peer pad. @pad must be linked.
2942  *
2943  * Returns: a #GstFlowReturn from the peer pad.
2944  *
2945  * MT safe.
2946  */
2947 GstFlowReturn
2948 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
2949     GstBuffer ** buffer)
2950 {
2951   GstPad *peer;
2952   GstFlowReturn ret;
2953
2954   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2955   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
2956       GST_FLOW_ERROR);
2957   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2958
2959   GST_LOCK (pad);
2960
2961   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2962     handle_pad_block (pad);
2963
2964   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2965     goto not_connected;
2966
2967   gst_object_ref (peer);
2968   GST_UNLOCK (pad);
2969
2970   ret = gst_pad_get_range (peer, offset, size, buffer);
2971
2972   gst_object_unref (peer);
2973
2974   return ret;
2975
2976   /* ERROR recovery here */
2977 not_connected:
2978   {
2979     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2980         "pulling range, but it was not linked");
2981     GST_UNLOCK (pad);
2982     return GST_FLOW_NOT_CONNECTED;
2983   }
2984 }
2985
2986 /**
2987  * gst_pad_push_event:
2988  * @pad: a #GstPad to push the event to.
2989  * @event: the #GstEvent to send to the pad.
2990  *
2991  * Sends the event to the peer of the given pad. This function is
2992  * mainly used by elements to send events to their peer
2993  * elements.
2994  *
2995  * Returns: TRUE if the event was handled.
2996  *
2997  * MT safe.
2998  */
2999 gboolean
3000 gst_pad_push_event (GstPad * pad, GstEvent * event)
3001 {
3002   GstPad *peerpad;
3003   gboolean result;
3004
3005   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3006   g_return_val_if_fail (event != NULL, FALSE);
3007
3008   GST_LOCK (pad);
3009   peerpad = GST_PAD_PEER (pad);
3010   if (peerpad == NULL)
3011     goto not_linked;
3012
3013   gst_object_ref (peerpad);
3014   GST_UNLOCK (pad);
3015
3016   result = gst_pad_send_event (peerpad, event);
3017
3018   gst_object_unref (peerpad);
3019
3020   return result;
3021
3022   /* ERROR handling */
3023 not_linked:
3024   {
3025     gst_event_unref (event);
3026     GST_UNLOCK (pad);
3027     return FALSE;
3028   }
3029 }
3030
3031 /**
3032  * gst_pad_send_event:
3033  * @pad: a #GstPad to send the event to.
3034  * @event: the #GstEvent to send to the pad.
3035  *
3036  * Sends the event to the pad. This function can be used
3037  * by applications to send events in the pipeline.
3038  *
3039  * Returns: TRUE if the event was handled.
3040  */
3041 gboolean
3042 gst_pad_send_event (GstPad * pad, GstEvent * event)
3043 {
3044   gboolean result = FALSE;
3045   GstPadEventFunction eventfunc;
3046
3047   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3048   g_return_val_if_fail (event != NULL, FALSE);
3049
3050   GST_LOCK (pad);
3051
3052   if (GST_EVENT_SRC (event) == NULL)
3053     GST_EVENT_SRC (event) = gst_object_ref (pad);
3054
3055   switch (GST_EVENT_TYPE (event)) {
3056     case GST_EVENT_FLUSH:
3057       GST_CAT_DEBUG (GST_CAT_EVENT, "have event type %d (FLUSH) on pad %s:%s",
3058           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3059
3060       if (GST_EVENT_FLUSH_DONE (event)) {
3061         GST_PAD_UNSET_FLUSHING (pad);
3062         GST_CAT_DEBUG (GST_CAT_EVENT, "cleared flush flag");
3063       } else {
3064         /* can't even accept a flush begin event when flushing */
3065         if (GST_PAD_IS_FLUSHING (pad))
3066           goto flushing;
3067         GST_PAD_SET_FLUSHING (pad);
3068         GST_CAT_DEBUG (GST_CAT_EVENT, "set flush flag");
3069       }
3070       break;
3071     default:
3072       GST_CAT_DEBUG (GST_CAT_EVENT, "have event type %d on pad %s:%s",
3073           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3074
3075       if (GST_PAD_IS_FLUSHING (pad))
3076         goto flushing;
3077       break;
3078   }
3079
3080   if ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL)
3081     goto no_function;
3082
3083   gst_object_ref (pad);
3084   GST_UNLOCK (pad);
3085
3086   result = eventfunc (GST_PAD_CAST (pad), event);
3087
3088   gst_object_unref (pad);
3089
3090   return result;
3091
3092   /* ERROR handling */
3093 no_function:
3094   {
3095     g_warning ("pad %s:%s has no event handler, file a bug.",
3096         GST_DEBUG_PAD_NAME (pad));
3097     GST_UNLOCK (pad);
3098     gst_event_unref (event);
3099     return FALSE;
3100   }
3101 flushing:
3102   {
3103     GST_UNLOCK (pad);
3104     GST_CAT_DEBUG (GST_CAT_EVENT, "Received event on flushing pad. Discarding");
3105     gst_event_unref (event);
3106     return FALSE;
3107   }
3108 }
3109
3110 /************************************************************************
3111  *
3112  * templates
3113  *
3114  */
3115 static void gst_pad_template_class_init (GstPadTemplateClass * klass);
3116 static void gst_pad_template_init (GstPadTemplate * templ);
3117 static void gst_pad_template_dispose (GObject * object);
3118
3119 GType
3120 gst_pad_template_get_type (void)
3121 {
3122   static GType padtemplate_type = 0;
3123
3124   if (!padtemplate_type) {
3125     static const GTypeInfo padtemplate_info = {
3126       sizeof (GstPadTemplateClass), NULL, NULL,
3127       (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
3128       sizeof (GstPadTemplate),
3129       0,
3130       (GInstanceInitFunc) gst_pad_template_init, NULL
3131     };
3132
3133     padtemplate_type =
3134         g_type_register_static (GST_TYPE_OBJECT, "GstPadTemplate",
3135         &padtemplate_info, 0);
3136   }
3137   return padtemplate_type;
3138 }
3139
3140 static void
3141 gst_pad_template_class_init (GstPadTemplateClass * klass)
3142 {
3143   GObjectClass *gobject_class;
3144   GstObjectClass *gstobject_class;
3145
3146   gobject_class = (GObjectClass *) klass;
3147   gstobject_class = (GstObjectClass *) klass;
3148
3149   padtemplate_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
3150
3151   gst_pad_template_signals[TEMPL_PAD_CREATED] =
3152       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
3153       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
3154       NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
3155
3156   gobject_class->dispose = gst_pad_template_dispose;
3157
3158   gstobject_class->path_string_separator = "*";
3159 }
3160
3161 static void
3162 gst_pad_template_init (GstPadTemplate * templ)
3163 {
3164 }
3165
3166 static void
3167 gst_pad_template_dispose (GObject * object)
3168 {
3169   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
3170
3171   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
3172   if (GST_PAD_TEMPLATE_CAPS (templ)) {
3173     gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
3174   }
3175
3176   G_OBJECT_CLASS (padtemplate_parent_class)->dispose (object);
3177 }
3178
3179 /* ALWAYS padtemplates cannot have conversion specifications, it doesn't make
3180  * sense.
3181  * SOMETIMES padtemplates can do whatever they want, they are provided by the
3182  * element.
3183  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
3184  * 'sink%d' template is automatically selected), so we need to restrict their
3185  * naming.
3186  */
3187 static gboolean
3188 name_is_valid (const gchar * name, GstPadPresence presence)
3189 {
3190   const gchar *str;
3191
3192   if (presence == GST_PAD_ALWAYS) {
3193     if (strchr (name, '%')) {
3194       g_warning ("invalid name template %s: conversion specifications are not"
3195           " allowed for GST_PAD_ALWAYS padtemplates", name);
3196       return FALSE;
3197     }
3198   } else if (presence == GST_PAD_REQUEST) {
3199     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
3200       g_warning ("invalid name template %s: only one conversion specification"
3201           " allowed in GST_PAD_REQUEST padtemplate", name);
3202       return FALSE;
3203     }
3204     if (str && (*(str + 1) != 's' && *(str + 1) != 'd')) {
3205       g_warning ("invalid name template %s: conversion specification must be of"
3206           " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
3207       return FALSE;
3208     }
3209     if (str && (*(str + 2) != '\0')) {
3210       g_warning ("invalid name template %s: conversion specification must"
3211           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
3212       return FALSE;
3213     }
3214   }
3215
3216   return TRUE;
3217 }
3218
3219 /**
3220  * gst_static_pad_template_get:
3221  * @pad_template: the static pad template
3222  *
3223  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
3224  *
3225  * Returns: a new #GstPadTemplate.
3226  */
3227 GstPadTemplate *
3228 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
3229 {
3230   GstPadTemplate *new;
3231
3232   if (!name_is_valid (pad_template->name_template, pad_template->presence))
3233     return NULL;
3234
3235   new = g_object_new (gst_pad_template_get_type (),
3236       "name", pad_template->name_template, NULL);
3237
3238   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (pad_template->name_template);
3239   GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
3240   GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
3241
3242   GST_PAD_TEMPLATE_CAPS (new) =
3243       gst_caps_copy (gst_static_caps_get (&pad_template->static_caps));
3244
3245   return new;
3246 }
3247
3248 /**
3249  * gst_pad_template_new:
3250  * @name_template: the name template.
3251  * @direction: the #GstPadDirection of the template.
3252  * @presence: the #GstPadPresence of the pad.
3253  * @caps: a #GstCaps set for the template. The caps are taken ownership of.
3254  *
3255  * Creates a new pad template with a name according to the given template
3256  * and with the given arguments. This functions takes ownership of the provided
3257  * caps, so be sure to not use them afterwards.
3258  *
3259  * Returns: a new #GstPadTemplate.
3260  */
3261 GstPadTemplate *
3262 gst_pad_template_new (const gchar * name_template,
3263     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
3264 {
3265   GstPadTemplate *new;
3266
3267   g_return_val_if_fail (name_template != NULL, NULL);
3268   g_return_val_if_fail (caps != NULL, NULL);
3269   g_return_val_if_fail (direction == GST_PAD_SRC
3270       || direction == GST_PAD_SINK, NULL);
3271   g_return_val_if_fail (presence == GST_PAD_ALWAYS
3272       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
3273
3274   if (!name_is_valid (name_template, presence))
3275     return NULL;
3276
3277   new = g_object_new (gst_pad_template_get_type (),
3278       "name", name_template, NULL);
3279
3280   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
3281   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
3282   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
3283   GST_PAD_TEMPLATE_CAPS (new) = caps;
3284
3285   return new;
3286 }
3287
3288 /**
3289  * gst_static_pad_template_get_caps:
3290  * @templ: a #GstStaticPadTemplate to get capabilities of.
3291  *
3292  * Gets the capabilities of the static pad template.
3293  *
3294  * Returns: the #GstCaps of the static pad template. If you need to keep a 
3295  * reference to the caps, take a ref (see gst_caps_ref ()).
3296  */
3297 GstCaps *
3298 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
3299 {
3300   g_return_val_if_fail (templ, NULL);
3301
3302   return (GstCaps *) gst_static_caps_get (&templ->static_caps);
3303 }
3304
3305 /**
3306  * gst_pad_template_get_caps:
3307  * @templ: a #GstPadTemplate to get capabilities of.
3308  *
3309  * Gets the capabilities of the pad template.
3310  *
3311  * Returns: the #GstCaps of the pad template. If you need to keep a reference to
3312  * the caps, take a ref (see gst_caps_ref ()).
3313  */
3314 GstCaps *
3315 gst_pad_template_get_caps (GstPadTemplate * templ)
3316 {
3317   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
3318
3319   return GST_PAD_TEMPLATE_CAPS (templ);
3320 }
3321
3322 /**
3323  * gst_pad_set_element_private:
3324  * @pad: the #GstPad to set the private data of.
3325  * @priv: The private data to attach to the pad.
3326  *
3327  * Set the given private data gpointer on the pad. 
3328  * This function can only be used by the element that owns the pad.
3329  */
3330 void
3331 gst_pad_set_element_private (GstPad * pad, gpointer priv)
3332 {
3333   pad->element_private = priv;
3334 }
3335
3336 /**
3337  * gst_pad_get_element_private:
3338  * @pad: the #GstPad to get the private data of.
3339  *
3340  * Gets the private data of a pad.
3341  *
3342  * Returns: a #gpointer to the private data.
3343  */
3344 gpointer
3345 gst_pad_get_element_private (GstPad * pad)
3346 {
3347   return pad->element_private;
3348 }
3349
3350 /**
3351  * gst_pad_start_task:
3352  * @pad: the #GstPad to start the task of
3353  * @func: the task function to call
3354  * @data: data passed to the task function
3355  *
3356  * Starts a task that repeadedly calls @func with @data. This function
3357  * is nostly used in the pad activation function to start the
3358  * dataflow. This function will automatically acauire the STREAM_LOCK of
3359  * the pad before calling @func.
3360  *
3361  * Returns: a TRUE if the task could be started. FALSE when the pad has
3362  * no parent or the parent has no scheduler.
3363  */
3364 gboolean
3365 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
3366 {
3367   GstElement *parent;
3368   GstScheduler *sched;
3369   GstTask *task;
3370
3371   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3372   g_return_val_if_fail (func != NULL, FALSE);
3373
3374   GST_LOCK (pad);
3375   parent = GST_PAD_PARENT (pad);
3376
3377   if (parent == NULL || !GST_IS_ELEMENT (parent))
3378     goto no_parent;
3379
3380   sched = GST_ELEMENT_SCHEDULER (parent);
3381   if (sched == NULL)
3382     goto no_sched;
3383
3384   task = GST_PAD_TASK (pad);
3385   if (task == NULL) {
3386     task = gst_scheduler_create_task (sched, func, data);
3387     gst_task_set_lock (task, GST_STREAM_GET_LOCK (pad));
3388
3389     GST_PAD_TASK (pad) = task;
3390   }
3391   GST_UNLOCK (pad);
3392
3393   gst_task_start (task);
3394
3395   return TRUE;
3396
3397   /* ERRORS */
3398 no_parent:
3399   {
3400     GST_UNLOCK (pad);
3401     GST_DEBUG ("no parent");
3402     return FALSE;
3403   }
3404 no_sched:
3405   {
3406     GST_UNLOCK (pad);
3407     GST_DEBUG ("no scheduler");
3408     return FALSE;
3409   }
3410 }
3411
3412 /**
3413  * gst_pad_pause_task:
3414  * @pad: the #GstPad to pause the task of
3415  *
3416  * Pause the task of @pad. This function will also make sure that the 
3417  * function executed by the task will effectively stop.
3418  *
3419  * Returns: a TRUE if the task could be paused or FALSE when the pad
3420  * has no task.
3421  */
3422 gboolean
3423 gst_pad_pause_task (GstPad * pad)
3424 {
3425   GstTask *task;
3426
3427   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3428
3429   GST_LOCK (pad);
3430   task = GST_PAD_TASK (pad);
3431   if (task == NULL)
3432     goto no_task;
3433   gst_task_pause (task);
3434   GST_UNLOCK (pad);
3435
3436   GST_STREAM_LOCK (pad);
3437   GST_STREAM_UNLOCK (pad);
3438
3439   return TRUE;
3440
3441 no_task:
3442   {
3443     GST_UNLOCK (pad);
3444     return TRUE;
3445   }
3446 }
3447
3448 /**
3449  * gst_pad_stop_task:
3450  * @pad: the #GstPad to stop the task of
3451  *
3452  * Stop the task of @pad. This function will also make sure that the 
3453  * function executed by the task will effectively stop.
3454  *
3455  * Returns: a TRUE if the task could be stopped or FALSE when the pad
3456  * has no task.
3457  */
3458 gboolean
3459 gst_pad_stop_task (GstPad * pad)
3460 {
3461   GstTask *task;
3462
3463   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3464
3465   GST_LOCK (pad);
3466   task = GST_PAD_TASK (pad);
3467   if (task == NULL)
3468     goto no_task;
3469   GST_PAD_TASK (pad) = NULL;
3470   GST_UNLOCK (pad);
3471
3472   gst_task_stop (task);
3473
3474   GST_STREAM_LOCK (pad);
3475   GST_STREAM_UNLOCK (pad);
3476
3477   gst_object_unref (task);
3478
3479   return TRUE;
3480
3481 no_task:
3482   {
3483     GST_UNLOCK (pad);
3484     return TRUE;
3485   }
3486 }