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