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