makes no sense as INFO
[platform/upstream/gstreamer.git] / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpad.c: Pads for connecting elements together
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* #define GST_DEBUG_ENABLED */
24 #include "gst_private.h"
25
26 #include "gstpad.h"
27 #include "gstutils.h"
28 #include "gstelement.h"
29 #include "gsttype.h"
30 #include "gstbin.h"
31 #include "gstscheduler.h"
32 #include "gstevent.h"
33
34 GType _gst_pad_type = 0;
35
36 /***** Start with the base GstPad class *****/
37 static void             gst_pad_class_init              (GstPadClass *klass);
38 static void             gst_pad_init                    (GstPad *pad);
39
40 static gboolean         gst_pad_try_reconnect_filtered_func (GstRealPad *srcpad, GstRealPad *sinkpad, 
41                                                          GstCaps *caps, gboolean clear);
42
43 #ifndef GST_DISABLE_LOADSAVE
44 static xmlNodePtr       gst_pad_save_thyself            (GstObject *object, xmlNodePtr parent);
45 #endif
46
47 static GstObject *pad_parent_class = NULL;
48
49 GType
50 gst_pad_get_type(void) 
51 {
52   if (!_gst_pad_type) {
53     static const GTypeInfo pad_info = {
54       sizeof(GstPadClass),
55       NULL,
56       NULL,
57       (GClassInitFunc)gst_pad_class_init,
58       NULL,
59       NULL,
60       sizeof(GstPad),
61       32,
62       (GInstanceInitFunc)gst_pad_init,
63       NULL
64     };
65     _gst_pad_type = g_type_register_static(GST_TYPE_OBJECT, "GstPad", &pad_info, 0);
66   }
67   return _gst_pad_type;
68 }
69
70 static void
71 gst_pad_class_init (GstPadClass *klass)
72 {
73   pad_parent_class = g_type_class_ref(GST_TYPE_OBJECT);
74 }
75
76 static void
77 gst_pad_init (GstPad *pad)
78 {
79   pad->element_private = NULL;
80
81   pad->padtemplate = NULL;
82 }
83
84
85
86 /***** Then do the Real Pad *****/
87 /* Pad signals and args */
88 enum {
89   REAL_SET_ACTIVE,
90   REAL_CAPS_CHANGED,
91   REAL_CAPS_NEGO_FAILED,
92   REAL_CONNECTED,
93   REAL_DISCONNECTED,
94   REAL_EVENT_RECEIVED,
95   /* FILL ME */
96   REAL_LAST_SIGNAL
97 };
98
99 enum {
100   REAL_ARG_0,
101   REAL_ARG_ACTIVE,
102   /* FILL ME */
103 };
104
105 static void     gst_real_pad_class_init         (GstRealPadClass *klass);
106 static void     gst_real_pad_init               (GstRealPad *pad);
107
108 static void     gst_real_pad_set_property       (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
109 static void     gst_real_pad_get_property       (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
110
111 static void     gst_real_pad_dispose            (GObject *object);
112
113 static void     gst_pad_push_func               (GstPad *pad, GstBuffer *buf);
114
115 GType _gst_real_pad_type = 0;
116
117 static GstPad *real_pad_parent_class = NULL;
118 static guint gst_real_pad_signals[REAL_LAST_SIGNAL] = { 0 };
119
120 GType
121 gst_real_pad_get_type(void) {
122   if (!_gst_real_pad_type) {
123     static const GTypeInfo pad_info = {
124       sizeof(GstRealPadClass),
125       NULL,
126       NULL,
127       (GClassInitFunc)gst_real_pad_class_init,
128       NULL,
129       NULL,
130       sizeof(GstRealPad),
131       32,
132       (GInstanceInitFunc)gst_real_pad_init,
133       NULL
134     };
135     _gst_real_pad_type = g_type_register_static(GST_TYPE_PAD, "GstRealPad", &pad_info, 0);
136   }
137   return _gst_real_pad_type;
138 }
139
140 static void
141 gst_real_pad_class_init (GstRealPadClass *klass)
142 {
143   GObjectClass *gobject_class;
144   GstObjectClass *gstobject_class;
145
146   gobject_class = (GObjectClass*) klass;
147   gstobject_class = (GstObjectClass*) klass;
148
149   real_pad_parent_class = g_type_class_ref (GST_TYPE_PAD);
150
151   gobject_class->dispose  = GST_DEBUG_FUNCPTR (gst_real_pad_dispose);
152   gobject_class->set_property  = GST_DEBUG_FUNCPTR (gst_real_pad_set_property);
153   gobject_class->get_property  = GST_DEBUG_FUNCPTR (gst_real_pad_get_property);
154
155   gst_real_pad_signals[REAL_SET_ACTIVE] =
156     g_signal_new ("set_active", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
157                     G_STRUCT_OFFSET (GstRealPadClass, set_active), NULL, NULL,
158                     gst_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1,
159                     G_TYPE_BOOLEAN);
160   gst_real_pad_signals[REAL_CAPS_CHANGED] =
161     g_signal_new ("caps_changed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
162                     G_STRUCT_OFFSET (GstRealPadClass, caps_changed), NULL, NULL,
163                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
164                     G_TYPE_POINTER);
165   gst_real_pad_signals[REAL_CAPS_NEGO_FAILED] =
166     g_signal_new ("caps_nego_failed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
167                     G_STRUCT_OFFSET (GstRealPadClass, caps_nego_failed), NULL, NULL,
168                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
169                     G_TYPE_POINTER);
170   gst_real_pad_signals[REAL_CONNECTED] =
171     g_signal_new ("connected", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
172                     G_STRUCT_OFFSET (GstRealPadClass, connected), NULL, NULL,
173                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
174                     G_TYPE_POINTER);
175   gst_real_pad_signals[REAL_DISCONNECTED] =
176     g_signal_new ("disconnected", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
177                     G_STRUCT_OFFSET (GstRealPadClass, disconnected), NULL, NULL,
178                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
179                     G_TYPE_POINTER);
180   gst_real_pad_signals[REAL_EVENT_RECEIVED] =
181     g_signal_new ("event_received", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
182                     G_STRUCT_OFFSET (GstRealPadClass, event_received), NULL, NULL,
183                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
184                     G_TYPE_POINTER);
185
186 /*  gtk_object_add_arg_type ("GstRealPad::active", G_TYPE_BOOLEAN, */
187 /*                           GTK_ARG_READWRITE, REAL_ARG_ACTIVE); */
188   g_object_class_install_property (G_OBJECT_CLASS (klass), REAL_ARG_ACTIVE,
189     g_param_spec_boolean ("active", "Active", "Whether the pad is active.",
190                           TRUE,G_PARAM_READWRITE));
191
192 #ifndef GST_DISABLE_LOADSAVE
193   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
194 #endif
195   gstobject_class->path_string_separator = ".";
196 }
197
198 static void
199 gst_real_pad_init (GstRealPad *pad)
200 {
201   pad->direction = GST_PAD_UNKNOWN;
202   pad->peer = NULL;
203
204   pad->sched = NULL;
205   pad->sched_private = NULL;
206
207   pad->chainfunc = NULL;
208   pad->getfunc = NULL;
209   pad->getregionfunc = NULL;
210
211   pad->chainhandler = GST_DEBUG_FUNCPTR (gst_pad_push_func);
212   pad->gethandler = NULL;
213   pad->pullregionfunc = NULL;
214
215   pad->bufferpoolfunc = NULL;
216   pad->ghostpads = NULL;
217   pad->caps = NULL;
218
219   pad->connectfunc = NULL;
220   pad->getcapsfunc = NULL;
221 }
222
223 static void
224 gst_real_pad_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
225 {
226   g_return_if_fail (GST_IS_PAD (object));
227
228   switch (prop_id) {
229     case REAL_ARG_ACTIVE:
230       if (g_value_get_boolean (value)) {
231         GST_DEBUG (GST_CAT_PADS, "activating pad %s:%s", GST_DEBUG_PAD_NAME (object));
232         GST_FLAG_UNSET (object, GST_PAD_DISABLED);
233       } else {
234         GST_DEBUG (GST_CAT_PADS, "de-activating pad %s:%s", GST_DEBUG_PAD_NAME (object));
235         GST_FLAG_SET (object, GST_PAD_DISABLED);
236       }
237       g_signal_emit (G_OBJECT (object), gst_real_pad_signals[REAL_SET_ACTIVE], 0,
238                       !GST_FLAG_IS_SET (object, GST_PAD_DISABLED));
239       break;
240     default:
241       break;
242   }
243 }
244
245 static void
246 gst_real_pad_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
247 {
248   /* it's not null if we got it, but it might not be ours */
249   g_return_if_fail (GST_IS_PAD (object));
250
251   switch (prop_id) {
252     case REAL_ARG_ACTIVE:
253       g_value_set_boolean (value, !GST_FLAG_IS_SET (object, GST_PAD_DISABLED));
254       break;
255     default:
256       break;
257   }
258 }
259
260
261 /**
262  * gst_pad_new:
263  * @name: name of new pad
264  * @direction: either GST_PAD_SRC or GST_PAD_SINK
265  *
266  * Create a new pad with given name.
267  *
268  * Returns: new pad
269  */
270 GstPad*
271 gst_pad_new (gchar *name,
272              GstPadDirection direction)
273 {
274   GstRealPad *pad;
275
276   g_return_val_if_fail (name != NULL, NULL);
277   g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
278
279   pad = g_object_new (gst_real_pad_get_type (), NULL);
280   gst_object_set_name (GST_OBJECT (pad), name);
281   GST_RPAD_DIRECTION (pad) = direction;
282
283   return GST_PAD (pad);
284 }
285
286 /**
287  * gst_pad_new_from_template:
288  * @templ: the pad template to use
289  * @name: the name of the element
290  *
291  * Create a new pad with given name from the given template.
292  *
293  * Returns: new pad
294  */
295 GstPad*
296 gst_pad_new_from_template (GstPadTemplate *templ,
297                            gchar *name)
298 {
299   GstPad *pad;
300
301   g_return_val_if_fail (name != NULL, NULL);
302   g_return_val_if_fail (templ != NULL, NULL);
303
304   pad = gst_pad_new (name, templ->direction);
305   
306   gst_object_ref (GST_OBJECT (templ));
307   GST_PAD_PAD_TEMPLATE (pad) = templ;
308   
309   return pad;
310 }
311
312 /**
313  * gst_pad_get_direction:
314  * @pad: the Pad to get the direction from
315  *
316  * Get the direction of the pad.
317  *
318  * Returns: the direction of the pad
319  */
320 GstPadDirection
321 gst_pad_get_direction (GstPad *pad)
322 {
323   g_return_val_if_fail (pad != NULL, GST_PAD_UNKNOWN);
324   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
325
326   return GST_PAD_DIRECTION (pad);
327 }
328
329 /**
330  * gst_pad_set_name:
331  * @pad: the pad to set the name of
332  * @name: the name of the pad
333  *
334  * Set the name of a pad.
335  */
336 void
337 gst_pad_set_name (GstPad *pad,
338                   const gchar *name)
339 {
340   g_return_if_fail (pad != NULL);
341   g_return_if_fail (GST_IS_PAD (pad));
342
343   gst_object_set_name (GST_OBJECT (pad), name);
344 }
345
346 /**
347  * gst_pad_get_name:
348  * @pad: the pad to get the name of
349  *
350  * Get the name of a pad.
351  *
352  * Returns: the name of the pad, don't free.
353  */
354 const gchar*
355 gst_pad_get_name (GstPad *pad)
356 {
357   g_return_val_if_fail (pad != NULL, NULL);
358   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
359
360   return GST_OBJECT_NAME (pad);
361 }
362
363 /**
364  * gst_pad_set_chain_function:
365  * @pad: the pad to set the chain function for
366  * @chain: the chain function
367  *
368  * Set the given chain function for the pad.
369  */
370 void 
371 gst_pad_set_chain_function (GstPad *pad,
372                             GstPadChainFunction chain)
373 {
374   g_return_if_fail (pad != NULL);
375   g_return_if_fail (GST_IS_REAL_PAD (pad));
376
377   GST_RPAD_CHAINFUNC(pad) = chain;
378   GST_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
379              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
380 }
381
382 /**
383  * gst_pad_set_get_function:
384  * @pad: the pad to set the get function for
385  * @get: the get function
386  *
387  * Set the given get function for the pad.
388  */
389 void
390 gst_pad_set_get_function (GstPad *pad,
391                           GstPadGetFunction get)
392 {
393   g_return_if_fail (pad != NULL);
394   g_return_if_fail (GST_IS_REAL_PAD (pad));
395
396   GST_RPAD_GETFUNC(pad) = get;
397   GST_DEBUG (GST_CAT_PADS, "getfunc for %s:%s  set to %s",
398              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
399 }
400
401 /**
402  * gst_pad_set_event_function:
403  * @pad: the pad to set the event handler for
404  * @event: the event handler
405  *
406  * Set the given event handler for the pad.
407  */
408 void
409 gst_pad_set_event_function (GstPad *pad,
410                             GstPadEventFunction event)
411 {
412   g_return_if_fail (pad != NULL);
413   g_return_if_fail (GST_IS_REAL_PAD (pad));
414
415   GST_RPAD_EVENTFUNC(pad) = event;
416   GST_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
417              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
418 }
419
420 /**
421  * gst_pad_set_getregion_function:
422  * @pad: the pad to set the getregion function for
423  * @getregion: the getregion function
424  *
425  * Set the given getregion function for the pad.
426  */
427 void
428 gst_pad_set_getregion_function (GstPad *pad,
429                                 GstPadGetRegionFunction getregion)
430 {
431   g_return_if_fail (pad != NULL);
432   g_return_if_fail (GST_IS_REAL_PAD (pad));
433
434   GST_RPAD_GETREGIONFUNC(pad) = getregion;
435   GST_DEBUG (GST_CAT_PADS, "getregionfunc for %s:%s set to %s",
436              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getregion));
437 }
438
439 /**
440  * gst_pad_set_connect_function:
441  * @pad: the pad to set the connect function for
442  * @connect: the connect function
443  *
444  * Set the given connect function for the pad. It will be called
445  * when the pad is connected or reconnected with caps.
446  */
447 void
448 gst_pad_set_connect_function (GstPad *pad,
449                               GstPadConnectFunction connect)
450 {
451   g_return_if_fail (pad != NULL);
452   g_return_if_fail (GST_IS_REAL_PAD (pad));
453
454   GST_RPAD_CONNECTFUNC (pad) = connect;
455   GST_DEBUG (GST_CAT_PADS, "connectfunc for %s:%s set to %s",
456              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (connect));
457 }
458
459 /**
460  * gst_pad_set_getcaps_function:
461  * @pad: the pad to set the getcaps function for
462  * @getcaps: the getcaps function
463  *
464  * Set the given getcaps function for the pad.
465  */
466 void
467 gst_pad_set_getcaps_function (GstPad *pad,
468                               GstPadGetCapsFunction getcaps)
469 {
470   g_return_if_fail (pad != NULL);
471   g_return_if_fail (GST_IS_REAL_PAD (pad));
472
473   GST_RPAD_GETCAPSFUNC (pad) = getcaps;
474   GST_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
475              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
476 }
477 /**
478  * gst_pad_set_bufferpool_function:
479  * @pad: the pad to set the bufferpool function for
480  * @bufpool: the bufferpool function
481  *
482  * Set the given bufferpool function for the pad.
483  */
484 void
485 gst_pad_set_bufferpool_function (GstPad *pad,
486                                  GstPadBufferPoolFunction bufpool)
487 {
488   g_return_if_fail (pad != NULL);
489   g_return_if_fail (GST_IS_REAL_PAD (pad));
490
491   GST_RPAD_BUFFERPOOLFUNC (pad) = bufpool;
492   GST_DEBUG (GST_CAT_PADS, "bufferpoolfunc for %s:%s set to %s",
493              GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufpool));
494 }
495
496 static void
497 gst_pad_push_func(GstPad *pad, GstBuffer *buf)
498 {
499   if (GST_RPAD_CHAINFUNC (GST_RPAD_PEER (pad)) != NULL) {
500     GST_DEBUG (GST_CAT_DATAFLOW, "calling chain function %s",
501                GST_DEBUG_FUNCPTR_NAME (GST_RPAD_CHAINFUNC (GST_RPAD_PEER (pad))));
502     (GST_RPAD_CHAINFUNC (GST_RPAD_PEER (pad))) (pad, buf);
503   } else {
504     GST_DEBUG (GST_CAT_DATAFLOW, "default pad_push handler in place, no chain function");
505     g_warning ("(internal error) default pad_push in place for pad %s:%s but it has no chain function", 
506                     GST_DEBUG_PAD_NAME (pad));
507   }
508 }
509
510
511 /**
512  * gst_pad_disconnect:
513  * @srcpad: the source pad to disconnect
514  * @sinkpad: the sink pad to disconnect
515  *
516  * Disconnects the source pad from the sink pad.
517  */
518 void
519 gst_pad_disconnect (GstPad *srcpad,
520                     GstPad *sinkpad)
521 {
522   GstRealPad *realsrc, *realsink;
523
524   /* generic checks */
525   g_return_if_fail (srcpad != NULL);
526   g_return_if_fail (GST_IS_PAD (srcpad));
527   g_return_if_fail (sinkpad != NULL);
528   g_return_if_fail (GST_IS_PAD (sinkpad));
529
530   GST_INFO (GST_CAT_ELEMENT_PADS, "disconnecting %s:%s(%p) and %s:%s(%p)",
531             GST_DEBUG_PAD_NAME (srcpad), srcpad, GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
532
533   /* now we need to deal with the real/ghost stuff */
534   realsrc = GST_PAD_REALIZE (srcpad);
535   realsink = GST_PAD_REALIZE (sinkpad);
536
537   g_return_if_fail (GST_RPAD_PEER (realsrc) != NULL);
538   g_return_if_fail (GST_RPAD_PEER (realsink) == realsrc);
539
540   if ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SINK) &&
541       (GST_RPAD_DIRECTION (realsink) == GST_PAD_SRC)) {
542     GstRealPad *temppad;
543
544     temppad = realsrc;
545     realsrc = realsink;
546     realsink = temppad;
547   }
548   g_return_if_fail ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) &&
549                     (GST_RPAD_DIRECTION (realsink) == GST_PAD_SINK));
550
551   /* first clear peers */
552   GST_RPAD_PEER (realsrc) = NULL;
553   GST_RPAD_PEER (realsink) = NULL;
554
555   /* reset the filters, both filters are refcounted once */
556   if (GST_RPAD_FILTER (realsrc)) {
557     gst_caps_unref (GST_RPAD_FILTER (realsrc));
558     GST_RPAD_FILTER (realsink) = NULL;
559     GST_RPAD_FILTER (realsrc) = NULL;
560   }
561
562   /* now tell the scheduler */
563   if (GST_PAD_PARENT (realsrc)->sched)
564     gst_scheduler_pad_disconnect (GST_PAD_PARENT (realsrc)->sched, (GstPad *)realsrc, (GstPad *)realsink);
565   else if (GST_PAD_PARENT (realsink)->sched)
566     gst_scheduler_pad_disconnect (GST_PAD_PARENT (realsink)->sched, (GstPad *)realsrc, (GstPad *)realsink);
567
568   /* hold a reference, as they can go away in the signal handlers */
569   gst_object_ref (GST_OBJECT (realsrc));
570   gst_object_ref (GST_OBJECT (realsink));
571
572   /* fire off a signal to each of the pads telling them that they've been disconnected */
573   g_signal_emit (G_OBJECT (realsrc), gst_real_pad_signals[REAL_DISCONNECTED], 0, realsink);
574   g_signal_emit (G_OBJECT (realsink), gst_real_pad_signals[REAL_DISCONNECTED], 0, realsrc);
575
576   GST_INFO (GST_CAT_ELEMENT_PADS, "disconnected %s:%s and %s:%s",
577             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
578
579   gst_object_unref (GST_OBJECT (realsrc));
580   gst_object_unref (GST_OBJECT (realsink));
581 }
582
583 /**
584  * gst_pad_can_connect_filtered:
585  * @srcpad: the source pad to connect
586  * @sinkpad: the sink pad to connect
587  * @filtercaps: the filter caps.
588  *
589  * Checks if the source pad and the sink pad can be connected. 
590  * The filter indicates the media type that should flow trought this connection.
591  *
592  * Returns: TRUE if the pad can be connected, FALSE otherwise
593  */
594 gboolean
595 gst_pad_can_connect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
596 {
597   gint num_decoupled = 0;
598   GstRealPad *realsrc, *realsink;
599
600   /* generic checks */
601   g_return_val_if_fail (srcpad != NULL, FALSE);
602   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
603   g_return_val_if_fail (sinkpad != NULL, FALSE);
604   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
605
606   /* now we need to deal with the real/ghost stuff */
607   realsrc = GST_PAD_REALIZE (srcpad);
608   realsink = GST_PAD_REALIZE (sinkpad);
609
610   g_return_val_if_fail (GST_RPAD_PEER (realsrc) == NULL, FALSE);
611   g_return_val_if_fail (GST_RPAD_PEER (realsink) == NULL, FALSE);
612   g_return_val_if_fail (GST_PAD_PARENT (realsrc) != NULL, FALSE);
613   g_return_val_if_fail (GST_PAD_PARENT (realsink) != NULL, FALSE);
614
615   if (realsrc->sched && realsink->sched) {
616     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsrc), GST_ELEMENT_DECOUPLED))
617       num_decoupled++;
618     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsink), GST_ELEMENT_DECOUPLED))
619       num_decoupled++;
620
621     if (realsrc->sched != realsink->sched && num_decoupled != 1) {
622       g_warning ("connecting pads with different scheds requires exactly one decoupled element (queue)");
623       return FALSE;
624     }
625   }
626   
627   /* check if the directions are compatible */
628   if (!(((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SINK) &&
629          (GST_RPAD_DIRECTION (realsink) == GST_PAD_SRC)) ||
630         ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) &&
631          (GST_RPAD_DIRECTION (realsink) == GST_PAD_SINK))))
632   {
633     return FALSE;
634   }
635   
636   return TRUE;
637 }
638 /**
639  * gst_pad_can_connect:
640  * @srcpad: the source pad to connect
641  * @sinkpad: the sink pad to connect
642  *
643  * Checks if the source pad can be connected to the sink pad.
644  *
645  * Returns: TRUE if the pads can be connected, FALSE otherwise
646  */
647 gboolean
648 gst_pad_can_connect (GstPad *srcpad, GstPad *sinkpad)
649 {
650   return gst_pad_can_connect_filtered (srcpad, sinkpad, NULL);
651 }
652
653 /**
654  * gst_pad_connect_filtered:
655  * @srcpad: the source pad to connect
656  * @sinkpad: the sink pad to connect
657  * @filtercaps: the filter caps.
658  *
659  * Connects the source pad to the sink pad. The filter indicates the media type
660  * that should flow trought this connection.
661  *
662  * Returns: TRUE if the pad could be connected, FALSE otherwise
663  */
664 gboolean
665 gst_pad_connect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
666 {
667   GstRealPad *realsrc, *realsink;
668   gint num_decoupled = 0;
669
670   /* generic checks */
671   g_return_val_if_fail (srcpad != NULL, FALSE);
672   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
673   g_return_val_if_fail (sinkpad != NULL, FALSE);
674   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
675
676   GST_INFO (GST_CAT_PADS, "connecting %s:%s and %s:%s",
677             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
678
679   /* now we need to deal with the real/ghost stuff */
680   realsrc = GST_PAD_REALIZE (srcpad);
681   realsink = GST_PAD_REALIZE (sinkpad);
682
683   if ((GST_PAD (realsrc) != srcpad) || (GST_PAD (realsink) != sinkpad)) {
684     GST_INFO (GST_CAT_PADS, "*actually* connecting %s:%s and %s:%s",
685               GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
686   }
687
688   g_return_val_if_fail (GST_RPAD_PEER (realsrc) == NULL, FALSE);
689   g_return_val_if_fail (GST_RPAD_PEER (realsink) == NULL, FALSE);
690   g_return_val_if_fail (GST_PAD_PARENT (realsrc) != NULL, FALSE);
691   g_return_val_if_fail (GST_PAD_PARENT (realsink) != NULL, FALSE);
692
693   if (realsrc->sched && realsink->sched) {
694     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsrc), GST_ELEMENT_DECOUPLED))
695       num_decoupled++;
696     if (GST_FLAG_IS_SET (GST_PAD_PARENT (realsink), GST_ELEMENT_DECOUPLED))
697       num_decoupled++;
698
699     if (realsrc->sched != realsink->sched && num_decoupled != 1) {
700       g_warning ("connecting pads with different scheds requires exactly one decoupled element (queue)\n");
701       return FALSE;
702     }
703   }
704
705   /* check for reversed directions and swap if necessary */
706   if ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SINK) &&
707       (GST_RPAD_DIRECTION (realsink) == GST_PAD_SRC)) {
708     GstRealPad *temppad;
709
710     temppad = realsrc;
711     realsrc = realsink;
712     realsink = temppad;
713   }
714   g_return_val_if_fail ((GST_RPAD_DIRECTION (realsrc) == GST_PAD_SRC) &&
715                         (GST_RPAD_DIRECTION (realsink) == GST_PAD_SINK), FALSE);
716
717   /* first set peers */
718   GST_RPAD_PEER (realsrc) = realsink;
719   GST_RPAD_PEER (realsink) = realsrc;
720
721   /* try to negotiate the pads, we don't need to clear the caps here */
722   if (!gst_pad_try_reconnect_filtered_func (realsrc, realsink, filtercaps, FALSE)) {
723     GST_DEBUG (GST_CAT_CAPS, "pads cannot connect");
724
725     GST_RPAD_PEER (realsrc) = NULL;
726     GST_RPAD_PEER (realsink) = NULL;
727
728     return FALSE;
729   }
730
731   /* fire off a signal to each of the pads telling them that they've been connected */
732   g_signal_emit (G_OBJECT (realsrc), gst_real_pad_signals[REAL_CONNECTED], 0, realsink);
733   g_signal_emit (G_OBJECT (realsink), gst_real_pad_signals[REAL_CONNECTED], 0, realsrc);
734
735   /* now tell the scheduler(s) */
736   if (realsrc->sched)
737     gst_scheduler_pad_connect (realsrc->sched, (GstPad *)realsrc, (GstPad *)realsink);
738   else if (realsink->sched)
739     gst_scheduler_pad_connect (realsink->sched, (GstPad *)realsrc, (GstPad *)realsink);
740
741   GST_INFO (GST_CAT_PADS, "connected %s:%s and %s:%s",
742             GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
743   gst_caps_debug (gst_pad_get_caps (GST_PAD_CAST (realsrc)), "caps of newly connected src pad");
744
745   return TRUE;
746 }
747
748 /**
749  * gst_pad_connect:
750  * @srcpad: the source pad to connect
751  * @sinkpad: the sink pad to connect
752  *
753  * Connects the source pad to the sink pad.
754  *
755  * Returns: TRUE if the pad could be connected, FALSE otherwise
756  */
757 gboolean
758 gst_pad_connect (GstPad *srcpad, GstPad *sinkpad)
759 {
760   return gst_pad_connect_filtered (srcpad, sinkpad, NULL);
761 }
762
763 /**
764  * gst_pad_set_parent:
765  * @pad: the pad to set the parent
766  * @parent: the object to set the parent to
767  *
768  * Sets the parent object of a pad.
769  */
770 void
771 gst_pad_set_parent (GstPad *pad,
772                     GstObject *parent)
773 {
774   g_return_if_fail (pad != NULL);
775   g_return_if_fail (GST_IS_PAD (pad));
776   g_return_if_fail (GST_PAD_PARENT (pad) == NULL);
777   g_return_if_fail (parent != NULL);
778   g_return_if_fail (GST_IS_OBJECT (parent));
779   g_return_if_fail ((gpointer)pad != (gpointer)parent);
780
781   gst_object_set_parent (GST_OBJECT (pad), parent);
782 }
783
784 /**
785  * gst_pad_get_parent:
786  * @pad: the pad to get the parent from
787  *
788  * Get the parent object of this pad.
789  *
790  * Returns: the parent object
791  */
792 GstElement*
793 gst_pad_get_parent (GstPad *pad)
794 {
795   g_return_val_if_fail (pad != NULL, NULL);
796   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
797
798   return GST_PAD_PARENT (pad);
799 }
800
801 /**
802  * gst_pad_get_pad_template:
803  * @pad: the pad to get the padtemplate from
804  *
805  * Get the padtemplate object of this pad.
806  *
807  * Returns: the padtemplate object
808  */
809 GstPadTemplate*
810 gst_pad_get_pad_template (GstPad *pad)
811 {
812   g_return_val_if_fail (pad != NULL, NULL);
813   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
814
815   return GST_PAD_PAD_TEMPLATE (pad); 
816 }
817
818
819 /**
820  * gst_pad_set_sched:
821  * @pad: the pad to set the scheduler for
822  * @sched: The scheduler to set
823  *
824  * Set the scheduler for the pad
825  */
826 void
827 gst_pad_set_sched (GstPad *pad, GstScheduler *sched)
828 {
829   g_return_if_fail (pad != NULL);
830   g_return_if_fail (GST_IS_PAD (pad));
831  
832   GST_RPAD_SCHED(pad) = sched;
833 }
834  
835 /**
836  * gst_pad_get_sched:
837  * @pad: the pad to get the scheduler from
838  *
839  * Get the scheduler of the pad
840  *
841  * Returns: the scheduler of the pad.
842  */
843 GstScheduler*
844 gst_pad_get_sched (GstPad *pad)
845 {
846   g_return_val_if_fail (pad != NULL, NULL);
847   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
848  
849   return GST_RPAD_SCHED(pad);
850 }
851
852 /**
853  * gst_pad_unset_sched:
854  * @pad: the pad to unset the scheduler for
855  *
856  * Unset the scheduler for the pad
857  */
858 void
859 gst_pad_unset_sched (GstPad *pad)
860 {
861   g_return_if_fail (pad != NULL);
862   g_return_if_fail (GST_IS_PAD (pad));
863  
864   GST_RPAD_SCHED(pad) = NULL;
865 }
866  
867 /**
868  * gst_pad_get_real_parent:
869  * @pad: the pad to get the parent from
870  *
871  * Get the real parent object of this pad. If the pad
872  * is a ghostpad, the actual owner of the real pad is
873  * returned, as opposed to the gst_pad_get_parent().
874  *
875  * Returns: the parent object
876  */
877 GstElement*
878 gst_pad_get_real_parent (GstPad *pad)
879 {
880   g_return_val_if_fail (pad != NULL, NULL);
881   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
882
883   return GST_PAD_PARENT (GST_PAD (GST_PAD_REALIZE (pad)));
884 }
885
886 /**
887  * gst_pad_add_ghost_pad:
888  * @pad: the pad to set the ghost parent
889  * @ghostpad: the ghost pad to add
890  *
891  * Add a ghost pad to a pad.
892  */
893 void
894 gst_pad_add_ghost_pad (GstPad *pad,
895                        GstPad *ghostpad)
896 {
897   GstRealPad *realpad;
898
899   g_return_if_fail (pad != NULL);
900   g_return_if_fail (GST_IS_PAD (pad));
901   g_return_if_fail (ghostpad != NULL);
902   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
903
904   realpad = GST_PAD_REALIZE (pad);
905
906   realpad->ghostpads = g_list_prepend (realpad->ghostpads, ghostpad);
907 }
908
909
910 /**
911  * gst_pad_remove_ghost_pad:
912  * @pad: the pad to remove the ghost parent
913  * @ghostpad: the ghost pad to remove from the pad
914  *
915  * Remove a ghost pad from a pad.
916  */
917 void
918 gst_pad_remove_ghost_pad (GstPad *pad,
919                           GstPad *ghostpad)
920 {
921   GstRealPad *realpad;
922
923   g_return_if_fail (pad != NULL);
924   g_return_if_fail (GST_IS_PAD (pad));
925   g_return_if_fail (ghostpad != NULL);
926   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
927
928   realpad = GST_PAD_REALIZE (pad);
929
930   realpad->ghostpads = g_list_remove (realpad->ghostpads, ghostpad);
931 }
932
933 /**
934  * gst_pad_get_ghost_pad_list:
935  * @pad: the pad to get the ghost parents from
936  *
937  * Get the ghost parents of this pad.
938  *
939  * Returns: a GList of ghost pads
940  */
941 GList*
942 gst_pad_get_ghost_pad_list (GstPad *pad)
943 {
944   g_return_val_if_fail (pad != NULL, NULL);
945   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
946
947   return GST_PAD_REALIZE(pad)->ghostpads;
948 }
949
950 /* an internal caps negotiation helper function does:
951  * 
952  * 1. optinally calls the pad connect function with the provided caps
953  * 2. deal with the result code of the connect function
954  * 3. set fixed caps on the pad.
955  */
956 static GstPadConnectReturn
957 gst_pad_try_set_caps_func (GstRealPad *pad, GstCaps *caps, gboolean notify)
958 {
959   GstCaps *oldcaps;
960   GstPadTemplate *template;
961   GstElement *parent = GST_PAD_PARENT (pad);
962
963   /* thomas: FIXME: is this the right result to return ? */
964   g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_REFUSED);
965   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_CONNECT_REFUSED);
966   
967   /* if this pad has a parent and the parent is not READY, delay the
968    * negotiation */
969   if (parent && GST_STATE (parent) < GST_STATE_READY)
970   {
971     GST_DEBUG (GST_CAT_CAPS, "parent %s of pad %s:%s is not ready",
972                GST_ELEMENT_NAME (parent), GST_DEBUG_PAD_NAME (pad));
973     return GST_PAD_CONNECT_DELAYED;
974   }
975           
976   GST_INFO (GST_CAT_CAPS, "trying to set caps %p on pad %s:%s",
977             caps, GST_DEBUG_PAD_NAME (pad));
978   
979   if ((template = gst_pad_get_pad_template (GST_PAD_CAST (pad)))) {
980     if (!gst_caps_intersect (caps, gst_pad_template_get_caps (template))) {
981       GST_INFO (GST_CAT_CAPS, "caps did not intersect with %s:%s's padtemplate",
982                 GST_DEBUG_PAD_NAME (pad));
983       gst_caps_debug (caps, "caps themselves (attemped to set)");
984       gst_caps_debug (gst_pad_template_get_caps (template),
985                       "pad template caps that did not agree with caps");
986       return GST_PAD_CONNECT_REFUSED;
987     }
988     /* given that the caps are fixed, we know that their intersection with the
989      * padtemplate caps is the same as caps itself */
990   }
991
992   /* we need to notify the connect function */
993   if (notify && GST_RPAD_CONNECTFUNC (pad)) {
994     GstPadConnectReturn res;
995     gchar *debug_string;
996
997     GST_INFO (GST_CAT_CAPS, "calling connect function on pad %s:%s",
998             GST_DEBUG_PAD_NAME (pad));
999
1000     /* call the connect function */
1001     res = GST_RPAD_CONNECTFUNC (pad) (GST_PAD (pad), caps);
1002
1003     switch (res) {
1004       case GST_PAD_CONNECT_REFUSED:
1005         debug_string = "REFUSED";
1006         break;
1007       case GST_PAD_CONNECT_OK:
1008         debug_string = "OK";
1009         break;
1010       case GST_PAD_CONNECT_DONE:
1011         debug_string = "DONE";
1012         break;
1013       case GST_PAD_CONNECT_DELAYED:
1014         debug_string = "DELAYED";
1015         break;
1016       default:
1017         g_warning ("unknown return code from connect function of pad %s:%s %d",
1018             GST_DEBUG_PAD_NAME (pad), res);
1019         return GST_PAD_CONNECT_REFUSED;
1020     }
1021
1022     GST_INFO (GST_CAT_CAPS, "got reply %s (%d) from connect function on pad %s:%s",
1023             debug_string, res, GST_DEBUG_PAD_NAME (pad));
1024
1025     /* done means the connect function called another caps negotiate function
1026      * on this pad that succeeded, we dont need to continue */
1027     if (res == GST_PAD_CONNECT_DONE) {
1028       GST_INFO (GST_CAT_CAPS, "pad %s:%s is done", GST_DEBUG_PAD_NAME (pad));
1029       return GST_PAD_CONNECT_DONE;
1030     }
1031     if (res == GST_PAD_CONNECT_REFUSED) {
1032       GST_INFO (GST_CAT_CAPS, "pad %s:%s doesn't accept caps",
1033                     GST_DEBUG_PAD_NAME (pad));
1034       return GST_PAD_CONNECT_REFUSED;
1035     }
1036   }
1037   /* we can only set caps on the pad if they are fixed */
1038   if (GST_CAPS_IS_FIXED (caps)) {
1039
1040     GST_INFO (GST_CAT_CAPS, "setting caps on pad %s:%s",
1041               GST_DEBUG_PAD_NAME (pad));
1042     /* if we got this far all is ok, remove the old caps, set the new one */
1043     oldcaps = GST_PAD_CAPS (pad);
1044     if (caps) gst_caps_ref (caps);
1045     GST_PAD_CAPS (pad) = caps;
1046     if (oldcaps) gst_caps_unref (oldcaps);
1047   }
1048   else {
1049     GST_INFO (GST_CAT_CAPS, "caps are not fixed on pad %s:%s, not setting them yet",
1050               GST_DEBUG_PAD_NAME (pad));
1051   }
1052
1053   return GST_PAD_CONNECT_OK;
1054 }
1055
1056 /**
1057  * gst_pad_try_set_caps:
1058  * @pad: the pad to try to set the caps on
1059  * @caps: the caps to set
1060  *
1061  * Try to set the caps on the given pad.
1062  *
1063  * Returns: TRUE if the caps could be set
1064  */
1065 gboolean
1066 gst_pad_try_set_caps (GstPad *pad, GstCaps *caps)
1067 {
1068   GstRealPad *peer, *realpad;
1069
1070   realpad = GST_PAD_REALIZE (pad);
1071   peer = GST_RPAD_PEER (realpad);
1072
1073   GST_INFO (GST_CAT_CAPS, "trying to set caps %p on pad %s:%s",
1074             caps, GST_DEBUG_PAD_NAME (realpad));
1075
1076   gst_caps_debug (caps, "caps that we are trying to set");
1077
1078   /* setting non fixed caps on a pad is not allowed */
1079   if (!GST_CAPS_IS_FIXED (caps)) {
1080   GST_INFO (GST_CAT_CAPS, "trying to set unfixed caps on pad %s:%s, not allowed",
1081                   GST_DEBUG_PAD_NAME (realpad));
1082     g_warning ("trying to set non fixed caps on pad %s:%s, not allowed",
1083             GST_DEBUG_PAD_NAME (realpad));
1084     gst_caps_debug (caps, "unfixed caps");
1085     return FALSE;
1086   }
1087
1088   /* if we have a peer try to set the caps, notifying the peerpad
1089    * if it has a connect function */
1090   if (peer && (gst_pad_try_set_caps_func (peer, caps, TRUE) != GST_PAD_CONNECT_OK))
1091   {
1092     GST_INFO (GST_CAT_CAPS, "tried to set caps on peerpad %s:%s but couldn't",
1093               GST_DEBUG_PAD_NAME (peer));
1094     return FALSE;
1095   }
1096
1097   /* then try to set our own caps, we don't need to be notified */
1098   if (gst_pad_try_set_caps_func (realpad, caps, FALSE) != GST_PAD_CONNECT_OK)
1099   {
1100     GST_INFO (GST_CAT_CAPS, "tried to set own caps on pad %s:%s but couldn't",
1101               GST_DEBUG_PAD_NAME (realpad));
1102     return FALSE;
1103   }
1104   GST_INFO (GST_CAT_CAPS, "succeeded setting caps %p on pad %s:%s",
1105             caps, GST_DEBUG_PAD_NAME (realpad));
1106   g_assert (GST_PAD_CAPS (pad));
1107                           
1108   return TRUE;
1109 }
1110
1111 /* this is a caps negotiation convenience routine, it performs:
1112  *
1113  * 1. optionally clear any pad caps
1114  * 2. calculate the intersection between the two pad tamplate/getcaps caps
1115  * 3. calculate the intersection with the (optional) filtercaps.
1116  * 4. store the intersection in the pad filter
1117  * 5. store the app filtercaps in the pad appfilter.
1118  * 6. start the caps negotiation.
1119  */
1120 static gboolean
1121 gst_pad_try_reconnect_filtered_func (GstRealPad *srcpad, GstRealPad *sinkpad, GstCaps *filtercaps, gboolean clear)
1122 {
1123   GstCaps *srccaps, *sinkcaps;
1124   GstCaps *intersection = NULL;
1125   GstRealPad *realsrc, *realsink;
1126
1127   realsrc = GST_PAD_REALIZE (srcpad);
1128   realsink = GST_PAD_REALIZE (sinkpad);
1129
1130   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1131   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1132
1133   /* optinally clear the caps */
1134   if (clear) {
1135     GST_INFO (GST_CAT_PADS, "reconnect filtered %s:%s and %s:%s, clearing caps",
1136         GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1137
1138     GST_PAD_CAPS (GST_PAD (realsrc)) = NULL;
1139     GST_PAD_CAPS (GST_PAD (realsink)) = NULL;
1140   }
1141   else {
1142     GST_INFO (GST_CAT_PADS, "reconnect filtered %s:%s and %s:%s",
1143         GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1144   }
1145
1146   srccaps = gst_pad_get_caps (GST_PAD (realsrc));
1147   GST_DEBUG (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsrc));
1148   gst_caps_debug (srccaps, "caps of src pad (pre-reconnect)");
1149   sinkcaps = gst_pad_get_caps (GST_PAD (realsink));
1150   GST_DEBUG (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsink));
1151   gst_caps_debug (sinkcaps, "caps of sink pad (pre-reconnect)");
1152
1153   /* first take the intersection of the pad caps */
1154   intersection = gst_caps_intersect (srccaps, sinkcaps);
1155
1156   /* if we have no intersection but one of the caps was not NULL.. */
1157   if (!intersection && (srccaps || sinkcaps)) {
1158     /* the intersection is NULL but the pad caps were not both NULL,
1159      * this means they have no common format */
1160     GST_INFO (GST_CAT_PADS, "pads %s:%s and %s:%s have no common type",
1161          GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1162     return FALSE;
1163   } else if (intersection) {
1164     GST_INFO (GST_CAT_PADS, "pads %s:%s and %s:%s intersected to %s caps",
1165          GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink), 
1166          ((intersection && GST_CAPS_IS_FIXED (intersection)) ? "fixed" : "variable"));
1167
1168     /* then filter this against the app filter */
1169     if (filtercaps) {
1170       GstCaps *filtered_intersection = gst_caps_intersect (intersection, filtercaps);
1171
1172       /* get rid of the old intersection here */
1173       gst_caps_unref (intersection);
1174
1175       if (!filtered_intersection) {
1176         GST_INFO (GST_CAT_PADS, "filtered connection between pads %s:%s and %s:%s is empty",
1177              GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1178         return FALSE;
1179       }
1180       intersection = filtered_intersection;
1181
1182       /* keep a reference to the app caps */
1183       GST_RPAD_APPFILTER (realsink) = filtercaps;
1184       GST_RPAD_APPFILTER (realsrc) = filtercaps;
1185     }
1186   }
1187   GST_DEBUG (GST_CAT_CAPS, "setting filter for connection to:");
1188   gst_caps_debug (intersection, "filter for connection");
1189
1190   /* both the app filter and the filter, while stored on both peer pads, are the
1191      equal to the same thing on both */
1192   GST_RPAD_FILTER (realsrc) = intersection; 
1193   GST_RPAD_FILTER (realsink) = intersection; 
1194
1195   return gst_pad_perform_negotiate (GST_PAD (realsrc), GST_PAD (realsink));
1196 }
1197
1198 /**
1199  * gst_pad_perform_negotiate:
1200  * @srcpad: a srcpad
1201  * @sinkpad: a sinkpad 
1202  *
1203  * Try to negotiate the pads.
1204  *
1205  * Returns: a boolean indicating the pad succesfully negotiated.
1206  */
1207 gboolean
1208 gst_pad_perform_negotiate (GstPad *srcpad, GstPad *sinkpad) 
1209 {
1210   GstCaps *intersection, *filtered_intersection;
1211   GstRealPad *realsrc, *realsink;
1212   GstCaps *srccaps, *sinkcaps, *filter;
1213
1214   g_return_val_if_fail (srcpad != NULL, FALSE);
1215   g_return_val_if_fail (sinkpad != NULL, FALSE);
1216   
1217   realsrc = GST_PAD_REALIZE (srcpad);
1218   realsink = GST_PAD_REALIZE (sinkpad);
1219     
1220   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1221   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1222
1223   filter = GST_RPAD_APPFILTER (realsrc);
1224   if (filter) {
1225     GST_INFO (GST_CAT_PADS, "dumping filter for connection %s:%s-%s:%s",
1226               GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1227     gst_caps_debug (filter, "connection filter caps");
1228   }
1229
1230   /* calculate the new caps here */
1231   srccaps = gst_pad_get_caps (GST_PAD (realsrc));
1232   GST_DEBUG (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsrc));
1233   gst_caps_debug (srccaps, "src caps, awaiting negotiation, after applying filter");
1234   sinkcaps = gst_pad_get_caps (GST_PAD (realsink));
1235   GST_DEBUG (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsink));
1236   gst_caps_debug (sinkcaps, "sink caps, awaiting negotiation, after applying filter");
1237   intersection = gst_caps_intersect (srccaps, sinkcaps);
1238   filtered_intersection = gst_caps_intersect (intersection, filter);
1239   if (filtered_intersection) {
1240     gst_caps_unref (intersection);
1241     intersection = filtered_intersection;
1242   }
1243
1244   /* no negotiation is performed if the pads have filtercaps */
1245   if (intersection) {
1246     GstPadConnectReturn res;
1247
1248     res = gst_pad_try_set_caps_func (realsrc, intersection, TRUE);
1249     if (res == GST_PAD_CONNECT_REFUSED) 
1250       return FALSE;
1251     if (res == GST_PAD_CONNECT_DONE) 
1252       return TRUE;
1253
1254     res = gst_pad_try_set_caps_func (realsink, intersection, TRUE);
1255     if (res == GST_PAD_CONNECT_REFUSED) 
1256       return FALSE;
1257     if (res == GST_PAD_CONNECT_DONE) 
1258       return TRUE;
1259   }
1260   return TRUE;
1261 }
1262
1263 /**
1264  * gst_pad_try_reconnect_filtered:
1265  * @srcpad: the source"pad to reconnect
1266  * @sinkpad: the sink pad to reconnect
1267  * @filtercaps: the capabilities to use in the reconnectiong
1268  *
1269  * Try to reconnect this pad and its peer with the specified caps
1270  *
1271  * Returns: a boolean indicating the peer pad could accept the caps.
1272  */
1273 gboolean
1274 gst_pad_try_reconnect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
1275 {
1276   GstRealPad *realsrc, *realsink;
1277
1278   g_return_val_if_fail (srcpad != NULL, FALSE);
1279   g_return_val_if_fail (sinkpad != NULL, FALSE);
1280
1281   realsrc = GST_PAD_REALIZE (srcpad);
1282   realsink = GST_PAD_REALIZE (sinkpad);
1283
1284   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1285   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1286   
1287   return gst_pad_try_reconnect_filtered_func (realsrc, realsink, filtercaps, TRUE);
1288 }
1289
1290 /**
1291  * gst_pad_reconnect_filtered:
1292  * @srcpad: the source"pad to reconnect
1293  * @sinkpad: the sink pad to reconnect
1294  * @filtercaps: the capabilities to use in the reconnectiong
1295  *
1296  * Try to reconnect this pad and its peer with the specified caps. 
1297  *
1298  * Returns: a boolean indicating the peer pad could accept the caps.
1299  *    if FALSE is returned, the pads are disconnected.
1300  */
1301 gboolean
1302 gst_pad_reconnect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
1303 {
1304   GstRealPad *realsrc, *realsink;
1305
1306   g_return_val_if_fail (srcpad != NULL, FALSE);
1307   g_return_val_if_fail (sinkpad != NULL, FALSE);
1308
1309   realsrc = GST_PAD_REALIZE (srcpad);
1310   realsink = GST_PAD_REALIZE (sinkpad);
1311
1312   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1313   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1314   
1315   if (!gst_pad_try_reconnect_filtered_func (realsrc, realsink, filtercaps, TRUE)) {
1316     gst_pad_disconnect (srcpad, GST_PAD (GST_PAD_PEER (srcpad)));
1317     return FALSE;
1318   }
1319   return TRUE;
1320 }
1321
1322 /**
1323  * gst_pad_proxy_connect:
1324  * @pad: the pad to proxy to
1325  * @caps: the capabilities to use in the proxying
1326  *
1327  * Proxy the connect function to the specified pad.
1328  *
1329  * Returns: a boolean indicating the peer pad could accept the caps.
1330  */
1331 GstPadConnectReturn
1332 gst_pad_proxy_connect (GstPad *pad, GstCaps *caps)
1333 {
1334   GstRealPad *peer, *realpad;
1335
1336   realpad = GST_PAD_REALIZE (pad);
1337
1338   peer = GST_RPAD_PEER (realpad);
1339
1340   GST_INFO (GST_CAT_CAPS, "proxy connect to pad %s:%s",
1341             GST_DEBUG_PAD_NAME (realpad));
1342
1343   if (peer && gst_pad_try_set_caps_func (peer, caps, TRUE) < 0)
1344     return GST_PAD_CONNECT_REFUSED;
1345   if (gst_pad_try_set_caps_func (realpad, caps, FALSE) < 0)
1346     return GST_PAD_CONNECT_REFUSED;
1347
1348   return GST_PAD_CONNECT_OK;
1349 }
1350
1351 /**
1352  * gst_pad_get_caps:
1353  * @pad: the pad to get the capabilities from
1354  *
1355  * Get the capabilities of this pad.
1356  *
1357  * Returns: the capabilities of this pad
1358  */
1359 GstCaps*
1360 gst_pad_get_caps (GstPad *pad)
1361 {
1362   GstRealPad *realpad;
1363
1364   g_return_val_if_fail (pad != NULL, NULL);
1365   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1366
1367   realpad = GST_PAD_REALIZE (pad);
1368
1369   GST_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1370             GST_DEBUG_PAD_NAME (realpad), realpad);
1371
1372   if (GST_PAD_CAPS (realpad)) {
1373     GST_DEBUG (GST_CAT_CAPS, "using pad real caps");
1374     return GST_PAD_CAPS (realpad);
1375   }
1376   else if GST_RPAD_GETCAPSFUNC (realpad) {
1377     GST_DEBUG (GST_CAT_CAPS, "using pad get function");
1378     return GST_RPAD_GETCAPSFUNC (realpad) (GST_PAD_CAST (realpad), NULL);
1379   }
1380   else if (GST_PAD_PAD_TEMPLATE (realpad)) {
1381     GST_DEBUG (GST_CAT_CAPS, "using pad template");
1382     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (realpad));
1383   }
1384   GST_DEBUG (GST_CAT_CAPS, "pad has no caps");
1385
1386   return NULL;
1387 }
1388
1389 /**
1390  * gst_pad_get_pad_template_caps:
1391  * @pad: the pad to get the capabilities from
1392  *
1393  * Get the capabilities of this pad.
1394  *
1395  * Returns: a list of the capabilities of this pad
1396  */
1397 GstCaps*
1398 gst_pad_get_pad_template_caps (GstPad *pad)
1399 {
1400   g_return_val_if_fail (pad != NULL, NULL);
1401   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1402
1403   if (GST_PAD_PAD_TEMPLATE (pad))
1404     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1405
1406   return NULL;
1407 }
1408
1409
1410 /**
1411  * gst_pad_template_get_caps_by_name:
1412  * @templ: the padtemplate to get the capabilities from
1413  * @name: the name of the capability to get
1414  *
1415  * Get the capability with the given name from this padtemplate.
1416  *
1417  * Returns: a capability or NULL if not found
1418  */
1419 GstCaps*
1420 gst_pad_template_get_caps_by_name (GstPadTemplate *templ, const gchar *name)
1421 {
1422   GstCaps *caps;
1423
1424   g_return_val_if_fail (templ != NULL, NULL);
1425
1426   caps = GST_PAD_TEMPLATE_CAPS (templ);
1427   if (!caps) 
1428     return NULL;
1429
1430   return gst_caps_get_by_name (caps, name);
1431 }
1432
1433 /**
1434  * gst_pad_check_compatibility:
1435  * @srcpad: the srcpad to check
1436  * @sinkpad: the sinkpad to check against
1437  *
1438  * Check if two pads have compatible capabilities.
1439  *
1440  * Returns: TRUE if they are compatible or the capabilities
1441  * could not be checked
1442  */
1443 gboolean
1444 gst_pad_check_compatibility (GstPad *srcpad, GstPad *sinkpad)
1445 {
1446   g_return_val_if_fail (srcpad != NULL, FALSE);
1447   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1448   g_return_val_if_fail (sinkpad != NULL, FALSE);
1449   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1450
1451   if (GST_PAD_CAPS (srcpad) && GST_PAD_CAPS (sinkpad)) {
1452     if (!gst_caps_check_compatibility (GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad))) {
1453       return FALSE;
1454     }
1455     else {
1456       return TRUE;
1457     }
1458   }
1459   else {
1460     GST_DEBUG (GST_CAT_PADS, "could not check capabilities of pads (%s:%s) and (%s:%s) %p %p",
1461                     GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad), 
1462                     GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad));
1463     return TRUE;
1464   }
1465 }
1466
1467 /**
1468  * gst_pad_get_peer:
1469  * @pad: the pad to get the peer from
1470  *
1471  * Get the peer pad of this pad.
1472  *
1473  * Returns: the peer pad
1474  */
1475 GstPad*
1476 gst_pad_get_peer (GstPad *pad)
1477 {
1478   g_return_val_if_fail (pad != NULL, NULL);
1479   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1480
1481   return GST_PAD (GST_PAD_PEER (pad));
1482 }
1483
1484 /**
1485  * gst_pad_get_allowed_caps:
1486  * @pad: the pad to get the allowed caps from
1487  *
1488  * Get the caps of the allowed media types that can
1489  * go through this pad.
1490  *
1491  * Returns: the allowed caps, newly allocated
1492  */
1493 GstCaps*
1494 gst_pad_get_allowed_caps (GstPad *pad)
1495 {
1496   g_return_val_if_fail (pad != NULL, NULL);
1497   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1498
1499   GST_DEBUG (GST_CAT_PROPERTIES, "get allowed caps of %s:%s", GST_DEBUG_PAD_NAME (pad));
1500
1501   return gst_caps_copy (GST_RPAD_FILTER (pad));
1502 }
1503
1504 /**
1505  * gst_pad_recalc_allowed_caps:
1506  * @pad: the pad to recaculate the caps of
1507  *
1508  * Attempt to reconnect the pad to its peer through its filter, 
1509  * set with gst_pad_[re]connect_filtered. This function is useful when a
1510  * plugin has new capabilities on a pad and wants to notify the peer.
1511  *
1512  * Returns: TRUE on success, FALSE otherwise.
1513  */
1514 gboolean
1515 gst_pad_recalc_allowed_caps (GstPad *pad)
1516 {
1517   GstRealPad *peer;
1518
1519   g_return_val_if_fail (pad != NULL, FALSE);
1520   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1521
1522   GST_DEBUG (GST_CAT_PROPERTIES, "set allowed caps of %s:%s", GST_DEBUG_PAD_NAME (pad));
1523
1524   peer = GST_RPAD_PEER (pad);
1525   if (peer)
1526     return gst_pad_try_reconnect_filtered (pad, GST_PAD (peer), GST_RPAD_APPFILTER (pad));
1527
1528   return TRUE;
1529 }
1530
1531 /**
1532  * gst_pad_get_bufferpool:
1533  * @pad: the pad to get the bufferpool from
1534  *
1535  * Get the bufferpool of the peer pad of the given
1536  * pad.
1537  *
1538  * Returns: The GstBufferPool or NULL.
1539  */
1540 GstBufferPool*          
1541 gst_pad_get_bufferpool (GstPad *pad)
1542 {
1543   GstRealPad *peer;
1544
1545   g_return_val_if_fail (pad != NULL, NULL);
1546   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1547    
1548   peer = GST_RPAD_PEER (pad);
1549
1550   if (!peer)
1551     return NULL;
1552
1553   GST_DEBUG_ENTER ("(%s:%s)", GST_DEBUG_PAD_NAME (pad));
1554
1555   if (peer->bufferpoolfunc) {
1556     GST_DEBUG (GST_CAT_PADS, "calling bufferpoolfunc &%s (@%p) of peer pad %s:%s",
1557       GST_DEBUG_FUNCPTR_NAME (peer->bufferpoolfunc), &peer->bufferpoolfunc, GST_DEBUG_PAD_NAME (((GstPad*) peer)));
1558     return (peer->bufferpoolfunc) (((GstPad*) peer));
1559   } else {
1560     GST_DEBUG (GST_CAT_PADS, "no bufferpoolfunc for peer pad %s:%s at %p",
1561                     GST_DEBUG_PAD_NAME (((GstPad*) peer)), &peer->bufferpoolfunc);
1562     return NULL;
1563   }
1564 }
1565
1566 static void
1567 gst_real_pad_dispose (GObject *object)
1568 {
1569   GstPad *pad = GST_PAD (object);
1570   
1571   /* No connected pad can ever be disposed.
1572    * It has to have a parent to be connected and a parent would hold a reference */
1573   g_assert (GST_PAD_PEER (pad) == NULL);
1574
1575   GST_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s", GST_DEBUG_PAD_NAME(pad));
1576
1577   if (GST_PAD_PAD_TEMPLATE (pad)){
1578     GST_DEBUG (GST_CAT_REFCOUNTING, "unreffing padtemplate'%s'", GST_OBJECT_NAME (GST_PAD_PAD_TEMPLATE (pad)));
1579     gst_object_unref (GST_OBJECT (GST_PAD_PAD_TEMPLATE (pad)));
1580     GST_PAD_PAD_TEMPLATE (pad) = NULL;
1581   }
1582   
1583   /* we destroy the ghostpads, because they are nothing without the real pad  */
1584   if (GST_REAL_PAD (pad)->ghostpads) {
1585     GList *orig, *ghostpads;
1586
1587     orig = ghostpads = g_list_copy (GST_REAL_PAD (pad)->ghostpads);
1588
1589     while (ghostpads) {
1590       GstPad *ghostpad = GST_PAD (ghostpads->data);
1591
1592       if (GST_IS_ELEMENT (GST_OBJECT_PARENT (ghostpad))){
1593         GST_DEBUG (GST_CAT_REFCOUNTING, "removing ghost pad from element '%s'", 
1594                         GST_OBJECT_NAME (GST_OBJECT_PARENT (ghostpad)));
1595
1596         gst_element_remove_ghost_pad (GST_ELEMENT (GST_OBJECT_PARENT (ghostpad)), GST_PAD (ghostpad));
1597       }
1598       ghostpads = g_list_next (ghostpads);
1599     }
1600     g_list_free (orig);
1601     g_list_free (GST_REAL_PAD(pad)->ghostpads);
1602   }
1603
1604   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))){
1605     GST_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'",
1606                     GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
1607     
1608     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
1609   }
1610   
1611   G_OBJECT_CLASS (real_pad_parent_class)->dispose (object);
1612 }
1613
1614
1615 #ifndef GST_DISABLE_LOADSAVE
1616 /**
1617  * gst_pad_load_and_connect:
1618  * @self: the XML node to read the description from
1619  * @parent: the element that has the pad
1620  *
1621  * Read the pad definition from the XML node and connect the given pad
1622  * in element to a pad of an element up in the hierarchy.
1623  */
1624 void
1625 gst_pad_load_and_connect (xmlNodePtr self,
1626                           GstObject *parent)
1627 {
1628   xmlNodePtr field = self->xmlChildrenNode;
1629   GstPad *pad = NULL, *targetpad;
1630   guchar *peer = NULL;
1631   gchar **split;
1632   GstElement *target;
1633   GstObject *grandparent;
1634
1635   while (field) {
1636     if (!strcmp (field->name, "name")) {
1637       pad = gst_element_get_pad (GST_ELEMENT (parent), xmlNodeGetContent (field));
1638     }
1639     else if (!strcmp(field->name, "peer")) {
1640       peer = xmlNodeGetContent (field);
1641     }
1642     field = field->next;
1643   }
1644   g_return_if_fail (pad != NULL);
1645
1646   if (peer == NULL) return;
1647
1648   split = g_strsplit (peer, ".", 2);
1649
1650   g_return_if_fail (split[0] != NULL);
1651   g_return_if_fail (split[1] != NULL);
1652
1653   grandparent = gst_object_get_parent (parent);
1654
1655   if (grandparent && GST_IS_BIN (grandparent)) {
1656     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
1657   }
1658   else
1659     goto cleanup;
1660
1661   if (target == NULL) goto cleanup;
1662
1663   targetpad = gst_element_get_pad (target, split[1]);
1664
1665   if (targetpad == NULL) goto cleanup;
1666
1667   gst_pad_connect (pad, targetpad);
1668
1669 cleanup:
1670   g_strfreev (split);
1671 }
1672
1673 /**
1674  * gst_pad_save_thyself:
1675  * @pad: the pad to save
1676  * @parent: the parent XML node to save the description in
1677  *
1678  * Saves the pad into an xml representation
1679  *
1680  * Returns: the xml representation of the pad
1681  */
1682 static xmlNodePtr
1683 gst_pad_save_thyself (GstObject *object,
1684                       xmlNodePtr parent)
1685 {
1686   GstRealPad *realpad;
1687   GstPad *peer;
1688
1689   g_return_val_if_fail (GST_IS_REAL_PAD (object), NULL);
1690
1691   realpad = GST_REAL_PAD(object);
1692
1693   xmlNewChild(parent,NULL,"name", GST_PAD_NAME (realpad));
1694   if (GST_RPAD_PEER(realpad) != NULL) {
1695     peer = GST_PAD(GST_RPAD_PEER(realpad));
1696     /* first check to see if the peer's parent's parent is the same */
1697     /* we just save it off */
1698     xmlNewChild(parent,NULL,"peer",g_strdup_printf("%s.%s",
1699                     GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer)));
1700   } else
1701     xmlNewChild(parent,NULL,"peer","");
1702
1703   return parent;
1704 }
1705
1706 /**
1707  * gst_pad_ghost_save_thyself:
1708  * @pad: the pad to save
1709  * @bin: the bin
1710  * @parent: the parent XML node to save the description in
1711  *
1712  * Saves the ghost pad into an xml representation.
1713  *
1714  * Returns: the xml representation of the pad
1715  */
1716 xmlNodePtr
1717 gst_pad_ghost_save_thyself (GstPad *pad,
1718                             GstElement *bin,
1719                             xmlNodePtr parent)
1720 {
1721   xmlNodePtr self;
1722
1723   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
1724
1725   self = xmlNewChild (parent, NULL, "ghostpad", NULL);
1726   xmlNewChild (self, NULL, "name", GST_PAD_NAME (pad));
1727   xmlNewChild (self, NULL, "parent", GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
1728
1729   /* FIXME FIXME FIXME! */
1730
1731   return self;
1732 }
1733 #endif /* GST_DISABLE_LOADSAVE */
1734
1735 #ifndef gst_pad_push
1736 /**
1737  * gst_pad_push:
1738  * @pad: the pad to push
1739  * @buf: the buffer to push
1740  *
1741  * Push a buffer to the peer of the pad.
1742  */
1743 void 
1744 gst_pad_push (GstPad *pad, GstBuffer *buf) 
1745 {
1746   GstRealPad *peer = GST_RPAD_PEER (pad);
1747
1748   GST_DEBUG_ENTER ("(%s:%s)", GST_DEBUG_PAD_NAME (pad));
1749
1750   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
1751
1752   if (!peer) {
1753     g_warning ("push on pad %s:%s but it is unconnected", GST_DEBUG_PAD_NAME (pad));
1754   }
1755   else {
1756     if (peer->chainhandler) {
1757       if (buf) {
1758         GST_DEBUG (GST_CAT_DATAFLOW, "calling chainhandler &%s of peer pad %s:%s",
1759             GST_DEBUG_FUNCPTR_NAME (peer->chainhandler), GST_DEBUG_PAD_NAME (GST_PAD (peer)));
1760         (peer->chainhandler) (GST_PAD_CAST (peer), buf);
1761         return;
1762       }
1763       else {
1764         g_warning ("trying to push a NULL buffer on pad %s:%s", GST_DEBUG_PAD_NAME (peer));
1765         return;
1766       }
1767     } 
1768     else {
1769       g_warning ("(internal error) push on pad %s:%s but it has no chainhandler", GST_DEBUG_PAD_NAME (peer));
1770     }
1771   }
1772   /* clean up the mess here */
1773   if (buf != NULL) {
1774     if (GST_IS_BUFFER (buf))
1775       gst_buffer_unref (buf);
1776     else
1777       gst_event_free (GST_EVENT (buf));
1778   }
1779 }
1780 #endif
1781
1782 #ifndef gst_pad_pull
1783 /**
1784  * gst_pad_pull:
1785  * @pad: the pad to pull
1786  *
1787  * Pull a buffer from the peer pad.
1788  *
1789  * Returns: a new buffer from the peer pad.
1790  */
1791 GstBuffer*
1792 gst_pad_pull (GstPad *pad) 
1793 {
1794   GstRealPad *peer = GST_RPAD_PEER(pad);
1795   
1796   GST_DEBUG_ENTER("(%s:%s)",GST_DEBUG_PAD_NAME(pad));
1797
1798   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1799
1800   if (!peer) {
1801     gst_element_error (GST_PAD_PARENT (pad), 
1802                     "pull on pad %s:%s but it was unconnected", 
1803                     GST_ELEMENT_NAME (GST_PAD_PARENT (pad)), GST_PAD_NAME (pad),
1804                     NULL);
1805   }
1806   else {
1807     if (peer->gethandler) {
1808       GstBuffer *buf;
1809
1810       GST_DEBUG (GST_CAT_DATAFLOW, "calling gethandler %s of peer pad %s:%s",
1811         GST_DEBUG_FUNCPTR_NAME (peer->gethandler), GST_DEBUG_PAD_NAME (peer));
1812
1813       buf = (peer->gethandler) (GST_PAD_CAST (peer));
1814       if (buf)
1815         return buf;
1816       /* no null buffers allowed */
1817       gst_element_error (GST_PAD_PARENT (pad), 
1818                     "NULL buffer during pull on %s:%s", GST_DEBUG_PAD_NAME (pad), NULL);
1819           
1820     } else {
1821       gst_element_error (GST_PAD_PARENT (pad), 
1822                     "(internal error) pull on pad %s:%s but the peer pad %s:%s has no gethandler", 
1823                     GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer),
1824                     NULL);
1825     }
1826   }
1827   return NULL;
1828 }
1829 #endif
1830
1831 #ifndef gst_pad_pullregion
1832 /**
1833  * gst_pad_pullregion:
1834  * @pad: the pad to pull the region from
1835  * @type: the regiontype
1836  * @offset: the offset/start of the buffer to pull
1837  * @len: the length of the buffer to pull
1838  *
1839  * Pull a buffer region from the peer pad. The region to pull can be 
1840  * specified with a offset/lenght pair or with a start/legnth time
1841  * indicator as specified by the type parameter.
1842  *
1843  * Returns: a new buffer from the peer pad with data in the specified
1844  * region.
1845  */
1846 GstBuffer*
1847 gst_pad_pullregion (GstPad *pad, GstRegionType type, guint64 offset, guint64 len) 
1848 {
1849   GstRealPad *peer;
1850   GstBuffer *result = NULL;
1851   
1852   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1853
1854   do {
1855     peer = GST_RPAD_PEER(pad);
1856     g_return_val_if_fail (peer != NULL, NULL);
1857
1858     if (result) 
1859       gst_buffer_unref (result);
1860
1861     GST_DEBUG_ENTER("(%s:%s,%d,%lld,%lld)",GST_DEBUG_PAD_NAME(pad),type,offset,len);
1862
1863     if (peer->pullregionfunc) {
1864       GST_DEBUG (GST_CAT_DATAFLOW, "calling pullregionfunc &%s of peer pad %s:%s",
1865           GST_DEBUG_FUNCPTR_NAME (peer->pullregionfunc), GST_DEBUG_PAD_NAME(GST_PAD_CAST (peer)));
1866       result = (peer->pullregionfunc) (GST_PAD_CAST (peer), type, offset, len);
1867     } else {
1868       GST_DEBUG (GST_CAT_DATAFLOW,"no pullregionfunc");
1869       result = NULL;
1870       break;
1871     }
1872   }
1873   /* FIXME */
1874   while (result && !(GST_BUFFER_OFFSET (result) == offset && 
1875            GST_BUFFER_SIZE (result) == len));
1876
1877   return result;
1878 }
1879 #endif
1880
1881 /**
1882  * gst_pad_peek:
1883  * @pad: the pad to peek
1884  *
1885  * Peek for a buffer from the peer pad.
1886  *
1887  * Returns: a from the peer pad or NULL if the peer has no buffer.
1888  */
1889 GstBuffer*
1890 gst_pad_peek (GstPad *pad)
1891 {
1892   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1893
1894   return GST_RPAD_BUFPEN (GST_RPAD_PEER (pad));
1895 }
1896
1897 /**
1898  * gst_pad_select:
1899  * @padlist: A list of pads 
1900  *
1901  * Wait for a buffer on the list of pads.
1902  *
1903  * Returns: The pad that has a buffer available, use 
1904  * #gst_pad_pull to get the buffer.
1905  */
1906 GstPad*
1907 gst_pad_select (GList *padlist)
1908 {
1909   GstPad *pad;
1910
1911   pad = gst_scheduler_pad_select (GST_PAD_PARENT (padlist->data)->sched, padlist);
1912
1913   return pad;
1914 }
1915
1916 /**
1917  * gst_pad_selectv:
1918  * @pad: The first pad to perform the select on 
1919  * @...: More pads
1920  *
1921  * Wait for a buffer on the given of pads.
1922  *
1923  * Returns: The pad that has a buffer available, use 
1924  * #gst_pad_pull to get the buffer.
1925  */
1926 GstPad*
1927 gst_pad_selectv (GstPad *pad, ...)
1928 {
1929   GstPad *result;
1930   GList *padlist = NULL;
1931   va_list var_args;
1932
1933   if (pad == NULL)
1934     return NULL;
1935
1936   va_start (var_args, pad);
1937
1938   while (pad) {
1939     padlist = g_list_prepend (padlist, pad);
1940     pad = va_arg (var_args, GstPad *);
1941   }
1942   result = gst_pad_select (padlist);
1943   g_list_free (padlist);
1944
1945   va_end (var_args);
1946   
1947   return result;
1948 }
1949
1950 /************************************************************************
1951  *
1952  * templates
1953  *
1954  */
1955 static void             gst_pad_template_class_init     (GstPadTemplateClass *klass);
1956 static void             gst_pad_template_init           (GstPadTemplate *templ);
1957
1958 enum {
1959   TEMPL_PAD_CREATED,
1960   /* FILL ME */
1961   TEMPL_LAST_SIGNAL
1962 };
1963
1964 static GstObject *padtemplate_parent_class = NULL;
1965 static guint gst_pad_template_signals[TEMPL_LAST_SIGNAL] = { 0 };
1966
1967 GType
1968 gst_pad_template_get_type (void)
1969 {
1970   static GType padtemplate_type = 0;
1971
1972   if (!padtemplate_type) {
1973     static const GTypeInfo padtemplate_info = {
1974       sizeof(GstPadTemplateClass),
1975       NULL,
1976       NULL,
1977       (GClassInitFunc)gst_pad_template_class_init,
1978       NULL,
1979       NULL,
1980       sizeof(GstPadTemplate),
1981       32,
1982       (GInstanceInitFunc)gst_pad_template_init,
1983       NULL
1984     };
1985     padtemplate_type = g_type_register_static(GST_TYPE_OBJECT, "GstPadTemplate", &padtemplate_info, 0);
1986   }
1987   return padtemplate_type;
1988 }
1989
1990 static void
1991 gst_pad_template_class_init (GstPadTemplateClass *klass)
1992 {
1993   GObjectClass *gobject_class;
1994   GstObjectClass *gstobject_class;
1995
1996   gobject_class = (GObjectClass*)klass;
1997   gstobject_class = (GstObjectClass*)klass;
1998
1999   padtemplate_parent_class = g_type_class_ref(GST_TYPE_OBJECT);
2000
2001   gst_pad_template_signals[TEMPL_PAD_CREATED] =
2002     g_signal_new ("pad_created", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
2003                     G_STRUCT_OFFSET (GstPadTemplateClass, pad_created), NULL, NULL,
2004                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
2005                     G_TYPE_POINTER);
2006
2007
2008   gstobject_class->path_string_separator = "*";
2009 }
2010
2011 static void
2012 gst_pad_template_init (GstPadTemplate *templ)
2013 {
2014 }
2015
2016 /* ALWAYS padtemplates cannot have conversion specifications, it doesn't make
2017  * sense.
2018  * SOMETIMES padtemplates can do whatever they want, they are provided by the
2019  * element.
2020  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
2021  * 'sink%d' template is automatically selected), so we need to restrict their
2022  * naming.
2023  */
2024 static gboolean
2025 name_is_valid (const gchar *name, GstPadPresence presence)
2026 {
2027   const gchar *str;
2028   
2029   if (presence == GST_PAD_ALWAYS) {
2030     if (strchr (name, '%')) {
2031       g_warning ("invalid name template %s: conversion specifications are not"
2032                  " allowed for GST_PAD_ALWAYS padtemplates", name);
2033       return FALSE;
2034     }
2035   } else if (presence == GST_PAD_REQUEST) {
2036     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
2037       g_warning ("invalid name template %s: only one conversion specification"
2038                  " allowed in GST_PAD_REQUEST padtemplate", name);
2039       return FALSE;
2040     }
2041     if (str && (*(str+1) != 's' && *(str+1) != 'd')) {
2042       g_warning ("invalid name template %s: conversion specification must be of"
2043                  " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
2044       return FALSE;
2045     }
2046     if (str && (*(str+2) != '\0')) {
2047       g_warning ("invalid name template %s: conversion specification must appear"
2048                  " at the end of the GST_PAD_REQUEST padtemplate name", name);
2049       return FALSE;
2050     }
2051   }
2052   
2053   return TRUE;
2054 }
2055
2056 /**
2057  * gst_pad_template_new:
2058  * @name_template: the name template
2059  * @direction: the direction for the template
2060  * @presence: the presence of the pad
2061  * @caps: a list of capabilities for the template
2062  * @...: more capabilities
2063  *
2064  * Creates a new padtemplate from the given arguments.
2065  *
2066  * Returns: the new padtemplate
2067  */
2068 GstPadTemplate*
2069 gst_pad_template_new (gchar *name_template,
2070                      GstPadDirection direction, GstPadPresence presence,
2071                      GstCaps *caps, ...)
2072 {
2073   GstPadTemplate *new;
2074   va_list var_args;
2075   GstCaps *thecaps = NULL;
2076
2077   g_return_val_if_fail (name_template != NULL, NULL);
2078
2079   if (!name_is_valid (name_template, presence))
2080     return NULL;
2081
2082   new = g_object_new(gst_pad_template_get_type () ,NULL);
2083
2084   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = name_template;
2085   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
2086   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
2087
2088   va_start (var_args, caps);
2089
2090   while (caps) {
2091     new->fixed &= caps->fixed;
2092     thecaps = gst_caps_append (thecaps, caps);
2093     caps = va_arg (var_args, GstCaps*);
2094   }
2095   va_end (var_args);
2096   
2097   GST_PAD_TEMPLATE_CAPS (new) = thecaps;
2098
2099   return new;
2100 }
2101
2102 /**
2103  * gst_pad_template_get_caps:
2104  * @templ: the padtemplate to use
2105  *
2106  * Get the capabilities of the padtemplate
2107  *
2108  * Returns: a GstCaps*
2109  */
2110 GstCaps*
2111 gst_pad_template_get_caps (GstPadTemplate *templ)
2112 {
2113   g_return_val_if_fail (templ != NULL, NULL);
2114
2115   return GST_PAD_TEMPLATE_CAPS (templ);
2116 }
2117
2118 #ifndef GST_DISABLE_LOADSAVE
2119 /**
2120  * gst_pad_template_save_thyself:
2121  * @templ: the padtemplate to save
2122  * @parent: the parent XML tree
2123  *
2124  * Saves the padtemplate into XML.
2125  *
2126  * Returns: the new XML tree
2127  */
2128 xmlNodePtr
2129 gst_pad_template_save_thyself (GstPadTemplate *templ, xmlNodePtr parent)
2130 {
2131   xmlNodePtr subtree;
2132   guchar *presence;
2133
2134   GST_DEBUG (GST_CAT_XML,"saving padtemplate %s", templ->name_template);
2135
2136   xmlNewChild(parent,NULL,"nametemplate", templ->name_template);
2137   xmlNewChild(parent,NULL,"direction", (templ->direction == GST_PAD_SINK? "sink":"src"));
2138
2139   switch (templ->presence) {
2140     case GST_PAD_ALWAYS:
2141       presence = "always";
2142       break;
2143     case GST_PAD_SOMETIMES:
2144       presence = "sometimes";
2145       break;
2146     case GST_PAD_REQUEST:
2147       presence = "request";
2148       break;
2149     default:
2150       presence = "unknown";
2151       break;
2152   }
2153   xmlNewChild(parent,NULL,"presence", presence);
2154
2155   if (GST_PAD_TEMPLATE_CAPS (templ)) {
2156     subtree = xmlNewChild (parent, NULL, "caps", NULL);
2157     gst_caps_save_thyself (GST_PAD_TEMPLATE_CAPS (templ), subtree);
2158   }
2159
2160   return parent;
2161 }
2162
2163 /**
2164  * gst_pad_template_load_thyself:
2165  * @parent: the source XML tree
2166  *
2167  * Loads a padtemplate from the XML tree.
2168  *
2169  * Returns: the new padtemplate
2170  */
2171 GstPadTemplate*
2172 gst_pad_template_load_thyself (xmlNodePtr parent)
2173 {
2174   xmlNodePtr field = parent->xmlChildrenNode;
2175   GstPadTemplate *factory;
2176   gchar *name_template = NULL;
2177   GstPadDirection direction = GST_PAD_UNKNOWN;
2178   GstPadPresence presence = GST_PAD_ALWAYS;
2179   GstCaps *caps = NULL;
2180
2181   while (field) {
2182     if (!strcmp(field->name, "nametemplate")) {
2183       name_template = xmlNodeGetContent(field);
2184     }
2185     if (!strcmp(field->name, "direction")) {
2186       gchar *value = xmlNodeGetContent(field);
2187
2188       if (!strcmp(value, "sink")) {
2189         direction = GST_PAD_SINK;
2190       }
2191       else if (!strcmp(value, "src")) {
2192         direction = GST_PAD_SRC;
2193       }
2194       g_free (value);
2195     }
2196     if (!strcmp(field->name, "presence")) {
2197       gchar *value = xmlNodeGetContent(field);
2198
2199       if (!strcmp(value, "always")) {
2200         presence = GST_PAD_ALWAYS;
2201       }
2202       else if (!strcmp(value, "sometimes")) {
2203         presence = GST_PAD_SOMETIMES;
2204       }
2205       else if (!strcmp(value, "request")) {
2206         presence = GST_PAD_REQUEST;
2207       }
2208       g_free (value);
2209     }
2210     else if (!strcmp(field->name, "caps")) {
2211       caps = gst_caps_load_thyself (field);
2212     }
2213     field = field->next;
2214   }
2215
2216   factory = gst_pad_template_new (name_template, direction, presence, caps, NULL);
2217
2218   return factory;
2219 }
2220 #endif /* !GST_DISABLE_LOADSAVE */
2221
2222
2223 /**
2224  * gst_pad_set_element_private:
2225  * @pad: the pad to set the private data to
2226  * @priv: The private data to attach to the pad
2227  *
2228  * Set the given private data pointer to the pad. This
2229  * function can only be used by the element that own the
2230  * pad.
2231  */
2232 void
2233 gst_pad_set_element_private (GstPad *pad, gpointer priv)
2234 {
2235   pad->element_private = priv;
2236 }
2237
2238 /**
2239  * gst_pad_get_element_private:
2240  * @pad: the pad to get the private data of
2241  *
2242  * Get the private data of a pad. The private data can
2243  * only be set by the parent element of this pad.
2244  *
2245  * Returns: a pointer to the private data.
2246  */
2247 gpointer
2248 gst_pad_get_element_private (GstPad *pad)
2249 {
2250   return pad->element_private;
2251 }
2252
2253
2254 /***** ghost pads *****/
2255 GType _gst_ghost_pad_type = 0;
2256
2257 static void     gst_ghost_pad_class_init         (GstGhostPadClass *klass);
2258 static void     gst_ghost_pad_init               (GstGhostPad *pad);
2259
2260 static GstPad *ghost_pad_parent_class = NULL;
2261 /* static guint gst_ghost_pad_signals[LAST_SIGNAL] = { 0 }; */
2262
2263 GType
2264 gst_ghost_pad_get_type (void) 
2265 {
2266   if (!_gst_ghost_pad_type) {
2267     static const GTypeInfo pad_info = {
2268       sizeof (GstGhostPadClass),
2269       NULL,
2270       NULL,
2271       (GClassInitFunc) gst_ghost_pad_class_init,
2272       NULL,
2273       NULL,
2274       sizeof(GstGhostPad),
2275       8,
2276       (GInstanceInitFunc) gst_ghost_pad_init,
2277       NULL
2278     };
2279     _gst_ghost_pad_type = g_type_register_static (GST_TYPE_PAD, "GstGhostPad", &pad_info, 0);
2280   }
2281   return _gst_ghost_pad_type;
2282 }
2283
2284 static void
2285 gst_ghost_pad_class_init (GstGhostPadClass *klass)
2286 {
2287   GObjectClass *gobject_class;
2288
2289   gobject_class = (GObjectClass*) klass;
2290
2291   ghost_pad_parent_class = g_type_class_ref (GST_TYPE_PAD);
2292 }
2293
2294 static void
2295 gst_ghost_pad_init (GstGhostPad *pad)
2296 {
2297   pad->realpad = NULL;
2298 }
2299
2300 /**
2301  * gst_ghost_pad_new:
2302  * @name: name of the new ghost pad
2303  * @pad: the pad to create a ghost pad of
2304  *
2305  * Create a new ghost pad associated with the given pad.
2306  *
2307  * Returns: new ghost pad
2308  */
2309 GstPad*
2310 gst_ghost_pad_new (gchar *name,
2311                    GstPad *pad)
2312 {
2313   GstGhostPad *ghostpad;
2314   GstRealPad *realpad;
2315
2316   g_return_val_if_fail (name != NULL, NULL);
2317   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2318
2319   ghostpad = g_object_new (gst_ghost_pad_get_type () ,NULL);
2320   gst_pad_set_name (GST_PAD (ghostpad), name);
2321
2322   realpad = (GstRealPad *) pad;
2323
2324   while (!GST_IS_REAL_PAD (realpad)) {
2325     realpad = GST_PAD_REALIZE (realpad);
2326   }
2327   GST_GPAD_REALPAD (ghostpad) = realpad;
2328   GST_PAD_PAD_TEMPLATE (ghostpad) = GST_PAD_PAD_TEMPLATE (pad);
2329
2330   /* add ourselves to the real pad's list of ghostpads */
2331   gst_pad_add_ghost_pad (pad, GST_PAD (ghostpad));
2332
2333   /* FIXME need to ref the real pad here... ? */
2334
2335   GST_DEBUG (GST_CAT_PADS, "created ghost pad \"%s\"", name);
2336
2337   return GST_PAD (ghostpad);
2338 }
2339
2340 static void 
2341 gst_pad_event_default_dispatch (GstPad *pad, GstElement *element, GstEvent *event)
2342 {
2343   GList *pads = element->pads;
2344
2345   while (pads) {
2346     GstPad *eventpad = GST_PAD (pads->data);
2347     pads = g_list_next (pads);
2348
2349     /* for all pads in the opposite direction that are connected */
2350     if (GST_PAD_DIRECTION (eventpad) != GST_PAD_DIRECTION (pad) && GST_PAD_IS_CONNECTED (eventpad)) {
2351       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2352         gst_pad_push (eventpad, GST_BUFFER (gst_event_copy (event)));
2353       }
2354       else {
2355         GstPad *peerpad = GST_PAD_CAST (GST_RPAD_PEER (eventpad));
2356
2357         gst_pad_send_event (peerpad, gst_event_copy (event));
2358       }
2359     }
2360   }
2361 }
2362
2363 /**
2364  * gst_pad_event_default:
2365  * @pad: the pad to operate on
2366  * @event: the event to handle
2367  *
2368  * Invoke the default event handler for the given pad.
2369  */
2370 void 
2371 gst_pad_event_default (GstPad *pad, GstEvent *event)
2372 {
2373   GstElement *element = GST_PAD_PARENT (pad);
2374
2375   g_signal_emit (G_OBJECT (pad), gst_real_pad_signals[REAL_EVENT_RECEIVED], 0, event);
2376  
2377   switch (GST_EVENT_TYPE (event)) {
2378     case GST_EVENT_EOS:
2379       gst_element_set_eos (element);
2380       gst_pad_event_default_dispatch (pad, element, event);
2381       /* we have to try to schedule another element because this one is disabled */
2382       gst_element_yield (element);
2383       break;
2384     case GST_EVENT_FLUSH:
2385     default:
2386       gst_pad_event_default_dispatch (pad, element, event);
2387       break;
2388   }
2389 }
2390
2391 /**
2392  * gst_pad_send_event:
2393  * @pad: the pad to send the event to
2394  * @event: the event to send to the pad.
2395  *
2396  * Send the event to the pad.
2397  *
2398  * Returns: TRUE if the event was handled.
2399  */
2400 gboolean
2401 gst_pad_send_event (GstPad *pad, GstEvent *event)
2402 {
2403   gboolean handled = FALSE;
2404
2405   g_return_val_if_fail (event, FALSE);
2406
2407   if (GST_EVENT_SRC (event) == NULL)
2408     GST_EVENT_SRC (event) = gst_object_ref (GST_OBJECT (pad));
2409
2410   GST_DEBUG (GST_CAT_EVENT, "have event %d on pad %s:%s",
2411                   GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
2412
2413   if (GST_RPAD_EVENTFUNC (pad))
2414     handled = GST_RPAD_EVENTFUNC (pad) (pad, event);
2415   else {
2416     GST_DEBUG(GST_CAT_EVENT, "there's no event function for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2417   }
2418
2419   if (!handled) {
2420     GST_DEBUG(GST_CAT_EVENT, "proceeding with default event behavior here");
2421     gst_pad_event_default (pad, event);
2422     handled = TRUE;
2423   }
2424
2425   return handled;
2426 }
2427