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