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