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