more debug output used CONNECT_OK because otherwise functions don't get a proper...
[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\n", 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\n", 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_PADTEMPLATE (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\n",
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\n",
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\n",
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\n",
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\n",
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\n",
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\n",
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\n",
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\n");
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) != NULL);
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)\n");
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\n");
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
744   return TRUE;
745 }
746
747 /**
748  * gst_pad_connect:
749  * @srcpad: the source pad to connect
750  * @sinkpad: the sink pad to connect
751  *
752  * Connects the source pad to the sink pad.
753  *
754  * Returns: TRUE if the pad could be connected, FALSE otherwise
755  */
756 gboolean
757 gst_pad_connect (GstPad *srcpad, GstPad *sinkpad)
758 {
759   return gst_pad_connect_filtered (srcpad, sinkpad, NULL);
760 }
761
762 /**
763  * gst_pad_set_parent:
764  * @pad: the pad to set the parent
765  * @parent: the object to set the parent to
766  *
767  * Sets the parent object of a pad.
768  */
769 void
770 gst_pad_set_parent (GstPad *pad,
771                     GstObject *parent)
772 {
773   g_return_if_fail (pad != NULL);
774   g_return_if_fail (GST_IS_PAD (pad));
775   g_return_if_fail (GST_PAD_PARENT (pad) == NULL);
776   g_return_if_fail (parent != NULL);
777   g_return_if_fail (GST_IS_OBJECT (parent));
778   g_return_if_fail ((gpointer)pad != (gpointer)parent);
779
780   gst_object_set_parent (GST_OBJECT (pad), parent);
781 }
782
783 /**
784  * gst_pad_get_parent:
785  * @pad: the pad to get the parent from
786  *
787  * Get the parent object of this pad.
788  *
789  * Returns: the parent object
790  */
791 GstElement*
792 gst_pad_get_parent (GstPad *pad)
793 {
794   g_return_val_if_fail (pad != NULL, NULL);
795   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
796
797   return GST_PAD_PARENT (pad);
798 }
799
800 /**
801  * gst_pad_get_padtemplate:
802  * @pad: the pad to get the padtemplate from
803  *
804  * Get the padtemplate object of this pad.
805  *
806  * Returns: the padtemplate object
807  */
808 GstPadTemplate*
809 gst_pad_get_padtemplate (GstPad *pad)
810 {
811   g_return_val_if_fail (pad != NULL, NULL);
812   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
813
814   return GST_PAD_PADTEMPLATE (pad); 
815 }
816
817
818 /**
819  * gst_pad_set_sched:
820  * @pad: the pad to set the scheduler for
821  * @sched: The scheduler to set
822  *
823  * Set the scheduler for the pad
824  */
825 void
826 gst_pad_set_sched (GstPad *pad, GstScheduler *sched)
827 {
828   g_return_if_fail (pad != NULL);
829   g_return_if_fail (GST_IS_PAD (pad));
830  
831   GST_RPAD_SCHED(pad) = sched;
832 }
833  
834 /**
835  * gst_pad_get_sched:
836  * @pad: the pad to get the scheduler from
837  *
838  * Get the scheduler of the pad
839  *
840  * Returns: the scheduler of the pad.
841  */
842 GstScheduler*
843 gst_pad_get_sched (GstPad *pad)
844 {
845   g_return_val_if_fail (pad != NULL, NULL);
846   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
847  
848   return GST_RPAD_SCHED(pad);
849 }
850
851 /**
852  * gst_pad_unset_sched:
853  * @pad: the pad to unset the scheduler for
854  *
855  * Unset the scheduler for the pad
856  */
857 void
858 gst_pad_unset_sched (GstPad *pad)
859 {
860   g_return_if_fail (pad != NULL);
861   g_return_if_fail (GST_IS_PAD (pad));
862  
863   GST_RPAD_SCHED(pad) = NULL;
864 }
865  
866 /**
867  * gst_pad_get_real_parent:
868  * @pad: the pad to get the parent from
869  *
870  * Get the real parent object of this pad. If the pad
871  * is a ghostpad, the actual owner of the real pad is
872  * returned, as opposed to the gst_pad_get_parent().
873  *
874  * Returns: the parent object
875  */
876 GstElement*
877 gst_pad_get_real_parent (GstPad *pad)
878 {
879   g_return_val_if_fail (pad != NULL, NULL);
880   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
881
882   return GST_PAD_PARENT (GST_PAD (GST_PAD_REALIZE (pad)));
883 }
884
885 /**
886  * gst_pad_add_ghost_pad:
887  * @pad: the pad to set the ghost parent
888  * @ghostpad: the ghost pad to add
889  *
890  * Add a ghost pad to a pad.
891  */
892 void
893 gst_pad_add_ghost_pad (GstPad *pad,
894                        GstPad *ghostpad)
895 {
896   GstRealPad *realpad;
897
898   g_return_if_fail (pad != NULL);
899   g_return_if_fail (GST_IS_PAD (pad));
900   g_return_if_fail (ghostpad != NULL);
901   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
902
903   realpad = GST_PAD_REALIZE (pad);
904
905   realpad->ghostpads = g_list_prepend (realpad->ghostpads, ghostpad);
906 }
907
908
909 /**
910  * gst_pad_remove_ghost_pad:
911  * @pad: the pad to remove the ghost parent
912  * @ghostpad: the ghost pad to remove from the pad
913  *
914  * Remove a ghost pad from a pad.
915  */
916 void
917 gst_pad_remove_ghost_pad (GstPad *pad,
918                           GstPad *ghostpad)
919 {
920   GstRealPad *realpad;
921
922   g_return_if_fail (pad != NULL);
923   g_return_if_fail (GST_IS_PAD (pad));
924   g_return_if_fail (ghostpad != NULL);
925   g_return_if_fail (GST_IS_GHOST_PAD (ghostpad));
926
927   realpad = GST_PAD_REALIZE (pad);
928
929   realpad->ghostpads = g_list_remove (realpad->ghostpads, ghostpad);
930 }
931
932 /**
933  * gst_pad_get_ghost_pad_list:
934  * @pad: the pad to get the ghost parents from
935  *
936  * Get the ghost parents of this pad.
937  *
938  * Returns: a GList of ghost pads
939  */
940 GList*
941 gst_pad_get_ghost_pad_list (GstPad *pad)
942 {
943   g_return_val_if_fail (pad != NULL, NULL);
944   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
945
946   return GST_PAD_REALIZE(pad)->ghostpads;
947 }
948
949 /* an internal caps negotiation helper function does:
950  * 
951  * 1. optinally calls the pad connect function with the provided caps
952  * 2. deal with the result code of the connect function
953  * 3. set fixed caps on the pad.
954  */
955 static GstPadConnectReturn
956 gst_pad_try_set_caps_func (GstRealPad *pad, GstCaps *caps, gboolean notify)
957 {
958   GstCaps *oldcaps;
959   GstElement *parent = GST_PAD_PARENT (pad);
960
961   /* thomas: FIXME: is this the right result to return ? */
962   g_return_val_if_fail (pad != NULL, GST_PAD_CONNECT_REFUSED);
963   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_CONNECT_REFUSED);
964   
965   /* if this pad has a parent and the parent is not READY, delay the
966    * negotiation */
967   if (parent && GST_STATE (parent) < GST_STATE_READY)
968   {
969     GST_DEBUG (GST_CAT_CAPS, "parent %s of pad %s:%s is not ready\n",
970                GST_ELEMENT_NAME (parent), GST_DEBUG_PAD_NAME (pad));
971     return GST_PAD_CONNECT_DELAYED;
972   }
973           
974   GST_INFO (GST_CAT_CAPS, "trying to set caps %p on pad %s:%s",
975             caps, GST_DEBUG_PAD_NAME (pad));
976
977   /* we need to notify the connect function */
978   if (notify && GST_RPAD_CONNECTFUNC (pad)) {
979     GstPadConnectReturn res;
980     gchar *debug_string;
981
982     GST_INFO (GST_CAT_CAPS, "calling connect function on pad %s:%s",
983             GST_DEBUG_PAD_NAME (pad));
984
985     /* call the connect function */
986     res = GST_RPAD_CONNECTFUNC (pad) (GST_PAD (pad), caps);
987
988     switch (res) {
989       case GST_PAD_CONNECT_REFUSED:
990         debug_string = "REFUSED";
991         break;
992       case GST_PAD_CONNECT_OK:
993         debug_string = "OK";
994         break;
995       case GST_PAD_CONNECT_DONE:
996         debug_string = "DONE";
997         break;
998       case GST_PAD_CONNECT_DELAYED:
999         debug_string = "DELAYED";
1000         break;
1001       default:
1002         g_warning ("unknown return code from connect function of pad %s:%s",
1003             GST_DEBUG_PAD_NAME (pad));
1004         return GST_PAD_CONNECT_REFUSED;
1005     }
1006
1007     GST_INFO (GST_CAT_CAPS, "got reply %s (%d) from connect function on pad %s:%s",
1008             debug_string, res, GST_DEBUG_PAD_NAME (pad));
1009
1010     /* done means the connect function called another caps negotiate function
1011      * on this pad that succeeded, we dont need to continue */
1012     if (res == GST_PAD_CONNECT_DONE) {
1013       GST_INFO (GST_CAT_CAPS, "pad %s:%s is done", GST_DEBUG_PAD_NAME (pad));
1014       return GST_PAD_CONNECT_DONE;
1015     }
1016     if (res == GST_PAD_CONNECT_REFUSED) {
1017       GST_INFO (GST_CAT_CAPS, "pad %s:%s doesn't accept caps",
1018                     GST_DEBUG_PAD_NAME (pad));
1019       return GST_PAD_CONNECT_REFUSED;
1020     }
1021   }
1022   /* we can only set caps on the pad if they are fixed */
1023   if (GST_CAPS_IS_FIXED (caps)) {
1024
1025     GST_INFO (GST_CAT_CAPS, "setting caps on pad %s:%s",
1026               GST_DEBUG_PAD_NAME (pad));
1027     /* if we got this far all is ok, remove the old caps, set the new one */
1028     oldcaps = GST_PAD_CAPS (pad);
1029     if (caps) gst_caps_ref (caps);
1030     GST_PAD_CAPS (pad) = caps;
1031     if (oldcaps) gst_caps_unref (oldcaps);
1032   }
1033   else {
1034     GST_INFO (GST_CAT_CAPS, "caps are not fixed on pad %s:%s, not setting them yet",
1035               GST_DEBUG_PAD_NAME (pad));
1036   }
1037
1038   return GST_PAD_CONNECT_OK;
1039 }
1040
1041 /**
1042  * gst_pad_try_set_caps:
1043  * @pad: the pad to try to set the caps on
1044  * @caps: the caps to set
1045  *
1046  * Try to set the caps on the given pad.
1047  *
1048  * Returns: TRUE if the caps could be set
1049  */
1050 gboolean
1051 gst_pad_try_set_caps (GstPad *pad, GstCaps *caps)
1052 {
1053   GstRealPad *peer, *realpad;
1054
1055   realpad = GST_PAD_REALIZE (pad);
1056   peer = GST_RPAD_PEER (realpad);
1057
1058   GST_INFO (GST_CAT_CAPS, "trying to set caps %p on pad %s:%s",
1059             caps, GST_DEBUG_PAD_NAME (realpad));
1060
1061   /* setting non fixed caps on a pad is not allowed */
1062   if (!GST_CAPS_IS_FIXED (caps)) {
1063   GST_INFO (GST_CAT_CAPS, "trying to set unfixed caps on pad %s:%s, not allowed",
1064                   GST_DEBUG_PAD_NAME (realpad));
1065     g_warning ("trying to set non fixed caps on pad %s:%s, not allowed",
1066             GST_DEBUG_PAD_NAME (realpad));
1067     gst_caps_debug (caps);
1068     return FALSE;
1069   }
1070   /* if we have a peer try to set the caps, notifying the peerpad
1071    * if it has a connect function */
1072   if (peer && (gst_pad_try_set_caps_func (peer, caps, TRUE) != GST_PAD_CONNECT_OK))
1073   {
1074     GST_INFO (GST_CAT_CAPS, "tried to set caps on peerpad %s:%s but couldn't",
1075               GST_DEBUG_PAD_NAME (peer));
1076     return FALSE;
1077   }
1078   /* then try to set our own caps, we don't need to be notified */
1079   if (gst_pad_try_set_caps_func (realpad, caps, FALSE) != GST_PAD_CONNECT_OK)
1080   {
1081     GST_INFO (GST_CAT_CAPS, "tried to set own caps on pad %s:%s but couldn't",
1082               GST_DEBUG_PAD_NAME (realpad));
1083     return FALSE;
1084   }
1085   GST_INFO (GST_CAT_CAPS, "succeeded setting caps %p on pad %s:%s",
1086             caps, GST_DEBUG_PAD_NAME (realpad));
1087   gst_caps_debug (caps);
1088   g_assert (GST_PAD_CAPS (pad));
1089                           
1090   return TRUE;
1091 }
1092
1093 /* this is a caps negotiation convenience routine, it performs:
1094  *
1095  * 1. optionally clear any pad caps
1096  * 2. calculate the intersection between the two pad tamplate/getcaps caps
1097  * 3. calculate the intersection with the (optional) filtercaps.
1098  * 4. store the intersection in the pad filter
1099  * 5. store the app filtercaps in the pad appfilter.
1100  * 6. start the caps negotiation.
1101  */
1102 static gboolean
1103 gst_pad_try_reconnect_filtered_func (GstRealPad *srcpad, GstRealPad *sinkpad, GstCaps *filtercaps, gboolean clear)
1104 {
1105   GstCaps *srccaps, *sinkcaps;
1106   GstCaps *intersection = NULL;
1107   GstRealPad *realsrc, *realsink;
1108
1109   realsrc = GST_PAD_REALIZE (srcpad);
1110   realsink = GST_PAD_REALIZE (sinkpad);
1111
1112   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1113   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1114
1115   /* optinally clear the caps */
1116   if (clear) {
1117     GST_INFO (GST_CAT_PADS, "reconnect filtered %s:%s and %s:%s, clearing caps",
1118         GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1119
1120     GST_PAD_CAPS (GST_PAD (realsrc)) = NULL;
1121     GST_PAD_CAPS (GST_PAD (realsink)) = NULL;
1122   }
1123   else {
1124     GST_INFO (GST_CAT_PADS, "reconnect filtered %s:%s and %s:%s",
1125         GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1126   }
1127
1128   srccaps = gst_pad_get_caps (GST_PAD (realsrc));
1129   GST_INFO (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsrc));
1130   gst_caps_debug (srccaps);
1131   sinkcaps = gst_pad_get_caps (GST_PAD (realsink));
1132   GST_INFO (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsink));
1133   gst_caps_debug (sinkcaps);
1134
1135   /* first take the intersection of the pad caps */
1136   intersection = gst_caps_intersect (srccaps, sinkcaps);
1137
1138   /* if we have no intersection but one of the caps was not NULL.. */
1139   if (!intersection && (srccaps || sinkcaps )) {
1140     /* the intersection is NULL but the pad caps were not both NULL,
1141      * this means they have no common format */
1142     GST_INFO (GST_CAT_PADS, "pads %s:%s and %s:%s have no common type",
1143          GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1144     return FALSE;
1145   }
1146   else {
1147     GST_INFO (GST_CAT_PADS, "pads %s:%s and %s:%s intersected to %s caps",
1148          GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink), 
1149          ((intersection && GST_CAPS_IS_FIXED (intersection)) ? "fixed" : "variable"));
1150
1151     /* then filter this against the app filter */
1152     if (filtercaps) {
1153       GstCaps *filtered_intersection = gst_caps_intersect (intersection, filtercaps);
1154
1155       /* get rid of the old intersection here */
1156       gst_caps_unref (intersection);
1157
1158       if (!filtered_intersection) {
1159         GST_INFO (GST_CAT_PADS, "filtered connection between pads %s:%s and %s:%s is empty",
1160              GST_DEBUG_PAD_NAME (realsrc), GST_DEBUG_PAD_NAME (realsink));
1161         return FALSE;
1162       }
1163       intersection = filtered_intersection;
1164
1165       /* keep a reference to the app caps */
1166       GST_RPAD_APPFILTER (realsink) = filtercaps;
1167       GST_RPAD_APPFILTER (realsrc) = filtercaps;
1168     }
1169   }
1170   GST_DEBUG (GST_CAT_CAPS, "setting filter for connection to:\n");
1171   gst_caps_debug (intersection);
1172
1173   GST_RPAD_FILTER (realsrc) = intersection; 
1174   GST_RPAD_FILTER (realsink) = intersection; 
1175
1176   return gst_pad_perform_negotiate (GST_PAD (realsrc), GST_PAD (realsink));
1177 }
1178
1179 /**
1180  * gst_pad_perform_negotiate:
1181  * @srcpad: a srcpad
1182  * @sinkpad: a sinkpad 
1183  *
1184  * Try to negotiate the pads.
1185  *
1186  * Returns: a boolean indicating the pad succesfully negotiated.
1187  */
1188 gboolean
1189 gst_pad_perform_negotiate (GstPad *srcpad, GstPad *sinkpad) 
1190 {
1191   GstCaps *intersection;
1192   GstRealPad *realsrc, *realsink;
1193   GstCaps *srccaps, *sinkcaps;
1194
1195   g_return_val_if_fail (srcpad != NULL, FALSE);
1196   g_return_val_if_fail (sinkpad != NULL, FALSE);
1197   
1198   realsrc = GST_PAD_REALIZE (srcpad);
1199   realsink = GST_PAD_REALIZE (sinkpad);
1200     
1201   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1202   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1203
1204   /* calculate the new caps here */
1205   srccaps = gst_pad_get_caps (GST_PAD (realsrc));
1206   GST_INFO (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsrc));
1207   gst_caps_debug (srccaps);
1208   sinkcaps = gst_pad_get_caps (GST_PAD (realsink));
1209   GST_INFO (GST_CAT_PADS, "dumping caps of pad %s:%s", GST_DEBUG_PAD_NAME (realsink));
1210   gst_caps_debug (sinkcaps);
1211   intersection = gst_caps_intersect (srccaps, sinkcaps);
1212
1213   /* no negotiation is performed it the pads have filtercaps */
1214   if (intersection) {
1215     GstPadConnectReturn res;
1216
1217     res = gst_pad_try_set_caps_func (realsrc, intersection, TRUE);
1218     if (res == GST_PAD_CONNECT_REFUSED) 
1219       return FALSE;
1220     if (res == GST_PAD_CONNECT_DONE) 
1221       return TRUE;
1222
1223     res = gst_pad_try_set_caps_func (realsink, intersection, TRUE);
1224     if (res == GST_PAD_CONNECT_REFUSED) 
1225       return FALSE;
1226     if (res == GST_PAD_CONNECT_DONE) 
1227       return TRUE;
1228   }
1229   return TRUE;
1230 }
1231
1232 /**
1233  * gst_pad_try_reconnect_filtered:
1234  * @pad: the pad to reconnect
1235  * @caps: the capabilities to use in the reconnectiong
1236  *
1237  * Try to reconnect this pad and its peer with the specified caps
1238  *
1239  * Returns: a boolean indicating the peer pad could accept the caps.
1240  */
1241 gboolean
1242 gst_pad_try_reconnect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
1243 {
1244   GstRealPad *realsrc, *realsink;
1245
1246   g_return_val_if_fail (srcpad != NULL, FALSE);
1247   g_return_val_if_fail (sinkpad != NULL, FALSE);
1248
1249   realsrc = GST_PAD_REALIZE (srcpad);
1250   realsink = GST_PAD_REALIZE (sinkpad);
1251
1252   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1253   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1254   
1255   return gst_pad_try_reconnect_filtered_func (realsrc, realsink, filtercaps, TRUE);
1256 }
1257
1258 /**
1259  * gst_pad_reconnect_filtered:
1260  * @pad: the pad to reconnect
1261  * @caps: the capabilities to use in the reconnectiong
1262  *
1263  * Try to reconnect this pad and its peer with the specified caps. 
1264  *
1265  * Returns: a boolean indicating the peer pad could accept the caps.
1266  *    if FALSE is returned, the pads are disconnected.
1267  */
1268 gboolean
1269 gst_pad_reconnect_filtered (GstPad *srcpad, GstPad *sinkpad, GstCaps *filtercaps)
1270 {
1271   GstRealPad *realsrc, *realsink;
1272
1273   g_return_val_if_fail (srcpad != NULL, FALSE);
1274   g_return_val_if_fail (sinkpad != NULL, FALSE);
1275
1276   realsrc = GST_PAD_REALIZE (srcpad);
1277   realsink = GST_PAD_REALIZE (sinkpad);
1278
1279   g_return_val_if_fail (GST_RPAD_PEER (realsrc) != NULL, FALSE);
1280   g_return_val_if_fail (GST_RPAD_PEER (realsink) == realsrc, FALSE);
1281   
1282   if (!gst_pad_try_reconnect_filtered_func (realsrc, realsink, filtercaps, TRUE)) {
1283     gst_pad_disconnect (srcpad, GST_PAD (GST_PAD_PEER (srcpad)));
1284     return FALSE;
1285   }
1286   return TRUE;
1287 }
1288
1289 /**
1290  * gst_pad_proxy_connect:
1291  * @pad: the pad to proxy to
1292  * @caps: the capabilities to use in the proxying
1293  *
1294  * Proxy the connect function to the specified pad.
1295  *
1296  * Returns: a boolean indicating the peer pad could accept the caps.
1297  */
1298 GstPadConnectReturn
1299 gst_pad_proxy_connect (GstPad *pad, GstCaps *caps)
1300 {
1301   GstRealPad *peer, *realpad;
1302
1303   realpad = GST_PAD_REALIZE (pad);
1304
1305   peer = GST_RPAD_PEER (realpad);
1306
1307   GST_INFO (GST_CAT_CAPS, "proxy connect to pad %s:%s",
1308             GST_DEBUG_PAD_NAME (realpad));
1309
1310   if (peer && !gst_pad_try_set_caps_func (peer, caps, TRUE))
1311     return GST_PAD_CONNECT_REFUSED;
1312   if (!gst_pad_try_set_caps_func (realpad, caps, FALSE))
1313     return GST_PAD_CONNECT_REFUSED;
1314
1315   return GST_PAD_CONNECT_OK;
1316 }
1317
1318 /**
1319  * gst_pad_get_caps:
1320  * @pad: the pad to get the capabilities from
1321  *
1322  * Get the capabilities of this pad.
1323  *
1324  * Returns: the capabilities of this pad
1325  */
1326 GstCaps*
1327 gst_pad_get_caps (GstPad *pad)
1328 {
1329   GstRealPad *realpad;
1330
1331   g_return_val_if_fail (pad != NULL, NULL);
1332   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1333
1334   realpad = GST_PAD_REALIZE (pad);
1335
1336   GST_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)\n",
1337             GST_DEBUG_PAD_NAME (realpad), realpad);
1338
1339   if (GST_PAD_CAPS (realpad)) {
1340     GST_DEBUG (GST_CAT_CAPS, "using pad real caps\n");
1341     return GST_PAD_CAPS (realpad);
1342   }
1343   else if GST_RPAD_GETCAPSFUNC (realpad) {
1344     GST_DEBUG (GST_CAT_CAPS, "using pad get function\n");
1345     return GST_RPAD_GETCAPSFUNC (realpad) (GST_PAD_CAST (realpad), NULL);
1346   }
1347   else if (GST_PAD_PADTEMPLATE (realpad)) {
1348     GST_DEBUG (GST_CAT_CAPS, "using pad template\n");
1349     return GST_PADTEMPLATE_CAPS (GST_PAD_PADTEMPLATE (realpad));
1350   }
1351   GST_DEBUG (GST_CAT_CAPS, "pad has no caps\n");
1352
1353   return NULL;
1354 }
1355
1356 /**
1357  * gst_pad_get_padtemplate_caps:
1358  * @pad: the pad to get the capabilities from
1359  *
1360  * Get the capabilities of this pad.
1361  *
1362  * Returns: a list of the capabilities of this pad
1363  */
1364 GstCaps*
1365 gst_pad_get_padtemplate_caps (GstPad *pad)
1366 {
1367   g_return_val_if_fail (pad != NULL, NULL);
1368   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1369
1370   if (GST_PAD_PADTEMPLATE (pad))
1371     return GST_PADTEMPLATE_CAPS (GST_PAD_PADTEMPLATE (pad));
1372
1373   return NULL;
1374 }
1375
1376
1377 /**
1378  * gst_padtemplate_get_caps_by_name:
1379  * @templ: the padtemplate to get the capabilities from
1380  * @name: the name of the capability to get
1381  *
1382  * Get the capability with the given name from this padtemplate.
1383  *
1384  * Returns: a capability or NULL if not found
1385  */
1386 GstCaps*
1387 gst_padtemplate_get_caps_by_name (GstPadTemplate *templ, const gchar *name)
1388 {
1389   GstCaps *caps;
1390
1391   g_return_val_if_fail (templ != NULL, NULL);
1392
1393   caps = GST_PADTEMPLATE_CAPS (templ);
1394   if (!caps) 
1395     return NULL;
1396
1397   return gst_caps_get_by_name (caps, name);
1398 }
1399
1400 /**
1401  * gst_pad_check_compatibility:
1402  * @srcpad: the srcpad to check
1403  * @sinkpad: the sinkpad to check against
1404  *
1405  * Check if two pads have compatible capabilities.
1406  *
1407  * Returns: TRUE if they are compatible or the capabilities
1408  * could not be checked
1409  */
1410 gboolean
1411 gst_pad_check_compatibility (GstPad *srcpad, GstPad *sinkpad)
1412 {
1413   g_return_val_if_fail (srcpad != NULL, FALSE);
1414   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1415   g_return_val_if_fail (sinkpad != NULL, FALSE);
1416   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1417
1418   if (GST_PAD_CAPS (srcpad) && GST_PAD_CAPS (sinkpad)) {
1419     if (!gst_caps_check_compatibility (GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad))) {
1420       return FALSE;
1421     }
1422     else {
1423       return TRUE;
1424     }
1425   }
1426   else {
1427     GST_DEBUG (GST_CAT_PADS, "could not check capabilities of pads (%s:%s) and (%s:%s) %p %p\n",
1428                     GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad), 
1429                     GST_PAD_CAPS (srcpad), GST_PAD_CAPS (sinkpad));
1430     return TRUE;
1431   }
1432 }
1433
1434 /**
1435  * gst_pad_get_peer:
1436  * @pad: the pad to get the peer from
1437  *
1438  * Get the peer pad of this pad.
1439  *
1440  * Returns: the peer pad
1441  */
1442 GstPad*
1443 gst_pad_get_peer (GstPad *pad)
1444 {
1445   g_return_val_if_fail (pad != NULL, NULL);
1446   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1447
1448   return GST_PAD (GST_PAD_PEER (pad));
1449 }
1450
1451 /**
1452  * gst_pad_get_allowed_caps:
1453  * @pad: the pad to get the allowed caps from
1454  *
1455  * Gst the caps of the allowed media types that can
1456  * go through this pad.
1457  *
1458  * Returns: the allowed caps
1459  */
1460 GstCaps*
1461 gst_pad_get_allowed_caps (GstPad *pad)
1462 {
1463   g_return_val_if_fail (pad != NULL, NULL);
1464   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1465
1466   GST_DEBUG (GST_CAT_PROPERTIES, "get allowed caps of %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1467
1468   return gst_caps_copy (GST_RPAD_FILTER (pad));
1469 }
1470
1471 /**
1472  * gst_pad_get_allowed_caps:
1473  * @pad: the pad to get the allowed caps from
1474  *
1475  * Gst the caps of the allowed media types that can
1476  * go through this pad.
1477  *
1478  * Returns: the allowed caps
1479  */
1480 gboolean
1481 gst_pad_recalc_allowed_caps (GstPad *pad)
1482 {
1483   GstRealPad *peer;
1484
1485   g_return_val_if_fail (pad != NULL, FALSE);
1486   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1487
1488   GST_DEBUG (GST_CAT_PROPERTIES, "set allowed caps of %s:%s\n", GST_DEBUG_PAD_NAME (pad));
1489
1490   peer = GST_RPAD_PEER (pad);
1491   if (peer)
1492     return gst_pad_try_reconnect_filtered (pad, GST_PAD (peer), GST_RPAD_APPFILTER (pad));
1493
1494   return TRUE;
1495 }
1496
1497 /**
1498  * gst_pad_get_bufferpool:
1499  * @pad: the pad to get the bufferpool from
1500  *
1501  * Get the bufferpool of the peer pad of the given
1502  * pad
1503  *
1504  * Returns: The GstBufferPool or NULL.
1505  */
1506 GstBufferPool*          
1507 gst_pad_get_bufferpool (GstPad *pad)
1508 {
1509   GstRealPad *peer;
1510
1511   g_return_val_if_fail (pad != NULL, NULL);
1512   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1513    
1514   peer = GST_RPAD_PEER (pad);
1515
1516   if (!peer)
1517     return NULL;
1518
1519   GST_DEBUG_ENTER ("(%s:%s)", GST_DEBUG_PAD_NAME (pad));
1520
1521   if (peer->bufferpoolfunc) {
1522     GST_DEBUG (GST_CAT_PADS, "calling bufferpoolfunc &%s (@%p) of peer pad %s:%s\n",
1523       GST_DEBUG_FUNCPTR_NAME (peer->bufferpoolfunc), &peer->bufferpoolfunc, GST_DEBUG_PAD_NAME (((GstPad*) peer)));
1524     return (peer->bufferpoolfunc) (((GstPad*) peer));
1525   } else {
1526     GST_DEBUG (GST_CAT_PADS, "no bufferpoolfunc for peer pad %s:%s at %p\n",
1527                     GST_DEBUG_PAD_NAME (((GstPad*) peer)), &peer->bufferpoolfunc);
1528     return NULL;
1529   }
1530 }
1531
1532 static void
1533 gst_real_pad_dispose (GObject *object)
1534 {
1535   GstPad *pad = GST_PAD (object);
1536   
1537   /* No connected pad can ever be disposed.
1538    * It has to have a parent to be connected and a parent would hold a reference */
1539   g_assert (GST_PAD_PEER (pad) == NULL);
1540
1541   GST_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s\n", GST_DEBUG_PAD_NAME(pad));
1542
1543   if (GST_PAD_PADTEMPLATE (pad)){
1544     GST_DEBUG (GST_CAT_REFCOUNTING, "unreffing padtemplate'%s'\n", GST_OBJECT_NAME (GST_PAD_PADTEMPLATE (pad)));
1545     gst_object_unref (GST_OBJECT (GST_PAD_PADTEMPLATE (pad)));
1546     GST_PAD_PADTEMPLATE (pad) = NULL;
1547   }
1548   
1549   /* we destroy the ghostpads, because they are nothing without the real pad  */
1550   if (GST_REAL_PAD (pad)->ghostpads) {
1551     GList *orig, *ghostpads;
1552
1553     orig = ghostpads = g_list_copy (GST_REAL_PAD (pad)->ghostpads);
1554
1555     while (ghostpads) {
1556       GstPad *ghostpad = GST_PAD (ghostpads->data);
1557
1558       if (GST_IS_ELEMENT (GST_OBJECT_PARENT (ghostpad))){
1559         GST_DEBUG (GST_CAT_REFCOUNTING, "removing ghost pad from element '%s'\n", 
1560                         GST_OBJECT_NAME (GST_OBJECT_PARENT (ghostpad)));
1561
1562         gst_element_remove_ghost_pad (GST_ELEMENT (GST_OBJECT_PARENT (ghostpad)), GST_PAD (ghostpad));
1563       }
1564       ghostpads = g_list_next (ghostpads);
1565     }
1566     g_list_free (orig);
1567     g_list_free (GST_REAL_PAD(pad)->ghostpads);
1568   }
1569
1570   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))){
1571     GST_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'\n",
1572                     GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
1573     
1574     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
1575   }
1576   
1577   G_OBJECT_CLASS (real_pad_parent_class)->dispose (object);
1578 }
1579
1580
1581 #ifndef GST_DISABLE_LOADSAVE
1582 /**
1583  * gst_pad_load_and_connect:
1584  * @self: the XML node to read the description from
1585  * @parent: the element that has the pad
1586  *
1587  * Read the pad definition from the XML node and connect the given pad
1588  * in element to a pad of an element up in the hierarchy.
1589  */
1590 void
1591 gst_pad_load_and_connect (xmlNodePtr self,
1592                           GstObject *parent)
1593 {
1594   xmlNodePtr field = self->xmlChildrenNode;
1595   GstPad *pad = NULL, *targetpad;
1596   guchar *peer = NULL;
1597   gchar **split;
1598   GstElement *target;
1599   GstObject *grandparent;
1600
1601   while (field) {
1602     if (!strcmp (field->name, "name")) {
1603       pad = gst_element_get_pad (GST_ELEMENT (parent), xmlNodeGetContent (field));
1604     }
1605     else if (!strcmp(field->name, "peer")) {
1606       peer = xmlNodeGetContent (field);
1607     }
1608     field = field->next;
1609   }
1610   g_return_if_fail (pad != NULL);
1611
1612   if (peer == NULL) return;
1613
1614   split = g_strsplit (peer, ".", 2);
1615
1616   g_return_if_fail (split[0] != NULL);
1617   g_return_if_fail (split[1] != NULL);
1618
1619   grandparent = gst_object_get_parent (parent);
1620
1621   if (grandparent && GST_IS_BIN (grandparent)) {
1622     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
1623   }
1624   else
1625     goto cleanup;
1626
1627   if (target == NULL) goto cleanup;
1628
1629   targetpad = gst_element_get_pad (target, split[1]);
1630
1631   if (targetpad == NULL) goto cleanup;
1632
1633   gst_pad_connect (pad, targetpad);
1634
1635 cleanup:
1636   g_strfreev (split);
1637 }
1638
1639 /**
1640  * gst_pad_save_thyself:
1641  * @pad: the pad to save
1642  * @parent: the parent XML node to save the description in
1643  *
1644  * Saves the pad into an xml representation
1645  *
1646  * Returns: the xml representation of the pad
1647  */
1648 static xmlNodePtr
1649 gst_pad_save_thyself (GstObject *object,
1650                       xmlNodePtr parent)
1651 {
1652   GstRealPad *realpad;
1653   GstPad *peer;
1654
1655   g_return_val_if_fail (GST_IS_REAL_PAD (object), NULL);
1656
1657   realpad = GST_REAL_PAD(object);
1658
1659   xmlNewChild(parent,NULL,"name", GST_PAD_NAME (realpad));
1660   if (GST_RPAD_PEER(realpad) != NULL) {
1661     peer = GST_PAD(GST_RPAD_PEER(realpad));
1662     /* first check to see if the peer's parent's parent is the same */
1663     /* we just save it off */
1664     xmlNewChild(parent,NULL,"peer",g_strdup_printf("%s.%s",
1665                     GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer)));
1666   } else
1667     xmlNewChild(parent,NULL,"peer","");
1668
1669   return parent;
1670 }
1671
1672 /**
1673  * gst_pad_ghost_save_thyself:
1674  * @pad: the pad to save
1675  * @bin: the bin
1676  * @parent: the parent XML node to save the description in
1677  *
1678  * Saves the ghost pad into an xml representation.
1679  *
1680  * Returns: the xml representation of the pad
1681  */
1682 xmlNodePtr
1683 gst_pad_ghost_save_thyself (GstPad *pad,
1684                             GstElement *bin,
1685                             xmlNodePtr parent)
1686 {
1687   xmlNodePtr self;
1688
1689   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
1690
1691   self = xmlNewChild (parent, NULL, "ghostpad", NULL);
1692   xmlNewChild (self, NULL, "name", GST_PAD_NAME (pad));
1693   xmlNewChild (self, NULL, "parent", GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
1694
1695   /* FIXME FIXME FIXME! */
1696
1697   return self;
1698 }
1699 #endif /* GST_DISABLE_LOADSAVE */
1700
1701 #ifndef gst_pad_push
1702 /**
1703  * gst_pad_push:
1704  * @pad: the pad to push
1705  * @buf: the buffer to push
1706  *
1707  * Push a buffer to the peer of the pad.
1708  */
1709 void 
1710 gst_pad_push (GstPad *pad, GstBuffer *buf) 
1711 {
1712   GstRealPad *peer = GST_RPAD_PEER (pad);
1713
1714   GST_DEBUG_ENTER ("(%s:%s)", GST_DEBUG_PAD_NAME (pad));
1715
1716   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
1717
1718   if (!peer) {
1719     g_warning ("push on pad %s:%s but it is unconnected", GST_DEBUG_PAD_NAME (pad));
1720   }
1721   else {
1722     if (peer->chainhandler) {
1723       if (buf) {
1724         GST_DEBUG (GST_CAT_DATAFLOW, "calling chainhandler &%s of peer pad %s:%s\n",
1725             GST_DEBUG_FUNCPTR_NAME (peer->chainhandler), GST_DEBUG_PAD_NAME (GST_PAD (peer)));
1726         (peer->chainhandler) (GST_PAD_CAST (peer), buf);
1727         return;
1728       }
1729       else {
1730         g_warning ("trying to push a NULL buffer on pad %s:%s", GST_DEBUG_PAD_NAME (peer));
1731         return;
1732       }
1733     } 
1734     else {
1735       g_warning ("(internal error) push on pad %s:%s but it has no chainhandler", GST_DEBUG_PAD_NAME (peer));
1736     }
1737   }
1738   /* clean up the mess here */
1739   if (buf != NULL) {
1740     if (GST_IS_BUFFER (buf))
1741       gst_buffer_unref (buf);
1742     else
1743       gst_event_free (GST_EVENT (buf));
1744   }
1745 }
1746 #endif
1747
1748 #ifndef gst_pad_pull
1749 /**
1750  * gst_pad_pull:
1751  * @pad: the pad to pull
1752  *
1753  * Pull a buffer from the peer pad.
1754  *
1755  * Returns: a new buffer from the peer pad.
1756  */
1757 GstBuffer*
1758 gst_pad_pull (GstPad *pad) 
1759 {
1760   GstRealPad *peer = GST_RPAD_PEER(pad);
1761   
1762   GST_DEBUG_ENTER("(%s:%s)",GST_DEBUG_PAD_NAME(pad));
1763
1764   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1765
1766   if (!peer) {
1767     gst_element_error (GST_PAD_PARENT (pad), 
1768                     "pull on pad %s:%s but it was unconnected", 
1769                     GST_ELEMENT_NAME (GST_PAD_PARENT (pad)), GST_PAD_NAME (pad),
1770                     NULL);
1771   }
1772   else {
1773     if (peer->gethandler) {
1774       GstBuffer *buf;
1775
1776       GST_DEBUG (GST_CAT_DATAFLOW, "calling gethandler %s of peer pad %s:%s\n",
1777         GST_DEBUG_FUNCPTR_NAME (peer->gethandler), GST_DEBUG_PAD_NAME (peer));
1778
1779       buf = (peer->gethandler) (GST_PAD_CAST (peer));
1780       if (buf)
1781         return buf;
1782       /* no null buffers allowed */
1783       gst_element_error (GST_PAD_PARENT (pad), 
1784                     "NULL buffer during pull on %s:%s", GST_DEBUG_PAD_NAME (pad), NULL);
1785           
1786     } else {
1787       gst_element_error (GST_PAD_PARENT (pad), 
1788                     "(internal error) pull on pad %s:%s but the peer pad %s:%s has no gethandler", 
1789                     GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer),
1790                     NULL);
1791     }
1792   }
1793   return NULL;
1794 }
1795 #endif
1796
1797 #ifndef gst_pad_pullregion
1798 /**
1799  * gst_pad_pullregion:
1800  * @pad: the pad to pull the region from
1801  * @type: the regiontype
1802  * @offset: the offset/start of the buffer to pull
1803  * @len: the length of the buffer to pull
1804  *
1805  * Pull a buffer region from the peer pad. The region to pull can be 
1806  * specified with a offset/lenght pair or with a start/legnth time
1807  * indicator as specified by the type parameter.
1808  *
1809  * Returns: a new buffer from the peer pad with data in the specified
1810  * region.
1811  */
1812 GstBuffer*
1813 gst_pad_pullregion (GstPad *pad, GstRegionType type, guint64 offset, guint64 len) 
1814 {
1815   GstRealPad *peer;
1816   GstBuffer *result = NULL;
1817   
1818   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1819
1820   do {
1821     peer = GST_RPAD_PEER(pad);
1822     g_return_val_if_fail (peer != NULL, NULL);
1823
1824     if (result) 
1825       gst_buffer_unref (result);
1826
1827     GST_DEBUG_ENTER("(%s:%s,%d,%lld,%lld)",GST_DEBUG_PAD_NAME(pad),type,offset,len);
1828
1829     if (peer->pullregionfunc) {
1830       GST_DEBUG (GST_CAT_DATAFLOW, "calling pullregionfunc &%s of peer pad %s:%s\n",
1831           GST_DEBUG_FUNCPTR_NAME (peer->pullregionfunc), GST_DEBUG_PAD_NAME(GST_PAD_CAST (peer)));
1832       result = (peer->pullregionfunc) (GST_PAD_CAST (peer), type, offset, len);
1833     } else {
1834       GST_DEBUG (GST_CAT_DATAFLOW,"no pullregionfunc\n");
1835       result = NULL;
1836       break;
1837     }
1838   }
1839   /* FIXME */
1840   while (result && !(GST_BUFFER_OFFSET (result) == offset && 
1841            GST_BUFFER_SIZE (result) == len));
1842
1843   return result;
1844 }
1845 #endif
1846
1847 /**
1848  * gst_pad_peek:
1849  * @pad: the pad to peek
1850  *
1851  * Peek for a buffer from the peer pad.
1852  *
1853  * Returns: a from the peer pad or NULL if the peer has no buffer.
1854  */
1855 GstBuffer*
1856 gst_pad_peek (GstPad *pad)
1857 {
1858   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK, NULL);
1859
1860   return GST_RPAD_BUFPEN (GST_RPAD_PEER (pad));
1861 }
1862
1863 /**
1864  * gst_pad_select:
1865  * @padlist: A list of pads 
1866  *
1867  * Wait for a buffer on the list of pads.
1868  *
1869  * Returns: The pad that has a buffer available, use 
1870  * #gst_pad_pull to get the buffer.
1871  */
1872 GstPad*
1873 gst_pad_select (GList *padlist)
1874 {
1875   GstPad *pad;
1876
1877   pad = gst_scheduler_pad_select (GST_PAD_PARENT (padlist->data)->sched, padlist);
1878
1879   return pad;
1880 }
1881
1882 /**
1883  * gst_pad_selectv:
1884  * @pad: The first pad to perform the select on 
1885  * @...: More pads
1886  *
1887  * Wait for a buffer on the given of pads.
1888  *
1889  * Returns: The pad that has a buffer available, use 
1890  * #gst_pad_pull to get the buffer.
1891  */
1892 GstPad*
1893 gst_pad_selectv (GstPad *pad, ...)
1894 {
1895   GstPad *result;
1896   GList *padlist = NULL;
1897   va_list var_args;
1898
1899   if (pad == NULL)
1900     return NULL;
1901
1902   va_start (var_args, pad);
1903
1904   while (pad) {
1905     padlist = g_list_prepend (padlist, pad);
1906     pad = va_arg (var_args, GstPad *);
1907   }
1908   result = gst_pad_select (padlist);
1909   g_list_free (padlist);
1910
1911   va_end (var_args);
1912   
1913   return result;
1914 }
1915
1916 /************************************************************************
1917  *
1918  * templates
1919  *
1920  */
1921 static void             gst_padtemplate_class_init      (GstPadTemplateClass *klass);
1922 static void             gst_padtemplate_init            (GstPadTemplate *templ);
1923
1924 enum {
1925   TEMPL_PAD_CREATED,
1926   /* FILL ME */
1927   TEMPL_LAST_SIGNAL
1928 };
1929
1930 static GstObject *padtemplate_parent_class = NULL;
1931 static guint gst_padtemplate_signals[TEMPL_LAST_SIGNAL] = { 0 };
1932
1933 GType
1934 gst_padtemplate_get_type (void)
1935 {
1936   static GType padtemplate_type = 0;
1937
1938   if (!padtemplate_type) {
1939     static const GTypeInfo padtemplate_info = {
1940       sizeof(GstPadTemplateClass),
1941       NULL,
1942       NULL,
1943       (GClassInitFunc)gst_padtemplate_class_init,
1944       NULL,
1945       NULL,
1946       sizeof(GstPadTemplate),
1947       32,
1948       (GInstanceInitFunc)gst_padtemplate_init,
1949       NULL
1950     };
1951     padtemplate_type = g_type_register_static(GST_TYPE_OBJECT, "GstPadTemplate", &padtemplate_info, 0);
1952   }
1953   return padtemplate_type;
1954 }
1955
1956 static void
1957 gst_padtemplate_class_init (GstPadTemplateClass *klass)
1958 {
1959   GObjectClass *gobject_class;
1960   GstObjectClass *gstobject_class;
1961
1962   gobject_class = (GObjectClass*)klass;
1963   gstobject_class = (GstObjectClass*)klass;
1964
1965   padtemplate_parent_class = g_type_class_ref(GST_TYPE_OBJECT);
1966
1967   gst_padtemplate_signals[TEMPL_PAD_CREATED] =
1968     g_signal_new ("pad_created", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
1969                     G_STRUCT_OFFSET (GstPadTemplateClass, pad_created), NULL, NULL,
1970                     gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
1971                     G_TYPE_POINTER);
1972
1973
1974   gstobject_class->path_string_separator = "*";
1975 }
1976
1977 static void
1978 gst_padtemplate_init (GstPadTemplate *templ)
1979 {
1980 }
1981
1982 /**
1983  * gst_padtemplate_new:
1984  * @name_template: the name template
1985  * @direction: the direction for the template
1986  * @presence: the presence of the pad
1987  * @caps: a list of capabilities for the template
1988  * @...: more capabilities
1989  *
1990  * Creates a new padtemplate from the given arguments.
1991  *
1992  * Returns: the new padtemplate
1993  */
1994 GstPadTemplate*
1995 gst_padtemplate_new (gchar *name_template,
1996                      GstPadDirection direction, GstPadPresence presence,
1997                      GstCaps *caps, ...)
1998 {
1999   GstPadTemplate *new;
2000   va_list var_args;
2001   GstCaps *thecaps = NULL;
2002
2003   g_return_val_if_fail (name_template != NULL, NULL);
2004
2005   new = g_object_new(gst_padtemplate_get_type () ,NULL);
2006
2007   GST_PADTEMPLATE_NAME_TEMPLATE (new) = name_template;
2008   GST_PADTEMPLATE_DIRECTION (new) = direction;
2009   GST_PADTEMPLATE_PRESENCE (new) = presence;
2010
2011   va_start (var_args, caps);
2012
2013   while (caps) {
2014     new->fixed &= caps->fixed;
2015     thecaps = gst_caps_append (thecaps, caps);
2016     caps = va_arg (var_args, GstCaps*);
2017   }
2018   va_end (var_args);
2019   
2020   GST_PADTEMPLATE_CAPS (new) = thecaps;
2021
2022   return new;
2023 }
2024
2025 /**
2026  * gst_padtemplate_get_caps:
2027  * @templ: the padtemplate to use
2028  *
2029  * Get the capabilities of the padtemplate
2030  *
2031  * Returns: a GstCaps*
2032  */
2033 GstCaps*
2034 gst_padtemplate_get_caps (GstPadTemplate *templ)
2035 {
2036   g_return_val_if_fail (templ != NULL, NULL);
2037
2038   return GST_PADTEMPLATE_CAPS (templ);
2039 }
2040
2041 #ifndef GST_DISABLE_LOADSAVE
2042 /**
2043  * gst_padtemplate_save_thyself:
2044  * @templ: the padtemplate to save
2045  * @parent: the parent XML tree
2046  *
2047  * Saves the padtemplate into XML.
2048  *
2049  * Returns: the new XML tree
2050  */
2051 xmlNodePtr
2052 gst_padtemplate_save_thyself (GstPadTemplate *templ, xmlNodePtr parent)
2053 {
2054   xmlNodePtr subtree;
2055   guchar *presence;
2056
2057   GST_DEBUG (GST_CAT_XML,"saving padtemplate %s\n", templ->name_template);
2058
2059   xmlNewChild(parent,NULL,"nametemplate", templ->name_template);
2060   xmlNewChild(parent,NULL,"direction", (templ->direction == GST_PAD_SINK? "sink":"src"));
2061
2062   switch (templ->presence) {
2063     case GST_PAD_ALWAYS:
2064       presence = "always";
2065       break;
2066     case GST_PAD_SOMETIMES:
2067       presence = "sometimes";
2068       break;
2069     case GST_PAD_REQUEST:
2070       presence = "request";
2071       break;
2072     default:
2073       presence = "unknown";
2074       break;
2075   }
2076   xmlNewChild(parent,NULL,"presence", presence);
2077
2078   if (GST_PADTEMPLATE_CAPS (templ)) {
2079     subtree = xmlNewChild (parent, NULL, "caps", NULL);
2080     gst_caps_save_thyself (GST_PADTEMPLATE_CAPS (templ), subtree);
2081   }
2082
2083   return parent;
2084 }
2085
2086 /**
2087  * gst_padtemplate_load_thyself:
2088  * @parent: the source XML tree
2089  *
2090  * Loads a padtemplate from the XML tree.
2091  *
2092  * Returns: the new padtemplate
2093  */
2094 GstPadTemplate*
2095 gst_padtemplate_load_thyself (xmlNodePtr parent)
2096 {
2097   xmlNodePtr field = parent->xmlChildrenNode;
2098   GstPadTemplate *factory;
2099   gchar *name_template = NULL;
2100   GstPadDirection direction = GST_PAD_UNKNOWN;
2101   GstPadPresence presence = GST_PAD_ALWAYS;
2102   GstCaps *caps = NULL;
2103
2104   while (field) {
2105     if (!strcmp(field->name, "nametemplate")) {
2106       name_template = xmlNodeGetContent(field);
2107     }
2108     if (!strcmp(field->name, "direction")) {
2109       gchar *value = xmlNodeGetContent(field);
2110
2111       if (!strcmp(value, "sink")) {
2112         direction = GST_PAD_SINK;
2113       }
2114       else if (!strcmp(value, "src")) {
2115         direction = GST_PAD_SRC;
2116       }
2117       g_free (value);
2118     }
2119     if (!strcmp(field->name, "presence")) {
2120       gchar *value = xmlNodeGetContent(field);
2121
2122       if (!strcmp(value, "always")) {
2123         presence = GST_PAD_ALWAYS;
2124       }
2125       else if (!strcmp(value, "sometimes")) {
2126         presence = GST_PAD_SOMETIMES;
2127       }
2128       else if (!strcmp(value, "request")) {
2129         presence = GST_PAD_REQUEST;
2130       }
2131       g_free (value);
2132     }
2133     else if (!strcmp(field->name, "caps")) {
2134       caps = gst_caps_load_thyself (field);
2135     }
2136     field = field->next;
2137   }
2138
2139   factory = gst_padtemplate_new (name_template, direction, presence, caps, NULL);
2140
2141   return factory;
2142 }
2143 #endif /* !GST_DISABLE_LOADSAVE */
2144
2145
2146 /**
2147  * gst_pad_set_element_private:
2148  * @pad: the pad to set the private data to
2149  * @priv: The private data to attach to the pad
2150  *
2151  * Set the given private data pointer to the pad. This
2152  * function can only be used by the element that own the
2153  * pad.
2154  */
2155 void
2156 gst_pad_set_element_private (GstPad *pad, gpointer priv)
2157 {
2158   pad->element_private = priv;
2159 }
2160
2161 /**
2162  * gst_pad_get_element_private:
2163  * @pad: the pad to get the private data of
2164  *
2165  * Get the private data of a pad. The private data can
2166  * only be set by the parent element of this pad.
2167  *
2168  * Returns: a pointer to the private data.
2169  */
2170 gpointer
2171 gst_pad_get_element_private (GstPad *pad)
2172 {
2173   return pad->element_private;
2174 }
2175
2176
2177 /***** ghost pads *****/
2178 GType _gst_ghost_pad_type = 0;
2179
2180 static void     gst_ghost_pad_class_init         (GstGhostPadClass *klass);
2181 static void     gst_ghost_pad_init               (GstGhostPad *pad);
2182
2183 static GstPad *ghost_pad_parent_class = NULL;
2184 /* static guint gst_ghost_pad_signals[LAST_SIGNAL] = { 0 }; */
2185
2186 GType
2187 gst_ghost_pad_get_type(void) {
2188   if (!_gst_ghost_pad_type) {
2189     static const GTypeInfo pad_info = {
2190       sizeof(GstGhostPadClass),
2191       NULL,
2192       NULL,
2193       (GClassInitFunc)gst_ghost_pad_class_init,
2194       NULL,
2195       NULL,
2196       sizeof(GstGhostPad),
2197       8,
2198       (GInstanceInitFunc)gst_ghost_pad_init,
2199       NULL
2200     };
2201     _gst_ghost_pad_type = g_type_register_static(GST_TYPE_PAD, "GstGhostPad", &pad_info, 0);
2202   }
2203   return _gst_ghost_pad_type;
2204 }
2205
2206 static void
2207 gst_ghost_pad_class_init (GstGhostPadClass *klass)
2208 {
2209   GObjectClass *gobject_class;
2210
2211   gobject_class = (GObjectClass*)klass;
2212
2213   ghost_pad_parent_class = g_type_class_ref(GST_TYPE_PAD);
2214 }
2215
2216 static void
2217 gst_ghost_pad_init (GstGhostPad *pad)
2218 {
2219   pad->realpad = NULL;
2220 }
2221
2222 /**
2223  * gst_ghost_pad_new:
2224  * @name: name of the new ghost pad
2225  * @pad: the pad to create a ghost pad of
2226  *
2227  * Create a new ghost pad associated with the given pad.
2228  *
2229  * Returns: new ghost pad
2230  */
2231 GstPad*
2232 gst_ghost_pad_new (gchar *name,
2233                    GstPad *pad)
2234 {
2235   GstGhostPad *ghostpad;
2236
2237   g_return_val_if_fail (name != NULL, NULL);
2238   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2239
2240   ghostpad = g_object_new (gst_ghost_pad_get_type () ,NULL);
2241   gst_pad_set_name (GST_PAD (ghostpad), name);
2242   GST_GPAD_REALPAD (ghostpad) = GST_PAD_REALIZE (pad);
2243   GST_PAD_PADTEMPLATE (ghostpad) = GST_PAD_PADTEMPLATE (pad);
2244
2245   /* add ourselves to the real pad's list of ghostpads */
2246   gst_pad_add_ghost_pad (pad, GST_PAD (ghostpad));
2247
2248   /* FIXME need to ref the real pad here... ? */
2249
2250   GST_DEBUG (GST_CAT_PADS, "created ghost pad \"%s\"\n", name);
2251
2252   return GST_PAD (ghostpad);
2253 }
2254
2255 static void 
2256 gst_pad_event_default_dispatch (GstPad *pad, GstElement *element, GstEvent *event)
2257 {
2258   GList *pads = element->pads;
2259
2260   while (pads) {
2261     GstPad *eventpad = GST_PAD (pads->data);
2262     pads = g_list_next (pads);
2263
2264     /* for all pads in the opposite direction that are connected */
2265     if (GST_PAD_DIRECTION (eventpad) != GST_PAD_DIRECTION (pad) && GST_PAD_IS_CONNECTED (eventpad)) {
2266       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2267         gst_pad_push (eventpad, GST_BUFFER (gst_event_new (GST_EVENT_TYPE (event))));
2268       }
2269       else {
2270         GstPad *peerpad = GST_PAD_CAST (GST_RPAD_PEER (eventpad));
2271
2272         gst_pad_send_event (peerpad, gst_event_new (GST_EVENT_TYPE (event)));
2273       }
2274     }
2275   }
2276 }
2277
2278 /**
2279  * gst_pad_event_default:
2280  * @pad: the pad to operate on
2281  * @event: the event to handle
2282  *
2283  * Invoke the default event handler for the given pad.
2284  */
2285 void 
2286 gst_pad_event_default (GstPad *pad, GstEvent *event)
2287 {
2288   GstElement *element = GST_PAD_PARENT (pad);
2289
2290   g_signal_emit (G_OBJECT (pad), gst_real_pad_signals[REAL_EVENT_RECEIVED], 0, event);
2291  
2292   switch (GST_EVENT_TYPE (event)) {
2293     case GST_EVENT_EOS:
2294       gst_element_set_eos (element);
2295       gst_pad_event_default_dispatch (pad, element, event);
2296       gst_event_free (event);
2297       /* we have to try to schedule another element because this one is disabled */
2298       gst_element_yield (element);
2299       break;
2300     case GST_EVENT_FLUSH:
2301     default:
2302       gst_pad_event_default_dispatch (pad, element, event);
2303       gst_event_free (event);
2304       break;
2305   }
2306 }
2307
2308 /**
2309  * gst_pad_send_event:
2310  * @pad: the pad to send the event to
2311  * @event: the event to send to the pad.
2312  *
2313  * Send the event to the pad.
2314  *
2315  * Returns: TRUE if the event was handled.
2316  */
2317 gboolean
2318 gst_pad_send_event (GstPad *pad, GstEvent *event)
2319 {
2320   gboolean handled = FALSE;
2321
2322   g_return_val_if_fail (event, FALSE);
2323
2324   if (GST_EVENT_SRC (event) == NULL)
2325     GST_EVENT_SRC (event) = gst_object_ref (GST_OBJECT (pad));
2326
2327   GST_DEBUG (GST_CAT_EVENT, "have event %d on pad %s:%s\n",
2328                   GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
2329
2330   if (GST_RPAD_EVENTFUNC (pad))
2331     handled = GST_RPAD_EVENTFUNC (pad) (pad, event);
2332   else {
2333     GST_DEBUG(GST_CAT_EVENT, "there's no event function for pad %s:%s\n", GST_DEBUG_PAD_NAME (pad));
2334   }
2335
2336   if (!handled) {
2337     GST_DEBUG(GST_CAT_EVENT, "proceeding with default event behavior here\n");
2338     gst_pad_event_default (pad, event);
2339     handled = TRUE;
2340   }
2341
2342   return handled;
2343 }
2344