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