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