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