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