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