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