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