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