gst/gstpad.c: emit have-data before checking for peers. This allows for probe handle...
[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 whether the pad should be blocked or unblocked
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 happens right before the next
776  * attempt at pushing a buffer on the pad.
777  *
778  * This can take a while as
779  * the pad can only become blocked when real dataflow is happening.
780  * When the pipeline is stalled, for example in PAUSED, this can
781  * take an indeterminate amount of time.
782  * You can pass NULL as the callback to make this call block. Be
783  * carefull with this blocking call as it might not return for
784  * reasons stated above.
785  *
786  * Returns: TRUE if the pad could be blocked. This function can fail
787  *   if wrong parameters were passed or the pad was already in the
788  *   requested state.
789  *
790  * MT safe.
791  */
792 gboolean
793 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
794     GstPadBlockCallback callback, gpointer user_data)
795 {
796   gboolean was_blocked;
797
798   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
799
800   GST_LOCK (pad);
801
802   was_blocked = GST_PAD_IS_BLOCKED (pad);
803
804   if (G_UNLIKELY (was_blocked == blocked))
805     goto had_right_state;
806
807   if (blocked) {
808     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad %s:%s",
809         GST_DEBUG_PAD_NAME (pad));
810
811     GST_FLAG_SET (pad, GST_PAD_BLOCKED);
812     pad->block_callback = callback;
813     pad->block_data = user_data;
814     if (!callback) {
815       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
816       GST_PAD_BLOCK_WAIT (pad);
817       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
818     }
819   } else {
820     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad %s:%s",
821         GST_DEBUG_PAD_NAME (pad));
822
823     GST_FLAG_UNSET (pad, GST_PAD_BLOCKED);
824
825     pad->block_callback = callback;
826     pad->block_data = user_data;
827
828     if (callback) {
829       GST_PAD_BLOCK_SIGNAL (pad);
830     } else {
831       GST_PAD_BLOCK_SIGNAL (pad);
832       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
833       GST_PAD_BLOCK_WAIT (pad);
834       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
835     }
836   }
837   GST_UNLOCK (pad);
838
839   return TRUE;
840
841 had_right_state:
842   {
843     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
844         "pad %s:%s was in right state", GST_DEBUG_PAD_NAME (pad));
845     GST_UNLOCK (pad);
846     return FALSE;
847   }
848 }
849
850 /**
851  * gst_pad_set_blocked:
852  * @pad: the #GstPad to block or unblock
853  * @blocked: boolean indicating we should block or unblock
854  *
855  * Blocks or unblocks the dataflow on a pad. This function is
856  * a shortcut for @gst_pad_set_blocked_async() with a NULL
857  * callback.
858  *
859  * Returns: TRUE if the pad could be blocked. This function can fail
860  *   wrong parameters were passed or the pad was already in the 
861  *   requested state.
862  *
863  * MT safe.
864  */
865 gboolean
866 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
867 {
868   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
869 }
870
871 /**
872  * gst_pad_is_blocked:
873  * @pad: the #GstPad to query 
874  *
875  * Checks if the pad is blocked or not. This function returns the
876  * last requested state of the pad. It is not certain that the pad
877  * is actually blocked at this point.
878  *
879  * Returns: TRUE if the pad is blocked.
880  *
881  * MT safe.
882  */
883 gboolean
884 gst_pad_is_blocked (GstPad * pad)
885 {
886   gboolean result = FALSE;
887
888   g_return_val_if_fail (GST_IS_PAD (pad), result);
889
890   GST_LOCK (pad);
891   result = GST_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
892   GST_UNLOCK (pad);
893
894   return result;
895 }
896
897 /**
898  * gst_pad_set_activate_function:
899  * @pad: a sink #GstPad.
900  * @activate: the #GstPadActivateFunction to set.
901  *
902  * Sets the given activate function for the pad. The activate function will
903  * dispatch to activate_push or activate_pull to perform the actual activation.
904  * Only makes sense to set on sink pads.
905  *
906  * Call this function if your sink pad can start a pull-based task.
907  */
908 void
909 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
910 {
911   g_return_if_fail (GST_IS_PAD (pad));
912
913   GST_PAD_ACTIVATEFUNC (pad) = activate;
914   GST_CAT_DEBUG (GST_CAT_PADS, "activatefunc for %s:%s set to %s",
915       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activate));
916 }
917
918 /**
919  * gst_pad_set_activatepull_function:
920  * @pad: a sink #GstPad.
921  * @activatepull: the #GstPadActivateModeFunction to set.
922  *
923  * Sets the given activate_pull function for the pad. An activate_pull function
924  * prepares the element and any upstream connections for pulling. See XXX
925  * part-activation.txt for details.
926  */
927 void
928 gst_pad_set_activatepull_function (GstPad * pad,
929     GstPadActivateModeFunction activatepull)
930 {
931   g_return_if_fail (GST_IS_PAD (pad));
932
933   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
934   GST_CAT_DEBUG (GST_CAT_PADS, "activatepullfunc for %s:%s set to %s",
935       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepull));
936 }
937
938 /**
939  * gst_pad_set_activatepush_function:
940  * @pad: a sink #GstPad.
941  * @activatepush: the #GstPadActivateModeFunction to set.
942  *
943  * Sets the given activate_push function for the pad. An activate_push function
944  * prepares the element for pushing. See XXX part-activation.txt for details.
945  */
946 void
947 gst_pad_set_activatepush_function (GstPad * pad,
948     GstPadActivateModeFunction activatepush)
949 {
950   g_return_if_fail (GST_IS_PAD (pad));
951
952   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
953   GST_CAT_DEBUG (GST_CAT_PADS, "activatepushfunc for %s:%s set to %s",
954       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepush));
955 }
956
957 /**
958  * gst_pad_set_chain_function:
959  * @pad: a sink #GstPad.
960  * @chain: the #GstPadChainFunction to set.
961  *
962  * Sets the given chain function for the pad. The chain function is called to
963  * process a #GstBuffer input buffer.
964  */
965 void
966 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
967 {
968   g_return_if_fail (GST_IS_PAD (pad));
969   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
970
971   GST_PAD_CHAINFUNC (pad) = chain;
972   GST_CAT_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
973       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
974 }
975
976 /**
977  * gst_pad_set_getrange_function:
978  * @pad: a source #GstPad.
979  * @get: the #GstPadGetRangeFunction to set.
980  *
981  * Sets the given getrange function for the pad. The getrange function is called to
982  * produce a new #GstBuffer to start the processing pipeline. Getrange functions cannot
983  * return %NULL.
984  */
985 void
986 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
987 {
988   g_return_if_fail (GST_IS_PAD (pad));
989   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
990
991   GST_PAD_GETRANGEFUNC (pad) = get;
992
993   GST_CAT_DEBUG (GST_CAT_PADS, "getrangefunc for %s:%s  set to %s",
994       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
995 }
996
997 /**
998  * gst_pad_set_checkgetrange_function:
999  * @pad: a source #GstPad.
1000  * @check: the #GstPadCheckGetRangeFunction to set.
1001  *
1002  * Sets the given checkgetrange function for the pad. Implement this function on
1003  * a pad if you dynamically support getrange based scheduling on the pad.
1004  */
1005 void
1006 gst_pad_set_checkgetrange_function (GstPad * pad,
1007     GstPadCheckGetRangeFunction check)
1008 {
1009   g_return_if_fail (GST_IS_PAD (pad));
1010   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
1011
1012   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1013
1014   GST_CAT_DEBUG (GST_CAT_PADS, "checkgetrangefunc for %s:%s  set to %s",
1015       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (check));
1016 }
1017
1018 /**
1019  * gst_pad_set_event_function:
1020  * @pad: a source #GstPad.
1021  * @event: the #GstPadEventFunction to set.
1022  *
1023  * Sets the given event handler for the pad.
1024  */
1025 void
1026 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1027 {
1028   g_return_if_fail (GST_IS_PAD (pad));
1029
1030   GST_PAD_EVENTFUNC (pad) = event;
1031
1032   GST_CAT_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
1033       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
1034 }
1035
1036 /**
1037  * gst_pad_set_query_function:
1038  * @pad: a #GstPad of either direction.
1039  * @query: the #GstPadQueryFunction to set.
1040  *
1041  * Set the given query function for the pad.
1042  */
1043 void
1044 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1045 {
1046   g_return_if_fail (GST_IS_PAD (pad));
1047
1048   GST_PAD_QUERYFUNC (pad) = query;
1049
1050   GST_CAT_DEBUG (GST_CAT_PADS, "queryfunc for %s:%s  set to %s",
1051       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (query));
1052 }
1053
1054 /**
1055  * gst_pad_set_query_type_function:
1056  * @pad: a #GstPad of either direction.
1057  * @type_func: the #GstPadQueryTypeFunction to set.
1058  *
1059  * Set the given query type function for the pad.
1060  */
1061 void
1062 gst_pad_set_query_type_function (GstPad * pad,
1063     GstPadQueryTypeFunction type_func)
1064 {
1065   g_return_if_fail (GST_IS_PAD (pad));
1066
1067   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1068
1069   GST_CAT_DEBUG (GST_CAT_PADS, "querytypefunc for %s:%s  set to %s",
1070       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (type_func));
1071 }
1072
1073 /**
1074  * gst_pad_get_query_types:
1075  * @pad: a #GstPad.
1076  *
1077  * Get an array of supported queries that can be performed
1078  * on this pad.
1079  *
1080  * Returns: a zero-terminated array of #GstQueryType.
1081  */
1082 const GstQueryType *
1083 gst_pad_get_query_types (GstPad * pad)
1084 {
1085   GstPadQueryTypeFunction func;
1086
1087   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1088
1089   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1090     goto no_func;
1091
1092   return func (pad);
1093
1094 no_func:
1095   {
1096     return NULL;
1097   }
1098 }
1099
1100 static gboolean
1101 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1102 {
1103   *data = gst_pad_get_query_types (pad);
1104
1105   return TRUE;
1106 }
1107
1108 /**
1109  * gst_pad_get_query_types_default:
1110  * @pad: a #GstPad.
1111  *
1112  * Invoke the default dispatcher for the query types on
1113  * the pad.
1114  *
1115  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1116  * internally-linked pads has a query types function.
1117  */
1118 const GstQueryType *
1119 gst_pad_get_query_types_default (GstPad * pad)
1120 {
1121   GstQueryType *result = NULL;
1122
1123   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1124
1125   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1126       gst_pad_get_query_types_dispatcher, &result);
1127
1128   return result;
1129 }
1130
1131 /**
1132  * gst_pad_set_internal_link_function:
1133  * @pad: a #GstPad of either direction.
1134  * @intlink: the #GstPadIntLinkFunction to set.
1135  *
1136  * Sets the given internal link function for the pad.
1137  */
1138 void
1139 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1140 {
1141   g_return_if_fail (GST_IS_PAD (pad));
1142
1143   GST_PAD_INTLINKFUNC (pad) = intlink;
1144   GST_CAT_DEBUG (GST_CAT_PADS, "internal link for %s:%s  set to %s",
1145       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (intlink));
1146 }
1147
1148 /**
1149  * gst_pad_set_link_function:
1150  * @pad: a #GstPad.
1151  * @link: the #GstPadLinkFunction to set.
1152  * 
1153  * Sets the given link function for the pad. It will be called when the pad is
1154  * linked or relinked with caps. The caps passed to the link function is
1155  * the caps for the connnection. It can contain a non fixed caps.
1156  * 
1157  * The return value GST_PAD_LINK_OK should be used when the connection can be
1158  * made.
1159  * 
1160  * The return value GST_PAD_LINK_REFUSED should be used when the connection
1161  * cannot be made for some reason.
1162  */
1163 void
1164 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1165 {
1166   g_return_if_fail (GST_IS_PAD (pad));
1167
1168   GST_PAD_LINKFUNC (pad) = link;
1169   GST_CAT_DEBUG (GST_CAT_PADS, "linkfunc for %s:%s set to %s",
1170       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (link));
1171 }
1172
1173 /**
1174  * gst_pad_set_unlink_function:
1175  * @pad: a #GstPad.
1176  * @unlink: the #GstPadUnlinkFunction to set.
1177  *
1178  * Sets the given unlink function for the pad. It will be called
1179  * when the pad is unlinked.
1180  */
1181 void
1182 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1183 {
1184   g_return_if_fail (GST_IS_PAD (pad));
1185
1186   GST_PAD_UNLINKFUNC (pad) = unlink;
1187   GST_CAT_DEBUG (GST_CAT_PADS, "unlinkfunc for %s:%s set to %s",
1188       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (unlink));
1189 }
1190
1191 /**
1192  * gst_pad_set_getcaps_function:
1193  * @pad: a #GstPad.
1194  * @getcaps: the #GstPadGetCapsFunction to set.
1195  * 
1196  * Sets the given getcaps function for the pad. @getcaps should return the
1197  * allowable caps for a pad in the context of the element's state, its link to
1198  * other elements, and the devices or files it has opened. These caps must be a
1199  * subset of the pad template caps. In the NULL state with no links, @getcaps
1200  * should ideally return the same caps as the pad template. In rare
1201  * circumstances, an object property can affect the caps returned by @getcaps,
1202  * but this is discouraged.
1203  *
1204  * You do not need to call this function if @pad's allowed caps are always the
1205  * same as the pad template caps. This can only be true if the padtemplate 
1206  * has fixed simple caps.
1207  *
1208  * For most filters, the caps returned by @getcaps is directly affected by the
1209  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1210  * the srcpad's getcaps function is directly related to the stream data. Again,
1211  * @getcaps should return the most specific caps it reasonably can, since this
1212  * helps with autoplugging. 
1213  *
1214  * Note that the return value from @getcaps is owned by the caller, so the caller
1215  * should unref the caps after usage.
1216  */
1217 void
1218 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1219 {
1220   g_return_if_fail (GST_IS_PAD (pad));
1221
1222   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1223   GST_CAT_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
1224       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
1225 }
1226
1227 /**
1228  * gst_pad_set_acceptcaps_function:
1229  * @pad: a #GstPad.
1230  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1231  *
1232  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1233  * will be called to check if the pad can accept the given caps.
1234  */
1235 void
1236 gst_pad_set_acceptcaps_function (GstPad * pad,
1237     GstPadAcceptCapsFunction acceptcaps)
1238 {
1239   g_return_if_fail (GST_IS_PAD (pad));
1240
1241   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1242   GST_CAT_DEBUG (GST_CAT_PADS, "acceptcapsfunc for %s:%s set to %s",
1243       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1244 }
1245
1246 /**
1247  * gst_pad_set_fixatecaps_function:
1248  * @pad: a #GstPad.
1249  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1250  *
1251  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1252  * will be called whenever the default values for a GstCaps needs to be
1253  * filled in.
1254  */
1255 void
1256 gst_pad_set_fixatecaps_function (GstPad * pad,
1257     GstPadFixateCapsFunction fixatecaps)
1258 {
1259   g_return_if_fail (GST_IS_PAD (pad));
1260
1261   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1262   GST_CAT_DEBUG (GST_CAT_PADS, "fixatecapsfunc for %s:%s set to %s",
1263       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1264 }
1265
1266 /**
1267  * gst_pad_set_setcaps_function:
1268  * @pad: a #GstPad.
1269  * @setcaps: the #GstPadSetCapsFunction to set.
1270  *
1271  * Sets the given setcaps function for the pad.  The setcaps function
1272  * will be called whenever a buffer with a new media type is pushed or
1273  * pulled from the pad. The pad/element needs to update it's internal
1274  * structures to process the new media type. If this new type is not
1275  * acceptable, the setcaps function should return FALSE.
1276  */
1277 void
1278 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1279 {
1280   g_return_if_fail (GST_IS_PAD (pad));
1281
1282   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1283   GST_CAT_DEBUG (GST_CAT_PADS, "setcapsfunc for %s:%s set to %s",
1284       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (setcaps));
1285 }
1286
1287 /**
1288  * gst_pad_set_bufferalloc_function:
1289  * @pad: a sink #GstPad.
1290  * @bufalloc: the #GstPadBufferAllocFunction to set.
1291  *
1292  * Sets the given bufferalloc function for the pad. Note that the
1293  * bufferalloc function can only be set on sinkpads.
1294  */
1295 void
1296 gst_pad_set_bufferalloc_function (GstPad * pad,
1297     GstPadBufferAllocFunction bufalloc)
1298 {
1299   g_return_if_fail (GST_IS_PAD (pad));
1300   g_return_if_fail (GST_PAD_IS_SINK (pad));
1301
1302   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1303   GST_CAT_DEBUG (GST_CAT_PADS, "bufferallocfunc for %s:%s set to %s",
1304       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufalloc));
1305 }
1306
1307 /**
1308  * gst_pad_unlink:
1309  * @srcpad: the source #GstPad to unlink.
1310  * @sinkpad: the sink #GstPad to unlink.
1311  *
1312  * Unlinks the source pad from the sink pad. Will emit the "unlinked" signal on
1313  * both pads.
1314  *
1315  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1316  * the pads were not linked together.
1317  *
1318  * MT safe.
1319  */
1320 gboolean
1321 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1322 {
1323   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1324   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1325
1326   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1327       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1328       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1329
1330   GST_LOCK (srcpad);
1331
1332   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1333     goto not_srcpad;
1334
1335   GST_LOCK (sinkpad);
1336
1337   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1338     goto not_sinkpad;
1339
1340   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1341     goto not_linked_together;
1342
1343   if (GST_PAD_UNLINKFUNC (srcpad)) {
1344     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1345   }
1346   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1347     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1348   }
1349
1350   /* first clear peers */
1351   GST_PAD_PEER (srcpad) = NULL;
1352   GST_PAD_PEER (sinkpad) = NULL;
1353
1354   GST_UNLOCK (sinkpad);
1355   GST_UNLOCK (srcpad);
1356
1357   /* fire off a signal to each of the pads telling them 
1358    * that they've been unlinked */
1359   g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1360   g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1361
1362   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1363       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1364
1365   return TRUE;
1366
1367 not_srcpad:
1368   {
1369     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1370     GST_UNLOCK (srcpad);
1371     return FALSE;
1372   }
1373 not_sinkpad:
1374   {
1375     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1376     GST_UNLOCK (sinkpad);
1377     GST_UNLOCK (srcpad);
1378     return FALSE;
1379   }
1380 not_linked_together:
1381   {
1382     /* we do not emit a warning in this case because unlinking cannot
1383      * be made MT safe.*/
1384     GST_UNLOCK (sinkpad);
1385     GST_UNLOCK (srcpad);
1386     return FALSE;
1387   }
1388 }
1389
1390 /**
1391  * gst_pad_is_linked:
1392  * @pad: pad to check
1393  *
1394  * Checks if a @pad is linked to another pad or not.
1395  *
1396  * Returns: TRUE if the pad is linked, FALSE otherwise.
1397  *
1398  * MT safe.
1399  */
1400 gboolean
1401 gst_pad_is_linked (GstPad * pad)
1402 {
1403   gboolean result;
1404
1405   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1406
1407   GST_LOCK (pad);
1408   result = (GST_PAD_PEER (pad) != NULL);
1409   GST_UNLOCK (pad);
1410
1411   return result;
1412 }
1413
1414 /* get the caps from both pads and see if the intersection
1415  * is not empty.
1416  *
1417  * This function should be called with the pad LOCK on both
1418  * pads
1419  */
1420 static gboolean
1421 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1422 {
1423   GstCaps *srccaps;
1424   GstCaps *sinkcaps;
1425
1426   srccaps = gst_pad_get_caps_unlocked (src);
1427   sinkcaps = gst_pad_get_caps_unlocked (sink);
1428   GST_CAT_DEBUG (GST_CAT_CAPS, " src caps %" GST_PTR_FORMAT, srccaps);
1429   GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1430
1431   /* if we have caps on both pads we can check the intersection */
1432   if (srccaps && sinkcaps) {
1433     GstCaps *icaps;
1434
1435     icaps = gst_caps_intersect (srccaps, sinkcaps);
1436     gst_caps_unref (srccaps);
1437     gst_caps_unref (sinkcaps);
1438
1439     GST_CAT_DEBUG (GST_CAT_CAPS,
1440         "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1441
1442     if (!icaps || gst_caps_is_empty (icaps)) {
1443       GST_CAT_DEBUG (GST_CAT_CAPS, "intersection is empty");
1444       gst_caps_unref (icaps);
1445       return FALSE;
1446     }
1447     gst_caps_unref (icaps);
1448   }
1449
1450   return TRUE;
1451 }
1452
1453 /* check if the grandparents of both pads are the same.
1454  * This check is required so that we don't try to link
1455  * pads from elements in different bins without ghostpads.
1456  *
1457  * The LOCK should be helt on both pads
1458  */
1459 static gboolean
1460 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1461 {
1462   GstObject *psrc, *psink;
1463   gboolean res = TRUE;
1464
1465   psrc = GST_OBJECT_PARENT (src);
1466   psink = GST_OBJECT_PARENT (sink);
1467
1468   /* if one of the pads has no parent, we allow the link */
1469   if (psrc && psink) {
1470     /* if the parents are the same, we have a loop */
1471     if (psrc == psink) {
1472       GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1473           psrc);
1474       res = FALSE;
1475       goto done;
1476     }
1477     /* if they both have a parent, we check the grandparents */
1478     psrc = gst_object_get_parent (psrc);
1479     psink = gst_object_get_parent (psink);
1480
1481     if (psrc != psink) {
1482       /* if they have grandparents but they are not the same */
1483       GST_CAT_DEBUG (GST_CAT_CAPS,
1484           "pads have different grandparents %" GST_PTR_FORMAT " and %"
1485           GST_PTR_FORMAT, psrc, psink);
1486       res = FALSE;
1487     }
1488     if (psrc)
1489       gst_object_unref (psrc);
1490     if (psink)
1491       gst_object_unref (psink);
1492   }
1493 done:
1494   return res;
1495 }
1496
1497 /* FIXME leftover from an attempt at refactoring... */
1498 /* call with the two pads unlocked */
1499 static GstPadLinkReturn
1500 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1501 {
1502   /* generic checks */
1503   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1504   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1505
1506   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1507       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1508
1509   GST_LOCK (srcpad);
1510
1511   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1512     goto not_srcpad;
1513
1514   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1515     goto src_was_linked;
1516
1517   GST_LOCK (sinkpad);
1518
1519   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1520     goto not_sinkpad;
1521
1522   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1523     goto sink_was_linked;
1524
1525   /* check hierarchy, pads can only be linked if the grandparents
1526    * are the same. */
1527   if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1528     goto wrong_hierarchy;
1529
1530   /* check pad caps for non-empty intersection */
1531   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1532     goto no_format;
1533
1534   /* FIXME check pad scheduling for non-empty intersection */
1535
1536   return GST_PAD_LINK_OK;
1537
1538 not_srcpad:
1539   {
1540     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1541     GST_UNLOCK (srcpad);
1542     return GST_PAD_LINK_WRONG_DIRECTION;
1543   }
1544 src_was_linked:
1545   {
1546     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was linked",
1547         GST_DEBUG_PAD_NAME (srcpad));
1548     /* we do not emit a warning in this case because unlinking cannot
1549      * be made MT safe.*/
1550     GST_UNLOCK (srcpad);
1551     return GST_PAD_LINK_WAS_LINKED;
1552   }
1553 not_sinkpad:
1554   {
1555     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1556     GST_UNLOCK (sinkpad);
1557     GST_UNLOCK (srcpad);
1558     return GST_PAD_LINK_WRONG_DIRECTION;
1559   }
1560 sink_was_linked:
1561   {
1562     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was linked",
1563         GST_DEBUG_PAD_NAME (sinkpad));
1564     /* we do not emit a warning in this case because unlinking cannot
1565      * be made MT safe.*/
1566     GST_UNLOCK (sinkpad);
1567     GST_UNLOCK (srcpad);
1568     return GST_PAD_LINK_WAS_LINKED;
1569   }
1570 wrong_hierarchy:
1571   {
1572     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1573     GST_UNLOCK (sinkpad);
1574     GST_UNLOCK (srcpad);
1575     return GST_PAD_LINK_WRONG_HIERARCHY;
1576   }
1577 no_format:
1578   {
1579     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1580     GST_UNLOCK (sinkpad);
1581     GST_UNLOCK (srcpad);
1582     return GST_PAD_LINK_NOFORMAT;
1583   }
1584 }
1585
1586 /**
1587  * gst_pad_link:
1588  * @srcpad: the source #GstPad to link.
1589  * @sinkpad: the sink #GstPad to link.
1590  *
1591  * Links the source pad and the sink pad.
1592  *
1593  * Returns: A result code indicating if the connection worked or
1594  *          what went wrong.
1595  *
1596  * MT Safe.
1597  */
1598 GstPadLinkReturn
1599 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1600 {
1601   GstPadLinkReturn result;
1602
1603   /* prepare will also lock the two pads */
1604   result = gst_pad_link_prepare (srcpad, sinkpad);
1605
1606   if (result != GST_PAD_LINK_OK)
1607     goto prepare_failed;
1608
1609   GST_UNLOCK (sinkpad);
1610   GST_UNLOCK (srcpad);
1611
1612   /* FIXME released the locks here, concurrent thread might link
1613    * something else. */
1614   if (GST_PAD_LINKFUNC (srcpad)) {
1615     /* this one will call the peer link function */
1616     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
1617   } else if (GST_PAD_LINKFUNC (sinkpad)) {
1618     /* if no source link function, we need to call the sink link
1619      * function ourselves. */
1620     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
1621   } else {
1622     result = GST_PAD_LINK_OK;
1623   }
1624
1625   GST_LOCK (srcpad);
1626   GST_LOCK (sinkpad);
1627
1628   if (result == GST_PAD_LINK_OK) {
1629     GST_PAD_PEER (srcpad) = sinkpad;
1630     GST_PAD_PEER (sinkpad) = srcpad;
1631
1632     GST_UNLOCK (sinkpad);
1633     GST_UNLOCK (srcpad);
1634
1635     /* fire off a signal to each of the pads telling them
1636      * that they've been linked */
1637     g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
1638     g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
1639
1640     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1641         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1642   } else {
1643     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1644         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1645
1646     GST_UNLOCK (sinkpad);
1647     GST_UNLOCK (srcpad);
1648   }
1649   return result;
1650
1651 prepare_failed:
1652   {
1653     return result;
1654   }
1655 }
1656
1657 static void
1658 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1659 {
1660   /* this function would need checks if it weren't static */
1661
1662   GST_LOCK (pad);
1663   gst_object_replace ((GstObject **) & pad->padtemplate, (GstObject *) templ);
1664   GST_UNLOCK (pad);
1665
1666   if (templ)
1667     gst_pad_template_pad_created (templ, pad);
1668 }
1669
1670 /**
1671  * gst_pad_get_pad_template:
1672  * @pad: a #GstPad.
1673  *
1674  * Gets the template for @pad.
1675  *
1676  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
1677  * if this pad has no template.
1678  *
1679  * FIXME: currently returns an unrefcounted padtemplate.
1680  */
1681 GstPadTemplate *
1682 gst_pad_get_pad_template (GstPad * pad)
1683 {
1684   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1685
1686   return GST_PAD_PAD_TEMPLATE (pad);
1687 }
1688
1689
1690 /* should be called with the pad LOCK held */
1691 /* refs the caps, so caller is responsible for getting it unreffed */
1692 static GstCaps *
1693 gst_pad_get_caps_unlocked (GstPad * pad)
1694 {
1695   GstCaps *result = NULL;
1696
1697   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1698       GST_DEBUG_PAD_NAME (pad), pad);
1699
1700   if (GST_PAD_GETCAPSFUNC (pad)) {
1701     GST_CAT_DEBUG (GST_CAT_CAPS, "dispatching to pad getcaps function");
1702
1703     GST_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
1704     GST_UNLOCK (pad);
1705     result = GST_PAD_GETCAPSFUNC (pad) (pad);
1706     GST_LOCK (pad);
1707     GST_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
1708
1709     if (result == NULL) {
1710       g_critical ("pad %s:%s returned NULL caps from getcaps function",
1711           GST_DEBUG_PAD_NAME (pad));
1712     } else {
1713       GST_CAT_DEBUG (GST_CAT_CAPS,
1714           "pad getcaps %s:%s returned %" GST_PTR_FORMAT,
1715           GST_DEBUG_PAD_NAME (pad), result);
1716 #ifndef G_DISABLE_ASSERT
1717       /* check that the returned caps are a real subset of the template caps */
1718       if (GST_PAD_PAD_TEMPLATE (pad)) {
1719         const GstCaps *templ_caps =
1720             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1721         if (!gst_caps_is_subset (result, templ_caps)) {
1722           GstCaps *temp;
1723
1724           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
1725               "pad returned caps %" GST_PTR_FORMAT
1726               " which are not a real subset of its template caps %"
1727               GST_PTR_FORMAT, result, templ_caps);
1728           g_warning
1729               ("pad %s:%s returned caps that are not a real subset of its template caps",
1730               GST_DEBUG_PAD_NAME (pad));
1731           temp = gst_caps_intersect (templ_caps, result);
1732           gst_caps_unref (result);
1733           result = temp;
1734         }
1735       }
1736 #endif
1737       goto done;
1738     }
1739   }
1740   if (GST_PAD_PAD_TEMPLATE (pad)) {
1741     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1742
1743     result = GST_PAD_TEMPLATE_CAPS (templ);
1744     GST_CAT_DEBUG (GST_CAT_CAPS,
1745         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1746         result);
1747
1748     result = gst_caps_ref (result);
1749     goto done;
1750   }
1751   if (GST_PAD_CAPS (pad)) {
1752     result = GST_PAD_CAPS (pad);
1753
1754     GST_CAT_DEBUG (GST_CAT_CAPS,
1755         "using pad caps %p %" GST_PTR_FORMAT, result, result);
1756
1757     result = gst_caps_ref (result);
1758     goto done;
1759   }
1760
1761   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1762   result = gst_caps_new_empty ();
1763
1764 done:
1765   return result;
1766 }
1767
1768 /**
1769  * gst_pad_get_caps:
1770  * @pad: a  #GstPad to get the capabilities of.
1771  *
1772  * Gets the capabilities this pad can produce or consume.
1773  * Note that this method doesn't necessarily returns the caps set by
1774  * #gst_pad_set_caps - use #GST_PAD_CAPS for that instead.
1775  * gst_pad_get_caps returns all possible caps a pad can operate with, using
1776  * the pad's get_caps function;
1777  * this returns the pad template caps if not explicitly set.
1778  *
1779  * Returns: a newly allocated copy of the #GstCaps of this pad.
1780  *
1781  * MT safe.
1782  */
1783 GstCaps *
1784 gst_pad_get_caps (GstPad * pad)
1785 {
1786   GstCaps *result = NULL;
1787
1788   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1789
1790   GST_LOCK (pad);
1791
1792   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1793       GST_DEBUG_PAD_NAME (pad), pad);
1794
1795   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (pad)))
1796     goto was_dispatching;
1797
1798   result = gst_pad_get_caps_unlocked (pad);
1799   GST_UNLOCK (pad);
1800
1801   return result;
1802
1803 was_dispatching:
1804   {
1805     GST_CAT_DEBUG (GST_CAT_CAPS,
1806         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1807     g_warning ("pad %s:%s recursively called getcaps!",
1808         GST_DEBUG_PAD_NAME (pad));
1809     GST_UNLOCK (pad);
1810     return NULL;
1811   }
1812 }
1813
1814 /**
1815  * gst_pad_peer_get_caps:
1816  * @pad: a  #GstPad to get the peer capabilities of.
1817  *
1818  * Gets the capabilities of the peer connected to this pad.
1819  *
1820  * Returns: the #GstCaps of the peer pad. This function returns a new caps, so use 
1821  * gst_caps_unref to get rid of it. this function returns NULL if there is no
1822  * peer pad or when this function is called recursively from a getcaps function.
1823  */
1824 GstCaps *
1825 gst_pad_peer_get_caps (GstPad * pad)
1826 {
1827   GstPad *peerpad;
1828   GstCaps *result = NULL;
1829
1830   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1831
1832   GST_LOCK (pad);
1833
1834   GST_CAT_DEBUG (GST_CAT_CAPS, "get peer caps of %s:%s (%p)",
1835       GST_DEBUG_PAD_NAME (pad), pad);
1836
1837   peerpad = GST_PAD_PEER (pad);
1838   if (G_UNLIKELY (peerpad == NULL))
1839     goto no_peer;
1840
1841   if (G_UNLIKELY (GST_PAD_IS_IN_GETCAPS (peerpad)))
1842     goto was_dispatching;
1843
1844   gst_object_ref (peerpad);
1845   GST_UNLOCK (pad);
1846
1847   result = gst_pad_get_caps (peerpad);
1848
1849   gst_object_unref (peerpad);
1850
1851   return result;
1852
1853 no_peer:
1854   {
1855     GST_UNLOCK (pad);
1856     return NULL;
1857   }
1858 was_dispatching:
1859   {
1860     GST_CAT_DEBUG (GST_CAT_CAPS,
1861         "pad %s:%s is already dispatching!", GST_DEBUG_PAD_NAME (pad));
1862     g_warning ("pad %s:%s recursively called getcaps!",
1863         GST_DEBUG_PAD_NAME (pad));
1864     GST_UNLOCK (pad);
1865     return NULL;
1866   }
1867 }
1868
1869 static gboolean
1870 fixate_value (GValue * dest, const GValue * src)
1871 {
1872   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
1873     g_value_init (dest, G_TYPE_INT);
1874     g_value_set_int (dest, gst_value_get_int_range_min (src));
1875   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
1876     g_value_init (dest, G_TYPE_DOUBLE);
1877     g_value_set_double (dest, gst_value_get_double_range_min (src));
1878   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1879     GValue temp = { 0 };
1880
1881     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
1882     if (!fixate_value (dest, &temp))
1883       gst_value_init_and_copy (dest, &temp);
1884     g_value_unset (&temp);
1885   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
1886     gboolean res = FALSE;
1887     gint n;
1888
1889     g_value_init (dest, GST_TYPE_ARRAY);
1890     for (n = 0; n < gst_value_list_get_size (src); n++) {
1891       GValue kid = { 0 };
1892       const GValue *orig_kid = gst_value_list_get_value (src, n);
1893
1894       if (!fixate_value (&kid, orig_kid))
1895         gst_value_init_and_copy (&kid, orig_kid);
1896       else
1897         res = TRUE;
1898       gst_value_list_append_value (dest, &kid);
1899       g_value_unset (&kid);
1900     }
1901
1902     if (!res)
1903       g_value_unset (dest);
1904
1905     return res;
1906   } else {
1907     return FALSE;
1908   }
1909
1910   return TRUE;
1911 }
1912
1913 static gboolean
1914 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
1915 {
1916   GstStructure *s = data;
1917   GValue v = { 0 };
1918
1919   if (fixate_value (&v, value)) {
1920     gst_structure_id_set_value (s, field_id, &v);
1921     g_value_unset (&v);
1922   }
1923
1924   return TRUE;
1925 }
1926
1927 /**
1928  * gst_pad_fixate_caps:
1929  * @pad: a  #GstPad to fixate
1930  * @caps: the  #GstCaps to fixate
1931  *
1932  * Fixate a caps on the given pad. Modifies the caps in place, so you should
1933  * make sure that the caps are actually writable (see gst_caps_make_writable()).
1934  */
1935 void
1936 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
1937 {
1938   GstPadFixateCapsFunction fixatefunc;
1939   gint n;
1940
1941   g_return_if_fail (GST_IS_PAD (pad));
1942   g_return_if_fail (caps != NULL);
1943
1944   if (gst_caps_is_fixed (caps))
1945     return;
1946
1947   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
1948   if (fixatefunc) {
1949     fixatefunc (pad, caps);
1950   }
1951
1952   /* default fixation */
1953   for (n = 0; n < gst_caps_get_size (caps); n++) {
1954     GstStructure *s = gst_caps_get_structure (caps, n);
1955
1956     gst_structure_foreach (s, gst_pad_default_fixate, s);
1957   }
1958 }
1959
1960 /**
1961  * gst_pad_accept_caps:
1962  * @pad: a #GstPad to check
1963  * @caps: a #GstCaps to check on the pad
1964  *
1965  * Check if the given pad accepts the caps.
1966  *
1967  * Returns: TRUE if the pad can accept the caps.
1968  */
1969 gboolean
1970 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
1971 {
1972   gboolean result;
1973   GstPadAcceptCapsFunction acceptfunc;
1974
1975   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1976
1977   /* any pad can be unnegotiated */
1978   if (caps == NULL)
1979     return TRUE;
1980
1981   GST_LOCK (pad);
1982   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
1983
1984   GST_CAT_DEBUG (GST_CAT_CAPS, "pad accept caps of %s:%s (%p)",
1985       GST_DEBUG_PAD_NAME (pad), pad);
1986   GST_UNLOCK (pad);
1987
1988   if (acceptfunc) {
1989     /* we can call the function */
1990     result = acceptfunc (pad, caps);
1991   } else {
1992     /* else see get the caps and see if it intersects to something
1993      * not empty */
1994     GstCaps *intersect;
1995     GstCaps *allowed;
1996
1997     allowed = gst_pad_get_caps (pad);
1998     if (allowed) {
1999       intersect = gst_caps_intersect (allowed, caps);
2000
2001       result = !gst_caps_is_empty (intersect);
2002
2003       gst_caps_unref (allowed);
2004       gst_caps_unref (intersect);
2005     } else {
2006       result = FALSE;
2007     }
2008   }
2009   return result;
2010 }
2011
2012 /**
2013  * gst_pad_peer_accept_caps:
2014  * @pad: a  #GstPad to check
2015  * @caps: a #GstCaps to check on the pad
2016  *
2017  * Check if the given pad accepts the caps.
2018  *
2019  * Returns: TRUE if the pad can accept the caps.
2020  */
2021 gboolean
2022 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2023 {
2024   GstPad *peerpad;
2025   gboolean result;
2026
2027   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2028
2029   GST_LOCK (pad);
2030
2031   GST_CAT_DEBUG (GST_CAT_CAPS, "peer accept caps of %s:%s (%p)",
2032       GST_DEBUG_PAD_NAME (pad), pad);
2033
2034   peerpad = GST_PAD_PEER (pad);
2035   if (G_UNLIKELY (peerpad == NULL))
2036     goto no_peer;
2037
2038   result = gst_pad_accept_caps (peerpad, caps);
2039   GST_UNLOCK (pad);
2040
2041   return result;
2042
2043 no_peer:
2044   {
2045     GST_UNLOCK (pad);
2046     return TRUE;
2047   }
2048 }
2049
2050 /**
2051  * gst_pad_set_caps:
2052  * @pad: a  #GstPad to set the capabilities of.
2053  * @caps: a #GstCaps to set.
2054  *
2055  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2056  * caps on the pad will be unreffed. This function refs the caps so you should
2057  * unref if as soon as you don't need it anymore.
2058  * It is possible to set NULL caps, which will make the pad unnegotiated
2059  * again.
2060  *
2061  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2062  * or bad parameters were provided to this function.
2063  *
2064  * MT safe.
2065  */
2066 gboolean
2067 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2068 {
2069   GstPadSetCapsFunction setcaps;
2070   GstCaps *existing;
2071
2072   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2073   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2074
2075   GST_LOCK (pad);
2076   setcaps = GST_PAD_SETCAPSFUNC (pad);
2077
2078   existing = GST_PAD_CAPS (pad);
2079   if (caps == existing)
2080     goto setting_same_caps;
2081   else if (caps && existing && gst_caps_is_equal (caps, existing))
2082     goto setting_same_caps;
2083
2084   /* call setcaps function to configure the pad */
2085   if (setcaps != NULL && caps) {
2086     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2087       GST_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2088       GST_UNLOCK (pad);
2089       if (!setcaps (pad, caps))
2090         goto could_not_set;
2091       GST_LOCK (pad);
2092       GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2093     } else {
2094       GST_CAT_DEBUG (GST_CAT_CAPS, "pad %s:%s was dispatching",
2095           GST_DEBUG_PAD_NAME (pad));
2096     }
2097   }
2098
2099   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2100   GST_CAT_DEBUG (GST_CAT_CAPS, "%s:%s caps %" GST_PTR_FORMAT,
2101       GST_DEBUG_PAD_NAME (pad), caps);
2102   GST_UNLOCK (pad);
2103
2104   g_object_notify (G_OBJECT (pad), "caps");
2105
2106   return TRUE;
2107
2108 setting_same_caps:
2109   {
2110     GST_UNLOCK (pad);
2111     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2112     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2113         "caps %" GST_PTR_FORMAT " same as existing, updating ptr only", caps);
2114     return TRUE;
2115   }
2116 /* errors */
2117 could_not_set:
2118   {
2119     GST_LOCK (pad);
2120     GST_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2121     GST_CAT_DEBUG (GST_CAT_CAPS,
2122         "pad %s:%s, caps %" GST_PTR_FORMAT " could not be set",
2123         GST_DEBUG_PAD_NAME (pad), caps);
2124     GST_UNLOCK (pad);
2125
2126     return FALSE;
2127   }
2128 }
2129
2130 static gboolean
2131 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2132 {
2133   GstPadAcceptCapsFunction acceptcaps;
2134   GstPadSetCapsFunction setcaps;
2135   gboolean res;
2136
2137   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2138   setcaps = GST_PAD_SETCAPSFUNC (pad);
2139
2140   /* See if pad accepts the caps, by calling acceptcaps, only
2141    * needed if no setcaps function */
2142   if (setcaps == NULL && acceptcaps != NULL) {
2143     if (!acceptcaps (pad, caps))
2144       goto not_accepted;
2145   }
2146   /* set caps on pad if call succeeds */
2147   res = gst_pad_set_caps (pad, caps);
2148   /* no need to unref the caps here, set_caps takes a ref and
2149    * our ref goes away when we leave this function. */
2150
2151   return res;
2152
2153 not_accepted:
2154   {
2155     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2156     return FALSE;
2157   }
2158 }
2159
2160 /* returns TRUE if the src pad could be configured to accept the given caps */
2161 static gboolean
2162 gst_pad_configure_src (GstPad * pad, GstCaps * caps)
2163 {
2164   GstPadAcceptCapsFunction acceptcaps;
2165   GstPadSetCapsFunction setcaps;
2166   gboolean res;
2167
2168   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2169   setcaps = GST_PAD_SETCAPSFUNC (pad);
2170
2171   /* See if pad accepts the caps, by calling acceptcaps, only
2172    * needed if no setcaps function */
2173   if (setcaps == NULL && acceptcaps != NULL) {
2174     if (!acceptcaps (pad, caps))
2175       goto not_accepted;
2176   }
2177   /* set caps on pad if call succeeds */
2178   res = gst_pad_set_caps (pad, caps);
2179   /* no need to unref the caps here, set_caps takes a ref and
2180    * our ref goes away when we leave this function. */
2181
2182   return res;
2183
2184 not_accepted:
2185   {
2186     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2187     return FALSE;
2188   }
2189 }
2190
2191 /**
2192  * gst_pad_get_pad_template_caps:
2193  * @pad: a #GstPad to get the template capabilities from.
2194  *
2195  * Gets the capabilities for @pad's template.
2196  *
2197  * Returns: the #GstCaps of this pad template. If you intend to keep a reference
2198  * on the caps, make a copy (see gst_caps_copy ()).
2199  */
2200 const GstCaps *
2201 gst_pad_get_pad_template_caps (GstPad * pad)
2202 {
2203   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2204
2205   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2206
2207   if (GST_PAD_PAD_TEMPLATE (pad))
2208     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2209
2210   return gst_static_caps_get (&anycaps);
2211 }
2212
2213
2214 /**
2215  * gst_pad_get_peer:
2216  * @pad: a #GstPad to get the peer of.
2217  *
2218  * Gets the peer of @pad. This function refs the peer pad so
2219  * you need to unref it after use.
2220  *
2221  * Returns: the peer #GstPad. Unref after usage.
2222  *
2223  * MT safe.
2224  */
2225 GstPad *
2226 gst_pad_get_peer (GstPad * pad)
2227 {
2228   GstPad *result;
2229
2230   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2231
2232   GST_LOCK (pad);
2233   result = GST_PAD_PEER (pad);
2234   if (result)
2235     gst_object_ref (result);
2236   GST_UNLOCK (pad);
2237
2238   return result;
2239 }
2240
2241 /**
2242  * gst_pad_get_allowed_caps:
2243  * @srcpad: a #GstPad, it must a a source pad.
2244  *
2245  * Gets the capabilities of the allowed media types that can flow through
2246  * @srcpad and its peer. The pad must be a source pad.
2247  * The caller must free the resulting caps.
2248  *
2249  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2250  * you no longer need it. This function returns NULL when the @srcpad has no
2251  * peer.
2252  *
2253  * MT safe.
2254  */
2255 GstCaps *
2256 gst_pad_get_allowed_caps (GstPad * srcpad)
2257 {
2258   GstCaps *mycaps;
2259   GstCaps *caps;
2260   GstCaps *peercaps;
2261   GstPad *peer;
2262
2263   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
2264   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
2265
2266   GST_LOCK (srcpad);
2267
2268   peer = GST_PAD_PEER (srcpad);
2269   if (G_UNLIKELY (peer == NULL))
2270     goto no_peer;
2271
2272   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting allowed caps",
2273       GST_DEBUG_PAD_NAME (srcpad));
2274
2275   gst_object_ref (peer);
2276   GST_UNLOCK (srcpad);
2277   mycaps = gst_pad_get_caps (srcpad);
2278
2279   peercaps = gst_pad_get_caps (peer);
2280   gst_object_unref (peer);
2281
2282   caps = gst_caps_intersect (mycaps, peercaps);
2283   gst_caps_unref (peercaps);
2284   gst_caps_unref (mycaps);
2285
2286   GST_CAT_DEBUG (GST_CAT_CAPS, "allowed caps %" GST_PTR_FORMAT, caps);
2287
2288   return caps;
2289
2290 no_peer:
2291   {
2292     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2293         GST_DEBUG_PAD_NAME (srcpad));
2294     GST_UNLOCK (srcpad);
2295
2296     return NULL;
2297   }
2298 }
2299
2300 /**
2301  * gst_pad_get_negotiated_caps:
2302  * @pad: a #GstPad.
2303  *
2304  * Gets the capabilities of the media type that currently flows through @pad
2305  * and its peer.
2306  *
2307  * This function can be used on both src and sinkpads. Note that srcpads are
2308  * always negotiated before sinkpads so it is possible that the negotiated caps
2309  * on the srcpad do not match the negotiated caps of the peer.
2310  *
2311  * Returns: the negotiated #GstCaps of the pad link.  Free the caps when
2312  * you no longer need it. This function returns NULL when the @pad has no
2313  * peer or is not negotiated yet.
2314  *
2315  * MT safe.
2316  */
2317 GstCaps *
2318 gst_pad_get_negotiated_caps (GstPad * pad)
2319 {
2320   GstCaps *caps;
2321   GstPad *peer;
2322
2323   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2324
2325   GST_LOCK (pad);
2326
2327   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2328     goto no_peer;
2329
2330   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting negotiated caps",
2331       GST_DEBUG_PAD_NAME (pad));
2332
2333   caps = GST_PAD_CAPS (pad);
2334   if (caps)
2335     gst_caps_ref (caps);
2336   GST_UNLOCK (pad);
2337
2338   GST_CAT_DEBUG (GST_CAT_CAPS, "negotiated caps %" GST_PTR_FORMAT, caps);
2339
2340   return caps;
2341
2342 no_peer:
2343   {
2344     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2345         GST_DEBUG_PAD_NAME (pad));
2346     GST_UNLOCK (pad);
2347
2348     return NULL;
2349   }
2350 }
2351
2352 /**
2353  * gst_pad_alloc_buffer:
2354  * @pad: a source #GstPad
2355  * @offset: the offset of the new buffer in the stream
2356  * @size: the size of the new buffer
2357  * @caps: the caps of the new buffer
2358  * @buf: a newly allocated buffer
2359  *
2360  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2361  * function only works if @pad is a source pad and has a peer.
2362  *
2363  * You need to check the caps of the buffer after performing this
2364  * function and renegotiate to the format if needed.
2365  *
2366  * A new, empty #GstBuffer will be put in the @buf argument.
2367  *
2368  * Returns: a result code indicating success of the operation. Any
2369  * result code other than GST_FLOW_OK is an error and @buf should
2370  * not be used.
2371  * An error can occur if the pad is not connected or when the downstream
2372  * peer elements cannot provide an acceptable buffer.
2373  *
2374  * MT safe.
2375  */
2376 GstFlowReturn
2377 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
2378     GstBuffer ** buf)
2379 {
2380   GstPad *peer;
2381   GstFlowReturn ret;
2382   GstPadBufferAllocFunction bufferallocfunc;
2383   gboolean caps_changed;
2384
2385   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2386   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2387   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2388
2389   GST_LOCK (pad);
2390   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2391     handle_pad_block (pad);
2392
2393   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2394     goto no_peer;
2395
2396   gst_object_ref (peer);
2397   GST_UNLOCK (pad);
2398
2399   if (G_LIKELY ((bufferallocfunc = peer->bufferallocfunc) == NULL))
2400     goto fallback;
2401
2402   GST_LOCK (peer);
2403   /* when the peer is flushing we cannot give a buffer */
2404   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (peer)))
2405     goto flushing;
2406
2407   if (offset == GST_BUFFER_OFFSET_NONE) {
2408     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2409         "calling bufferallocfunc &%s (@%p) of peer pad %s:%s for size %d offset NONE",
2410         GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2411         &bufferallocfunc, GST_DEBUG_PAD_NAME (peer), size);
2412   } else {
2413     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2414         "calling bufferallocfunc &%s (@%p) of peer pad %s:%s for size %d offset %"
2415         G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2416         &bufferallocfunc, GST_DEBUG_PAD_NAME (peer), size, offset);
2417   }
2418   GST_UNLOCK (peer);
2419
2420   ret = bufferallocfunc (peer, offset, size, caps, buf);
2421
2422   if (G_UNLIKELY (ret != GST_FLOW_OK))
2423     goto peer_error;
2424   if (G_UNLIKELY (*buf == NULL))
2425     goto fallback;
2426
2427   /* If the buffer alloc function didn't set up the caps like it should,
2428    * do it for it */
2429   if (caps && (GST_BUFFER_CAPS (*buf) == NULL)) {
2430     GST_WARNING ("Buffer allocation function for pad % " GST_PTR_FORMAT
2431         " did not set up caps. Setting", peer);
2432
2433     gst_buffer_set_caps (*buf, caps);
2434   }
2435
2436 do_caps:
2437   gst_object_unref (peer);
2438
2439   /* FIXME, move capnego this into a base class? */
2440   caps = GST_BUFFER_CAPS (*buf);
2441   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2442   /* we got a new datatype on the pad, see if it can handle it */
2443   if (G_UNLIKELY (caps_changed)) {
2444     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
2445     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps)))
2446       goto not_negotiated;
2447   }
2448   return ret;
2449
2450 no_peer:
2451   {
2452     /* pad has no peer */
2453     GST_CAT_DEBUG (GST_CAT_PADS,
2454         "%s:%s called bufferallocfunc but had no peer",
2455         GST_DEBUG_PAD_NAME (pad));
2456     GST_UNLOCK (pad);
2457     return GST_FLOW_NOT_LINKED;
2458   }
2459 flushing:
2460   {
2461     /* peer was flushing */
2462     GST_UNLOCK (peer);
2463     gst_object_unref (peer);
2464     GST_CAT_DEBUG (GST_CAT_PADS,
2465         "%s:%s called bufferallocfunc but peer was flushing",
2466         GST_DEBUG_PAD_NAME (pad));
2467     return GST_FLOW_WRONG_STATE;
2468   }
2469   /* fallback case, allocate a buffer of our own, add pad caps. */
2470 fallback:
2471   {
2472     GST_CAT_DEBUG (GST_CAT_PADS,
2473         "%s:%s fallback buffer alloc", GST_DEBUG_PAD_NAME (pad));
2474     *buf = gst_buffer_new_and_alloc (size);
2475     GST_BUFFER_OFFSET (*buf) = offset;
2476     gst_buffer_set_caps (*buf, caps);
2477
2478     ret = GST_FLOW_OK;
2479
2480     goto do_caps;
2481   }
2482 not_negotiated:
2483   {
2484     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2485         "alloc function retured unacceptable buffer");
2486     return GST_FLOW_NOT_NEGOTIATED;
2487   }
2488 peer_error:
2489   {
2490     gst_object_unref (peer);
2491     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2492         "alloc function retured error %s", gst_flow_get_name (ret));
2493     return ret;
2494   }
2495 }
2496
2497 /**
2498  * gst_pad_get_internal_links_default:
2499  * @pad: the #GstPad to get the internal links of.
2500  *
2501  * Gets a list of pads to which the given pad is linked to
2502  * inside of the parent element.
2503  * This is the default handler, and thus returns a list of all of the
2504  * pads inside the parent element with opposite direction.
2505  * The caller must free this list after use.
2506  *
2507  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
2508  *
2509  * Not MT safe.
2510  */
2511 GList *
2512 gst_pad_get_internal_links_default (GstPad * pad)
2513 {
2514   GList *res = NULL;
2515   GstElement *parent;
2516   GList *parent_pads;
2517   GstPadDirection direction;
2518
2519   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2520
2521   direction = pad->direction;
2522
2523   parent = GST_PAD_PARENT (pad);
2524   if (!parent)
2525     return NULL;
2526
2527   parent_pads = parent->pads;
2528
2529   while (parent_pads) {
2530     GstPad *parent_pad = GST_PAD_CAST (parent_pads->data);
2531
2532     if (parent_pad->direction != direction) {
2533       res = g_list_prepend (res, parent_pad);
2534     }
2535
2536     parent_pads = g_list_next (parent_pads);
2537   }
2538
2539   return res;
2540 }
2541
2542 /**
2543  * gst_pad_get_internal_links:
2544  * @pad: the #GstPad to get the internal links of.
2545  *
2546  * Gets a list of pads to which the given pad is linked to
2547  * inside of the parent element.
2548  * The caller must free this list after use.
2549  *
2550  * Returns: a newly allocated #GList of pads.
2551  *
2552  * Not MT safe.
2553  */
2554 GList *
2555 gst_pad_get_internal_links (GstPad * pad)
2556 {
2557   GList *res = NULL;
2558
2559   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2560
2561   if (GST_PAD_INTLINKFUNC (pad))
2562     res = GST_PAD_INTLINKFUNC (pad) (pad);
2563
2564   return res;
2565 }
2566
2567
2568 static gboolean
2569 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2570 {
2571   GList *orig, *pads;
2572   gboolean result;
2573
2574   GST_INFO_OBJECT (pad, "Sending event %p to all internally linked pads",
2575       event);
2576
2577   result = (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
2578
2579   orig = pads = gst_pad_get_internal_links (pad);
2580
2581   while (pads) {
2582     GstPad *eventpad = GST_PAD_CAST (pads->data);
2583
2584     pads = g_list_next (pads);
2585
2586     /* for all of the internally-linked pads that are actually linked */
2587     if (GST_PAD_IS_LINKED (eventpad)) {
2588       if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2589         /* for each pad we send to, we should ref the event; it's up
2590          * to downstream to unref again when handled. */
2591         GST_LOG_OBJECT (pad, "Reffing and sending event %p to %s:%s", event,
2592             GST_DEBUG_PAD_NAME (eventpad));
2593         gst_event_ref (event);
2594         gst_pad_push_event (eventpad, event);
2595       } else {
2596         /* we only send the event on one pad, multi-sinkpad elements
2597          * should implement a handler */
2598         GST_LOG_OBJECT (pad, "sending event %p to one sink pad %s:%s", event,
2599             GST_DEBUG_PAD_NAME (eventpad));
2600         result = gst_pad_push_event (eventpad, event);
2601         goto done;
2602       }
2603     }
2604   }
2605   /* we handled the incoming event so we unref once */
2606   GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
2607   gst_event_unref (event);
2608
2609 done:
2610   g_list_free (orig);
2611
2612   return result;
2613 }
2614
2615 /**
2616  * gst_pad_event_default:
2617  * @pad: a #GstPad to call the default event handler on.
2618  * @event: the #GstEvent to handle.
2619  *
2620  * Invokes the default event handler for the given pad. End-of-stream and
2621  * discontinuity events are handled specially, and then the event is sent to all
2622  * pads internally linked to @pad. Note that if there are many possible sink
2623  * pads that are internally linked to @pad, only one will be sent an event.
2624  * Multi-sinkpad elements should implement custom event handlers.
2625  *
2626  * Returns: TRUE if the event was sent succesfully.
2627  */
2628 gboolean
2629 gst_pad_event_default (GstPad * pad, GstEvent * event)
2630 {
2631   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2632   g_return_val_if_fail (event != NULL, FALSE);
2633
2634   switch (GST_EVENT_TYPE (event)) {
2635     case GST_EVENT_EOS:
2636     {
2637       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2638       gst_pad_pause_task (pad);
2639     }
2640     default:
2641       break;
2642   }
2643
2644   return gst_pad_event_default_dispatch (pad, event);
2645 }
2646
2647 /**
2648  * gst_pad_dispatcher:
2649  * @pad: a #GstPad to dispatch.
2650  * @dispatch: the #GstDispatcherFunction to call.
2651  * @data: gpointer user data passed to the dispatcher function.
2652  *
2653  * Invokes the given dispatcher function on all pads that are 
2654  * internally linked to the given pad. 
2655  * The GstPadDispatcherFunction should return TRUE when no further pads 
2656  * need to be processed.
2657  *
2658  * Returns: TRUE if one of the dispatcher functions returned TRUE.
2659  */
2660 gboolean
2661 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
2662     gpointer data)
2663 {
2664   gboolean res = FALSE;
2665   GList *int_pads, *orig;
2666
2667   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2668   g_return_val_if_fail (dispatch != NULL, FALSE);
2669
2670   orig = int_pads = gst_pad_get_internal_links (pad);
2671
2672   while (int_pads) {
2673     GstPad *int_pad = GST_PAD_CAST (int_pads->data);
2674     GstPad *int_peer = GST_PAD_PEER (int_pad);
2675
2676     if (int_peer) {
2677       res = dispatch (int_peer, data);
2678       if (res)
2679         break;
2680     }
2681     int_pads = g_list_next (int_pads);
2682   }
2683
2684   g_list_free (orig);
2685
2686   return res;
2687 }
2688
2689 /**
2690  * gst_pad_query:
2691  * @pad: a #GstPad to invoke the default query on.
2692  * @query: the #GstQuery to perform.
2693  *
2694  * Dispatches a query to a pad. The query should have been allocated by the
2695  * caller via one of the type-specific allocation functions in gstquery.h. The
2696  * element is responsible for filling the query with an appropriate response,
2697  * which should then be parsed with a type-specific query parsing function.
2698  *
2699  * Again, the caller is responsible for both the allocation and deallocation of
2700  * the query structure.
2701  *
2702  * Returns: TRUE if the query could be performed.
2703  */
2704 gboolean
2705 gst_pad_query (GstPad * pad, GstQuery * query)
2706 {
2707   GstPadQueryFunction func;
2708
2709   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2710   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2711
2712   GST_DEBUG ("sending query %p to pad %s:%s", query, GST_DEBUG_PAD_NAME (pad));
2713
2714   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
2715     goto no_func;
2716
2717   return func (pad, query);
2718
2719 no_func:
2720   {
2721     GST_DEBUG ("pad had no query function");
2722     return FALSE;
2723   }
2724 }
2725
2726 gboolean
2727 gst_pad_query_default (GstPad * pad, GstQuery * query)
2728 {
2729   switch (GST_QUERY_TYPE (query)) {
2730     case GST_QUERY_POSITION:
2731     case GST_QUERY_SEEKING:
2732     case GST_QUERY_FORMATS:
2733     case GST_QUERY_LATENCY:
2734     case GST_QUERY_JITTER:
2735     case GST_QUERY_RATE:
2736     case GST_QUERY_CONVERT:
2737     default:
2738       return gst_pad_dispatcher
2739           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
2740   }
2741 }
2742
2743 #ifndef GST_DISABLE_LOADSAVE
2744 /* FIXME: why isn't this on a GstElement ? */
2745 /**
2746  * gst_pad_load_and_link:
2747  * @self: an #xmlNodePtr to read the description from.
2748  * @parent: the #GstObject element that owns the pad.
2749  *
2750  * Reads the pad definition from the XML node and links the given pad
2751  * in the element to a pad of an element up in the hierarchy.
2752  */
2753 void
2754 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
2755 {
2756   xmlNodePtr field = self->xmlChildrenNode;
2757   GstPad *pad = NULL, *targetpad;
2758   gchar *peer = NULL;
2759   gchar **split;
2760   GstElement *target;
2761   GstObject *grandparent;
2762   gchar *name = NULL;
2763
2764   while (field) {
2765     if (!strcmp ((char *) field->name, "name")) {
2766       name = (gchar *) xmlNodeGetContent (field);
2767       pad = gst_element_get_pad (GST_ELEMENT (parent), name);
2768       g_free (name);
2769     } else if (!strcmp ((char *) field->name, "peer")) {
2770       peer = (gchar *) xmlNodeGetContent (field);
2771     }
2772     field = field->next;
2773   }
2774   g_return_if_fail (pad != NULL);
2775
2776   if (peer == NULL)
2777     return;
2778
2779   split = g_strsplit (peer, ".", 2);
2780
2781   if (split[0] == NULL || split[1] == NULL) {
2782     GST_CAT_DEBUG (GST_CAT_XML,
2783         "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2784         peer, GST_DEBUG_PAD_NAME (pad));
2785
2786     g_free (peer);
2787     return;
2788   }
2789   g_free (peer);
2790
2791   g_return_if_fail (split[0] != NULL);
2792   g_return_if_fail (split[1] != NULL);
2793
2794   grandparent = gst_object_get_parent (parent);
2795
2796   if (grandparent && GST_IS_BIN (grandparent)) {
2797     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2798   } else
2799     goto cleanup;
2800
2801   if (target == NULL)
2802     goto cleanup;
2803
2804   targetpad = gst_element_get_pad (target, split[1]);
2805
2806   if (targetpad == NULL)
2807     goto cleanup;
2808
2809   gst_pad_link (pad, targetpad);
2810
2811 cleanup:
2812   g_strfreev (split);
2813 }
2814
2815 /**
2816  * gst_pad_save_thyself:
2817  * @pad: a #GstPad to save.
2818  * @parent: the parent #xmlNodePtr to save the description in.
2819  *
2820  * Saves the pad into an xml representation.
2821  *
2822  * Returns: the #xmlNodePtr representation of the pad.
2823  */
2824 static xmlNodePtr
2825 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
2826 {
2827   GstPad *pad;
2828   GstPad *peer;
2829
2830   g_return_val_if_fail (GST_IS_PAD (object), NULL);
2831
2832   pad = GST_PAD (object);
2833
2834   xmlNewChild (parent, NULL, (xmlChar *) "name",
2835       (xmlChar *) GST_PAD_NAME (pad));
2836   if (GST_PAD_PEER (pad) != NULL) {
2837     gchar *content;
2838
2839     peer = GST_PAD_PEER (pad);
2840     /* first check to see if the peer's parent's parent is the same */
2841     /* we just save it off */
2842     content = g_strdup_printf ("%s.%s",
2843         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
2844     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
2845     g_free (content);
2846   } else
2847     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
2848
2849   return parent;
2850 }
2851
2852 #if 0
2853 /**
2854  * gst_ghost_pad_save_thyself:
2855  * @pad: a ghost #GstPad to save.
2856  * @parent: the parent #xmlNodePtr to save the description in.
2857  *
2858  * Saves the ghost pad into an xml representation.
2859  *
2860  * Returns: the #xmlNodePtr representation of the pad.
2861  */
2862 xmlNodePtr
2863 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
2864 {
2865   xmlNodePtr self;
2866
2867   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
2868
2869   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
2870   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
2871   xmlNewChild (self, NULL, (xmlChar *) "parent",
2872       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
2873
2874   /* FIXME FIXME FIXME! */
2875
2876   return self;
2877 }
2878 #endif /* 0 */
2879 #endif /* GST_DISABLE_LOADSAVE */
2880
2881 /* 
2882  * should be called with pad lock held 
2883  *
2884  * MT safe.
2885  */
2886 static void
2887 handle_pad_block (GstPad * pad)
2888 {
2889   GstPadBlockCallback callback;
2890   gpointer user_data;
2891
2892   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2893       "signal block taken on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2894
2895   /* need to grab extra ref for the callbacks */
2896   gst_object_ref (pad);
2897
2898   callback = pad->block_callback;
2899   if (callback) {
2900     user_data = pad->block_data;
2901     GST_UNLOCK (pad);
2902     callback (pad, TRUE, user_data);
2903     GST_LOCK (pad);
2904   } else {
2905     GST_PAD_BLOCK_SIGNAL (pad);
2906   }
2907
2908   while (GST_PAD_IS_BLOCKED (pad))
2909     GST_PAD_BLOCK_WAIT (pad);
2910
2911   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
2912
2913   callback = pad->block_callback;
2914   if (callback) {
2915     user_data = pad->block_data;
2916     GST_UNLOCK (pad);
2917     callback (pad, FALSE, user_data);
2918     GST_LOCK (pad);
2919   } else {
2920     GST_PAD_BLOCK_SIGNAL (pad);
2921   }
2922
2923   gst_object_unref (pad);
2924 }
2925
2926 /**********************************************************************
2927  * Data passing functions
2928  */
2929
2930 static gboolean
2931 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
2932 {
2933   GValue ret = { 0 };
2934   GValue args[2] = { {0}, {0} };
2935   gboolean res;
2936   GQuark detail;
2937
2938   /* init */
2939   g_value_init (&ret, G_TYPE_BOOLEAN);
2940   g_value_set_boolean (&ret, TRUE);
2941   g_value_init (&args[0], GST_TYPE_PAD);
2942   g_value_set_object (&args[0], pad);
2943   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);        // G_TYPE_POINTER);
2944   gst_value_set_mini_object (&args[1], obj);
2945
2946   if (GST_IS_EVENT (obj))
2947     detail = event_quark;
2948   else
2949     detail = buffer_quark;
2950
2951   /* actually emit */
2952   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
2953   res = g_value_get_boolean (&ret);
2954
2955   /* clean up */
2956   g_value_unset (&ret);
2957   g_value_unset (&args[0]);
2958   g_value_unset (&args[1]);
2959
2960   return res;
2961 }
2962
2963 /**
2964  * gst_pad_chain:
2965  * @pad: a sink #GstPad.
2966  * @buffer: the #GstBuffer to send.
2967  *
2968  * Chain a buffer to @pad.
2969  *
2970  * Returns: a #GstFlowReturn from the pad.
2971  *
2972  * MT safe.
2973  */
2974 GstFlowReturn
2975 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
2976 {
2977   GstCaps *caps;
2978   gboolean caps_changed;
2979   GstPadChainFunction chainfunc;
2980   GstFlowReturn ret;
2981   gboolean emit_signal;
2982
2983   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2984   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
2985       GST_FLOW_ERROR);
2986   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2987   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2988
2989   GST_STREAM_LOCK (pad);
2990
2991   GST_LOCK (pad);
2992   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2993     goto flushing;
2994
2995   caps = GST_BUFFER_CAPS (buffer);
2996   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2997
2998   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
2999   GST_UNLOCK (pad);
3000
3001   /* see if the signal should be emited, we emit before caps nego as
3002    * we might drop the buffer and do capsnego for nothing. */
3003   if (G_UNLIKELY (emit_signal)) {
3004     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer)))
3005       goto dropping;
3006   }
3007
3008   /* we got a new datatype on the pad, see if it can handle it */
3009   if (G_UNLIKELY (caps_changed)) {
3010     GST_DEBUG ("caps changed to %" GST_PTR_FORMAT, caps);
3011     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
3012       goto not_negotiated;
3013   }
3014
3015   /* NOTE: we read the chainfunc unlocked.
3016    * we cannot hold the lock for the pad so we might send
3017    * the data to the wrong function. This is not really a
3018    * problem since functions are assigned at creation time
3019    * and don't change that often... */
3020   if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3021     goto no_function;
3022
3023   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3024       "calling chainfunction &%s of pad %s:%s",
3025       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad));
3026
3027   ret = chainfunc (pad, buffer);
3028
3029   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3030       "called chainfunction &%s of pad %s:%s, returned %s",
3031       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad),
3032       gst_flow_get_name (ret));
3033
3034   GST_STREAM_UNLOCK (pad);
3035
3036   return ret;
3037
3038   /* ERRORS */
3039 flushing:
3040   {
3041     gst_buffer_unref (buffer);
3042     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3043         "pushing, but pad was flushing");
3044     GST_UNLOCK (pad);
3045     GST_STREAM_UNLOCK (pad);
3046     return GST_FLOW_WRONG_STATE;
3047   }
3048 dropping:
3049   {
3050     gst_buffer_unref (buffer);
3051     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3052     GST_STREAM_UNLOCK (pad);
3053     return GST_FLOW_OK;
3054   }
3055 not_negotiated:
3056   {
3057     gst_buffer_unref (buffer);
3058     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3059         "pushing buffer but pad did not accept");
3060     GST_STREAM_UNLOCK (pad);
3061     return GST_FLOW_NOT_NEGOTIATED;
3062   }
3063 no_function:
3064   {
3065     gst_buffer_unref (buffer);
3066     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3067         "pushing, but not chainhandler");
3068     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3069         ("push on pad %s:%s but it has no chainfunction",
3070             GST_DEBUG_PAD_NAME (pad)));
3071     GST_STREAM_UNLOCK (pad);
3072     return GST_FLOW_ERROR;
3073   }
3074 }
3075
3076 /**
3077  * gst_pad_push:
3078  * @pad: a source #GstPad.
3079  * @buffer: the #GstBuffer to push.
3080  *
3081  * Pushes a buffer to the peer of @pad.
3082  * buffer probes will be triggered before the buffer gets pushed.
3083  *
3084  * Returns: a #GstFlowReturn from the peer pad.
3085  *
3086  * MT safe.
3087  */
3088 GstFlowReturn
3089 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3090 {
3091   GstPad *peer;
3092   GstFlowReturn ret;
3093   gboolean emit_signal;
3094   gboolean signal_ret = TRUE;
3095
3096   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3097   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3098   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3099   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3100
3101   GST_LOCK (pad);
3102
3103   /* FIXME: this check can go away; pad_set_blocked could be implemented with
3104    * probes completely */
3105   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3106     handle_pad_block (pad);
3107
3108   /* we emit signals on the pad arg, the peer will have a chance to
3109    * emit in the _chain() function */
3110   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3111   GST_UNLOCK (pad);
3112
3113   if (G_UNLIKELY (emit_signal)) {
3114     signal_ret = gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer));
3115   }
3116
3117   /* if the signal handler returned FALSE, it means we should just drop the
3118    * buffer */
3119   if (signal_ret == FALSE) {
3120     gst_buffer_unref (buffer);
3121     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3122     return GST_FLOW_OK;
3123   }
3124
3125   GST_LOCK (pad);
3126
3127   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3128     goto not_linked;
3129   gst_object_ref (peer);
3130   GST_UNLOCK (pad);
3131
3132   ret = gst_pad_chain (peer, buffer);
3133
3134   gst_object_unref (peer);
3135
3136   return ret;
3137
3138   /* ERROR recovery here */
3139 not_linked:
3140   {
3141     gst_buffer_unref (buffer);
3142     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3143         "pushing, but it was not linked");
3144     GST_UNLOCK (pad);
3145     return GST_FLOW_NOT_LINKED;
3146   }
3147 }
3148
3149 /**
3150  * gst_pad_check_pull_range:
3151  * @pad: a sink #GstPad.
3152  *
3153  * Checks if a #gst_pad_pull_range() can be performed on the peer
3154  * source pad. This function is used by plugins that want to check
3155  * if they can use random access on the peer source pad. 
3156  *
3157  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
3158  * if it needs to perform some logic to determine if pull_range is
3159  * possible.
3160  *
3161  * Returns: a gboolean with the result.
3162  *
3163  * MT safe.
3164  */
3165 gboolean
3166 gst_pad_check_pull_range (GstPad * pad)
3167 {
3168   GstPad *peer;
3169   gboolean ret;
3170   GstPadCheckGetRangeFunction checkgetrangefunc;
3171
3172   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3173
3174   GST_LOCK (pad);
3175   if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
3176     goto wrong_direction;
3177
3178   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3179     goto not_connected;
3180
3181   gst_object_ref (peer);
3182   GST_UNLOCK (pad);
3183
3184   /* see note in above function */
3185   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
3186     /* FIXME, kindoff ghetto */
3187     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
3188   } else {
3189     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3190         "calling checkgetrangefunc %s of peer pad %s:%s",
3191         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
3192
3193     ret = checkgetrangefunc (peer);
3194   }
3195
3196   gst_object_unref (peer);
3197
3198   return ret;
3199
3200   /* ERROR recovery here */
3201 wrong_direction:
3202   {
3203     GST_UNLOCK (pad);
3204     return FALSE;
3205   }
3206 not_connected:
3207   {
3208     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3209         "checking pull range, but it was not linked");
3210     GST_UNLOCK (pad);
3211     return FALSE;
3212   }
3213 }
3214
3215 /**
3216  * gst_pad_get_range:
3217  * @pad: a src #GstPad.
3218  * @offset: The start offset of the buffer
3219  * @size: The length of the buffer
3220  * @buffer: a pointer to hold the #GstBuffer.
3221  *
3222  * Calls the getrange function of @pad. 
3223  *
3224  * Returns: a #GstFlowReturn from the pad.
3225  *
3226  * MT safe.
3227  */
3228 GstFlowReturn
3229 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3230     GstBuffer ** buffer)
3231 {
3232   GstFlowReturn ret;
3233   GstPadGetRangeFunction getrangefunc;
3234   gboolean emit_signal;
3235
3236   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3237   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3238   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3239
3240   GST_STREAM_LOCK (pad);
3241
3242   GST_LOCK (pad);
3243   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3244     goto flushing;
3245
3246   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3247   GST_UNLOCK (pad);
3248
3249   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3250     goto no_function;
3251
3252   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3253       "calling getrangefunc %s of peer pad %s:%s, offset %"
3254       G_GUINT64_FORMAT ", size %u",
3255       GST_DEBUG_FUNCPTR_NAME (getrangefunc), GST_DEBUG_PAD_NAME (pad),
3256       offset, size);
3257
3258   ret = getrangefunc (pad, offset, size, buffer);
3259
3260   /* can only fire the signal if we have a valid buffer */
3261   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3262     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3263       goto dropping;
3264   }
3265
3266   GST_STREAM_UNLOCK (pad);
3267
3268   return ret;
3269
3270   /* ERRORS */
3271 flushing:
3272   {
3273     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3274         "pulling range, but pad was flushing");
3275     GST_UNLOCK (pad);
3276     GST_STREAM_UNLOCK (pad);
3277     return GST_FLOW_WRONG_STATE;
3278   }
3279 no_function:
3280   {
3281     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3282         ("pullrange on pad %s:%s but it has no getrangefunction",
3283             GST_DEBUG_PAD_NAME (pad)));
3284     GST_STREAM_UNLOCK (pad);
3285     return GST_FLOW_ERROR;
3286   }
3287 dropping:
3288   {
3289     GST_DEBUG ("Dropping data after FALSE probe return");
3290     GST_STREAM_UNLOCK (pad);
3291     gst_buffer_unref (*buffer);
3292     *buffer = NULL;
3293     return GST_FLOW_UNEXPECTED;
3294   }
3295 }
3296
3297
3298 /**
3299  * gst_pad_pull_range:
3300  * @pad: a sink #GstPad.
3301  * @offset: The start offset of the buffer
3302  * @size: The length of the buffer
3303  * @buffer: a pointer to hold the #GstBuffer.
3304  *
3305  * Pulls a buffer from the peer pad. @pad must be a linked
3306  * sinkpad.
3307  *
3308  * Returns: a #GstFlowReturn from the peer pad.
3309  *
3310  * MT safe.
3311  */
3312 GstFlowReturn
3313 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3314     GstBuffer ** buffer)
3315 {
3316   GstPad *peer;
3317   GstFlowReturn ret;
3318   gboolean emit_signal;
3319
3320   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3321   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
3322       GST_FLOW_ERROR);
3323   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3324
3325   GST_LOCK (pad);
3326
3327   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3328     handle_pad_block (pad);
3329
3330   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3331     goto not_connected;
3332
3333   /* signal emision for the pad, peer has chance to emit when
3334    * we call _get_range() */
3335   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3336
3337   gst_object_ref (peer);
3338   GST_UNLOCK (pad);
3339
3340   ret = gst_pad_get_range (peer, offset, size, buffer);
3341
3342   gst_object_unref (peer);
3343
3344   /* can only fire the signal if we have a valid buffer */
3345   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3346     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3347       goto dropping;
3348   }
3349   return ret;
3350
3351   /* ERROR recovery here */
3352 not_connected:
3353   {
3354     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3355         "pulling range, but it was not linked");
3356     GST_UNLOCK (pad);
3357     return GST_FLOW_NOT_LINKED;
3358   }
3359 dropping:
3360   {
3361     GST_DEBUG ("Dropping data after FALSE probe return");
3362     gst_buffer_unref (*buffer);
3363     *buffer = NULL;
3364     return GST_FLOW_UNEXPECTED;
3365   }
3366 }
3367
3368 /**
3369  * gst_pad_push_event:
3370  * @pad: a #GstPad to push the event to.
3371  * @event: the #GstEvent to send to the pad.
3372  *
3373  * Sends the event to the peer of the given pad. This function is
3374  * mainly used by elements to send events to their peer
3375  * elements.
3376  *
3377  * Returns: TRUE if the event was handled.
3378  *
3379  * MT safe.
3380  */
3381 gboolean
3382 gst_pad_push_event (GstPad * pad, GstEvent * event)
3383 {
3384   GstPad *peerpad;
3385   gboolean result;
3386   gboolean emit_signal;
3387   gboolean signal_ret = TRUE;
3388
3389   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3390   g_return_val_if_fail (event != NULL, FALSE);
3391
3392   emit_signal = GST_PAD_DO_EVENT_SIGNALS (pad) > 0;
3393
3394   if (G_UNLIKELY (emit_signal)) {
3395     signal_ret = gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event));
3396   }
3397
3398   if (signal_ret == FALSE) {
3399     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
3400     gst_event_unref (event);
3401     return FALSE;
3402   }
3403
3404   GST_LOCK (pad);
3405   peerpad = GST_PAD_PEER (pad);
3406   if (peerpad == NULL)
3407     goto not_linked;
3408
3409   gst_object_ref (peerpad);
3410   GST_UNLOCK (pad);
3411
3412   result = gst_pad_send_event (peerpad, event);
3413
3414   gst_object_unref (peerpad);
3415
3416   return result;
3417
3418   /* ERROR handling */
3419 not_linked:
3420   {
3421     gst_event_unref (event);
3422     GST_UNLOCK (pad);
3423     return FALSE;
3424   }
3425 }
3426
3427 /**
3428  * gst_pad_send_event:
3429  * @pad: a #GstPad to send the event to.
3430  * @event: the #GstEvent to send to the pad.
3431  *
3432  * Sends the event to the pad. This function can be used
3433  * by applications to send events in the pipeline.
3434  *
3435  * Returns: TRUE if the event was handled.
3436  */
3437 gboolean
3438 gst_pad_send_event (GstPad * pad, GstEvent * event)
3439 {
3440   gboolean result = FALSE;
3441   GstPadEventFunction eventfunc;
3442   gboolean emit_signal;
3443
3444   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3445   g_return_val_if_fail (event != NULL, FALSE);
3446
3447   GST_LOCK (pad);
3448
3449   if (GST_PAD_IS_SINK (pad) && !GST_EVENT_IS_DOWNSTREAM (event))
3450     goto wrong_direction;
3451   if (GST_PAD_IS_SRC (pad) && !GST_EVENT_IS_UPSTREAM (event))
3452     goto wrong_direction;
3453
3454   if (GST_EVENT_SRC (event) == NULL)
3455     GST_EVENT_SRC (event) = gst_object_ref (pad);
3456
3457   switch (GST_EVENT_TYPE (event)) {
3458     case GST_EVENT_FLUSH_START:
3459       GST_CAT_DEBUG (GST_CAT_EVENT,
3460           "have event type %d (FLUSH_START) on pad %s:%s",
3461           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3462
3463       /* can't even accept a flush begin event when flushing */
3464       if (GST_PAD_IS_FLUSHING (pad))
3465         goto flushing;
3466       GST_PAD_SET_FLUSHING (pad);
3467       GST_CAT_DEBUG (GST_CAT_EVENT, "set flush flag");
3468       break;
3469     case GST_EVENT_FLUSH_STOP:
3470       GST_PAD_UNSET_FLUSHING (pad);
3471       GST_CAT_DEBUG (GST_CAT_EVENT, "cleared flush flag");
3472       break;
3473     default:
3474       GST_CAT_DEBUG (GST_CAT_EVENT, "have event type %d on pad %s:%s",
3475           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3476
3477       if (GST_PAD_IS_FLUSHING (pad))
3478         goto flushing;
3479       break;
3480   }
3481
3482   if ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL)
3483     goto no_function;
3484
3485   emit_signal = GST_PAD_DO_EVENT_SIGNALS (pad) > 0;
3486
3487   gst_object_ref (pad);
3488   GST_UNLOCK (pad);
3489
3490   if (G_UNLIKELY (emit_signal)) {
3491     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
3492       goto dropping;
3493   }
3494
3495   result = eventfunc (GST_PAD_CAST (pad), event);
3496
3497   gst_object_unref (pad);
3498
3499   return result;
3500
3501   /* ERROR handling */
3502 wrong_direction:
3503   {
3504     g_warning ("pad %s:%s sending event in wrong direction",
3505         GST_DEBUG_PAD_NAME (pad));
3506     GST_UNLOCK (pad);
3507     gst_event_unref (event);
3508     return FALSE;
3509   }
3510 no_function:
3511   {
3512     g_warning ("pad %s:%s has no event handler, file a bug.",
3513         GST_DEBUG_PAD_NAME (pad));
3514     GST_UNLOCK (pad);
3515     gst_event_unref (event);
3516     return FALSE;
3517   }
3518 flushing:
3519   {
3520     GST_UNLOCK (pad);
3521     GST_CAT_DEBUG (GST_CAT_EVENT, "Received event on flushing pad. Discarding");
3522     gst_event_unref (event);
3523     return FALSE;
3524   }
3525 dropping:
3526   {
3527     GST_DEBUG ("Dropping event after FALSE probe return");
3528     gst_object_unref (pad);
3529     gst_event_unref (event);
3530     return FALSE;
3531   }
3532 }
3533
3534 /**
3535  * gst_pad_set_element_private:
3536  * @pad: the #GstPad to set the private data of.
3537  * @priv: The private data to attach to the pad.
3538  *
3539  * Set the given private data gpointer on the pad. 
3540  * This function can only be used by the element that owns the pad.
3541  */
3542 void
3543 gst_pad_set_element_private (GstPad * pad, gpointer priv)
3544 {
3545   pad->element_private = priv;
3546 }
3547
3548 /**
3549  * gst_pad_get_element_private:
3550  * @pad: the #GstPad to get the private data of.
3551  *
3552  * Gets the private data of a pad.
3553  *
3554  * Returns: a #gpointer to the private data.
3555  */
3556 gpointer
3557 gst_pad_get_element_private (GstPad * pad)
3558 {
3559   return pad->element_private;
3560 }
3561
3562 /**
3563  * gst_pad_start_task:
3564  * @pad: the #GstPad to start the task of
3565  * @func: the task function to call
3566  * @data: data passed to the task function
3567  *
3568  * Starts a task that repeadedly calls @func with @data. This function
3569  * is nostly used in the pad activation function to start the
3570  * dataflow. This function will automatically acauire the STREAM_LOCK of
3571  * the pad before calling @func.
3572  *
3573  * Returns: a TRUE if the task could be started. 
3574  */
3575 gboolean
3576 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
3577 {
3578   GstTask *task;
3579
3580   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3581   g_return_val_if_fail (func != NULL, FALSE);
3582
3583   GST_LOCK (pad);
3584   task = GST_PAD_TASK (pad);
3585   if (task == NULL) {
3586     task = gst_task_create (func, data);
3587     gst_task_set_lock (task, GST_STREAM_GET_LOCK (pad));
3588     GST_PAD_TASK (pad) = task;
3589   }
3590   gst_task_start (task);
3591   GST_UNLOCK (pad);
3592
3593   return TRUE;
3594 }
3595
3596 /**
3597  * gst_pad_pause_task:
3598  * @pad: the #GstPad to pause the task of
3599  *
3600  * Pause the task of @pad. This function will also make sure that the 
3601  * function executed by the task will effectively stop.
3602  *
3603  * Returns: a TRUE if the task could be paused or FALSE when the pad
3604  * has no task.
3605  */
3606 gboolean
3607 gst_pad_pause_task (GstPad * pad)
3608 {
3609   GstTask *task;
3610
3611   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3612
3613   GST_LOCK (pad);
3614   task = GST_PAD_TASK (pad);
3615   if (task == NULL)
3616     goto no_task;
3617   gst_task_pause (task);
3618   GST_UNLOCK (pad);
3619
3620   GST_STREAM_LOCK (pad);
3621   GST_STREAM_UNLOCK (pad);
3622
3623   return TRUE;
3624
3625 no_task:
3626   {
3627     GST_UNLOCK (pad);
3628     return TRUE;
3629   }
3630 }
3631
3632 /**
3633  * gst_pad_stop_task:
3634  * @pad: the #GstPad to stop the task of
3635  *
3636  * Stop the task of @pad. This function will also make sure that the 
3637  * function executed by the task will effectively stop if not called
3638  * from the GstTaskFunction.
3639  *
3640  * This function will deadlock if called from the GstTaskFunction of
3641  * the task. Use #gst_task_pause() instead.
3642  *
3643  * Returns: a TRUE if the task could be stopped or FALSE when the pad
3644  * has no task.
3645  */
3646 gboolean
3647 gst_pad_stop_task (GstPad * pad)
3648 {
3649   GstTask *task;
3650
3651   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3652
3653   GST_LOCK (pad);
3654   task = GST_PAD_TASK (pad);
3655   if (task == NULL)
3656     goto no_task;
3657   GST_PAD_TASK (pad) = NULL;
3658   gst_task_stop (task);
3659   GST_UNLOCK (pad);
3660
3661   GST_STREAM_LOCK (pad);
3662   GST_STREAM_UNLOCK (pad);
3663
3664   gst_task_join (task);
3665
3666   gst_object_unref (task);
3667
3668   return TRUE;
3669
3670 no_task:
3671   {
3672     GST_UNLOCK (pad);
3673     return TRUE;
3674   }
3675 }