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