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