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