gst/gstbin.h: removed ';' from the end of macro defs
[platform/upstream/gstreamer.git] / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpad.c: Pads for linking elements together
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "gst_private.h"
24
25 #include "gstpad.h"
26 #include "gstenumtypes.h"
27 #include "gstmarshal.h"
28 #include "gstutils.h"
29 #include "gstinfo.h"
30 #include "gsterror.h"
31 #include "gstvalue.h"
32
33 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
34 #define DEBUG_DATA(obj,data,notice) G_STMT_START{\
35   if (!data) { \
36     GST_CAT_DEBUG_OBJECT (debug_dataflow, obj, "NULL data value"); \
37   } else if (GST_IS_EVENT (data)) { \
38     GST_CAT_DEBUG_OBJECT (debug_dataflow, obj, "%s event %p (type %d, refcount %d)", notice, data, \
39         GST_EVENT_TYPE (data), GST_DATA_REFCOUNT_VALUE (data)); \
40   } else { \
41     GST_CAT_LOG_OBJECT (debug_dataflow, obj, "%s buffer %p (size %u, refcount %d)", notice, data, \
42         GST_BUFFER_SIZE (data), GST_BUFFER_REFCOUNT_VALUE (data)); \
43   } \
44 }G_STMT_END
45 #define GST_CAT_DEFAULT GST_CAT_PADS
46
47 enum
48 {
49   TEMPL_PAD_CREATED,
50   /* FILL ME */
51   TEMPL_LAST_SIGNAL
52 };
53
54 static GstObject *padtemplate_parent_class = NULL;
55 static guint gst_pad_template_signals[TEMPL_LAST_SIGNAL] = { 0 };
56
57 /* Pad signals and args */
58 enum
59 {
60   PAD_LINKED,
61   PAD_UNLINKED,
62   PAD_REQUEST_LINK,
63   PAD_HAVE_DATA,
64   /* FILL ME */
65   PAD_LAST_SIGNAL
66 };
67
68 enum
69 {
70   PAD_PROP_0,
71   PAD_PROP_CAPS,
72   PAD_PROP_DIRECTION,
73   PAD_PROP_TEMPLATE,
74   /* FILL ME */
75 };
76
77 GType _gst_pad_type = 0;
78
79 static void gst_pad_class_init (GstPadClass * klass);
80 static void gst_pad_init (GstPad * pad);
81 static void gst_pad_dispose (GObject * object);
82 static void gst_pad_finalize (GObject * object);
83 static void gst_pad_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_pad_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87
88 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
89 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
90 static gboolean gst_pad_activate_default (GstPad * pad);
91
92 #ifndef GST_DISABLE_LOADSAVE
93 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
94 #endif
95
96 static GstObjectClass *pad_parent_class = NULL;
97 static guint gst_pad_signals[PAD_LAST_SIGNAL] = { 0 };
98
99 GType
100 gst_pad_get_type (void)
101 {
102   if (!_gst_pad_type) {
103     static const GTypeInfo pad_info = {
104       sizeof (GstPadClass), NULL, NULL,
105       (GClassInitFunc) gst_pad_class_init, NULL, NULL,
106       sizeof (GstPad),
107       0,
108       (GInstanceInitFunc) gst_pad_init, NULL
109     };
110
111     _gst_pad_type = g_type_register_static (GST_TYPE_OBJECT, "GstPad",
112         &pad_info, 0);
113
114     GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW",
115         GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads");
116   }
117   return _gst_pad_type;
118 }
119
120 static gboolean
121 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
122     GValue * return_accu, const GValue * handler_return, gpointer dummy)
123 {
124   gboolean ret = g_value_get_boolean (handler_return);
125
126   GST_DEBUG ("accumulated %d", ret);
127   g_value_set_boolean (return_accu, ret);
128
129   return ret;
130 }
131
132 static gboolean
133 default_have_data (GstPad * pad, GstMiniObject * o)
134 {
135   return TRUE;
136 }
137
138 static void
139 gst_pad_class_init (GstPadClass * klass)
140 {
141   GObjectClass *gobject_class;
142
143
144   GstObjectClass *gstobject_class;
145
146   gobject_class = (GObjectClass *) klass;
147   gstobject_class = (GstObjectClass *) klass;
148
149   pad_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
150
151   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
152   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pad_finalize);
153   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pad_set_property);
154   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pad_get_property);
155
156   /**
157    * GstPad::linked:
158    * @pad: the pad that emitted the signal
159    * @peer: the peer pad that has been connected
160    *
161    * Signals that a pad has been linked to the peer pad.
162    */
163   gst_pad_signals[PAD_LINKED] =
164       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
165       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
166       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
167   /**
168    * GstPad::unlinked:
169    * @pad: the pad that emitted the signal
170    * @peer: the peer pad that has been disconnected
171    *
172    * Signals that a pad has been unlinked from the peer pad.
173    */
174   gst_pad_signals[PAD_UNLINKED] =
175       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
177       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
178   /**
179    * GstPad::request-link:
180    * @pad: the pad that emitted the signal
181    * @peer: the peer pad for which a connection is requested
182    *
183    * Signals that a pad connection has been requested.
184    */
185   gst_pad_signals[PAD_REQUEST_LINK] =
186       g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
187       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
188       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
189
190   /**
191    * GstPad::have-data:
192    * @pad: the pad that emitted the signal
193    * @mini_obj: new data
194    *
195    * Signals that new data is available on the pad. This signal is used 
196    * internally for implementing pad probes. 
197    * See gst_pad_add_*_probe functions.
198    *
199    * Returns: %TRUE to keep the data, %FALSE to drop it
200    */
201   gst_pad_signals[PAD_HAVE_DATA] =
202       g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
203       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, have_data),
204       _gst_do_pass_data_accumulator,
205       NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
206       GST_TYPE_MINI_OBJECT);
207
208   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_CAPS,
209       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
210           GST_TYPE_CAPS, G_PARAM_READABLE));
211   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_DIRECTION,
212       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
213           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
214           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
215   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_TEMPLATE,
216       g_param_spec_object ("template", "Template",
217           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
218           G_PARAM_READWRITE));
219
220 #ifndef GST_DISABLE_LOADSAVE
221   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
222 #endif
223   gstobject_class->path_string_separator = ".";
224
225   klass->have_data = default_have_data;
226 }
227
228 static void
229 gst_pad_init (GstPad * pad)
230 {
231   pad->direction = GST_PAD_UNKNOWN;
232   pad->peer = NULL;
233
234   pad->chainfunc = NULL;
235
236   pad->caps = NULL;
237
238   pad->linkfunc = NULL;
239   pad->getcapsfunc = NULL;
240
241   pad->activatefunc = gst_pad_activate_default;
242   pad->eventfunc = gst_pad_event_default;
243   pad->querytypefunc = gst_pad_get_query_types_default;
244   pad->queryfunc = gst_pad_query_default;
245   pad->intlinkfunc = gst_pad_get_internal_links_default;
246
247   pad->do_buffer_signals = 0;
248   pad->do_event_signals = 0;
249
250   GST_PAD_UNSET_FLUSHING (pad);
251
252   pad->preroll_lock = g_mutex_new ();
253   pad->preroll_cond = g_cond_new ();
254
255   pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
256   g_static_rec_mutex_init (pad->stream_rec_lock);
257
258   pad->block_cond = g_cond_new ();
259 }
260
261 static void
262 gst_pad_dispose (GObject * object)
263 {
264   GstPad *pad = GST_PAD (object);
265
266   gst_pad_set_pad_template (pad, NULL);
267   /* FIXME, we have links to many other things like caps
268    * and the peer pad... */
269
270   /* No linked pad can ever be disposed.
271    * It has to have a parent to be linked 
272    * and a parent would hold a reference */
273   /* FIXME: what about if g_object_dispose is explicitly called on the pad? Is
274      that legal? otherwise we could assert GST_OBJECT_PARENT (pad) == NULL as
275      well... */
276   g_assert (GST_PAD_PEER (pad) == NULL);
277
278   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s",
279       GST_DEBUG_PAD_NAME (pad));
280
281   /* clear the caps */
282   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
283
284   if (GST_IS_ELEMENT (GST_OBJECT_PARENT (pad))) {
285     GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "removing pad from element '%s'",
286         GST_OBJECT_NAME (GST_OBJECT (GST_ELEMENT (GST_OBJECT_PARENT (pad)))));
287
288     gst_element_remove_pad (GST_ELEMENT (GST_OBJECT_PARENT (pad)), pad);
289   }
290
291   G_OBJECT_CLASS (pad_parent_class)->dispose (object);
292 }
293
294 static void
295 gst_pad_finalize (GObject * object)
296 {
297   GstPad *pad = GST_PAD (object);
298   GstTask *task;
299
300   /* in case the task is still around, clean it up */
301   if ((task = GST_PAD_TASK (pad))) {
302     gst_task_join (task);
303     GST_PAD_TASK (pad) = NULL;
304     gst_object_unref (task);
305   }
306
307   if (pad->stream_rec_lock) {
308     g_static_rec_mutex_free (pad->stream_rec_lock);
309     g_free (pad->stream_rec_lock);
310     pad->stream_rec_lock = NULL;
311   }
312   if (pad->preroll_lock) {
313     g_mutex_free (pad->preroll_lock);
314     g_cond_free (pad->preroll_cond);
315     pad->preroll_lock = NULL;
316     pad->preroll_cond = NULL;
317   }
318   if (pad->block_cond) {
319     g_cond_free (pad->block_cond);
320     pad->block_cond = NULL;
321   }
322
323   G_OBJECT_CLASS (pad_parent_class)->finalize (object);
324 }
325
326 static void
327 gst_pad_set_property (GObject * object, guint prop_id,
328     const GValue * value, GParamSpec * pspec)
329 {
330   g_return_if_fail (GST_IS_PAD (object));
331
332   switch (prop_id) {
333     case PAD_PROP_DIRECTION:
334       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
335       break;
336     case PAD_PROP_TEMPLATE:
337       gst_pad_set_pad_template (GST_PAD_CAST (object),
338           (GstPadTemplate *) g_value_dup_object (value));
339       break;
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }
345
346 static void
347 gst_pad_get_property (GObject * object, guint prop_id,
348     GValue * value, GParamSpec * pspec)
349 {
350   g_return_if_fail (GST_IS_PAD (object));
351
352   switch (prop_id) {
353     case PAD_PROP_CAPS:
354       g_value_set_boxed (value, GST_PAD_CAPS (object));
355       break;
356     case PAD_PROP_DIRECTION:
357       g_value_set_enum (value, GST_PAD_DIRECTION (object));
358       break;
359     case PAD_PROP_TEMPLATE:
360       g_value_set_object (value, GST_PAD_TEMPLATE (object));
361       break;
362     default:
363       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364       break;
365   }
366 }
367
368 /**
369  * gst_pad_new:
370  * @name: the name of the new pad.
371  * @direction: the #GstPadDirection of the pad.
372  *
373  * Creates a new pad with the given name in the given direction.
374  * If name is NULL, a guaranteed unique name (across all pads) 
375  * will be assigned.
376  * This function makes a copy of the name so you can safely free the name.
377  *
378  * Returns: a new #GstPad, or NULL in case of an error.
379  *
380  * MT safe.
381  */
382 GstPad *
383 gst_pad_new (const gchar * name, GstPadDirection direction)
384 {
385   return g_object_new (GST_TYPE_PAD,
386       "name", name, "direction", direction, NULL);
387 }
388
389 /**
390  * gst_pad_new_from_template:
391  * @templ: the pad template to use
392  * @name: the name of the element
393  *
394  * Creates a new pad with the given name from the given template.
395  * If name is NULL, a guaranteed unique name (across all pads) 
396  * will be assigned.
397  * This function makes a copy of the name so you can safely free the name.
398  *
399  * Returns: a new #GstPad, or NULL in case of an error.
400  */
401 GstPad *
402 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
403 {
404   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
405
406   return g_object_new (GST_TYPE_PAD,
407       "name", name, "direction", templ->direction, "template", templ, NULL);
408 }
409
410 /**
411  * gst_pad_get_direction:
412  * @pad: a #GstPad to get the direction of.
413  *
414  * Gets the direction of the pad. The direction of the pad is
415  * decided at construction time so this function does not take 
416  * the LOCK.
417  *
418  * Returns: the #GstPadDirection of the pad.
419  *
420  * MT safe.
421  */
422 GstPadDirection
423 gst_pad_get_direction (GstPad * pad)
424 {
425   GstPadDirection result;
426
427   /* PAD_UNKNOWN is a little silly but we need some sort of
428    * error return value */
429   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
430
431   GST_LOCK (pad);
432   result = GST_PAD_DIRECTION (pad);
433   GST_UNLOCK (pad);
434
435   return result;
436 }
437
438 static gboolean
439 gst_pad_activate_default (GstPad * pad)
440 {
441   return gst_pad_activate_push (pad, TRUE);
442 }
443
444 static void
445 pre_activate_switch (GstPad * pad, gboolean new_active)
446 {
447   if (new_active) {
448     return;
449   } else {
450     GST_LOCK (pad);
451     GST_PAD_SET_FLUSHING (pad);
452     /* unlock blocked pads so element can resume and stop */
453     GST_PAD_BLOCK_SIGNAL (pad);
454     GST_UNLOCK (pad);
455   }
456 }
457
458 static void
459 post_activate_switch (GstPad * pad, gboolean new_active)
460 {
461   if (new_active) {
462     GST_LOCK (pad);
463     GST_PAD_UNSET_FLUSHING (pad);
464     GST_UNLOCK (pad);
465   } else {
466     /* make streaming stop */
467     GST_STREAM_LOCK (pad);
468     GST_STREAM_UNLOCK (pad);
469   }
470 }
471
472 /**
473  * gst_pad_set_active:
474  * @pad: the #GstPad to activate or deactivate.
475  * @active: whether or not the pad should be active.
476  *
477  * Activates or deactivates the given pad. Must be called with the STATE_LOCK.
478  * Normally called from within core state change functions.
479  *
480  * If @active, makes sure the pad is active. If it is already active, either in
481  * push or pull mode, just return. Otherwise dispatches to the pad's activate
482  * function to perform the actual activation.
483  *
484  * If not @active, checks the pad's current mode and calls
485  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
486  * FALSE argument.
487  *
488  * Returns: TRUE if the operation was successfull.
489  *
490  * MT safe. Must be called with STATE_LOCK.
491  */
492 gboolean
493 gst_pad_set_active (GstPad * pad, gboolean active)
494 {
495   GstActivateMode old;
496   gboolean ret = FALSE;
497
498   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
499
500   GST_LOCK (pad);
501   old = GST_PAD_ACTIVATE_MODE (pad);
502   GST_UNLOCK (pad);
503
504   if (active) {
505     switch (old) {
506       case GST_ACTIVATE_PUSH:
507       case GST_ACTIVATE_PULL:
508         ret = TRUE;
509         break;
510       case GST_ACTIVATE_NONE:
511         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
512         break;
513     }
514   } else {
515     switch (old) {
516       case GST_ACTIVATE_PUSH:
517         ret = gst_pad_activate_push (pad, FALSE);
518         break;
519       case GST_ACTIVATE_PULL:
520         ret = gst_pad_activate_pull (pad, FALSE);
521         break;
522       case GST_ACTIVATE_NONE:
523         ret = TRUE;
524         break;
525     }
526   }
527
528   return ret;
529 }
530
531 /**
532  * gst_pad_activate_pull:
533  * @pad: the #GstPad to activate or deactivate.
534  * @active: whether or not the pad should be active.
535  *
536  * Activates or deactivates the given pad in pull mode via dispatching to the
537  * pad's activatepullfunc. For use from within pad activation functions only.
538  * When called on sink pads, will first proxy the call to the peer pad, which is
539  * expected to activate its internally linked pads from within its activate_pull
540  * function.
541  *
542  * If you don't know what this is, you probably don't want to call it.
543  *
544  * Returns: TRUE if the operation was successfull.
545  *
546  * MT safe.
547  */
548 gboolean
549 gst_pad_activate_pull (GstPad * pad, gboolean active)
550 {
551   GstActivateMode old;
552
553   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
554
555   GST_LOCK (pad);
556   old = GST_PAD_ACTIVATE_MODE (pad);
557   GST_UNLOCK (pad);
558
559   if ((active && old == GST_ACTIVATE_PULL)
560       || (!active && old == GST_ACTIVATE_NONE))
561     goto was_ok;
562
563   if (active) {
564     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
565   } else {
566     g_return_val_if_fail (old == GST_ACTIVATE_PULL, FALSE);
567   }
568
569   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
570     GstPad *peer = gst_pad_get_peer (pad);
571
572     if (peer) {
573       if (!gst_pad_activate_pull (peer, active)) {
574         GST_LOCK (peer);
575         GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
576             "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
577         GST_UNLOCK (peer);
578         gst_object_unref (peer);
579         goto failure;
580       }
581     }
582   }
583
584   pre_activate_switch (pad, active);
585
586   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
587     if (GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)) {
588       goto success;
589     } else {
590       goto failure;
591     }
592   } else {
593     /* can happen for sinks of passthrough elements */
594     goto success;
595   }
596
597 was_ok:
598   {
599     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
600         active ? "activated" : "deactivated");
601     return TRUE;
602   }
603
604 success:
605   {
606     GST_LOCK (pad);
607     GST_PAD_ACTIVATE_MODE (pad) =
608         active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
609     GST_UNLOCK (pad);
610     post_activate_switch (pad, active);
611
612     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
613         active ? "activated" : "deactivated");
614     return TRUE;
615   }
616
617 failure:
618   {
619     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
620         active ? "activate" : "deactivate");
621     return FALSE;
622   }
623 }
624
625 /**
626  * gst_pad_activate_push:
627  * @pad: the #GstPad to activate or deactivate.
628  * @active: whether or not the pad should be active.
629  *
630  * Activates or deactivates the given pad in push mode via dispatching to the
631  * pad's activatepushfunc. For use from within pad activation functions only.
632  *
633  * If you don't know what this is, you probably don't want to call it.
634  *
635  * Returns: TRUE if the operation was successfull.
636  *
637  * MT safe.
638  */
639 gboolean
640 gst_pad_activate_push (GstPad * pad, gboolean active)
641 {
642   GstActivateMode old;
643
644   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
645   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
646       active ? "activated" : "deactivated");
647
648   GST_LOCK (pad);
649   old = GST_PAD_ACTIVATE_MODE (pad);
650   GST_UNLOCK (pad);
651
652   if ((active && old == GST_ACTIVATE_PUSH)
653       || (!active && old == GST_ACTIVATE_NONE))
654     goto was_ok;
655
656   if (active) {
657     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
658   } else {
659     g_return_val_if_fail (old == GST_ACTIVATE_PUSH, FALSE);
660   }
661
662   pre_activate_switch (pad, active);
663
664   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
665     if (GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active)) {
666       goto success;
667     } else {
668       goto failure;
669     }
670   } else {
671     /* quite ok, element relies on state change func to prepare itself */
672     goto success;
673   }
674
675 was_ok:
676   {
677     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
678         active ? "activated" : "deactivated");
679     return TRUE;
680   }
681
682 success:
683   {
684     GST_LOCK (pad);
685     GST_PAD_ACTIVATE_MODE (pad) =
686         active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
687     GST_UNLOCK (pad);
688     post_activate_switch (pad, active);
689
690     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
691         active ? "activated" : "deactivated");
692     return TRUE;
693   }
694
695 failure:
696   {
697     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
698         active ? "activate" : "deactivate");
699     return FALSE;
700   }
701 }
702
703 /**
704  * gst_pad_is_active:
705  * @pad: the #GstPad to query
706  *
707  * Query if a pad is active
708  *
709  * Returns: TRUE if the pad is active.
710  *
711  * MT safe.
712  */
713 gboolean
714 gst_pad_is_active (GstPad * pad)
715 {
716   gboolean result = FALSE;
717
718   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
719
720   GST_LOCK (pad);
721   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
722   GST_UNLOCK (pad);
723
724   return result;
725 }
726
727 /**
728  * gst_pad_set_blocked_async:
729  * @pad: the #GstPad to block or unblock
730  * @blocked: boolean indicating we should block or unblock
731  * @callback: #GstPadBlockCallback that will be called when the
732  *            operation succeeds.
733  * @user_data: user data passed to the callback
734  *
735  * Blocks or unblocks the dataflow on a pad. The provided callback
736  * is called when the operation succeeds. This can take a while as
737  * the pad can only become blocked when real dataflow is happening.
738  * When the pipeline is stalled, for example in PAUSED, this can
739  * take an indeterminate amount of time.
740  * You can pass NULL as the callback to make this call block. Be
741  * carefull with this blocking call as it might not return for
742  * reasons stated above.
743  *
744  * Returns: TRUE if the pad could be blocked. This function can fail
745  *   if wrong parameters were passed or the pad was already in the 
746  *   requested state.
747  *
748  * MT safe.
749  */
750 gboolean
751 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
752     GstPadBlockCallback callback, gpointer user_data)
753 {
754   gboolean was_blocked;
755
756   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
757
758   GST_LOCK (pad);
759
760   was_blocked = GST_PAD_IS_BLOCKED (pad);
761
762   if (G_UNLIKELY (was_blocked == blocked))
763     goto had_right_state;
764
765   if (blocked) {
766     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad %s:%s",
767         GST_DEBUG_PAD_NAME (pad));
768
769     GST_FLAG_SET (pad, GST_PAD_BLOCKED);
770     pad->block_callback = callback;
771     pad->block_data = user_data;
772     if (!callback) {
773       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
774       GST_PAD_BLOCK_WAIT (pad);
775       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
776     }
777   } else {
778     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad %s:%s",
779         GST_DEBUG_PAD_NAME (pad));
780
781     GST_FLAG_UNSET (pad, GST_PAD_BLOCKED);
782
783     pad->block_callback = callback;
784     pad->block_data = user_data;
785
786     if (callback) {
787       GST_PAD_BLOCK_SIGNAL (pad);
788     } else {
789       GST_PAD_BLOCK_SIGNAL (pad);
790       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
791       GST_PAD_BLOCK_WAIT (pad);
792       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
793     }
794   }
795   GST_UNLOCK (pad);
796
797   return TRUE;
798
799 had_right_state:
800   {
801     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
802         "pad %s:%s was in right state", GST_DEBUG_PAD_NAME (pad));
803     GST_UNLOCK (pad);
804     return FALSE;
805   }
806 }
807
808 /**
809  * gst_pad_set_blocked:
810  * @pad: the #GstPad to block or unblock
811  * @blocked: boolean indicating we should block or unblock
812  *
813  * Blocks or unblocks the dataflow on a pad. This function is
814  * a shortcut for @gst_pad_set_blocked_async() with a NULL
815  * callback.
816  *
817  * Returns: TRUE if the pad could be blocked. This function can fail
818  *   wrong parameters were passed or the pad was already in the 
819  *   requested state.
820  *
821  * MT safe.
822  */
823 gboolean
824 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
825 {
826   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
827 }
828
829 /**
830  * gst_pad_is_blocked:
831  * @pad: the #GstPad to query 
832  *
833  * Checks if the pad is blocked or not. This function returns the
834  * last requested state of the pad. It is not certain that the pad
835  * is actually blocked at this point.
836  *
837  * Returns: TRUE if the pad is blocked.
838  *
839  * MT safe.
840  */
841 gboolean
842 gst_pad_is_blocked (GstPad * pad)
843 {
844   gboolean result = FALSE;
845
846   g_return_val_if_fail (GST_IS_PAD (pad), result);
847
848   GST_LOCK (pad);
849   result = GST_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
850   GST_UNLOCK (pad);
851
852   return result;
853 }
854
855 /**
856  * gst_pad_set_activate_function:
857  * @pad: a sink #GstPad.
858  * @activate: the #GstPadActivateFunction to set.
859  *
860  * Sets the given activate function for the pad. The activate function will
861  * dispatch to activate_push or activate_pull to perform the actual activation.
862  * Only makes sense to set on sink pads.
863  *
864  * Call this function if your sink pad can start a pull-based task.
865  */
866 void
867 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
868 {
869   g_return_if_fail (GST_IS_PAD (pad));
870
871   GST_PAD_ACTIVATEFUNC (pad) = activate;
872   GST_CAT_DEBUG (GST_CAT_PADS, "activatefunc for %s:%s set to %s",
873       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activate));
874 }
875
876 /**
877  * gst_pad_set_activatepull_function:
878  * @pad: a sink #GstPad.
879  * @activatepull: the #GstPadActivateModeFunction to set.
880  *
881  * Sets the given activate_pull function for the pad. An activate_pull function
882  * prepares the element and any upstream connections for pulling. See XXX
883  * part-activation.txt for details.
884  */
885 void
886 gst_pad_set_activatepull_function (GstPad * pad,
887     GstPadActivateModeFunction activatepull)
888 {
889   g_return_if_fail (GST_IS_PAD (pad));
890
891   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
892   GST_CAT_DEBUG (GST_CAT_PADS, "activatepullfunc for %s:%s set to %s",
893       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepull));
894 }
895
896 /**
897  * gst_pad_set_activatepush_function:
898  * @pad: a sink #GstPad.
899  * @activatepush: the #GstPadActivateModeFunction to set.
900  *
901  * Sets the given activate_push function for the pad. An activate_push function
902  * prepares the element for pushing. See XXX part-activation.txt for details.
903  */
904 void
905 gst_pad_set_activatepush_function (GstPad * pad,
906     GstPadActivateModeFunction activatepush)
907 {
908   g_return_if_fail (GST_IS_PAD (pad));
909
910   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
911   GST_CAT_DEBUG (GST_CAT_PADS, "activatepushfunc for %s:%s set to %s",
912       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepush));
913 }
914
915 /**
916  * gst_pad_set_chain_function:
917  * @pad: a sink #GstPad.
918  * @chain: the #GstPadChainFunction to set.
919  *
920  * Sets the given chain function for the pad. The chain function is called to
921  * process a #GstBuffer input buffer.
922  */
923 void
924 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
925 {
926   g_return_if_fail (GST_IS_PAD (pad));
927   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
928
929   GST_PAD_CHAINFUNC (pad) = chain;
930   GST_CAT_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
931       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
932 }
933
934 /**
935  * gst_pad_set_getrange_function:
936  * @pad: a source #GstPad.
937  * @get: the #GstPadGetRangeFunction to set.
938  *
939  * Sets the given getrange function for the pad. The getrange function is called to
940  * produce a new #GstBuffer to start the processing pipeline. Getrange functions cannot
941  * return %NULL.
942  */
943 void
944 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
945 {
946   g_return_if_fail (GST_IS_PAD (pad));
947   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
948
949   GST_PAD_GETRANGEFUNC (pad) = get;
950
951   GST_CAT_DEBUG (GST_CAT_PADS, "getrangefunc for %s:%s  set to %s",
952       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
953 }
954
955 /**
956  * gst_pad_set_checkgetrange_function:
957  * @pad: a source #GstPad.
958  * @check: the #GstPadCheckGetRangeFunction to set.
959  *
960  * Sets the given checkgetrange function for the pad. Implement this function on
961  * a pad if you dynamically support getrange based scheduling on the pad.
962  */
963 void
964 gst_pad_set_checkgetrange_function (GstPad * pad,
965     GstPadCheckGetRangeFunction check)
966 {
967   g_return_if_fail (GST_IS_PAD (pad));
968   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
969
970   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
971
972   GST_CAT_DEBUG (GST_CAT_PADS, "checkgetrangefunc for %s:%s  set to %s",
973       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (check));
974 }
975
976 /**
977  * gst_pad_set_event_function:
978  * @pad: a source #GstPad.
979  * @event: the #GstPadEventFunction to set.
980  *
981  * Sets the given event handler for the pad.
982  */
983 void
984 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
985 {
986   g_return_if_fail (GST_IS_PAD (pad));
987
988   GST_PAD_EVENTFUNC (pad) = event;
989
990   GST_CAT_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
991       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
992 }
993
994 /**
995  * gst_pad_set_query_function:
996  * @pad: a #GstPad of either direction.
997  * @query: the #GstPadQueryFunction to set.
998  *
999  * Set the given query function for the pad.
1000  */
1001 void
1002 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1003 {
1004   g_return_if_fail (GST_IS_PAD (pad));
1005
1006   GST_PAD_QUERYFUNC (pad) = query;
1007
1008   GST_CAT_DEBUG (GST_CAT_PADS, "queryfunc for %s:%s  set to %s",
1009       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (query));
1010 }
1011
1012 /**
1013  * gst_pad_set_query_type_function:
1014  * @pad: a #GstPad of either direction.
1015  * @type_func: the #GstPadQueryTypeFunction to set.
1016  *
1017  * Set the given query type function for the pad.
1018  */
1019 void
1020 gst_pad_set_query_type_function (GstPad * pad,
1021     GstPadQueryTypeFunction type_func)
1022 {
1023   g_return_if_fail (GST_IS_PAD (pad));
1024
1025   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1026
1027   GST_CAT_DEBUG (GST_CAT_PADS, "querytypefunc for %s:%s  set to %s",
1028       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (type_func));
1029 }
1030
1031 /**
1032  * gst_pad_get_query_types:
1033  * @pad: a #GstPad.
1034  *
1035  * Get an array of supported queries that can be performed
1036  * on this pad.
1037  *
1038  * Returns: a zero-terminated array of #GstQueryType.
1039  */
1040 const GstQueryType *
1041 gst_pad_get_query_types (GstPad * pad)
1042 {
1043   GstPadQueryTypeFunction func;
1044
1045   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1046
1047   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1048     goto no_func;
1049
1050   return func (pad);
1051
1052 no_func:
1053   {
1054     return NULL;
1055   }
1056 }
1057
1058 static gboolean
1059 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1060 {
1061   *data = gst_pad_get_query_types (pad);
1062
1063   return TRUE;
1064 }
1065
1066 /**
1067  * gst_pad_get_query_types_default:
1068  * @pad: a #GstPad.
1069  *
1070  * Invoke the default dispatcher for the query types on
1071  * the pad.
1072  *
1073  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1074  * internally-linked pads has a query types function.
1075  */
1076 const GstQueryType *
1077 gst_pad_get_query_types_default (GstPad * pad)
1078 {
1079   GstQueryType *result = NULL;
1080
1081   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1082
1083   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1084       gst_pad_get_query_types_dispatcher, &result);
1085
1086   return result;
1087 }
1088
1089 /**
1090  * gst_pad_set_internal_link_function:
1091  * @pad: a #GstPad of either direction.
1092  * @intlink: the #GstPadIntLinkFunction to set.
1093  *
1094  * Sets the given internal link function for the pad.
1095  */
1096 void
1097 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1098 {
1099   g_return_if_fail (GST_IS_PAD (pad));
1100
1101   GST_PAD_INTLINKFUNC (pad) = intlink;
1102   GST_CAT_DEBUG (GST_CAT_PADS, "internal link for %s:%s  set to %s",
1103       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (intlink));
1104 }
1105
1106 /**
1107  * gst_pad_set_link_function:
1108  * @pad: a #GstPad.
1109  * @link: the #GstPadLinkFunction to set.
1110  * 
1111  * Sets the given link function for the pad. It will be called when the pad is
1112  * linked or relinked with caps. The caps passed to the link function is
1113  * the caps for the connnection. It can contain a non fixed caps.
1114  * 
1115  * The return value GST_PAD_LINK_OK should be used when the connection can be
1116  * made.
1117  * 
1118  * The return value GST_PAD_LINK_REFUSED should be used when the connection
1119  * cannot be made for some reason.
1120  */
1121 void
1122 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1123 {
1124   g_return_if_fail (GST_IS_PAD (pad));
1125
1126   GST_PAD_LINKFUNC (pad) = link;
1127   GST_CAT_DEBUG (GST_CAT_PADS, "linkfunc for %s:%s set to %s",
1128       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (link));
1129 }
1130
1131 /**
1132  * gst_pad_set_unlink_function:
1133  * @pad: a #GstPad.
1134  * @unlink: the #GstPadUnlinkFunction to set.
1135  *
1136  * Sets the given unlink function for the pad. It will be called
1137  * when the pad is unlinked.
1138  */
1139 void
1140 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1141 {
1142   g_return_if_fail (GST_IS_PAD (pad));
1143
1144   GST_PAD_UNLINKFUNC (pad) = unlink;
1145   GST_CAT_DEBUG (GST_CAT_PADS, "unlinkfunc for %s:%s set to %s",
1146       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (unlink));
1147 }
1148
1149 /**
1150  * gst_pad_set_getcaps_function:
1151  * @pad: a #GstPad.
1152  * @getcaps: the #GstPadGetCapsFunction to set.
1153  * 
1154  * Sets the given getcaps function for the pad. @getcaps should return the
1155  * allowable caps for a pad in the context of the element's state, its link to
1156  * other elements, and the devices or files it has opened. These caps must be a
1157  * subset of the pad template caps. In the NULL state with no links, @getcaps
1158  * should ideally return the same caps as the pad template. In rare
1159  * circumstances, an object property can affect the caps returned by @getcaps,
1160  * but this is discouraged.
1161  *
1162  * You do not need to call this function if @pad's allowed caps are always the
1163  * same as the pad template caps. This can only be true if the padtemplate 
1164  * has fixed simple caps.
1165  *
1166  * For most filters, the caps returned by @getcaps is directly affected by the
1167  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1168  * the srcpad's getcaps function is directly related to the stream data. Again,
1169  * @getcaps should return the most specific caps it reasonably can, since this
1170  * helps with autoplugging. 
1171  *
1172  * Note that the return value from @getcaps is owned by the caller, so the caller
1173  * should unref the caps after usage.
1174  */
1175 void
1176 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1177 {
1178   g_return_if_fail (GST_IS_PAD (pad));
1179
1180   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1181   GST_CAT_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
1182       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
1183 }
1184
1185 /**
1186  * gst_pad_set_acceptcaps_function:
1187  * @pad: a #GstPad.
1188  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1189  *
1190  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1191  * will be called to check if the pad can accept the given caps.
1192  */
1193 void
1194 gst_pad_set_acceptcaps_function (GstPad * pad,
1195     GstPadAcceptCapsFunction acceptcaps)
1196 {
1197   g_return_if_fail (GST_IS_PAD (pad));
1198
1199   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1200   GST_CAT_DEBUG (GST_CAT_PADS, "acceptcapsfunc for %s:%s set to %s",
1201       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1202 }
1203
1204 /**
1205  * gst_pad_set_fixatecaps_function:
1206  * @pad: a #GstPad.
1207  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1208  *
1209  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1210  * will be called whenever the default values for a GstCaps needs to be
1211  * filled in.
1212  */
1213 void
1214 gst_pad_set_fixatecaps_function (GstPad * pad,
1215     GstPadFixateCapsFunction fixatecaps)
1216 {
1217   g_return_if_fail (GST_IS_PAD (pad));
1218
1219   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1220   GST_CAT_DEBUG (GST_CAT_PADS, "fixatecapsfunc for %s:%s set to %s",
1221       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1222 }
1223
1224 /**
1225  * gst_pad_set_setcaps_function:
1226  * @pad: a #GstPad.
1227  * @setcaps: the #GstPadSetCapsFunction to set.
1228  *
1229  * Sets the given setcaps function for the pad.  The setcaps function
1230  * will be called whenever a buffer with a new media type is pushed or
1231  * pulled from the pad. The pad/element needs to update it's internal
1232  * structures to process the new media type. If this new type is not
1233  * acceptable, the setcaps function should return FALSE.
1234  */
1235 void
1236 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1237 {
1238   g_return_if_fail (GST_IS_PAD (pad));
1239
1240   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1241   GST_CAT_DEBUG (GST_CAT_PADS, "setcapsfunc for %s:%s set to %s",
1242       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (setcaps));
1243 }
1244
1245 /**
1246  * gst_pad_set_bufferalloc_function:
1247  * @pad: a sink #GstPad.
1248  * @bufalloc: the #GstPadBufferAllocFunction to set.
1249  *
1250  * Sets the given bufferalloc function for the pad. Note that the
1251  * bufferalloc function can only be set on sinkpads.
1252  */
1253 void
1254 gst_pad_set_bufferalloc_function (GstPad * pad,
1255     GstPadBufferAllocFunction bufalloc)
1256 {
1257   g_return_if_fail (GST_IS_PAD (pad));
1258   g_return_if_fail (GST_PAD_IS_SINK (pad));
1259
1260   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1261   GST_CAT_DEBUG (GST_CAT_PADS, "bufferallocfunc for %s:%s set to %s",
1262       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufalloc));
1263 }
1264
1265 /**
1266  * gst_pad_unlink:
1267  * @srcpad: the source #GstPad to unlink.
1268  * @sinkpad: the sink #GstPad to unlink.
1269  *
1270  * Unlinks the source pad from the sink pad. Will emit the "unlinked" signal on
1271  * both pads.
1272  *
1273  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1274  * the pads were not linked together.
1275  *
1276  * MT safe.
1277  */
1278 gboolean
1279 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1280 {
1281   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1282   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1283
1284   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1285       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1286       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1287
1288   GST_LOCK (srcpad);
1289
1290   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1291     goto not_srcpad;
1292
1293   GST_LOCK (sinkpad);
1294
1295   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1296     goto not_sinkpad;
1297
1298   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1299     goto not_linked_together;
1300
1301   if (GST_PAD_UNLINKFUNC (srcpad)) {
1302     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1303   }
1304   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1305     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1306   }
1307
1308   /* first clear peers */
1309   GST_PAD_PEER (srcpad) = NULL;
1310   GST_PAD_PEER (sinkpad) = NULL;
1311
1312   GST_UNLOCK (sinkpad);
1313   GST_UNLOCK (srcpad);
1314
1315   /* fire off a signal to each of the pads telling them 
1316    * that they've been unlinked */
1317   g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1318   g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1319
1320   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1321       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1322
1323   return TRUE;
1324
1325 not_srcpad:
1326   {
1327     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1328     GST_UNLOCK (srcpad);
1329     return FALSE;
1330   }
1331 not_sinkpad:
1332   {
1333     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1334     GST_UNLOCK (sinkpad);
1335     GST_UNLOCK (srcpad);
1336     return FALSE;
1337   }
1338 not_linked_together:
1339   {
1340     /* we do not emit a warning in this case because unlinking cannot
1341      * be made MT safe.*/
1342     GST_UNLOCK (sinkpad);
1343     GST_UNLOCK (srcpad);
1344     return FALSE;
1345   }
1346 }
1347
1348 /**
1349  * gst_pad_is_linked:
1350  * @pad: pad to check
1351  *
1352  * Checks if a @pad is linked to another pad or not.
1353  *
1354  * Returns: TRUE if the pad is linked, FALSE otherwise.
1355  *
1356  * MT safe.
1357  */
1358 gboolean
1359 gst_pad_is_linked (GstPad * pad)
1360 {
1361   gboolean result;
1362
1363   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1364
1365   GST_LOCK (pad);
1366   result = (GST_PAD_PEER (pad) != NULL);
1367   GST_UNLOCK (pad);
1368
1369   return result;
1370 }
1371
1372 /* get the caps from both pads and see if the intersection
1373  * is not empty.
1374  *
1375  * This function should be called with the pad LOCK on both
1376  * pads
1377  */
1378 static gboolean
1379 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1380 {
1381   GstCaps *srccaps;
1382   GstCaps *sinkcaps;
1383
1384   srccaps = gst_pad_get_caps_unlocked (src);
1385   sinkcaps = gst_pad_get_caps_unlocked (sink);
1386   GST_CAT_DEBUG (GST_CAT_CAPS, " src caps %" GST_PTR_FORMAT, srccaps);
1387   GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1388
1389   /* if we have caps on both pads we can check the intersection */
1390   if (srccaps && sinkcaps) {
1391     GstCaps *icaps;
1392
1393     icaps = gst_caps_intersect (srccaps, sinkcaps);
1394     gst_caps_unref (srccaps);
1395     gst_caps_unref (sinkcaps);
1396
1397     GST_CAT_DEBUG (GST_CAT_CAPS,
1398         "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1399
1400     if (!icaps || gst_caps_is_empty (icaps)) {
1401       GST_CAT_DEBUG (GST_CAT_CAPS, "intersection is empty");
1402       gst_caps_unref (icaps);
1403       return FALSE;
1404     }
1405     gst_caps_unref (icaps);
1406   }
1407
1408   return TRUE;
1409 }
1410
1411 /* check if the grandparents of both pads are the same.
1412  * This check is required so that we don't try to link
1413  * pads from elements in different bins without ghostpads.
1414  *
1415  * The LOCK should be helt on both pads
1416  */
1417 static gboolean
1418 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1419 {
1420   GstObject *psrc, *psink;
1421   gboolean res = TRUE;
1422
1423   psrc = GST_OBJECT_PARENT (src);
1424   psink = GST_OBJECT_PARENT (sink);
1425
1426   /* if one of the pads has no parent, we allow the link */
1427   if (psrc && psink) {
1428     /* if the parents are the same, we have a loop */
1429     if (psrc == psink) {
1430       GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1431           psrc);
1432       res = FALSE;
1433       goto done;
1434     }
1435     /* if they both have a parent, we check the grandparents */
1436     psrc = gst_object_get_parent (psrc);
1437     psink = gst_object_get_parent (psink);
1438
1439     if (psrc != psink) {
1440       /* if they have grandparents but they are not the same */
1441       GST_CAT_DEBUG (GST_CAT_CAPS,
1442           "pads have different grandparents %" GST_PTR_FORMAT " and %"
1443           GST_PTR_FORMAT, psrc, psink);
1444       res = FALSE;
1445     }
1446     if (psrc)
1447       gst_object_unref (psrc);
1448     if (psink)
1449       gst_object_unref (psink);
1450   }
1451 done:
1452   return res;
1453 }
1454
1455 /* FIXME leftover from an attempt at refactoring... */
1456 /* call with the two pads unlocked */
1457 static GstPadLinkReturn
1458 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1459 {
1460   /* generic checks */
1461   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1462   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1463
1464   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1465       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1466
1467   GST_LOCK (srcpad);
1468
1469   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1470     goto not_srcpad;
1471
1472   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1473     goto src_was_linked;
1474
1475   GST_LOCK (sinkpad);
1476
1477   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1478     goto not_sinkpad;
1479
1480   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1481     goto sink_was_linked;
1482
1483   /* check hierarchy, pads can only be linked if the grandparents
1484    * are the same. */
1485   if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1486     goto wrong_hierarchy;
1487
1488   /* check pad caps for non-empty intersection */
1489   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1490     goto no_format;
1491
1492   /* FIXME check pad scheduling for non-empty intersection */
1493
1494   return GST_PAD_LINK_OK;
1495
1496 not_srcpad:
1497   {
1498     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1499     GST_UNLOCK (srcpad);
1500     return GST_PAD_LINK_WRONG_DIRECTION;
1501   }
1502 src_was_linked:
1503   {
1504     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was linked",
1505         GST_DEBUG_PAD_NAME (srcpad));
1506     /* we do not emit a warning in this case because unlinking cannot
1507      * be made MT safe.*/
1508     GST_UNLOCK (srcpad);
1509     return GST_PAD_LINK_WAS_LINKED;
1510   }
1511 not_sinkpad:
1512   {
1513     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1514     GST_UNLOCK (sinkpad);
1515     GST_UNLOCK (srcpad);
1516     return GST_PAD_LINK_WRONG_DIRECTION;
1517   }
1518 sink_was_linked:
1519   {
1520     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was linked",
1521         GST_DEBUG_PAD_NAME (sinkpad));
1522     /* we do not emit a warning in this case because unlinking cannot
1523      * be made MT safe.*/
1524     GST_UNLOCK (sinkpad);
1525     GST_UNLOCK (srcpad);
1526     return GST_PAD_LINK_WAS_LINKED;
1527   }
1528 wrong_hierarchy:
1529   {
1530     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1531     GST_UNLOCK (sinkpad);
1532     GST_UNLOCK (srcpad);
1533     return GST_PAD_LINK_WRONG_HIERARCHY;
1534   }
1535 no_format:
1536   {
1537     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1538     GST_UNLOCK (sinkpad);
1539     GST_UNLOCK (srcpad);
1540     return GST_PAD_LINK_NOFORMAT;
1541   }
1542 }
1543
1544 /**
1545  * gst_pad_link:
1546  * @srcpad: the source #GstPad to link.
1547  * @sinkpad: the sink #GstPad to link.
1548  *
1549  * Links the source pad and the sink pad.
1550  *
1551  * Returns: A result code indicating if the connection worked or
1552  *          what went wrong.
1553  *
1554  * MT Safe.
1555  */
1556 GstPadLinkReturn
1557 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1558 {
1559   GstPadLinkReturn result;
1560
1561   /* prepare will also lock the two pads */
1562   result = gst_pad_link_prepare (srcpad, sinkpad);
1563
1564   if (result != GST_PAD_LINK_OK)
1565     goto prepare_failed;
1566
1567   GST_UNLOCK (sinkpad);
1568   GST_UNLOCK (srcpad);
1569
1570   /* FIXME released the locks here, concurrent thread might link
1571    * something else. */
1572   if (GST_PAD_LINKFUNC (srcpad)) {
1573     /* this one will call the peer link function */
1574     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
1575   } else if (GST_PAD_LINKFUNC (sinkpad)) {
1576     /* if no source link function, we need to call the sink link
1577      * function ourselves. */
1578     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
1579   } else {
1580     result = GST_PAD_LINK_OK;
1581   }
1582
1583   GST_LOCK (srcpad);
1584   GST_LOCK (sinkpad);
1585
1586   if (result == GST_PAD_LINK_OK) {
1587     GST_PAD_PEER (srcpad) = sinkpad;
1588     GST_PAD_PEER (sinkpad) = srcpad;
1589
1590     GST_UNLOCK (sinkpad);
1591     GST_UNLOCK (srcpad);
1592
1593     /* fire off a signal to each of the pads telling them
1594      * that they've been linked */
1595     g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
1596     g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
1597
1598     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1599         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1600   } else {
1601     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1602         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1603
1604     GST_UNLOCK (sinkpad);
1605     GST_UNLOCK (srcpad);
1606   }
1607   return result;
1608
1609 prepare_failed:
1610   {
1611     return result;
1612   }
1613 }
1614
1615 static void
1616 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1617 {
1618   /* this function would need checks if it weren't static */
1619
1620   GST_LOCK (pad);
1621   gst_object_replace ((GstObject **) & pad->padtemplate, (GstObject *) templ);
1622   GST_UNLOCK (pad);
1623
1624   if (templ) {
1625     gst_object_sink (GST_OBJECT (templ));
1626     g_signal_emit (G_OBJECT (templ),
1627         gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
1628   }
1629 }
1630
1631 /**
1632  * gst_pad_get_pad_template:
1633  * @pad: a #GstPad.
1634  *
1635  * Gets the template for @pad.
1636  *
1637  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
1638  * if this pad has no template.
1639  *
1640  * FIXME: currently returns an unrefcounted padtemplate.
1641  */
1642 GstPadTemplate *
1643 gst_pad_get_pad_template (GstPad * pad)
1644 {
1645   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1646
1647   return GST_PAD_PAD_TEMPLATE (pad);
1648 }
1649
1650
1651 /* should be called with the pad LOCK held */
1652 /* refs the caps, so caller is responsible for getting it unreffed */
1653 static GstCaps *
1654 gst_pad_get_caps_unlocked (GstPad * pad)
1655 {
1656   GstCaps *result = NULL;
1657
1658   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1659       GST_DEBUG_PAD_NAME (pad), pad);
1660
1661   if (GST_PAD_GETCAPSFUNC (pad)) {
1662     GST_CAT_DEBUG (GST_CAT_CAPS, "dispatching to pad getcaps function");
1663
1664     GST_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
1665     GST_UNLOCK (pad);
1666     result = GST_PAD_GETCAPSFUNC (pad) (pad);
1667     GST_LOCK (pad);
1668     GST_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
1669
1670     if (result == NULL) {
1671       g_critical ("pad %s:%s returned NULL caps from getcaps function",
1672           GST_DEBUG_PAD_NAME (pad));
1673     } else {
1674       GST_CAT_DEBUG (GST_CAT_CAPS,
1675           "pad getcaps %s:%s returned %" GST_PTR_FORMAT,
1676           GST_DEBUG_PAD_NAME (pad), result);
1677 #ifndef G_DISABLE_ASSERT
1678       /* check that the returned caps are a real subset of the template caps */
1679       if (GST_PAD_PAD_TEMPLATE (pad)) {
1680         const GstCaps *templ_caps =
1681             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1682         if (!gst_caps_is_subset (result, templ_caps)) {
1683           GstCaps *temp;
1684
1685           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
1686               "pad returned caps %" GST_PTR_FORMAT
1687               " which are not a real subset of its template caps %"
1688               GST_PTR_FORMAT, result, templ_caps);
1689           g_warning
1690               ("pad %s:%s returned caps that are not a real subset of its template caps",
1691               GST_DEBUG_PAD_NAME (pad));
1692           temp = gst_caps_intersect (templ_caps, result);
1693           gst_caps_unref (result);
1694           result = temp;
1695         }
1696       }
1697 #endif
1698       goto done;
1699     }
1700   }
1701   if (GST_PAD_PAD_TEMPLATE (pad)) {
1702     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1703
1704     result = GST_PAD_TEMPLATE_CAPS (templ);
1705     GST_CAT_DEBUG (GST_CAT_CAPS,
1706         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1707         result);
1708
1709     result = gst_caps_ref (result);
1710     goto done;
1711   }
1712   if (GST_PAD_CAPS (pad)) {
1713     result = GST_PAD_CAPS (pad);
1714
1715     GST_CAT_DEBUG (GST_CAT_CAPS,
1716         "using pad caps %p %" GST_PTR_FORMAT, result, result);
1717
1718     result = gst_caps_ref (result);
1719     goto done;
1720   }
1721
1722   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1723   result = gst_caps_new_empty ();
1724
1725 done:
1726   return result;
1727 }
1728
1729 /**
1730  * gst_pad_get_caps:
1731  * @pad: a  #GstPad to get the capabilities of.
1732  *
1733  * Gets the capabilities this pad can produce or consume.
1734  * Note that this method doesn't necessarily returns the caps set by
1735  * #gst_pad_set_caps - use #GST_PAD_CAPS for that instead.
1736  * gst_pad_get_caps returns all possible caps a pad can operate with, using
1737  * the pad's get_caps function;
1738  * this returns the pad template caps if not explicitly set.
1739  *
1740  * Returns: a newly allocated copy of the #GstCaps of this pad.
1741  *
1742  * MT safe.
1743  */
1744 GstCaps *
1745 gst_pad_get_caps (GstPad * pad)
1746 {
1747   GstCaps *result = NULL;
1748
1749   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1750
1751   GST_LOCK (pad);
1752
1753   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1754       GST_DEBUG_PAD_NAME (pad), pad);
1755
1756   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (pad)))
1757     goto was_dispatching;
1758
1759   result = gst_pad_get_caps_unlocked (pad);
1760   GST_UNLOCK (pad);
1761
1762   return result;
1763
1764 was_dispatching:
1765   {
1766     GST_CAT_DEBUG (GST_CAT_CAPS,
1767         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1768     g_warning ("pad %s:%s recursively called getcaps!",
1769         GST_DEBUG_PAD_NAME (pad));
1770     GST_UNLOCK (pad);
1771     return NULL;
1772   }
1773 }
1774
1775 /**
1776  * gst_pad_peer_get_caps:
1777  * @pad: a  #GstPad to get the peer capabilities of.
1778  *
1779  * Gets the capabilities of the peer connected to this pad.
1780  *
1781  * Returns: the #GstCaps of the peer pad. This function returns a new caps, so use 
1782  * gst_caps_unref to get rid of it. this function returns NULL if there is no
1783  * peer pad or when this function is called recursively from a getcaps function.
1784  */
1785 GstCaps *
1786 gst_pad_peer_get_caps (GstPad * pad)
1787 {
1788   GstPad *peerpad;
1789   GstCaps *result = NULL;
1790
1791   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1792
1793   GST_LOCK (pad);
1794
1795   GST_CAT_DEBUG (GST_CAT_CAPS, "get peer caps of %s:%s (%p)",
1796       GST_DEBUG_PAD_NAME (pad), pad);
1797
1798   peerpad = GST_PAD_PEER (pad);
1799   if (G_UNLIKELY (peerpad == NULL))
1800     goto no_peer;
1801
1802   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (peerpad)))
1803     goto was_dispatching;
1804
1805   gst_object_ref (peerpad);
1806   GST_UNLOCK (pad);
1807
1808   result = gst_pad_get_caps (peerpad);
1809
1810   gst_object_unref (peerpad);
1811
1812   return result;
1813
1814 no_peer:
1815   {
1816     GST_UNLOCK (pad);
1817     return NULL;
1818   }
1819 was_dispatching:
1820   {
1821     GST_CAT_DEBUG (GST_CAT_CAPS,
1822         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1823     g_warning ("pad %s:%s recursively called getcaps!",
1824         GST_DEBUG_PAD_NAME (pad));
1825     GST_UNLOCK (pad);
1826     return NULL;
1827   }
1828 }
1829
1830 static gboolean
1831 fixate_value (GValue * dest, const GValue * src)
1832 {
1833   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
1834     g_value_init (dest, G_TYPE_INT);
1835     g_value_set_int (dest, gst_value_get_int_range_min (src));
1836   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
1837     g_value_init (dest, G_TYPE_DOUBLE);
1838     g_value_set_double (dest, gst_value_get_double_range_min (src));
1839   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1840     GValue temp = { 0 };
1841
1842     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
1843     if (!fixate_value (dest, &temp))
1844       gst_value_init_and_copy (dest, &temp);
1845     g_value_unset (&temp);
1846   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
1847     gboolean res = FALSE;
1848     gint n;
1849
1850     g_value_init (dest, GST_TYPE_ARRAY);
1851     for (n = 0; n < gst_value_list_get_size (src); n++) {
1852       GValue kid = { 0 };
1853       const GValue *orig_kid = gst_value_list_get_value (src, n);
1854
1855       if (!fixate_value (&kid, orig_kid))
1856         gst_value_init_and_copy (&kid, orig_kid);
1857       else
1858         res = TRUE;
1859       gst_value_list_append_value (dest, &kid);
1860       g_value_unset (&kid);
1861     }
1862
1863     if (!res)
1864       g_value_unset (dest);
1865
1866     return res;
1867   } else {
1868     return FALSE;
1869   }
1870
1871   return TRUE;
1872 }
1873
1874 static gboolean
1875 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
1876 {
1877   GstStructure *s = data;
1878   GValue v = { 0 };
1879
1880   if (fixate_value (&v, value)) {
1881     gst_structure_id_set_value (s, field_id, &v);
1882     g_value_unset (&v);
1883   }
1884
1885   return TRUE;
1886 }
1887
1888 /**
1889  * gst_pad_fixate_caps:
1890  * @pad: a  #GstPad to fixate
1891  * @caps: the  #GstCaps to fixate
1892  *
1893  * Fixate a caps on the given pad. Modifies the caps in place, so you should
1894  * make sure that the caps are actually writable (see gst_caps_make_writable()).
1895  */
1896 void
1897 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
1898 {
1899   GstPadFixateCapsFunction fixatefunc;
1900   gint n;
1901
1902   g_return_if_fail (GST_IS_PAD (pad));
1903   g_return_if_fail (caps != NULL);
1904
1905   if (gst_caps_is_fixed (caps))
1906     return;
1907
1908   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
1909   if (fixatefunc) {
1910     fixatefunc (pad, caps);
1911   }
1912
1913   /* default fixation */
1914   for (n = 0; n < gst_caps_get_size (caps); n++) {
1915     GstStructure *s = gst_caps_get_structure (caps, n);
1916
1917     gst_structure_foreach (s, gst_pad_default_fixate, s);
1918   }
1919 }
1920
1921 /**
1922  * gst_pad_accept_caps:
1923  * @pad: a  #GstPad to check
1924  *
1925  * Check if the given pad accepts the caps.
1926  *
1927  * Returns: TRUE if the pad can accept the caps.
1928  */
1929 gboolean
1930 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
1931 {
1932   gboolean result;
1933   GstPadAcceptCapsFunction acceptfunc;
1934
1935   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1936
1937   /* any pad can be unnegotiated */
1938   if (caps == NULL)
1939     return TRUE;
1940
1941   GST_LOCK (pad);
1942   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
1943
1944   GST_CAT_DEBUG (GST_CAT_CAPS, "pad accept caps of %s:%s (%p)",
1945       GST_DEBUG_PAD_NAME (pad), pad);
1946   GST_UNLOCK (pad);
1947
1948   if (acceptfunc) {
1949     /* we can call the function */
1950     result = acceptfunc (pad, caps);
1951   } else {
1952     /* else see get the caps and see if it intersects to something
1953      * not empty */
1954     GstCaps *intersect;
1955     GstCaps *allowed;
1956
1957     allowed = gst_pad_get_caps (pad);
1958     if (allowed) {
1959       intersect = gst_caps_intersect (allowed, caps);
1960
1961       result = !gst_caps_is_empty (intersect);
1962
1963       gst_caps_unref (allowed);
1964       gst_caps_unref (intersect);
1965     } else {
1966       result = FALSE;
1967     }
1968   }
1969   return result;
1970 }
1971
1972 /**
1973  * gst_pad_peer_accept_caps:
1974  * @pad: a  #GstPad to check
1975  *
1976  * Check if the given pad accepts the caps.
1977  *
1978  * Returns: TRUE if the pad can accept the caps.
1979  */
1980 gboolean
1981 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
1982 {
1983   GstPad *peerpad;
1984   gboolean result;
1985
1986   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1987
1988   GST_LOCK (pad);
1989
1990   GST_CAT_DEBUG (GST_CAT_CAPS, "peer accept caps of %s:%s (%p)",
1991       GST_DEBUG_PAD_NAME (pad), pad);
1992
1993   peerpad = GST_PAD_PEER (pad);
1994   if (G_UNLIKELY (peerpad == NULL))
1995     goto no_peer;
1996
1997   result = gst_pad_accept_caps (peerpad, caps);
1998   GST_UNLOCK (pad);
1999
2000   return result;
2001
2002 no_peer:
2003   {
2004     GST_UNLOCK (pad);
2005     return TRUE;
2006   }
2007 }
2008
2009 /**
2010  * gst_pad_set_caps:
2011  * @pad: a  #GstPad to set the capabilities of.
2012  * @caps: a #GstCaps to set.
2013  *
2014  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2015  * caps on the pad will be unreffed. This function refs the caps so you should
2016  * unref if as soon as you don't need it anymore.
2017  * It is possible to set NULL caps, which will make the pad unnegotiated
2018  * again.
2019  *
2020  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2021  * or bad parameters were provided to this function.
2022  *
2023  * MT safe.
2024  */
2025 gboolean
2026 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2027 {
2028   GstPadSetCapsFunction setcaps;
2029   GstCaps *existing;
2030
2031   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2032   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2033
2034   GST_LOCK (pad);
2035   setcaps = GST_PAD_SETCAPSFUNC (pad);
2036
2037   existing = GST_PAD_CAPS (pad);
2038   if (caps == existing)
2039     goto setting_same_caps;
2040   else if (caps && existing && gst_caps_is_equal (caps, existing))
2041     goto setting_same_caps;
2042
2043   /* call setcaps function to configure the pad */
2044   if (setcaps != NULL && caps) {
2045     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2046       GST_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2047       GST_UNLOCK (pad);
2048       if (!setcaps (pad, caps))
2049         goto could_not_set;
2050       GST_LOCK (pad);
2051       GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2052     } else {
2053       GST_CAT_DEBUG (GST_CAT_CAPS, "pad %s:%s was dispatching",
2054           GST_DEBUG_PAD_NAME (pad));
2055     }
2056   }
2057
2058   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2059   GST_CAT_DEBUG (GST_CAT_CAPS, "%s:%s caps %" GST_PTR_FORMAT,
2060       GST_DEBUG_PAD_NAME (pad), caps);
2061   GST_UNLOCK (pad);
2062
2063   g_object_notify (G_OBJECT (pad), "caps");
2064
2065   return TRUE;
2066
2067 setting_same_caps:
2068   {
2069     GST_UNLOCK (pad);
2070     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2071     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2072         "caps %" GST_PTR_FORMAT " same as existing, updating ptr only", caps);
2073     return TRUE;
2074   }
2075 /* errors */
2076 could_not_set:
2077   {
2078     GST_LOCK (pad);
2079     GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2080     GST_CAT_DEBUG (GST_CAT_CAPS,
2081         "pad %s:%s, caps %" GST_PTR_FORMAT " could not be set",
2082         GST_DEBUG_PAD_NAME (pad), caps);
2083     GST_UNLOCK (pad);
2084
2085     return FALSE;
2086   }
2087 }
2088
2089 static gboolean
2090 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2091 {
2092   GstPadAcceptCapsFunction acceptcaps;
2093   GstPadSetCapsFunction setcaps;
2094   gboolean res;
2095
2096   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2097   setcaps = GST_PAD_SETCAPSFUNC (pad);
2098
2099   /* See if pad accepts the caps, by calling acceptcaps, only
2100    * needed if no setcaps function */
2101   if (setcaps == NULL && acceptcaps != NULL) {
2102     if (!acceptcaps (pad, caps))
2103       goto not_accepted;
2104   }
2105   /* set caps on pad if call succeeds */
2106   res = gst_pad_set_caps (pad, caps);
2107   /* no need to unref the caps here, set_caps takes a ref and
2108    * our ref goes away when we leave this function. */
2109
2110   return res;
2111
2112 not_accepted:
2113   {
2114     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2115     return FALSE;
2116   }
2117 }
2118
2119 /* returns TRUE if the src pad could be configured to accept the given caps */
2120 static gboolean
2121 gst_pad_configure_src (GstPad * pad, GstCaps * caps)
2122 {
2123   GstPadAcceptCapsFunction acceptcaps;
2124   GstPadSetCapsFunction setcaps;
2125   gboolean res;
2126
2127   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2128   setcaps = GST_PAD_SETCAPSFUNC (pad);
2129
2130   /* See if pad accepts the caps, by calling acceptcaps, only
2131    * needed if no setcaps function */
2132   if (setcaps == NULL && acceptcaps != NULL) {
2133     if (!acceptcaps (pad, caps))
2134       goto not_accepted;
2135   }
2136   /* set caps on pad if call succeeds */
2137   res = gst_pad_set_caps (pad, caps);
2138   /* no need to unref the caps here, set_caps takes a ref and
2139    * our ref goes away when we leave this function. */
2140
2141   return res;
2142
2143 not_accepted:
2144   {
2145     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2146     return FALSE;
2147   }
2148 }
2149
2150 /**
2151  * gst_pad_get_pad_template_caps:
2152  * @pad: a #GstPad to get the template capabilities from.
2153  *
2154  * Gets the capabilities for @pad's template.
2155  *
2156  * Returns: the #GstCaps of this pad template. If you intend to keep a reference
2157  * on the caps, make a copy (see gst_caps_copy ()).
2158  */
2159 const GstCaps *
2160 gst_pad_get_pad_template_caps (GstPad * pad)
2161 {
2162   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2163
2164   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2165
2166   if (GST_PAD_PAD_TEMPLATE (pad))
2167     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2168
2169   return gst_static_caps_get (&anycaps);
2170 }
2171
2172
2173 /**
2174  * gst_pad_get_peer:
2175  * @pad: a #GstPad to get the peer of.
2176  *
2177  * Gets the peer of @pad. This function refs the peer pad so
2178  * you need to unref it after use.
2179  *
2180  * Returns: the peer #GstPad. Unref after usage.
2181  *
2182  * MT safe.
2183  */
2184 GstPad *
2185 gst_pad_get_peer (GstPad * pad)
2186 {
2187   GstPad *result;
2188
2189   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2190
2191   GST_LOCK (pad);
2192   result = GST_PAD_PEER (pad);
2193   if (result)
2194     gst_object_ref (result);
2195   GST_UNLOCK (pad);
2196
2197   return result;
2198 }
2199
2200 /**
2201  * gst_pad_get_allowed_caps:
2202  * @srcpad: a #GstPad, it must a a source pad.
2203  *
2204  * Gets the capabilities of the allowed media types that can flow through
2205  * @srcpad and its peer. The pad must be a source pad.
2206  * The caller must free the resulting caps.
2207  *
2208  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2209  * you no longer need it. This function returns NULL when the @srcpad has no
2210  * peer.
2211  *
2212  * MT safe.
2213  */
2214 GstCaps *
2215 gst_pad_get_allowed_caps (GstPad * srcpad)
2216 {
2217   GstCaps *mycaps;
2218   GstCaps *caps;
2219   GstCaps *peercaps;
2220   GstPad *peer;
2221
2222   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
2223   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
2224
2225   GST_LOCK (srcpad);
2226
2227   peer = GST_PAD_PEER (srcpad);
2228   if (G_UNLIKELY (peer == NULL))
2229     goto no_peer;
2230
2231   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting allowed caps",
2232       GST_DEBUG_PAD_NAME (srcpad));
2233
2234   gst_object_ref (peer);
2235   GST_UNLOCK (srcpad);
2236   mycaps = gst_pad_get_caps (srcpad);
2237
2238   peercaps = gst_pad_get_caps (peer);
2239   gst_object_unref (peer);
2240
2241   caps = gst_caps_intersect (mycaps, peercaps);
2242   gst_caps_unref (peercaps);
2243   gst_caps_unref (mycaps);
2244
2245   GST_CAT_DEBUG (GST_CAT_CAPS, "allowed caps %" GST_PTR_FORMAT, caps);
2246
2247   return caps;
2248
2249 no_peer:
2250   {
2251     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2252         GST_DEBUG_PAD_NAME (srcpad));
2253     GST_UNLOCK (srcpad);
2254
2255     return NULL;
2256   }
2257 }
2258
2259 /**
2260  * gst_pad_get_negotiated_caps:
2261  * @pad: a #GstPad.
2262  *
2263  * Gets the capabilities of the media type that currently flows through @pad
2264  * and its peer.
2265  *
2266  * This function can be used on both src and sinkpads. Note that srcpads are
2267  * always negotiated before sinkpads so it is possible that the negotiated caps
2268  * on the srcpad do not match the negotiated caps of the peer.
2269  *
2270  * Returns: the negotiated #GstCaps of the pad link.  Free the caps when
2271  * you no longer need it. This function returns NULL when the @pad has no
2272  * peer or is not negotiated yet.
2273  *
2274  * MT safe.
2275  */
2276 GstCaps *
2277 gst_pad_get_negotiated_caps (GstPad * pad)
2278 {
2279   GstCaps *caps;
2280   GstPad *peer;
2281
2282   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2283
2284   GST_LOCK (pad);
2285
2286   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2287     goto no_peer;
2288
2289   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting negotiated caps",
2290       GST_DEBUG_PAD_NAME (pad));
2291
2292   caps = GST_PAD_CAPS (pad);
2293   if (caps)
2294     gst_caps_ref (caps);
2295   GST_UNLOCK (pad);
2296
2297   GST_CAT_DEBUG (GST_CAT_CAPS, "negotiated caps %" GST_PTR_FORMAT, caps);
2298
2299   return caps;
2300
2301 no_peer:
2302   {
2303     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2304         GST_DEBUG_PAD_NAME (pad));
2305     GST_UNLOCK (pad);
2306
2307     return NULL;
2308   }
2309 }
2310
2311 /**
2312  * gst_pad_alloc_buffer:
2313  * @pad: a source #GstPad
2314  * @offset: the offset of the new buffer in the stream
2315  * @size: the size of the new buffer
2316  * @caps: the caps of the new buffer
2317  * @buf: a newly allocated buffer
2318  *
2319  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2320  * function only works if @pad is a source pad and has a peer.
2321  *
2322  * You need to check the caps of the buffer after performing this
2323  * function and renegotiate to the format if needed.
2324  *
2325  * A new, empty #GstBuffer will be put in the @buf argument.
2326  *
2327  * Returns: a result code indicating success of the operation. Any
2328  * result code other than GST_FLOW_OK is an error and @buf should
2329  * not be used.
2330  * An error can occur if the pad is not connected or when the downstream
2331  * peer elements cannot provide an acceptable buffer.
2332  *
2333  * MT safe.
2334  */
2335 GstFlowReturn
2336 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
2337     GstBuffer ** buf)
2338 {
2339   GstPad *peer;
2340   GstFlowReturn ret;
2341   GstPadBufferAllocFunction bufferallocfunc;
2342   gboolean caps_changed;
2343
2344   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2345   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2346   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2347
2348   GST_LOCK (pad);
2349   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2350     goto no_peer;
2351
2352   gst_object_ref (peer);
2353   GST_UNLOCK (pad);
2354
2355   if (G_LIKELY ((bufferallocfunc = peer->bufferallocfunc) == NULL))
2356     goto fallback;
2357
2358   GST_LOCK (peer);
2359   /* when the peer is flushing we cannot give a buffer */
2360   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (peer)))
2361     goto flushing;
2362
2363   GST_CAT_DEBUG (GST_CAT_PADS,
2364       "calling bufferallocfunc &%s (@%p) of peer pad %s:%s for size %d",
2365       GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2366       &bufferallocfunc, GST_DEBUG_PAD_NAME (peer), size);
2367   GST_UNLOCK (peer);
2368
2369   ret = bufferallocfunc (peer, offset, size, caps, buf);
2370
2371   if (G_UNLIKELY (ret != GST_FLOW_OK))
2372     goto peer_error;
2373   if (G_UNLIKELY (*buf == NULL))
2374     goto fallback;
2375
2376 do_caps:
2377   gst_object_unref (peer);
2378
2379   /* FIXME, move capnego this into a base class? */
2380   caps = GST_BUFFER_CAPS (*buf);
2381   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2382   /* we got a new datatype on the pad, see if it can handle it */
2383   if (G_UNLIKELY (caps_changed)) {
2384     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
2385     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps)))
2386       goto not_negotiated;
2387   }
2388   return ret;
2389
2390 no_peer:
2391   {
2392     /* pad has no peer */
2393     GST_CAT_DEBUG (GST_CAT_PADS,
2394         "%s:%s called bufferallocfunc but had no peer",
2395         GST_DEBUG_PAD_NAME (pad));
2396     GST_UNLOCK (pad);
2397     return GST_FLOW_NOT_LINKED;
2398   }
2399 flushing:
2400   {
2401     /* peer was flushing */
2402     GST_UNLOCK (peer);
2403     gst_object_unref (peer);
2404     GST_CAT_DEBUG (GST_CAT_PADS,
2405         "%s:%s called bufferallocfunc but peer was flushing",
2406         GST_DEBUG_PAD_NAME (pad));
2407     return GST_FLOW_WRONG_STATE;
2408   }
2409   /* fallback case, allocate a buffer of our own, add pad caps. */
2410 fallback:
2411   {
2412     GST_CAT_DEBUG (GST_CAT_PADS,
2413         "%s:%s fallback buffer alloc", GST_DEBUG_PAD_NAME (pad));
2414     *buf = gst_buffer_new_and_alloc (size);
2415     gst_buffer_set_caps (*buf, caps);
2416
2417     ret = GST_FLOW_OK;
2418
2419     goto do_caps;
2420   }
2421 not_negotiated:
2422   {
2423     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2424         "alloc function retured unacceptable buffer");
2425     return GST_FLOW_NOT_NEGOTIATED;
2426   }
2427 peer_error:
2428   {
2429     gst_object_unref (peer);
2430     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2431         "alloc function retured error %d", ret);
2432     return ret;
2433   }
2434 }
2435
2436 /**
2437  * gst_pad_get_internal_links_default:
2438  * @pad: the #GstPad to get the internal links of.
2439  *
2440  * Gets a list of pads to which the given pad is linked to
2441  * inside of the parent element.
2442  * This is the default handler, and thus returns a list of all of the
2443  * pads inside the parent element with opposite direction.
2444  * The caller must free this list after use.
2445  *
2446  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
2447  *
2448  * Not MT safe.
2449  */
2450 GList *
2451 gst_pad_get_internal_links_default (GstPad * pad)
2452 {
2453   GList *res = NULL;
2454   GstElement *parent;
2455   GList *parent_pads;
2456   GstPadDirection direction;
2457
2458   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2459
2460   direction = pad->direction;
2461
2462   parent = GST_PAD_PARENT (pad);
2463   if (!parent)
2464     return NULL;
2465
2466   parent_pads = parent->pads;
2467
2468   while (parent_pads) {
2469     GstPad *parent_pad = GST_PAD_CAST (parent_pads->data);
2470
2471     if (parent_pad->direction != direction) {
2472       res = g_list_prepend (res, parent_pad);
2473     }
2474
2475     parent_pads = g_list_next (parent_pads);
2476   }
2477
2478   return res;
2479 }
2480
2481 /**
2482  * gst_pad_get_internal_links:
2483  * @pad: the #GstPad to get the internal links of.
2484  *
2485  * Gets a list of pads to which the given pad is linked to
2486  * inside of the parent element.
2487  * The caller must free this list after use.
2488  *
2489  * Returns: a newly allocated #GList of pads.
2490  *
2491  * Not MT safe.
2492  */
2493 GList *
2494 gst_pad_get_internal_links (GstPad * pad)
2495 {
2496   GList *res = NULL;
2497
2498   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2499
2500   if (GST_PAD_INTLINKFUNC (pad))
2501     res = GST_PAD_INTLINKFUNC (pad) (pad);
2502
2503   return res;
2504 }
2505
2506
2507 static gboolean
2508 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2509 {
2510   GList *orig, *pads;
2511   gboolean result;
2512
2513   GST_INFO_OBJECT (pad, "Sending event %p to all internally linked pads",
2514       event);
2515
2516   result = (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
2517
2518   orig = pads = gst_pad_get_internal_links (pad);
2519
2520   while (pads) {
2521     GstPad *eventpad = GST_PAD_CAST (pads->data);
2522
2523     pads = g_list_next (pads);
2524
2525     /* for all of the internally-linked pads that are actually linked */
2526     if (GST_PAD_IS_LINKED (eventpad)) {
2527       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2528         /* for each pad we send to, we should ref the event; it's up
2529          * to downstream to unref again when handled. */
2530         GST_LOG_OBJECT (pad, "Reffing and sending event %p to %s:%s", event,
2531             GST_DEBUG_PAD_NAME (eventpad));
2532         gst_event_ref (event);
2533         gst_pad_push_event (eventpad, event);
2534       } else {
2535         /* we only send the event on one pad, multi-sinkpad elements
2536          * should implement a handler */
2537         GST_LOG_OBJECT (pad, "sending event %p to one sink pad %s:%s", event,
2538             GST_DEBUG_PAD_NAME (eventpad));
2539         result = gst_pad_push_event (eventpad, event);
2540         goto done;
2541       }
2542     }
2543   }
2544   /* we handled the incoming event so we unref once */
2545   GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
2546   gst_event_unref (event);
2547
2548 done:
2549   g_list_free (orig);
2550
2551   return result;
2552 }
2553
2554 /**
2555  * gst_pad_event_default:
2556  * @pad: a #GstPad to call the default event handler on.
2557  * @event: the #GstEvent to handle.
2558  *
2559  * Invokes the default event handler for the given pad. End-of-stream and
2560  * discontinuity events are handled specially, and then the event is sent to all
2561  * pads internally linked to @pad. Note that if there are many possible sink
2562  * pads that are internally linked to @pad, only one will be sent an event.
2563  * Multi-sinkpad elements should implement custom event handlers.
2564  *
2565  * Returns: TRUE if the event was sent succesfully.
2566  */
2567 gboolean
2568 gst_pad_event_default (GstPad * pad, GstEvent * event)
2569 {
2570   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2571   g_return_val_if_fail (event != NULL, FALSE);
2572
2573   switch (GST_EVENT_TYPE (event)) {
2574     case GST_EVENT_EOS:
2575     {
2576       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2577       gst_pad_pause_task (pad);
2578     }
2579     default:
2580       break;
2581   }
2582
2583   return gst_pad_event_default_dispatch (pad, event);
2584 }
2585
2586 /**
2587  * gst_pad_dispatcher:
2588  * @pad: a #GstPad to dispatch.
2589  * @dispatch: the #GstDispatcherFunction to call.
2590  * @data: gpointer user data passed to the dispatcher function.
2591  *
2592  * Invokes the given dispatcher function on all pads that are 
2593  * internally linked to the given pad. 
2594  * The GstPadDispatcherFunction should return TRUE when no further pads 
2595  * need to be processed.
2596  *
2597  * Returns: TRUE if one of the dispatcher functions returned TRUE.
2598  */
2599 gboolean
2600 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
2601     gpointer data)
2602 {
2603   gboolean res = FALSE;
2604   GList *int_pads, *orig;
2605
2606   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2607   g_return_val_if_fail (dispatch != NULL, FALSE);
2608
2609   orig = int_pads = gst_pad_get_internal_links (pad);
2610
2611   while (int_pads) {
2612     GstPad *int_pad = GST_PAD_CAST (int_pads->data);
2613     GstPad *int_peer = GST_PAD_PEER (int_pad);
2614
2615     if (int_peer) {
2616       res = dispatch (int_peer, data);
2617       if (res)
2618         break;
2619     }
2620     int_pads = g_list_next (int_pads);
2621   }
2622
2623   g_list_free (orig);
2624
2625   return res;
2626 }
2627
2628 /**
2629  * gst_pad_query:
2630  * @pad: a #GstPad to invoke the default query on.
2631  * @query: the #GstQuery to perform.
2632  *
2633  * Dispatches a query to a pad. The query should have been allocated by the
2634  * caller via one of the type-specific allocation functions in gstquery.h. The
2635  * element is responsible for filling the query with an appropriate response,
2636  * which should then be parsed with a type-specific query parsing function.
2637  *
2638  * Again, the caller is responsible for both the allocation and deallocation of
2639  * the query structure.
2640  *
2641  * Returns: TRUE if the query could be performed.
2642  */
2643 gboolean
2644 gst_pad_query (GstPad * pad, GstQuery * query)
2645 {
2646   GstPadQueryFunction func;
2647
2648   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2649   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2650
2651   GST_DEBUG ("sending query %p to pad %s:%s", query, GST_DEBUG_PAD_NAME (pad));
2652
2653   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
2654     goto no_func;
2655
2656   return func (pad, query);
2657
2658 no_func:
2659   {
2660     GST_DEBUG ("pad had no query function");
2661     return FALSE;
2662   }
2663 }
2664
2665 gboolean
2666 gst_pad_query_default (GstPad * pad, GstQuery * query)
2667 {
2668   switch (GST_QUERY_TYPE (query)) {
2669     case GST_QUERY_POSITION:
2670     case GST_QUERY_SEEKING:
2671     case GST_QUERY_FORMATS:
2672     case GST_QUERY_LATENCY:
2673     case GST_QUERY_JITTER:
2674     case GST_QUERY_RATE:
2675     case GST_QUERY_CONVERT:
2676     default:
2677       return gst_pad_dispatcher
2678           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
2679   }
2680 }
2681
2682 #ifndef GST_DISABLE_LOADSAVE
2683 /* FIXME: why isn't this on a GstElement ? */
2684 /**
2685  * gst_pad_load_and_link:
2686  * @self: an #xmlNodePtr to read the description from.
2687  * @parent: the #GstObject element that owns the pad.
2688  *
2689  * Reads the pad definition from the XML node and links the given pad
2690  * in the element to a pad of an element up in the hierarchy.
2691  */
2692 void
2693 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
2694 {
2695   xmlNodePtr field = self->xmlChildrenNode;
2696   GstPad *pad = NULL, *targetpad;
2697   gchar *peer = NULL;
2698   gchar **split;
2699   GstElement *target;
2700   GstObject *grandparent;
2701   gchar *name = NULL;
2702
2703   while (field) {
2704     if (!strcmp ((char *) field->name, "name")) {
2705       name = (gchar *) xmlNodeGetContent (field);
2706       pad = gst_element_get_pad (GST_ELEMENT (parent), name);
2707       g_free (name);
2708     } else if (!strcmp ((char *) field->name, "peer")) {
2709       peer = (gchar *) xmlNodeGetContent (field);
2710     }
2711     field = field->next;
2712   }
2713   g_return_if_fail (pad != NULL);
2714
2715   if (peer == NULL)
2716     return;
2717
2718   split = g_strsplit (peer, ".", 2);
2719
2720   if (split[0] == NULL || split[1] == NULL) {
2721     GST_CAT_DEBUG (GST_CAT_XML,
2722         "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2723         peer, GST_DEBUG_PAD_NAME (pad));
2724
2725     g_free (peer);
2726     return;
2727   }
2728   g_free (peer);
2729
2730   g_return_if_fail (split[0] != NULL);
2731   g_return_if_fail (split[1] != NULL);
2732
2733   grandparent = gst_object_get_parent (parent);
2734
2735   if (grandparent && GST_IS_BIN (grandparent)) {
2736     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2737   } else
2738     goto cleanup;
2739
2740   if (target == NULL)
2741     goto cleanup;
2742
2743   targetpad = gst_element_get_pad (target, split[1]);
2744
2745   if (targetpad == NULL)
2746     goto cleanup;
2747
2748   gst_pad_link (pad, targetpad);
2749
2750 cleanup:
2751   g_strfreev (split);
2752 }
2753
2754 /**
2755  * gst_pad_save_thyself:
2756  * @pad: a #GstPad to save.
2757  * @parent: the parent #xmlNodePtr to save the description in.
2758  *
2759  * Saves the pad into an xml representation.
2760  *
2761  * Returns: the #xmlNodePtr representation of the pad.
2762  */
2763 static xmlNodePtr
2764 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
2765 {
2766   GstPad *pad;
2767   GstPad *peer;
2768
2769   g_return_val_if_fail (GST_IS_PAD (object), NULL);
2770
2771   pad = GST_PAD (object);
2772
2773   xmlNewChild (parent, NULL, (xmlChar *) "name",
2774       (xmlChar *) GST_PAD_NAME (pad));
2775   if (GST_PAD_PEER (pad) != NULL) {
2776     gchar *content;
2777
2778     peer = GST_PAD_PEER (pad);
2779     /* first check to see if the peer's parent's parent is the same */
2780     /* we just save it off */
2781     content = g_strdup_printf ("%s.%s",
2782         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
2783     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
2784     g_free (content);
2785   } else
2786     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
2787
2788   return parent;
2789 }
2790
2791 #if 0
2792 /**
2793  * gst_ghost_pad_save_thyself:
2794  * @pad: a ghost #GstPad to save.
2795  * @parent: the parent #xmlNodePtr to save the description in.
2796  *
2797  * Saves the ghost pad into an xml representation.
2798  *
2799  * Returns: the #xmlNodePtr representation of the pad.
2800  */
2801 xmlNodePtr
2802 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
2803 {
2804   xmlNodePtr self;
2805
2806   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
2807
2808   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
2809   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
2810   xmlNewChild (self, NULL, (xmlChar *) "parent",
2811       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
2812
2813   /* FIXME FIXME FIXME! */
2814
2815   return self;
2816 }
2817 #endif /* 0 */
2818 #endif /* GST_DISABLE_LOADSAVE */
2819
2820 /* 
2821  * should be called with pad lock held 
2822  *
2823  * MT safe.
2824  */
2825 static void
2826 handle_pad_block (GstPad * pad)
2827 {
2828   GstPadBlockCallback callback;
2829   gpointer user_data;
2830
2831   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2832       "signal block taken on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2833
2834   /* need to grab extra ref for the callbacks */
2835   gst_object_ref (pad);
2836
2837   callback = pad->block_callback;
2838   if (callback) {
2839     user_data = pad->block_data;
2840     GST_UNLOCK (pad);
2841     callback (pad, TRUE, user_data);
2842     GST_LOCK (pad);
2843   } else {
2844     GST_PAD_BLOCK_SIGNAL (pad);
2845   }
2846
2847   while (GST_PAD_IS_BLOCKED (pad))
2848     GST_PAD_BLOCK_WAIT (pad);
2849
2850   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
2851
2852   callback = pad->block_callback;
2853   if (callback) {
2854     user_data = pad->block_data;
2855     GST_UNLOCK (pad);
2856     callback (pad, FALSE, user_data);
2857     GST_LOCK (pad);
2858   } else {
2859     GST_PAD_BLOCK_SIGNAL (pad);
2860   }
2861
2862   gst_object_unref (pad);
2863 }
2864
2865 /**********************************************************************
2866  * Data passing functions
2867  */
2868
2869 static gboolean
2870 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
2871 {
2872   GValue ret = { 0 };
2873   GValue args[2] = { {0}, {0} };
2874   gboolean res;
2875
2876   /* init */
2877   g_value_init (&ret, G_TYPE_BOOLEAN);
2878   g_value_set_boolean (&ret, TRUE);
2879   g_value_init (&args[0], GST_TYPE_PAD);
2880   g_value_set_object (&args[0], pad);
2881   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);        // G_TYPE_POINTER);
2882   gst_value_set_mini_object (&args[1], obj);
2883
2884   /* actually emit */
2885   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], 0, &ret);
2886   res = g_value_get_boolean (&ret);
2887
2888   /* clean up */
2889   g_value_unset (&ret);
2890   g_value_unset (&args[0]);
2891   g_value_unset (&args[1]);
2892
2893   return res;
2894 }
2895
2896 /**
2897  * gst_pad_chain:
2898  * @pad: a sink #GstPad.
2899  * @buffer: the #GstBuffer to send.
2900  *
2901  * Chain a buffer to @pad.
2902  *
2903  * Returns: a #GstFlowReturn from the pad.
2904  *
2905  * MT safe.
2906  */
2907 GstFlowReturn
2908 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
2909 {
2910   GstCaps *caps;
2911   gboolean caps_changed;
2912   GstPadChainFunction chainfunc;
2913   GstFlowReturn ret;
2914   gboolean emit_signal;
2915
2916   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2917   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
2918       GST_FLOW_ERROR);
2919   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2920   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2921
2922   GST_STREAM_LOCK (pad);
2923
2924   GST_LOCK (pad);
2925   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2926     goto flushing;
2927
2928   caps = GST_BUFFER_CAPS (buffer);
2929   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2930
2931   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
2932   GST_UNLOCK (pad);
2933
2934   /* see if the signal should be emited, we emit before caps nego as
2935    * we might drop the buffer and do capsnego for nothing. */
2936   if (G_UNLIKELY (emit_signal)) {
2937     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer)))
2938       goto dropping;
2939   }
2940
2941   /* we got a new datatype on the pad, see if it can handle it */
2942   if (G_UNLIKELY (caps_changed)) {
2943     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
2944     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
2945       goto not_negotiated;
2946   }
2947
2948   /* NOTE: we read the chainfunc unlocked.
2949    * we cannot hold the lock for the pad so we might send
2950    * the data to the wrong function. This is not really a
2951    * problem since functions are assigned at creation time
2952    * and don't change that often... */
2953   if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
2954     goto no_function;
2955
2956   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2957       "calling chainfunction &%s of pad %s:%s",
2958       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad));
2959
2960   ret = chainfunc (pad, buffer);
2961
2962   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2963       "called chainfunction &%s of pad %s:%s, returned %d",
2964       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad), ret);
2965
2966   GST_STREAM_UNLOCK (pad);
2967
2968   return ret;
2969
2970   /* ERRORS */
2971 flushing:
2972   {
2973     gst_buffer_unref (buffer);
2974     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2975         "pushing, but pad was flushing");
2976     GST_UNLOCK (pad);
2977     GST_STREAM_UNLOCK (pad);
2978     return GST_FLOW_WRONG_STATE;
2979   }
2980 dropping:
2981   {
2982     gst_buffer_unref (buffer);
2983     GST_DEBUG ("Dropping buffer due to FALSE probe return");
2984     GST_STREAM_UNLOCK (pad);
2985     return GST_FLOW_OK;
2986   }
2987 not_negotiated:
2988   {
2989     gst_buffer_unref (buffer);
2990     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2991         "pushing buffer but pad did not accept");
2992     GST_STREAM_UNLOCK (pad);
2993     return GST_FLOW_NOT_NEGOTIATED;
2994   }
2995 no_function:
2996   {
2997     gst_buffer_unref (buffer);
2998     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2999         "pushing, but not chainhandler");
3000     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3001         ("push on pad %s:%s but it has no chainfunction",
3002             GST_DEBUG_PAD_NAME (pad)));
3003     GST_STREAM_UNLOCK (pad);
3004     return GST_FLOW_ERROR;
3005   }
3006 }
3007
3008 /**
3009  * gst_pad_push:
3010  * @pad: a source #GstPad.
3011  * @buffer: the #GstBuffer to push.
3012  *
3013  * Pushes a buffer to the peer of @pad. @pad must be linked.
3014  *
3015  * Returns: a #GstFlowReturn from the peer pad.
3016  *
3017  * MT safe.
3018  */
3019 GstFlowReturn
3020 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3021 {
3022   GstPad *peer;
3023   GstFlowReturn ret;
3024   gboolean emit_signal;
3025
3026   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3027   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3028   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3029   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3030
3031   GST_LOCK (pad);
3032   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3033     handle_pad_block (pad);
3034
3035   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3036     goto not_linked;
3037
3038   /* we emit signals on the pad areg, the peer will have a chance to
3039    * emit in the _chain() function */
3040   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3041
3042   gst_object_ref (peer);
3043   GST_UNLOCK (pad);
3044
3045   if (G_UNLIKELY (emit_signal)) {
3046     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer)))
3047       goto dropping;
3048   }
3049
3050   ret = gst_pad_chain (peer, buffer);
3051
3052   gst_object_unref (peer);
3053
3054   return ret;
3055
3056   /* ERROR recovery here */
3057 not_linked:
3058   {
3059     gst_buffer_unref (buffer);
3060     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3061         "pushing, but it was not linked");
3062     GST_UNLOCK (pad);
3063     return GST_FLOW_NOT_LINKED;
3064   }
3065 dropping:
3066   {
3067     gst_buffer_unref (buffer);
3068     gst_object_unref (peer);
3069     GST_DEBUG ("Dropping buffer due to FALSE probe return");
3070     return GST_FLOW_OK;
3071   }
3072 }
3073
3074 /**
3075  * gst_pad_check_pull_range:
3076  * @pad: a sink #GstPad.
3077  *
3078  * Checks if a #gst_pad_pull_range() can be performed on the peer
3079  * source pad. This function is used by plugins that want to check
3080  * if they can use random access on the peer source pad. 
3081  *
3082  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
3083  * if it needs to perform some logic to determine if pull_range is
3084  * possible.
3085  *
3086  * Returns: a gboolean with the result.
3087  *
3088  * MT safe.
3089  */
3090 gboolean
3091 gst_pad_check_pull_range (GstPad * pad)
3092 {
3093   GstPad *peer;
3094   gboolean ret;
3095   GstPadCheckGetRangeFunction checkgetrangefunc;
3096
3097   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3098
3099   GST_LOCK (pad);
3100   if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
3101     goto wrong_direction;
3102
3103   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3104     goto not_connected;
3105
3106   gst_object_ref (peer);
3107   GST_UNLOCK (pad);
3108
3109   /* see note in above function */
3110   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
3111     /* FIXME, kindoff ghetto */
3112     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
3113   } else {
3114     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3115         "calling checkgetrangefunc %s of peer pad %s:%s",
3116         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
3117
3118     ret = checkgetrangefunc (peer);
3119   }
3120
3121   gst_object_unref (peer);
3122
3123   return ret;
3124
3125   /* ERROR recovery here */
3126 wrong_direction:
3127   {
3128     GST_UNLOCK (pad);
3129     return FALSE;
3130   }
3131 not_connected:
3132   {
3133     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3134         "checking pull range, but it was not linked");
3135     GST_UNLOCK (pad);
3136     return FALSE;
3137   }
3138 }
3139
3140 /**
3141  * gst_pad_get_range:
3142  * @pad: a src #GstPad.
3143  * @offset: The start offset of the buffer
3144  * @size: The length of the buffer
3145  * @buffer: a pointer to hold the #GstBuffer.
3146  *
3147  * Calls the getrange function of @pad. 
3148  *
3149  * Returns: a #GstFlowReturn from the pad.
3150  *
3151  * MT safe.
3152  */
3153 GstFlowReturn
3154 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3155     GstBuffer ** buffer)
3156 {
3157   GstFlowReturn ret;
3158   GstPadGetRangeFunction getrangefunc;
3159   gboolean emit_signal;
3160
3161   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3162   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3163   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3164
3165   GST_STREAM_LOCK (pad);
3166
3167   GST_LOCK (pad);
3168   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3169     goto flushing;
3170
3171   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3172   GST_UNLOCK (pad);
3173
3174   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3175     goto no_function;
3176
3177   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3178       "calling getrangefunc %s of peer pad %s:%s, offset %"
3179       G_GUINT64_FORMAT ", size %u",
3180       GST_DEBUG_FUNCPTR_NAME (getrangefunc), GST_DEBUG_PAD_NAME (pad),
3181       offset, size);
3182
3183   ret = getrangefunc (pad, offset, size, buffer);
3184
3185   /* can only fire the signal if we have a valid buffer */
3186   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3187     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3188       goto dropping;
3189   }
3190
3191   GST_STREAM_UNLOCK (pad);
3192
3193   return ret;
3194
3195   /* ERRORS */
3196 flushing:
3197   {
3198     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3199         "pulling range, but pad was flushing");
3200     GST_UNLOCK (pad);
3201     GST_STREAM_UNLOCK (pad);
3202     return GST_FLOW_WRONG_STATE;
3203   }
3204 no_function:
3205   {
3206     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3207         ("pullrange on pad %s:%s but it has no getrangefunction",
3208             GST_DEBUG_PAD_NAME (pad)));
3209     GST_STREAM_UNLOCK (pad);
3210     return GST_FLOW_ERROR;
3211   }
3212 dropping:
3213   {
3214     GST_DEBUG ("Dropping data after FALSE probe return");
3215     GST_STREAM_UNLOCK (pad);
3216     gst_buffer_unref (*buffer);
3217     *buffer = NULL;
3218     return GST_FLOW_UNEXPECTED;
3219   }
3220 }
3221
3222
3223 /**
3224  * gst_pad_pull_range:
3225  * @pad: a sink #GstPad.
3226  * @offset: The start offset of the buffer
3227  * @size: The length of the buffer
3228  * @buffer: a pointer to hold the #GstBuffer.
3229  *
3230  * Pulls a buffer from the peer pad. @pad must be linked.
3231  *
3232  * Returns: a #GstFlowReturn from the peer pad.
3233  *
3234  * MT safe.
3235  */
3236 GstFlowReturn
3237 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3238     GstBuffer ** buffer)
3239 {
3240   GstPad *peer;
3241   GstFlowReturn ret;
3242   gboolean emit_signal;
3243
3244   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3245   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
3246       GST_FLOW_ERROR);
3247   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3248
3249   GST_LOCK (pad);
3250
3251   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3252     handle_pad_block (pad);
3253
3254   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3255     goto not_connected;
3256
3257   /* signal emision for the pad, peer has chance to emit when
3258    * we call _get_range() */
3259   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3260
3261   gst_object_ref (peer);
3262   GST_UNLOCK (pad);
3263
3264   ret = gst_pad_get_range (peer, offset, size, buffer);
3265
3266   gst_object_unref (peer);
3267
3268   /* can only fire the signal if we have a valid buffer */
3269   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3270     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3271       goto dropping;
3272   }
3273   return ret;
3274
3275   /* ERROR recovery here */
3276 not_connected:
3277   {
3278     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3279         "pulling range, but it was not linked");
3280     GST_UNLOCK (pad);
3281     return GST_FLOW_NOT_LINKED;
3282   }
3283 dropping:
3284   {
3285     GST_DEBUG ("Dropping data after FALSE probe return");
3286     gst_buffer_unref (*buffer);
3287     *buffer = NULL;
3288     return GST_FLOW_UNEXPECTED;
3289   }
3290 }
3291
3292 /**
3293  * gst_pad_push_event:
3294  * @pad: a #GstPad to push the event to.
3295  * @event: the #GstEvent to send to the pad.
3296  *
3297  * Sends the event to the peer of the given pad. This function is
3298  * mainly used by elements to send events to their peer
3299  * elements.
3300  *
3301  * Returns: TRUE if the event was handled.
3302  *
3303  * MT safe.
3304  */
3305 gboolean
3306 gst_pad_push_event (GstPad * pad, GstEvent * event)
3307 {
3308   GstPad *peerpad;
3309   gboolean result;
3310   gboolean emit_signal;
3311
3312   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3313   g_return_val_if_fail (event != NULL, FALSE);
3314
3315   GST_LOCK (pad);
3316   peerpad = GST_PAD_PEER (pad);
3317   if (peerpad == NULL)
3318     goto not_linked;
3319
3320   emit_signal = GST_PAD_DO_EVENT_SIGNALS (pad) > 0;
3321
3322   gst_object_ref (peerpad);
3323   GST_UNLOCK (pad);
3324
3325   if (G_UNLIKELY (emit_signal)) {
3326     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
3327       goto dropping;
3328   }
3329
3330   result = gst_pad_send_event (peerpad, event);
3331
3332   gst_object_unref (peerpad);
3333
3334   return result;
3335
3336   /* ERROR handling */
3337 not_linked:
3338   {
3339     gst_event_unref (event);
3340     GST_UNLOCK (pad);
3341     return FALSE;
3342   }
3343 dropping:
3344   {
3345     GST_DEBUG ("Dropping event after FALSE probe return");
3346     gst_object_unref (peerpad);
3347     gst_event_unref (event);
3348     return FALSE;
3349   }
3350 }
3351
3352 /**
3353  * gst_pad_send_event:
3354  * @pad: a #GstPad to send the event to.
3355  * @event: the #GstEvent to send to the pad.
3356  *
3357  * Sends the event to the pad. This function can be used
3358  * by applications to send events in the pipeline.
3359  *
3360  * Returns: TRUE if the event was handled.
3361  */
3362 gboolean
3363 gst_pad_send_event (GstPad * pad, GstEvent * event)
3364 {
3365   gboolean result = FALSE;
3366   GstPadEventFunction eventfunc;
3367   gboolean emit_signal;
3368
3369   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3370   g_return_val_if_fail (event != NULL, FALSE);
3371
3372   GST_LOCK (pad);
3373
3374   if (GST_PAD_IS_SINK (pad) && !GST_EVENT_IS_DOWNSTREAM (event))
3375     goto wrong_direction;
3376   if (GST_PAD_IS_SRC (pad) && !GST_EVENT_IS_UPSTREAM (event))
3377     goto wrong_direction;
3378
3379   if (GST_EVENT_SRC (event) == NULL)
3380     GST_EVENT_SRC (event) = gst_object_ref (pad);
3381
3382   switch (GST_EVENT_TYPE (event)) {
3383     case GST_EVENT_FLUSH_START:
3384       GST_CAT_DEBUG (GST_CAT_EVENT,
3385           "have event type %d (FLUSH_START) on pad %s:%s",
3386           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3387
3388       /* can't even accept a flush begin event when flushing */
3389       if (GST_PAD_IS_FLUSHING (pad))
3390         goto flushing;
3391       GST_PAD_SET_FLUSHING (pad);
3392       GST_CAT_DEBUG (GST_CAT_EVENT, "set flush flag");
3393       break;
3394     case GST_EVENT_FLUSH_STOP:
3395       GST_PAD_UNSET_FLUSHING (pad);
3396       GST_CAT_DEBUG (GST_CAT_EVENT, "cleared flush flag");
3397       break;
3398     default:
3399       GST_CAT_DEBUG (GST_CAT_EVENT, "have event type %d on pad %s:%s",
3400           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3401
3402       if (GST_PAD_IS_FLUSHING (pad))
3403         goto flushing;
3404       break;
3405   }
3406
3407   if ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL)
3408     goto no_function;
3409
3410   emit_signal = GST_PAD_DO_EVENT_SIGNALS (pad) > 0;
3411
3412   gst_object_ref (pad);
3413   GST_UNLOCK (pad);
3414
3415   if (G_UNLIKELY (emit_signal)) {
3416     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
3417       goto dropping;
3418   }
3419
3420   result = eventfunc (GST_PAD_CAST (pad), event);
3421
3422   gst_object_unref (pad);
3423
3424   return result;
3425
3426   /* ERROR handling */
3427 wrong_direction:
3428   {
3429     g_warning ("pad %s:%s sending event in wrong direction",
3430         GST_DEBUG_PAD_NAME (pad));
3431     GST_UNLOCK (pad);
3432     gst_event_unref (event);
3433     return FALSE;
3434   }
3435 no_function:
3436   {
3437     g_warning ("pad %s:%s has no event handler, file a bug.",
3438         GST_DEBUG_PAD_NAME (pad));
3439     GST_UNLOCK (pad);
3440     gst_event_unref (event);
3441     return FALSE;
3442   }
3443 flushing:
3444   {
3445     GST_UNLOCK (pad);
3446     GST_CAT_DEBUG (GST_CAT_EVENT, "Received event on flushing pad. Discarding");
3447     gst_event_unref (event);
3448     return FALSE;
3449   }
3450 dropping:
3451   {
3452     GST_DEBUG ("Dropping event after FALSE probe return");
3453     gst_object_unref (pad);
3454     gst_event_unref (event);
3455     return FALSE;
3456   }
3457 }
3458
3459 /************************************************************************
3460  *
3461  * templates
3462  *
3463  */
3464 static void gst_pad_template_class_init (GstPadTemplateClass * klass);
3465 static void gst_pad_template_init (GstPadTemplate * templ);
3466 static void gst_pad_template_dispose (GObject * object);
3467
3468 GType
3469 gst_pad_template_get_type (void)
3470 {
3471   static GType padtemplate_type = 0;
3472
3473   if (!padtemplate_type) {
3474     static const GTypeInfo padtemplate_info = {
3475       sizeof (GstPadTemplateClass), NULL, NULL,
3476       (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
3477       sizeof (GstPadTemplate),
3478       0,
3479       (GInstanceInitFunc) gst_pad_template_init, NULL
3480     };
3481
3482     padtemplate_type =
3483         g_type_register_static (GST_TYPE_OBJECT, "GstPadTemplate",
3484         &padtemplate_info, 0);
3485   }
3486   return padtemplate_type;
3487 }
3488
3489 static void
3490 gst_pad_template_class_init (GstPadTemplateClass * klass)
3491 {
3492   GObjectClass *gobject_class;
3493   GstObjectClass *gstobject_class;
3494
3495   gobject_class = (GObjectClass *) klass;
3496   gstobject_class = (GstObjectClass *) klass;
3497
3498   padtemplate_parent_class = g_type_class_ref (GST_TYPE_OBJECT);
3499
3500   gst_pad_template_signals[TEMPL_PAD_CREATED] =
3501       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
3502       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
3503       NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
3504
3505   gobject_class->dispose = gst_pad_template_dispose;
3506
3507   gstobject_class->path_string_separator = "*";
3508 }
3509
3510 static void
3511 gst_pad_template_init (GstPadTemplate * templ)
3512 {
3513 }
3514
3515 static void
3516 gst_pad_template_dispose (GObject * object)
3517 {
3518   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
3519
3520   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
3521   if (GST_PAD_TEMPLATE_CAPS (templ)) {
3522     gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
3523   }
3524
3525   G_OBJECT_CLASS (padtemplate_parent_class)->dispose (object);
3526 }
3527
3528 /* ALWAYS padtemplates cannot have conversion specifications, it doesn't make
3529  * sense.
3530  * SOMETIMES padtemplates can do whatever they want, they are provided by the
3531  * element.
3532  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
3533  * 'sink%d' template is automatically selected), so we need to restrict their
3534  * naming.
3535  */
3536 static gboolean
3537 name_is_valid (const gchar * name, GstPadPresence presence)
3538 {
3539   const gchar *str;
3540
3541   if (presence == GST_PAD_ALWAYS) {
3542     if (strchr (name, '%')) {
3543       g_warning ("invalid name template %s: conversion specifications are not"
3544           " allowed for GST_PAD_ALWAYS padtemplates", name);
3545       return FALSE;
3546     }
3547   } else if (presence == GST_PAD_REQUEST) {
3548     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
3549       g_warning ("invalid name template %s: only one conversion specification"
3550           " allowed in GST_PAD_REQUEST padtemplate", name);
3551       return FALSE;
3552     }
3553     if (str && (*(str + 1) != 's' && *(str + 1) != 'd')) {
3554       g_warning ("invalid name template %s: conversion specification must be of"
3555           " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
3556       return FALSE;
3557     }
3558     if (str && (*(str + 2) != '\0')) {
3559       g_warning ("invalid name template %s: conversion specification must"
3560           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
3561       return FALSE;
3562     }
3563   }
3564
3565   return TRUE;
3566 }
3567
3568 /**
3569  * gst_static_pad_template_get:
3570  * @pad_template: the static pad template
3571  *
3572  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
3573  *
3574  * Returns: a new #GstPadTemplate.
3575  */
3576 GstPadTemplate *
3577 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
3578 {
3579   GstPadTemplate *new;
3580
3581   if (!name_is_valid (pad_template->name_template, pad_template->presence))
3582     return NULL;
3583
3584   new = g_object_new (gst_pad_template_get_type (),
3585       "name", pad_template->name_template, NULL);
3586
3587   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (pad_template->name_template);
3588   GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
3589   GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
3590
3591   GST_PAD_TEMPLATE_CAPS (new) =
3592       gst_caps_copy (gst_static_caps_get (&pad_template->static_caps));
3593
3594   return new;
3595 }
3596
3597 /**
3598  * gst_pad_template_new:
3599  * @name_template: the name template.
3600  * @direction: the #GstPadDirection of the template.
3601  * @presence: the #GstPadPresence of the pad.
3602  * @caps: a #GstCaps set for the template. The caps are taken ownership of.
3603  *
3604  * Creates a new pad template with a name according to the given template
3605  * and with the given arguments. This functions takes ownership of the provided
3606  * caps, so be sure to not use them afterwards.
3607  *
3608  * Returns: a new #GstPadTemplate.
3609  */
3610 GstPadTemplate *
3611 gst_pad_template_new (const gchar * name_template,
3612     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
3613 {
3614   GstPadTemplate *new;
3615
3616   g_return_val_if_fail (name_template != NULL, NULL);
3617   g_return_val_if_fail (caps != NULL, NULL);
3618   g_return_val_if_fail (direction == GST_PAD_SRC
3619       || direction == GST_PAD_SINK, NULL);
3620   g_return_val_if_fail (presence == GST_PAD_ALWAYS
3621       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
3622
3623   if (!name_is_valid (name_template, presence))
3624     return NULL;
3625
3626   new = g_object_new (gst_pad_template_get_type (),
3627       "name", name_template, NULL);
3628
3629   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
3630   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
3631   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
3632   GST_PAD_TEMPLATE_CAPS (new) = caps;
3633
3634   return new;
3635 }
3636
3637 /**
3638  * gst_static_pad_template_get_caps:
3639  * @templ: a #GstStaticPadTemplate to get capabilities of.
3640  *
3641  * Gets the capabilities of the static pad template.
3642  *
3643  * Returns: the #GstCaps of the static pad template. If you need to keep a 
3644  * reference to the caps, take a ref (see gst_caps_ref ()).
3645  */
3646 GstCaps *
3647 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
3648 {
3649   g_return_val_if_fail (templ, NULL);
3650
3651   return (GstCaps *) gst_static_caps_get (&templ->static_caps);
3652 }
3653
3654 /**
3655  * gst_pad_template_get_caps:
3656  * @templ: a #GstPadTemplate to get capabilities of.
3657  *
3658  * Gets the capabilities of the pad template.
3659  *
3660  * Returns: the #GstCaps of the pad template. If you need to keep a reference to
3661  * the caps, take a ref (see gst_caps_ref ()).
3662  */
3663 GstCaps *
3664 gst_pad_template_get_caps (GstPadTemplate * templ)
3665 {
3666   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
3667
3668   return GST_PAD_TEMPLATE_CAPS (templ);
3669 }
3670
3671 /**
3672  * gst_pad_set_element_private:
3673  * @pad: the #GstPad to set the private data of.
3674  * @priv: The private data to attach to the pad.
3675  *
3676  * Set the given private data gpointer on the pad. 
3677  * This function can only be used by the element that owns the pad.
3678  */
3679 void
3680 gst_pad_set_element_private (GstPad * pad, gpointer priv)
3681 {
3682   pad->element_private = priv;
3683 }
3684
3685 /**
3686  * gst_pad_get_element_private:
3687  * @pad: the #GstPad to get the private data of.
3688  *
3689  * Gets the private data of a pad.
3690  *
3691  * Returns: a #gpointer to the private data.
3692  */
3693 gpointer
3694 gst_pad_get_element_private (GstPad * pad)
3695 {
3696   return pad->element_private;
3697 }
3698
3699 /**
3700  * gst_pad_start_task:
3701  * @pad: the #GstPad to start the task of
3702  * @func: the task function to call
3703  * @data: data passed to the task function
3704  *
3705  * Starts a task that repeadedly calls @func with @data. This function
3706  * is nostly used in the pad activation function to start the
3707  * dataflow. This function will automatically acauire the STREAM_LOCK of
3708  * the pad before calling @func.
3709  *
3710  * Returns: a TRUE if the task could be started. 
3711  */
3712 gboolean
3713 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
3714 {
3715   GstTask *task;
3716
3717   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3718   g_return_val_if_fail (func != NULL, FALSE);
3719
3720   GST_LOCK (pad);
3721   task = GST_PAD_TASK (pad);
3722   if (task == NULL) {
3723     task = gst_task_create (func, data);
3724     gst_task_set_lock (task, GST_STREAM_GET_LOCK (pad));
3725     GST_PAD_TASK (pad) = task;
3726   }
3727   gst_task_start (task);
3728   GST_UNLOCK (pad);
3729
3730   return TRUE;
3731 }
3732
3733 /**
3734  * gst_pad_pause_task:
3735  * @pad: the #GstPad to pause the task of
3736  *
3737  * Pause the task of @pad. This function will also make sure that the 
3738  * function executed by the task will effectively stop.
3739  *
3740  * Returns: a TRUE if the task could be paused or FALSE when the pad
3741  * has no task.
3742  */
3743 gboolean
3744 gst_pad_pause_task (GstPad * pad)
3745 {
3746   GstTask *task;
3747
3748   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3749
3750   GST_LOCK (pad);
3751   task = GST_PAD_TASK (pad);
3752   if (task == NULL)
3753     goto no_task;
3754   gst_task_pause (task);
3755   GST_UNLOCK (pad);
3756
3757   GST_STREAM_LOCK (pad);
3758   GST_STREAM_UNLOCK (pad);
3759
3760   return TRUE;
3761
3762 no_task:
3763   {
3764     GST_UNLOCK (pad);
3765     return TRUE;
3766   }
3767 }
3768
3769 /**
3770  * gst_pad_stop_task:
3771  * @pad: the #GstPad to stop the task of
3772  *
3773  * Stop the task of @pad. This function will also make sure that the 
3774  * function executed by the task will effectively stop if not called
3775  * from the GstTaskFunction.
3776  *
3777  * This function will deadlock if called from the GstTaskFunction of
3778  * the task. Use #gst_task_pause() instead.
3779  *
3780  * Returns: a TRUE if the task could be stopped or FALSE when the pad
3781  * has no task.
3782  */
3783 gboolean
3784 gst_pad_stop_task (GstPad * pad)
3785 {
3786   GstTask *task;
3787
3788   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3789
3790   GST_LOCK (pad);
3791   task = GST_PAD_TASK (pad);
3792   if (task == NULL)
3793     goto no_task;
3794   GST_PAD_TASK (pad) = NULL;
3795   gst_task_stop (task);
3796   GST_UNLOCK (pad);
3797
3798   GST_STREAM_LOCK (pad);
3799   GST_STREAM_UNLOCK (pad);
3800
3801   gst_task_join (task);
3802
3803   gst_object_unref (task);
3804
3805   return TRUE;
3806
3807 no_task:
3808   {
3809     GST_UNLOCK (pad);
3810     return TRUE;
3811   }
3812 }