7cc77f379c72d014df0741de7db1010b068aa6a7
[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  *
2266  * Calls gst_pad_try_set_caps() for every other pad belonging to the
2267  * same element as @pad.  If gst_pad_try_set_caps() fails on any pad,
2268  * the proxy link fails. May be used only during negotiation.
2269  *
2270  * Returns: GST_PAD_LINK_OK if sucessful
2271  */
2272 GstPadLinkReturn
2273 gst_pad_proxy_pad_link (GstPad * pad, const GstCaps * caps)
2274 {
2275   GstElement *element;
2276   const GList *pads;
2277   GstPadLinkReturn ret;
2278
2279   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_LINK_REFUSED);
2280   g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
2281
2282   GST_DEBUG ("proxying pad link for %s:%s", GST_DEBUG_PAD_NAME (pad));
2283
2284   element = gst_pad_get_parent (pad);
2285
2286   pads = gst_element_get_pad_list (element);
2287
2288   while (pads) {
2289     GstPad *otherpad = GST_PAD (pads->data);
2290
2291     if (otherpad != pad) {
2292       ret = gst_pad_try_set_caps (otherpad, caps);
2293       if (GST_PAD_LINK_FAILED (ret)) {
2294         return ret;
2295       }
2296     }
2297     pads = g_list_next (pads);
2298   }
2299
2300   return GST_PAD_LINK_OK;
2301 }
2302
2303 /**
2304  * gst_pad_proxy_fixate:
2305  * @pad: a #GstPad to proxy.
2306  *
2307  * Implements a default fixate function based on the caps set on the other
2308  * pads in the element.  This function should only be used if every pad
2309  * has the same pad template caps.
2310  *
2311  * Returns: a fixated caps, or NULL if caps cannot be fixed
2312  */
2313 GstCaps *
2314 gst_pad_proxy_fixate (GstPad * pad, const GstCaps * caps)
2315 {
2316   GstElement *element;
2317   const GList *pads;
2318   const GstCaps *othercaps;
2319
2320   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2321   g_return_val_if_fail (caps != NULL, NULL);
2322
2323   GST_DEBUG ("proxying fixate for %s:%s\n", GST_DEBUG_PAD_NAME (pad));
2324
2325   element = gst_pad_get_parent (pad);
2326
2327   pads = gst_element_get_pad_list (element);
2328
2329   while (pads) {
2330     GstPad *otherpad = GST_PAD (pads->data);
2331
2332     /* FIXME check that each pad has the same pad template caps */
2333
2334     if (otherpad != pad) {
2335       othercaps = gst_pad_get_negotiated_caps (otherpad);
2336
2337       if (othercaps) {
2338         GstCaps *icaps;
2339
2340         icaps = gst_caps_intersect (othercaps, caps);
2341         if (!gst_caps_is_empty (icaps)) {
2342           return icaps;
2343         } else {
2344           gst_caps_free (icaps);
2345         }
2346       }
2347     }
2348     pads = g_list_next (pads);
2349   }
2350
2351   return NULL;
2352 }
2353
2354 /**
2355  * gst_pad_set_explicit_caps:
2356  * @pad: a #GstPad to set the explicit caps of
2357  * @caps: the #GstCaps to set
2358  *
2359  * If a pad has been told to use explicit caps, this function is used
2360  * to set the explicit caps.  If @caps is NULL, the explicit caps are
2361  * unset.
2362  *
2363  * This function calls gst_pad_try_set_caps() on the pad.  If that
2364  * call fails, GST_ELEMENT_ERROR() is called to indicate a negotiation
2365  * failure.
2366  * 
2367  * Returns: TRUE if the caps were set correctly, otherwise FALSE
2368  */
2369 gboolean
2370 gst_pad_set_explicit_caps (GstPad * pad, const GstCaps * caps)
2371 {
2372   GstPadLinkReturn link_ret;
2373
2374   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2375
2376   GST_CAT_DEBUG (GST_CAT_PADS, "setting explicit caps to %" GST_PTR_FORMAT,
2377       caps);
2378
2379   if (caps == NULL) {
2380     GST_CAT_DEBUG (GST_CAT_PADS, "caps is NULL");
2381     gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), NULL);
2382     return TRUE;
2383   }
2384
2385   gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), gst_caps_copy (caps));
2386
2387   if (!GST_PAD_IS_LINKED (pad)) {
2388     GST_CAT_DEBUG (GST_CAT_PADS, "pad is not linked");
2389     return TRUE;
2390   }
2391   link_ret = gst_pad_try_set_caps (pad, caps);
2392   if (link_ret == GST_PAD_LINK_REFUSED) {
2393     gchar *caps_str = gst_caps_to_string (caps);
2394
2395     GST_ELEMENT_ERROR (gst_pad_get_parent (pad), CORE, PAD, (NULL),
2396         ("failed to negotiate (try_set_caps with \"%s\" returned REFUSED)",
2397             caps_str));
2398     g_free (caps_str);
2399     return FALSE;
2400   }
2401
2402   return TRUE;
2403 }
2404
2405 static GstCaps *
2406 gst_pad_explicit_getcaps (GstPad * pad)
2407 {
2408   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2409
2410   if (GST_RPAD_EXPLICIT_CAPS (pad) == NULL) {
2411     const GstCaps *caps = gst_pad_get_pad_template_caps (pad);
2412
2413     return gst_caps_copy (caps);
2414   }
2415   return gst_caps_copy (GST_RPAD_EXPLICIT_CAPS (pad));
2416 }
2417
2418 static GstPadLinkReturn
2419 gst_pad_explicit_link (GstPad * pad, const GstCaps * caps)
2420 {
2421   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_LINK_REFUSED);
2422   g_return_val_if_fail (caps != NULL, GST_PAD_LINK_REFUSED);
2423
2424   if (GST_RPAD_EXPLICIT_CAPS (pad) == NULL) {
2425     return GST_PAD_LINK_DELAYED;
2426   }
2427
2428   return GST_PAD_LINK_OK;
2429 }
2430
2431 /**
2432  * gst_pad_use_explicit_caps:
2433  * @pad: a #GstPad to set to use explicit caps
2434  *
2435  * This function handles negotiation for pads that need to be set
2436  * to particular caps under complete control of the element, based
2437  * on some state in the element.  This is often the case with
2438  * decoders and other elements whose caps is determined by the data
2439  * stream.
2440  *
2441  * WARNING: This function is a hack and will be replaced with something
2442  * better in gstreamer-0.9.
2443  */
2444 void
2445 gst_pad_use_explicit_caps (GstPad * pad)
2446 {
2447   g_return_if_fail (GST_IS_PAD (pad));
2448
2449   gst_pad_set_getcaps_function (pad, gst_pad_explicit_getcaps);
2450   gst_pad_set_link_function (pad, gst_pad_explicit_link);
2451   gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), NULL);
2452 }
2453
2454 /**
2455  * gst_pad_proxy_link:
2456  * @pad: a #GstPad to proxy to.
2457  * @caps: the #GstCaps to use in proxying.
2458  *
2459  * Proxies the link function to the specified pad.
2460  *
2461  * Returns: TRUE if the peer pad accepted the caps, FALSE otherwise.
2462  */
2463 GstPadLinkReturn
2464 gst_pad_proxy_link (GstPad * pad, const GstCaps * caps)
2465 {
2466   return gst_pad_try_set_caps (pad, caps);
2467 }
2468
2469 /**
2470  * gst_pad_is_negotiated:
2471  * @pad: a #GstPad to get the negotiation status of
2472  *
2473  * Returns: TRUE if the pad has successfully negotiated caps.
2474  */
2475 gboolean
2476 gst_pad_is_negotiated (GstPad * pad)
2477 {
2478   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2479
2480   if (!GST_PAD_REALIZE (pad))
2481     return FALSE;
2482   if (!GST_RPAD_LINK (pad))
2483     return FALSE;
2484
2485   return (GST_RPAD_LINK (pad)->caps != NULL);
2486 }
2487
2488 /**
2489  * gst_pad_get_negotiated_caps:
2490  * @pad: a #GstPad to get the negotiated capabilites of
2491  *
2492  * Gets the currently negotiated caps of a pad.
2493  *
2494  * Returns: the currently negotiated caps of a pad, or NULL if the pad isn't
2495  *          negotiated.
2496  */
2497 G_CONST_RETURN GstCaps *
2498 gst_pad_get_negotiated_caps (GstPad * pad)
2499 {
2500   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2501
2502   if (!GST_PAD_REALIZE (pad))
2503     return NULL;
2504   if (!GST_RPAD_LINK (pad))
2505     return NULL;
2506
2507   return GST_RPAD_LINK (pad)->caps;
2508 }
2509
2510 /**
2511  * gst_pad_get_caps:
2512  * @pad: a  #GstPad to get the capabilities of.
2513  *
2514  * Gets the capabilities of this pad.
2515  *
2516  * Returns: the #GstCaps of this pad. This function returns a new caps, so use 
2517  * gst_caps_free to get rid of it.
2518  */
2519 GstCaps *
2520 gst_pad_get_caps (GstPad * pad)
2521 {
2522   GstRealPad *realpad;
2523
2524   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2525
2526   realpad = GST_PAD_REALIZE (pad);
2527
2528   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
2529       GST_DEBUG_PAD_NAME (realpad), realpad);
2530
2531   if (GST_RPAD_GETCAPSFUNC (realpad)) {
2532     GstCaps *caps;
2533
2534     GST_CAT_DEBUG (GST_CAT_CAPS, "using pad getcaps function");
2535     caps = GST_RPAD_GETCAPSFUNC (realpad) (GST_PAD (realpad));
2536
2537     if (caps == NULL) {
2538       g_critical ("pad %s:%s returned NULL caps from getcaps function\n",
2539           GST_ELEMENT_NAME (GST_PAD_PARENT (GST_PAD (realpad))),
2540           GST_PAD_NAME (realpad));
2541       caps = gst_caps_new_any ();
2542     }
2543
2544     return caps;
2545   } else if (GST_PAD_PAD_TEMPLATE (realpad)) {
2546     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (realpad);
2547     const GstCaps *caps;
2548
2549     caps = GST_PAD_TEMPLATE_CAPS (templ);
2550     GST_CAT_DEBUG (GST_CAT_CAPS,
2551         "using pad template %p with caps %" GST_PTR_FORMAT, templ, caps);
2552
2553 #if 0
2554     /* FIXME we should enable something like this someday, but this is
2555      * a bit buggy */
2556     if (!gst_caps_is_fixed (caps)) {
2557       g_warning
2558           ("pad %s:%s (%p) has no getcaps function and the pad template returns non-fixed caps.  Element is probably broken.\n",
2559           GST_DEBUG_PAD_NAME (realpad), realpad);
2560     }
2561 #endif
2562
2563     return gst_caps_copy (GST_PAD_TEMPLATE_CAPS (templ));
2564   }
2565   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2566
2567 #if 0
2568   /* FIXME enable */
2569   g_warning ("pad %s:%s (%p) has no pad template\n",
2570       GST_DEBUG_PAD_NAME (realpad), realpad);
2571 #endif
2572
2573   return gst_caps_new_any ();
2574 }
2575
2576 /**
2577  * gst_pad_get_pad_template_caps:
2578  * @pad: a #GstPad to get the template capabilities from.
2579  *
2580  * Gets the capabilities for @pad's template.
2581  *
2582  * Returns: the #GstCaps of this pad template. If you intend to keep a reference
2583  * on the caps, make a copy (see gst_caps_copy ()).
2584  */
2585 const GstCaps *
2586 gst_pad_get_pad_template_caps (GstPad * pad)
2587 {
2588   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2589
2590   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2591
2592   if (GST_PAD_PAD_TEMPLATE (pad))
2593     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2594
2595 #if 0
2596   /* FIXME this should be enabled some day */
2597   /* wingo: why? mail the list during 0.9 when you find this :) */
2598   g_warning ("pad %s:%s (%p) has no pad template\n",
2599       GST_DEBUG_PAD_NAME (realpad), realpad);
2600 #endif
2601
2602   return gst_static_caps_get (&anycaps);
2603 }
2604
2605 /* FIXME 0.9: This function should probably die, or at least be renamed to
2606  * get_caps_by_format. */
2607 /**
2608  * gst_pad_template_get_caps_by_name:
2609  * @templ: a #GstPadTemplate to get the capabilities of.
2610  * @name: the name of the capability to get.
2611  *
2612  * Gets the capability with the given name from @templ.
2613  *
2614  * Returns: the #GstCaps of this pad template, or NULL if not found. If you
2615  * intend to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2616  */
2617 const GstCaps *
2618 gst_pad_template_get_caps_by_name (GstPadTemplate * templ, const gchar * name)
2619 {
2620   GstCaps *caps;
2621
2622   g_return_val_if_fail (templ != NULL, NULL);
2623
2624   caps = GST_PAD_TEMPLATE_CAPS (templ);
2625   if (!caps)
2626     return NULL;
2627
2628   /* FIXME */
2629   //return gst_caps_copy (gst_caps_get_by_name (caps, name));
2630   return NULL;
2631 }
2632
2633 /* FIXME 0.9: What good is this if it only works for already-negotiated pads? */
2634 /**
2635  * gst_pad_check_compatibility:
2636  * @srcpad: the source #GstPad to check.
2637  * @sinkpad: the sink #GstPad to check against.
2638  *
2639  * Checks if two pads have compatible capabilities. If neither one has yet been
2640  * negotiated, returns TRUE for no good reason.
2641  *
2642  * Returns: TRUE if they are compatible or if the capabilities could not be
2643  * checked, FALSE if the capabilities are not compatible.
2644  */
2645 gboolean
2646 gst_pad_check_compatibility (GstPad * srcpad, GstPad * sinkpad)
2647 {
2648   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2649   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2650
2651   if (GST_PAD_CAPS (srcpad) && GST_PAD_CAPS (sinkpad)) {
2652     if (!gst_caps_is_always_compatible (GST_PAD_CAPS (srcpad),
2653             GST_PAD_CAPS (sinkpad))) {
2654       return FALSE;
2655     } else {
2656       return TRUE;
2657     }
2658   } else {
2659     GST_CAT_DEBUG (GST_CAT_PADS,
2660         "could not check capabilities of pads (%s:%s) and (%s:%s) %p %p",
2661         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2662         GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad));
2663     return TRUE;
2664   }
2665 }
2666
2667 /**
2668  * gst_pad_get_peer:
2669  * @pad: a #GstPad to get the peer of.
2670  *
2671  * Gets the peer of @pad.
2672  *
2673  * Returns: the peer #GstPad.
2674  */
2675 GstPad *
2676 gst_pad_get_peer (GstPad * pad)
2677 {
2678   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2679
2680   return GST_PAD (GST_PAD_PEER (pad));
2681 }
2682
2683 /**
2684  * gst_pad_get_allowed_caps:
2685  * @pad: a real #GstPad.
2686  *
2687  * Gets the capabilities of the allowed media types that can flow through @pad.
2688  * The caller must free the resulting caps.
2689  *
2690  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2691  * you no longer need it.
2692  */
2693 GstCaps *
2694 gst_pad_get_allowed_caps (GstPad * pad)
2695 {
2696   const GstCaps *mycaps;
2697   GstCaps *caps;
2698   GstCaps *peercaps;
2699   GstCaps *icaps;
2700   GstPadLink *link;
2701
2702   g_return_val_if_fail (GST_IS_REAL_PAD (pad), NULL);
2703
2704   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "get allowed caps of %s:%s",
2705       GST_DEBUG_PAD_NAME (pad));
2706
2707   mycaps = gst_pad_get_pad_template_caps (pad);
2708   if (GST_RPAD_PEER (pad) == NULL) {
2709     return gst_caps_copy (mycaps);
2710   }
2711
2712   peercaps = gst_pad_get_caps (GST_PAD_PEER (pad));
2713   caps = gst_caps_intersect (mycaps, peercaps);
2714   gst_caps_free (peercaps);
2715
2716   link = GST_RPAD_LINK (pad);
2717   if (link->filtercaps) {
2718     icaps = gst_caps_intersect (caps, link->filtercaps);
2719     gst_caps_free (caps);
2720     return icaps;
2721   } else {
2722     return caps;
2723   }
2724 }
2725
2726 /**
2727  * gst_pad_caps_change_notify:
2728  * @pad: a #GstPad
2729  *
2730  * Called to indicate that the return value of @pad's getcaps function may have
2731  * changed, and that a renegotiation is suggested.
2732  */
2733 void
2734 gst_pad_caps_change_notify (GstPad * pad)
2735 {
2736 }
2737
2738 /**
2739  * gst_pad_recover_caps_error:
2740  * @pad: a #GstPad that had a failed capsnego
2741  * @allowed: possible caps for the link
2742  *
2743  * Attempt to recover from a failed caps negotiation. This function
2744  * is typically called by a plugin that exhausted its list of caps
2745  * and wants the application to resolve the issue. The application
2746  * should connect to the pad's caps_nego_failed signal and should
2747  * resolve the issue by connecting another element for example.
2748  *
2749  * Returns: TRUE when the issue was resolved, dumps detailed information
2750  * on the console and returns FALSE otherwise.
2751  */
2752 gboolean
2753 gst_pad_recover_caps_error (GstPad * pad, const GstCaps * allowed)
2754 {
2755   /* FIXME */
2756   return FALSE;
2757 }
2758
2759 /* FIXME 0.9: Is it so difficult to write SOURCE? */
2760 /**
2761  * gst_pad_alloc_buffer:
2762  * @pad: a source #GstPad.
2763  *
2764  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2765  * function only works if @pad is a src pad.
2766  *
2767  * Returns: a new, empty #GstBuffer, or NULL if there is an error
2768  */
2769 GstBuffer *
2770 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size)
2771 {
2772   GstRealPad *peer;
2773
2774   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2775   g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
2776
2777   peer = GST_RPAD_PEER (pad);
2778
2779   if (!peer)
2780     return gst_buffer_new_and_alloc (size);
2781
2782   GST_CAT_DEBUG (GST_CAT_BUFFER, "(%s:%s): getting buffer",
2783       GST_DEBUG_PAD_NAME (pad));
2784
2785   if (peer->bufferallocfunc) {
2786     GST_CAT_DEBUG (GST_CAT_PADS,
2787         "calling bufferallocfunc &%s (@%p) of peer pad %s:%s",
2788         GST_DEBUG_FUNCPTR_NAME (peer->bufferallocfunc),
2789         &peer->bufferallocfunc, GST_DEBUG_PAD_NAME (((GstPad *) peer)));
2790     return (peer->bufferallocfunc) (GST_PAD (peer), offset, size);
2791   } else {
2792     return gst_buffer_new_and_alloc (size);
2793   }
2794 }
2795
2796 static void
2797 gst_real_pad_dispose (GObject * object)
2798 {
2799   GstPad *pad = GST_PAD (object);
2800
2801   /* No linked pad can ever be disposed.
2802    * It has to have a parent to be linked 
2803    * and a parent would hold a reference */
2804   /* FIXME: what about if g_object_dispose is explicitly called on the pad? Is
2805      that legal? otherwise we could assert GST_OBJECT_PARENT (pad) == NULL as
2806      well... */
2807   g_assert (GST_PAD_PEER (pad) == NULL);
2808
2809   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s",
2810       GST_DEBUG_PAD_NAME (pad));
2811
2812   /* we destroy the ghostpads, because they are nothing without the real pad */
2813   if (GST_REAL_PAD (pad)->ghostpads) {
2814     GList *orig, *ghostpads;
2815
2816     orig = ghostpads = g_list_copy (GST_REAL_PAD (pad)->ghostpads);
2817
2818     while (ghostpads) {
2819       GstPad *ghostpad = GST_PAD (ghostpads->data);
2820
2821       if (GST_IS_ELEMENT (GST_OBJECT_PARENT (ghostpad))) {
2822         GstElement *parent = GST_ELEMENT (GST_OBJECT_PARENT (ghostpad));
2823
2824         GST_CAT_DEBUG (GST_CAT_REFCOUNTING,
2825             "removing ghost pad from element '%s'", GST_OBJECT_NAME (parent));
2826         gst_element_remove_pad (parent, ghostpad);
2827       } else {
2828         /* handle the case where we have some floating ghost pad that was never
2829            added to an element */
2830         g_object_set (ghostpad, "real-pad", NULL, NULL);
2831       }
2832       ghostpads = g_list_next (ghostpads);
2833     }
2834     g_list_free (orig);
2835     /* as the ghost pads are removed, they remove themselves from ->ghostpads.
2836        So it should be empty now. Let's assert that. */
2837     g_assert (GST_REAL_PAD (pad)->ghostpads == NULL);
2838   }
2839
2840   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))) {
2841     GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'",
2842         GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
2843
2844     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
2845   }
2846
2847   if (GST_RPAD_EXPLICIT_CAPS (pad)) {
2848     GST_ERROR_OBJECT (pad, "still explicit caps %" GST_PTR_FORMAT " set",
2849         GST_RPAD_EXPLICIT_CAPS (pad));
2850     g_warning ("pad %p has still explicit caps set", pad);
2851     gst_caps_replace (&GST_RPAD_EXPLICIT_CAPS (pad), NULL);
2852   }
2853   G_OBJECT_CLASS (real_pad_parent_class)->dispose (object);
2854 }
2855
2856
2857 #ifndef GST_DISABLE_LOADSAVE
2858 /* FIXME: why isn't this on a GstElement ? */
2859 /**
2860  * gst_pad_load_and_link:
2861  * @self: an #xmlNodePtr to read the description from.
2862  * @parent: the #GstObject element that owns the pad.
2863  *
2864  * Reads the pad definition from the XML node and links the given pad
2865  * in the element to a pad of an element up in the hierarchy.
2866  */
2867 void
2868 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
2869 {
2870   xmlNodePtr field = self->xmlChildrenNode;
2871   GstPad *pad = NULL, *targetpad;
2872   gchar *peer = NULL;
2873   gchar **split;
2874   GstElement *target;
2875   GstObject *grandparent;
2876
2877   while (field) {
2878     if (!strcmp (field->name, "name")) {
2879       pad = gst_element_get_pad (GST_ELEMENT (parent),
2880           xmlNodeGetContent (field));
2881     } else if (!strcmp (field->name, "peer")) {
2882       peer = xmlNodeGetContent (field);
2883     }
2884     field = field->next;
2885   }
2886   g_return_if_fail (pad != NULL);
2887
2888   if (peer == NULL)
2889     return;
2890
2891   split = g_strsplit (peer, ".", 2);
2892
2893   if (split[0] == NULL || split[1] == NULL) {
2894     GST_CAT_DEBUG (GST_CAT_XML,
2895         "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2896         peer, GST_DEBUG_PAD_NAME (pad));
2897     return;
2898   }
2899
2900   g_return_if_fail (split[0] != NULL);
2901   g_return_if_fail (split[1] != NULL);
2902
2903   grandparent = gst_object_get_parent (parent);
2904
2905   if (grandparent && GST_IS_BIN (grandparent)) {
2906     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2907   } else
2908     goto cleanup;
2909
2910   if (target == NULL)
2911     goto cleanup;
2912
2913   targetpad = gst_element_get_pad (target, split[1]);
2914
2915   if (targetpad == NULL)
2916     goto cleanup;
2917
2918   gst_pad_link (pad, targetpad);
2919
2920 cleanup:
2921   g_strfreev (split);
2922 }
2923
2924 /**
2925  * gst_pad_save_thyself:
2926  * @pad: a #GstPad to save.
2927  * @parent: the parent #xmlNodePtr to save the description in.
2928  *
2929  * Saves the pad into an xml representation.
2930  *
2931  * Returns: the #xmlNodePtr representation of the pad.
2932  */
2933 static xmlNodePtr
2934 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
2935 {
2936   GstRealPad *realpad;
2937   GstPad *peer;
2938
2939   g_return_val_if_fail (GST_IS_REAL_PAD (object), NULL);
2940
2941   realpad = GST_REAL_PAD (object);
2942
2943   xmlNewChild (parent, NULL, "name", GST_PAD_NAME (realpad));
2944   if (GST_RPAD_PEER (realpad) != NULL) {
2945     gchar *content;
2946
2947     peer = GST_PAD (GST_RPAD_PEER (realpad));
2948     /* first check to see if the peer's parent's parent is the same */
2949     /* we just save it off */
2950     content = g_strdup_printf ("%s.%s",
2951         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
2952     xmlNewChild (parent, NULL, "peer", content);
2953     g_free (content);
2954   } else
2955     xmlNewChild (parent, NULL, "peer", "");
2956
2957   return parent;
2958 }
2959
2960 /* FIXME: shouldn't it be gst_pad_ghost_* ?
2961  * dunno -- wingo 7 feb 2004
2962  */
2963 /**
2964  * gst_ghost_pad_save_thyself:
2965  * @pad: a ghost #GstPad to save.
2966  * @parent: the parent #xmlNodePtr to save the description in.
2967  *
2968  * Saves the ghost pad into an xml representation.
2969  *
2970  * Returns: the #xmlNodePtr representation of the pad.
2971  */
2972 xmlNodePtr
2973 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
2974 {
2975   xmlNodePtr self;
2976
2977   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
2978
2979   self = xmlNewChild (parent, NULL, "ghostpad", NULL);
2980   xmlNewChild (self, NULL, "name", GST_PAD_NAME (pad));
2981   xmlNewChild (self, NULL, "parent", GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
2982
2983   /* FIXME FIXME FIXME! */
2984
2985   return self;
2986 }
2987 #endif /* GST_DISABLE_LOADSAVE */
2988
2989 /**
2990  * gst_pad_push:
2991  * @pad: a source #GstPad.
2992  * @data: the #GstData to push.
2993  *
2994  * Pushes a buffer or an event to the peer of @pad. @pad must be linked. May
2995  * only be called by @pad's parent.
2996  */
2997 void
2998 gst_pad_push (GstPad * pad, GstData * data)
2999 {
3000   GstRealPad *peer;
3001
3002   g_return_if_fail (GST_IS_PAD (pad));
3003   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
3004   g_return_if_fail (data != NULL);
3005
3006   if (!gst_probe_dispatcher_dispatch (&(GST_REAL_PAD (pad)->probedisp), &data))
3007     return;
3008
3009   if (!GST_PAD_IS_LINKED (pad)) {
3010     GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad,
3011         "not pushing data %p as pad is unconnected", data);
3012     gst_data_unref (data);
3013     return;
3014   }
3015
3016   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad, "pushing");
3017   peer = GST_RPAD_PEER (pad);
3018
3019   if (!peer) {
3020     g_warning ("push on pad %s:%s but it is unlinked",
3021         GST_DEBUG_PAD_NAME (pad));
3022   } else {
3023     if (!GST_IS_EVENT (data) && !GST_PAD_IS_ACTIVE (peer)) {
3024       g_warning ("push on peer of pad %s:%s but peer is not active",
3025           GST_DEBUG_PAD_NAME (pad));
3026       return;
3027     }
3028
3029     if (peer->chainhandler) {
3030       if (data) {
3031         GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad,
3032             "calling chainhandler &%s of peer pad %s:%s",
3033             GST_DEBUG_FUNCPTR_NAME (peer->chainhandler),
3034             GST_DEBUG_PAD_NAME (GST_PAD (peer)));
3035         if (!gst_probe_dispatcher_dispatch (&peer->probedisp, &data))
3036           return;
3037
3038         (peer->chainhandler) (GST_PAD (peer), data);
3039         return;
3040       } else {
3041         g_warning ("trying to push a NULL buffer on pad %s:%s",
3042             GST_DEBUG_PAD_NAME (peer));
3043         return;
3044       }
3045     } else {
3046       g_warning ("internal error: push on pad %s:%s but it has no chainhandler",
3047           GST_DEBUG_PAD_NAME (peer));
3048     }
3049   }
3050   /* clean up the mess here */
3051   if (data != NULL)
3052     gst_data_unref (data);
3053 }
3054
3055 /**
3056  * gst_pad_pull:
3057  * @pad: a sink #GstPad.
3058  *
3059  * Pulls an event or a buffer from the peer pad. May only be called by @pad's
3060  * parent.
3061  *
3062  * Returns: a new #GstData from the peer pad.
3063  */
3064 GstData *
3065 gst_pad_pull (GstPad * pad)
3066 {
3067   GstRealPad *peer;
3068
3069   GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad, "pulling");
3070
3071   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
3072       GST_DATA (gst_event_new (GST_EVENT_INTERRUPT)));
3073
3074   peer = GST_RPAD_PEER (pad);
3075
3076   if (!peer) {
3077     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3078         ("pull on pad %s:%s but it was unlinked", GST_DEBUG_PAD_NAME (pad)));
3079   } else {
3080   restart:
3081     if (peer->gethandler) {
3082       GstData *data;
3083
3084       GST_CAT_LOG_OBJECT (GST_CAT_DATAFLOW, pad,
3085           "calling gethandler %s of peer pad %s:%s",
3086           GST_DEBUG_FUNCPTR_NAME (peer->gethandler), GST_DEBUG_PAD_NAME (peer));
3087
3088       data = (peer->gethandler) (GST_PAD (peer));
3089
3090       if (data) {
3091         if (!gst_probe_dispatcher_dispatch (&peer->probedisp, &data))
3092           goto restart;
3093         return data;
3094       }
3095
3096       /* no null buffers allowed */
3097       GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3098           ("NULL buffer during pull on %s:%s", GST_DEBUG_PAD_NAME (pad)));
3099     } else {
3100       GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3101           ("pull on pad %s:%s but the peer pad %s:%s has no gethandler",
3102               GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer)));
3103     }
3104   }
3105   return GST_DATA (gst_event_new (GST_EVENT_INTERRUPT));
3106 }
3107
3108 /**
3109  * gst_pad_selectv:
3110  * @padlist: a #GList of sink pads.
3111  *
3112  * Waits for a buffer on any of the list of pads. Each #GstPad in @padlist must
3113  * be owned by the calling code.
3114  *
3115  * Returns: the #GstPad that has a buffer available. 
3116  * Use #gst_pad_pull() to get the buffer.
3117  */
3118 GstPad *
3119 gst_pad_selectv (GList * padlist)
3120 {
3121   GstPad *pad;
3122
3123   pad = gst_scheduler_pad_select (GST_PAD_PARENT (padlist->data)->sched,
3124       padlist);
3125   return pad;
3126 }
3127
3128 /* FIXME 0.9: Don't allow the first pad to be NULL */
3129 /**
3130  * gst_pad_select:
3131  * @pad: a first sink #GstPad to perform the select on.
3132  * @...: A NULL-terminated list of more pads to select on.
3133  *
3134  * Waits for a buffer on the given set of pads.
3135  *
3136  * Returns: the #GstPad that has a buffer available.
3137  * Use #gst_pad_pull() to get the buffer.
3138  */
3139 GstPad *
3140 gst_pad_select (GstPad * pad, ...)
3141 {
3142   GstPad *result;
3143   va_list var_args;
3144
3145   if (pad == NULL)
3146     return NULL;
3147
3148   va_start (var_args, pad);
3149
3150   result = gst_pad_select_valist (pad, var_args);
3151
3152   va_end (var_args);
3153
3154   return result;
3155 }
3156
3157 /**
3158  * gst_pad_select_valist:
3159  * @pad: a first #GstPad to perform the select on.
3160  * @varargs: A va_list of more pads to select on.
3161  *
3162  * Waits for a buffer on the given set of pads.
3163  *
3164  * Returns: the #GstPad that has a buffer available.
3165  * Use #gst_pad_pull() to get the buffer.
3166  */
3167 GstPad *
3168 gst_pad_select_valist (GstPad * pad, va_list var_args)
3169 {
3170   GstPad *result;
3171   GList *padlist = NULL;
3172
3173   if (pad == NULL)
3174     return NULL;
3175
3176   while (pad) {
3177     padlist = g_list_prepend (padlist, pad);
3178     pad = va_arg (var_args, GstPad *);
3179   }
3180   result = gst_pad_selectv (padlist);
3181   g_list_free (padlist);
3182
3183   return result;
3184 }
3185
3186 /************************************************************************
3187  *
3188  * templates
3189  *
3190  */
3191 static void gst_pad_template_class_init (GstPadTemplateClass * klass);
3192 static void gst_pad_template_init (GstPadTemplate * templ);
3193 static void gst_pad_template_dispose (GObject * object);
3194
3195 GType
3196 gst_pad_template_get_type (void)
3197 {
3198   static GType padtemplate_type = 0;
3199
3200   if (!padtemplate_type) {
3201     static const GTypeInfo padtemplate_info = {
3202       sizeof (GstPadTemplateClass), NULL, NULL,
3203       (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
3204       sizeof (GstPadTemplate),
3205       32,
3206       (GInstanceInitFunc) gst_pad_template_init, NULL
3207     };
3208
3209     padtemplate_type =
3210         g_type_register_static (GST_TYPE_OBJECT, "GstPadTemplate",
3211         &padtemplate_info, 0);
3212   }
3213   return padtemplate_type;
3214 }
3215
3216 static void
3217 gst_pad_template_class_init (GstPadTemplateClass * klass)
3218 {
3219   GObjectClass *gobject_class;
3220   GstObjectClass *gstobject_class;
3221
3222   gobject_class = (GObjectClass *) klass;
3223   gstobject_class = (GstObjectClass *) klass;
3224
3225   padtemplate_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
3226
3227   gst_pad_template_signals[TEMPL_PAD_CREATED] =
3228       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
3229       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
3230       NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
3231
3232   gobject_class->dispose = gst_pad_template_dispose;
3233
3234   gstobject_class->path_string_separator = "*";
3235 }
3236
3237 static void
3238 gst_pad_template_init (GstPadTemplate * templ)
3239 {
3240 }
3241
3242 static void
3243 gst_pad_template_dispose (GObject * object)
3244 {
3245   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
3246
3247   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
3248   if (GST_PAD_TEMPLATE_CAPS (templ)) {
3249     gst_caps_free (GST_PAD_TEMPLATE_CAPS (templ));
3250   }
3251
3252   G_OBJECT_CLASS (padtemplate_parent_class)->dispose (object);
3253 }
3254
3255 /* ALWAYS padtemplates cannot have conversion specifications, it doesn't make
3256  * sense.
3257  * SOMETIMES padtemplates can do whatever they want, they are provided by the
3258  * element.
3259  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
3260  * 'sink%d' template is automatically selected), so we need to restrict their
3261  * naming.
3262  */
3263 static gboolean
3264 name_is_valid (const gchar * name, GstPadPresence presence)
3265 {
3266   const gchar *str;
3267
3268   if (presence == GST_PAD_ALWAYS) {
3269     if (strchr (name, '%')) {
3270       g_warning ("invalid name template %s: conversion specifications are not"
3271           " allowed for GST_PAD_ALWAYS padtemplates", name);
3272       return FALSE;
3273     }
3274   } else if (presence == GST_PAD_REQUEST) {
3275     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
3276       g_warning ("invalid name template %s: only one conversion specification"
3277           " allowed in GST_PAD_REQUEST padtemplate", name);
3278       return FALSE;
3279     }
3280     if (str && (*(str + 1) != 's' && *(str + 1) != 'd')) {
3281       g_warning ("invalid name template %s: conversion specification must be of"
3282           " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
3283       return FALSE;
3284     }
3285     if (str && (*(str + 2) != '\0')) {
3286       g_warning ("invalid name template %s: conversion specification must"
3287           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
3288       return FALSE;
3289     }
3290   }
3291
3292   return TRUE;
3293 }
3294
3295 /**
3296  * gst_static_pad_template_get:
3297  * @pad_template: the static pad template
3298  *
3299  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
3300  *
3301  * Returns: a new #GstPadTemplate.
3302  */
3303 GstPadTemplate *
3304 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
3305 {
3306   GstPadTemplate *new;
3307
3308   if (!name_is_valid (pad_template->name_template, pad_template->presence))
3309     return NULL;
3310
3311   new = g_object_new (gst_pad_template_get_type (),
3312       "name", pad_template->name_template, NULL);
3313
3314   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (pad_template->name_template);
3315   GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
3316   GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
3317
3318   GST_PAD_TEMPLATE_CAPS (new) =
3319       gst_caps_copy (gst_static_caps_get (&pad_template->static_caps));
3320
3321   return new;
3322 }
3323
3324 /**
3325  * gst_pad_template_new:
3326  * @name_template: the name template.
3327  * @direction: the #GstPadDirection of the template.
3328  * @presence: the #GstPadPresence of the pad.
3329  * @caps: a #GstCaps set for the template. The caps are taken ownership of.
3330  *
3331  * Creates a new pad template with a name according to the given template
3332  * and with the given arguments. This functions takes ownership of the provided
3333  * caps, so be sure to not use them afterwards.
3334  *
3335  * Returns: a new #GstPadTemplate.
3336  */
3337 GstPadTemplate *
3338 gst_pad_template_new (const gchar * name_template,
3339     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
3340 {
3341   GstPadTemplate *new;
3342
3343   g_return_val_if_fail (name_template != NULL, NULL);
3344
3345   if (!name_is_valid (name_template, presence))
3346     return NULL;
3347
3348 #if 0
3349 #ifdef USE_POISONING
3350   if (caps) {
3351     GstCaps *newcaps = gst_caps_copy (caps);
3352
3353     gst_caps_free (caps);
3354     caps = newcaps;
3355   }
3356 #endif
3357 #endif
3358   new = g_object_new (gst_pad_template_get_type (),
3359       "name", name_template, NULL);
3360
3361   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
3362   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
3363   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
3364   GST_PAD_TEMPLATE_CAPS (new) = caps;
3365
3366   return new;
3367 }
3368
3369 /**
3370  * gst_pad_template_get_caps:
3371  * @templ: a #GstPadTemplate to get capabilities of.
3372  *
3373  * Gets the capabilities of the pad template.
3374  *
3375  * Returns: the #GstCaps of the pad template. If you need to keep a reference to
3376  * the caps, make a copy (see gst_caps_copy ()).
3377  */
3378 const GstCaps *
3379 gst_pad_template_get_caps (GstPadTemplate * templ)
3380 {
3381   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
3382
3383   return GST_PAD_TEMPLATE_CAPS (templ);
3384 }
3385
3386 /**
3387  * gst_pad_set_element_private:
3388  * @pad: the #GstPad to set the private data of.
3389  * @priv: The private data to attach to the pad.
3390  *
3391  * Set the given private data gpointer on the pad. 
3392  * This function can only be used by the element that owns the pad.
3393  */
3394 void
3395 gst_pad_set_element_private (GstPad * pad, gpointer priv)
3396 {
3397   pad->element_private = priv;
3398 }
3399
3400 /**
3401  * gst_pad_get_element_private:
3402  * @pad: the #GstPad to get the private data of.
3403  *
3404  * Gets the private data of a pad.
3405  *
3406  * Returns: a #gpointer to the private data.
3407  */
3408 gpointer
3409 gst_pad_get_element_private (GstPad * pad)
3410 {
3411   return pad->element_private;
3412 }
3413
3414
3415 /***** ghost pads *****/
3416 GType _gst_ghost_pad_type = 0;
3417
3418 static void gst_ghost_pad_class_init (GstGhostPadClass * klass);
3419 static void gst_ghost_pad_init (GstGhostPad * pad);
3420 static void gst_ghost_pad_dispose (GObject * object);
3421 static void gst_ghost_pad_get_property (GObject * object, guint prop_id,
3422     GValue * value, GParamSpec * pspec);
3423 static void gst_ghost_pad_set_property (GObject * object, guint prop_id,
3424     const GValue * value, GParamSpec * pspec);
3425
3426 static GstPad *ghost_pad_parent_class = NULL;
3427
3428 /* static guint gst_ghost_pad_signals[LAST_SIGNAL] = { 0 }; */
3429 enum
3430 {
3431   GPAD_ARG_0,
3432   GPAD_ARG_REAL_PAD
3433       /* fill me */
3434 };
3435
3436 GType
3437 gst_ghost_pad_get_type (void)
3438 {
3439   if (!_gst_ghost_pad_type) {
3440     static const GTypeInfo pad_info = {
3441       sizeof (GstGhostPadClass), NULL, NULL,
3442       (GClassInitFunc) gst_ghost_pad_class_init, NULL, NULL,
3443       sizeof (GstGhostPad),
3444       8,
3445       (GInstanceInitFunc) gst_ghost_pad_init,
3446       NULL
3447     };
3448
3449     _gst_ghost_pad_type = g_type_register_static (GST_TYPE_PAD, "GstGhostPad",
3450         &pad_info, 0);
3451   }
3452   return _gst_ghost_pad_type;
3453 }
3454
3455 static void
3456 gst_ghost_pad_class_init (GstGhostPadClass * klass)
3457 {
3458   GObjectClass *gobject_class;
3459
3460   gobject_class = (GObjectClass *) klass;
3461
3462   ghost_pad_parent_class = g_type_class_ref (GST_TYPE_PAD);
3463
3464   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ghost_pad_dispose);
3465   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_ghost_pad_set_property);
3466   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ghost_pad_get_property);
3467
3468   g_object_class_install_property (gobject_class, GPAD_ARG_REAL_PAD,
3469       g_param_spec_object ("real-pad", "Real pad",
3470           "The real pad for the ghost pad", GST_TYPE_PAD, G_PARAM_READWRITE));
3471 }
3472
3473 static void
3474 gst_ghost_pad_init (GstGhostPad * pad)
3475 {
3476   /* zeroed by glib */
3477 }
3478
3479 static void
3480 gst_ghost_pad_dispose (GObject * object)
3481 {
3482   g_object_set (object, "real-pad", NULL, NULL);
3483
3484   G_OBJECT_CLASS (ghost_pad_parent_class)->dispose (object);
3485 }
3486
3487 static void
3488 gst_ghost_pad_set_property (GObject * object, guint prop_id,
3489     const GValue * value, GParamSpec * pspec)
3490 {
3491   GstPad *ghostpad = (GstPad *) object;
3492   GstPad *oldrealpad = (GstPad *) GST_GPAD_REALPAD (ghostpad);
3493   GstPad *realpad = NULL;
3494
3495   switch (prop_id) {
3496     case GPAD_ARG_REAL_PAD:
3497       realpad = g_value_get_object (value);
3498
3499       if (oldrealpad) {
3500         if (realpad == oldrealpad)
3501           return;
3502         else
3503           gst_pad_remove_ghost_pad (oldrealpad, ghostpad);
3504       }
3505
3506       if (realpad)
3507         gst_pad_add_ghost_pad (realpad, ghostpad);
3508       break;
3509
3510     default:
3511       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3512       break;
3513   }
3514 }
3515
3516 static void
3517 gst_ghost_pad_get_property (GObject * object, guint prop_id,
3518     GValue * value, GParamSpec * pspec)
3519 {
3520   switch (prop_id) {
3521     case GPAD_ARG_REAL_PAD:
3522       g_value_set_object (value, GST_GPAD_REALPAD (object));
3523       break;
3524
3525     default:
3526       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3527       break;
3528   }
3529 }
3530
3531 /**
3532  * gst_ghost_pad_new:
3533  * @name: the name of the new ghost pad.
3534  * @pad: the #GstPad to create a ghost pad for.
3535  *
3536  * Creates a new ghost pad associated with @pad, and named @name. If @name is
3537  * %NULL, a guaranteed unique name (across all ghost pads) will be assigned.
3538  *
3539  * Returns: a new ghost #GstPad, or %NULL in case of an error.
3540  */
3541 GstPad *
3542 gst_ghost_pad_new (const gchar * name, GstPad * pad)
3543 {
3544   GstPad *gpad;
3545
3546   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3547
3548   gpad = g_object_new (GST_TYPE_GHOST_PAD, "name", name, "real-pad", pad, NULL);
3549
3550   GST_CAT_DEBUG (GST_CAT_PADS, "created ghost pad \"%s\" for pad %s:%s",
3551       GST_OBJECT_NAME (gpad), GST_DEBUG_PAD_NAME (pad));
3552
3553   return gpad;
3554 }
3555
3556 /**
3557  * gst_pad_get_internal_links_default:
3558  * @pad: the #GstPad to get the internal links of.
3559  *
3560  * Gets a list of pads to which the given pad is linked to
3561  * inside of the parent element.
3562  * This is the default handler, and thus returns a list of all of the
3563  * pads inside the parent element with opposite direction.
3564  * The caller must free this list after use.
3565  *
3566  * Returns: a newly allocated #GList of pads.
3567  */
3568 GList *
3569 gst_pad_get_internal_links_default (GstPad * pad)
3570 {
3571   GList *res = NULL;
3572   GstElement *parent;
3573   GList *parent_pads;
3574   GstPadDirection direction;
3575   GstRealPad *rpad;
3576
3577   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3578
3579   rpad = GST_PAD_REALIZE (pad);
3580   direction = rpad->direction;
3581
3582   parent = GST_PAD_PARENT (rpad);
3583   parent_pads = parent->pads;
3584
3585   while (parent_pads) {
3586     GstRealPad *parent_pad = GST_PAD_REALIZE (parent_pads->data);
3587
3588     if (parent_pad->direction != direction) {
3589       res = g_list_prepend (res, parent_pad);
3590     }
3591
3592     parent_pads = g_list_next (parent_pads);
3593   }
3594
3595   return res;
3596 }
3597
3598 /**
3599  * gst_pad_get_internal_links:
3600  * @pad: the #GstPad to get the internal links of.
3601  *
3602  * Gets a list of pads to which the given pad is linked to
3603  * inside of the parent element.
3604  * The caller must free this list after use.
3605  *
3606  * Returns: a newly allocated #GList of pads.
3607  */
3608 GList *
3609 gst_pad_get_internal_links (GstPad * pad)
3610 {
3611   GList *res = NULL;
3612   GstRealPad *rpad;
3613
3614   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3615
3616   rpad = GST_PAD_REALIZE (pad);
3617
3618   if (GST_RPAD_INTLINKFUNC (rpad))
3619     res = GST_RPAD_INTLINKFUNC (rpad) (GST_PAD (rpad));
3620
3621   return res;
3622 }
3623
3624
3625 static gboolean
3626 gst_pad_event_default_dispatch (GstPad * pad, GstElement * element,
3627     GstEvent * event)
3628 {
3629   GList *orig, *pads;
3630
3631   GST_INFO_OBJECT (pad, "Sending event %p to all internally linked pads",
3632       event);
3633
3634   orig = pads = gst_pad_get_internal_links (pad);
3635
3636   while (pads) {
3637     GstPad *eventpad = GST_PAD (pads->data);
3638
3639     pads = g_list_next (pads);
3640
3641     /* for all of the internally-linked pads that are actually linked */
3642     if (GST_PAD_IS_LINKED (eventpad)) {
3643       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
3644         /* increase the refcount */
3645         gst_event_ref (event);
3646         gst_pad_push (eventpad, GST_DATA (event));
3647       } else {
3648         GstPad *peerpad = GST_PAD (GST_RPAD_PEER (eventpad));
3649
3650         /* we only send the event on one pad, multi-sinkpad elements 
3651          * should implement a handler */
3652         g_list_free (orig);
3653         return gst_pad_send_event (peerpad, event);
3654       }
3655     }
3656   }
3657   gst_event_unref (event);
3658   g_list_free (orig);
3659   return (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
3660 }
3661
3662 /**
3663  * gst_pad_event_default:
3664  * @pad: a #GstPad to call the default event handler on.
3665  * @event: the #GstEvent to handle.
3666  *
3667  * Invokes the default event handler for the given pad. End-of-stream and
3668  * discontinuity events are handled specially, and then the event is sent to all
3669  * pads internally linked to @pad. Note that if there are many possible sink
3670  * pads that are internally linked to @pad, only one will be sent an event.
3671  * Multi-sinkpad elements should implement custom event handlers.
3672  *
3673  * Returns: TRUE if the event was sent succesfully.
3674  */
3675 gboolean
3676 gst_pad_event_default (GstPad * pad, GstEvent * event)
3677 {
3678   GstElement *element;
3679
3680   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3681   g_return_val_if_fail (event != NULL, FALSE);
3682
3683   element = GST_PAD_PARENT (pad);
3684
3685   switch (GST_EVENT_TYPE (event)) {
3686     case GST_EVENT_EOS:
3687       gst_pad_event_default_dispatch (pad, element, event);
3688       gst_element_set_eos (element);
3689       break;
3690     case GST_EVENT_DISCONTINUOUS:
3691     {
3692       guint64 time;
3693
3694       if (gst_element_requires_clock (element) && element->clock) {
3695         if (gst_event_discont_get_value (event, GST_FORMAT_TIME, &time)) {
3696           gst_element_set_time (element, time);
3697         } else {
3698           GstFormat format = GST_FORMAT_TIME;
3699           guint i;
3700
3701           for (i = 0; i < event->event_data.discont.noffsets; i++) {
3702             if (gst_pad_convert (pad,
3703                     event->event_data.discont.offsets[i].format,
3704                     event->event_data.discont.offsets[i].value, &format,
3705                     &time)) {
3706               gst_element_set_time (element, time);
3707             } else if (i == event->event_data.discont.noffsets) {
3708               g_warning
3709                   ("can't adjust clock to new time when time not provided");
3710             }
3711           }
3712         }
3713       }
3714     }
3715     default:
3716       return gst_pad_event_default_dispatch (pad, element, event);
3717   }
3718   return TRUE;
3719 }
3720
3721 /**
3722  * gst_pad_dispatcher:
3723  * @pad: a #GstPad to dispatch.
3724  * @dispatch: the #GstDispatcherFunction to call.
3725  * @data: gpointer user data passed to the dispatcher function.
3726  *
3727  * Invokes the given dispatcher function on all pads that are 
3728  * internally linked to the given pad. 
3729  * The GstPadDispatcherFunction should return TRUE when no further pads 
3730  * need to be processed.
3731  *
3732  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3733  */
3734 gboolean
3735 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3736     gpointer data)
3737 {
3738   gboolean res = FALSE;
3739   GList *int_pads, *orig;
3740
3741   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3742   g_return_val_if_fail (dispatch != NULL, FALSE);
3743
3744   orig = int_pads = gst_pad_get_internal_links (pad);
3745
3746   while (int_pads) {
3747     GstRealPad *int_rpad = GST_PAD_REALIZE (int_pads->data);
3748     GstRealPad *int_peer = GST_RPAD_PEER (int_rpad);
3749
3750     if (int_peer) {
3751       res = dispatch (GST_PAD (int_peer), data);
3752       if (res)
3753         break;
3754     }
3755     int_pads = g_list_next (int_pads);
3756   }
3757
3758   g_list_free (orig);
3759
3760   return res;
3761 }
3762
3763 /**
3764  * gst_pad_send_event:
3765  * @pad: a #GstPad to send the event to.
3766  * @event: the #GstEvent to send to the pad.
3767  *
3768  * Sends the event to the pad.
3769  *
3770  * Returns: TRUE if the event was handled.
3771  */
3772 gboolean
3773 gst_pad_send_event (GstPad * pad, GstEvent * event)
3774 {
3775   gboolean success = FALSE;
3776   GstRealPad *rpad;
3777
3778   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3779   g_return_val_if_fail (event != NULL, FALSE);
3780
3781   rpad = GST_PAD_REALIZE (pad);
3782
3783   if (GST_EVENT_SRC (event) == NULL)
3784     GST_EVENT_SRC (event) = gst_object_ref (GST_OBJECT (rpad));
3785
3786   GST_CAT_DEBUG (GST_CAT_EVENT, "have event %d on pad %s:%s",
3787       GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (rpad));
3788
3789   if (GST_RPAD_EVENTHANDLER (rpad))
3790     success = GST_RPAD_EVENTHANDLER (rpad) (GST_PAD (rpad), event);
3791   else {
3792     g_warning ("pad %s:%s has no event handler", GST_DEBUG_PAD_NAME (rpad));
3793     gst_event_unref (event);
3794   }
3795
3796   return success;
3797 }
3798
3799 typedef struct
3800 {
3801   GstFormat src_format;
3802   gint64 src_value;
3803   GstFormat *dest_format;
3804   gint64 *dest_value;
3805 }
3806 GstPadConvertData;
3807
3808 static gboolean
3809 gst_pad_convert_dispatcher (GstPad * pad, GstPadConvertData * data)
3810 {
3811   return gst_pad_convert (pad, data->src_format, data->src_value,
3812       data->dest_format, data->dest_value);
3813 }
3814
3815 /**
3816  * gst_pad_convert_default:
3817  * @pad: a #GstPad to invoke the default converter on.
3818  * @src_format: the source #GstFormat.
3819  * @src_value: the source value.
3820  * @dest_format: a pointer to the destination #GstFormat.
3821  * @dest_value: a pointer to the destination value.
3822  *
3823  * Invokes the default converter on a pad. 
3824  * This will forward the call to the pad obtained 
3825  * using the internal link of
3826  * the element.
3827  *
3828  * Returns: TRUE if the conversion could be performed.
3829  */
3830 gboolean
3831 gst_pad_convert_default (GstPad * pad,
3832     GstFormat src_format, gint64 src_value,
3833     GstFormat * dest_format, gint64 * dest_value)
3834 {
3835   GstPadConvertData data;
3836
3837   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3838   g_return_val_if_fail (dest_format != NULL, FALSE);
3839   g_return_val_if_fail (dest_value != NULL, FALSE);
3840
3841   data.src_format = src_format;
3842   data.src_value = src_value;
3843   data.dest_format = dest_format;
3844   data.dest_value = dest_value;
3845
3846   return gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
3847       gst_pad_convert_dispatcher, &data);
3848 }
3849
3850 /**
3851  * gst_pad_convert:
3852  * @pad: a #GstPad to invoke the default converter on.
3853  * @src_format: the source #GstFormat.
3854  * @src_value: the source value.
3855  * @dest_format: a pointer to the destination #GstFormat.
3856  * @dest_value: a pointer to the destination value.
3857  *
3858  * Invokes a conversion on the pad.
3859  *
3860  * Returns: TRUE if the conversion could be performed.
3861  */
3862 gboolean
3863 gst_pad_convert (GstPad * pad,
3864     GstFormat src_format, gint64 src_value,
3865     GstFormat * dest_format, gint64 * dest_value)
3866 {
3867   GstRealPad *rpad;
3868
3869   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3870   g_return_val_if_fail (dest_format != NULL, FALSE);
3871   g_return_val_if_fail (dest_value != NULL, FALSE);
3872
3873   if (src_format == *dest_format) {
3874     *dest_value = src_value;
3875     return TRUE;
3876   }
3877
3878   rpad = GST_PAD_REALIZE (pad);
3879
3880   if (GST_RPAD_CONVERTFUNC (rpad)) {
3881     return GST_RPAD_CONVERTFUNC (rpad) (GST_PAD (rpad), src_format,
3882         src_value, dest_format, dest_value);
3883   }
3884
3885   return FALSE;
3886 }
3887
3888 typedef struct
3889 {
3890   GstQueryType type;
3891   GstFormat *format;
3892   gint64 *value;
3893 }
3894 GstPadQueryData;
3895
3896 static gboolean
3897 gst_pad_query_dispatcher (GstPad * pad, GstPadQueryData * data)
3898 {
3899   return gst_pad_query (pad, data->type, data->format, data->value);
3900 }
3901
3902 /**
3903  * gst_pad_query_default:
3904  * @pad: a #GstPad to invoke the default query on.
3905  * @type: the #GstQueryType of the query to perform.
3906  * @format: a pointer to the #GstFormat of the result.
3907  * @value: a pointer to the result.
3908  *
3909  * Invokes the default query function on a pad. 
3910  *
3911  * Returns: TRUE if the query could be performed.
3912  */
3913 gboolean
3914 gst_pad_query_default (GstPad * pad, GstQueryType type,
3915     GstFormat * format, gint64 * value)
3916 {
3917   GstPadQueryData data;
3918
3919   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3920   g_return_val_if_fail (format != NULL, FALSE);
3921   g_return_val_if_fail (value != NULL, FALSE);
3922
3923   data.type = type;
3924   data.format = format;
3925   data.value = value;
3926
3927   return gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
3928       gst_pad_query_dispatcher, &data);
3929 }
3930
3931 /**
3932  * gst_pad_query:
3933  * @pad: a #GstPad to invoke the default query on.
3934  * @type: the #GstQueryType of the query to perform.
3935  * @format: a pointer to the #GstFormat asked for.
3936  *          On return contains the #GstFormat used.
3937  * @value: a pointer to the result.
3938  *
3939  * Queries a pad for one of the available properties. The format will be
3940  * adjusted to the actual format used when specifying formats such as 
3941  * GST_FORMAT_DEFAULT.
3942  * FIXME: Tell if the format can be adjusted when specifying a definite format.
3943  *
3944  * Returns: TRUE if the query could be performed.
3945  */
3946 gboolean
3947 gst_pad_query (GstPad * pad, GstQueryType type,
3948     GstFormat * format, gint64 * value)
3949 {
3950   GstRealPad *rpad;
3951
3952   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3953   g_return_val_if_fail (format != NULL, FALSE);
3954   g_return_val_if_fail (value != NULL, FALSE);
3955
3956   rpad = GST_PAD_REALIZE (pad);
3957
3958   g_return_val_if_fail (rpad, FALSE);
3959
3960   if (GST_RPAD_QUERYFUNC (rpad))
3961     return GST_RPAD_QUERYFUNC (rpad) (GST_PAD (pad), type, format, value);
3962
3963   return FALSE;
3964 }
3965
3966 static gboolean
3967 gst_pad_get_formats_dispatcher (GstPad * pad, const GstFormat ** data)
3968 {
3969   *data = gst_pad_get_formats (pad);
3970
3971   return TRUE;
3972 }
3973
3974 /**
3975  * gst_pad_get_formats_default:
3976  * @pad: a #GstPad to query
3977  *
3978  * Invoke the default format dispatcher for the pad.
3979  *
3980  * Returns: An array of GstFormats ended with a 0 value.
3981  */
3982 const GstFormat *
3983 gst_pad_get_formats_default (GstPad * pad)
3984 {
3985   GstFormat *result = NULL;
3986
3987   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3988
3989   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
3990       gst_pad_get_formats_dispatcher, &result);
3991
3992   return result;
3993 }
3994
3995 /**
3996  * gst_pad_get_formats:
3997  * @pad: a #GstPad to query
3998  *
3999  * Gets the list of supported formats from the pad.
4000  *
4001  * Returns: An array of GstFormats ended with a 0 value.
4002  */
4003 const GstFormat *
4004 gst_pad_get_formats (GstPad * pad)
4005 {
4006   GstRealPad *rpad;
4007
4008   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4009
4010   rpad = GST_PAD_REALIZE (pad);
4011
4012   if (GST_RPAD_FORMATSFUNC (rpad))
4013     return GST_RPAD_FORMATSFUNC (rpad) (GST_PAD (pad));
4014
4015   return NULL;
4016 }