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