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