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