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