gstpad: Fix flush-stop event handling
[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() and
58  * gst_pad_push_event().
59  *
60  * Last reviewed on 2006-07-06 (0.10.9)
61  */
62
63 #include "gst_private.h"
64
65 #include "gstpad.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
69 #include "gstutils.h"
70 #include "gstinfo.h"
71 #include "gsterror.h"
72 #include "gstvalue.h"
73 #include "glib-compat-private.h"
74
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
77
78 /* Pad signals and args */
79 enum
80 {
81   PAD_LINKED,
82   PAD_UNLINKED,
83   PAD_REQUEST_LINK,
84   PAD_HAVE_DATA,
85   /* FILL ME */
86   LAST_SIGNAL
87 };
88
89 enum
90 {
91   PAD_PROP_0,
92   PAD_PROP_CAPS,
93   PAD_PROP_DIRECTION,
94   PAD_PROP_TEMPLATE,
95   /* FILL ME */
96 };
97
98 #define GST_PAD_GET_PRIVATE(obj)  \
99    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
100
101 #define GST_PAD_CHAINLISTFUNC(pad) ((pad)->abidata.ABI.priv->chainlistfunc)
102
103 struct _GstPadPrivate
104 {
105   GstPadChainListFunction chainlistfunc;
106 };
107
108 static void gst_pad_dispose (GObject * object);
109 static void gst_pad_finalize (GObject * object);
110 static void gst_pad_set_property (GObject * object, guint prop_id,
111     const GValue * value, GParamSpec * pspec);
112 static void gst_pad_get_property (GObject * object, guint prop_id,
113     GValue * value, GParamSpec * pspec);
114
115 static GstFlowReturn handle_pad_block (GstPad * pad);
116 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
117 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
118 static gboolean gst_pad_activate_default (GstPad * pad);
119 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
120
121 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
122 #ifdef GST_DISABLE_DEPRECATED
123 #include <libxml/parser.h>
124 #endif
125 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
126 void gst_pad_load_and_link (xmlNodePtr self, GstObject * parent);
127 #endif
128
129 /* Some deprecated stuff that we need inside here for
130  * backwards compatibility */
131 #ifdef GST_DISABLE_DEPRECATED
132 #ifndef GST_REMOVE_DEPRECATED
133 #define GST_PAD_INTLINKFUNC(pad)        (GST_PAD_CAST(pad)->intlinkfunc)
134 GList *gst_pad_get_internal_links_default (GstPad * pad);
135 #endif
136 #endif
137
138 static GstObjectClass *parent_class = NULL;
139 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
140
141 /* quarks for probe signals */
142 static GQuark buffer_quark;
143 static GQuark event_quark;
144
145 typedef struct
146 {
147   const gint ret;
148   const gchar *name;
149   GQuark quark;
150 } GstFlowQuarks;
151
152 static GstFlowQuarks flow_quarks[] = {
153   {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
154   {GST_FLOW_RESEND, "resend", 0},
155   {GST_FLOW_OK, "ok", 0},
156   {GST_FLOW_NOT_LINKED, "not-linked", 0},
157   {GST_FLOW_WRONG_STATE, "wrong-state", 0},
158   {GST_FLOW_UNEXPECTED, "unexpected", 0},
159   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
160   {GST_FLOW_ERROR, "error", 0},
161   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
162   {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
163 };
164
165 /**
166  * gst_flow_get_name:
167  * @ret: a #GstFlowReturn to get the name of.
168  *
169  * Gets a string representing the given flow return.
170  *
171  * Returns: a static string with the name of the flow return.
172  */
173 G_CONST_RETURN gchar *
174 gst_flow_get_name (GstFlowReturn ret)
175 {
176   gint i;
177
178   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
179
180   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
181     if (ret == flow_quarks[i].ret)
182       return flow_quarks[i].name;
183   }
184   return "unknown";
185 }
186
187 /**
188  * gst_flow_to_quark:
189  * @ret: a #GstFlowReturn to get the quark of.
190  *
191  * Get the unique quark for the given GstFlowReturn.
192  *
193  * Returns: the quark associated with the flow return or 0 if an
194  * invalid return was specified.
195  */
196 GQuark
197 gst_flow_to_quark (GstFlowReturn ret)
198 {
199   gint i;
200
201   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
202
203   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
204     if (ret == flow_quarks[i].ret)
205       return flow_quarks[i].quark;
206   }
207   return 0;
208 }
209
210 #define _do_init \
211 { \
212   gint i; \
213   \
214   buffer_quark = g_quark_from_static_string ("buffer"); \
215   event_quark = g_quark_from_static_string ("event"); \
216   \
217   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {                    \
218     flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
219   } \
220   \
221   GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
222       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
223 }
224
225 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
226
227 static gboolean
228 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
229     GValue * return_accu, const GValue * handler_return, gpointer dummy)
230 {
231   gboolean ret = g_value_get_boolean (handler_return);
232
233   GST_DEBUG ("accumulated %d", ret);
234   g_value_set_boolean (return_accu, ret);
235
236   return ret;
237 }
238
239 static gboolean
240 default_have_data (GstPad * pad, GstMiniObject * o)
241 {
242   return TRUE;
243 }
244
245 static void
246 gst_pad_class_init (GstPadClass * klass)
247 {
248   GObjectClass *gobject_class;
249   GstObjectClass *gstobject_class;
250
251   gobject_class = G_OBJECT_CLASS (klass);
252   gstobject_class = GST_OBJECT_CLASS (klass);
253
254   g_type_class_add_private (klass, sizeof (GstPadPrivate));
255
256   parent_class = g_type_class_peek_parent (klass);
257
258   gobject_class->dispose = gst_pad_dispose;
259   gobject_class->finalize = gst_pad_finalize;
260   gobject_class->set_property = gst_pad_set_property;
261   gobject_class->get_property = gst_pad_get_property;
262
263   /**
264    * GstPad::linked:
265    * @pad: the pad that emitted the signal
266    * @peer: the peer pad that has been connected
267    *
268    * Signals that a pad has been linked to the peer pad.
269    */
270   gst_pad_signals[PAD_LINKED] =
271       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
272       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
273       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
274   /**
275    * GstPad::unlinked:
276    * @pad: the pad that emitted the signal
277    * @peer: the peer pad that has been disconnected
278    *
279    * Signals that a pad has been unlinked from the peer pad.
280    */
281   gst_pad_signals[PAD_UNLINKED] =
282       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
283       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
284       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
285   /**
286    * GstPad::request-link:
287    * @pad: the pad that emitted the signal
288    * @peer: the peer pad for which a connection is requested
289    *
290    * Signals that a pad connection has been requested.
291    */
292   gst_pad_signals[PAD_REQUEST_LINK] =
293       g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
294       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
295       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
296
297   /**
298    * GstPad::have-data:
299    * @pad: the pad that emitted the signal
300    * @mini_obj: new data
301    *
302    * Signals that new data is available on the pad. This signal is used
303    * internally for implementing pad probes.
304    * See gst_pad_add_*_probe functions.
305    *
306    * Returns: %TRUE to keep the data, %FALSE to drop it
307    */
308   gst_pad_signals[PAD_HAVE_DATA] =
309       g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
310       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
311       G_STRUCT_OFFSET (GstPadClass, have_data),
312       _gst_do_pass_data_accumulator,
313       NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
314       GST_TYPE_MINI_OBJECT);
315
316   g_object_class_install_property (gobject_class, PAD_PROP_CAPS,
317       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
318           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319   g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
320       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
321           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
322           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
323   /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
324   g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
325       g_param_spec_object ("template", "Template",
326           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
327           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328
329 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
330   gstobject_class->save_thyself =
331       ((gpointer (*)(GstObject * object,
332               gpointer self)) * GST_DEBUG_FUNCPTR (gst_pad_save_thyself));
333 #endif
334   gstobject_class->path_string_separator = ".";
335
336   /* Register common function pointer descriptions */
337   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
338   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
339   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
340   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
341 #ifndef GST_REMOVE_DEPRECATED
342   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_internal_links_default);
343 #endif
344   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
345   GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
346
347   klass->have_data = default_have_data;
348 }
349
350 static void
351 gst_pad_init (GstPad * pad)
352 {
353   pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
354
355   GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
356   GST_PAD_PEER (pad) = NULL;
357
358   GST_PAD_CHAINFUNC (pad) = NULL;
359
360   GST_PAD_LINKFUNC (pad) = NULL;
361
362   GST_PAD_CAPS (pad) = NULL;
363   GST_PAD_GETCAPSFUNC (pad) = NULL;
364
365   GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
366   GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
367   GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
368   GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
369 #ifndef GST_REMOVE_DEPRECATED
370   GST_PAD_INTLINKFUNC (pad) = gst_pad_get_internal_links_default;
371 #endif
372   GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
373
374   GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
375
376   pad->do_buffer_signals = 0;
377   pad->do_event_signals = 0;
378
379   GST_PAD_SET_FLUSHING (pad);
380
381   pad->preroll_lock = g_mutex_new ();
382   pad->preroll_cond = g_cond_new ();
383
384   /* FIXME 0.11: Store this directly in the instance struct */
385   pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
386   g_static_rec_mutex_init (pad->stream_rec_lock);
387
388   pad->block_cond = g_cond_new ();
389 }
390
391 static void
392 gst_pad_dispose (GObject * object)
393 {
394   GstPad *pad = GST_PAD_CAST (object);
395   GstPad *peer;
396
397   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
398
399   /* unlink the peer pad */
400   if ((peer = gst_pad_get_peer (pad))) {
401     /* window for MT unsafeness, someone else could unlink here
402      * and then we call unlink with wrong pads. The unlink
403      * function would catch this and safely return failed. */
404     if (GST_PAD_IS_SRC (pad))
405       gst_pad_unlink (pad, peer);
406     else
407       gst_pad_unlink (peer, pad);
408
409     gst_object_unref (peer);
410   }
411
412   /* clear the caps */
413   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
414
415   gst_pad_set_pad_template (pad, NULL);
416
417   if (pad->block_destroy_data && pad->block_data) {
418     pad->block_destroy_data (pad->block_data);
419     pad->block_data = NULL;
420   }
421
422   G_OBJECT_CLASS (parent_class)->dispose (object);
423 }
424
425 static void
426 gst_pad_finalize (GObject * object)
427 {
428   GstPad *pad = GST_PAD_CAST (object);
429   GstTask *task;
430
431   /* in case the task is still around, clean it up */
432   if ((task = GST_PAD_TASK (pad))) {
433     gst_task_join (task);
434     GST_PAD_TASK (pad) = NULL;
435     gst_object_unref (task);
436   }
437
438   if (pad->stream_rec_lock) {
439     g_static_rec_mutex_free (pad->stream_rec_lock);
440     g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
441     pad->stream_rec_lock = NULL;
442   }
443   if (pad->preroll_lock) {
444     g_mutex_free (pad->preroll_lock);
445     g_cond_free (pad->preroll_cond);
446     pad->preroll_lock = NULL;
447     pad->preroll_cond = NULL;
448   }
449   if (pad->block_cond) {
450     g_cond_free (pad->block_cond);
451     pad->block_cond = NULL;
452   }
453
454   G_OBJECT_CLASS (parent_class)->finalize (object);
455 }
456
457 static void
458 gst_pad_set_property (GObject * object, guint prop_id,
459     const GValue * value, GParamSpec * pspec)
460 {
461   g_return_if_fail (GST_IS_PAD (object));
462
463   switch (prop_id) {
464     case PAD_PROP_DIRECTION:
465       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
466       break;
467     case PAD_PROP_TEMPLATE:
468       gst_pad_set_pad_template (GST_PAD_CAST (object),
469           (GstPadTemplate *) g_value_get_object (value));
470       break;
471     default:
472       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473       break;
474   }
475 }
476
477 static void
478 gst_pad_get_property (GObject * object, guint prop_id,
479     GValue * value, GParamSpec * pspec)
480 {
481   g_return_if_fail (GST_IS_PAD (object));
482
483   switch (prop_id) {
484     case PAD_PROP_CAPS:
485       GST_OBJECT_LOCK (object);
486       g_value_set_boxed (value, GST_PAD_CAPS (object));
487       GST_OBJECT_UNLOCK (object);
488       break;
489     case PAD_PROP_DIRECTION:
490       g_value_set_enum (value, GST_PAD_DIRECTION (object));
491       break;
492     case PAD_PROP_TEMPLATE:
493       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
494       break;
495     default:
496       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
497       break;
498   }
499 }
500
501 /**
502  * gst_pad_new:
503  * @name: the name of the new pad.
504  * @direction: the #GstPadDirection of the pad.
505  *
506  * Creates a new pad with the given name in the given direction.
507  * If name is NULL, a guaranteed unique name (across all pads)
508  * will be assigned.
509  * This function makes a copy of the name so you can safely free the name.
510  *
511  * Returns: a new #GstPad, or NULL in case of an error.
512  *
513  * MT safe.
514  */
515 GstPad *
516 gst_pad_new (const gchar * name, GstPadDirection direction)
517 {
518   return g_object_new (GST_TYPE_PAD,
519       "name", name, "direction", direction, NULL);
520 }
521
522 /**
523  * gst_pad_new_from_template:
524  * @templ: the pad template to use
525  * @name: the name of the element
526  *
527  * Creates a new pad with the given name from the given template.
528  * If name is NULL, a guaranteed unique name (across all pads)
529  * will be assigned.
530  * This function makes a copy of the name so you can safely free the name.
531  *
532  * Returns: a new #GstPad, or NULL in case of an error.
533  */
534 GstPad *
535 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
536 {
537   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
538
539   return g_object_new (GST_TYPE_PAD,
540       "name", name, "direction", templ->direction, "template", templ, NULL);
541 }
542
543 /**
544  * gst_pad_new_from_static_template:
545  * @templ: the #GstStaticPadTemplate to use
546  * @name: the name of the element
547  *
548  * Creates a new pad with the given name from the given static template.
549  * If name is NULL, a guaranteed unique name (across all pads)
550  * will be assigned.
551  * This function makes a copy of the name so you can safely free the name.
552  *
553  * Returns: a new #GstPad, or NULL in case of an error.
554  */
555 GstPad *
556 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
557     const gchar * name)
558 {
559   GstPad *pad;
560   GstPadTemplate *template;
561
562   template = gst_static_pad_template_get (templ);
563   pad = gst_pad_new_from_template (template, name);
564   gst_object_unref (template);
565   return pad;
566 }
567
568 /**
569  * gst_pad_get_direction:
570  * @pad: a #GstPad to get the direction of.
571  *
572  * Gets the direction of the pad. The direction of the pad is
573  * decided at construction time so this function does not take
574  * the LOCK.
575  *
576  * Returns: the #GstPadDirection of the pad.
577  *
578  * MT safe.
579  */
580 GstPadDirection
581 gst_pad_get_direction (GstPad * pad)
582 {
583   GstPadDirection result;
584
585   /* PAD_UNKNOWN is a little silly but we need some sort of
586    * error return value */
587   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
588
589   result = GST_PAD_DIRECTION (pad);
590
591   return result;
592 }
593
594 static gboolean
595 gst_pad_activate_default (GstPad * pad)
596 {
597   return gst_pad_activate_push (pad, TRUE);
598 }
599
600 static void
601 pre_activate (GstPad * pad, GstActivateMode new_mode)
602 {
603   switch (new_mode) {
604     case GST_ACTIVATE_PUSH:
605     case GST_ACTIVATE_PULL:
606       GST_OBJECT_LOCK (pad);
607       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
608           new_mode);
609       GST_PAD_UNSET_FLUSHING (pad);
610       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
611       GST_OBJECT_UNLOCK (pad);
612       break;
613     case GST_ACTIVATE_NONE:
614       GST_OBJECT_LOCK (pad);
615       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
616       GST_PAD_SET_FLUSHING (pad);
617       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
618       /* unlock blocked pads so element can resume and stop */
619       GST_PAD_BLOCK_BROADCAST (pad);
620       GST_OBJECT_UNLOCK (pad);
621       break;
622   }
623 }
624
625 static void
626 post_activate (GstPad * pad, GstActivateMode new_mode)
627 {
628   switch (new_mode) {
629     case GST_ACTIVATE_PUSH:
630     case GST_ACTIVATE_PULL:
631       /* nop */
632       break;
633     case GST_ACTIVATE_NONE:
634       /* ensures that streaming stops */
635       GST_PAD_STREAM_LOCK (pad);
636       GST_DEBUG_OBJECT (pad, "stopped streaming");
637       GST_PAD_STREAM_UNLOCK (pad);
638       break;
639   }
640 }
641
642 /**
643  * gst_pad_set_active:
644  * @pad: the #GstPad to activate or deactivate.
645  * @active: whether or not the pad should be active.
646  *
647  * Activates or deactivates the given pad.
648  * Normally called from within core state change functions.
649  *
650  * If @active, makes sure the pad is active. If it is already active, either in
651  * push or pull mode, just return. Otherwise dispatches to the pad's activate
652  * function to perform the actual activation.
653  *
654  * If not @active, checks the pad's current mode and calls
655  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
656  * FALSE argument.
657  *
658  * Returns: #TRUE if the operation was successful.
659  *
660  * MT safe.
661  */
662 gboolean
663 gst_pad_set_active (GstPad * pad, gboolean active)
664 {
665   GstActivateMode old;
666   gboolean ret = FALSE;
667
668   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
669
670   GST_OBJECT_LOCK (pad);
671   old = GST_PAD_ACTIVATE_MODE (pad);
672   GST_OBJECT_UNLOCK (pad);
673
674   if (active) {
675     switch (old) {
676       case GST_ACTIVATE_PUSH:
677         GST_DEBUG_OBJECT (pad, "activating pad from push");
678         ret = TRUE;
679         break;
680       case GST_ACTIVATE_PULL:
681         GST_DEBUG_OBJECT (pad, "activating pad from pull");
682         ret = TRUE;
683         break;
684       case GST_ACTIVATE_NONE:
685         GST_DEBUG_OBJECT (pad, "activating pad from none");
686         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
687         break;
688     }
689   } else {
690     switch (old) {
691       case GST_ACTIVATE_PUSH:
692         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
693         ret = gst_pad_activate_push (pad, FALSE);
694         break;
695       case GST_ACTIVATE_PULL:
696         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
697         ret = gst_pad_activate_pull (pad, FALSE);
698         break;
699       case GST_ACTIVATE_NONE:
700         GST_DEBUG_OBJECT (pad, "deactivating pad from none");
701         ret = TRUE;
702         break;
703     }
704   }
705
706   if (!ret) {
707     GST_OBJECT_LOCK (pad);
708     if (!active) {
709       g_critical ("Failed to deactivate pad %s:%s, very bad",
710           GST_DEBUG_PAD_NAME (pad));
711     } else {
712       GST_WARNING_OBJECT (pad, "Failed to activate pad");
713     }
714     GST_OBJECT_UNLOCK (pad);
715   }
716
717   return ret;
718 }
719
720 /**
721  * gst_pad_activate_pull:
722  * @pad: the #GstPad to activate or deactivate.
723  * @active: whether or not the pad should be active.
724  *
725  * Activates or deactivates the given pad in pull mode via dispatching to the
726  * pad's activatepullfunc. For use from within pad activation functions only.
727  * When called on sink pads, will first proxy the call to the peer pad, which
728  * is expected to activate its internally linked pads from within its
729  * activate_pull function.
730  *
731  * If you don't know what this is, you probably don't want to call it.
732  *
733  * Returns: TRUE if the operation was successful.
734  *
735  * MT safe.
736  */
737 gboolean
738 gst_pad_activate_pull (GstPad * pad, gboolean active)
739 {
740   GstActivateMode old, new;
741   GstPad *peer;
742
743   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
744
745   GST_OBJECT_LOCK (pad);
746   old = GST_PAD_ACTIVATE_MODE (pad);
747   GST_OBJECT_UNLOCK (pad);
748
749   if (active) {
750     switch (old) {
751       case GST_ACTIVATE_PULL:
752         GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
753         goto was_ok;
754       case GST_ACTIVATE_PUSH:
755         GST_DEBUG_OBJECT (pad,
756             "activating pad from push, deactivate push first");
757         /* pad was activate in the wrong direction, deactivate it
758          * and reactivate it in pull mode */
759         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
760           goto deactivate_failed;
761         /* fallthrough, pad is deactivated now. */
762       case GST_ACTIVATE_NONE:
763         GST_DEBUG_OBJECT (pad, "activating pad from none");
764         break;
765     }
766   } else {
767     switch (old) {
768       case GST_ACTIVATE_NONE:
769         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
770         goto was_ok;
771       case GST_ACTIVATE_PUSH:
772         GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
773         /* pad was activated in the other direction, deactivate it
774          * in push mode, this should not happen... */
775         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
776           goto deactivate_failed;
777         /* everything is fine now */
778         goto was_ok;
779       case GST_ACTIVATE_PULL:
780         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
781         break;
782     }
783   }
784
785   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
786     if ((peer = gst_pad_get_peer (pad))) {
787       GST_DEBUG_OBJECT (pad, "calling peer");
788       if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
789         goto peer_failed;
790       gst_object_unref (peer);
791     } else {
792       /* there is no peer, this is only fatal when we activate. When we
793        * deactivate, we must assume the application has unlinked the peer and
794        * will deactivate it eventually. */
795       if (active)
796         goto not_linked;
797       else
798         GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
799     }
800   } else {
801     if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
802       goto failure;             /* Can't activate pull on a src without a
803                                    getrange function */
804   }
805
806   new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
807   pre_activate (pad, new);
808
809   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
810     if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
811       goto failure;
812   } else {
813     /* can happen for sinks of passthrough elements */
814   }
815
816   post_activate (pad, new);
817
818   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
819       active ? "activated" : "deactivated");
820
821   return TRUE;
822
823 was_ok:
824   {
825     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
826         active ? "activated" : "deactivated");
827     return TRUE;
828   }
829 deactivate_failed:
830   {
831     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
832         "failed to %s in switch to pull from mode %d",
833         (active ? "activate" : "deactivate"), old);
834     return FALSE;
835   }
836 peer_failed:
837   {
838     GST_OBJECT_LOCK (peer);
839     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
840         "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
841     GST_OBJECT_UNLOCK (peer);
842     gst_object_unref (peer);
843     return FALSE;
844   }
845 not_linked:
846   {
847     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
848         "pad in pull mode");
849     return FALSE;
850   }
851 failure:
852   {
853     GST_OBJECT_LOCK (pad);
854     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
855         active ? "activate" : "deactivate");
856     GST_PAD_SET_FLUSHING (pad);
857     GST_PAD_ACTIVATE_MODE (pad) = old;
858     GST_OBJECT_UNLOCK (pad);
859     return FALSE;
860   }
861 }
862
863 /**
864  * gst_pad_activate_push:
865  * @pad: the #GstPad to activate or deactivate.
866  * @active: whether the pad should be active or not.
867  *
868  * Activates or deactivates the given pad in push mode via dispatching to the
869  * pad's activatepushfunc. For use from within pad activation functions only.
870  *
871  * If you don't know what this is, you probably don't want to call it.
872  *
873  * Returns: %TRUE if the operation was successful.
874  *
875  * MT safe.
876  */
877 gboolean
878 gst_pad_activate_push (GstPad * pad, gboolean active)
879 {
880   GstActivateMode old, new;
881
882   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
883   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
884       active ? "activated" : "deactivated");
885
886   GST_OBJECT_LOCK (pad);
887   old = GST_PAD_ACTIVATE_MODE (pad);
888   GST_OBJECT_UNLOCK (pad);
889
890   if (active) {
891     switch (old) {
892       case GST_ACTIVATE_PUSH:
893         GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
894         goto was_ok;
895       case GST_ACTIVATE_PULL:
896         GST_DEBUG_OBJECT (pad,
897             "activating pad from push, deactivating pull first");
898         /* pad was activate in the wrong direction, deactivate it
899          * an reactivate it in push mode */
900         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
901           goto deactivate_failed;
902         /* fallthrough, pad is deactivated now. */
903       case GST_ACTIVATE_NONE:
904         GST_DEBUG_OBJECT (pad, "activating pad from none");
905         break;
906     }
907   } else {
908     switch (old) {
909       case GST_ACTIVATE_NONE:
910         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
911         goto was_ok;
912       case GST_ACTIVATE_PULL:
913         GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
914         /* pad was activated in the other direction, deactivate it
915          * in pull mode, this should not happen... */
916         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
917           goto deactivate_failed;
918         /* everything is fine now */
919         goto was_ok;
920       case GST_ACTIVATE_PUSH:
921         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
922         break;
923     }
924   }
925
926   new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
927   pre_activate (pad, new);
928
929   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
930     if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
931       goto failure;
932     }
933   } else {
934     /* quite ok, element relies on state change func to prepare itself */
935   }
936
937   post_activate (pad, new);
938
939   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
940       active ? "activated" : "deactivated");
941   return TRUE;
942
943 was_ok:
944   {
945     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
946         active ? "activated" : "deactivated");
947     return TRUE;
948   }
949 deactivate_failed:
950   {
951     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
952         "failed to %s in switch to push from mode %d",
953         (active ? "activate" : "deactivate"), old);
954     return FALSE;
955   }
956 failure:
957   {
958     GST_OBJECT_LOCK (pad);
959     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
960         active ? "activate" : "deactivate");
961     GST_PAD_SET_FLUSHING (pad);
962     GST_PAD_ACTIVATE_MODE (pad) = old;
963     GST_OBJECT_UNLOCK (pad);
964     return FALSE;
965   }
966 }
967
968 /**
969  * gst_pad_is_active:
970  * @pad: the #GstPad to query
971  *
972  * Query if a pad is active
973  *
974  * Returns: TRUE if the pad is active.
975  *
976  * MT safe.
977  */
978 gboolean
979 gst_pad_is_active (GstPad * pad)
980 {
981   gboolean result = FALSE;
982
983   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
984
985   GST_OBJECT_LOCK (pad);
986   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
987   GST_OBJECT_UNLOCK (pad);
988
989   return result;
990 }
991
992 /**
993  * gst_pad_set_blocked_async_full:
994  * @pad: the #GstPad to block or unblock
995  * @blocked: boolean indicating whether the pad should be blocked or unblocked
996  * @callback: #GstPadBlockCallback that will be called when the
997  *            operation succeeds
998  * @user_data: user data passed to the callback
999  * @destroy_data: #GDestroyNotify for user_data
1000  *
1001  * Blocks or unblocks the dataflow on a pad. The provided callback
1002  * is called when the operation succeeds; this happens right before the next
1003  * attempt at pushing a buffer on the pad.
1004  *
1005  * This can take a while as the pad can only become blocked when real dataflow
1006  * is happening.
1007  * When the pipeline is stalled, for example in PAUSED, this can
1008  * take an indeterminate amount of time.
1009  * You can pass NULL as the callback to make this call block. Be careful with
1010  * this blocking call as it might not return for reasons stated above.
1011  *
1012  * Returns: TRUE if the pad could be blocked. This function can fail if the
1013  * wrong parameters were passed or the pad was already in the requested state.
1014  *
1015  * MT safe.
1016  *
1017  * Since: 0.10.23
1018  */
1019 gboolean
1020 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1021     GstPadBlockCallback callback, gpointer user_data,
1022     GDestroyNotify destroy_data)
1023 {
1024   gboolean was_blocked = FALSE;
1025
1026   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1027
1028   GST_OBJECT_LOCK (pad);
1029
1030   was_blocked = GST_PAD_IS_BLOCKED (pad);
1031
1032   if (G_UNLIKELY (was_blocked == blocked))
1033     goto had_right_state;
1034
1035   if (blocked) {
1036     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1037
1038     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1039
1040     if (pad->block_destroy_data && pad->block_data)
1041       pad->block_destroy_data (pad->block_data);
1042
1043     pad->block_callback = callback;
1044     pad->block_data = user_data;
1045     pad->block_destroy_data = destroy_data;
1046     pad->abidata.ABI.block_callback_called = FALSE;
1047     if (!callback) {
1048       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1049       GST_PAD_BLOCK_WAIT (pad);
1050       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1051     }
1052   } else {
1053     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1054
1055     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1056
1057     if (pad->block_destroy_data && pad->block_data)
1058       pad->block_destroy_data (pad->block_data);
1059
1060     pad->block_callback = callback;
1061     pad->block_data = user_data;
1062     pad->block_destroy_data = destroy_data;
1063     pad->abidata.ABI.block_callback_called = FALSE;
1064
1065     GST_PAD_BLOCK_BROADCAST (pad);
1066     if (!callback) {
1067       /* no callback, wait for the unblock to happen */
1068       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1069       GST_PAD_BLOCK_WAIT (pad);
1070       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1071     }
1072   }
1073   GST_OBJECT_UNLOCK (pad);
1074
1075   return TRUE;
1076
1077 had_right_state:
1078   {
1079     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1080         "pad was in right state (%d)", was_blocked);
1081     GST_OBJECT_UNLOCK (pad);
1082
1083     return FALSE;
1084   }
1085 }
1086
1087 /**
1088  * gst_pad_set_blocked_async:
1089  * @pad: the #GstPad to block or unblock
1090  * @blocked: boolean indicating whether the pad should be blocked or unblocked
1091  * @callback: #GstPadBlockCallback that will be called when the
1092  *            operation succeeds
1093  * @user_data: user data passed to the callback
1094  *
1095  * Blocks or unblocks the dataflow on a pad. The provided callback
1096  * is called when the operation succeeds; this happens right before the next
1097  * attempt at pushing a buffer on the pad.
1098  *
1099  * This can take a while as the pad can only become blocked when real dataflow
1100  * is happening.
1101  * When the pipeline is stalled, for example in PAUSED, this can
1102  * take an indeterminate amount of time.
1103  * You can pass NULL as the callback to make this call block. Be careful with
1104  * this blocking call as it might not return for reasons stated above.
1105  *
1106  * Returns: TRUE if the pad could be blocked. This function can fail if the
1107  * wrong parameters were passed or the pad was already in the requested state.
1108  *
1109  * MT safe.
1110  */
1111 gboolean
1112 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1113     GstPadBlockCallback callback, gpointer user_data)
1114 {
1115   return gst_pad_set_blocked_async_full (pad, blocked,
1116       callback, user_data, NULL);
1117 }
1118
1119 /**
1120  * gst_pad_set_blocked:
1121  * @pad: the #GstPad to block or unblock
1122  * @blocked: boolean indicating we should block or unblock
1123  *
1124  * Blocks or unblocks the dataflow on a pad. This function is
1125  * a shortcut for gst_pad_set_blocked_async() with a NULL
1126  * callback.
1127  *
1128  * Returns: TRUE if the pad could be blocked. This function can fail if the
1129  * wrong parameters were passed or the pad was already in the requested state.
1130  *
1131  * MT safe.
1132  */
1133 gboolean
1134 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1135 {
1136   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1137 }
1138
1139 /**
1140  * gst_pad_is_blocked:
1141  * @pad: the #GstPad to query
1142  *
1143  * Checks if the pad is blocked or not. This function returns the
1144  * last requested state of the pad. It is not certain that the pad
1145  * is actually blocking at this point (see gst_pad_is_blocking()).
1146  *
1147  * Returns: TRUE if the pad is blocked.
1148  *
1149  * MT safe.
1150  */
1151 gboolean
1152 gst_pad_is_blocked (GstPad * pad)
1153 {
1154   gboolean result = FALSE;
1155
1156   g_return_val_if_fail (GST_IS_PAD (pad), result);
1157
1158   GST_OBJECT_LOCK (pad);
1159   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1160   GST_OBJECT_UNLOCK (pad);
1161
1162   return result;
1163 }
1164
1165 /**
1166  * gst_pad_is_blocking:
1167  * @pad: the #GstPad to query
1168  *
1169  * Checks if the pad is blocking or not. This is a guaranteed state
1170  * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1171  *
1172  * Returns: TRUE if the pad is blocking.
1173  *
1174  * MT safe.
1175  *
1176  * Since: 0.10.11
1177  */
1178 gboolean
1179 gst_pad_is_blocking (GstPad * pad)
1180 {
1181   gboolean result = FALSE;
1182
1183   g_return_val_if_fail (GST_IS_PAD (pad), result);
1184
1185   GST_OBJECT_LOCK (pad);
1186   /* the blocking flag is only valid if the pad is not flushing */
1187   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1188       !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1189   GST_OBJECT_UNLOCK (pad);
1190
1191   return result;
1192 }
1193
1194 /**
1195  * gst_pad_set_activate_function:
1196  * @pad: a #GstPad.
1197  * @activate: the #GstPadActivateFunction to set.
1198  *
1199  * Sets the given activate function for @pad. The activate function will
1200  * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1201  * the actual activation. Only makes sense to set on sink pads.
1202  *
1203  * Call this function if your sink pad can start a pull-based task.
1204  */
1205 void
1206 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1207 {
1208   g_return_if_fail (GST_IS_PAD (pad));
1209
1210   GST_PAD_ACTIVATEFUNC (pad) = activate;
1211   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1212       GST_DEBUG_FUNCPTR_NAME (activate));
1213 }
1214
1215 /**
1216  * gst_pad_set_activatepull_function:
1217  * @pad: a #GstPad.
1218  * @activatepull: the #GstPadActivateModeFunction to set.
1219  *
1220  * Sets the given activate_pull function for the pad. An activate_pull function
1221  * prepares the element and any upstream connections for pulling. See XXX
1222  * part-activation.txt for details.
1223  */
1224 void
1225 gst_pad_set_activatepull_function (GstPad * pad,
1226     GstPadActivateModeFunction activatepull)
1227 {
1228   g_return_if_fail (GST_IS_PAD (pad));
1229
1230   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1231   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1232       GST_DEBUG_FUNCPTR_NAME (activatepull));
1233 }
1234
1235 /**
1236  * gst_pad_set_activatepush_function:
1237  * @pad: a #GstPad.
1238  * @activatepush: the #GstPadActivateModeFunction to set.
1239  *
1240  * Sets the given activate_push function for the pad. An activate_push function
1241  * prepares the element for pushing. See XXX part-activation.txt for details.
1242  */
1243 void
1244 gst_pad_set_activatepush_function (GstPad * pad,
1245     GstPadActivateModeFunction activatepush)
1246 {
1247   g_return_if_fail (GST_IS_PAD (pad));
1248
1249   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1250   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1251       GST_DEBUG_FUNCPTR_NAME (activatepush));
1252 }
1253
1254 /**
1255  * gst_pad_set_chain_function:
1256  * @pad: a sink #GstPad.
1257  * @chain: the #GstPadChainFunction to set.
1258  *
1259  * Sets the given chain function for the pad. The chain function is called to
1260  * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1261  */
1262 void
1263 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1264 {
1265   g_return_if_fail (GST_IS_PAD (pad));
1266   g_return_if_fail (GST_PAD_IS_SINK (pad));
1267
1268   GST_PAD_CHAINFUNC (pad) = chain;
1269   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1270       GST_DEBUG_FUNCPTR_NAME (chain));
1271 }
1272
1273 /**
1274  * gst_pad_set_chain_list_function:
1275  * @pad: a sink #GstPad.
1276  * @chainlist: the #GstPadChainListFunction to set.
1277  *
1278  * Sets the given chain list function for the pad. The chainlist function is
1279  * called to process a #GstBufferList input buffer list. See
1280  * #GstPadChainListFunction for more details.
1281  *
1282  * Since: 0.10.24
1283  */
1284 void
1285 gst_pad_set_chain_list_function (GstPad * pad,
1286     GstPadChainListFunction chainlist)
1287 {
1288   g_return_if_fail (GST_IS_PAD (pad));
1289   g_return_if_fail (GST_PAD_IS_SINK (pad));
1290
1291   GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1292   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1293       GST_DEBUG_FUNCPTR_NAME (chainlist));
1294 }
1295
1296 /**
1297  * gst_pad_set_getrange_function:
1298  * @pad: a source #GstPad.
1299  * @get: the #GstPadGetRangeFunction to set.
1300  *
1301  * Sets the given getrange function for the pad. The getrange function is
1302  * called to produce a new #GstBuffer to start the processing pipeline. see
1303  * #GstPadGetRangeFunction for a description of the getrange function.
1304  */
1305 void
1306 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1307 {
1308   g_return_if_fail (GST_IS_PAD (pad));
1309   g_return_if_fail (GST_PAD_IS_SRC (pad));
1310
1311   GST_PAD_GETRANGEFUNC (pad) = get;
1312
1313   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1314       GST_DEBUG_FUNCPTR_NAME (get));
1315 }
1316
1317 /**
1318  * gst_pad_set_checkgetrange_function:
1319  * @pad: a source #GstPad.
1320  * @check: the #GstPadCheckGetRangeFunction to set.
1321  *
1322  * Sets the given checkgetrange function for the pad. Implement this function
1323  * on a pad if you dynamically support getrange based scheduling on the pad.
1324  */
1325 void
1326 gst_pad_set_checkgetrange_function (GstPad * pad,
1327     GstPadCheckGetRangeFunction check)
1328 {
1329   g_return_if_fail (GST_IS_PAD (pad));
1330   g_return_if_fail (GST_PAD_IS_SRC (pad));
1331
1332   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1333
1334   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1335       GST_DEBUG_FUNCPTR_NAME (check));
1336 }
1337
1338 /**
1339  * gst_pad_set_event_function:
1340  * @pad: a #GstPad of either direction.
1341  * @event: the #GstPadEventFunction to set.
1342  *
1343  * Sets the given event handler for the pad.
1344  */
1345 void
1346 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1347 {
1348   g_return_if_fail (GST_IS_PAD (pad));
1349
1350   GST_PAD_EVENTFUNC (pad) = event;
1351
1352   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1353       GST_DEBUG_FUNCPTR_NAME (event));
1354 }
1355
1356 /**
1357  * gst_pad_set_query_function:
1358  * @pad: a #GstPad of either direction.
1359  * @query: the #GstPadQueryFunction to set.
1360  *
1361  * Set the given query function for the pad.
1362  */
1363 void
1364 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1365 {
1366   g_return_if_fail (GST_IS_PAD (pad));
1367
1368   GST_PAD_QUERYFUNC (pad) = query;
1369
1370   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1371       GST_DEBUG_FUNCPTR_NAME (query));
1372 }
1373
1374 /**
1375  * gst_pad_set_query_type_function:
1376  * @pad: a #GstPad of either direction.
1377  * @type_func: the #GstPadQueryTypeFunction to set.
1378  *
1379  * Set the given query type function for the pad.
1380  */
1381 void
1382 gst_pad_set_query_type_function (GstPad * pad,
1383     GstPadQueryTypeFunction type_func)
1384 {
1385   g_return_if_fail (GST_IS_PAD (pad));
1386
1387   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1388
1389   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1390       GST_DEBUG_FUNCPTR_NAME (type_func));
1391 }
1392
1393 /**
1394  * gst_pad_get_query_types:
1395  * @pad: a #GstPad.
1396  *
1397  * Get an array of supported queries that can be performed
1398  * on this pad.
1399  *
1400  * Returns: a zero-terminated array of #GstQueryType.
1401  */
1402 const GstQueryType *
1403 gst_pad_get_query_types (GstPad * pad)
1404 {
1405   GstPadQueryTypeFunction func;
1406
1407   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1408
1409   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1410     goto no_func;
1411
1412   return func (pad);
1413
1414 no_func:
1415   {
1416     return NULL;
1417   }
1418 }
1419
1420 static gboolean
1421 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1422 {
1423   *data = gst_pad_get_query_types (pad);
1424
1425   return TRUE;
1426 }
1427
1428 /**
1429  * gst_pad_get_query_types_default:
1430  * @pad: a #GstPad.
1431  *
1432  * Invoke the default dispatcher for the query types on
1433  * the pad.
1434  *
1435  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1436  * internally-linked pads has a query types function.
1437  */
1438 const GstQueryType *
1439 gst_pad_get_query_types_default (GstPad * pad)
1440 {
1441   GstQueryType *result = NULL;
1442
1443   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1444
1445   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1446       gst_pad_get_query_types_dispatcher, &result);
1447
1448   return result;
1449 }
1450
1451 /**
1452  * gst_pad_set_iterate_internal_links_function:
1453  * @pad: a #GstPad of either direction.
1454  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1455  *
1456  * Sets the given internal link iterator function for the pad.
1457  *
1458  * Since: 0.10.21
1459  */
1460 void
1461 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1462     GstPadIterIntLinkFunction iterintlink)
1463 {
1464   g_return_if_fail (GST_IS_PAD (pad));
1465
1466   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1467   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1468       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1469 }
1470
1471 /**
1472  * gst_pad_set_internal_link_function:
1473  * @pad: a #GstPad of either direction.
1474  * @intlink: the #GstPadIntLinkFunction to set.
1475  *
1476  * Sets the given internal link function for the pad.
1477  *
1478  * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1479  */
1480 #ifndef GST_REMOVE_DEPRECATED
1481 #ifdef GST_DISABLE_DEPRECATED
1482 void
1483 gst_pad_set_internal_link_function (GstPad * pad,
1484     GstPadIntLinkFunction intlink);
1485 #endif
1486 void
1487 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1488 {
1489   g_return_if_fail (GST_IS_PAD (pad));
1490
1491   GST_PAD_INTLINKFUNC (pad) = intlink;
1492   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1493       GST_DEBUG_FUNCPTR_NAME (intlink));
1494 }
1495 #endif /* GST_REMOVE_DEPRECATED */
1496
1497 /**
1498  * gst_pad_set_link_function:
1499  * @pad: a #GstPad.
1500  * @link: the #GstPadLinkFunction to set.
1501  *
1502  * Sets the given link function for the pad. It will be called when
1503  * the pad is linked with another pad.
1504  *
1505  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1506  * made.
1507  *
1508  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1509  * cannot be made for some reason.
1510  *
1511  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1512  * of the peer sink pad, if present.
1513  */
1514 void
1515 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1516 {
1517   g_return_if_fail (GST_IS_PAD (pad));
1518
1519   GST_PAD_LINKFUNC (pad) = link;
1520   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1521       GST_DEBUG_FUNCPTR_NAME (link));
1522 }
1523
1524 /**
1525  * gst_pad_set_unlink_function:
1526  * @pad: a #GstPad.
1527  * @unlink: the #GstPadUnlinkFunction to set.
1528  *
1529  * Sets the given unlink function for the pad. It will be called
1530  * when the pad is unlinked.
1531  */
1532 void
1533 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1534 {
1535   g_return_if_fail (GST_IS_PAD (pad));
1536
1537   GST_PAD_UNLINKFUNC (pad) = unlink;
1538   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1539       GST_DEBUG_FUNCPTR_NAME (unlink));
1540 }
1541
1542 /**
1543  * gst_pad_set_getcaps_function:
1544  * @pad: a #GstPad.
1545  * @getcaps: the #GstPadGetCapsFunction to set.
1546  *
1547  * Sets the given getcaps function for the pad. @getcaps should return the
1548  * allowable caps for a pad in the context of the element's state, its link to
1549  * other elements, and the devices or files it has opened. These caps must be a
1550  * subset of the pad template caps. In the NULL state with no links, @getcaps
1551  * should ideally return the same caps as the pad template. In rare
1552  * circumstances, an object property can affect the caps returned by @getcaps,
1553  * but this is discouraged.
1554  *
1555  * You do not need to call this function if @pad's allowed caps are always the
1556  * same as the pad template caps. This can only be true if the padtemplate
1557  * has fixed simple caps.
1558  *
1559  * For most filters, the caps returned by @getcaps is directly affected by the
1560  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1561  * the srcpad's getcaps function is directly related to the stream data. Again,
1562  * @getcaps should return the most specific caps it reasonably can, since this
1563  * helps with autoplugging.
1564  *
1565  * Note that the return value from @getcaps is owned by the caller, so the
1566  * caller should unref the caps after usage.
1567  */
1568 void
1569 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1570 {
1571   g_return_if_fail (GST_IS_PAD (pad));
1572
1573   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1574   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1575       GST_DEBUG_FUNCPTR_NAME (getcaps));
1576 }
1577
1578 /**
1579  * gst_pad_set_acceptcaps_function:
1580  * @pad: a #GstPad.
1581  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1582  *
1583  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1584  * will be called to check if the pad can accept the given caps. Setting the
1585  * acceptcaps function to NULL restores the default behaviour of allowing
1586  * any caps that matches the caps from gst_pad_get_caps().
1587  */
1588 void
1589 gst_pad_set_acceptcaps_function (GstPad * pad,
1590     GstPadAcceptCapsFunction acceptcaps)
1591 {
1592   g_return_if_fail (GST_IS_PAD (pad));
1593
1594   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1595   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1596       GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1597 }
1598
1599 /**
1600  * gst_pad_set_fixatecaps_function:
1601  * @pad: a #GstPad.
1602  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1603  *
1604  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1605  * will be called whenever the default values for a GstCaps needs to be
1606  * filled in.
1607  */
1608 void
1609 gst_pad_set_fixatecaps_function (GstPad * pad,
1610     GstPadFixateCapsFunction fixatecaps)
1611 {
1612   g_return_if_fail (GST_IS_PAD (pad));
1613
1614   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1615   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1616       GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1617 }
1618
1619 /**
1620  * gst_pad_set_setcaps_function:
1621  * @pad: a #GstPad.
1622  * @setcaps: the #GstPadSetCapsFunction to set.
1623  *
1624  * Sets the given setcaps function for the pad.  The setcaps function
1625  * will be called whenever a buffer with a new media type is pushed or
1626  * pulled from the pad. The pad/element needs to update its internal
1627  * structures to process the new media type. If this new type is not
1628  * acceptable, the setcaps function should return FALSE.
1629  */
1630 void
1631 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1632 {
1633   g_return_if_fail (GST_IS_PAD (pad));
1634
1635   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1636   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1637       GST_DEBUG_FUNCPTR_NAME (setcaps));
1638 }
1639
1640 /**
1641  * gst_pad_set_bufferalloc_function:
1642  * @pad: a sink #GstPad.
1643  * @bufalloc: the #GstPadBufferAllocFunction to set.
1644  *
1645  * Sets the given bufferalloc function for the pad. Note that the
1646  * bufferalloc function can only be set on sinkpads.
1647  */
1648 void
1649 gst_pad_set_bufferalloc_function (GstPad * pad,
1650     GstPadBufferAllocFunction bufalloc)
1651 {
1652   g_return_if_fail (GST_IS_PAD (pad));
1653   g_return_if_fail (GST_PAD_IS_SINK (pad));
1654
1655   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1656   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1657       GST_DEBUG_FUNCPTR_NAME (bufalloc));
1658 }
1659
1660 /**
1661  * gst_pad_unlink:
1662  * @srcpad: the source #GstPad to unlink.
1663  * @sinkpad: the sink #GstPad to unlink.
1664  *
1665  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1666  * signal on both pads.
1667  *
1668  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1669  * the pads were not linked together.
1670  *
1671  * MT safe.
1672  */
1673 gboolean
1674 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1675 {
1676   gboolean result = FALSE;
1677   GstElement *parent = NULL;
1678
1679   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1680   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1681   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1682   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1683
1684   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1685       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1686       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1687
1688   /* We need to notify the parent before taking any pad locks as the bin in
1689    * question might be waiting for a lock on the pad while holding its lock
1690    * that our message will try to take. */
1691   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1692     if (GST_IS_ELEMENT (parent)) {
1693       gst_element_post_message (parent,
1694           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1695               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1696     } else {
1697       gst_object_unref (parent);
1698       parent = NULL;
1699     }
1700   }
1701
1702   GST_OBJECT_LOCK (srcpad);
1703
1704   GST_OBJECT_LOCK (sinkpad);
1705
1706   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1707     goto not_linked_together;
1708
1709   if (GST_PAD_UNLINKFUNC (srcpad)) {
1710     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1711   }
1712   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1713     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1714   }
1715
1716   /* first clear peers */
1717   GST_PAD_PEER (srcpad) = NULL;
1718   GST_PAD_PEER (sinkpad) = NULL;
1719
1720   GST_OBJECT_UNLOCK (sinkpad);
1721   GST_OBJECT_UNLOCK (srcpad);
1722
1723   /* fire off a signal to each of the pads telling them
1724    * that they've been unlinked */
1725   g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1726   g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1727
1728   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1729       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1730
1731   result = TRUE;
1732
1733 done:
1734   if (parent != NULL) {
1735     gst_element_post_message (parent,
1736         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1737             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1738     gst_object_unref (parent);
1739   }
1740   return result;
1741
1742   /* ERRORS */
1743 not_linked_together:
1744   {
1745     /* we do not emit a warning in this case because unlinking cannot
1746      * be made MT safe.*/
1747     GST_OBJECT_UNLOCK (sinkpad);
1748     GST_OBJECT_UNLOCK (srcpad);
1749     goto done;
1750   }
1751 }
1752
1753 /**
1754  * gst_pad_is_linked:
1755  * @pad: pad to check
1756  *
1757  * Checks if a @pad is linked to another pad or not.
1758  *
1759  * Returns: TRUE if the pad is linked, FALSE otherwise.
1760  *
1761  * MT safe.
1762  */
1763 gboolean
1764 gst_pad_is_linked (GstPad * pad)
1765 {
1766   gboolean result;
1767
1768   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1769
1770   GST_OBJECT_LOCK (pad);
1771   result = (GST_PAD_PEER (pad) != NULL);
1772   GST_OBJECT_UNLOCK (pad);
1773
1774   return result;
1775 }
1776
1777 /* get the caps from both pads and see if the intersection
1778  * is not empty.
1779  *
1780  * This function should be called with the pad LOCK on both
1781  * pads
1782  */
1783 static gboolean
1784 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1785     GstPadLinkCheck flags)
1786 {
1787   GstCaps *srccaps = NULL;
1788   GstCaps *sinkcaps = NULL;
1789   gboolean compatible = FALSE;
1790
1791   if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1792     return TRUE;
1793
1794   /* Doing the expensive caps checking takes priority over only checking the template caps */
1795   if (flags & GST_PAD_LINK_CHECK_CAPS) {
1796     srccaps = gst_pad_get_caps_unlocked (src);
1797     sinkcaps = gst_pad_get_caps_unlocked (sink);
1798   } else {
1799     /* If one of the two pads doesn't have a template, consider the intersection
1800      * as valid.*/
1801     if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1802             || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1803       compatible = TRUE;
1804       goto done;
1805     }
1806     srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1807     sinkcaps =
1808         gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1809   }
1810
1811   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1812       srccaps);
1813   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1814       sinkcaps);
1815
1816   /* if we have caps on both pads we can check the intersection. If one
1817    * of the caps is NULL, we return TRUE. */
1818   if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1819     if (srccaps)
1820       gst_caps_unref (srccaps);
1821     if (sinkcaps)
1822       gst_caps_unref (sinkcaps);
1823     goto done;
1824   }
1825
1826   compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1827   gst_caps_unref (srccaps);
1828   gst_caps_unref (sinkcaps);
1829
1830 done:
1831   GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1832       (compatible ? "" : "not"));
1833
1834   return compatible;
1835 }
1836
1837 /* check if the grandparents of both pads are the same.
1838  * This check is required so that we don't try to link
1839  * pads from elements in different bins without ghostpads.
1840  *
1841  * The LOCK should be held on both pads
1842  */
1843 static gboolean
1844 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1845 {
1846   GstObject *psrc, *psink;
1847
1848   psrc = GST_OBJECT_PARENT (src);
1849   psink = GST_OBJECT_PARENT (sink);
1850
1851   /* if one of the pads has no parent, we allow the link */
1852   if (G_UNLIKELY (psrc == NULL || psink == NULL))
1853     goto no_parent;
1854
1855   /* only care about parents that are elements */
1856   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1857     goto no_element_parent;
1858
1859   /* if the parents are the same, we have a loop */
1860   if (G_UNLIKELY (psrc == psink))
1861     goto same_parents;
1862
1863   /* if they both have a parent, we check the grandparents. We can not lock
1864    * the parent because we hold on the child (pad) and the locking order is
1865    * parent >> child. */
1866   psrc = GST_OBJECT_PARENT (psrc);
1867   psink = GST_OBJECT_PARENT (psink);
1868
1869   /* if they have grandparents but they are not the same */
1870   if (G_UNLIKELY (psrc != psink))
1871     goto wrong_grandparents;
1872
1873   return TRUE;
1874
1875   /* ERRORS */
1876 no_parent:
1877   {
1878     GST_CAT_DEBUG (GST_CAT_CAPS,
1879         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1880         GST_PTR_FORMAT, psrc, psink);
1881     return TRUE;
1882   }
1883 no_element_parent:
1884   {
1885     GST_CAT_DEBUG (GST_CAT_CAPS,
1886         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1887         GST_PTR_FORMAT, psrc, psink);
1888     return TRUE;
1889   }
1890 same_parents:
1891   {
1892     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1893         psrc);
1894     return FALSE;
1895   }
1896 wrong_grandparents:
1897   {
1898     GST_CAT_DEBUG (GST_CAT_CAPS,
1899         "pads have different grandparents %" GST_PTR_FORMAT " and %"
1900         GST_PTR_FORMAT, psrc, psink);
1901     return FALSE;
1902   }
1903 }
1904
1905 /* FIXME leftover from an attempt at refactoring... */
1906 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1907  * the two pads will be locked in the srcpad, sinkpad order. */
1908 static GstPadLinkReturn
1909 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1910 {
1911   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1912       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1913
1914   GST_OBJECT_LOCK (srcpad);
1915
1916   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1917     goto src_was_linked;
1918
1919   GST_OBJECT_LOCK (sinkpad);
1920
1921   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1922     goto sink_was_linked;
1923
1924   /* check hierarchy, pads can only be linked if the grandparents
1925    * are the same. */
1926   if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1927       && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1928     goto wrong_hierarchy;
1929
1930   /* check pad caps for non-empty intersection */
1931   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1932     goto no_format;
1933
1934   /* FIXME check pad scheduling for non-empty intersection */
1935
1936   return GST_PAD_LINK_OK;
1937
1938 src_was_linked:
1939   {
1940     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1941         GST_DEBUG_PAD_NAME (srcpad),
1942         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1943     /* we do not emit a warning in this case because unlinking cannot
1944      * be made MT safe.*/
1945     GST_OBJECT_UNLOCK (srcpad);
1946     return GST_PAD_LINK_WAS_LINKED;
1947   }
1948 sink_was_linked:
1949   {
1950     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1951         GST_DEBUG_PAD_NAME (sinkpad),
1952         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1953     /* we do not emit a warning in this case because unlinking cannot
1954      * be made MT safe.*/
1955     GST_OBJECT_UNLOCK (sinkpad);
1956     GST_OBJECT_UNLOCK (srcpad);
1957     return GST_PAD_LINK_WAS_LINKED;
1958   }
1959 wrong_hierarchy:
1960   {
1961     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1962     GST_OBJECT_UNLOCK (sinkpad);
1963     GST_OBJECT_UNLOCK (srcpad);
1964     return GST_PAD_LINK_WRONG_HIERARCHY;
1965   }
1966 no_format:
1967   {
1968     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1969     GST_OBJECT_UNLOCK (sinkpad);
1970     GST_OBJECT_UNLOCK (srcpad);
1971     return GST_PAD_LINK_NOFORMAT;
1972   }
1973 }
1974
1975 /**
1976  * gst_pad_can_link:
1977  * @srcpad: the source #GstPad.
1978  * @sinkpad: the sink #GstPad.
1979  *
1980  * Checks if the source pad and the sink pad are compatible so they can be
1981  * linked. 
1982  *
1983  * Returns: TRUE if the pads can be linked.
1984  */
1985 gboolean
1986 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1987 {
1988   GstPadLinkReturn result;
1989
1990   /* generic checks */
1991   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1992   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1993
1994   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1995       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1996
1997   /* gst_pad_link_prepare does everything for us, we only release the locks
1998    * on the pads that it gets us. If this function returns !OK the locks are not
1999    * taken anymore. */
2000   result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2001   if (result != GST_PAD_LINK_OK)
2002     goto done;
2003
2004   GST_OBJECT_UNLOCK (srcpad);
2005   GST_OBJECT_UNLOCK (sinkpad);
2006
2007 done:
2008   return result == GST_PAD_LINK_OK;
2009 }
2010
2011 /**
2012  * gst_pad_link_full:
2013  * @srcpad: the source #GstPad to link.
2014  * @sinkpad: the sink #GstPad to link.
2015  * @flags: the checks to validate when linking
2016  *
2017  * Links the source pad and the sink pad.
2018  *
2019  * This variant of #gst_pad_link provides a more granular control on the
2020  * checks being done when linking. While providing some considerable speedups
2021  * the caller of this method must be aware that wrong usage of those flags
2022  * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2023  * for more information.
2024  *
2025  * MT Safe.
2026  *
2027  * Returns: A result code indicating if the connection worked or
2028  *          what went wrong.
2029  *
2030  * Since: 0.10.30
2031  */
2032 GstPadLinkReturn
2033 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2034 {
2035   GstPadLinkReturn result;
2036   GstElement *parent;
2037
2038   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2039   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2040   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2041   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2042       GST_PAD_LINK_WRONG_DIRECTION);
2043
2044   /* Notify the parent early. See gst_pad_unlink for details. */
2045   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2046     if (GST_IS_ELEMENT (parent)) {
2047       gst_element_post_message (parent,
2048           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2049               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2050     } else {
2051       gst_object_unref (parent);
2052       parent = NULL;
2053     }
2054   }
2055
2056   /* prepare will also lock the two pads */
2057   result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2058
2059   if (result != GST_PAD_LINK_OK)
2060     goto done;
2061
2062   /* must set peers before calling the link function */
2063   GST_PAD_PEER (srcpad) = sinkpad;
2064   GST_PAD_PEER (sinkpad) = srcpad;
2065
2066   GST_OBJECT_UNLOCK (sinkpad);
2067   GST_OBJECT_UNLOCK (srcpad);
2068
2069   /* FIXME released the locks here, concurrent thread might link
2070    * something else. */
2071   if (GST_PAD_LINKFUNC (srcpad)) {
2072     /* this one will call the peer link function */
2073     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2074   } else if (GST_PAD_LINKFUNC (sinkpad)) {
2075     /* if no source link function, we need to call the sink link
2076      * function ourselves. */
2077     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2078   } else {
2079     result = GST_PAD_LINK_OK;
2080   }
2081
2082   GST_OBJECT_LOCK (srcpad);
2083   GST_OBJECT_LOCK (sinkpad);
2084
2085   if (result == GST_PAD_LINK_OK) {
2086     GST_OBJECT_UNLOCK (sinkpad);
2087     GST_OBJECT_UNLOCK (srcpad);
2088
2089     /* fire off a signal to each of the pads telling them
2090      * that they've been linked */
2091     g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2092     g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2093
2094     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2095         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2096   } else {
2097     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2098         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2099
2100     GST_PAD_PEER (srcpad) = NULL;
2101     GST_PAD_PEER (sinkpad) = NULL;
2102
2103     GST_OBJECT_UNLOCK (sinkpad);
2104     GST_OBJECT_UNLOCK (srcpad);
2105   }
2106
2107 done:
2108   if (parent) {
2109     gst_element_post_message (parent,
2110         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2111             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2112     gst_object_unref (parent);
2113   }
2114
2115   return result;
2116 }
2117
2118 /**
2119  * gst_pad_link:
2120  * @srcpad: the source #GstPad to link.
2121  * @sinkpad: the sink #GstPad to link.
2122  *
2123  * Links the source pad and the sink pad.
2124  *
2125  * Returns: A result code indicating if the connection worked or
2126  *          what went wrong.
2127  *
2128  * MT Safe.
2129  */
2130 GstPadLinkReturn
2131 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2132 {
2133   return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2134 }
2135
2136 static void
2137 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2138 {
2139   GstPadTemplate **template_p;
2140
2141   /* this function would need checks if it weren't static */
2142
2143   GST_OBJECT_LOCK (pad);
2144   template_p = &pad->padtemplate;
2145   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2146   GST_OBJECT_UNLOCK (pad);
2147
2148   if (templ)
2149     gst_pad_template_pad_created (templ, pad);
2150 }
2151
2152 /**
2153  * gst_pad_get_pad_template:
2154  * @pad: a #GstPad.
2155  *
2156  * Gets the template for @pad.
2157  *
2158  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2159  * if this pad has no template.
2160  *
2161  * FIXME: currently returns an unrefcounted padtemplate.
2162  */
2163 GstPadTemplate *
2164 gst_pad_get_pad_template (GstPad * pad)
2165 {
2166   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2167
2168   return GST_PAD_PAD_TEMPLATE (pad);
2169 }
2170
2171
2172 /* should be called with the pad LOCK held */
2173 /* refs the caps, so caller is responsible for getting it unreffed */
2174 static GstCaps *
2175 gst_pad_get_caps_unlocked (GstPad * pad)
2176 {
2177   GstCaps *result = NULL;
2178   GstPadTemplate *templ;
2179
2180   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2181
2182   if (GST_PAD_GETCAPSFUNC (pad)) {
2183     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2184         "dispatching to pad getcaps function");
2185
2186     GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2187     GST_OBJECT_UNLOCK (pad);
2188     result = GST_PAD_GETCAPSFUNC (pad) (pad);
2189     GST_OBJECT_LOCK (pad);
2190     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2191
2192     if (result == NULL) {
2193       g_critical ("pad %s:%s returned NULL caps from getcaps function",
2194           GST_DEBUG_PAD_NAME (pad));
2195     } else {
2196       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2197           "pad getcaps returned %" GST_PTR_FORMAT, result);
2198 #ifndef G_DISABLE_ASSERT
2199       /* check that the returned caps are a real subset of the template caps */
2200       if (GST_PAD_PAD_TEMPLATE (pad)) {
2201         const GstCaps *templ_caps =
2202             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2203         if (!gst_caps_is_subset (result, templ_caps)) {
2204           GstCaps *temp;
2205
2206           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2207               "pad returned caps %" GST_PTR_FORMAT
2208               " which are not a real subset of its template caps %"
2209               GST_PTR_FORMAT, result, templ_caps);
2210           g_warning
2211               ("pad %s:%s returned caps which are not a real "
2212               "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2213           temp = gst_caps_intersect (templ_caps, result);
2214           gst_caps_unref (result);
2215           result = temp;
2216         }
2217       }
2218 #endif
2219       goto done;
2220     }
2221   }
2222   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2223     result = GST_PAD_TEMPLATE_CAPS (templ);
2224     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2225         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2226         result);
2227
2228     result = gst_caps_ref (result);
2229     goto done;
2230   }
2231   if ((result = GST_PAD_CAPS (pad))) {
2232     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2233         "using pad caps %p %" GST_PTR_FORMAT, result, result);
2234
2235     result = gst_caps_ref (result);
2236     goto done;
2237   }
2238
2239   /* this almost never happens */
2240   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2241   result = gst_caps_new_empty ();
2242
2243 done:
2244   return result;
2245 }
2246
2247 /* FIXME-0.11: what about making this the default and using
2248  * gst_caps_make_writable() explicitely where needed
2249  */
2250 /**
2251  * gst_pad_get_caps_reffed:
2252  * @pad: a  #GstPad to get the capabilities of.
2253  *
2254  * Gets the capabilities this pad can produce or consume. Preferred function if
2255  * one only wants to read or intersect the caps.
2256  *
2257  * Returns: the caps of the pad with incremented ref-count.
2258  *
2259  * Since: 0.10.26
2260  */
2261 GstCaps *
2262 gst_pad_get_caps_reffed (GstPad * pad)
2263 {
2264   GstCaps *result = NULL;
2265
2266   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2267
2268   GST_OBJECT_LOCK (pad);
2269
2270   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2271
2272   result = gst_pad_get_caps_unlocked (pad);
2273
2274   GST_OBJECT_UNLOCK (pad);
2275
2276   return result;
2277 }
2278
2279 /**
2280  * gst_pad_get_caps:
2281  * @pad: a  #GstPad to get the capabilities of.
2282  *
2283  * Gets the capabilities this pad can produce or consume.
2284  * Note that this method doesn't necessarily return the caps set by
2285  * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2286  * gst_pad_get_caps returns all possible caps a pad can operate with, using
2287  * the pad's get_caps function;
2288  * this returns the pad template caps if not explicitly set.
2289  *
2290  * Returns: a newly allocated copy of the #GstCaps of this pad.
2291  *
2292  * MT safe.
2293  */
2294 GstCaps *
2295 gst_pad_get_caps (GstPad * pad)
2296 {
2297   GstCaps *result = gst_pad_get_caps_reffed (pad);
2298
2299   /* be sure that we have a copy */
2300   if (G_LIKELY (result))
2301     result = gst_caps_make_writable (result);
2302
2303   return result;
2304 }
2305
2306 /* FIXME-0.11: what about making this the default and using
2307  * gst_caps_make_writable() explicitely where needed
2308  */
2309 /**
2310  * gst_pad_peer_get_caps_reffed:
2311  * @pad: a  #GstPad to get the capabilities of.
2312  *
2313  * Gets the capabilities of the peer connected to this pad. Preferred function
2314  * if one only wants to read or intersect the caps.
2315  *
2316  * Returns: the caps of the pad with incremented ref-count.
2317  *
2318  * Since: 0.10.26
2319  */
2320 GstCaps *
2321 gst_pad_peer_get_caps_reffed (GstPad * pad)
2322 {
2323   GstPad *peerpad;
2324   GstCaps *result = NULL;
2325
2326   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2327
2328   GST_OBJECT_LOCK (pad);
2329
2330   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2331
2332   peerpad = GST_PAD_PEER (pad);
2333   if (G_UNLIKELY (peerpad == NULL))
2334     goto no_peer;
2335
2336   gst_object_ref (peerpad);
2337   GST_OBJECT_UNLOCK (pad);
2338
2339   result = gst_pad_get_caps_reffed (peerpad);
2340
2341   gst_object_unref (peerpad);
2342
2343   return result;
2344
2345 no_peer:
2346   {
2347     GST_OBJECT_UNLOCK (pad);
2348     return NULL;
2349   }
2350 }
2351
2352 /**
2353  * gst_pad_peer_get_caps:
2354  * @pad: a  #GstPad to get the peer capabilities of.
2355  *
2356  * Gets the capabilities of the peer connected to this pad. Similar to
2357  * gst_pad_get_caps().
2358  *
2359  * Returns: a newly allocated copy of the #GstCaps of the peer pad. Use
2360  * gst_caps_unref() to get rid of it. This function returns %NULL if there is
2361  * no peer pad.
2362  */
2363 GstCaps *
2364 gst_pad_peer_get_caps (GstPad * pad)
2365 {
2366   GstPad *peerpad;
2367   GstCaps *result = NULL;
2368
2369   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2370
2371   GST_OBJECT_LOCK (pad);
2372
2373   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2374
2375   peerpad = GST_PAD_PEER (pad);
2376   if (G_UNLIKELY (peerpad == NULL))
2377     goto no_peer;
2378
2379   gst_object_ref (peerpad);
2380   GST_OBJECT_UNLOCK (pad);
2381
2382   result = gst_pad_get_caps (peerpad);
2383
2384   gst_object_unref (peerpad);
2385
2386   return result;
2387
2388 no_peer:
2389   {
2390     GST_OBJECT_UNLOCK (pad);
2391     return NULL;
2392   }
2393 }
2394
2395 static gboolean
2396 fixate_value (GValue * dest, const GValue * src)
2397 {
2398   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2399     g_value_init (dest, G_TYPE_INT);
2400     g_value_set_int (dest, gst_value_get_int_range_min (src));
2401   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2402     g_value_init (dest, G_TYPE_DOUBLE);
2403     g_value_set_double (dest, gst_value_get_double_range_min (src));
2404   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2405     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2406   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2407     GValue temp = { 0 };
2408
2409     /* list could be empty */
2410     if (gst_value_list_get_size (src) <= 0)
2411       return FALSE;
2412
2413     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2414
2415     if (!fixate_value (dest, &temp))
2416       gst_value_init_and_copy (dest, &temp);
2417     g_value_unset (&temp);
2418   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2419     gboolean res = FALSE;
2420     guint n, len;
2421
2422     len = gst_value_array_get_size (src);
2423     g_value_init (dest, GST_TYPE_ARRAY);
2424     for (n = 0; n < len; n++) {
2425       GValue kid = { 0 };
2426       const GValue *orig_kid = gst_value_array_get_value (src, n);
2427
2428       if (!fixate_value (&kid, orig_kid))
2429         gst_value_init_and_copy (&kid, orig_kid);
2430       else
2431         res = TRUE;
2432       gst_value_array_append_value (dest, &kid);
2433       g_value_unset (&kid);
2434     }
2435
2436     if (!res)
2437       g_value_unset (dest);
2438
2439     return res;
2440   } else {
2441     return FALSE;
2442   }
2443
2444   return TRUE;
2445 }
2446
2447 static gboolean
2448 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2449 {
2450   GstStructure *s = data;
2451   GValue v = { 0 };
2452
2453   if (fixate_value (&v, value)) {
2454     gst_structure_id_set_value (s, field_id, &v);
2455     g_value_unset (&v);
2456   }
2457
2458   return TRUE;
2459 }
2460
2461 /**
2462  * gst_pad_fixate_caps:
2463  * @pad: a  #GstPad to fixate
2464  * @caps: the  #GstCaps to fixate
2465  *
2466  * Fixate a caps on the given pad. Modifies the caps in place, so you should
2467  * make sure that the caps are actually writable (see gst_caps_make_writable()).
2468  */
2469 void
2470 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2471 {
2472   GstPadFixateCapsFunction fixatefunc;
2473   GstStructure *s;
2474
2475   g_return_if_fail (GST_IS_PAD (pad));
2476   g_return_if_fail (caps != NULL);
2477   g_return_if_fail (!gst_caps_is_empty (caps));
2478   /* FIXME-0.11: do not allow fixating any-caps
2479    * g_return_if_fail (!gst_caps_is_any (caps));
2480    */
2481
2482   if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2483     return;
2484
2485   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2486   if (fixatefunc) {
2487     fixatefunc (pad, caps);
2488   }
2489
2490   /* default fixation */
2491   gst_caps_truncate (caps);
2492   s = gst_caps_get_structure (caps, 0);
2493   gst_structure_foreach (s, gst_pad_default_fixate, s);
2494 }
2495
2496 /* Default accept caps implementation just checks against
2497  * against the allowed caps for the pad */
2498 static gboolean
2499 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2500 {
2501   /* get the caps and see if it intersects to something not empty */
2502   GstCaps *allowed;
2503   gboolean result = FALSE;
2504
2505   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2506
2507   allowed = gst_pad_get_caps_reffed (pad);
2508   if (!allowed)
2509     goto nothing_allowed;
2510
2511   GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2512
2513   result = gst_caps_can_intersect (allowed, caps);
2514
2515   gst_caps_unref (allowed);
2516
2517   return result;
2518
2519   /* ERRORS */
2520 nothing_allowed:
2521   {
2522     GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2523     return FALSE;
2524   }
2525 }
2526
2527 /**
2528  * gst_pad_accept_caps:
2529  * @pad: a #GstPad to check
2530  * @caps: a #GstCaps to check on the pad
2531  *
2532  * Check if the given pad accepts the caps.
2533  *
2534  * Returns: TRUE if the pad can accept the caps.
2535  */
2536 gboolean
2537 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2538 {
2539   gboolean result;
2540   GstPadAcceptCapsFunction acceptfunc;
2541   GstCaps *existing = NULL;
2542
2543   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2544
2545   /* any pad can be unnegotiated */
2546   if (caps == NULL)
2547     return TRUE;
2548
2549   /* lock for checking the existing caps */
2550   GST_OBJECT_LOCK (pad);
2551   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2552   /* The current caps on a pad are trivially acceptable */
2553   if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2554     if (caps == existing || gst_caps_is_equal (caps, existing))
2555       goto is_same_caps;
2556   }
2557   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2558   GST_OBJECT_UNLOCK (pad);
2559
2560   if (G_LIKELY (acceptfunc)) {
2561     /* we can call the function */
2562     result = acceptfunc (pad, caps);
2563     GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2564   } else {
2565     /* Only null if the element explicitly unset it */
2566     result = gst_pad_acceptcaps_default (pad, caps);
2567     GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2568   }
2569   return result;
2570
2571 is_same_caps:
2572   {
2573     GST_DEBUG_OBJECT (pad, "pad had same caps");
2574     GST_OBJECT_UNLOCK (pad);
2575     return TRUE;
2576   }
2577 }
2578
2579 /**
2580  * gst_pad_peer_accept_caps:
2581  * @pad: a  #GstPad to check the peer of
2582  * @caps: a #GstCaps to check on the pad
2583  *
2584  * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2585  * returns TRUE.
2586  *
2587  * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2588  */
2589 gboolean
2590 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2591 {
2592   GstPad *peerpad;
2593   gboolean result;
2594
2595   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2596
2597   GST_OBJECT_LOCK (pad);
2598
2599   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2600
2601   peerpad = GST_PAD_PEER (pad);
2602   if (G_UNLIKELY (peerpad == NULL))
2603     goto no_peer;
2604
2605   gst_object_ref (peerpad);
2606   /* release lock before calling external methods but keep ref to pad */
2607   GST_OBJECT_UNLOCK (pad);
2608
2609   result = gst_pad_accept_caps (peerpad, caps);
2610
2611   gst_object_unref (peerpad);
2612
2613   return result;
2614
2615 no_peer:
2616   {
2617     GST_OBJECT_UNLOCK (pad);
2618     return TRUE;
2619   }
2620 }
2621
2622 /**
2623  * gst_pad_set_caps:
2624  * @pad: a  #GstPad to set the capabilities of.
2625  * @caps: a #GstCaps to set.
2626  *
2627  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2628  * caps on the pad will be unreffed. This function refs the caps so you should
2629  * unref if as soon as you don't need it anymore.
2630  * It is possible to set NULL caps, which will make the pad unnegotiated
2631  * again.
2632  *
2633  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2634  * or bad parameters were provided to this function.
2635  *
2636  * MT safe.
2637  */
2638 gboolean
2639 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2640 {
2641   GstPadSetCapsFunction setcaps;
2642   GstCaps *existing;
2643
2644   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2645   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2646
2647   GST_OBJECT_LOCK (pad);
2648   existing = GST_PAD_CAPS (pad);
2649   if (existing == caps)
2650     goto was_ok;
2651
2652   if (gst_caps_is_equal (caps, existing))
2653     goto setting_same_caps;
2654
2655   setcaps = GST_PAD_SETCAPSFUNC (pad);
2656
2657   /* call setcaps function to configure the pad only if the
2658    * caps is not NULL */
2659   if (setcaps != NULL && caps) {
2660     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2661       GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2662       GST_OBJECT_UNLOCK (pad);
2663       if (!setcaps (pad, caps))
2664         goto could_not_set;
2665       GST_OBJECT_LOCK (pad);
2666       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2667     } else {
2668       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2669     }
2670   }
2671
2672   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2673   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2674       caps);
2675   GST_OBJECT_UNLOCK (pad);
2676
2677   g_object_notify (G_OBJECT (pad), "caps");
2678
2679   return TRUE;
2680
2681 was_ok:
2682   {
2683     GST_OBJECT_UNLOCK (pad);
2684     return TRUE;
2685   }
2686 setting_same_caps:
2687   {
2688     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2689     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2690         "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2691         caps);
2692     GST_OBJECT_UNLOCK (pad);
2693     return TRUE;
2694   }
2695
2696   /* ERRORS */
2697 could_not_set:
2698   {
2699     GST_OBJECT_LOCK (pad);
2700     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2701     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2702         "caps %" GST_PTR_FORMAT " could not be set", caps);
2703     GST_OBJECT_UNLOCK (pad);
2704
2705     return FALSE;
2706   }
2707 }
2708
2709 static gboolean
2710 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2711 {
2712   gboolean res;
2713
2714   /* See if pad accepts the caps */
2715   if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2716     goto not_accepted;
2717
2718   /* set caps on pad if call succeeds */
2719   res = gst_pad_set_caps (pad, caps);
2720   /* no need to unref the caps here, set_caps takes a ref and
2721    * our ref goes away when we leave this function. */
2722
2723   return res;
2724
2725 not_accepted:
2726   {
2727     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2728         "caps %" GST_PTR_FORMAT " not accepted", caps);
2729     return FALSE;
2730   }
2731 }
2732
2733 /* returns TRUE if the src pad could be configured to accept the given caps */
2734 static gboolean
2735 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2736 {
2737   gboolean res;
2738
2739   if (dosetcaps) {
2740     /* See if pad accepts the caps */
2741     if (!gst_pad_accept_caps (pad, caps))
2742       goto not_accepted;
2743
2744     res = gst_pad_set_caps (pad, caps);
2745   } else {
2746     res = TRUE;
2747   }
2748   return res;
2749
2750 not_accepted:
2751   {
2752     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2753         "caps %" GST_PTR_FORMAT " not accepted", caps);
2754     return FALSE;
2755   }
2756 }
2757
2758 /**
2759  * gst_pad_get_pad_template_caps:
2760  * @pad: a #GstPad to get the template capabilities from.
2761  *
2762  * Gets the capabilities for @pad's template.
2763  *
2764  * Returns: the #GstCaps of this pad template. If you intend to keep a
2765  * reference on the caps, make a copy (see gst_caps_copy ()).
2766  */
2767 const GstCaps *
2768 gst_pad_get_pad_template_caps (GstPad * pad)
2769 {
2770   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2771
2772   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2773
2774   if (GST_PAD_PAD_TEMPLATE (pad))
2775     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2776
2777   return gst_static_caps_get (&anycaps);
2778 }
2779
2780 /**
2781  * gst_pad_get_peer:
2782  * @pad: a #GstPad to get the peer of.
2783  *
2784  * Gets the peer of @pad. This function refs the peer pad so
2785  * you need to unref it after use.
2786  *
2787  * Returns: the peer #GstPad. Unref after usage.
2788  *
2789  * MT safe.
2790  */
2791 GstPad *
2792 gst_pad_get_peer (GstPad * pad)
2793 {
2794   GstPad *result;
2795
2796   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2797
2798   GST_OBJECT_LOCK (pad);
2799   result = GST_PAD_PEER (pad);
2800   if (result)
2801     gst_object_ref (result);
2802   GST_OBJECT_UNLOCK (pad);
2803
2804   return result;
2805 }
2806
2807 /**
2808  * gst_pad_get_allowed_caps:
2809  * @pad: a #GstPad.
2810  *
2811  * Gets the capabilities of the allowed media types that can flow through
2812  * @pad and its peer.
2813  *
2814  * The allowed capabilities is calculated as the intersection of the results of
2815  * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2816  * on the resulting caps.
2817  *
2818  * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2819  * longer need it. This function returns NULL when @pad has no peer.
2820  *
2821  * MT safe.
2822  */
2823 GstCaps *
2824 gst_pad_get_allowed_caps (GstPad * pad)
2825 {
2826   GstCaps *mycaps;
2827   GstCaps *caps;
2828   GstCaps *peercaps;
2829   GstPad *peer;
2830
2831   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2832
2833   GST_OBJECT_LOCK (pad);
2834
2835   peer = GST_PAD_PEER (pad);
2836   if (G_UNLIKELY (peer == NULL))
2837     goto no_peer;
2838
2839   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2840
2841   gst_object_ref (peer);
2842   GST_OBJECT_UNLOCK (pad);
2843   mycaps = gst_pad_get_caps_reffed (pad);
2844
2845   peercaps = gst_pad_get_caps_reffed (peer);
2846   gst_object_unref (peer);
2847
2848   caps = gst_caps_intersect (mycaps, peercaps);
2849   gst_caps_unref (peercaps);
2850   gst_caps_unref (mycaps);
2851
2852   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2853       caps);
2854
2855   return caps;
2856
2857 no_peer:
2858   {
2859     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2860     GST_OBJECT_UNLOCK (pad);
2861
2862     return NULL;
2863   }
2864 }
2865
2866 /**
2867  * gst_pad_get_negotiated_caps:
2868  * @pad: a #GstPad.
2869  *
2870  * Gets the capabilities of the media type that currently flows through @pad
2871  * and its peer.
2872  *
2873  * This function can be used on both src and sinkpads. Note that srcpads are
2874  * always negotiated before sinkpads so it is possible that the negotiated caps
2875  * on the srcpad do not match the negotiated caps of the peer.
2876  *
2877  * Returns: the negotiated #GstCaps of the pad link.  Unref the caps when
2878  * you no longer need it. This function returns NULL when the @pad has no
2879  * peer or is not negotiated yet.
2880  *
2881  * MT safe.
2882  */
2883 GstCaps *
2884 gst_pad_get_negotiated_caps (GstPad * pad)
2885 {
2886   GstCaps *caps;
2887   GstPad *peer;
2888
2889   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2890
2891   GST_OBJECT_LOCK (pad);
2892
2893   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2894     goto no_peer;
2895
2896   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2897
2898   caps = GST_PAD_CAPS (pad);
2899   if (caps)
2900     gst_caps_ref (caps);
2901   GST_OBJECT_UNLOCK (pad);
2902
2903   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2904       caps);
2905
2906   return caps;
2907
2908 no_peer:
2909   {
2910     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2911     GST_OBJECT_UNLOCK (pad);
2912
2913     return NULL;
2914   }
2915 }
2916
2917 /* calls the buffer_alloc function on the given pad */
2918 static GstFlowReturn
2919 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2920     GstCaps * caps, GstBuffer ** buf)
2921 {
2922   GstFlowReturn ret;
2923   GstPadBufferAllocFunction bufferallocfunc;
2924
2925   GST_OBJECT_LOCK (pad);
2926   /* when the pad is flushing we cannot give a buffer */
2927   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2928     goto flushing;
2929
2930   bufferallocfunc = pad->bufferallocfunc;
2931
2932   if (offset == GST_BUFFER_OFFSET_NONE) {
2933     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2934         "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2935         GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2936   } else {
2937     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2938         "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2939         G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2940         bufferallocfunc, size, offset);
2941   }
2942   GST_OBJECT_UNLOCK (pad);
2943
2944   /* G_LIKELY for now since most elements don't implement a buffer alloc
2945    * function and there is no default alloc proxy function as this is usually
2946    * not possible. */
2947   if (G_LIKELY (bufferallocfunc == NULL))
2948     goto fallback;
2949
2950   ret = bufferallocfunc (pad, offset, size, caps, buf);
2951
2952   if (G_UNLIKELY (ret != GST_FLOW_OK))
2953     goto error;
2954
2955   /* no error, but NULL buffer means fallback to the default */
2956   if (G_UNLIKELY (*buf == NULL))
2957     goto fallback;
2958
2959   /* If the buffer alloc function didn't set up the caps like it should,
2960    * do it for it */
2961   if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2962     GST_WARNING_OBJECT (pad,
2963         "Buffer allocation function did not set caps. Setting");
2964     gst_buffer_set_caps (*buf, caps);
2965   }
2966   return ret;
2967
2968 flushing:
2969   {
2970     /* pad was flushing */
2971     GST_OBJECT_UNLOCK (pad);
2972     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2973     return GST_FLOW_WRONG_STATE;
2974   }
2975 error:
2976   {
2977     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2978         "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2979     return ret;
2980   }
2981 fallback:
2982   {
2983     /* fallback case, allocate a buffer of our own, add pad caps. */
2984     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2985
2986     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2987       GST_BUFFER_OFFSET (*buf) = offset;
2988       gst_buffer_set_caps (*buf, caps);
2989       return GST_FLOW_OK;
2990     } else {
2991       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2992           "out of memory allocating %d bytes", size);
2993       return GST_FLOW_ERROR;
2994     }
2995   }
2996 }
2997
2998 /* FIXME 0.11: size should be unsigned */
2999 static GstFlowReturn
3000 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
3001     GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
3002 {
3003   GstPad *peer;
3004   GstFlowReturn ret;
3005   GstCaps *newcaps;
3006   gboolean caps_changed;
3007
3008   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3009   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3010   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
3011   g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
3012
3013   GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
3014       GST_PTR_FORMAT, offset, size, caps);
3015
3016   GST_OBJECT_LOCK (pad);
3017   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3018     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3019       goto flushed;
3020
3021   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3022     goto no_peer;
3023
3024   gst_object_ref (peer);
3025   GST_OBJECT_UNLOCK (pad);
3026
3027   ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
3028   gst_object_unref (peer);
3029
3030   if (G_UNLIKELY (ret != GST_FLOW_OK))
3031     goto peer_error;
3032
3033   /* FIXME, move capnego this into a base class? */
3034   newcaps = GST_BUFFER_CAPS (*buf);
3035
3036   /* Lock for checking caps, pretty pointless as the _pad_push() function might
3037    * change it concurrently, one of the problems with automatic caps setting in
3038    * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
3039    * when there is heavy renegotiation going on in both directions. */
3040   GST_OBJECT_LOCK (pad);
3041   caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
3042   GST_OBJECT_UNLOCK (pad);
3043
3044   /* we got a new datatype on the pad, see if it can handle it */
3045   if (G_UNLIKELY (caps_changed)) {
3046     GST_DEBUG_OBJECT (pad,
3047         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3048         GST_PAD_CAPS (pad), newcaps, newcaps);
3049     if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
3050       goto not_negotiated;
3051   }
3052
3053   /* sanity check (only if caps are the same) */
3054   if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
3055     goto wrong_size_fallback;
3056
3057   return ret;
3058
3059 flushed:
3060   {
3061     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
3062     GST_OBJECT_UNLOCK (pad);
3063     return ret;
3064   }
3065 no_peer:
3066   {
3067     /* pad has no peer */
3068     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3069         "called bufferallocfunc but had no peer");
3070     GST_OBJECT_UNLOCK (pad);
3071     return GST_FLOW_NOT_LINKED;
3072   }
3073 peer_error:
3074   {
3075     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3076         "alloc function returned error %s", gst_flow_get_name (ret));
3077     return ret;
3078   }
3079 not_negotiated:
3080   {
3081     gst_buffer_unref (*buf);
3082     *buf = NULL;
3083     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3084         "alloc function returned unacceptable buffer");
3085     return GST_FLOW_NOT_NEGOTIATED;
3086   }
3087 wrong_size_fallback:
3088   {
3089     GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3090         "function is too small (%u < %d), doing fallback buffer alloc",
3091         GST_BUFFER_SIZE (*buf), size);
3092
3093     gst_buffer_unref (*buf);
3094
3095     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3096       GST_BUFFER_OFFSET (*buf) = offset;
3097       gst_buffer_set_caps (*buf, caps);
3098       return GST_FLOW_OK;
3099     } else {
3100       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3101           "out of memory allocating %d bytes", size);
3102       return GST_FLOW_ERROR;
3103     }
3104   }
3105 }
3106
3107 /**
3108  * gst_pad_alloc_buffer:
3109  * @pad: a source #GstPad
3110  * @offset: the offset of the new buffer in the stream
3111  * @size: the size of the new buffer
3112  * @caps: the caps of the new buffer
3113  * @buf: a newly allocated buffer
3114  *
3115  * Allocates a new, empty buffer optimized to push to pad @pad.  This
3116  * function only works if @pad is a source pad and has a peer.
3117  *
3118  * A new, empty #GstBuffer will be put in the @buf argument.
3119  * You need to check the caps of the buffer after performing this
3120  * function and renegotiate to the format if needed. If the caps changed, it is
3121  * possible that the buffer returned in @buf is not of the right size for the
3122  * new format, @buf needs to be unreffed and reallocated if this is the case.
3123  *
3124  * Returns: a result code indicating success of the operation. Any
3125  * result code other than #GST_FLOW_OK is an error and @buf should
3126  * not be used.
3127  * An error can occur if the pad is not connected or when the downstream
3128  * peer elements cannot provide an acceptable buffer.
3129  *
3130  * MT safe.
3131  */
3132
3133 /* FIXME 0.11: size should be unsigned */
3134 GstFlowReturn
3135 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3136     GstBuffer ** buf)
3137 {
3138   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3139 }
3140
3141 /**
3142  * gst_pad_alloc_buffer_and_set_caps:
3143  * @pad: a source #GstPad
3144  * @offset: the offset of the new buffer in the stream
3145  * @size: the size of the new buffer
3146  * @caps: the caps of the new buffer
3147  * @buf: a newly allocated buffer
3148  *
3149  * In addition to the function gst_pad_alloc_buffer(), this function
3150  * automatically calls gst_pad_set_caps() when the caps of the
3151  * newly allocated buffer are different from the @pad caps.
3152  *
3153  * After a renegotiation, the size of the new buffer returned in @buf could
3154  * be of the wrong size for the new format and must be unreffed an reallocated
3155  * in that case.
3156  *
3157  * Returns: a result code indicating success of the operation. Any
3158  * result code other than #GST_FLOW_OK is an error and @buf should
3159  * not be used.
3160  * An error can occur if the pad is not connected or when the downstream
3161  * peer elements cannot provide an acceptable buffer.
3162  *
3163  * MT safe.
3164  */
3165
3166 /* FIXME 0.11: size should be unsigned */
3167 GstFlowReturn
3168 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3169     GstCaps * caps, GstBuffer ** buf)
3170 {
3171   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3172 }
3173
3174
3175 #ifndef GST_REMOVE_DEPRECATED
3176 typedef struct
3177 {
3178   GList *list;
3179   guint32 cookie;
3180 } IntLinkIterData;
3181
3182 static void
3183 int_link_iter_data_free (IntLinkIterData * data)
3184 {
3185   g_list_free (data->list);
3186   g_slice_free (IntLinkIterData, data);
3187 }
3188 #endif
3189
3190 static GstIteratorItem
3191 iterate_pad (GstIterator * it, GstPad * pad)
3192 {
3193   gst_object_ref (pad);
3194   return GST_ITERATOR_ITEM_PASS;
3195 }
3196
3197 /**
3198  * gst_pad_iterate_internal_links_default:
3199  * @pad: the #GstPad to get the internal links of.
3200  *
3201  * Iterate the list of pads to which the given pad is linked to inside of
3202  * the parent element.
3203  * This is the default handler, and thus returns an iterator of all of the
3204  * pads inside the parent element with opposite direction.
3205  *
3206  * The caller must free this iterator after use with gst_iterator_free().
3207  *
3208  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3209  * returned pad with gst_object_unref().
3210  *
3211  * Since: 0.10.21
3212  */
3213 GstIterator *
3214 gst_pad_iterate_internal_links_default (GstPad * pad)
3215 {
3216   GstIterator *res;
3217   GList **padlist;
3218   guint32 *cookie;
3219   GMutex *lock;
3220   gpointer owner;
3221   GstIteratorDisposeFunction dispose;
3222
3223   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3224
3225 #ifndef GST_REMOVE_DEPRECATED
3226   /* when we get here, the default handler for the iterate links is called,
3227    * which means that the user has not installed a custom one. We first check if
3228    * there is maybe a custom legacy function we can call. */
3229   if (GST_PAD_INTLINKFUNC (pad) &&
3230       GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3231     IntLinkIterData *data;
3232
3233     /* make an iterator for the list. We can't protect the list with a
3234      * cookie. If we would take the cookie of the parent element, we need to
3235      * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3236      * cache the per-pad list and invalidate the list when a new call to
3237      * INTLINKFUNC() returned a different list but then this would only work if
3238      * two concurrent iterators were used and the last iterator would still be
3239      * thread-unsafe. Just don't use this method anymore. */
3240     data = g_slice_new (IntLinkIterData);
3241     data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3242     data->cookie = 0;
3243
3244     GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3245
3246     cookie = &data->cookie;
3247     padlist = &data->list;
3248     owner = data;
3249     dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3250     /* reuse the pad lock, it's all we have here */
3251     lock = GST_OBJECT_GET_LOCK (pad);
3252   } else
3253 #endif
3254   {
3255     GstElement *parent;
3256
3257     GST_OBJECT_LOCK (pad);
3258     parent = GST_PAD_PARENT (pad);
3259     if (!parent || !GST_IS_ELEMENT (parent))
3260       goto no_parent;
3261
3262     gst_object_ref (parent);
3263     GST_OBJECT_UNLOCK (pad);
3264
3265     if (pad->direction == GST_PAD_SRC)
3266       padlist = &parent->sinkpads;
3267     else
3268       padlist = &parent->srcpads;
3269
3270     GST_DEBUG_OBJECT (pad, "Making iterator");
3271
3272     cookie = &parent->pads_cookie;
3273     owner = parent;
3274     dispose = (GstIteratorDisposeFunction) gst_object_unref;
3275     lock = GST_OBJECT_GET_LOCK (parent);
3276   }
3277
3278   res = gst_iterator_new_list (GST_TYPE_PAD,
3279       lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3280       dispose);
3281
3282   return res;
3283
3284   /* ERRORS */
3285 no_parent:
3286   {
3287     GST_OBJECT_UNLOCK (pad);
3288     GST_DEBUG_OBJECT (pad, "no parent element");
3289     return NULL;
3290   }
3291 }
3292
3293 /**
3294  * gst_pad_iterate_internal_links:
3295  * @pad: the GstPad to get the internal links of.
3296  *
3297  * Gets an iterator for the pads to which the given pad is linked to inside
3298  * of the parent element.
3299  *
3300  * Each #GstPad element yielded by the iterator will have its refcount increased,
3301  * so unref after use.
3302  *
3303  * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3304  * iterator function configured. Use gst_iterator_free() after usage.
3305  *
3306  * Since: 0.10.21
3307  */
3308 GstIterator *
3309 gst_pad_iterate_internal_links (GstPad * pad)
3310 {
3311   GstIterator *res = NULL;
3312
3313   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3314
3315   if (GST_PAD_ITERINTLINKFUNC (pad))
3316     res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3317
3318   return res;
3319 }
3320
3321 #ifndef GST_REMOVE_DEPRECATED
3322 static void
3323 add_unref_pad_to_list (GstPad * pad, GList ** list)
3324 {
3325   *list = g_list_prepend (*list, pad);
3326   gst_object_unref (pad);
3327 }
3328 #endif
3329
3330 /**
3331  * gst_pad_get_internal_links_default:
3332  * @pad: the #GstPad to get the internal links of.
3333  *
3334  * Gets a list of pads to which the given pad is linked to
3335  * inside of the parent element.
3336  * This is the default handler, and thus returns a list of all of the
3337  * pads inside the parent element with opposite direction.
3338  *
3339  * The caller must free this list after use with g_list_free().
3340  *
3341  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3342  *
3343  * Not MT safe.
3344  *
3345  * Deprecated: This function does not ref the pads in the list so that they
3346  * could become invalid by the time the application accesses them. It's also
3347  * possible that the list changes while handling the pads, which the caller of
3348  * this function is unable to know. Use the thread-safe 
3349  * gst_pad_iterate_internal_links_default() instead.
3350  */
3351 #ifndef GST_REMOVE_DEPRECATED
3352 GList *
3353 gst_pad_get_internal_links_default (GstPad * pad)
3354 {
3355   GList *res = NULL;
3356   GstElement *parent;
3357
3358   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3359
3360   GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3361
3362   /* when we get here, the default handler for get_internal_links is called,
3363    * which means that the user has not installed a custom one. We first check if
3364    * there is maybe a custom iterate function we can call. */
3365   if (GST_PAD_ITERINTLINKFUNC (pad) &&
3366       GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3367     GstIterator *it;
3368     GstIteratorResult ires;
3369     gboolean done = FALSE;
3370
3371     it = gst_pad_iterate_internal_links (pad);
3372     /* loop over the iterator and put all elements into a list, we also
3373      * immediatly unref them, which is bad. */
3374     do {
3375       ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, &res);
3376       switch (ires) {
3377         case GST_ITERATOR_OK:
3378         case GST_ITERATOR_DONE:
3379         case GST_ITERATOR_ERROR:
3380           done = TRUE;
3381           break;
3382         case GST_ITERATOR_RESYNC:
3383           /* restart, discard previous list */
3384           gst_iterator_resync (it);
3385           g_list_free (res);
3386           res = NULL;
3387           break;
3388       }
3389     } while (!done);
3390
3391     gst_iterator_free (it);
3392   } else {
3393     /* lock pad, check and ref parent */
3394     GST_OBJECT_LOCK (pad);
3395     parent = GST_PAD_PARENT (pad);
3396     if (!parent || !GST_IS_ELEMENT (parent))
3397       goto no_parent;
3398
3399     parent = gst_object_ref (parent);
3400     GST_OBJECT_UNLOCK (pad);
3401
3402     /* now lock the parent while we copy the pads */
3403     GST_OBJECT_LOCK (parent);
3404     if (pad->direction == GST_PAD_SRC)
3405       res = g_list_copy (parent->sinkpads);
3406     else
3407       res = g_list_copy (parent->srcpads);
3408     GST_OBJECT_UNLOCK (parent);
3409
3410     gst_object_unref (parent);
3411   }
3412
3413   /* At this point pads can be changed and unreffed. Nothing we can do about it
3414    * because for compatibility reasons this function cannot ref the pads or
3415    * notify the app that the list changed. */
3416
3417   return res;
3418
3419 no_parent:
3420   {
3421     GST_DEBUG_OBJECT (pad, "no parent");
3422     GST_OBJECT_UNLOCK (pad);
3423     return NULL;
3424   }
3425 }
3426 #endif /* GST_REMOVE_DEPRECATED */
3427
3428 /**
3429  * gst_pad_get_internal_links:
3430  * @pad: the #GstPad to get the internal links of.
3431  *
3432  * Gets a list of pads to which the given pad is linked to
3433  * inside of the parent element.
3434  * The caller must free this list after use.
3435  *
3436  * Not MT safe.
3437  *
3438  * Returns: a newly allocated #GList of pads, free with g_list_free().
3439  * 
3440  * Deprecated: This function does not ref the pads in the list so that they
3441  * could become invalid by the time the application accesses them. It's also
3442  * possible that the list changes while handling the pads, which the caller of
3443  * this function is unable to know. Use the thread-safe 
3444  * gst_pad_iterate_internal_links() instead.
3445  */
3446 #ifndef GST_REMOVE_DEPRECATED
3447 #ifdef GST_DISABLE_DEPRECATED
3448 GList *gst_pad_get_internal_links (GstPad * pad);
3449 #endif
3450 GList *
3451 gst_pad_get_internal_links (GstPad * pad)
3452 {
3453   GList *res = NULL;
3454
3455   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3456
3457   GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3458
3459   if (GST_PAD_INTLINKFUNC (pad))
3460     res = GST_PAD_INTLINKFUNC (pad) (pad);
3461
3462   return res;
3463 }
3464 #endif /* GST_REMOVE_DEPRECATED */
3465
3466 static gboolean
3467 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3468 {
3469   gboolean result = FALSE;
3470   GstIterator *iter;
3471   gboolean done = FALSE;
3472   gpointer item;
3473   GstPad *eventpad;
3474   GList *pushed_pads = NULL;
3475
3476   GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3477       event, GST_EVENT_TYPE_NAME (event));
3478
3479   iter = gst_pad_iterate_internal_links (pad);
3480
3481   if (!iter)
3482     goto no_iter;
3483
3484   while (!done) {
3485     switch (gst_iterator_next (iter, &item)) {
3486       case GST_ITERATOR_OK:
3487         eventpad = GST_PAD_CAST (item);
3488
3489         /* if already pushed,  skip */
3490         if (g_list_find (pushed_pads, eventpad)) {
3491           gst_object_unref (item);
3492           break;
3493         }
3494
3495         if (GST_PAD_IS_SRC (eventpad)) {
3496           /* for each pad we send to, we should ref the event; it's up
3497            * to downstream to unref again when handled. */
3498           GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3499               event, GST_EVENT_TYPE_NAME (event),
3500               GST_DEBUG_PAD_NAME (eventpad));
3501           gst_event_ref (event);
3502           result |= gst_pad_push_event (eventpad, event);
3503         } else {
3504           /* we only send the event on one pad, multi-sinkpad elements
3505            * should implement a handler */
3506           GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3507               event, GST_EVENT_TYPE_NAME (event),
3508               GST_DEBUG_PAD_NAME (eventpad));
3509           result = gst_pad_push_event (eventpad, event);
3510           done = TRUE;
3511           event = NULL;
3512         }
3513
3514         pushed_pads = g_list_prepend (pushed_pads, eventpad);
3515
3516         gst_object_unref (item);
3517         break;
3518       case GST_ITERATOR_RESYNC:
3519         /* FIXME, if we want to reset the result value we need to remember which
3520          * pads pushed with which result */
3521         gst_iterator_resync (iter);
3522         break;
3523       case GST_ITERATOR_ERROR:
3524         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3525         done = TRUE;
3526         break;
3527       case GST_ITERATOR_DONE:
3528         done = TRUE;
3529         break;
3530     }
3531   }
3532   gst_iterator_free (iter);
3533
3534 no_iter:
3535
3536   /* If this is a sinkpad and we don't have pads to send the event to, we
3537    * return TRUE. This is so that when using the default handler on a sink
3538    * element, we don't fail to push it. */
3539   if (!pushed_pads)
3540     result = GST_PAD_IS_SINK (pad);
3541
3542   g_list_free (pushed_pads);
3543
3544   /* we handled the incoming event so we unref once */
3545   if (event) {
3546     GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3547     gst_event_unref (event);
3548   }
3549
3550   return result;
3551 }
3552
3553 /**
3554  * gst_pad_event_default:
3555  * @pad: a #GstPad to call the default event handler on.
3556  * @event: the #GstEvent to handle.
3557  *
3558  * Invokes the default event handler for the given pad. End-of-stream and
3559  * discontinuity events are handled specially, and then the event is sent to all
3560  * pads internally linked to @pad. Note that if there are many possible sink
3561  * pads that are internally linked to @pad, only one will be sent an event.
3562  * Multi-sinkpad elements should implement custom event handlers.
3563  *
3564  * Returns: TRUE if the event was sent succesfully.
3565  */
3566 gboolean
3567 gst_pad_event_default (GstPad * pad, GstEvent * event)
3568 {
3569   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3570   g_return_val_if_fail (event != NULL, FALSE);
3571
3572   GST_LOG_OBJECT (pad, "default event handler");
3573
3574   switch (GST_EVENT_TYPE (event)) {
3575     case GST_EVENT_EOS:
3576     {
3577       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3578       gst_pad_pause_task (pad);
3579     }
3580       /* fall thru */
3581     default:
3582       break;
3583   }
3584
3585   return gst_pad_event_default_dispatch (pad, event);
3586 }
3587
3588 /**
3589  * gst_pad_dispatcher:
3590  * @pad: a #GstPad to dispatch.
3591  * @dispatch: the #GstPadDispatcherFunction to call.
3592  * @data: gpointer user data passed to the dispatcher function.
3593  *
3594  * Invokes the given dispatcher function on each respective peer of
3595  * all pads that are internally linked to the given pad.
3596  * The GstPadDispatcherFunction should return TRUE when no further pads
3597  * need to be processed.
3598  *
3599  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3600  */
3601 gboolean
3602 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3603     gpointer data)
3604 {
3605   gboolean res = FALSE;
3606   GstIterator *iter = NULL;
3607   gboolean done = FALSE;
3608   gpointer item;
3609
3610   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3611   g_return_val_if_fail (dispatch != NULL, FALSE);
3612
3613   iter = gst_pad_iterate_internal_links (pad);
3614
3615   if (!iter)
3616     goto no_iter;
3617
3618   while (!done) {
3619     switch (gst_iterator_next (iter, &item)) {
3620       case GST_ITERATOR_OK:
3621       {
3622         GstPad *int_pad = GST_PAD_CAST (item);
3623         GstPad *int_peer = gst_pad_get_peer (int_pad);
3624
3625         if (int_peer) {
3626           GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3627               GST_DEBUG_PAD_NAME (int_peer));
3628           done = res = dispatch (int_peer, data);
3629           gst_object_unref (int_peer);
3630         } else {
3631           GST_DEBUG_OBJECT (int_pad, "no peer");
3632         }
3633       }
3634         gst_object_unref (item);
3635         break;
3636       case GST_ITERATOR_RESYNC:
3637         gst_iterator_resync (iter);
3638         break;
3639       case GST_ITERATOR_ERROR:
3640         done = TRUE;
3641         GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3642         break;
3643       case GST_ITERATOR_DONE:
3644         done = TRUE;
3645         break;
3646     }
3647   }
3648   gst_iterator_free (iter);
3649
3650   GST_DEBUG_OBJECT (pad, "done, result %d", res);
3651
3652 no_iter:
3653
3654   return res;
3655 }
3656
3657 /**
3658  * gst_pad_query:
3659  * @pad: a #GstPad to invoke the default query on.
3660  * @query: the #GstQuery to perform.
3661  *
3662  * Dispatches a query to a pad. The query should have been allocated by the
3663  * caller via one of the type-specific allocation functions in gstquery.h. The
3664  * element is responsible for filling the query with an appropriate response,
3665  * which should then be parsed with a type-specific query parsing function.
3666  *
3667  * Again, the caller is responsible for both the allocation and deallocation of
3668  * the query structure.
3669  *
3670  * Returns: TRUE if the query could be performed.
3671  */
3672 gboolean
3673 gst_pad_query (GstPad * pad, GstQuery * query)
3674 {
3675   GstPadQueryFunction func;
3676
3677   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3678   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3679
3680   GST_DEBUG_OBJECT (pad, "sending query %p", query);
3681
3682   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3683     goto no_func;
3684
3685   return func (pad, query);
3686
3687 no_func:
3688   {
3689     GST_DEBUG_OBJECT (pad, "had no query function");
3690     return FALSE;
3691   }
3692 }
3693
3694 /**
3695  * gst_pad_peer_query:
3696  * @pad: a #GstPad to invoke the peer query on.
3697  * @query: the #GstQuery to perform.
3698  *
3699  * Performs gst_pad_query() on the peer of @pad.
3700  *
3701  * The caller is responsible for both the allocation and deallocation of
3702  * the query structure.
3703  *
3704  * Returns: TRUE if the query could be performed. This function returns %FALSE
3705  * if @pad has no peer.
3706  *
3707  * Since: 0.10.15
3708  */
3709 gboolean
3710 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3711 {
3712   GstPad *peerpad;
3713   gboolean result;
3714
3715   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3716   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3717
3718   GST_OBJECT_LOCK (pad);
3719
3720   GST_DEBUG_OBJECT (pad, "peer query");
3721
3722   peerpad = GST_PAD_PEER (pad);
3723   if (G_UNLIKELY (peerpad == NULL))
3724     goto no_peer;
3725
3726   gst_object_ref (peerpad);
3727   GST_OBJECT_UNLOCK (pad);
3728
3729   result = gst_pad_query (peerpad, query);
3730
3731   gst_object_unref (peerpad);
3732
3733   return result;
3734
3735   /* ERRORS */
3736 no_peer:
3737   {
3738     GST_WARNING_OBJECT (pad, "pad has no peer");
3739     GST_OBJECT_UNLOCK (pad);
3740     return FALSE;
3741   }
3742 }
3743
3744 /**
3745  * gst_pad_query_default:
3746  * @pad: a #GstPad to call the default query handler on.
3747  * @query: the #GstQuery to handle.
3748  *
3749  * Invokes the default query handler for the given pad.
3750  * The query is sent to all pads internally linked to @pad. Note that
3751  * if there are many possible sink pads that are internally linked to
3752  * @pad, only one will be sent the query.
3753  * Multi-sinkpad elements should implement custom query handlers.
3754  *
3755  * Returns: TRUE if the query was performed succesfully.
3756  */
3757 gboolean
3758 gst_pad_query_default (GstPad * pad, GstQuery * query)
3759 {
3760   switch (GST_QUERY_TYPE (query)) {
3761     case GST_QUERY_POSITION:
3762     case GST_QUERY_SEEKING:
3763     case GST_QUERY_FORMATS:
3764     case GST_QUERY_LATENCY:
3765     case GST_QUERY_JITTER:
3766     case GST_QUERY_RATE:
3767     case GST_QUERY_CONVERT:
3768     default:
3769       return gst_pad_dispatcher
3770           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3771   }
3772 }
3773
3774 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
3775 /* FIXME: why isn't this on a GstElement ? */
3776 /**
3777  * gst_pad_load_and_link:
3778  * @self: an #xmlNodePtr to read the description from.
3779  * @parent: the #GstObject element that owns the pad.
3780  *
3781  * Reads the pad definition from the XML node and links the given pad
3782  * in the element to a pad of an element up in the hierarchy.
3783  */
3784 void
3785 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3786 {
3787   xmlNodePtr field = self->xmlChildrenNode;
3788   GstPad *pad = NULL, *targetpad;
3789   GstPadTemplate *tmpl;
3790   gchar *peer = NULL;
3791   gchar **split;
3792   GstElement *target;
3793   GstObject *grandparent;
3794   gchar *name = NULL;
3795
3796   while (field) {
3797     if (!strcmp ((char *) field->name, "name")) {
3798       name = (gchar *) xmlNodeGetContent (field);
3799       pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3800       if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3801               && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3802         pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3803       g_free (name);
3804     } else if (!strcmp ((char *) field->name, "peer")) {
3805       peer = (gchar *) xmlNodeGetContent (field);
3806     }
3807     field = field->next;
3808   }
3809   g_return_if_fail (pad != NULL);
3810
3811   if (peer == NULL)
3812     return;
3813
3814   split = g_strsplit (peer, ".", 2);
3815
3816   if (split[0] == NULL || split[1] == NULL) {
3817     GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3818         "Could not parse peer '%s', leaving unlinked", peer);
3819
3820     g_free (peer);
3821     return;
3822   }
3823   g_free (peer);
3824
3825   g_return_if_fail (split[0] != NULL);
3826   g_return_if_fail (split[1] != NULL);
3827
3828   grandparent = gst_object_get_parent (parent);
3829
3830   if (grandparent && GST_IS_BIN (grandparent)) {
3831     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3832   } else
3833     goto cleanup;
3834
3835   if (target == NULL)
3836     goto cleanup;
3837
3838   targetpad = gst_element_get_static_pad (target, split[1]);
3839   if (!targetpad)
3840     targetpad = gst_element_get_request_pad (target, split[1]);
3841
3842   if (targetpad == NULL)
3843     goto cleanup;
3844
3845   if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3846     gst_pad_link (pad, targetpad);
3847   else
3848     gst_pad_link (targetpad, pad);
3849
3850 cleanup:
3851   g_strfreev (split);
3852 }
3853
3854 /**
3855  * gst_pad_save_thyself:
3856  * @pad: a #GstPad to save.
3857  * @parent: the parent #xmlNodePtr to save the description in.
3858  *
3859  * Saves the pad into an xml representation.
3860  *
3861  * Returns: the #xmlNodePtr representation of the pad.
3862  */
3863 static xmlNodePtr
3864 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3865 {
3866   GstPad *pad;
3867   GstPad *peer;
3868
3869   g_return_val_if_fail (GST_IS_PAD (object), NULL);
3870
3871   pad = GST_PAD_CAST (object);
3872
3873   xmlNewChild (parent, NULL, (xmlChar *) "name",
3874       (xmlChar *) GST_PAD_NAME (pad));
3875
3876   if (GST_PAD_IS_SRC (pad)) {
3877     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3878   } else if (GST_PAD_IS_SINK (pad)) {
3879     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3880   } else {
3881     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3882   }
3883
3884   if (GST_PAD_PEER (pad) != NULL) {
3885     gchar *content;
3886
3887     peer = GST_PAD_PEER (pad);
3888     /* first check to see if the peer's parent's parent is the same */
3889     /* we just save it off */
3890     content = g_strdup_printf ("%s.%s",
3891         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3892     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3893     g_free (content);
3894   } else
3895     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3896
3897   return parent;
3898 }
3899
3900 #if 0
3901 /**
3902  * gst_ghost_pad_save_thyself:
3903  * @pad: a ghost #GstPad to save.
3904  * @parent: the parent #xmlNodePtr to save the description in.
3905  *
3906  * Saves the ghost pad into an xml representation.
3907  *
3908  * Returns: the #xmlNodePtr representation of the pad.
3909  */
3910 xmlNodePtr
3911 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3912 {
3913   xmlNodePtr self;
3914
3915   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3916
3917   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3918   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3919   xmlNewChild (self, NULL, (xmlChar *) "parent",
3920       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3921
3922   /* FIXME FIXME FIXME! */
3923
3924   return self;
3925 }
3926 #endif /* 0 */
3927 #endif /* GST_DISABLE_LOADSAVE */
3928
3929 /*
3930  * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3931  * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3932  * called.
3933  *
3934  * This function performs the pad blocking when an event, buffer push
3935  * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3936  * streaming thread after informing the pad has been blocked.
3937  *
3938  * An application can with this method wait and block any streaming
3939  * thread and perform operations such as seeking or linking.
3940  *
3941  * Two methods are available for notifying the application of the
3942  * block:
3943  * - the callback method, which happens in the STREAMING thread with
3944  *   the STREAM_LOCK held. With this method, the most useful way of
3945  *   dealing with the callback is to post a message to the main thread
3946  *   where the pad block can then be handled outside of the streaming
3947  *   thread. With the last method one can perform all operations such
3948  *   as doing a state change, linking, unblocking, seeking etc on the
3949  *   pad.
3950  * - the GCond signal method, which makes any thread unblock when
3951  *   the pad block happens.
3952  *
3953  * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3954  * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3955  *
3956  * MT safe.
3957  */
3958 static GstFlowReturn
3959 handle_pad_block (GstPad * pad)
3960 {
3961   GstPadBlockCallback callback;
3962   gpointer user_data;
3963   GstFlowReturn ret = GST_FLOW_OK;
3964
3965   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3966
3967   /* flushing, don't bother trying to block and return WRONG_STATE
3968    * right away */
3969   if (GST_PAD_IS_FLUSHING (pad))
3970     goto flushingnonref;
3971
3972   /* we grab an extra ref for the callbacks, this is probably not
3973    * needed (callback code does not have a ref and cannot unref). I
3974    * think this was done to make it possible to unref the element in
3975    * the callback, which is in the end totally impossible as it
3976    * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3977    * all taken when calling this function. */
3978   gst_object_ref (pad);
3979
3980   while (GST_PAD_IS_BLOCKED (pad)) {
3981     do {
3982       /* we either have a callback installed to notify the block or
3983        * some other thread is doing a GCond wait. */
3984       callback = pad->block_callback;
3985       pad->abidata.ABI.block_callback_called = TRUE;
3986       if (callback) {
3987         /* there is a callback installed, call it. We release the
3988          * lock so that the callback can do something usefull with the
3989          * pad */
3990         user_data = pad->block_data;
3991         GST_OBJECT_UNLOCK (pad);
3992         callback (pad, TRUE, user_data);
3993         GST_OBJECT_LOCK (pad);
3994
3995         /* we released the lock, recheck flushing */
3996         if (GST_PAD_IS_FLUSHING (pad))
3997           goto flushing;
3998       } else {
3999         /* no callback, signal the thread that is doing a GCond wait
4000          * if any. */
4001         GST_PAD_BLOCK_BROADCAST (pad);
4002       }
4003     } while (pad->abidata.ABI.block_callback_called == FALSE
4004         && GST_PAD_IS_BLOCKED (pad));
4005
4006     /* OBJECT_LOCK could have been released when we did the callback, which
4007      * then could have made the pad unblock so we need to check the blocking
4008      * condition again.   */
4009     if (!GST_PAD_IS_BLOCKED (pad))
4010       break;
4011
4012     /* now we block the streaming thread. It can be unlocked when we
4013      * deactivate the pad (which will also set the FLUSHING flag) or
4014      * when the pad is unblocked. A flushing event will also unblock
4015      * the pad after setting the FLUSHING flag. */
4016     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4017         "Waiting to be unblocked or set flushing");
4018     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
4019     GST_PAD_BLOCK_WAIT (pad);
4020     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
4021
4022     /* see if we got unblocked by a flush or not */
4023     if (GST_PAD_IS_FLUSHING (pad))
4024       goto flushing;
4025   }
4026
4027   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
4028
4029   /* when we get here, the pad is unblocked again and we perform
4030    * the needed unblock code. */
4031   callback = pad->block_callback;
4032   if (callback) {
4033     /* we need to call the callback */
4034     user_data = pad->block_data;
4035     GST_OBJECT_UNLOCK (pad);
4036     callback (pad, FALSE, user_data);
4037     GST_OBJECT_LOCK (pad);
4038   } else {
4039     /* we need to signal the thread waiting on the GCond */
4040     GST_PAD_BLOCK_BROADCAST (pad);
4041   }
4042
4043   gst_object_unref (pad);
4044
4045   return ret;
4046
4047 flushingnonref:
4048   {
4049     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
4050     return GST_FLOW_WRONG_STATE;
4051   }
4052 flushing:
4053   {
4054     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
4055     gst_object_unref (pad);
4056     return GST_FLOW_WRONG_STATE;
4057   }
4058 }
4059
4060 /**********************************************************************
4061  * Data passing functions
4062  */
4063
4064 static gboolean
4065 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
4066 {
4067   GValue ret = { 0 };
4068   GValue args[2] = { {0}, {0} };
4069   gboolean res;
4070   GQuark detail;
4071
4072   /* init */
4073   g_value_init (&ret, G_TYPE_BOOLEAN);
4074   g_value_set_boolean (&ret, TRUE);
4075   g_value_init (&args[0], GST_TYPE_PAD);
4076   g_value_set_object (&args[0], pad);
4077   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4078   gst_value_set_mini_object (&args[1], obj);
4079
4080   if (GST_IS_EVENT (obj))
4081     detail = event_quark;
4082   else
4083     detail = buffer_quark;
4084
4085   /* actually emit */
4086   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4087   res = g_value_get_boolean (&ret);
4088
4089   /* clean up */
4090   g_value_unset (&ret);
4091   g_value_unset (&args[0]);
4092   g_value_unset (&args[1]);
4093
4094   return res;
4095 }
4096
4097 static void
4098 gst_pad_data_unref (gboolean is_buffer, void *data)
4099 {
4100   if (G_LIKELY (is_buffer)) {
4101     gst_buffer_unref (data);
4102   } else {
4103     gst_buffer_list_unref (data);
4104   }
4105 }
4106
4107 static GstCaps *
4108 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4109 {
4110   GstCaps *caps;
4111
4112   if (G_LIKELY (is_buffer)) {
4113     caps = GST_BUFFER_CAPS (data);
4114   } else {
4115     GstBuffer *buf;
4116
4117     if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4118       caps = GST_BUFFER_CAPS (buf);
4119     else
4120       caps = NULL;
4121   }
4122   return caps;
4123 }
4124
4125 /* this is the chain function that does not perform the additional argument
4126  * checking for that little extra speed.
4127  */
4128 static inline GstFlowReturn
4129 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
4130 {
4131   GstCaps *caps;
4132   gboolean caps_changed;
4133   GstFlowReturn ret;
4134   gboolean emit_signal;
4135
4136   GST_PAD_STREAM_LOCK (pad);
4137
4138   GST_OBJECT_LOCK (pad);
4139   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4140     goto flushing;
4141
4142   caps = gst_pad_data_get_caps (is_buffer, data);
4143   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4144
4145   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4146   GST_OBJECT_UNLOCK (pad);
4147
4148   /* see if the signal should be emited, we emit before caps nego as
4149    * we might drop the buffer and do capsnego for nothing. */
4150   if (G_UNLIKELY (emit_signal)) {
4151     if (G_LIKELY (is_buffer)) {
4152       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4153         goto dropping;
4154     } else {
4155       /* chain all groups in the buffer list one by one to avoid problems with
4156        * buffer probes that push buffers or events */
4157       goto chain_groups;
4158     }
4159   }
4160
4161   /* we got a new datatype on the pad, see if it can handle it */
4162   if (G_UNLIKELY (caps_changed)) {
4163     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4164     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4165       goto not_negotiated;
4166   }
4167
4168   /* NOTE: we read the chainfunc unlocked.
4169    * we cannot hold the lock for the pad so we might send
4170    * the data to the wrong function. This is not really a
4171    * problem since functions are assigned at creation time
4172    * and don't change that often... */
4173   if (G_LIKELY (is_buffer)) {
4174     GstPadChainFunction chainfunc;
4175
4176     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4177       goto no_function;
4178
4179     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4180         "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4181
4182     ret = chainfunc (pad, GST_BUFFER_CAST (data));
4183
4184     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4185         "called chainfunction &%s, returned %s",
4186         GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4187   } else {
4188     GstPadChainListFunction chainlistfunc;
4189
4190     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4191       goto chain_groups;
4192
4193     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4194         "calling chainlistfunction &%s",
4195         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4196
4197     ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4198
4199     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4200         "called chainlistfunction &%s, returned %s",
4201         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4202   }
4203
4204   GST_PAD_STREAM_UNLOCK (pad);
4205
4206   return ret;
4207
4208 chain_groups:
4209   {
4210     GstBufferList *list;
4211     GstBufferListIterator *it;
4212     GstBuffer *group;
4213
4214     GST_PAD_STREAM_UNLOCK (pad);
4215
4216     GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4217
4218     list = GST_BUFFER_LIST_CAST (data);
4219     it = gst_buffer_list_iterate (list);
4220
4221     if (gst_buffer_list_iterator_next_group (it)) {
4222       do {
4223         group = gst_buffer_list_iterator_merge_group (it);
4224         if (group == NULL) {
4225           group = gst_buffer_new ();
4226           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4227         } else {
4228           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4229         }
4230         ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4231       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4232     } else {
4233       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4234       ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4235     }
4236
4237     gst_buffer_list_iterator_free (it);
4238     gst_buffer_list_unref (list);
4239
4240     return ret;
4241   }
4242
4243   /* ERRORS */
4244 flushing:
4245   {
4246     gst_pad_data_unref (is_buffer, data);
4247     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4248         "pushing, but pad was flushing");
4249     GST_OBJECT_UNLOCK (pad);
4250     GST_PAD_STREAM_UNLOCK (pad);
4251     return GST_FLOW_WRONG_STATE;
4252   }
4253 dropping:
4254   {
4255     gst_pad_data_unref (is_buffer, data);
4256     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4257     GST_PAD_STREAM_UNLOCK (pad);
4258     return GST_FLOW_OK;
4259   }
4260 not_negotiated:
4261   {
4262     gst_pad_data_unref (is_buffer, data);
4263     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4264         "pushing data but pad did not accept");
4265     GST_PAD_STREAM_UNLOCK (pad);
4266     return GST_FLOW_NOT_NEGOTIATED;
4267   }
4268 no_function:
4269   {
4270     gst_pad_data_unref (is_buffer, data);
4271     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4272         "pushing, but not chainhandler");
4273     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4274         ("push on pad %s:%s but it has no chainfunction",
4275             GST_DEBUG_PAD_NAME (pad)));
4276     GST_PAD_STREAM_UNLOCK (pad);
4277     return GST_FLOW_NOT_SUPPORTED;
4278   }
4279 }
4280
4281 /**
4282  * gst_pad_chain:
4283  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4284  * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4285  *
4286  * Chain a buffer to @pad.
4287  *
4288  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4289  *
4290  * If the caps on @buffer are different from the current caps on @pad, this
4291  * function will call any setcaps function (see gst_pad_set_setcaps_function())
4292  * installed on @pad. If the new caps are not acceptable for @pad, this
4293  * function returns #GST_FLOW_NOT_NEGOTIATED.
4294  *
4295  * The function proceeds calling the chain function installed on @pad (see
4296  * gst_pad_set_chain_function()) and the return value of that function is
4297  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4298  * chain function.
4299  *
4300  * In all cases, success or failure, the caller loses its reference to @buffer
4301  * after calling this function.
4302  *
4303  * Returns: a #GstFlowReturn from the pad.
4304  *
4305  * MT safe.
4306  */
4307 GstFlowReturn
4308 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4309 {
4310   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4311   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4312   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4313
4314   return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4315 }
4316
4317 /**
4318  * gst_pad_chain_list:
4319  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4320  * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4321  *
4322  * Chain a bufferlist to @pad.
4323  *
4324  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4325  *
4326  * If the caps on the first buffer of @list are different from the current
4327  * caps on @pad, this function will call any setcaps function
4328  * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4329  * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4330  *
4331  * The function proceeds calling the chainlist function installed on @pad (see
4332  * gst_pad_set_chain_list_function()) and the return value of that function is
4333  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4334  * chainlist function.
4335  *
4336  * In all cases, success or failure, the caller loses its reference to @list
4337  * after calling this function.
4338  *
4339  * MT safe.
4340  *
4341  * Returns: a #GstFlowReturn from the pad.
4342  *
4343  * Since: 0.10.24
4344  */
4345 GstFlowReturn
4346 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4347 {
4348   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4349   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4350   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4351
4352   return gst_pad_chain_data_unchecked (pad, FALSE, list);
4353 }
4354
4355 static GstFlowReturn
4356 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4357 {
4358   GstPad *peer;
4359   GstFlowReturn ret;
4360   GstCaps *caps;
4361   gboolean caps_changed;
4362
4363   GST_OBJECT_LOCK (pad);
4364
4365   /* FIXME: this check can go away; pad_set_blocked could be implemented with
4366    * probes completely or probes with an extended pad block. */
4367   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4368     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4369       goto flushed;
4370
4371   /* we emit signals on the pad arg, the peer will have a chance to
4372    * emit in the _chain() function */
4373   if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4374     /* unlock before emitting */
4375     GST_OBJECT_UNLOCK (pad);
4376
4377     if (G_LIKELY (is_buffer)) {
4378       /* if the signal handler returned FALSE, it means we should just drop the
4379        * buffer */
4380       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4381         goto dropped;
4382     } else {
4383       /* push all buffers in the list */
4384       goto push_groups;
4385     }
4386     GST_OBJECT_LOCK (pad);
4387   }
4388
4389   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4390     goto not_linked;
4391
4392   /* Before pushing the buffer to the peer pad, ensure that caps
4393    * are set on this pad */
4394   caps = gst_pad_data_get_caps (is_buffer, data);
4395   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4396
4397   /* take ref to peer pad before releasing the lock */
4398   gst_object_ref (peer);
4399
4400   GST_OBJECT_UNLOCK (pad);
4401
4402   /* we got a new datatype from the pad, it had better handle it */
4403   if (G_UNLIKELY (caps_changed)) {
4404     GST_DEBUG_OBJECT (pad,
4405         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4406         GST_PAD_CAPS (pad), caps, caps);
4407     if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
4408       goto not_negotiated;
4409   }
4410
4411   ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4412
4413   gst_object_unref (peer);
4414
4415   return ret;
4416
4417 push_groups:
4418   {
4419     GstBufferList *list;
4420     GstBufferListIterator *it;
4421     GstBuffer *group;
4422
4423     GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4424
4425     list = GST_BUFFER_LIST_CAST (data);
4426     it = gst_buffer_list_iterate (list);
4427
4428     if (gst_buffer_list_iterator_next_group (it)) {
4429       do {
4430         group = gst_buffer_list_iterator_merge_group (it);
4431         if (group == NULL) {
4432           group = gst_buffer_new ();
4433           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4434         } else {
4435           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4436         }
4437         ret = gst_pad_push_data (pad, TRUE, group);
4438       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4439     } else {
4440       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4441       ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4442     }
4443
4444     gst_buffer_list_iterator_free (it);
4445     gst_buffer_list_unref (list);
4446
4447     return ret;
4448   }
4449
4450   /* ERROR recovery here */
4451 flushed:
4452   {
4453     gst_pad_data_unref (is_buffer, data);
4454     GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4455     GST_OBJECT_UNLOCK (pad);
4456     return ret;
4457   }
4458 dropped:
4459   {
4460     gst_pad_data_unref (is_buffer, data);
4461     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4462     return GST_FLOW_OK;
4463   }
4464 not_linked:
4465   {
4466     gst_pad_data_unref (is_buffer, data);
4467     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4468         "pushing, but it was not linked");
4469     GST_OBJECT_UNLOCK (pad);
4470     return GST_FLOW_NOT_LINKED;
4471   }
4472 not_negotiated:
4473   {
4474     gst_pad_data_unref (is_buffer, data);
4475     gst_object_unref (peer);
4476     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4477         "element pushed data then refused to accept the caps");
4478     return GST_FLOW_NOT_NEGOTIATED;
4479   }
4480 }
4481
4482 /**
4483  * gst_pad_push:
4484  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4485  * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4486  *
4487  * Pushes a buffer to the peer of @pad.
4488  *
4489  * This function will call an installed pad block before triggering any
4490  * installed pad probes.
4491  *
4492  * If the caps on @buffer are different from the currently configured caps on
4493  * @pad, this function will call any installed setcaps function on @pad (see
4494  * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4495  * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4496  *
4497  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4498  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4499  * be returned.
4500  *
4501  * In all cases, success or failure, the caller loses its reference to @buffer
4502  * after calling this function.
4503  *
4504  * Returns: a #GstFlowReturn from the peer pad.
4505  *
4506  * MT safe.
4507  */
4508 GstFlowReturn
4509 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4510 {
4511   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4512   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4513   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4514
4515   return gst_pad_push_data (pad, TRUE, buffer);
4516 }
4517
4518 /**
4519  * gst_pad_push_list:
4520  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4521  * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4522  *
4523  * Pushes a buffer list to the peer of @pad.
4524  *
4525  * This function will call an installed pad block before triggering any
4526  * installed pad probes.
4527  *
4528  * If the caps on the first buffer in the first group of @list are different
4529  * from the currently configured caps on @pad, this function will call any
4530  * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4531  * case of failure to renegotiate the new format, this function returns
4532  * #GST_FLOW_NOT_NEGOTIATED.
4533  *
4534  * If there are any probes installed on @pad every group of the buffer list
4535  * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4536  * buffer list will be unreffed.
4537  *
4538  * The function proceeds calling the chain function on the peer pad and returns
4539  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4540  * be returned. If the peer pad does not have any installed chainlist function
4541  * every group buffer of the list will be merged into a normal #GstBuffer and
4542  * chained via gst_pad_chain().
4543  *
4544  * In all cases, success or failure, the caller loses its reference to @list
4545  * after calling this function.
4546  *
4547  * Returns: a #GstFlowReturn from the peer pad.
4548  *
4549  * MT safe.
4550  *
4551  * Since: 0.10.24
4552  */
4553 GstFlowReturn
4554 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4555 {
4556   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4557   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4558   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4559
4560   return gst_pad_push_data (pad, FALSE, list);
4561 }
4562
4563 /**
4564  * gst_pad_check_pull_range:
4565  * @pad: a sink #GstPad.
4566  *
4567  * Checks if a gst_pad_pull_range() can be performed on the peer
4568  * source pad. This function is used by plugins that want to check
4569  * if they can use random access on the peer source pad.
4570  *
4571  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4572  * if it needs to perform some logic to determine if pull_range is
4573  * possible.
4574  *
4575  * Returns: a gboolean with the result.
4576  *
4577  * MT safe.
4578  */
4579 gboolean
4580 gst_pad_check_pull_range (GstPad * pad)
4581 {
4582   GstPad *peer;
4583   gboolean ret;
4584   GstPadCheckGetRangeFunction checkgetrangefunc;
4585
4586   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4587
4588   GST_OBJECT_LOCK (pad);
4589   if (!GST_PAD_IS_SINK (pad))
4590     goto wrong_direction;
4591
4592   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4593     goto not_connected;
4594
4595   gst_object_ref (peer);
4596   GST_OBJECT_UNLOCK (pad);
4597
4598   /* see note in above function */
4599   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4600     /* FIXME, kindoff ghetto */
4601     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4602     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4603         "no checkgetrangefunc, assuming %d", ret);
4604   } else {
4605     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4606         "calling checkgetrangefunc %s of peer pad %s:%s",
4607         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4608
4609     ret = checkgetrangefunc (peer);
4610   }
4611
4612   gst_object_unref (peer);
4613
4614   return ret;
4615
4616   /* ERROR recovery here */
4617 wrong_direction:
4618   {
4619     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4620         "checking pull range, but pad must be a sinkpad");
4621     GST_OBJECT_UNLOCK (pad);
4622     return FALSE;
4623   }
4624 not_connected:
4625   {
4626     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4627         "checking pull range, but it was not linked");
4628     GST_OBJECT_UNLOCK (pad);
4629     return FALSE;
4630   }
4631 }
4632
4633 static GstFlowReturn
4634 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4635     GstBuffer ** buffer)
4636 {
4637   GstFlowReturn ret;
4638   GstPadGetRangeFunction getrangefunc;
4639   gboolean emit_signal;
4640   GstCaps *caps;
4641   gboolean caps_changed;
4642
4643   GST_PAD_STREAM_LOCK (pad);
4644
4645   GST_OBJECT_LOCK (pad);
4646   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4647     goto flushing;
4648
4649   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4650   GST_OBJECT_UNLOCK (pad);
4651
4652   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4653     goto no_function;
4654
4655   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4656       "calling getrangefunc %s, offset %"
4657       G_GUINT64_FORMAT ", size %u",
4658       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4659
4660   ret = getrangefunc (pad, offset, size, buffer);
4661
4662   /* can only fire the signal if we have a valid buffer */
4663   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4664     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4665       goto dropping;
4666   }
4667
4668   GST_PAD_STREAM_UNLOCK (pad);
4669
4670   if (G_UNLIKELY (ret != GST_FLOW_OK))
4671     goto get_range_failed;
4672
4673   GST_OBJECT_LOCK (pad);
4674   /* Before pushing the buffer to the peer pad, ensure that caps
4675    * are set on this pad */
4676   caps = GST_BUFFER_CAPS (*buffer);
4677   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4678   GST_OBJECT_UNLOCK (pad);
4679
4680   if (G_UNLIKELY (caps_changed)) {
4681     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4682     /* this should usually work because the element produced the buffer */
4683     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4684       goto not_negotiated;
4685   }
4686   return ret;
4687
4688   /* ERRORS */
4689 flushing:
4690   {
4691     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4692         "pulling range, but pad was flushing");
4693     GST_OBJECT_UNLOCK (pad);
4694     GST_PAD_STREAM_UNLOCK (pad);
4695     return GST_FLOW_WRONG_STATE;
4696   }
4697 no_function:
4698   {
4699     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4700         ("pullrange on pad %s:%s but it has no getrangefunction",
4701             GST_DEBUG_PAD_NAME (pad)));
4702     GST_PAD_STREAM_UNLOCK (pad);
4703     return GST_FLOW_NOT_SUPPORTED;
4704   }
4705 dropping:
4706   {
4707     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4708         "Dropping data after FALSE probe return");
4709     GST_PAD_STREAM_UNLOCK (pad);
4710     gst_buffer_unref (*buffer);
4711     *buffer = NULL;
4712     return GST_FLOW_UNEXPECTED;
4713   }
4714 get_range_failed:
4715   {
4716     *buffer = NULL;
4717     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4718         (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4719         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4720     return ret;
4721   }
4722 not_negotiated:
4723   {
4724     gst_buffer_unref (*buffer);
4725     *buffer = NULL;
4726     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4727         "getrange returned buffer of unaccaptable caps");
4728     return GST_FLOW_NOT_NEGOTIATED;
4729   }
4730 }
4731
4732 /**
4733  * gst_pad_get_range:
4734  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4735  * @offset: The start offset of the buffer
4736  * @size: The length of the buffer
4737  * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4738  *
4739  * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4740  * immediatly and @buffer is %NULL.
4741  *
4742  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4743  * description of a getrange function. If @pad has no getrange function
4744  * installed (see gst_pad_set_getrange_function()) this function returns
4745  * #GST_FLOW_NOT_SUPPORTED.
4746  *
4747  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4748  *
4749  * Returns: a #GstFlowReturn from the pad.
4750  *
4751  * MT safe.
4752  */
4753 GstFlowReturn
4754 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4755     GstBuffer ** buffer)
4756 {
4757   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4758   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4759   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4760
4761   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4762 }
4763
4764 /**
4765  * gst_pad_pull_range:
4766  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4767  * @offset: The start offset of the buffer
4768  * @size: The length of the buffer
4769  * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4770  *
4771  * Pulls a @buffer from the peer pad.
4772  *
4773  * This function will first trigger the pad block signal if it was
4774  * installed.
4775  *
4776  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4777  * function returns the result of gst_pad_get_range() on the peer pad.
4778  * See gst_pad_get_range() for a list of return values and for the
4779  * semantics of the arguments of this function.
4780  *
4781  * @buffer's caps must either be unset or the same as what is already
4782  * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4783  * supported.
4784  *
4785  * Returns: a #GstFlowReturn from the peer pad.
4786  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4787  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4788  * @buffer may not be used or freed when any other return value than
4789  * #GST_FLOW_OK is returned.
4790  *
4791  * MT safe.
4792  */
4793 GstFlowReturn
4794 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4795     GstBuffer ** buffer)
4796 {
4797   GstPad *peer;
4798   GstFlowReturn ret;
4799   gboolean emit_signal;
4800   GstCaps *caps;
4801   gboolean caps_changed;
4802
4803   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4804   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4805   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4806
4807   GST_OBJECT_LOCK (pad);
4808
4809   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4810     handle_pad_block (pad);
4811
4812   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4813     goto not_connected;
4814
4815   /* signal emision for the pad, peer has chance to emit when
4816    * we call _get_range() */
4817   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4818
4819   gst_object_ref (peer);
4820   GST_OBJECT_UNLOCK (pad);
4821
4822   ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4823
4824   gst_object_unref (peer);
4825
4826   if (G_UNLIKELY (ret != GST_FLOW_OK))
4827     goto pull_range_failed;
4828
4829   /* can only fire the signal if we have a valid buffer */
4830   if (G_UNLIKELY (emit_signal)) {
4831     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4832       goto dropping;
4833   }
4834
4835   GST_OBJECT_LOCK (pad);
4836   /* Before pushing the buffer to the peer pad, ensure that caps
4837    * are set on this pad */
4838   caps = GST_BUFFER_CAPS (*buffer);
4839   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4840   GST_OBJECT_UNLOCK (pad);
4841
4842   /* we got a new datatype on the pad, see if it can handle it */
4843   if (G_UNLIKELY (caps_changed)) {
4844     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4845     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4846       goto not_negotiated;
4847   }
4848   return ret;
4849
4850   /* ERROR recovery here */
4851 not_connected:
4852   {
4853     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4854         "pulling range, but it was not linked");
4855     GST_OBJECT_UNLOCK (pad);
4856     return GST_FLOW_NOT_LINKED;
4857   }
4858 pull_range_failed:
4859   {
4860     *buffer = NULL;
4861     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4862         (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4863         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4864     return ret;
4865   }
4866 dropping:
4867   {
4868     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4869         "Dropping data after FALSE probe return");
4870     gst_buffer_unref (*buffer);
4871     *buffer = NULL;
4872     return GST_FLOW_UNEXPECTED;
4873   }
4874 not_negotiated:
4875   {
4876     gst_buffer_unref (*buffer);
4877     *buffer = NULL;
4878     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4879         "pullrange returned buffer of different caps");
4880     return GST_FLOW_NOT_NEGOTIATED;
4881   }
4882 }
4883
4884 /**
4885  * gst_pad_push_event:
4886  * @pad: a #GstPad to push the event to.
4887  * @event: the #GstEvent to send to the pad.
4888  *
4889  * Sends the event to the peer of the given pad. This function is
4890  * mainly used by elements to send events to their peer
4891  * elements.
4892  *
4893  * This function takes owership of the provided event so you should
4894  * gst_event_ref() it if you want to reuse the event after this call.
4895  *
4896  * Returns: TRUE if the event was handled.
4897  *
4898  * MT safe.
4899  */
4900 gboolean
4901 gst_pad_push_event (GstPad * pad, GstEvent * event)
4902 {
4903   GstPad *peerpad;
4904   gboolean result;
4905
4906   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4907   g_return_val_if_fail (event != NULL, FALSE);
4908   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4909
4910   GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4911
4912   GST_OBJECT_LOCK (pad);
4913
4914   /* Two checks to be made:
4915    * . (un)set the FLUSHING flag for flushing events,
4916    * . handle pad blocking */
4917   switch (GST_EVENT_TYPE (event)) {
4918     case GST_EVENT_FLUSH_START:
4919       GST_PAD_SET_FLUSHING (pad);
4920
4921       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4922         /* flush start will have set the FLUSHING flag and will then
4923          * unlock all threads doing a GCond wait on the blocking pad. This
4924          * will typically unblock the STREAMING thread blocked on a pad. */
4925         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4926             "doing block signal.");
4927         GST_PAD_BLOCK_BROADCAST (pad);
4928         goto flushed;
4929       }
4930       break;
4931     case GST_EVENT_FLUSH_STOP:
4932       GST_PAD_UNSET_FLUSHING (pad);
4933
4934       /* if we are blocked, flush away the FLUSH_STOP event */
4935       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4936         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4937         goto flushed;
4938       }
4939       break;
4940     default:
4941       while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4942         /* block the event as long as the pad is blocked */
4943         if (handle_pad_block (pad) != GST_FLOW_OK)
4944           goto flushed;
4945       }
4946       break;
4947   }
4948
4949   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4950     GST_OBJECT_UNLOCK (pad);
4951
4952     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4953       goto dropping;
4954
4955     GST_OBJECT_LOCK (pad);
4956   }
4957   peerpad = GST_PAD_PEER (pad);
4958   if (peerpad == NULL)
4959     goto not_linked;
4960
4961   GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4962       GST_EVENT_TYPE_NAME (event), peerpad);
4963   gst_object_ref (peerpad);
4964   GST_OBJECT_UNLOCK (pad);
4965
4966   result = gst_pad_send_event (peerpad, event);
4967
4968   /* Note: we gave away ownership of the event at this point */
4969   GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4970       peerpad, result);
4971   gst_object_unref (peerpad);
4972
4973   return result;
4974
4975   /* ERROR handling */
4976 dropping:
4977   {
4978     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4979     gst_event_unref (event);
4980     return FALSE;
4981   }
4982 not_linked:
4983   {
4984     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4985     gst_event_unref (event);
4986     GST_OBJECT_UNLOCK (pad);
4987     return FALSE;
4988   }
4989 flushed:
4990   {
4991     GST_DEBUG_OBJECT (pad,
4992         "Not forwarding event since we're flushing and blocking");
4993     gst_event_unref (event);
4994     GST_OBJECT_UNLOCK (pad);
4995     return TRUE;
4996   }
4997 }
4998
4999 /**
5000  * gst_pad_send_event:
5001  * @pad: a #GstPad to send the event to.
5002  * @event: the #GstEvent to send to the pad.
5003  *
5004  * Sends the event to the pad. This function can be used
5005  * by applications to send events in the pipeline.
5006  *
5007  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5008  * sink pad, @event should be a downstream event. For example, you would not
5009  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5010  * Furthermore, some downstream events have to be serialized with data flow,
5011  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5012  * the event needs to be serialized with data flow, this function will take the
5013  * pad's stream lock while calling its event function.
5014  *
5015  * To find out whether an event type is upstream, downstream, or downstream and
5016  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5017  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5018  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5019  * plugin doesn't need to bother itself with this information; the core handles
5020  * all necessary locks and checks.
5021  *
5022  * This function takes owership of the provided event so you should
5023  * gst_event_ref() it if you want to reuse the event after this call.
5024  *
5025  * Returns: TRUE if the event was handled.
5026  */
5027 gboolean
5028 gst_pad_send_event (GstPad * pad, GstEvent * event)
5029 {
5030   gboolean result = FALSE;
5031   GstPadEventFunction eventfunc;
5032   gboolean serialized, need_unlock = FALSE;
5033
5034   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5035   g_return_val_if_fail (event != NULL, FALSE);
5036
5037   GST_OBJECT_LOCK (pad);
5038   if (GST_PAD_IS_SINK (pad)) {
5039     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5040       goto wrong_direction;
5041     serialized = GST_EVENT_IS_SERIALIZED (event);
5042   } else if (GST_PAD_IS_SRC (pad)) {
5043     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5044       goto wrong_direction;
5045     /* events on srcpad never are serialized */
5046     serialized = FALSE;
5047   } else
5048     goto unknown_direction;
5049
5050   if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5051     GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5052     GST_EVENT_SRC (event) = gst_object_ref (pad);
5053   }
5054
5055   /* pad signals */
5056   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5057     GST_OBJECT_UNLOCK (pad);
5058
5059     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
5060       goto dropping;
5061
5062     GST_OBJECT_LOCK (pad);
5063   }
5064
5065   switch (GST_EVENT_TYPE (event)) {
5066     case GST_EVENT_FLUSH_START:
5067       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5068           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5069
5070       /* can't even accept a flush begin event when flushing */
5071       if (GST_PAD_IS_FLUSHING (pad))
5072         goto flushing;
5073       GST_PAD_SET_FLUSHING (pad);
5074       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5075       break;
5076     case GST_EVENT_FLUSH_STOP:
5077       if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
5078         GST_PAD_UNSET_FLUSHING (pad);
5079         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5080       }
5081       GST_OBJECT_UNLOCK (pad);
5082       /* grab stream lock */
5083       GST_PAD_STREAM_LOCK (pad);
5084       need_unlock = TRUE;
5085       GST_OBJECT_LOCK (pad);
5086       break;
5087     default:
5088       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5089           GST_EVENT_TYPE_NAME (event));
5090
5091       /* make this a little faster, no point in grabbing the lock
5092        * if the pad is allready flushing. */
5093       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5094         goto flushing;
5095
5096       if (serialized) {
5097         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5098         GST_OBJECT_UNLOCK (pad);
5099         GST_PAD_STREAM_LOCK (pad);
5100         need_unlock = TRUE;
5101         GST_OBJECT_LOCK (pad);
5102         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5103           goto flushing;
5104       }
5105       break;
5106   }
5107   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5108     goto no_function;
5109
5110   GST_OBJECT_UNLOCK (pad);
5111
5112   result = eventfunc (pad, event);
5113
5114   if (need_unlock)
5115     GST_PAD_STREAM_UNLOCK (pad);
5116
5117   GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5118
5119   return result;
5120
5121   /* ERROR handling */
5122 wrong_direction:
5123   {
5124     g_warning ("pad %s:%s sending %s event in wrong direction",
5125         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5126     GST_OBJECT_UNLOCK (pad);
5127     gst_event_unref (event);
5128     return FALSE;
5129   }
5130 unknown_direction:
5131   {
5132     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5133     GST_OBJECT_UNLOCK (pad);
5134     gst_event_unref (event);
5135     return FALSE;
5136   }
5137 no_function:
5138   {
5139     g_warning ("pad %s:%s has no event handler, file a bug.",
5140         GST_DEBUG_PAD_NAME (pad));
5141     GST_OBJECT_UNLOCK (pad);
5142     if (need_unlock)
5143       GST_PAD_STREAM_UNLOCK (pad);
5144     gst_event_unref (event);
5145     return FALSE;
5146   }
5147 flushing:
5148   {
5149     GST_OBJECT_UNLOCK (pad);
5150     if (need_unlock)
5151       GST_PAD_STREAM_UNLOCK (pad);
5152     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5153         "Received event on flushing pad. Discarding");
5154     gst_event_unref (event);
5155     return FALSE;
5156   }
5157 dropping:
5158   {
5159     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5160     gst_event_unref (event);
5161     return FALSE;
5162   }
5163 }
5164
5165 /**
5166  * gst_pad_set_element_private:
5167  * @pad: the #GstPad to set the private data of.
5168  * @priv: The private data to attach to the pad.
5169  *
5170  * Set the given private data gpointer on the pad.
5171  * This function can only be used by the element that owns the pad.
5172  * No locking is performed in this function.
5173  */
5174 void
5175 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5176 {
5177   pad->element_private = priv;
5178 }
5179
5180 /**
5181  * gst_pad_get_element_private:
5182  * @pad: the #GstPad to get the private data of.
5183  *
5184  * Gets the private data of a pad.
5185  * No locking is performed in this function.
5186  *
5187  * Returns: a #gpointer to the private data.
5188  */
5189 gpointer
5190 gst_pad_get_element_private (GstPad * pad)
5191 {
5192   return pad->element_private;
5193 }
5194
5195 static void
5196 do_stream_status (GstPad * pad, GstStreamStatusType type,
5197     GThread * thread, GstTask * task)
5198 {
5199   GstElement *parent;
5200
5201   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5202
5203   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5204     if (GST_IS_ELEMENT (parent)) {
5205       GstMessage *message;
5206       GValue value = { 0 };
5207
5208       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5209         gchar *tname, *ename, *pname;
5210
5211         /* create a good task name */
5212         ename = gst_element_get_name (parent);
5213         pname = gst_pad_get_name (pad);
5214         tname = g_strdup_printf ("%s:%s", ename, pname);
5215         g_free (ename);
5216         g_free (pname);
5217
5218         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5219         g_free (tname);
5220       }
5221
5222       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5223           type, parent);
5224
5225       g_value_init (&value, GST_TYPE_TASK);
5226       g_value_set_object (&value, task);
5227       gst_message_set_stream_status_object (message, &value);
5228       g_value_unset (&value);
5229
5230       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5231       gst_element_post_message (parent, message);
5232     }
5233     gst_object_unref (parent);
5234   }
5235 }
5236
5237 static void
5238 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5239 {
5240   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5241       thread, task);
5242 }
5243
5244 static void
5245 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5246 {
5247   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5248       thread, task);
5249 }
5250
5251 static GstTaskThreadCallbacks thr_callbacks = {
5252   pad_enter_thread,
5253   pad_leave_thread,
5254 };
5255
5256 /**
5257  * gst_pad_start_task:
5258  * @pad: the #GstPad to start the task of
5259  * @func: the task function to call
5260  * @data: data passed to the task function
5261  *
5262  * Starts a task that repeatedly calls @func with @data. This function
5263  * is mostly used in pad activation functions to start the dataflow.
5264  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5265  * before @func is called.
5266  *
5267  * Returns: a %TRUE if the task could be started.
5268  */
5269 gboolean
5270 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5271 {
5272   GstTask *task;
5273   gboolean res;
5274
5275   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5276   g_return_val_if_fail (func != NULL, FALSE);
5277
5278   GST_DEBUG_OBJECT (pad, "start task");
5279
5280   GST_OBJECT_LOCK (pad);
5281   task = GST_PAD_TASK (pad);
5282   if (task == NULL) {
5283     task = gst_task_create (func, data);
5284     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5285     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5286     GST_DEBUG_OBJECT (pad, "created task");
5287     GST_PAD_TASK (pad) = task;
5288     gst_object_ref (task);
5289     /* release lock to post the message */
5290     GST_OBJECT_UNLOCK (pad);
5291
5292     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5293
5294     gst_object_unref (task);
5295
5296     GST_OBJECT_LOCK (pad);
5297     /* nobody else is supposed to have changed the pad now */
5298     if (GST_PAD_TASK (pad) != task)
5299       goto concurrent_stop;
5300   }
5301   res = gst_task_set_state (task, GST_TASK_STARTED);
5302   GST_OBJECT_UNLOCK (pad);
5303
5304   return res;
5305
5306   /* ERRORS */
5307 concurrent_stop:
5308   {
5309     GST_OBJECT_UNLOCK (pad);
5310     return TRUE;
5311   }
5312 }
5313
5314 /**
5315  * gst_pad_pause_task:
5316  * @pad: the #GstPad to pause the task of
5317  *
5318  * Pause the task of @pad. This function will also wait until the
5319  * function executed by the task is finished if this function is not
5320  * called from the task function.
5321  *
5322  * Returns: a TRUE if the task could be paused or FALSE when the pad
5323  * has no task.
5324  */
5325 gboolean
5326 gst_pad_pause_task (GstPad * pad)
5327 {
5328   GstTask *task;
5329   gboolean res;
5330
5331   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5332
5333   GST_DEBUG_OBJECT (pad, "pause task");
5334
5335   GST_OBJECT_LOCK (pad);
5336   task = GST_PAD_TASK (pad);
5337   if (task == NULL)
5338     goto no_task;
5339   res = gst_task_set_state (task, GST_TASK_PAUSED);
5340   GST_OBJECT_UNLOCK (pad);
5341
5342   /* wait for task function to finish, this lock is recursive so it does nothing
5343    * when the pause is called from the task itself */
5344   GST_PAD_STREAM_LOCK (pad);
5345   GST_PAD_STREAM_UNLOCK (pad);
5346
5347   return res;
5348
5349 no_task:
5350   {
5351     GST_DEBUG_OBJECT (pad, "pad has no task");
5352     GST_OBJECT_UNLOCK (pad);
5353     return FALSE;
5354   }
5355 }
5356
5357 /**
5358  * gst_pad_stop_task:
5359  * @pad: the #GstPad to stop the task of
5360  *
5361  * Stop the task of @pad. This function will also make sure that the
5362  * function executed by the task will effectively stop if not called
5363  * from the GstTaskFunction.
5364  *
5365  * This function will deadlock if called from the GstTaskFunction of
5366  * the task. Use gst_task_pause() instead.
5367  *
5368  * Regardless of whether the pad has a task, the stream lock is acquired and
5369  * released so as to ensure that streaming through this pad has finished.
5370  *
5371  * Returns: a TRUE if the task could be stopped or FALSE on error.
5372  */
5373 gboolean
5374 gst_pad_stop_task (GstPad * pad)
5375 {
5376   GstTask *task;
5377   gboolean res;
5378
5379   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5380
5381   GST_DEBUG_OBJECT (pad, "stop task");
5382
5383   GST_OBJECT_LOCK (pad);
5384   task = GST_PAD_TASK (pad);
5385   if (task == NULL)
5386     goto no_task;
5387   GST_PAD_TASK (pad) = NULL;
5388   res = gst_task_set_state (task, GST_TASK_STOPPED);
5389   GST_OBJECT_UNLOCK (pad);
5390
5391   GST_PAD_STREAM_LOCK (pad);
5392   GST_PAD_STREAM_UNLOCK (pad);
5393
5394   if (!gst_task_join (task))
5395     goto join_failed;
5396
5397   gst_object_unref (task);
5398
5399   return res;
5400
5401 no_task:
5402   {
5403     GST_DEBUG_OBJECT (pad, "no task");
5404     GST_OBJECT_UNLOCK (pad);
5405
5406     GST_PAD_STREAM_LOCK (pad);
5407     GST_PAD_STREAM_UNLOCK (pad);
5408
5409     /* this is not an error */
5410     return TRUE;
5411   }
5412 join_failed:
5413   {
5414     /* this is bad, possibly the application tried to join the task from
5415      * the task's thread. We install the task again so that it will be stopped
5416      * again from the right thread next time hopefully. */
5417     GST_OBJECT_LOCK (pad);
5418     GST_DEBUG_OBJECT (pad, "join failed");
5419     /* we can only install this task if there was no other task */
5420     if (GST_PAD_TASK (pad) == NULL)
5421       GST_PAD_TASK (pad) = task;
5422     GST_OBJECT_UNLOCK (pad);
5423
5424     return FALSE;
5425   }
5426 }