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