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