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