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