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