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