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