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