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