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