fix doc build fix autogen
[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 "gstutils.h"
27 #include "gstelement.h"
28 #include "gstbin.h"
29 #include "gstscheduler.h"
30 #include "gstevent.h"
31 #include "gstinfo.h"
32 #include "gsterror.h"
33 #include "gstvalue.h"
34
35 #define GST_CAT_DEFAULT GST_CAT_PADS
36
37 /* FIXME */
38 #define gst_caps_debug(a,b) GST_DEBUG_CAPS(b,a)
39
40
41 enum {
42   TEMPL_PAD_CREATED,
43   /* FILL ME */
44   TEMPL_LAST_SIGNAL
45 };
46
47 static GstObject *padtemplate_parent_class = NULL;
48 static guint gst_pad_template_signals[TEMPL_LAST_SIGNAL] = { 0 };
49
50 GType _gst_pad_type = 0;
51
52 /***** Start with the base GstPad class *****/
53 static void             gst_pad_class_init              (GstPadClass *klass);
54 static void             gst_pad_init                    (GstPad *pad);
55 static void             gst_pad_dispose                 (GObject *object);
56
57 static void             gst_pad_set_pad_template        (GstPad *pad, GstPadTemplate *templ);
58 static GstCaps *        _gst_pad_default_fixate_func    (GstPad *pad, const GstCaps *caps);
59
60 static gboolean         gst_pad_link_try                (GstPadLink *link);
61 static void             gst_pad_link_free               (GstPadLink *link);
62
63 #ifndef GST_DISABLE_LOADSAVE
64 static xmlNodePtr       gst_pad_save_thyself            (GstObject *object, xmlNodePtr parent);
65 #endif
66
67 static GstObject *pad_parent_class = NULL;
68
69 GType
70 gst_pad_get_type (void) 
71 {
72   if (!_gst_pad_type) {
73     static const GTypeInfo pad_info = {
74       sizeof (GstPadClass), NULL, NULL,
75       (GClassInitFunc) gst_pad_class_init, NULL, NULL,
76       sizeof (GstPad), 
77       32,
78       (GInstanceInitFunc) gst_pad_init, NULL
79     };
80     _gst_pad_type = g_type_register_static (GST_TYPE_OBJECT, "GstPad", 
81                                             &pad_info, 0);
82   }
83   return _gst_pad_type;
84 }
85
86 static void
87 gst_pad_class_init (GstPadClass *klass)
88 {
89   GObjectClass *gobject_class;
90
91   gobject_class = (GObjectClass*) klass;
92
93   pad_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
94
95   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
96 }
97
98 static void
99 gst_pad_init (GstPad *pad)
100 {
101   /* all structs are initialized to NULL by glib */
102 }
103 static void
104 gst_pad_dispose (GObject *object)
105 {
106   GstPad *pad = GST_PAD (object);
107
108   gst_pad_set_pad_template (pad, NULL);
109
110   G_OBJECT_CLASS (pad_parent_class)->dispose (object);
111 }
112
113
114
115 /***** Then do the Real Pad *****/
116 /* Pad signals and args */
117 enum {
118   REAL_CAPS_NEGO_FAILED,
119   REAL_LINKED,
120   REAL_UNLINKED,
121   REAL_FIXATE,
122   /* FILL ME */
123   REAL_LAST_SIGNAL
124 };
125
126 enum {
127   REAL_ARG_0,
128   REAL_ARG_CAPS,
129   REAL_ARG_ACTIVE,
130   /* FILL ME */
131 };
132
133 static void     gst_real_pad_class_init         (GstRealPadClass *klass);
134 static void     gst_real_pad_init               (GstRealPad *pad);
135 static void     gst_real_pad_dispose            (GObject *object);
136
137 static gboolean _gst_real_pad_fixate_accumulator (GSignalInvocationHint *ihint,
138     GValue *return_accu, const GValue *handler_return, gpointer dummy);
139 static void     gst_real_pad_set_property       (GObject *object, guint prop_id,
140                                                  const GValue *value, 
141                                                  GParamSpec *pspec);
142 static void     gst_real_pad_get_property       (GObject *object, guint prop_id,
143                                                  GValue *value, 
144                                                  GParamSpec *pspec);
145
146 GType _gst_real_pad_type = 0;
147
148 static GstPad *real_pad_parent_class = NULL;
149 static guint gst_real_pad_signals[REAL_LAST_SIGNAL] = { 0 };
150
151 GType
152 gst_real_pad_get_type (void) {
153   if (!_gst_real_pad_type) {
154     static const GTypeInfo pad_info = {
155       sizeof (GstRealPadClass), NULL, NULL,
156       (GClassInitFunc) gst_real_pad_class_init, NULL, NULL,
157       sizeof (GstRealPad),
158       32,
159       (GInstanceInitFunc) gst_real_pad_init, NULL
160     };
161     _gst_real_pad_type = g_type_register_static (GST_TYPE_PAD, "GstRealPad", 
162                                                  &pad_info, 0);
163   }
164   return _gst_real_pad_type;
165 }
166
167 static void
168 gst_real_pad_class_init (GstRealPadClass *klass)
169 {
170   GObjectClass *gobject_class;
171   GstObjectClass *gstobject_class;
172
173   gobject_class = (GObjectClass*) klass;
174   gstobject_class = (GstObjectClass*) klass;
175
176   real_pad_parent_class = g_type_class_ref (GST_TYPE_PAD);
177
178   gobject_class->dispose  = GST_DEBUG_FUNCPTR (gst_real_pad_dispose);
179   gobject_class->set_property  = GST_DEBUG_FUNCPTR (gst_real_pad_set_property);
180   gobject_class->get_property  = GST_DEBUG_FUNCPTR (gst_real_pad_get_property);
181
182   gst_real_pad_signals[REAL_CAPS_NEGO_FAILED] =
183     g_signal_new ("caps_nego_failed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
184                   G_STRUCT_OFFSET (GstRealPadClass, caps_nego_failed), NULL, NULL,
185                   gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
186                   G_TYPE_POINTER);
187   gst_real_pad_signals[REAL_LINKED] =
188     g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
189                   G_STRUCT_OFFSET (GstRealPadClass, linked), NULL, NULL,
190                   gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
191                   GST_TYPE_PAD);
192   gst_real_pad_signals[REAL_UNLINKED] =
193     g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
194                   G_STRUCT_OFFSET (GstRealPadClass, unlinked), NULL, NULL,
195                   gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
196                   GST_TYPE_PAD);
197   gst_real_pad_signals[REAL_FIXATE] =
198     g_signal_new ("fixate", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
199                   G_STRUCT_OFFSET (GstRealPadClass, appfixatefunc), 
200                   _gst_real_pad_fixate_accumulator, NULL,
201                   gst_marshal_POINTER__POINTER, G_TYPE_POINTER, 1,
202                   G_TYPE_POINTER);
203
204 /*  gtk_object_add_arg_type ("GstRealPad::active", G_TYPE_BOOLEAN, */
205 /*                           GTK_ARG_READWRITE, REAL_ARG_ACTIVE); */
206   g_object_class_install_property (G_OBJECT_CLASS (klass), REAL_ARG_ACTIVE,
207     g_param_spec_boolean ("active", "Active", "Whether the pad is active.",
208                           TRUE, G_PARAM_READWRITE));
209   g_object_class_install_property (G_OBJECT_CLASS (klass), REAL_ARG_CAPS,
210     g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
211                         GST_TYPE_CAPS, G_PARAM_READABLE));
212
213 #ifndef GST_DISABLE_LOADSAVE
214   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
215 #endif
216   gstobject_class->path_string_separator = ".";
217 }
218
219 static gboolean
220 _gst_real_pad_fixate_accumulator (GSignalInvocationHint *ihint,
221     GValue *return_accu, const GValue *handler_return, gpointer dummy)
222 {
223   if (g_value_get_pointer (handler_return)) {
224     g_value_copy (handler_return, return_accu);
225     /* stop emission if something was returned */
226     return FALSE;
227   }
228   return TRUE;
229 }
230
231 static void
232 gst_real_pad_init (GstRealPad *pad)
233 {
234   pad->direction = GST_PAD_UNKNOWN;
235   pad->peer = NULL;
236
237   pad->chainfunc = NULL;
238   pad->getfunc = NULL;
239
240   pad->chainhandler = NULL;
241   pad->gethandler = NULL;
242
243   pad->ghostpads = NULL;
244   pad->caps = NULL;
245
246   pad->linkfunc = NULL;
247   pad->getcapsfunc = NULL;
248
249   pad->eventfunc        = gst_pad_event_default;
250   pad->convertfunc      = gst_pad_convert_default;
251   pad->queryfunc        = gst_pad_query_default;
252   pad->intlinkfunc      = gst_pad_get_internal_links_default;
253
254   pad->eventmaskfunc    = gst_pad_get_event_masks_default;
255   pad->formatsfunc      = gst_pad_get_formats_default;
256   pad->querytypefunc    = gst_pad_get_query_types_default;
257
258   GST_FLAG_SET (pad, GST_PAD_DISABLED);
259   GST_FLAG_UNSET (pad, GST_PAD_NEGOTIATING);
260   
261   gst_probe_dispatcher_init (&pad->probedisp);
262 }
263
264 static void
265 gst_real_pad_set_property (GObject *object, guint prop_id, 
266                            const GValue *value, GParamSpec *pspec)
267 {
268   g_return_if_fail (GST_IS_PAD (object));
269
270   switch (prop_id) {
271     case REAL_ARG_ACTIVE:
272       gst_pad_set_active (GST_PAD (object), g_value_get_boolean (value));
273       break;
274     default:
275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276       break;
277   }
278 }
279
280 static void
281 gst_real_pad_get_property (GObject *object, guint prop_id, 
282                            GValue *value, GParamSpec *pspec)
283 {
284   g_return_if_fail (GST_IS_PAD (object));
285
286   switch (prop_id) {
287     case REAL_ARG_ACTIVE:
288       g_value_set_boolean (value, !GST_FLAG_IS_SET (object, GST_PAD_DISABLED));
289       break;
290     case REAL_ARG_CAPS:
291       g_value_set_boxed (value, GST_PAD_CAPS (GST_REAL_PAD (object)));
292       break;
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296   }
297 }
298
299
300 /**
301  * gst_pad_custom_new:
302  * @type: the #Gtype of the pad.
303  * @name: the name of the new pad.
304  * @direction: the #GstPadDirection of the pad.
305  *
306  * Creates a new pad with the given name and type in the given direction.
307  * If name is NULL, a guaranteed unique name (across all pads) 
308  * will be assigned.
309  *
310  * Returns: a new #GstPad, or NULL in case of an error.
311  */
312 GstPad*
313 gst_pad_custom_new (GType type, const gchar *name,
314                     GstPadDirection direction)
315 {
316   GstRealPad *pad;
317
318   g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
319
320   pad = g_object_new (type, NULL);
321   gst_object_set_name (GST_OBJECT (pad), name);
322   GST_RPAD_DIRECTION (pad) = direction;
323
324   return GST_PAD (pad);
325 }
326
327 /**
328  * gst_pad_new:
329  * @name: the name of the new pad.
330  * @direction: the #GstPadDirection of the pad.
331  *
332  * Creates a new real pad with the given name in the given direction.
333  * If name is NULL, a guaranteed unique name (across all pads) 
334  * will be assigned.
335  *
336  * Returns: a new #GstPad, or NULL in case of an error.
337  */
338 GstPad*
339 gst_pad_new (const gchar *name,
340              GstPadDirection direction)
341 {
342   return gst_pad_custom_new (gst_real_pad_get_type (), name, direction);
343 }
344
345 /**
346  * gst_pad_custom_new_from_template:
347  * @type: the custom #GType of the pad.
348  * @templ: the #GstPadTemplate to instantiate from.
349  * @name: the name of the new pad.
350  *
351  * Creates a new custom pad with the given name from the given template.
352  * If name is NULL, a guaranteed unique name (across all pads) 
353  * will be assigned.
354  *
355  * Returns: a new #GstPad, or NULL in case of an error.
356  */
357 GstPad*
358 gst_pad_custom_new_from_template (GType type, GstPadTemplate *templ,
359                                   const gchar *name)
360 {
361   GstPad *pad;
362
363   g_return_val_if_fail (templ != NULL, NULL);
364
365   pad = gst_pad_new (name, templ->direction);
366   gst_pad_set_pad_template (pad, templ);
367
368   return pad;
369 }
370
371 /**
372  * gst_pad_new_from_template:
373  * @templ: the pad template to use
374  * @name: the name of the element
375  *
376  * Creates a new real pad with the given name from the given template.
377  * If name is NULL, a guaranteed unique name (across all pads) 
378  * will be assigned.
379  *
380  * Returns: a new #GstPad, or NULL in case of an error.
381  */
382 GstPad*
383 gst_pad_new_from_template (GstPadTemplate *templ, const gchar *name)
384 {
385   return gst_pad_custom_new_from_template (gst_real_pad_get_type (), 
386                                            templ, name);
387 }
388
389 /**
390  * gst_pad_get_direction:
391  * @pad: a #GstPad to get the direction of.
392  *
393  * Gets the direction of the pad.
394  *
395  * Returns: the #GstPadDirection of the pad.
396  */
397 GstPadDirection
398 gst_pad_get_direction (GstPad *pad)
399 {
400   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
401
402   return GST_PAD_DIRECTION (pad);
403 }
404
405 /**
406  * gst_pad_set_active:
407  * @pad: the #GstPad to activate or deactivate.
408  * @active: TRUE to activate the pad.
409  *
410  * Activates or deactivates the given pad.
411  */
412 void
413 gst_pad_set_active (GstPad *pad, gboolean active)
414 {
415   GstRealPad *realpad;
416   gboolean old;
417
418   g_return_if_fail (GST_IS_PAD (pad));
419
420   old = GST_PAD_IS_ACTIVE (pad);
421
422   if (old == active)
423     return;
424
425   realpad = GST_PAD_REALIZE (pad);
426
427   if (active) {
428     GST_CAT_DEBUG (GST_CAT_PADS, "activating pad %s:%s", 
429                GST_DEBUG_PAD_NAME (realpad));
430     GST_FLAG_UNSET (realpad, GST_PAD_DISABLED);
431   } else {
432     GST_CAT_DEBUG (GST_CAT_PADS, "de-activating pad %s:%s", 
433                GST_DEBUG_PAD_NAME (realpad));
434     GST_FLAG_SET (realpad, GST_PAD_DISABLED);
435   }
436   
437   g_object_notify (G_OBJECT (realpad), "active");
438 }
439
440 /**
441  * gst_pad_is_active:
442  * @pad: the #GstPad to query
443  *
444  * Query if a pad is active
445  *
446  * Returns: TRUE if the pad is active.
447  */
448 gboolean
449 gst_pad_is_active (GstPad *pad)
450 {
451   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
452
453   return !GST_FLAG_IS_SET (pad, GST_PAD_DISABLED);
454 }
455
456 /**
457  * gst_pad_set_name:
458  * @pad: a #GstPad to set the name of.
459  * @name: the name of the pad.
460  *
461  * Sets the name of a pad.  If name is NULL, then a guaranteed unique
462  * name will be assigned.
463  */
464 void
465 gst_pad_set_name (GstPad *pad, const gchar *name)
466 {
467   g_return_if_fail (GST_IS_PAD (pad));
468
469   gst_object_set_name (GST_OBJECT (pad), name);
470 }
471
472 /**
473  * gst_pad_get_name:
474  * @pad: a #GstPad to get the name of.
475  *
476  * Gets the name of a pad.
477  *
478  * Returns: the name of the pad.  This is not a newly allocated pointer
479  * so you must not free it.
480  */
481 const gchar*
482 gst_pad_get_name (GstPad *pad)
483 {
484   g_return_val_if_fail (pad != NULL, NULL);
485   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
486
487   return GST_OBJECT_NAME (pad);
488 }
489
490 /**
491  * gst_pad_set_chain_function:
492  * @pad: a #GstPad to set the chain function for.
493  * @chain: the #GstPadChainFunction to set.
494  *
495  * Sets the given chain function for the pad.
496  */
497 void 
498 gst_pad_set_chain_function (GstPad *pad, GstPadChainFunction chain)
499 {
500   g_return_if_fail (pad != NULL);
501   g_return_if_fail (GST_IS_REAL_PAD (pad));
502   g_return_if_fail (GST_RPAD_DIRECTION (pad) == GST_PAD_SINK);
503
504   GST_RPAD_CHAINFUNC (pad) = chain;
505   GST_CAT_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
506              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
507 }
508
509 /**
510  * gst_pad_set_get_function:
511  * @pad: a #GstPad to set the get function for.
512  * @get: the #GstPadGetFunction to set.
513  *
514  * Sets the given get function for the pad.
515  */
516 void
517 gst_pad_set_get_function (GstPad *pad,
518                           GstPadGetFunction get)
519 {
520   g_return_if_fail (pad != NULL);
521   g_return_if_fail (GST_IS_REAL_PAD (pad));
522   g_return_if_fail (GST_RPAD_DIRECTION (pad) == GST_PAD_SRC);
523
524   GST_RPAD_GETFUNC (pad) = get;
525   
526   GST_CAT_DEBUG (GST_CAT_PADS, "getfunc for %s:%s  set to %s",
527              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
528 }
529
530 /**
531  * gst_pad_set_event_function:
532  * @pad: a #GstPad to set the event handler for.
533  * @event: the #GstPadEventFunction to set.
534  *
535  * Sets the given event handler for the pad.
536  */
537 void
538 gst_pad_set_event_function (GstPad *pad,
539                             GstPadEventFunction event)
540 {
541   g_return_if_fail (GST_IS_REAL_PAD (pad));
542   g_return_if_fail (GST_RPAD_DIRECTION (pad) == GST_PAD_SRC);
543
544   GST_RPAD_EVENTFUNC (pad) = event;
545
546   GST_CAT_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
547              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
548 }
549
550 /**
551  * gst_pad_set_event_mask_function:
552  * @pad: a #GstPad to set the event mask function for.
553  * @mask_func: the #GstPadEventMaskFunction to set.
554  *
555  * Sets the given event mask function for the pad.
556  */
557 void
558 gst_pad_set_event_mask_function (GstPad *pad, 
559                                  GstPadEventMaskFunction mask_func)
560 {
561   g_return_if_fail (GST_IS_REAL_PAD (pad));
562
563   GST_RPAD_EVENTMASKFUNC (pad) = mask_func;
564
565   GST_CAT_DEBUG (GST_CAT_PADS, "eventmaskfunc for %s:%s  set to %s",
566              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (mask_func));
567 }
568
569 /**
570  * gst_pad_get_event_masks:
571  * @pad: a #GstPad to get the event mask for.
572  *
573  * Gets the array of eventmasks from the given pad.
574  *
575  * Returns: an array with eventmasks, the list is ended 
576  * with 0
577  */
578 const GstEventMask*
579 gst_pad_get_event_masks (GstPad *pad)
580 {
581   GstRealPad *rpad;
582   
583   if (pad == NULL)
584     return FALSE;
585
586   rpad = GST_PAD_REALIZE (pad);
587
588   g_return_val_if_fail (rpad, FALSE);
589
590   if (GST_RPAD_EVENTMASKFUNC (rpad))
591     return GST_RPAD_EVENTMASKFUNC (rpad) (GST_PAD (pad));
592
593   return NULL;
594 }
595
596 static gboolean
597 gst_pad_get_event_masks_dispatcher (GstPad *pad, const GstEventMask **data)
598 {
599   *data = gst_pad_get_event_masks (pad);
600
601   return TRUE;
602 }
603
604 /**
605  * gst_pad_get_event_masks_default:
606  * @pad: a #GstPad to get the event mask for.
607  *
608  * Invokes the default event masks dispatcher on the pad.
609  *
610  * Returns: an array with eventmasks, the list is ended 
611  * with 0
612  */
613 const GstEventMask* 
614 gst_pad_get_event_masks_default (GstPad *pad)
615 {
616   GstEventMask *result = NULL;
617
618   gst_pad_dispatcher (pad, (GstPadDispatcherFunction) 
619                            gst_pad_get_event_masks_dispatcher, &result);
620
621   return result;
622 }
623
624 /**
625  * gst_pad_set_convert_function:
626  * @pad: a #GstPad to set the convert function for.
627  * @convert: the #GstPadConvertFunction to set.
628  *
629  * Sets the given convert function for the pad.
630  */
631 void
632 gst_pad_set_convert_function (GstPad *pad,
633                               GstPadConvertFunction convert)
634 {
635   g_return_if_fail (pad != NULL);
636   g_return_if_fail (GST_IS_REAL_PAD (pad));
637
638   GST_RPAD_CONVERTFUNC (pad) = convert;
639
640   GST_CAT_DEBUG (GST_CAT_PADS, "convertfunc for %s:%s  set to %s",
641              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (convert));
642 }
643
644 /**
645  * gst_pad_set_query_function:
646  * @pad: the #GstPad to set the query function for.
647  * @query: the #GstPadQueryFunction to set.
648  *
649  * Set the given query function for the pad.
650  */
651 void
652 gst_pad_set_query_function (GstPad *pad, GstPadQueryFunction query)
653 {
654   g_return_if_fail (pad != NULL);
655   g_return_if_fail (GST_IS_REAL_PAD (pad));
656
657   GST_RPAD_QUERYFUNC (pad) = query;
658
659   GST_CAT_DEBUG (GST_CAT_PADS, "queryfunc for %s:%s  set to %s",
660              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (query));
661 }
662
663 /**
664  * gst_pad_set_query_type_function:
665  * @pad: the #GstPad to set the query type function for.
666  * @type_func: the #GstPadQueryTypeFunction to set.
667  *
668  * Set the given query type function for the pad.
669  */
670 void
671 gst_pad_set_query_type_function (GstPad *pad, GstPadQueryTypeFunction type_func)
672 {
673   g_return_if_fail (pad != NULL);
674   g_return_if_fail (GST_IS_REAL_PAD (pad));
675
676   GST_RPAD_QUERYTYPEFUNC (pad) = type_func;
677
678   GST_CAT_DEBUG (GST_CAT_PADS, "querytypefunc for %s:%s  set to %s",
679              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (type_func));
680 }
681
682 /**
683  * gst_pad_get_query_types:
684  * @pad: the #GstPad to query
685  *
686  * Get an array of supported queries that can be performed
687  * on this pad.
688  *
689  * Returns: an array of querytypes anded with 0.
690  */
691 const GstQueryType*
692 gst_pad_get_query_types (GstPad *pad)
693 {
694   GstRealPad *rpad;
695   
696   if (pad == NULL)
697     return FALSE;
698
699   rpad = GST_PAD_REALIZE (pad);
700
701   g_return_val_if_fail (rpad, FALSE);
702
703   if (GST_RPAD_QUERYTYPEFUNC (rpad))
704     return GST_RPAD_QUERYTYPEFUNC (rpad) (GST_PAD (pad));
705
706   return NULL;
707 }
708
709 static gboolean
710 gst_pad_get_query_types_dispatcher (GstPad *pad, const GstQueryType **data)
711 {
712   *data = gst_pad_get_query_types (pad);
713
714   return TRUE;
715 }
716
717 /**
718  * gst_pad_get_query_types_default:
719  * @pad: the #GstPad to query
720  *
721  * Invoke the default dispatcher for the query types on
722  * the pad.
723  *
724  * Returns: an array of querytypes anded with 0.
725  */
726 const GstQueryType*
727 gst_pad_get_query_types_default (GstPad *pad)
728 {
729   GstQueryType *result = NULL;
730
731   gst_pad_dispatcher (pad, (GstPadDispatcherFunction) 
732                            gst_pad_get_query_types_dispatcher, &result);
733
734   return result;
735 }
736
737 /**
738  * gst_pad_set_internal_link_function:
739  * @pad: a #GstPad to set the internal link function for.
740  * @intlink: the #GstPadIntLinkFunction to set.
741  *
742  * Sets the given internal link function for the pad.
743  */
744 void
745 gst_pad_set_internal_link_function (GstPad *pad, 
746                                           GstPadIntLinkFunction intlink)
747 {
748   g_return_if_fail (pad != NULL);
749   g_return_if_fail (GST_IS_REAL_PAD (pad));
750
751   GST_RPAD_INTLINKFUNC (pad) = intlink;
752   GST_CAT_DEBUG (GST_CAT_PADS, "internal link for %s:%s  set to %s",
753              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (intlink));
754 }
755
756 /**
757  * gst_pad_set_formats_function:
758  * @pad: the #GstPad to set the formats function for.
759  * @formats: the #GstPadFormatsFunction to set.
760  *
761  * Sets the given formats function for the pad.
762  */
763 void
764 gst_pad_set_formats_function (GstPad *pad, GstPadFormatsFunction formats)
765 {
766   g_return_if_fail (pad != NULL);
767   g_return_if_fail (GST_IS_REAL_PAD (pad));
768
769   GST_RPAD_FORMATSFUNC (pad) = formats;
770   GST_CAT_DEBUG (GST_CAT_PADS, "formats function for %s:%s  set to %s",
771              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (formats));
772 }
773
774 /**
775  * gst_pad_set_link_function:
776  * @pad: a #GstPad to set the link function for.
777  * @link: the #GstPadLinkFunction to set.
778  *
779  * Sets the given link function for the pad. It will be called
780  * when the pad is linked or relinked with caps.
781  */
782 void
783 gst_pad_set_link_function (GstPad *pad,
784                               GstPadLinkFunction link)
785 {
786   g_return_if_fail (pad != NULL);
787   g_return_if_fail (GST_IS_REAL_PAD (pad));
788
789   GST_RPAD_LINKFUNC (pad) = link;
790   GST_CAT_DEBUG (GST_CAT_PADS, "linkfunc for %s:%s set to %s",
791              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (link));
792 }
793
794 /**
795  * gst_pad_set_unlink_function:
796  * @pad: a #GstPad to set the unlink function for.
797  * @unlink: the #GstPadUnlinkFunction to set.
798  *
799  * Sets the given unlink function for the pad. It will be called
800  * when the pad is unlinked.
801  */
802 void
803 gst_pad_set_unlink_function (GstPad *pad,
804                               GstPadUnlinkFunction unlink)
805 {
806   g_return_if_fail (pad != NULL);
807   g_return_if_fail (GST_IS_REAL_PAD (pad));
808
809   GST_RPAD_UNLINKFUNC (pad) = unlink;
810   GST_CAT_DEBUG (GST_CAT_PADS, "unlinkfunc for %s:%s set to %s",
811              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (unlink));
812 }
813
814 /**
815  * gst_pad_set_fixate_function:
816  * @pad: a #GstPad to set the fixate function for.
817  * @getcaps: the #GstPadFixateFunction to set.
818  *
819  * Sets the given fixate function for the pad.
820  */
821 void
822 gst_pad_set_fixate_function (GstPad *pad, GstPadFixateFunction fixate)
823 {
824   g_return_if_fail (pad != NULL);
825   g_return_if_fail (GST_IS_REAL_PAD (pad));
826
827   GST_RPAD_FIXATEFUNC (pad) = fixate;
828   GST_CAT_DEBUG (GST_CAT_PADS, "fixatefunc for %s:%s set to %s",
829              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (fixate));
830 }
831
832 /**
833  * gst_pad_set_getcaps_function:
834  * @pad: a #GstPad to set the getcaps function for.
835  * @getcaps: the #GstPadGetCapsFunction to set.
836  *
837  * Sets the given getcaps function for the pad.
838  */
839 void
840 gst_pad_set_getcaps_function (GstPad *pad,
841                               GstPadGetCapsFunction getcaps)
842 {
843   g_return_if_fail (pad != NULL);
844   g_return_if_fail (GST_IS_REAL_PAD (pad));
845
846   GST_RPAD_GETCAPSFUNC (pad) = getcaps;
847   GST_CAT_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
848              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
849 }
850
851 /**
852  * gst_pad_set_bufferalloc_function:
853  * @pad: a #GstPad to set the bufferalloc function for.
854  * @bufalloc: the #GstPadBufferAllocFunction to set.
855  *
856  * Sets the given bufferalloc function for the pad. Note that the
857  * bufferalloc function can only be set on sinkpads.
858  */
859 void
860 gst_pad_set_bufferalloc_function (GstPad *pad,
861                                  GstPadBufferAllocFunction bufalloc)
862 {
863   g_return_if_fail (pad != NULL);
864   g_return_if_fail (GST_IS_REAL_PAD (pad));
865   g_return_if_fail (GST_PAD_IS_SINK (pad));
866
867   GST_RPAD_BUFFERALLOCFUNC (pad) = bufalloc;
868   GST_CAT_DEBUG (GST_CAT_PADS, "bufferallocfunc for %s:%s set to %s",
869              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufalloc));
870 }
871
872 /**
873  * gst_pad_unlink:
874  * @srcpad: the source #GstPad to unlink.
875  * @sinkpad: the sink #GstPad to unlink.
876  *
877  * Unlinks the source pad from the sink pad.
878  */
879 void
880 gst_pad_unlink (GstPad *srcpad,
881                     GstPad *sinkpad)
882 {
883   GstRealPad *realsrc, *realsink;
884   GstScheduler *src_sched, *sink_sched;
885
886   /* generic checks */
887   g_return_if_fail (srcpad != NULL);
888   g_return_if_fail (GST_IS_PAD (srcpad));
889   g_return_if_fail (sinkpad != NULL);
890   g_return_if_fail (GST_IS_PAD (sinkpad));
891
892   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
893             GST_DEBUG_PAD_NAME (srcpad), srcpad, 
894             GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
895
896   /* now we need to deal with the real/ghost stuff */
897   realsrc = GST_PAD_REALIZE (srcpad);
898   realsink = GST_PAD_REALIZE (sinkpad);
899
900   g_return_if_fail (GST_RPAD_PEER (realsrc) != NULL);
901   g_return_if_fail (GST_RPAD_PEER (realsink) == realsrc);
902
903   if ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SINK) &&
904       (GST_RPAD_DIRECTION (realsink) == GST_PAD_SRC)) {
905     GstRealPad *temppad;
906
907     temppad = realsrc;
908     realsrc = realsink;
909     realsink = temppad;
910   }
911   g_return_if_fail ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) &&
912                     (GST_RPAD_DIRECTION (realsink) == GST_PAD_SINK));
913
914   if (GST_RPAD_UNLINKFUNC (realsrc)) {
915     GST_RPAD_UNLINKFUNC (realsrc) (GST_PAD (realsrc));
916   }
917   if (GST_RPAD_UNLINKFUNC (realsink)) {
918     GST_RPAD_UNLINKFUNC (realsink) (GST_PAD (realsink));
919   }
920
921   /* get the schedulers before we unlink */
922   src_sched = gst_pad_get_scheduler (GST_PAD (realsrc));
923   sink_sched = gst_pad_get_scheduler (GST_PAD (realsink));
924
925   if (GST_RPAD_LINK (realsrc)) gst_pad_link_free (GST_RPAD_LINK (realsrc));
926
927   /* first clear peers */
928   GST_RPAD_PEER (realsrc) = NULL;
929   GST_RPAD_PEER (realsink) = NULL;
930   GST_RPAD_LINK (realsrc) = NULL;
931   GST_RPAD_LINK (realsink) = NULL;
932
933   /* now tell the scheduler */
934   if (src_sched && src_sched == sink_sched) {
935     gst_scheduler_pad_unlink (src_sched, 
936                               GST_PAD (realsrc), 
937                               GST_PAD (realsink));
938   }
939
940   /* hold a reference, as they can go away in the signal handlers */
941   gst_object_ref (GST_OBJECT (realsrc));
942   gst_object_ref (GST_OBJECT (realsink));
943
944   /* fire off a signal to each of the pads telling them 
945    * that they've been unlinked */
946   g_signal_emit (G_OBJECT (realsrc), gst_real_pad_signals[REAL_UNLINKED], 
947                  0, realsink);
948   g_signal_emit (G_OBJECT (realsink), gst_real_pad_signals[REAL_UNLINKED], 
949                  0, realsrc);
950
951   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
952             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
953
954   gst_object_unref (GST_OBJECT (realsrc));
955   gst_object_unref (GST_OBJECT (realsink));
956 }
957
958 /**
959  * gst_pad_is_linked:
960  * @pad: pad to check
961  *
962  * Checks if a given pad is linked to another pad or not.
963  *
964  * Returns: TRUE if the pad is linked.
965  */
966 gboolean
967 gst_pad_is_linked (GstPad *pad)
968 {
969   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
970
971   return GST_PAD_PEER (pad) != NULL;
972 }
973
974 struct _GstPadLink {
975   GType type;
976
977   gboolean bla;
978   gboolean srcnotify;
979   gboolean sinknotify;
980
981   GstPad *srcpad;
982   GstPad *sinkpad;
983
984   GstCaps *srccaps;
985   GstCaps *sinkcaps;
986   GstCaps *filtercaps;
987   GstCaps *caps;
988
989   GstPadFixateFunction app_fixate;
990 };
991
992 static gboolean
993 gst_pad_check_schedulers (GstRealPad *realsrc, GstRealPad *realsink)
994 {
995   GstScheduler *src_sched, *sink_sched;
996   gint num_decoupled = 0;
997
998   src_sched = gst_pad_get_scheduler (GST_PAD (realsrc));
999   sink_sched = gst_pad_get_scheduler (GST_PAD (realsink));
1000
1001   if (src_sched && sink_sched) {
1002     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsrc), GST_ELEMENT_DECOUPLED))
1003       num_decoupled++;
1004     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsink), GST_ELEMENT_DECOUPLED))
1005       num_decoupled++;
1006
1007     if (src_sched != sink_sched && num_decoupled != 1) {
1008       return FALSE;
1009     }
1010   }
1011   return TRUE;
1012 }
1013
1014 #define GST_PAD_LINK_SRC(pad) ((GST_PAD_IS_SRC (pad)) ? (pad) : GST_PAD_PEER (pad))
1015 #define GST_PAD_LINK_SINK(pad) ((GST_PAD_IS_SINK (pad)) ? (pad) : GST_PAD_PEER (pad))
1016
1017 static GstPadLink *
1018 gst_pad_link_new (void)
1019 {
1020   GstPadLink *link;
1021   link = g_new0 (GstPadLink, 1);
1022   link->sinknotify = TRUE;
1023   link->srcnotify = TRUE;
1024   return link;
1025 }
1026
1027 static void
1028 gst_pad_link_free (GstPadLink *link)
1029 {
1030   if (link->srccaps) gst_caps_free (link->srccaps);
1031   if (link->sinkcaps) gst_caps_free (link->sinkcaps);
1032   if (link->filtercaps) gst_caps_free (link->filtercaps);
1033   if (link->caps) gst_caps_free (link->caps);
1034 #ifdef USE_POISONING
1035   memset(link,0xff, sizeof(*link));
1036 #endif
1037   g_free (link);
1038 }
1039
1040 static void gst_pad_link_intersect (GstPadLink *link)
1041 {
1042   GstCaps *pad_intersection;
1043
1044   if (link->caps) gst_caps_free (link->caps);
1045
1046   GST_DEBUG ("intersecting link from %s:%s to %s:%s",
1047       GST_DEBUG_PAD_NAME (link->srcpad), GST_DEBUG_PAD_NAME (link->sinkpad));
1048   GST_DEBUG_CAPS ("srccaps", link->srccaps);
1049   GST_DEBUG_CAPS ("sinkcaps", link->sinkcaps);
1050   GST_DEBUG_CAPS ("filtercaps", link->filtercaps);
1051
1052   pad_intersection = gst_caps_intersect (link->srccaps, link->sinkcaps);
1053
1054   if (link->filtercaps) {
1055     GST_DEBUG_CAPS ("unfiltered intersection", pad_intersection);
1056     link->caps = gst_caps_intersect (pad_intersection, link->filtercaps);
1057     gst_caps_free (pad_intersection);
1058   } else {
1059     link->caps = pad_intersection;
1060   }
1061
1062   GST_DEBUG_CAPS ("intersection", link->caps);
1063 }
1064
1065 static gboolean
1066 gst_pad_link_ready_for_negotiation (GstPadLink *link)
1067 {
1068   GstElement *parent;
1069
1070   parent = GST_PAD_PARENT (link->srcpad);
1071   if (!parent || GST_STATE (parent) < GST_STATE_READY) {
1072     GST_DEBUG ("parent %s of pad %s:%s is not READY",
1073         GST_ELEMENT_NAME (parent), GST_DEBUG_PAD_NAME (link->srcpad));
1074     return FALSE;
1075   }
1076   parent = GST_PAD_PARENT (link->sinkpad);
1077   if (!parent || GST_STATE (parent) < GST_STATE_READY) {
1078     GST_DEBUG ("parent %s of pad %s:%s is not READY",
1079         GST_ELEMENT_NAME (parent), GST_DEBUG_PAD_NAME (link->sinkpad));
1080     return FALSE;
1081   }
1082
1083   return TRUE;
1084 }
1085
1086 static void
1087 gst_pad_link_fixate (GstPadLink *link)
1088 {
1089   GstCaps *caps;
1090   GstCaps *newcaps;
1091
1092   caps = link->caps;
1093
1094   g_return_if_fail (caps != NULL);
1095   g_return_if_fail (!gst_caps_is_empty(caps));
1096
1097   GST_DEBUG_CAPS ("trying to fixate caps", caps);
1098
1099   while (!gst_caps_is_fixed (caps)) {
1100     int i;
1101
1102     for (i=0;i<5;i++){
1103       newcaps = NULL;
1104       switch (i) {
1105         case 0:
1106           g_signal_emit (G_OBJECT (link->srcpad),
1107               gst_real_pad_signals[REAL_FIXATE], 0, caps, &newcaps);
1108           GST_DEBUG_CAPS ("app srcpad signal fixated to", newcaps);
1109           break;
1110         case 1:
1111           g_signal_emit (G_OBJECT (link->sinkpad),
1112               gst_real_pad_signals[REAL_FIXATE], 0, caps, &newcaps);
1113           GST_DEBUG_CAPS ("app sinkpad signal fixated to", newcaps);
1114           break;
1115         case 2:
1116           if (GST_RPAD_FIXATEFUNC(link->srcpad)) {
1117             newcaps = GST_RPAD_FIXATEFUNC(link->srcpad) (
1118                 GST_PAD (link->srcpad), caps);
1119             GST_DEBUG_CAPS ("srcpad fixated to", newcaps);
1120           }
1121           break;
1122         case 3:
1123           if (GST_RPAD_FIXATEFUNC(link->sinkpad)) {
1124             newcaps = GST_RPAD_FIXATEFUNC(link->sinkpad) (
1125                 GST_PAD (link->sinkpad), caps);
1126             GST_DEBUG_CAPS ("sinkpad fixated to", newcaps);
1127           }
1128           break;
1129         case 4:
1130           newcaps = _gst_pad_default_fixate_func (
1131               GST_PAD(link->srcpad), caps);
1132           GST_DEBUG_CAPS ("core fixated to", newcaps);
1133           break;
1134       }
1135       if (newcaps) {
1136         gst_caps_free (caps);
1137         caps = newcaps;
1138         break;
1139       }
1140     }
1141   }
1142
1143   link->caps = caps;
1144 }
1145
1146 static GstPadLinkReturn
1147 gst_pad_link_call_link_functions (GstPadLink *link)
1148 {
1149   gboolean negotiating;
1150   GstPadLinkReturn res;
1151
1152   if (link->srcnotify && GST_RPAD_LINKFUNC (link->srcpad)){
1153     GST_DEBUG ("calling link function on pad %s:%s",
1154         GST_DEBUG_PAD_NAME (link->srcpad));
1155
1156     negotiating = GST_FLAG_IS_SET (link->srcpad, GST_PAD_NEGOTIATING);
1157
1158     /* set the NEGOTIATING flag if not already done */
1159     if (!negotiating) GST_FLAG_SET (link->srcpad, GST_PAD_NEGOTIATING);
1160     
1161     /* call the link function */
1162     res = GST_RPAD_LINKFUNC (link->srcpad) (GST_PAD (link->srcpad),
1163         link->caps);
1164
1165     /* unset again after negotiating only if we set it  */
1166     if (!negotiating) GST_FLAG_UNSET (link->srcpad, GST_PAD_NEGOTIATING);
1167
1168     GST_DEBUG ("got reply %d from link function on pad %s:%s",
1169         res, GST_DEBUG_PAD_NAME (link->srcpad));
1170
1171     if (GST_PAD_LINK_FAILED (res)) {
1172       GST_CAT_INFO (GST_CAT_CAPS, "pad %s:%s doesn't accept caps",
1173                 GST_DEBUG_PAD_NAME (link->srcpad));
1174       return res;
1175     }
1176   }
1177
1178   if (link->sinknotify && GST_RPAD_LINKFUNC (link->sinkpad)){
1179     GST_DEBUG ("calling link function on pad %s:%s",
1180         GST_DEBUG_PAD_NAME (link->sinkpad));
1181
1182     negotiating = GST_FLAG_IS_SET (link->sinkpad, GST_PAD_NEGOTIATING);
1183
1184     /* set the NEGOTIATING flag if not already done */
1185     if (!negotiating) GST_FLAG_SET (link->sinkpad, GST_PAD_NEGOTIATING);
1186     
1187     /* call the link function */
1188     res = GST_RPAD_LINKFUNC (link->sinkpad) (GST_PAD (link->sinkpad),
1189         link->caps);
1190
1191     /* unset again after negotiating only if we set it  */
1192     if (!negotiating) GST_FLAG_UNSET (link->sinkpad, GST_PAD_NEGOTIATING);
1193
1194     GST_DEBUG ("got reply %d from link function on pad %s:%s",
1195         res, GST_DEBUG_PAD_NAME (link->sinkpad));
1196
1197     if (GST_PAD_LINK_FAILED (res)) {
1198       GST_CAT_INFO (GST_CAT_CAPS, "pad %s:%s doesn't accept caps",
1199                 GST_DEBUG_PAD_NAME (link->sinkpad));
1200       return res;
1201     }
1202   }
1203
1204   return GST_PAD_LINK_OK;
1205 }
1206
1207 static GstPadLinkReturn
1208 gst_pad_link_negotiate (GstPadLink *link)
1209 {
1210   GST_DEBUG ("negotiating link from pad %s:%s to pad %s:%s",
1211       GST_DEBUG_PAD_NAME (link->srcpad), GST_DEBUG_PAD_NAME (link->sinkpad));
1212
1213   if (!gst_pad_link_ready_for_negotiation (link)) {
1214     return GST_PAD_LINK_DELAYED;
1215   }
1216
1217   gst_pad_link_intersect (link);
1218   if (gst_caps_is_empty (link->caps))
1219     return GST_PAD_LINK_REFUSED;
1220
1221   gst_pad_link_fixate (link);
1222   if (gst_caps_is_empty (link->caps))
1223     return GST_PAD_LINK_REFUSED;
1224
1225   return gst_pad_link_call_link_functions (link);
1226 }
1227
1228 /**
1229  * gst_pad_link_try:
1230  * @link: link to try
1231  *
1232  * Tries to (re)link the pads with the given link. The function takes ownership
1233  * of the supplied link. If the function returns FALSE and an old link existed,
1234  * that link can be assumed to work unchanged.
1235  *
1236  * Returns: TRUE if the link succeeded, FALSE if not.
1237  */
1238 static gboolean
1239 gst_pad_link_try (GstPadLink *link)
1240 {
1241   GstPad *srcpad, *sinkpad;
1242   GstPadLink *oldlink;
1243   GstPadLinkReturn ret;
1244   
1245   /* we use assertions here, because this function is static */
1246   g_assert (link);
1247   srcpad = link->srcpad;
1248   g_assert (srcpad);
1249   sinkpad = link->sinkpad;
1250   g_assert (sinkpad);
1251   oldlink = GST_RPAD_LINK (srcpad);
1252   g_assert (oldlink == GST_RPAD_LINK (sinkpad));
1253   
1254   ret = gst_pad_link_negotiate (link); 
1255   if (GST_PAD_LINK_FAILED (ret) && oldlink && oldlink->caps) {
1256     oldlink->srcnotify = link->srcnotify;
1257     oldlink->sinknotify = link->sinknotify;
1258     if (GST_PAD_LINK_FAILED (gst_pad_link_call_link_functions (oldlink))) {
1259       g_warning ("pads don't accept old caps. We assume they did though");
1260     }
1261   }
1262   if (ret == GST_PAD_LINK_REFUSED) {
1263     gst_pad_link_free (link);
1264     return ret;
1265   }
1266   if (ret == GST_PAD_LINK_DELAYED) {
1267     gst_caps_replace (&link->caps, NULL);
1268   }
1269
1270   GST_RPAD_PEER (srcpad) = GST_REAL_PAD(link->sinkpad);
1271   GST_RPAD_PEER (sinkpad) = GST_REAL_PAD(link->srcpad);
1272   if (oldlink)
1273     gst_pad_link_free (oldlink);
1274   GST_RPAD_LINK (srcpad) = link;
1275   GST_RPAD_LINK (sinkpad) = link;
1276   if (ret == GST_PAD_LINK_OK) {
1277     g_object_notify (G_OBJECT (srcpad), "caps");
1278     g_object_notify (G_OBJECT (sinkpad), "caps");
1279   }
1280   
1281   return ret;
1282 }
1283
1284 GstPadLinkReturn
1285 gst_pad_renegotiate (GstPad *pad)
1286 {
1287   GstPadLink *link;
1288
1289   g_return_val_if_fail (pad != NULL, GST_PAD_LINK_REFUSED);
1290   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_LINK_REFUSED);
1291
1292   g_return_val_if_fail (GST_PAD_LINK_SRC (pad), GST_PAD_LINK_REFUSED);
1293   g_return_val_if_fail (GST_PAD_LINK_SINK (pad), GST_PAD_LINK_REFUSED);
1294
1295   link = gst_pad_link_new ();
1296
1297   link->srcpad = GST_PAD_LINK_SRC (pad);
1298   link->sinkpad = GST_PAD_LINK_SINK (pad);
1299
1300   if (!gst_pad_link_ready_for_negotiation (link)) {
1301     gst_pad_link_free (link);
1302     return GST_PAD_LINK_DELAYED;
1303   }
1304
1305   if (GST_REAL_PAD(pad)->link->filtercaps) {
1306     link->filtercaps = gst_caps_copy (GST_REAL_PAD(pad)->link->filtercaps);
1307   }
1308   link->srccaps = gst_pad_get_caps (link->srcpad);
1309   link->sinkcaps = gst_pad_get_caps (link->sinkpad);
1310
1311   return gst_pad_link_try (link);
1312 }
1313
1314 GstPadLinkReturn
1315 gst_pad_try_set_caps (GstPad *pad, const GstCaps *caps)
1316 {
1317   GstPadLink *link;
1318   GstPadLink *oldlink;
1319   GstPadLinkReturn ret;
1320
1321   g_return_val_if_fail (pad != NULL, GST_PAD_LINK_REFUSED);
1322   g_return_val_if_fail (GST_IS_REAL_PAD (pad), GST_PAD_LINK_REFUSED);
1323   g_return_val_if_fail (!GST_FLAG_IS_SET (pad, GST_PAD_NEGOTIATING),
1324       GST_PAD_LINK_REFUSED);
1325
1326   /* setting non-fixed caps on a pad is not allowed */
1327   if (!gst_caps_is_fixed (caps)) {
1328     GST_CAT_INFO (GST_CAT_CAPS, 
1329               "trying to set unfixed caps on pad %s:%s, not allowed",
1330               GST_DEBUG_PAD_NAME (pad));
1331     g_warning ("trying to set non fixed caps on pad %s:%s, not allowed",
1332                GST_DEBUG_PAD_NAME (pad));
1333
1334     gst_caps_debug (caps, "unfixed caps");
1335     return GST_PAD_LINK_REFUSED;
1336   }
1337
1338   /* we allow setting caps on non-linked pads.  It's ignored */
1339   if (!GST_PAD_PEER (pad)) {
1340     return GST_PAD_LINK_OK;
1341   }
1342
1343   /* if the desired caps are already there, it's trivially ok */
1344   if (GST_PAD_CAPS (pad) && gst_caps_is_equal_fixed (caps,
1345         GST_PAD_CAPS (pad))) {
1346     return GST_PAD_LINK_OK;
1347   }
1348
1349   g_return_val_if_fail (GST_PAD_LINK_SRC (pad), GST_PAD_LINK_REFUSED);
1350   g_return_val_if_fail (GST_PAD_LINK_SINK (pad), GST_PAD_LINK_REFUSED);
1351
1352   link = gst_pad_link_new ();
1353
1354   link->srcpad = GST_PAD_LINK_SRC (pad);
1355   link->sinkpad = GST_PAD_LINK_SINK (pad);
1356
1357   if (!gst_pad_link_ready_for_negotiation (link)) {
1358     gst_pad_link_free (link);
1359     return GST_PAD_LINK_DELAYED;
1360   }
1361
1362   oldlink = GST_REAL_PAD(pad)->link;
1363   if (oldlink && oldlink->filtercaps) {
1364     link->filtercaps = gst_caps_copy (oldlink->filtercaps);
1365   }
1366   if (link->srcpad == pad) {
1367     link->srccaps = gst_caps_copy(caps);
1368     link->sinkcaps = gst_pad_get_caps (link->sinkpad);
1369     link->srcnotify = FALSE;
1370   } else {
1371     link->srccaps = gst_pad_get_caps (link->srcpad);
1372     link->sinkcaps = gst_caps_copy(caps);
1373     link->sinknotify = FALSE;
1374   }
1375
1376   ret = gst_pad_link_try (link);
1377
1378   return ret;
1379 }
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389 /**
1390  * gst_pad_can_link_filtered:
1391  * @srcpad: the source #GstPad to link.
1392  * @sinkpad: the sink #GstPad to link.
1393  * @filtercaps: the filter #GstCaps.
1394  *
1395  * Checks if the source pad and the sink pad can be linked when constrained
1396  * by the given filter caps.
1397  *
1398  * Returns: TRUE if the pads can be linked, FALSE otherwise.
1399  */
1400 gboolean
1401 gst_pad_can_link_filtered (GstPad *srcpad, GstPad *sinkpad,
1402                            const GstCaps *filtercaps)
1403 {
1404   GstRealPad *realsrc, *realsink;
1405   GstPadLink *link;
1406
1407   /* FIXME This function is gross.  It's almost a direct copy of
1408    * gst_pad_link_filtered().  Any decent programmer would attempt
1409    * to merge the two functions, which I will do some day. --ds
1410    */
1411
1412   /* generic checks */
1413   g_return_val_if_fail (srcpad != NULL, FALSE);
1414   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1415   g_return_val_if_fail (sinkpad != NULL, FALSE);
1416   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1417
1418   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1419             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1420
1421   /* now we need to deal with the real/ghost stuff */
1422   realsrc = GST_PAD_REALIZE (srcpad);
1423   realsink = GST_PAD_REALIZE (sinkpad);
1424
1425   if ((GST_PAD (realsrc) != srcpad) || (GST_PAD (realsink) != sinkpad)) {
1426     GST_CAT_INFO (GST_CAT_PADS, "*actually* linking %s:%s and %s:%s",
1427               GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1428   }
1429   /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1430   if (GST_RPAD_PEER (realsrc) != NULL) {
1431     GST_CAT_INFO (GST_CAT_PADS, "Real source pad %s:%s has a peer, failed",
1432               GST_DEBUG_PAD_NAME (realsrc));
1433     return FALSE;
1434   }
1435   if (GST_RPAD_PEER (realsink) != NULL) {
1436     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has a peer, failed",
1437               GST_DEBUG_PAD_NAME (realsink));
1438     return FALSE;
1439   }
1440   if (GST_PAD_PARENT (realsrc) == NULL) {
1441     GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s has no parent, failed",
1442               GST_DEBUG_PAD_NAME (realsrc));
1443     return FALSE;
1444   }
1445   if (GST_PAD_PARENT (realsink) == NULL) {
1446     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has no parent, failed",
1447               GST_DEBUG_PAD_NAME (realsrc));
1448     return FALSE;
1449   }
1450
1451   if (!gst_pad_check_schedulers (realsrc, realsink)) {
1452     g_warning ("linking pads with different scheds requires "
1453                "exactly one decoupled element (such as queue)");
1454     return FALSE;
1455   }
1456   
1457   g_return_val_if_fail (realsrc != NULL, GST_PAD_LINK_REFUSED);
1458   g_return_val_if_fail (realsink != NULL, GST_PAD_LINK_REFUSED);
1459
1460   link = gst_pad_link_new ();
1461
1462   if (GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) {
1463     link->srcpad = GST_PAD (realsrc);
1464     link->sinkpad = GST_PAD (realsink);
1465   } else {
1466     link->srcpad = GST_PAD (realsink);
1467     link->sinkpad = GST_PAD (realsrc);
1468   }
1469
1470   if (GST_RPAD_DIRECTION (link->srcpad) != GST_PAD_SRC) {
1471     GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s is not a source pad, failed",
1472               GST_DEBUG_PAD_NAME (link->srcpad));
1473     gst_pad_link_free (link);
1474     return FALSE;
1475   }    
1476   if (GST_RPAD_DIRECTION (link->sinkpad) != GST_PAD_SINK) {
1477     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s is not a sink pad, failed",
1478               GST_DEBUG_PAD_NAME (link->sinkpad));
1479     gst_pad_link_free (link);
1480     return FALSE;
1481   }
1482
1483   link->srccaps = gst_pad_get_caps (link->srcpad);
1484   link->sinkcaps = gst_pad_get_caps (link->sinkpad);
1485   if (filtercaps) link->filtercaps = gst_caps_copy (filtercaps);
1486
1487   gst_pad_link_intersect (link);
1488   if (gst_caps_is_empty (link->caps))
1489     return FALSE;
1490
1491   return TRUE;
1492 }
1493
1494 /**
1495  * gst_pad_can_link:
1496  * @srcpad: the source #GstPad to link.
1497  * @sinkpad: the sink #GstPad to link.
1498  *
1499  * Checks if the source pad and the sink pad can be link.
1500  *
1501  * Returns: TRUE if the pads can be linked, FALSE otherwise.
1502  */
1503 gboolean
1504 gst_pad_can_link (GstPad *srcpad, GstPad *sinkpad)
1505 {
1506   return gst_pad_can_link_filtered (srcpad, sinkpad, NULL);
1507 }
1508
1509 /**
1510  * gst_pad_link_filtered:
1511  * @srcpad: the source #GstPad to link.
1512  * @sinkpad: the sink #GstPad to link.
1513  * @filtercaps: the filter #GstCaps.
1514  *
1515  * Links the source pad and the sink pad, constrained
1516  * by the given filter caps. This function sinks the caps.
1517  *
1518  * Returns: TRUE if the pads have been linked, FALSE otherwise.
1519  */
1520 gboolean
1521 gst_pad_link_filtered (GstPad *srcpad, GstPad *sinkpad,
1522     const GstCaps *filtercaps)
1523 {
1524   GstRealPad *realsrc, *realsink;
1525   GstScheduler *src_sched, *sink_sched;
1526   GstPadLink *link;
1527
1528   /* generic checks */
1529   g_return_val_if_fail (srcpad != NULL, FALSE);
1530   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1531   g_return_val_if_fail (sinkpad != NULL, FALSE);
1532   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1533
1534   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1535             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1536
1537   /* now we need to deal with the real/ghost stuff */
1538   realsrc = GST_PAD_REALIZE (srcpad);
1539   realsink = GST_PAD_REALIZE (sinkpad);
1540
1541   if ((GST_PAD (realsrc) != srcpad) || (GST_PAD (realsink) != sinkpad)) {
1542     GST_CAT_INFO (GST_CAT_PADS, "*actually* linking %s:%s and %s:%s",
1543               GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1544   }
1545   /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1546   if (GST_RPAD_PEER (realsrc) != NULL) {
1547     GST_CAT_INFO (GST_CAT_PADS, "Real source pad %s:%s has a peer, failed",
1548               GST_DEBUG_PAD_NAME (realsrc));
1549     return FALSE;
1550   }
1551   if (GST_RPAD_PEER (realsink) != NULL) {
1552     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has a peer, failed",
1553               GST_DEBUG_PAD_NAME (realsink));
1554     return FALSE;
1555   }
1556   if (GST_PAD_PARENT (realsrc) == NULL) {
1557     GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s has no parent, failed",
1558               GST_DEBUG_PAD_NAME (realsrc));
1559     return FALSE;
1560   }
1561   if (GST_PAD_PARENT (realsink) == NULL) {
1562     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s has no parent, failed",
1563               GST_DEBUG_PAD_NAME (realsrc));
1564     return FALSE;
1565   }
1566
1567   if (!gst_pad_check_schedulers (realsrc, realsink)) {
1568     g_warning ("linking pads with different scheds requires "
1569                "exactly one decoupled element (such as queue)");
1570     return FALSE;
1571   }
1572   
1573   g_return_val_if_fail (realsrc != NULL, GST_PAD_LINK_REFUSED);
1574   g_return_val_if_fail (realsink != NULL, GST_PAD_LINK_REFUSED);
1575
1576   link = gst_pad_link_new ();
1577
1578   if (GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) {
1579     link->srcpad = GST_PAD (realsrc);
1580     link->sinkpad = GST_PAD (realsink);
1581   } else {
1582     link->srcpad = GST_PAD (realsink);
1583     link->sinkpad = GST_PAD (realsrc);
1584   }
1585
1586   if (GST_RPAD_DIRECTION (link->srcpad) != GST_PAD_SRC) {
1587     GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s is not a source pad, failed",
1588               GST_DEBUG_PAD_NAME (link->srcpad));
1589     gst_pad_link_free (link);
1590     return FALSE;
1591   }    
1592   if (GST_RPAD_DIRECTION (link->sinkpad) != GST_PAD_SINK) {
1593     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s is not a sink pad, failed",
1594               GST_DEBUG_PAD_NAME (link->sinkpad));
1595     gst_pad_link_free (link);
1596     return FALSE;
1597   }
1598
1599   link->srccaps = gst_pad_get_caps (link->srcpad);
1600   link->sinkcaps = gst_pad_get_caps (link->sinkpad);
1601   if (filtercaps) link->filtercaps = gst_caps_copy (filtercaps);
1602   if (gst_pad_link_try (link) == GST_PAD_LINK_REFUSED)
1603     return FALSE;
1604
1605   /* fire off a signal to each of the pads telling them 
1606    * that they've been linked */
1607   g_signal_emit (G_OBJECT (link->srcpad), gst_real_pad_signals[REAL_LINKED], 
1608                  0, link->sinkpad);
1609   g_signal_emit (G_OBJECT (link->sinkpad), gst_real_pad_signals[REAL_LINKED], 
1610                  0, link->srcpad);
1611
1612   src_sched = gst_pad_get_scheduler (GST_PAD (link->srcpad));
1613   sink_sched = gst_pad_get_scheduler (GST_PAD (link->sinkpad));
1614
1615   /* now tell the scheduler */
1616   if (src_sched && src_sched == sink_sched) {
1617     gst_scheduler_pad_link (src_sched, 
1618                             GST_PAD (link->srcpad), GST_PAD (link->sinkpad));
1619   }
1620   else {
1621     GST_CAT_INFO (GST_CAT_PADS, "not telling link to scheduler %s:%s and %s:%s, %p %p",
1622             GST_DEBUG_PAD_NAME (link->srcpad),
1623             GST_DEBUG_PAD_NAME (link->sinkpad),
1624             src_sched, sink_sched);
1625   }
1626
1627   GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1628             GST_DEBUG_PAD_NAME (link->srcpad),
1629             GST_DEBUG_PAD_NAME (link->sinkpad));
1630
1631   return TRUE;
1632 }
1633
1634 /**
1635  * gst_pad_link:
1636  * @srcpad: the source #GstPad to link.
1637  * @sinkpad: the sink #GstPad to link.
1638  *
1639  * Links the source pad to the sink pad.
1640  *
1641  * Returns: TRUE if the pad could be linked, FALSE otherwise.
1642  */
1643 gboolean
1644 gst_pad_link (GstPad *srcpad, GstPad *sinkpad)
1645 {
1646   return gst_pad_link_filtered (srcpad, sinkpad, NULL);
1647 }
1648
1649 /**
1650  * gst_pad_set_parent:
1651  * @pad: a #GstPad to set the parent of.
1652  * @parent: the new parent #GstElement.
1653  *
1654  * Sets the parent object of a pad.
1655  */
1656 void
1657 gst_pad_set_parent (GstPad *pad, GstElement *parent)
1658 {
1659   g_return_if_fail (pad != NULL);
1660   g_return_if_fail (GST_IS_PAD (pad));
1661   g_return_if_fail (GST_PAD_PARENT (pad) == NULL);
1662   g_return_if_fail (parent != NULL);
1663   g_return_if_fail (GST_IS_OBJECT (parent));
1664   g_return_if_fail ((gpointer) pad != (gpointer) parent);
1665
1666   gst_object_set_parent (GST_OBJECT (pad), GST_OBJECT (parent));
1667 }
1668
1669 /**
1670  * gst_pad_get_parent:
1671  * @pad: the #GstPad to get the parent of.
1672  *
1673  * Gets the parent object of this pad.
1674  *
1675  * Returns: the parent #GstElement.
1676  */
1677 GstElement*
1678 gst_pad_get_parent (GstPad *pad)
1679 {
1680   g_return_val_if_fail (pad != NULL, NULL);
1681   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1682
1683   return GST_PAD_PARENT (pad);
1684 }
1685
1686 static void
1687 gst_pad_set_pad_template (GstPad *pad, GstPadTemplate *templ)
1688 {
1689   /* this function would need checks if it weren't static */
1690
1691   gst_object_replace ((GstObject **) &pad->padtemplate, (GstObject *) templ);
1692   
1693   if (templ)
1694     g_signal_emit (G_OBJECT (templ), gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
1695
1696 /**
1697  * gst_pad_get_pad_template:
1698  * @pad: a #GstPad to get the pad template of.
1699  *
1700  * Gets the pad template object of this pad.
1701  *
1702  * Returns: the #GstPadTemplate from which this pad was instantiated.
1703  */
1704 GstPadTemplate*
1705 gst_pad_get_pad_template (GstPad *pad)
1706 {
1707   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1708
1709   return GST_PAD_PAD_TEMPLATE (pad); 
1710 }
1711
1712
1713 /**
1714  * gst_pad_get_scheduler:
1715  * @pad: a #GstPad to get the scheduler of.
1716  *
1717  * Gets the scheduler of the pad. Since the pad does not
1718  * have a scheduler of its own, the scheduler of the parent
1719  * is taken. For decoupled pads, the scheduler of the peer
1720  * parent is taken.
1721  *
1722  * Returns: the #GstScheduler of the pad.
1723  */
1724 GstScheduler*
1725 gst_pad_get_scheduler (GstPad *pad)
1726 {
1727   GstScheduler *scheduler = NULL;
1728   GstElement *parent;
1729   
1730   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1731   
1732   parent = gst_pad_get_parent (pad);
1733   if (parent) {
1734     if (GST_FLAG_IS_SET (parent, GST_ELEMENT_DECOUPLED)) {
1735       GstRealPad *peer = GST_RPAD_PEER (pad);
1736
1737       if (peer) {
1738         scheduler = gst_element_get_scheduler (gst_pad_get_parent (GST_PAD (peer)));
1739       }
1740     }
1741     else {
1742       scheduler = gst_element_get_scheduler (parent);
1743     }
1744   }
1745  
1746   return scheduler;
1747 }
1748
1749 /**
1750  * gst_pad_get_real_parent:
1751  * @pad: a #GstPad to get the real parent of.
1752  *
1753  * Gets the real parent object of this pad. If the pad
1754  * is a ghost pad, the actual owner of the real pad is
1755  * returned, as opposed to #gst_pad_get_parent().
1756  *
1757  * Returns: the parent #GstElement.
1758  */
1759 GstElement*
1760 gst_pad_get_real_parent (GstPad *pad)
1761 {
1762   g_return_val_if_fail (pad != NULL, NULL);
1763   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1764
1765   return GST_PAD_PARENT (GST_PAD (GST_PAD_REALIZE (pad)));
1766 }
1767
1768 /**
1769  * gst_pad_add_ghost_pad:
1770  * @pad: a #GstPad to attach the ghost pad to.
1771  * @ghostpad: the ghost #GstPad to to the pad.
1772  *
1773  * Adds a ghost pad to a pad.
1774  */
1775 void
1776 gst_pad_add_ghost_pad (GstPad *pad,
1777                        GstPad *ghostpad)
1778 {
1779   GstRealPad *realpad;
1780
1781   g_return_if_fail (pad != NULL);
1782   g_return_if_fail (GST_IS_PAD (pad));
1783   g_return_if_fail (ghostpad != NULL);
1784   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
1785
1786   realpad = GST_PAD_REALIZE (pad);
1787
1788   realpad->ghostpads = g_list_prepend (realpad->ghostpads, ghostpad);
1789 }
1790
1791
1792 /**
1793  * gst_pad_remove_ghost_pad:
1794  * @pad: a #GstPad to remove the ghost pad from.
1795  * @ghostpad: the ghost #GstPad to remove from the pad.
1796  *
1797  * Removes a ghost pad from a pad.
1798  */
1799 void
1800 gst_pad_remove_ghost_pad (GstPad *pad,
1801                           GstPad *ghostpad)
1802 {
1803   GstRealPad *realpad;
1804
1805   g_return_if_fail (GST_IS_PAD (pad));
1806   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
1807   realpad = GST_PAD_REALIZE (pad);
1808   g_return_if_fail (GST_GPAD_REALPAD (ghostpad) == realpad);
1809
1810   realpad->ghostpads = g_list_remove (realpad->ghostpads, ghostpad);
1811   GST_GPAD_REALPAD (ghostpad) = NULL;
1812 }
1813
1814 /**
1815  * gst_pad_get_ghost_pad_list:
1816  * @pad: a #GstPad to get the ghost pads of.
1817  *
1818  * Gets the ghost pads of this pad.
1819  *
1820  * Returns: a #GList of ghost pads.
1821  */
1822 GList*
1823 gst_pad_get_ghost_pad_list (GstPad *pad)
1824 {
1825   g_return_val_if_fail (pad != NULL, NULL);
1826   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1827
1828   return GST_PAD_REALIZE(pad)->ghostpads;
1829 }
1830
1831 static gboolean
1832 _gst_pad_default_fixate_foreach (GQuark field_id, GValue *value,
1833     gpointer s)
1834 {
1835   GstStructure *structure = (GstStructure *)s;
1836   GType type = G_VALUE_TYPE (value);
1837   
1838   if (G_TYPE_IS_FUNDAMENTAL (type) || type == GST_TYPE_FOURCC) return TRUE;
1839
1840   if (type == GST_TYPE_INT_RANGE) {
1841     gst_structure_set (structure, g_quark_to_string (field_id),
1842         G_TYPE_INT, gst_value_get_int_range_min (value), NULL);
1843     return FALSE;
1844   }
1845   if (type == GST_TYPE_DOUBLE_RANGE) {
1846     gst_structure_set (structure, g_quark_to_string (field_id),
1847         G_TYPE_DOUBLE, gst_value_get_double_range_min (value), NULL);
1848     return FALSE;
1849   }
1850   if (type == GST_TYPE_LIST) {
1851     gst_structure_set_value (structure, g_quark_to_string (field_id),
1852         gst_value_list_get_value (value, 0));
1853     return FALSE;
1854   }
1855
1856   g_critical ("don't know how to fixate type %s", g_type_name(type));
1857   return TRUE;
1858 }
1859
1860 static GstCaps *
1861 _gst_pad_default_fixate_func (GstPad *pad, const GstCaps *caps)
1862 {
1863   static GstStaticCaps octetcaps = GST_STATIC_CAPS (
1864       "application/octet-stream");
1865   GstStructure *structure;
1866   GstCaps *newcaps;
1867
1868   g_return_val_if_fail (pad != NULL, NULL);
1869   g_return_val_if_fail (caps != NULL, NULL);
1870   g_return_val_if_fail (!gst_caps_is_empty (caps), NULL);
1871
1872   if (gst_caps_is_any (caps)) {
1873     return gst_caps_copy (gst_static_caps_get (&octetcaps));
1874   }
1875
1876   if (caps->structs->len > 1) {
1877     return gst_caps_new_full (gst_caps_get_structure (caps, 0), NULL);
1878   }
1879
1880   newcaps = gst_caps_copy (caps);
1881   structure = gst_caps_get_structure (newcaps, 0);
1882   gst_structure_foreach (structure, _gst_pad_default_fixate_foreach,
1883       structure);
1884
1885   return newcaps;
1886 }
1887
1888 /**
1889  * gst_pad_perform_negotiate:
1890  * @srcpad: the source #GstPad.
1891  * @sinkpad: the sink #GstPad.
1892  *
1893  * Tries to negotiate the pads.
1894  *
1895  * Returns: TRUE if the pads were succesfully negotiated, FALSE otherwise.
1896  */
1897 gboolean
1898 gst_pad_perform_negotiate (GstPad *srcpad, GstPad *sinkpad) 
1899 {
1900   return GST_PAD_LINK_SUCCESSFUL (gst_pad_renegotiate (srcpad));
1901 }
1902
1903 void
1904 gst_pad_link_unnegotiate (GstPadLink *link)
1905 {
1906   g_return_if_fail (link != NULL);
1907
1908   if (link->caps) {
1909     gst_caps_free (link->caps);
1910     link->caps = NULL;
1911     if (GST_RPAD_LINK (link->srcpad) != link) {
1912       g_warning ("unnegotiating unset link");
1913     } else {
1914       g_object_notify (G_OBJECT (link->srcpad), "caps");
1915     }
1916     if (GST_RPAD_LINK (link->sinkpad) != link) {
1917       g_warning ("unnegotiating unset link");
1918     } else {
1919       g_object_notify (G_OBJECT (link->sinkpad), "caps");
1920     }
1921   }
1922 }
1923
1924 /**
1925  * gst_pad_unnegotiate:
1926  * @pad: pad to unnegotiate
1927  *
1928  * "Unnegotiates" a pad. The currently negotiated caps are cleared and the pad 
1929  * needs renegotiation.
1930  */
1931 void
1932 gst_pad_unnegotiate (GstPad *pad)
1933 {
1934   GstPadLink *link;
1935
1936   g_return_if_fail (GST_IS_PAD (pad));
1937
1938   link = GST_RPAD_LINK (GST_PAD_REALIZE (pad));
1939   if (link) 
1940     gst_pad_link_unnegotiate (link);
1941 }
1942
1943 /**
1944  * gst_pad_try_relink_filtered:
1945  * @srcpad: the source #GstPad to relink.
1946  * @sinkpad: the sink #GstPad to relink.
1947  * @filtercaps: the #GstPad to use as a filter in the relink.
1948  *
1949  * Tries to relink the given source and sink pad, constrained by the given
1950  * capabilities.
1951  *
1952  * Returns: TRUE if the pads were succesfully renegotiated, FALSE otherwise.
1953  */
1954 gboolean
1955 gst_pad_try_relink_filtered (GstPad *srcpad, GstPad *sinkpad, 
1956                                 const GstCaps *filtercaps)
1957 {
1958   GstRealPad *realsrc, *realsink;
1959   GstPadLink *link;
1960   gchar *str;
1961   
1962   /* generic checks */
1963   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1964   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1965
1966   str = filtercaps ? gst_caps_to_string (filtercaps) : g_strdup ("");
1967   GST_CAT_INFO (GST_CAT_PADS, "trying to relink %s:%s and %s:%s with filtercaps %s",
1968             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad), str);
1969   g_free (str);
1970
1971   /* now we need to deal with the real/ghost stuff */
1972   realsrc = GST_PAD_REALIZE (srcpad);
1973   realsink = GST_PAD_REALIZE (sinkpad);
1974
1975   g_return_val_if_fail (realsrc != NULL, FALSE);
1976   g_return_val_if_fail (realsink != NULL, FALSE);
1977   g_return_val_if_fail (GST_RPAD_PEER (realsrc) == realsink, FALSE);
1978   g_assert (realsrc == GST_RPAD_PEER (realsink));
1979   if ((GST_PAD (realsrc) != srcpad) || (GST_PAD (realsink) != sinkpad)) {
1980     GST_CAT_INFO (GST_CAT_PADS, "*actually* linking %s:%s and %s:%s",
1981               GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1982   }
1983   
1984   link = gst_pad_link_new ();
1985
1986   if (GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) {
1987     link->srcpad = GST_PAD (realsrc);
1988     link->sinkpad = GST_PAD (realsink);
1989   } else {
1990     link->srcpad = GST_PAD (realsink);
1991     link->sinkpad = GST_PAD (realsrc);
1992   }
1993
1994   if (GST_RPAD_DIRECTION (link->srcpad) != GST_PAD_SRC) {
1995     GST_CAT_INFO (GST_CAT_PADS, "Real src pad %s:%s is not a source pad, failed",
1996               GST_DEBUG_PAD_NAME (link->srcpad));
1997     gst_pad_link_free (link);
1998     return FALSE;
1999   }    
2000   if (GST_RPAD_DIRECTION (link->sinkpad) != GST_PAD_SINK) {
2001     GST_CAT_INFO (GST_CAT_PADS, "Real sink pad %s:%s is not a sink pad, failed",
2002               GST_DEBUG_PAD_NAME (link->sinkpad));
2003     gst_pad_link_free (link);
2004     return FALSE;
2005   }
2006
2007   link->srccaps = gst_pad_get_caps (link->srcpad);
2008   link->sinkcaps = gst_pad_get_caps (link->sinkpad);
2009   if (filtercaps) link->filtercaps = gst_caps_copy (filtercaps);
2010   if (GST_PAD_LINK_FAILED (gst_pad_link_try (link)))
2011     return FALSE;
2012
2013   return TRUE;
2014 }
2015
2016 /**
2017  * gst_pad_relink_filtered:
2018  * @srcpad: the source #GstPad to relink.
2019  * @sinkpad: the sink #GstPad to relink.
2020  * @filtercaps: the #GstPad to use as a filter in the relink.
2021  *
2022  * Relinks the given source and sink pad, constrained by the given
2023  * capabilities.  If the relink fails, the pads are unlinked
2024  * and FALSE is returned.
2025  *
2026  * Returns: TRUE if the pads were succesfully relinked, FALSE otherwise.
2027  */
2028 gboolean
2029 gst_pad_relink_filtered (GstPad *srcpad, GstPad *sinkpad, 
2030                             const GstCaps *filtercaps)
2031 {
2032   if (gst_pad_try_relink_filtered (srcpad, sinkpad, filtercaps))
2033     return TRUE;
2034
2035   gst_pad_unlink (srcpad, sinkpad);
2036   return FALSE;
2037 }
2038
2039 /**
2040  * gst_pad_proxy_getcaps:
2041  * @pad: a #GstPad to proxy.
2042  *
2043  * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2044  * same element as @pad, and returns the intersection of the results.
2045  *
2046  * This function is useful as a default getcaps function for an element
2047  * that can handle any stream format, but requires all its pads to have
2048  * the same caps.  Two such elements are tee and aggregator.
2049  *
2050  * Returns: the intersection of the other pads' allowed caps.
2051  */
2052 GstCaps *
2053 gst_pad_proxy_getcaps (GstPad *pad)
2054 {
2055   GstElement *element;
2056   const GList *pads;
2057   GstCaps *caps;
2058
2059   GST_DEBUG ("proxying getcaps for %s:%s\n", GST_DEBUG_PAD_NAME (pad));
2060
2061   element = gst_pad_get_parent (pad);
2062
2063   pads = gst_element_get_pad_list (element);
2064   
2065   caps = gst_caps_new_any ();
2066   while (pads) {
2067     GstPad *otherpad = GST_PAD (pads->data);
2068     GstCaps *temp;
2069
2070     if (otherpad != pad) {
2071       temp = gst_caps_intersect (caps, gst_pad_get_allowed_caps (otherpad));
2072       gst_caps_free (caps);
2073       caps = temp;
2074     }
2075
2076     pads = g_list_next (pads);
2077   }
2078
2079   return caps;
2080 }
2081
2082 /**
2083  * gst_pad_proxy_pad_link:
2084  * @pad: a #GstPad to proxy.
2085  *
2086  * Calls gst_pad_try_set_caps() for every other pad belonging to the
2087  * same element as @pad.  If gst_pad_try_set_caps() fails on any pad,
2088  * the proxy link fails.
2089  *
2090  * Returns: GST_PAD_LINK_OK if sucessful
2091  */
2092 GstPadLinkReturn
2093 gst_pad_proxy_pad_link (GstPad *pad, const GstCaps *caps)
2094 {
2095   GstElement *element;
2096   const GList *pads;
2097   GstPadLinkReturn ret;
2098
2099   GST_DEBUG ("proxying pad link for %s:%s\n", GST_DEBUG_PAD_NAME (pad));
2100
2101   element = gst_pad_get_parent (pad);
2102
2103   pads = gst_element_get_pad_list (element);
2104   
2105   while (pads) {
2106     GstPad *otherpad = GST_PAD (pads->data);
2107
2108     if (otherpad != pad) {
2109       ret = gst_pad_try_set_caps (otherpad, caps);
2110       if (GST_PAD_LINK_FAILED (ret)) {
2111         return ret;
2112       }
2113     }
2114     pads = g_list_next (pads);
2115   }
2116
2117   return GST_PAD_LINK_OK;
2118 }
2119
2120 /**
2121  * gst_pad_proxy_fixate:
2122  * @pad: a #GstPad to proxy.
2123  *
2124  * Implements a default fixate function based on the caps set on the other
2125  * pads in the element.  This function should only be used if every pad
2126  * has the same pad template caps.
2127  *
2128  * Returns: a fixated caps, or NULL if caps cannot be fixed
2129  */
2130 GstCaps *
2131 gst_pad_proxy_fixate (GstPad *pad, const GstCaps *caps)
2132 {
2133   GstElement *element;
2134   const GList *pads;
2135   const GstCaps *othercaps;
2136
2137   GST_DEBUG ("proxying fixate for %s:%s\n", GST_DEBUG_PAD_NAME (pad));
2138
2139   element = gst_pad_get_parent (pad);
2140
2141   pads = gst_element_get_pad_list (element);
2142
2143   while (pads) {
2144     GstPad *otherpad = GST_PAD (pads->data);
2145
2146     /* FIXME check that each pad has the same pad template caps */
2147
2148     if (otherpad != pad) {
2149       othercaps = gst_pad_get_negotiated_caps (otherpad);
2150
2151       if (othercaps) {
2152         GstCaps *icaps;
2153         icaps = gst_caps_intersect (othercaps, caps);
2154         if (!gst_caps_is_empty (icaps)) {
2155           return icaps;
2156         } else {
2157           gst_caps_free (icaps);
2158         }
2159       }
2160     }
2161     pads = g_list_next (pads);
2162   }
2163   
2164   return NULL;
2165 }
2166
2167 /**
2168  * gst_pad_set_explicit_caps:
2169  * @pad: a #GstPad to set the explicit caps of
2170  * @caps: the #GstCaps to set
2171  *
2172  * If a pad has been told to use explicit caps, this function is used
2173  * to set the explicit caps.  If @caps is NULL, the explicit caps are
2174  * unset.
2175  *
2176  * This function calls gst_pad_try_set_caps() on the pad.  If that
2177  * call fails, GST_ELEMENT_ERROR() is called to indicate a negotiation
2178  * failure.
2179  * 
2180  * Returns: TRUE if the caps were set correctly, otherwise FALSE
2181  */
2182 gboolean
2183 gst_pad_set_explicit_caps (GstPad *pad, const GstCaps *caps)
2184 {
2185   GstPadLinkReturn link_ret;
2186
2187   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2188
2189   GST_CAT_DEBUG (GST_CAT_PADS, "setting explicit caps to %s",
2190       gst_caps_to_string (caps));
2191
2192   if (caps == NULL) {
2193     GST_CAT_DEBUG (GST_CAT_PADS, "caps is NULL");
2194     gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), NULL);
2195     return TRUE;
2196   }
2197
2198   gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), gst_caps_copy(caps));
2199     
2200   if (!GST_PAD_IS_LINKED (pad)) {
2201     GST_CAT_DEBUG (GST_CAT_PADS, "pad is not linked");
2202     return TRUE;
2203   }
2204   link_ret = gst_pad_try_set_caps (pad, caps);
2205   if (link_ret == GST_PAD_LINK_REFUSED) {
2206     gst_element_error (gst_pad_get_parent (pad), CORE, PAD,
2207                        NULL,
2208                        ("failed to negotiate (try_set_caps returned REFUSED)"));
2209     return FALSE;
2210   }
2211
2212   return TRUE;
2213 }
2214
2215 static GstCaps *
2216 gst_pad_explicit_getcaps (GstPad *pad)
2217 {
2218   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2219
2220   if (GST_RPAD_EXPLICIT_CAPS (pad) == NULL) {
2221     const GstCaps *caps = gst_pad_get_pad_template_caps (pad);
2222
2223     return gst_caps_copy (caps);
2224   }
2225   return gst_caps_copy (GST_RPAD_EXPLICIT_CAPS (pad));
2226 }
2227
2228 static GstPadLinkReturn
2229 gst_pad_explicit_link (GstPad *pad, const GstCaps *caps)
2230 {
2231   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_LINK_REFUSED);
2232   g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
2233
2234   if (GST_RPAD_EXPLICIT_CAPS (pad) == NULL) {
2235     return GST_PAD_LINK_DELAYED;
2236   }
2237
2238   return GST_PAD_LINK_OK;
2239 }
2240
2241 /**
2242  * gst_pad_use_explicit_caps:
2243  * @pad: a #GstPad to set to use explicit caps
2244  *
2245  * This function handles negotiation for pads that need to be set
2246  * to particular caps under complete control of the element, based
2247  * on some state in the element.  This is often the case with
2248  * decoders and other elements whose caps is determined by the data
2249  * stream.
2250  *
2251  * WARNING: This function is a hack and will be replaced with something
2252  * better in gstreamer-0.9.
2253  */
2254 void
2255 gst_pad_use_explicit_caps (GstPad *pad)
2256 {
2257   g_return_if_fail (GST_IS_PAD (pad));
2258
2259   gst_pad_set_getcaps_function (pad, gst_pad_explicit_getcaps);
2260   gst_pad_set_link_function (pad, gst_pad_explicit_link);
2261   gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), NULL);
2262 }
2263
2264 /**
2265  * gst_pad_proxy_link:
2266  * @pad: a #GstPad to proxy to.
2267  * @caps: the #GstCaps to use in proxying.
2268  *
2269  * Proxies the link function to the specified pad.
2270  *
2271  * Returns: TRUE if the peer pad accepted the caps, FALSE otherwise.
2272  */
2273 GstPadLinkReturn
2274 gst_pad_proxy_link (GstPad *pad, const GstCaps *caps)
2275 {
2276   return gst_pad_try_set_caps (pad, caps);
2277 }
2278
2279 /**
2280  * gst_pad_is_negotiated:
2281  * @pad: a #GstPad to get the negotiation status of
2282  *
2283  * Returns: TRUE if the pad has successfully negotiated caps.
2284  */
2285 gboolean
2286 gst_pad_is_negotiated (GstPad *pad)
2287 {
2288   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2289
2290   if (!GST_PAD_REALIZE (pad))
2291     return FALSE;
2292   if (!GST_RPAD_LINK (pad))
2293     return FALSE;
2294
2295   return (GST_RPAD_LINK (pad)->caps != NULL);
2296 }
2297
2298 /**
2299  * gst_pad_get_negotiated_caps:
2300  * @pad: a #GstPad to get the negotiated capabilites of
2301  *
2302  * Gets the currently negotiated caps of a pad or NULL if the pad isn't
2303  * negotiated.
2304  *
2305  * Returns: the currently negotiated caps of a pad or NULL if the pad isn't
2306  *          negotiated.
2307  */
2308 G_CONST_RETURN GstCaps *
2309 gst_pad_get_negotiated_caps (GstPad *pad)
2310 {
2311   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2312
2313   if (!GST_PAD_REALIZE (pad))
2314     return NULL;
2315   if (!GST_RPAD_LINK (pad))
2316     return NULL;
2317
2318   return GST_RPAD_LINK (pad)->caps;
2319 }
2320
2321 /**
2322  * gst_pad_get_caps:
2323  * @pad: a  #GstPad to get the capabilities of.
2324  *
2325  * Gets the capabilities of this pad.
2326  *
2327  * Returns: the #GstCaps of this pad. This function returns a new caps, so use 
2328  * gst_caps_free to get rid of it.
2329  */
2330 GstCaps*
2331 gst_pad_get_caps (GstPad *pad)
2332 {
2333   GstRealPad *realpad;
2334
2335   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2336
2337   realpad = GST_PAD_REALIZE (pad);
2338
2339   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
2340             GST_DEBUG_PAD_NAME (realpad), realpad);
2341
2342   if (GST_RPAD_GETCAPSFUNC (realpad)) {
2343     GstCaps *caps;
2344
2345     GST_CAT_DEBUG (GST_CAT_CAPS, "using pad get function");
2346     caps = GST_RPAD_GETCAPSFUNC (realpad) (GST_PAD (realpad));
2347
2348     if (caps == NULL) {
2349       g_critical ("pad %s:%s returned NULL caps from getcaps function\n",
2350           GST_ELEMENT_NAME(GST_PAD_PARENT(GST_PAD (realpad))),
2351           GST_PAD_NAME(realpad));
2352       caps = gst_caps_new_any ();
2353     }
2354
2355     return caps;
2356   } else if (GST_PAD_PAD_TEMPLATE (realpad)) {
2357     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (realpad);
2358     const GstCaps *caps;
2359
2360     GST_CAT_DEBUG (GST_CAT_CAPS, "using pad template %p with caps %p", 
2361                templ, GST_PAD_TEMPLATE_CAPS (templ));
2362
2363     caps = GST_PAD_TEMPLATE_CAPS (templ);
2364 #if 0
2365     /* FIXME we should enable something like this someday, but this is
2366      * a bit buggy */
2367     if (!gst_caps_is_fixed (caps)) {
2368       g_warning("pad %s:%s (%p) has no getcaps function and the pad template returns non-fixed caps.  Element is probably broken.\n",
2369           GST_DEBUG_PAD_NAME (realpad), realpad);
2370     }
2371 #endif
2372
2373     return gst_caps_copy (GST_PAD_TEMPLATE_CAPS (templ));
2374   }
2375   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2376
2377 #if 0
2378   /* FIXME enable */
2379   g_warning("pad %s:%s (%p) has no pad template\n",
2380       GST_DEBUG_PAD_NAME (realpad), realpad);
2381 #endif
2382
2383   return gst_caps_new_any();
2384 }
2385
2386 /**
2387  * gst_pad_get_pad_template_caps:
2388  * @pad: a #GstPad to get the template capabilities from.
2389  *
2390  * Gets the template capabilities of this pad.
2391  *
2392  * Returns: the template #GstCaps of this pad, unref the caps
2393  * if you no longer need it.
2394  */
2395 const GstCaps*
2396 gst_pad_get_pad_template_caps (GstPad *pad)
2397 {
2398   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2399   g_return_val_if_fail (pad != NULL, NULL);
2400   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2401
2402   if (GST_PAD_PAD_TEMPLATE (pad))
2403     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2404
2405 #if 0
2406   /* FIXME this should be enabled some day */
2407   g_warning("pad %s:%s (%p) has no pad template\n",
2408       GST_DEBUG_PAD_NAME (realpad), realpad);
2409 #endif
2410
2411   return gst_static_caps_get(&anycaps);
2412 }
2413
2414 /**
2415  * gst_pad_template_get_caps_by_name:
2416  * @templ: a #GstPadTemplate to get the capabilities of.
2417  * @name: the name of the capability to get.
2418  *
2419  * Gets the capability with the given name from this pad template.
2420  *
2421  * Returns: the #GstCaps, or NULL if not found or in case of an error. unref 
2422  * the caps if you no longer need it.
2423  */
2424 const GstCaps*
2425 gst_pad_template_get_caps_by_name (GstPadTemplate *templ, const gchar *name)
2426 {
2427   GstCaps *caps;
2428
2429   g_return_val_if_fail (templ != NULL, NULL);
2430
2431   caps = GST_PAD_TEMPLATE_CAPS (templ);
2432   if (!caps) 
2433     return NULL;
2434
2435   /* FIXME */
2436   //return gst_caps_copy (gst_caps_get_by_name (caps, name));
2437   return NULL;
2438 }
2439
2440 /**
2441  * gst_pad_check_compatibility:
2442  * @srcpad: the source #GstPad to check.
2443  * @sinkpad: the sink #GstPad to check against.
2444  *
2445  * Checks if two pads have compatible capabilities.
2446  *
2447  * Returns: TRUE if they are compatible or if the capabilities
2448  * could not be checked
2449  */
2450 gboolean
2451 gst_pad_check_compatibility (GstPad *srcpad, GstPad *sinkpad)
2452 {
2453   g_return_val_if_fail (srcpad != NULL, FALSE);
2454   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2455   g_return_val_if_fail (sinkpad != NULL, FALSE);
2456   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2457
2458   if (GST_PAD_CAPS (srcpad) && GST_PAD_CAPS (sinkpad)) {
2459     if (!gst_caps_is_always_compatible (GST_PAD_CAPS (srcpad), 
2460                                         GST_PAD_CAPS (sinkpad))) {
2461       return FALSE;
2462     }
2463     else {
2464       return TRUE;
2465     }
2466   }
2467   else {
2468     GST_CAT_DEBUG (GST_CAT_PADS, 
2469                "could not check capabilities of pads (%s:%s) and (%s:%s) %p %p",
2470                GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad), 
2471                GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad));
2472     return TRUE;
2473   }
2474 }
2475
2476 /**
2477  * gst_pad_get_peer:
2478  * @pad: a #GstPad to get the peer of.
2479  *
2480  * Gets the peer pad of this pad.
2481  *
2482  * Returns: the peer #GstPad.
2483  */
2484 GstPad*
2485 gst_pad_get_peer (GstPad *pad)
2486 {
2487   g_return_val_if_fail (pad != NULL, NULL);
2488   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2489
2490   return GST_PAD (GST_PAD_PEER (pad));
2491 }
2492
2493 /**
2494  * gst_pad_get_allowed_caps:
2495  * @pad: a #GstPad to get the allowed caps of.
2496  *
2497  * Gets the capabilities of the allowed media types that can
2498  * flow through this pad.  The caller must free the resulting caps.
2499  *
2500  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2501  * you no longer need it.
2502  */
2503 GstCaps*
2504 gst_pad_get_allowed_caps (GstPad *pad)
2505 {
2506   const GstCaps *mycaps;
2507   GstCaps *caps;
2508   GstCaps *peercaps;
2509   GstCaps *icaps;
2510   GstPadLink *link;
2511
2512   g_return_val_if_fail (pad != NULL, NULL);
2513   g_return_val_if_fail (GST_IS_REAL_PAD (pad), NULL);
2514
2515   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "get allowed caps of %s:%s", 
2516              GST_DEBUG_PAD_NAME (pad));
2517
2518   mycaps = gst_pad_get_pad_template_caps (pad);
2519   if (GST_RPAD_PEER (pad) == NULL) {
2520     return gst_caps_copy (mycaps);
2521   }
2522
2523   peercaps = gst_pad_get_caps (GST_PAD_PEER (pad));
2524   caps = gst_caps_intersect (mycaps, peercaps);
2525   gst_caps_free (peercaps);
2526
2527   link = GST_RPAD_LINK (pad);
2528   if (link->filtercaps) {
2529     icaps = gst_caps_intersect (caps, link->filtercaps);
2530     gst_caps_free (caps);
2531     return icaps;
2532   } else {
2533     return caps;
2534   }
2535 }
2536
2537 void
2538 gst_pad_caps_change_notify (GstPad *pad)
2539 {
2540   /* call this to indicate that the return value of getcaps may have
2541    * changed, and a renegotiation is suggested */
2542 }
2543
2544 /**
2545  * gst_pad_recover_caps_error:
2546  * @pad: a #GstPad that had a failed capsnego
2547  * @allowed: possible caps for the link
2548  *
2549  * Attempt to recover from a failed caps negotiation. This function
2550  * is typically called by a plugin that exhausted its list of caps
2551  * and wants the application to resolve the issue. The application
2552  * should connect to the pad's caps_nego_failed signal and should
2553  * resolve the issue by connecting another element for example.
2554  *
2555  * Returns: TRUE when the issue was resolved, dumps detailed information
2556  * on the console and returns FALSE otherwise.
2557  */
2558 gboolean
2559 gst_pad_recover_caps_error (GstPad *pad, const GstCaps *allowed)
2560 {
2561 #if 0
2562   /* FIXME */
2563   GstElement *parent;
2564   
2565   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2566
2567   /* see if someone can resolve this */
2568   if (g_signal_has_handler_pending (G_OBJECT (pad), 
2569         gst_real_pad_signals[REAL_CAPS_NEGO_FAILED], 0, FALSE))
2570   {
2571     /* clear pad caps first */
2572     gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
2573
2574     /* lets hope some signal manages to set the caps again */
2575     g_signal_emit (G_OBJECT (pad), gst_real_pad_signals[REAL_CAPS_NEGO_FAILED], 0, allowed);
2576
2577     /* if the pad has caps now or is disabled, it's ok */
2578     if (GST_PAD_CAPS (pad) != NULL || !GST_PAD_IS_ACTIVE (pad))
2579       return TRUE;
2580   }
2581
2582   /* report error */
2583   parent = gst_pad_get_parent (pad);
2584   gst_element_error (parent, CORE, PAD,
2585                      NULL,
2586                      ("negotiation failed on pad %s:%s", GST_DEBUG_PAD_NAME (pad)));
2587 #endif
2588   return FALSE;
2589 }
2590
2591 /**
2592  * gst_pad_alloc_buffer:
2593  * @pad: a #GstPad to get the buffer from.
2594  *
2595  * Allocates a new, empty buffer optimized to push to pad #pad.  This
2596  * function only works if #pad is a src pad.
2597  *
2598  * Returns: a new, empty #GstBuffer, or NULL if there is an error
2599  */
2600 GstBuffer*          
2601 gst_pad_alloc_buffer (GstPad *pad, guint64 offset, gint size)
2602 {
2603   GstRealPad *peer;
2604
2605   g_return_val_if_fail (pad != NULL, NULL);
2606   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2607   g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
2608    
2609   peer = GST_RPAD_PEER (pad);
2610
2611   if (!peer)
2612     return gst_buffer_new_and_alloc(size);
2613
2614   GST_CAT_DEBUG (GST_CAT_BUFFER, "(%s:%s): getting buffer",
2615       GST_DEBUG_PAD_NAME (pad));
2616
2617   if (peer->bufferallocfunc) {
2618     GST_CAT_DEBUG (GST_CAT_PADS, 
2619                "calling bufferallocfunc &%s (@%p) of peer pad %s:%s",
2620                GST_DEBUG_FUNCPTR_NAME (peer->bufferallocfunc), 
2621                &peer->bufferallocfunc, GST_DEBUG_PAD_NAME (((GstPad*) peer)));
2622     return (peer->bufferallocfunc) (GST_PAD (peer), offset, size);
2623   } else {
2624     return gst_buffer_new_and_alloc(size);
2625   }
2626 }
2627
2628 static void
2629 gst_real_pad_dispose (GObject *object)
2630 {
2631   GstPad *pad = GST_PAD (object);
2632   
2633   /* No linked pad can ever be disposed.
2634    * It has to have a parent to be linked 
2635    * and a parent would hold a reference */
2636   g_assert (GST_PAD_PEER (pad) == NULL);
2637
2638   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s", GST_DEBUG_PAD_NAME(pad));
2639
2640   /* we destroy the ghostpads, because they are nothing without the real pad */
2641   if (GST_REAL_PAD (pad)->ghostpads) {
2642     GList *orig, *ghostpads;
2643
2644     orig = ghostpads = g_list_copy (GST_REAL_PAD (pad)->ghostpads);
2645
2646     while (ghostpads) {
2647       GstPad *ghostpad = GST_PAD (ghostpads->data);
2648
2649       if (GST_IS_ELEMENT (GST_OBJECT_PARENT (ghostpad))){
2650         GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "removing ghost pad from element '%s'", 
2651                    GST_OBJECT_NAME (GST_OBJECT_PARENT (ghostpad)));
2652
2653         gst_element_remove_ghost_pad (GST_ELEMENT (GST_OBJECT_PARENT (ghostpad)), GST_PAD (ghostpad));
2654       }
2655       ghostpads = g_list_next (ghostpads);
2656     }
2657     g_list_free (orig);
2658     g_list_free (GST_REAL_PAD(pad)->ghostpads);
2659   }
2660
2661   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))) {
2662     GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'",
2663                GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
2664     
2665     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
2666   }
2667   
2668   G_OBJECT_CLASS (real_pad_parent_class)->dispose (object);
2669 }
2670
2671
2672 #ifndef GST_DISABLE_LOADSAVE
2673 /* FIXME: why isn't this on a GstElement ? */
2674 /**
2675  * gst_pad_load_and_link:
2676  * @self: an #xmlNodePtr to read the description from.
2677  * @parent: the #GstObject element that owns the pad.
2678  *
2679  * Reads the pad definition from the XML node and links the given pad
2680  * in the element to a pad of an element up in the hierarchy.
2681  */
2682 void
2683 gst_pad_load_and_link (xmlNodePtr self, GstObject *parent)
2684 {
2685   xmlNodePtr field = self->xmlChildrenNode;
2686   GstPad *pad = NULL, *targetpad;
2687   gchar *peer = NULL;
2688   gchar **split;
2689   GstElement *target;
2690   GstObject *grandparent;
2691
2692   while (field) {
2693     if (!strcmp (field->name, "name")) {
2694       pad = gst_element_get_pad (GST_ELEMENT (parent), 
2695                                  xmlNodeGetContent (field));
2696     }
2697     else if (!strcmp(field->name, "peer")) {
2698       peer = xmlNodeGetContent (field);
2699     }
2700     field = field->next;
2701   }
2702   g_return_if_fail (pad != NULL);
2703
2704   if (peer == NULL) return;
2705
2706   split = g_strsplit (peer, ".", 2);
2707
2708   if (split[0] == NULL || split[1] == NULL) {
2709     GST_CAT_DEBUG (GST_CAT_XML, 
2710                "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2711                peer, GST_DEBUG_PAD_NAME (pad));
2712     return;
2713   }
2714   
2715   g_return_if_fail (split[0] != NULL);
2716   g_return_if_fail (split[1] != NULL);
2717
2718   grandparent = gst_object_get_parent (parent);
2719
2720   if (grandparent && GST_IS_BIN (grandparent)) {
2721     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2722   }
2723   else
2724     goto cleanup;
2725
2726   if (target == NULL) goto cleanup;
2727
2728   targetpad = gst_element_get_pad (target, split[1]);
2729
2730   if (targetpad == NULL) goto cleanup;
2731
2732   gst_pad_link (pad, targetpad);
2733
2734 cleanup:
2735   g_strfreev (split);
2736 }
2737
2738 /**
2739  * gst_pad_save_thyself:
2740  * @pad: a #GstPad to save.
2741  * @parent: the parent #xmlNodePtr to save the description in.
2742  *
2743  * Saves the pad into an xml representation.
2744  *
2745  * Returns: the #xmlNodePtr representation of the pad.
2746  */
2747 static xmlNodePtr
2748 gst_pad_save_thyself (GstObject *object, xmlNodePtr parent)
2749 {
2750   GstRealPad *realpad;
2751   GstPad *peer;
2752
2753   g_return_val_if_fail (GST_IS_REAL_PAD (object), NULL);
2754
2755   realpad = GST_REAL_PAD (object);
2756
2757   xmlNewChild (parent, NULL, "name", GST_PAD_NAME (realpad));
2758   if (GST_RPAD_PEER (realpad) != NULL) {
2759     gchar *content;
2760     
2761     peer = GST_PAD (GST_RPAD_PEER (realpad));
2762     /* first check to see if the peer's parent's parent is the same */
2763     /* we just save it off */
2764     content = g_strdup_printf ("%s.%s",
2765                                GST_OBJECT_NAME (GST_PAD_PARENT (peer)),
2766                                GST_PAD_NAME (peer));
2767     xmlNewChild (parent, NULL, "peer", content);
2768     g_free (content);
2769   } else
2770     xmlNewChild (parent, NULL, "peer", "");
2771
2772   return parent;
2773 }
2774
2775 /* FIXME: shouldn't pad and ghost be switched ?
2776  */
2777 /**
2778  * gst_ghost_pad_save_thyself:
2779  * @pad: a ghost #GstPad to save.
2780  * @parent: the parent #xmlNodePtr to save the description in.
2781  *
2782  * Saves the ghost pad into an xml representation.
2783  *
2784  * Returns: the #xmlNodePtr representation of the pad.
2785  */
2786 xmlNodePtr
2787 gst_ghost_pad_save_thyself (GstPad *pad, xmlNodePtr parent)
2788 {
2789   xmlNodePtr self;
2790
2791   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
2792
2793   self = xmlNewChild (parent, NULL, "ghostpad", NULL);
2794   xmlNewChild (self, NULL, "name", GST_PAD_NAME (pad));
2795   xmlNewChild (self, NULL, "parent", GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
2796
2797   /* FIXME FIXME FIXME! */
2798
2799   return self;
2800 }
2801 #endif /* GST_DISABLE_LOADSAVE */
2802
2803 /**
2804  * gst_pad_push:
2805  * @pad: a #GstPad to push the buffer out of.
2806  * @data: the #GstData to push.
2807  *
2808  * Pushes a buffer or an event to the peer of the pad.
2809  */
2810 void 
2811 gst_pad_push (GstPad *pad, GstData *data) 
2812 {
2813   GstRealPad *peer;
2814
2815   g_return_if_fail (GST_IS_PAD (pad));
2816   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
2817   g_return_if_fail (data != NULL);
2818
2819   if (!gst_probe_dispatcher_dispatch (&(GST_REAL_PAD (pad)->probedisp), &data))
2820     return;
2821   
2822   if (!GST_PAD_IS_LINKED (pad)) {
2823     GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad, "not pushing data %p as pad is unconnected", data);
2824     gst_data_unref (data);
2825     return;
2826   }      
2827
2828   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad, "pushing");
2829   peer = GST_RPAD_PEER (pad);
2830
2831   if (!peer) {
2832     g_warning ("push on pad %s:%s but it is unlinked", 
2833                GST_DEBUG_PAD_NAME (pad));
2834   }
2835   else {
2836     if (!GST_IS_EVENT (data) && !GST_PAD_IS_ACTIVE (peer)) {
2837       g_warning ("push on peer of pad %s:%s but peer is not active", 
2838                  GST_DEBUG_PAD_NAME (pad));
2839       return;
2840     }
2841
2842     if (peer->chainhandler) {
2843       if (data) {
2844         GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad,
2845                    "calling chainhandler &%s of peer pad %s:%s",
2846                    GST_DEBUG_FUNCPTR_NAME (peer->chainhandler), 
2847                    GST_DEBUG_PAD_NAME (GST_PAD (peer)));
2848         if (!gst_probe_dispatcher_dispatch (&peer->probedisp, &data))
2849           return;
2850
2851         (peer->chainhandler) (GST_PAD (peer), data);
2852         return;
2853       }
2854       else {
2855         g_warning ("trying to push a NULL buffer on pad %s:%s", 
2856                    GST_DEBUG_PAD_NAME (peer));
2857         return;
2858       }
2859     } 
2860     else {
2861       g_warning ("internal error: push on pad %s:%s but it has no chainhandler",
2862                  GST_DEBUG_PAD_NAME (peer));
2863     }
2864   }
2865   /* clean up the mess here */
2866   if (data != NULL) gst_data_unref (data);
2867 }
2868
2869 /**
2870  * gst_pad_pull:
2871  * @pad: a #GstPad to pull a buffer from.
2872  *
2873  * Pulls an event or a buffer from the peer pad.
2874  *
2875  * Returns: a new #GstData from the peer pad.
2876  */
2877 GstData*
2878 gst_pad_pull (GstPad *pad) 
2879 {
2880   GstRealPad *peer;
2881   
2882   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad, "pulling");
2883
2884   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
2885                         GST_DATA (gst_event_new (GST_EVENT_INTERRUPT)));
2886
2887   peer = GST_RPAD_PEER (pad);
2888
2889   if (!peer) {
2890     gst_element_error (GST_PAD_PARENT (pad), CORE, PAD, NULL,
2891                        ("pull on pad %s:%s but it was unlinked", GST_DEBUG_PAD_NAME (pad)));
2892   }
2893   else {
2894 restart:
2895     if (peer->gethandler) {
2896       GstData *data;
2897
2898       GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad,
2899                           "calling gethandler %s of peer pad %s:%s",
2900                           GST_DEBUG_FUNCPTR_NAME (peer->gethandler), 
2901                           GST_DEBUG_PAD_NAME (peer));
2902
2903       data = (peer->gethandler) (GST_PAD (peer));
2904
2905       if (data) {
2906         if (!gst_probe_dispatcher_dispatch (&peer->probedisp, &data))
2907           goto restart;
2908         return data;
2909       }
2910
2911       /* no null buffers allowed */
2912       gst_element_error (GST_PAD_PARENT (pad), CORE, PAD, NULL,
2913                        ("NULL buffer during pull on %s:%s", GST_DEBUG_PAD_NAME (pad)));
2914     } else {
2915       gst_element_error (GST_PAD_PARENT (pad), CORE, PAD, NULL,
2916                        ("pull on pad %s:%s but the peer pad %s:%s has no gethandler",
2917                          GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer)));
2918     }
2919   }
2920   return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
2921 }
2922
2923 /**
2924  * gst_pad_selectv:
2925  * @padlist: a #GList of pads.
2926  *
2927  * Waits for a buffer on any of the list of pads.
2928  *
2929  * Returns: the #GstPad that has a buffer available. 
2930  * Use #gst_pad_pull() to get the buffer.
2931  */
2932 GstPad*
2933 gst_pad_selectv (GList *padlist)
2934 {
2935   GstPad *pad;
2936
2937   pad = gst_scheduler_pad_select (GST_PAD_PARENT (padlist->data)->sched, 
2938                                   padlist);
2939   return pad;
2940 }
2941
2942 /**
2943  * gst_pad_select:
2944  * @pad: a first #GstPad to perform the select on.
2945  * @...: A NULL-terminated list of more pads to select on.
2946  *
2947  * Waits for a buffer on the given set of pads.
2948  *
2949  * Returns: the #GstPad that has a buffer available.
2950  * Use #gst_pad_pull() to get the buffer.
2951  */
2952 GstPad*
2953 gst_pad_select (GstPad *pad, ...)
2954 {
2955   GstPad *result;
2956   va_list var_args;
2957
2958   if (pad == NULL)
2959     return NULL;
2960
2961   va_start (var_args, pad);
2962
2963   result = gst_pad_select_valist (pad, var_args);
2964
2965   va_end (var_args);
2966   
2967   return result;
2968 }
2969
2970 /**
2971  * gst_pad_select_valist:
2972  * @pad: a first #GstPad to perform the select on.
2973  * @varargs: A va_list of more pads to select on.
2974  *
2975  * Waits for a buffer on the given set of pads.
2976  *
2977  * Returns: the #GstPad that has a buffer available.
2978  * Use #gst_pad_pull() to get the buffer.
2979  */
2980 GstPad*
2981 gst_pad_select_valist (GstPad *pad, va_list var_args)
2982 {
2983   GstPad *result;
2984   GList *padlist = NULL;
2985
2986   if (pad == NULL)
2987     return NULL;
2988
2989   while (pad) {
2990     padlist = g_list_prepend (padlist, pad);
2991     pad = va_arg (var_args, GstPad *);
2992   }
2993   result = gst_pad_selectv (padlist);
2994   g_list_free (padlist);
2995
2996   return result;
2997 }
2998
2999 /************************************************************************
3000  *
3001  * templates
3002  *
3003  */
3004 static void             gst_pad_template_class_init     (GstPadTemplateClass *klass);
3005 static void             gst_pad_template_init           (GstPadTemplate *templ);
3006 static void             gst_pad_template_dispose        (GObject *object);
3007
3008 GType
3009 gst_pad_template_get_type (void)
3010 {
3011   static GType padtemplate_type = 0;
3012
3013   if (!padtemplate_type) {
3014     static const GTypeInfo padtemplate_info = {
3015       sizeof (GstPadTemplateClass), NULL, NULL,
3016       (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
3017       sizeof (GstPadTemplate),
3018       32,
3019       (GInstanceInitFunc) gst_pad_template_init, NULL
3020     };
3021     padtemplate_type = g_type_register_static(GST_TYPE_OBJECT, "GstPadTemplate",
3022                                               &padtemplate_info, 0);
3023   }
3024   return padtemplate_type;
3025 }
3026
3027 static void
3028 gst_pad_template_class_init (GstPadTemplateClass *klass)
3029 {
3030   GObjectClass *gobject_class;
3031   GstObjectClass *gstobject_class;
3032
3033   gobject_class = (GObjectClass*) klass;
3034   gstobject_class = (GstObjectClass*) klass;
3035
3036   padtemplate_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
3037
3038   gst_pad_template_signals[TEMPL_PAD_CREATED] =
3039     g_signal_new ("pad_created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
3040                   G_STRUCT_OFFSET (GstPadTemplateClass, pad_created), 
3041                   NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
3042                   GST_TYPE_PAD);
3043
3044   gobject_class->dispose = gst_pad_template_dispose;
3045
3046   gstobject_class->path_string_separator = "*";
3047 }
3048
3049 static void
3050 gst_pad_template_init (GstPadTemplate *templ)
3051 {
3052 }
3053
3054 static void
3055 gst_pad_template_dispose (GObject *object)
3056 {
3057   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
3058
3059   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
3060   if (GST_PAD_TEMPLATE_CAPS (templ)) {
3061     gst_caps_free (GST_PAD_TEMPLATE_CAPS (templ));
3062   }
3063
3064   G_OBJECT_CLASS (padtemplate_parent_class)->dispose (object);
3065 }
3066
3067 /* ALWAYS padtemplates cannot have conversion specifications, it doesn't make
3068  * sense.
3069  * SOMETIMES padtemplates can do whatever they want, they are provided by the
3070  * element.
3071  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
3072  * 'sink%d' template is automatically selected), so we need to restrict their
3073  * naming.
3074  */
3075 static gboolean
3076 name_is_valid (const gchar *name, GstPadPresence presence)
3077 {
3078   const gchar *str;
3079   
3080   if (presence == GST_PAD_ALWAYS) {
3081     if (strchr (name, '%')) {
3082       g_warning ("invalid name template %s: conversion specifications are not"
3083                  " allowed for GST_PAD_ALWAYS padtemplates", name);
3084       return FALSE;
3085     }
3086   } else if (presence == GST_PAD_REQUEST) {
3087     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
3088       g_warning ("invalid name template %s: only one conversion specification"
3089                  " allowed in GST_PAD_REQUEST padtemplate", name);
3090       return FALSE;
3091     }
3092     if (str && (*(str+1) != 's' && *(str+1) != 'd')) {
3093       g_warning ("invalid name template %s: conversion specification must be of"
3094                  " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
3095       return FALSE;
3096     }
3097     if (str && (*(str+2) != '\0')) {
3098       g_warning ("invalid name template %s: conversion specification must"
3099                  " appear at the end of the GST_PAD_REQUEST padtemplate name", 
3100                  name);
3101       return FALSE;
3102     }
3103   }
3104   
3105   return TRUE;
3106 }
3107
3108 /**
3109  * gst_static_pad_template_get:
3110  * @pad_template: the static pad template
3111  *
3112  * Converts a GstStaticPadTemplate into a GstPadTemplate.
3113  *
3114  * Returns: a new #GstPadTemplate.
3115  */
3116 GstPadTemplate*
3117 gst_static_pad_template_get (GstStaticPadTemplate *pad_template)
3118 {
3119   GstPadTemplate *new;
3120
3121   if (!name_is_valid (pad_template->name_template, pad_template->presence))
3122     return NULL;
3123
3124   new = g_object_new (gst_pad_template_get_type (),
3125                       "name", pad_template->name_template,
3126                       NULL);
3127
3128   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) =
3129     g_strdup (pad_template->name_template);
3130   GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
3131   GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
3132
3133   GST_PAD_TEMPLATE_CAPS (new) = gst_caps_copy (
3134       gst_static_caps_get (&pad_template->static_caps));
3135
3136   return new;
3137 }
3138
3139 /**
3140  * gst_pad_template_new:
3141  * @name_template: the name template.
3142  * @direction: the #GstPadDirection of the template.
3143  * @presence: the #GstPadPresence of the pad.
3144  * @caps: a #GstCaps set for the template. The caps are taken ownership of.
3145  *
3146  * Creates a new pad template with a name according to the given template
3147  * and with the given arguments. This functions takes ownership of the provided
3148  * caps, so be sure to not use them afterwards.
3149  *
3150  * Returns: a new #GstPadTemplate.
3151  */
3152 GstPadTemplate*
3153 gst_pad_template_new (const gchar *name_template,
3154                      GstPadDirection direction, GstPadPresence presence,
3155                      GstCaps *caps)
3156 {
3157   GstPadTemplate *new;
3158
3159   g_return_val_if_fail (name_template != NULL, NULL);
3160
3161   if (!name_is_valid (name_template, presence))
3162     return NULL;
3163
3164 #if 0
3165 #ifdef USE_POISONING
3166   if (caps) {
3167     GstCaps *newcaps = gst_caps_copy (caps);
3168     gst_caps_free(caps);
3169     caps = newcaps;
3170   }
3171 #endif
3172 #endif
3173   new = g_object_new (gst_pad_template_get_type (),
3174                       "name", name_template,
3175                       NULL);
3176
3177   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
3178   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
3179   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
3180   GST_PAD_TEMPLATE_CAPS (new) = caps;
3181
3182   return new;
3183 }
3184
3185 /**
3186  * gst_pad_template_get_caps:
3187  * @templ: a #GstPadTemplate to get capabilities of.
3188  *
3189  * Gets the capabilities of the pad template.
3190  *
3191  * Returns: the #GstCaps of the pad template. unref the caps
3192  * after use.
3193  */
3194 const GstCaps*
3195 gst_pad_template_get_caps (GstPadTemplate *templ)
3196 {
3197   g_return_val_if_fail (templ != NULL, NULL);
3198
3199   return GST_PAD_TEMPLATE_CAPS (templ);
3200 }
3201
3202 /**
3203  * gst_pad_set_element_private:
3204  * @pad: the #GstPad to set the private data of.
3205  * @priv: The private data to attach to the pad.
3206  *
3207  * Set the given private data gpointer on the pad. 
3208  * This function can only be used by the element that owns the pad.
3209  */
3210 void
3211 gst_pad_set_element_private (GstPad *pad, gpointer priv)
3212 {
3213   pad->element_private = priv;
3214 }
3215
3216 /**
3217  * gst_pad_get_element_private:
3218  * @pad: the #GstPad to get the private data of.
3219  *
3220  * Gets the private data of a pad.
3221  *
3222  * Returns: a #gpointer to the private data.
3223  */
3224 gpointer
3225 gst_pad_get_element_private (GstPad *pad)
3226 {
3227   return pad->element_private;
3228 }
3229
3230
3231 /***** ghost pads *****/
3232 GType _gst_ghost_pad_type = 0;
3233
3234 static void     gst_ghost_pad_class_init        (GstGhostPadClass *klass);
3235 static void     gst_ghost_pad_init              (GstGhostPad *pad);
3236 static void     gst_ghost_pad_dispose           (GObject *object);
3237
3238 static GstPad *ghost_pad_parent_class = NULL;
3239 /* static guint gst_ghost_pad_signals[LAST_SIGNAL] = { 0 }; */
3240
3241 GType
3242 gst_ghost_pad_get_type (void) 
3243 {
3244   if (!_gst_ghost_pad_type) {
3245     static const GTypeInfo pad_info = {
3246       sizeof (GstGhostPadClass), NULL, NULL,
3247       (GClassInitFunc) gst_ghost_pad_class_init, NULL, NULL,
3248       sizeof (GstGhostPad),
3249       8,
3250       (GInstanceInitFunc) gst_ghost_pad_init,
3251       NULL
3252     };
3253     _gst_ghost_pad_type = g_type_register_static (GST_TYPE_PAD, "GstGhostPad", 
3254                                                   &pad_info, 0);
3255   }
3256   return _gst_ghost_pad_type;
3257 }
3258
3259 static void
3260 gst_ghost_pad_class_init (GstGhostPadClass *klass)
3261 {
3262   GObjectClass *gobject_class;
3263
3264   gobject_class = (GObjectClass*) klass;
3265
3266   ghost_pad_parent_class = g_type_class_ref (GST_TYPE_PAD);
3267
3268   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ghost_pad_dispose);
3269 }
3270
3271 static void
3272 gst_ghost_pad_init (GstGhostPad *pad)
3273 {
3274   /* zeroed by glib */
3275 }
3276 static void
3277 gst_ghost_pad_dispose (GObject *object)
3278 {
3279   GstGhostPad *pad = GST_GHOST_PAD (object);
3280
3281   if (pad->realpad)
3282     gst_pad_remove_ghost_pad((GstPad *) pad->realpad, (GstPad *) pad);
3283
3284   G_OBJECT_CLASS (ghost_pad_parent_class)->dispose (object);
3285 }
3286
3287 /**
3288  * gst_ghost_pad_new:
3289  * @name: the name of the new ghost pad.
3290  * @pad: the #GstPad to create a ghost pad for.
3291  *
3292  * Creates a new ghost pad associated with the given pad, and names it with
3293  * the given name.  If name is NULL, a guaranteed unique name (across all
3294  * ghost pads) will be assigned (most likely of the form ghostpad%d).
3295  *
3296  * Returns: a new ghost #GstPad, or NULL in case of an error.
3297  */
3298
3299 GstPad*
3300 gst_ghost_pad_new (const gchar *name,
3301                    GstPad *pad)
3302 {
3303   GstGhostPad *ghostpad;
3304   GstRealPad *realpad;
3305
3306   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3307
3308   ghostpad = g_object_new (gst_ghost_pad_get_type () ,NULL);
3309   gst_pad_set_name (GST_PAD (ghostpad), name);
3310
3311   realpad = (GstRealPad *) pad;
3312
3313   while (!GST_IS_REAL_PAD (realpad)) {
3314     realpad = GST_PAD_REALIZE (realpad);
3315   }
3316   GST_GPAD_REALPAD (ghostpad) = realpad;
3317   gst_pad_set_pad_template (GST_PAD (ghostpad), GST_PAD_PAD_TEMPLATE (pad));
3318
3319   /* add ourselves to the real pad's list of ghostpads */
3320   gst_pad_add_ghost_pad (pad, GST_PAD (ghostpad));
3321
3322   /* FIXME need to ref the real pad here... ? */
3323
3324   GST_CAT_DEBUG (GST_CAT_PADS, "created ghost pad \"%s\"", 
3325              gst_pad_get_name (GST_PAD (ghostpad)));
3326
3327   return GST_PAD (ghostpad);
3328 }
3329
3330 /**
3331  * gst_pad_get_internal_links_default:
3332  * @pad: the #GstPad to get the internal links of.
3333  *
3334  * Gets a list of pads to which the given pad is linked to
3335  * inside of the parent element.
3336  * This is the default handler, and thus returns a list of all of the
3337  * pads inside the parent element with opposite direction.
3338  * The caller must free this list after use.
3339  *
3340  * Returns: a newly allocated #GList of pads.
3341  */
3342 GList*
3343 gst_pad_get_internal_links_default (GstPad *pad)
3344 {
3345   GList *res = NULL;
3346   GstElement *parent;
3347   GList *parent_pads;
3348   GstPadDirection direction;
3349   GstRealPad *rpad;
3350   
3351   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3352
3353   rpad = GST_PAD_REALIZE (pad);
3354   direction = rpad->direction;
3355
3356   parent = GST_PAD_PARENT (rpad);
3357   parent_pads = parent->pads;
3358
3359   while (parent_pads) {
3360     GstRealPad *parent_pad = GST_PAD_REALIZE (parent_pads->data);
3361     
3362     if (parent_pad->direction != direction) {
3363       res = g_list_prepend (res, parent_pad);
3364     }
3365     
3366     parent_pads = g_list_next (parent_pads);
3367   }
3368
3369   return res;
3370 }
3371
3372 /**
3373  * gst_pad_get_internal_links:
3374  * @pad: the #GstPad to get the internal links of.
3375  *
3376  * Gets a list of pads to which the given pad is linked to
3377  * inside of the parent element.
3378  * The caller must free this list after use.
3379  *
3380  * Returns: a newly allocated #GList of pads.
3381  */
3382 GList*
3383 gst_pad_get_internal_links (GstPad *pad)
3384 {
3385   GList *res = NULL;
3386   GstRealPad *rpad;
3387
3388   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3389
3390   rpad = GST_PAD_REALIZE (pad);
3391
3392   if (GST_RPAD_INTLINKFUNC (rpad))
3393     res = GST_RPAD_INTLINKFUNC (rpad) (GST_PAD (rpad));
3394
3395   return res;
3396 }
3397
3398
3399 static gboolean 
3400 gst_pad_event_default_dispatch (GstPad *pad, GstElement *element, 
3401                                 GstEvent *event)
3402 {
3403   GList *orig, *pads;
3404
3405   orig = pads = gst_pad_get_internal_links (pad);
3406
3407   while (pads) {
3408     GstPad *eventpad = GST_PAD (pads->data);
3409     pads = g_list_next (pads);
3410
3411     /* for all pads in the opposite direction that are linked */
3412     if (GST_PAD_IS_LINKED (eventpad)) {
3413       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
3414         /* increase the refcount */
3415         gst_event_ref (event);
3416         gst_pad_push (eventpad, GST_DATA (event));
3417       }
3418       else {
3419         GstPad *peerpad = GST_PAD (GST_RPAD_PEER (eventpad));
3420
3421         /* we only send the event on one pad, multi-sinkpad elements 
3422          * should implement a handler */
3423         g_list_free (orig);
3424         return gst_pad_send_event (peerpad, event);
3425       }
3426     }
3427   }
3428   gst_event_unref (event);
3429   g_list_free (orig);
3430   return (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
3431 }
3432
3433 /**
3434  * gst_pad_event_default:
3435  * @pad: a #GstPad to call the default event handler on.
3436  * @event: the #GstEvent to handle.
3437  *
3438  * Invokes the default event handler for the given pad.
3439  *
3440  * Returns: TRUE if the event was sent succesfully.
3441  */
3442 gboolean 
3443 gst_pad_event_default (GstPad *pad, GstEvent *event)
3444 {
3445   GstElement *element;
3446   
3447   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3448   g_return_val_if_fail (event, FALSE);
3449   
3450   element = GST_PAD_PARENT (pad);
3451
3452   switch (GST_EVENT_TYPE (event)) {
3453     case GST_EVENT_EOS:
3454       gst_pad_event_default_dispatch (pad, element, event);
3455       gst_element_set_eos (element);
3456       break;
3457     case GST_EVENT_DISCONTINUOUS:
3458     {
3459       guint64 time;
3460               
3461       if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &time)) {
3462         if (gst_element_requires_clock (element) && element->clock) {
3463           gst_element_set_time (element, time); 
3464         }
3465       }
3466     }
3467     case GST_EVENT_FLUSH:
3468     default:
3469       return gst_pad_event_default_dispatch (pad, element, event);
3470   }
3471   return TRUE;
3472 }
3473
3474 /**
3475  * gst_pad_dispatcher:
3476  * @pad: a #GstPad to dispatch.
3477  * @dispatch: the #GstDispatcherFunction to call.
3478  * @data: gpointer user data passed to the dispatcher function.
3479  *
3480  * Invokes the given dispatcher function on all pads that are 
3481  * internally linked to the given pad. 
3482  * The GstPadDispatcherFunction should return TRUE when no further pads 
3483  * need to be processed.
3484  *
3485  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3486  */
3487 gboolean
3488 gst_pad_dispatcher (GstPad *pad, GstPadDispatcherFunction dispatch, 
3489                     gpointer data)
3490 {
3491   gboolean res = FALSE;
3492   GList *int_pads, *orig;
3493   
3494   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3495   g_return_val_if_fail (data, FALSE);
3496
3497   orig = int_pads = gst_pad_get_internal_links (pad);
3498
3499   while (int_pads) {
3500     GstRealPad *int_rpad = GST_PAD_REALIZE (int_pads->data);
3501     GstRealPad *int_peer = GST_RPAD_PEER (int_rpad);
3502
3503     if (int_peer) {
3504       res = dispatch (GST_PAD (int_peer), data);
3505       if (res)
3506         break;
3507     }
3508     int_pads = g_list_next (int_pads);
3509   }
3510
3511   g_list_free (orig);
3512   
3513   return res;
3514 }
3515
3516 /**
3517  * gst_pad_send_event:
3518  * @pad: a #GstPad to send the event to.
3519  * @event: the #GstEvent to send to the pad.
3520  *
3521  * Sends the event to the pad.
3522  *
3523  * Returns: TRUE if the event was handled.
3524  */
3525 gboolean
3526 gst_pad_send_event (GstPad *pad, GstEvent *event)
3527 {
3528   gboolean success = FALSE;
3529   GstRealPad *rpad;
3530
3531   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3532   g_return_val_if_fail (event, FALSE);
3533
3534   rpad = GST_PAD_REALIZE (pad);
3535
3536   if (GST_EVENT_SRC (event) == NULL)
3537     GST_EVENT_SRC (event) = gst_object_ref (GST_OBJECT (rpad));
3538
3539   GST_CAT_DEBUG (GST_CAT_EVENT, "have event %d on pad %s:%s",
3540                   GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (rpad));
3541
3542   if (GST_RPAD_EVENTHANDLER (rpad))
3543     success = GST_RPAD_EVENTHANDLER (rpad) (GST_PAD (rpad), event);
3544   else {
3545     g_warning ("pad %s:%s has no event handler", GST_DEBUG_PAD_NAME (rpad));
3546     gst_event_unref (event);
3547   }
3548
3549   return success;
3550 }
3551
3552 typedef struct 
3553 {
3554   GstFormat      src_format;
3555   gint64         src_value;
3556   GstFormat      *dest_format;
3557   gint64         *dest_value;
3558 } GstPadConvertData;
3559
3560 static gboolean
3561 gst_pad_convert_dispatcher (GstPad *pad, GstPadConvertData *data)
3562 {
3563   return gst_pad_convert (pad, data->src_format, data->src_value, 
3564                                data->dest_format, data->dest_value);
3565 }
3566
3567 /**
3568  * gst_pad_convert_default:
3569  * @pad: a #GstPad to invoke the default converter on.
3570  * @src_format: the source #GstFormat.
3571  * @src_value: the source value.
3572  * @dest_format: a pointer to the destination #GstFormat.
3573  * @dest_value: a pointer to the destination value.
3574  *
3575  * Invokes the default converter on a pad. 
3576  * This will forward the call to the pad obtained 
3577  * using the internal link of
3578  * the element.
3579  *
3580  * Returns: TRUE if the conversion could be performed.
3581  */
3582 gboolean
3583 gst_pad_convert_default (GstPad *pad, 
3584                          GstFormat src_format,  gint64  src_value,
3585                          GstFormat *dest_format, gint64 *dest_value)
3586 {
3587   GstPadConvertData data;
3588
3589   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3590   g_return_val_if_fail (dest_format, FALSE);
3591   g_return_val_if_fail (dest_value, FALSE);
3592
3593   data.src_format = src_format;
3594   data.src_value = src_value;
3595   data.dest_format = dest_format;
3596   data.dest_value = dest_value;
3597
3598   return gst_pad_dispatcher (pad, (GstPadDispatcherFunction) 
3599                                     gst_pad_convert_dispatcher, &data);
3600 }
3601
3602 /**
3603  * gst_pad_convert:
3604  * @pad: a #GstPad to invoke the default converter on.
3605  * @src_format: the source #GstFormat.
3606  * @src_value: the source value.
3607  * @dest_format: a pointer to the destination #GstFormat.
3608  * @dest_value: a pointer to the destination value.
3609  *
3610  * Invokes a conversion on the pad.
3611  *
3612  * Returns: TRUE if the conversion could be performed.
3613  */
3614 gboolean
3615 gst_pad_convert (GstPad *pad, 
3616                  GstFormat src_format,  gint64  src_value,
3617                  GstFormat *dest_format, gint64 *dest_value)
3618 {
3619   GstRealPad *rpad;
3620   
3621   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3622   g_return_val_if_fail (dest_format, FALSE);
3623   g_return_val_if_fail (dest_value, FALSE);
3624
3625   if (src_format == *dest_format) {
3626     *dest_value = src_value; 
3627     return TRUE;
3628   }     
3629
3630   rpad = GST_PAD_REALIZE (pad);
3631
3632   if (GST_RPAD_CONVERTFUNC (rpad)) {
3633     return GST_RPAD_CONVERTFUNC (rpad) (GST_PAD (rpad), src_format, 
3634                                         src_value, dest_format, dest_value);
3635   }
3636
3637   return FALSE;
3638 }
3639
3640 typedef struct 
3641 {
3642   GstQueryType    type;
3643   GstFormat      *format;
3644   gint64         *value;
3645 } GstPadQueryData;
3646
3647 static gboolean
3648 gst_pad_query_dispatcher (GstPad *pad, GstPadQueryData *data)
3649 {
3650   return gst_pad_query (pad, data->type, data->format, data->value);
3651 }
3652
3653 /**
3654  * gst_pad_query_default:
3655  * @pad: a #GstPad to invoke the default query on.
3656  * @type: the #GstQueryType of the query to perform.
3657  * @format: a pointer to the #GstFormat of the result.
3658  * @value: a pointer to the result.
3659  *
3660  * Invokes the default query function on a pad. 
3661  *
3662  * Returns: TRUE if the query could be performed.
3663  */
3664 gboolean
3665 gst_pad_query_default (GstPad *pad, GstQueryType type,
3666                        GstFormat *format,  gint64 *value)
3667 {
3668   GstPadQueryData data;
3669
3670   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3671   g_return_val_if_fail (format, FALSE);
3672   g_return_val_if_fail (value, FALSE);
3673
3674   data.type = type;
3675   data.format = format;
3676   data.value = value;
3677
3678   return gst_pad_dispatcher (pad, (GstPadDispatcherFunction) 
3679                                    gst_pad_query_dispatcher, &data);
3680 }
3681
3682 /**
3683  * gst_pad_query:
3684  * @pad: a #GstPad to invoke the default query on.
3685  * @type: the #GstQueryType of the query to perform.
3686  * @format: a pointer to the #GstFormat asked for.
3687  *          On return contains the #GstFormat used.
3688  * @value: a pointer to the result.
3689  *
3690  * Queries a pad for one of the available properties. The format will be
3691  * adjusted to the actual format used when specifying formats such as 
3692  * GST_FORMAT_DEFAULT.
3693  * FIXME: Tell if the format can be adjusted when specifying a definite format.
3694  *
3695  * Returns: TRUE if the query could be performed.
3696  */
3697 gboolean
3698 gst_pad_query (GstPad *pad, GstQueryType type,
3699                GstFormat *format, gint64 *value) 
3700 {
3701   GstRealPad *rpad;
3702   
3703   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3704   g_return_val_if_fail (format, FALSE);
3705   g_return_val_if_fail (value, FALSE);
3706
3707   rpad = GST_PAD_REALIZE (pad);
3708
3709   g_return_val_if_fail (rpad, FALSE);
3710
3711   if (GST_RPAD_QUERYFUNC (rpad))
3712     return GST_RPAD_QUERYFUNC (rpad) (GST_PAD (pad), type, format, value);
3713
3714   return FALSE;
3715 }
3716
3717 static gboolean
3718 gst_pad_get_formats_dispatcher (GstPad *pad, const GstFormat **data)
3719 {
3720   *data = gst_pad_get_formats (pad);
3721
3722   return TRUE;
3723 }
3724
3725 /**
3726  * gst_pad_get_formats_default:
3727  * @pad: a #GstPad to query
3728  *
3729  * Invoke the default format dispatcher for the pad.
3730  *
3731  * Returns: An array of GstFormats ended with a 0 value.
3732  */
3733 const GstFormat*
3734 gst_pad_get_formats_default (GstPad *pad)
3735 {
3736   GstFormat *result = NULL;
3737
3738   gst_pad_dispatcher (pad, (GstPadDispatcherFunction) 
3739                       gst_pad_get_formats_dispatcher, &result);
3740
3741   return result;
3742 }
3743
3744 /**
3745  * gst_pad_get_formats:
3746  * @pad: a #GstPad to query
3747  *
3748  * Gets the list of supported formats from the pad.
3749  *
3750  * Returns: An array of GstFormats ended with a 0 value.
3751  */
3752 const GstFormat*
3753 gst_pad_get_formats (GstPad *pad)
3754 {
3755   GstRealPad *rpad;
3756   
3757   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3758
3759   rpad = GST_PAD_REALIZE (pad);
3760
3761   if (GST_RPAD_FORMATSFUNC (rpad))
3762     return GST_RPAD_FORMATSFUNC (rpad) (GST_PAD (pad));
3763
3764   return NULL;
3765 }