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