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