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