tracer: add missing hooks
[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  * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1328  * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1329  * happen that the probe can be run immediately and if the probe returns
1330  * GST_PAD_PROBE_REMOVE this functions returns 0.
1331  *
1332  * MT safe.
1333  */
1334 gulong
1335 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1336     GstPadProbeCallback callback, gpointer user_data,
1337     GDestroyNotify destroy_data)
1338 {
1339   GHook *hook;
1340   gulong res;
1341
1342   g_return_val_if_fail (GST_IS_PAD (pad), 0);
1343   g_return_val_if_fail (mask != 0, 0);
1344
1345   GST_OBJECT_LOCK (pad);
1346
1347   /* make a new probe */
1348   hook = g_hook_alloc (&pad->probes);
1349
1350   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1351       mask);
1352
1353   /* when no contraints are given for the types, assume all types are
1354    * acceptable */
1355   if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1356     mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1357   if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1358     mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1359
1360   /* store our flags and other fields */
1361   hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1362   hook->func = callback;
1363   hook->data = user_data;
1364   hook->destroy = destroy_data;
1365   PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1366
1367   /* add the probe */
1368   g_hook_prepend (&pad->probes, hook);
1369   pad->num_probes++;
1370   /* incremenent cookie so that the new hook get's called */
1371   pad->priv->probe_list_cookie++;
1372
1373   /* get the id of the hook, we return this and it can be used to remove the
1374    * probe later */
1375   res = hook->hook_id;
1376
1377   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1378
1379   if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1380     /* we have a block probe */
1381     pad->num_blocked++;
1382     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1383     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1384         "now %d blocking probes", pad->num_blocked);
1385
1386     /* Might have new probes now that want to be called */
1387     GST_PAD_BLOCK_BROADCAST (pad);
1388   }
1389
1390   /* call the callback if we need to be called for idle callbacks */
1391   if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1392     if (pad->priv->using > 0) {
1393       /* the pad is in use, we can't signal the idle callback yet. Since we set the
1394        * flag above, the last thread to leave the push will do the callback. New
1395        * threads going into the push will block. */
1396       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1397           "pad is in use, delay idle callback");
1398       GST_OBJECT_UNLOCK (pad);
1399     } else {
1400       GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1401       GstPadProbeReturn ret;
1402
1403       /* Keep another ref, the callback could destroy the pad */
1404       gst_object_ref (pad);
1405       pad->priv->idle_running++;
1406
1407       /* the pad is idle now, we can signal the idle callback now */
1408       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1409           "pad is idle, trigger idle callback");
1410       GST_OBJECT_UNLOCK (pad);
1411
1412       ret = callback (pad, &info, user_data);
1413
1414       GST_OBJECT_LOCK (pad);
1415       switch (ret) {
1416         case GST_PAD_PROBE_REMOVE:
1417           /* remove the probe */
1418           GST_DEBUG_OBJECT (pad, "asked to remove hook");
1419           cleanup_hook (pad, hook);
1420           res = 0;
1421           break;
1422         case GST_PAD_PROBE_DROP:
1423           GST_DEBUG_OBJECT (pad, "asked to drop item");
1424           break;
1425         case GST_PAD_PROBE_PASS:
1426           GST_DEBUG_OBJECT (pad, "asked to pass item");
1427           break;
1428         case GST_PAD_PROBE_OK:
1429           GST_DEBUG_OBJECT (pad, "probe returned OK");
1430           break;
1431         case GST_PAD_PROBE_HANDLED:
1432           GST_DEBUG_OBJECT (pad, "probe handled the data");
1433           break;
1434         default:
1435           GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1436           break;
1437       }
1438       pad->priv->idle_running--;
1439       if (pad->priv->idle_running == 0) {
1440         GST_PAD_BLOCK_BROADCAST (pad);
1441       }
1442       GST_OBJECT_UNLOCK (pad);
1443
1444       gst_object_unref (pad);
1445     }
1446   } else {
1447     GST_OBJECT_UNLOCK (pad);
1448   }
1449   return res;
1450 }
1451
1452 /**
1453  * gst_pad_remove_probe:
1454  * @pad: the #GstPad with the probe
1455  * @id: the probe id to remove
1456  *
1457  * Remove the probe with @id from @pad.
1458  *
1459  * MT safe.
1460  */
1461 void
1462 gst_pad_remove_probe (GstPad * pad, gulong id)
1463 {
1464   GHook *hook;
1465
1466   g_return_if_fail (GST_IS_PAD (pad));
1467
1468   GST_OBJECT_LOCK (pad);
1469
1470   hook = g_hook_get (&pad->probes, id);
1471   if (hook == NULL)
1472     goto not_found;
1473
1474   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1475       hook->hook_id);
1476   cleanup_hook (pad, hook);
1477   GST_OBJECT_UNLOCK (pad);
1478
1479   return;
1480
1481 not_found:
1482   {
1483     GST_OBJECT_UNLOCK (pad);
1484     g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1485     return;
1486   }
1487 }
1488
1489 /**
1490  * gst_pad_is_blocked:
1491  * @pad: the #GstPad to query
1492  *
1493  * Checks if the pad is blocked or not. This function returns the
1494  * last requested state of the pad. It is not certain that the pad
1495  * is actually blocking at this point (see gst_pad_is_blocking()).
1496  *
1497  * Returns: %TRUE if the pad is blocked.
1498  *
1499  * MT safe.
1500  */
1501 gboolean
1502 gst_pad_is_blocked (GstPad * pad)
1503 {
1504   gboolean result = FALSE;
1505
1506   g_return_val_if_fail (GST_IS_PAD (pad), result);
1507
1508   GST_OBJECT_LOCK (pad);
1509   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1510   GST_OBJECT_UNLOCK (pad);
1511
1512   return result;
1513 }
1514
1515 /**
1516  * gst_pad_is_blocking:
1517  * @pad: the #GstPad to query
1518  *
1519  * Checks if the pad is blocking or not. This is a guaranteed state
1520  * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1521  *
1522  * Returns: %TRUE if the pad is blocking.
1523  *
1524  * MT safe.
1525  */
1526 gboolean
1527 gst_pad_is_blocking (GstPad * pad)
1528 {
1529   gboolean result = FALSE;
1530
1531   g_return_val_if_fail (GST_IS_PAD (pad), result);
1532
1533   GST_OBJECT_LOCK (pad);
1534   /* the blocking flag is only valid if the pad is not flushing */
1535   result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1536   GST_OBJECT_UNLOCK (pad);
1537
1538   return result;
1539 }
1540
1541 /**
1542  * gst_pad_needs_reconfigure:
1543  * @pad: the #GstPad to check
1544  *
1545  * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1546  * if the flag was set.
1547  *
1548  * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1549  */
1550 gboolean
1551 gst_pad_needs_reconfigure (GstPad * pad)
1552 {
1553   gboolean reconfigure;
1554
1555   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1556
1557   GST_OBJECT_LOCK (pad);
1558   reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1559   GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1560   GST_OBJECT_UNLOCK (pad);
1561
1562   return reconfigure;
1563 }
1564
1565 /**
1566  * gst_pad_check_reconfigure:
1567  * @pad: the #GstPad to check
1568  *
1569  * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1570  * if the flag was set.
1571  *
1572  * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1573  */
1574 gboolean
1575 gst_pad_check_reconfigure (GstPad * pad)
1576 {
1577   gboolean reconfigure;
1578
1579   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1580
1581   GST_OBJECT_LOCK (pad);
1582   reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1583   if (reconfigure) {
1584     GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1585     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1586   }
1587   GST_OBJECT_UNLOCK (pad);
1588
1589   return reconfigure;
1590 }
1591
1592 /**
1593  * gst_pad_mark_reconfigure:
1594  * @pad: the #GstPad to mark
1595  *
1596  * Mark a pad for needing reconfiguration. The next call to
1597  * gst_pad_check_reconfigure() will return %TRUE after this call.
1598  */
1599 void
1600 gst_pad_mark_reconfigure (GstPad * pad)
1601 {
1602   g_return_if_fail (GST_IS_PAD (pad));
1603
1604   GST_OBJECT_LOCK (pad);
1605   GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1606   GST_OBJECT_UNLOCK (pad);
1607 }
1608
1609 /**
1610  * gst_pad_set_activate_function:
1611  * @p: a #GstPad.
1612  * @f: the #GstPadActivateFunction to set.
1613  *
1614  * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1615  * notify.
1616  */
1617 /**
1618  * gst_pad_set_activate_function_full:
1619  * @pad: a #GstPad.
1620  * @activate: the #GstPadActivateFunction to set.
1621  * @user_data: user_data passed to @notify
1622  * @notify: notify called when @activate will not be used anymore.
1623  *
1624  * Sets the given activate function for @pad. The activate function will
1625  * dispatch to gst_pad_activate_mode() to perform the actual activation.
1626  * Only makes sense to set on sink pads.
1627  *
1628  * Call this function if your sink pad can start a pull-based task.
1629  */
1630 void
1631 gst_pad_set_activate_function_full (GstPad * pad,
1632     GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1633 {
1634   g_return_if_fail (GST_IS_PAD (pad));
1635
1636   if (pad->activatenotify)
1637     pad->activatenotify (pad->activatedata);
1638   GST_PAD_ACTIVATEFUNC (pad) = activate;
1639   pad->activatedata = user_data;
1640   pad->activatenotify = notify;
1641
1642   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1643       GST_DEBUG_FUNCPTR_NAME (activate));
1644 }
1645
1646 /**
1647  * gst_pad_set_activatemode_function:
1648  * @p: a #GstPad.
1649  * @f: the #GstPadActivateModeFunction to set.
1650  *
1651  * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1652  * notify.
1653  */
1654 /**
1655  * gst_pad_set_activatemode_function_full:
1656  * @pad: a #GstPad.
1657  * @activatemode: the #GstPadActivateModeFunction to set.
1658  * @user_data: user_data passed to @notify
1659  * @notify: notify called when @activatemode will not be used anymore.
1660  *
1661  * Sets the given activate_mode function for the pad. An activate_mode function
1662  * prepares the element for data passing.
1663  */
1664 void
1665 gst_pad_set_activatemode_function_full (GstPad * pad,
1666     GstPadActivateModeFunction activatemode, gpointer user_data,
1667     GDestroyNotify notify)
1668 {
1669   g_return_if_fail (GST_IS_PAD (pad));
1670
1671   if (pad->activatemodenotify)
1672     pad->activatemodenotify (pad->activatemodedata);
1673   GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1674   pad->activatemodedata = user_data;
1675   pad->activatemodenotify = notify;
1676
1677   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1678       GST_DEBUG_FUNCPTR_NAME (activatemode));
1679 }
1680
1681 /**
1682  * gst_pad_set_chain_function:
1683  * @p: a sink #GstPad.
1684  * @f: the #GstPadChainFunction to set.
1685  *
1686  * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1687  * notify.
1688  */
1689 /**
1690  * gst_pad_set_chain_function_full:
1691  * @pad: a sink #GstPad.
1692  * @chain: the #GstPadChainFunction to set.
1693  * @user_data: user_data passed to @notify
1694  * @notify: notify called when @chain will not be used anymore.
1695  *
1696  * Sets the given chain function for the pad. The chain function is called to
1697  * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1698  */
1699 void
1700 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1701     gpointer user_data, GDestroyNotify notify)
1702 {
1703   g_return_if_fail (GST_IS_PAD (pad));
1704   g_return_if_fail (GST_PAD_IS_SINK (pad));
1705
1706   if (pad->chainnotify)
1707     pad->chainnotify (pad->chaindata);
1708   GST_PAD_CHAINFUNC (pad) = chain;
1709   pad->chaindata = user_data;
1710   pad->chainnotify = notify;
1711
1712   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1713       GST_DEBUG_FUNCPTR_NAME (chain));
1714 }
1715
1716 /**
1717  * gst_pad_set_chain_list_function:
1718  * @p: a sink #GstPad.
1719  * @f: the #GstPadChainListFunction to set.
1720  *
1721  * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1722  * notify.
1723  */
1724 /**
1725  * gst_pad_set_chain_list_function_full:
1726  * @pad: a sink #GstPad.
1727  * @chainlist: the #GstPadChainListFunction to set.
1728  * @user_data: user_data passed to @notify
1729  * @notify: notify called when @chainlist will not be used anymore.
1730  *
1731  * Sets the given chain list function for the pad. The chainlist function is
1732  * called to process a #GstBufferList input buffer list. See
1733  * #GstPadChainListFunction for more details.
1734  */
1735 void
1736 gst_pad_set_chain_list_function_full (GstPad * pad,
1737     GstPadChainListFunction chainlist, gpointer user_data,
1738     GDestroyNotify notify)
1739 {
1740   g_return_if_fail (GST_IS_PAD (pad));
1741   g_return_if_fail (GST_PAD_IS_SINK (pad));
1742
1743   if (pad->chainlistnotify)
1744     pad->chainlistnotify (pad->chainlistdata);
1745   GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1746   pad->chainlistdata = user_data;
1747   pad->chainlistnotify = notify;
1748
1749   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1750       GST_DEBUG_FUNCPTR_NAME (chainlist));
1751 }
1752
1753 /**
1754  * gst_pad_set_getrange_function:
1755  * @p: a source #GstPad.
1756  * @f: the #GstPadGetRangeFunction to set.
1757  *
1758  * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1759  * notify.
1760  */
1761 /**
1762  * gst_pad_set_getrange_function_full:
1763  * @pad: a source #GstPad.
1764  * @get: the #GstPadGetRangeFunction to set.
1765  * @user_data: user_data passed to @notify
1766  * @notify: notify called when @get will not be used anymore.
1767  *
1768  * Sets the given getrange function for the pad. The getrange function is
1769  * called to produce a new #GstBuffer to start the processing pipeline. see
1770  * #GstPadGetRangeFunction for a description of the getrange function.
1771  */
1772 void
1773 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1774     gpointer user_data, GDestroyNotify notify)
1775 {
1776   g_return_if_fail (GST_IS_PAD (pad));
1777   g_return_if_fail (GST_PAD_IS_SRC (pad));
1778
1779   if (pad->getrangenotify)
1780     pad->getrangenotify (pad->getrangedata);
1781   GST_PAD_GETRANGEFUNC (pad) = get;
1782   pad->getrangedata = user_data;
1783   pad->getrangenotify = notify;
1784
1785   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1786       GST_DEBUG_FUNCPTR_NAME (get));
1787 }
1788
1789 /**
1790  * gst_pad_set_event_function:
1791  * @p: a #GstPad of either direction.
1792  * @f: the #GstPadEventFunction to set.
1793  *
1794  * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1795  * notify.
1796  */
1797 /**
1798  * gst_pad_set_event_function_full:
1799  * @pad: a #GstPad of either direction.
1800  * @event: the #GstPadEventFunction to set.
1801  * @user_data: user_data passed to @notify
1802  * @notify: notify called when @event will not be used anymore.
1803  *
1804  * Sets the given event handler for the pad.
1805  */
1806 void
1807 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1808     gpointer user_data, GDestroyNotify notify)
1809 {
1810   g_return_if_fail (GST_IS_PAD (pad));
1811
1812   if (pad->eventnotify)
1813     pad->eventnotify (pad->eventdata);
1814   GST_PAD_EVENTFUNC (pad) = event;
1815   pad->eventdata = user_data;
1816   pad->eventnotify = notify;
1817
1818   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1819       GST_DEBUG_FUNCPTR_NAME (event));
1820 }
1821
1822 /**
1823  * gst_pad_set_query_function:
1824  * @p: a #GstPad of either direction.
1825  * @f: the #GstPadQueryFunction to set.
1826  *
1827  * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1828  * notify.
1829  */
1830 /**
1831  * gst_pad_set_query_function_full:
1832  * @pad: a #GstPad of either direction.
1833  * @query: the #GstPadQueryFunction to set.
1834  * @user_data: user_data passed to @notify
1835  * @notify: notify called when @query will not be used anymore.
1836  *
1837  * Set the given query function for the pad.
1838  */
1839 void
1840 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1841     gpointer user_data, GDestroyNotify notify)
1842 {
1843   g_return_if_fail (GST_IS_PAD (pad));
1844
1845   if (pad->querynotify)
1846     pad->querynotify (pad->querydata);
1847   GST_PAD_QUERYFUNC (pad) = query;
1848   pad->querydata = user_data;
1849   pad->querynotify = notify;
1850
1851   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1852       GST_DEBUG_FUNCPTR_NAME (query));
1853 }
1854
1855 /**
1856  * gst_pad_set_iterate_internal_links_function:
1857  * @p: a #GstPad of either direction.
1858  * @f: the #GstPadIterIntLinkFunction to set.
1859  *
1860  * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1861  * for the user_data and notify.
1862  */
1863 /**
1864  * gst_pad_set_iterate_internal_links_function_full:
1865  * @pad: a #GstPad of either direction.
1866  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1867  * @user_data: user_data passed to @notify
1868  * @notify: notify called when @iterintlink will not be used anymore.
1869  *
1870  * Sets the given internal link iterator function for the pad.
1871  */
1872 void
1873 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1874     GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1875     GDestroyNotify notify)
1876 {
1877   g_return_if_fail (GST_IS_PAD (pad));
1878
1879   if (pad->iterintlinknotify)
1880     pad->iterintlinknotify (pad->iterintlinkdata);
1881   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1882   pad->iterintlinkdata = user_data;
1883   pad->iterintlinknotify = notify;
1884
1885   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1886       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1887 }
1888
1889 /**
1890  * gst_pad_set_link_function:
1891  * @p: a #GstPad.
1892  * @f: the #GstPadLinkFunction to set.
1893  *
1894  * Calls gst_pad_set_link_function_full() with %NULL
1895  * for the user_data and notify.
1896  */
1897 /**
1898  * gst_pad_set_link_function_full:
1899  * @pad: a #GstPad.
1900  * @link: the #GstPadLinkFunction to set.
1901  * @user_data: user_data passed to @notify
1902  * @notify: notify called when @link will not be used anymore.
1903  *
1904  * Sets the given link function for the pad. It will be called when
1905  * the pad is linked with another pad.
1906  *
1907  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1908  * made.
1909  *
1910  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1911  * cannot be made for some reason.
1912  *
1913  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1914  * of the peer sink pad, if present.
1915  */
1916 void
1917 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1918     gpointer user_data, GDestroyNotify notify)
1919 {
1920   g_return_if_fail (GST_IS_PAD (pad));
1921
1922   if (pad->linknotify)
1923     pad->linknotify (pad->linkdata);
1924   GST_PAD_LINKFUNC (pad) = link;
1925   pad->linkdata = user_data;
1926   pad->linknotify = notify;
1927
1928   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1929       GST_DEBUG_FUNCPTR_NAME (link));
1930 }
1931
1932 /**
1933  * gst_pad_set_unlink_function:
1934  * @p: a #GstPad.
1935  * @f: the #GstPadUnlinkFunction to set.
1936  *
1937  * Calls gst_pad_set_unlink_function_full() with %NULL
1938  * for the user_data and notify.
1939  */
1940 /**
1941  * gst_pad_set_unlink_function_full:
1942  * @pad: a #GstPad.
1943  * @unlink: the #GstPadUnlinkFunction to set.
1944  * @user_data: user_data passed to @notify
1945  * @notify: notify called when @unlink will not be used anymore.
1946  *
1947  * Sets the given unlink function for the pad. It will be called
1948  * when the pad is unlinked.
1949  */
1950 void
1951 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1952     gpointer user_data, GDestroyNotify notify)
1953 {
1954   g_return_if_fail (GST_IS_PAD (pad));
1955
1956   if (pad->unlinknotify)
1957     pad->unlinknotify (pad->unlinkdata);
1958   GST_PAD_UNLINKFUNC (pad) = unlink;
1959   pad->unlinkdata = user_data;
1960   pad->unlinknotify = notify;
1961
1962   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1963       GST_DEBUG_FUNCPTR_NAME (unlink));
1964 }
1965
1966 /**
1967  * gst_pad_unlink:
1968  * @srcpad: the source #GstPad to unlink.
1969  * @sinkpad: the sink #GstPad to unlink.
1970  *
1971  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1972  * signal on both pads.
1973  *
1974  * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
1975  * the pads were not linked together.
1976  *
1977  * MT safe.
1978  */
1979 gboolean
1980 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1981 {
1982   gboolean result = FALSE;
1983   GstElement *parent = NULL;
1984
1985   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1986   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1987   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1988   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1989
1990   GST_TRACER_PAD_UNLINK_PRE (srcpad, sinkpad);
1991
1992   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1993       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1994       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1995
1996   /* We need to notify the parent before taking any pad locks as the bin in
1997    * question might be waiting for a lock on the pad while holding its lock
1998    * that our message will try to take. */
1999   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2000     if (GST_IS_ELEMENT (parent)) {
2001       gst_element_post_message (parent,
2002           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2003               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
2004     } else {
2005       gst_object_unref (parent);
2006       parent = NULL;
2007     }
2008   }
2009
2010   GST_OBJECT_LOCK (srcpad);
2011   GST_OBJECT_LOCK (sinkpad);
2012
2013   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2014     goto not_linked_together;
2015
2016   if (GST_PAD_UNLINKFUNC (srcpad)) {
2017     GstObject *tmpparent;
2018
2019     ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2020
2021     GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2022     RELEASE_PARENT (tmpparent);
2023   }
2024 no_src_parent:
2025   if (GST_PAD_UNLINKFUNC (sinkpad)) {
2026     GstObject *tmpparent;
2027
2028     ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2029
2030     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2031     RELEASE_PARENT (tmpparent);
2032   }
2033 no_sink_parent:
2034
2035   /* first clear peers */
2036   GST_PAD_PEER (srcpad) = NULL;
2037   GST_PAD_PEER (sinkpad) = NULL;
2038
2039   GST_OBJECT_UNLOCK (sinkpad);
2040   GST_OBJECT_UNLOCK (srcpad);
2041
2042   /* fire off a signal to each of the pads telling them
2043    * that they've been unlinked */
2044   g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2045   g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2046
2047   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2048       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2049
2050   result = TRUE;
2051
2052 done:
2053   if (parent != NULL) {
2054     gst_element_post_message (parent,
2055         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2056             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2057     gst_object_unref (parent);
2058   }
2059   GST_TRACER_PAD_UNLINK_POST (srcpad, sinkpad, result);
2060   return result;
2061
2062   /* ERRORS */
2063 not_linked_together:
2064   {
2065     /* we do not emit a warning in this case because unlinking cannot
2066      * be made MT safe.*/
2067     GST_OBJECT_UNLOCK (sinkpad);
2068     GST_OBJECT_UNLOCK (srcpad);
2069     goto done;
2070   }
2071 }
2072
2073 /**
2074  * gst_pad_is_linked:
2075  * @pad: pad to check
2076  *
2077  * Checks if a @pad is linked to another pad or not.
2078  *
2079  * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2080  *
2081  * MT safe.
2082  */
2083 gboolean
2084 gst_pad_is_linked (GstPad * pad)
2085 {
2086   gboolean result;
2087
2088   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2089
2090   GST_OBJECT_LOCK (pad);
2091   result = (GST_PAD_PEER (pad) != NULL);
2092   GST_OBJECT_UNLOCK (pad);
2093
2094   return result;
2095 }
2096
2097 /* get the caps from both pads and see if the intersection
2098  * is not empty.
2099  *
2100  * This function should be called with the pad LOCK on both
2101  * pads
2102  */
2103 static gboolean
2104 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2105     GstPadLinkCheck flags)
2106 {
2107   GstCaps *srccaps = NULL;
2108   GstCaps *sinkcaps = NULL;
2109   gboolean compatible = FALSE;
2110
2111   if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2112     return TRUE;
2113
2114   /* Doing the expensive caps checking takes priority over only checking the template caps */
2115   if (flags & GST_PAD_LINK_CHECK_CAPS) {
2116     GST_OBJECT_UNLOCK (sink);
2117     GST_OBJECT_UNLOCK (src);
2118
2119     srccaps = gst_pad_query_caps (src, NULL);
2120     sinkcaps = gst_pad_query_caps (sink, NULL);
2121
2122     GST_OBJECT_LOCK (src);
2123     GST_OBJECT_LOCK (sink);
2124   } else {
2125     /* If one of the two pads doesn't have a template, consider the intersection
2126      * as valid.*/
2127     if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2128             || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2129       compatible = TRUE;
2130       goto done;
2131     }
2132     srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2133     sinkcaps =
2134         gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2135   }
2136
2137   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2138       srccaps);
2139   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2140       sinkcaps);
2141
2142   /* if we have caps on both pads we can check the intersection. If one
2143    * of the caps is %NULL, we return %TRUE. */
2144   if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2145     if (srccaps)
2146       gst_caps_unref (srccaps);
2147     if (sinkcaps)
2148       gst_caps_unref (sinkcaps);
2149     goto done;
2150   }
2151
2152   compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2153   gst_caps_unref (srccaps);
2154   gst_caps_unref (sinkcaps);
2155
2156 done:
2157   GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2158       (compatible ? "" : "not "));
2159
2160   return compatible;
2161 }
2162
2163 /* check if the grandparents of both pads are the same.
2164  * This check is required so that we don't try to link
2165  * pads from elements in different bins without ghostpads.
2166  *
2167  * The LOCK should be held on both pads
2168  */
2169 static gboolean
2170 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2171 {
2172   GstObject *psrc, *psink;
2173
2174   psrc = GST_OBJECT_PARENT (src);
2175   psink = GST_OBJECT_PARENT (sink);
2176
2177   /* if one of the pads has no parent, we allow the link */
2178   if (G_UNLIKELY (psrc == NULL || psink == NULL))
2179     goto no_parent;
2180
2181   /* only care about parents that are elements */
2182   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2183     goto no_element_parent;
2184
2185   /* if the parents are the same, we have a loop */
2186   if (G_UNLIKELY (psrc == psink))
2187     goto same_parents;
2188
2189   /* if they both have a parent, we check the grandparents. We can not lock
2190    * the parent because we hold on the child (pad) and the locking order is
2191    * parent >> child. */
2192   psrc = GST_OBJECT_PARENT (psrc);
2193   psink = GST_OBJECT_PARENT (psink);
2194
2195   /* if they have grandparents but they are not the same */
2196   if (G_UNLIKELY (psrc != psink))
2197     goto wrong_grandparents;
2198
2199   return TRUE;
2200
2201   /* ERRORS */
2202 no_parent:
2203   {
2204     GST_CAT_DEBUG (GST_CAT_CAPS,
2205         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2206         GST_PTR_FORMAT, psrc, psink);
2207     return TRUE;
2208   }
2209 no_element_parent:
2210   {
2211     GST_CAT_DEBUG (GST_CAT_CAPS,
2212         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2213         GST_PTR_FORMAT, psrc, psink);
2214     return TRUE;
2215   }
2216 same_parents:
2217   {
2218     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2219         psrc);
2220     return FALSE;
2221   }
2222 wrong_grandparents:
2223   {
2224     GST_CAT_DEBUG (GST_CAT_CAPS,
2225         "pads have different grandparents %" GST_PTR_FORMAT " and %"
2226         GST_PTR_FORMAT, psrc, psink);
2227     return FALSE;
2228   }
2229 }
2230
2231 /* FIXME leftover from an attempt at refactoring... */
2232 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2233  * the two pads will be locked in the srcpad, sinkpad order. */
2234 static GstPadLinkReturn
2235 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2236 {
2237   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2238       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2239
2240   GST_OBJECT_LOCK (srcpad);
2241
2242   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2243     goto src_was_linked;
2244
2245   GST_OBJECT_LOCK (sinkpad);
2246
2247   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2248     goto sink_was_linked;
2249
2250   /* check hierarchy, pads can only be linked if the grandparents
2251    * are the same. */
2252   if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2253       && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2254     goto wrong_hierarchy;
2255
2256   /* check pad caps for non-empty intersection */
2257   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2258     goto no_format;
2259
2260   /* FIXME check pad scheduling for non-empty intersection */
2261
2262   return GST_PAD_LINK_OK;
2263
2264 src_was_linked:
2265   {
2266     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2267         GST_DEBUG_PAD_NAME (srcpad),
2268         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2269     /* we do not emit a warning in this case because unlinking cannot
2270      * be made MT safe.*/
2271     GST_OBJECT_UNLOCK (srcpad);
2272     return GST_PAD_LINK_WAS_LINKED;
2273   }
2274 sink_was_linked:
2275   {
2276     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2277         GST_DEBUG_PAD_NAME (sinkpad),
2278         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2279     /* we do not emit a warning in this case because unlinking cannot
2280      * be made MT safe.*/
2281     GST_OBJECT_UNLOCK (sinkpad);
2282     GST_OBJECT_UNLOCK (srcpad);
2283     return GST_PAD_LINK_WAS_LINKED;
2284   }
2285 wrong_hierarchy:
2286   {
2287     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2288     GST_OBJECT_UNLOCK (sinkpad);
2289     GST_OBJECT_UNLOCK (srcpad);
2290     return GST_PAD_LINK_WRONG_HIERARCHY;
2291   }
2292 no_format:
2293   {
2294     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2295     GST_OBJECT_UNLOCK (sinkpad);
2296     GST_OBJECT_UNLOCK (srcpad);
2297     return GST_PAD_LINK_NOFORMAT;
2298   }
2299 }
2300
2301 /**
2302  * gst_pad_can_link:
2303  * @srcpad: the source #GstPad.
2304  * @sinkpad: the sink #GstPad.
2305  *
2306  * Checks if the source pad and the sink pad are compatible so they can be
2307  * linked.
2308  *
2309  * Returns: %TRUE if the pads can be linked.
2310  */
2311 gboolean
2312 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2313 {
2314   GstPadLinkReturn result;
2315
2316   /* generic checks */
2317   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2318   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2319
2320   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2321       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2322
2323   /* gst_pad_link_prepare does everything for us, we only release the locks
2324    * on the pads that it gets us. If this function returns !OK the locks are not
2325    * taken anymore. */
2326   result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2327   if (result != GST_PAD_LINK_OK)
2328     goto done;
2329
2330   GST_OBJECT_UNLOCK (srcpad);
2331   GST_OBJECT_UNLOCK (sinkpad);
2332
2333 done:
2334   return result == GST_PAD_LINK_OK;
2335 }
2336
2337 /**
2338  * gst_pad_link_full:
2339  * @srcpad: the source #GstPad to link.
2340  * @sinkpad: the sink #GstPad to link.
2341  * @flags: the checks to validate when linking
2342  *
2343  * Links the source pad and the sink pad.
2344  *
2345  * This variant of #gst_pad_link provides a more granular control on the
2346  * checks being done when linking. While providing some considerable speedups
2347  * the caller of this method must be aware that wrong usage of those flags
2348  * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2349  * for more information.
2350  *
2351  * MT Safe.
2352  *
2353  * Returns: A result code indicating if the connection worked or
2354  *          what went wrong.
2355  */
2356 GstPadLinkReturn
2357 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2358 {
2359   GstPadLinkReturn result;
2360   GstElement *parent;
2361   GstPadLinkFunction srcfunc, sinkfunc;
2362
2363   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2364   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2365   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2366   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2367       GST_PAD_LINK_WRONG_DIRECTION);
2368
2369   /* Notify the parent early. See gst_pad_unlink for details. */
2370   if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2371     if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2372       gst_element_post_message (parent,
2373           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2374               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2375     } else {
2376       gst_object_unref (parent);
2377       parent = NULL;
2378     }
2379   }
2380
2381   /* prepare will also lock the two pads */
2382   result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2383
2384   if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2385     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2386         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2387         gst_pad_link_get_name (result));
2388     goto done;
2389   }
2390
2391   /* must set peers before calling the link function */
2392   GST_PAD_PEER (srcpad) = sinkpad;
2393   GST_PAD_PEER (sinkpad) = srcpad;
2394
2395   /* check events, when something is different, mark pending */
2396   schedule_events (srcpad, sinkpad);
2397
2398   /* get the link functions */
2399   srcfunc = GST_PAD_LINKFUNC (srcpad);
2400   sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2401
2402   if (G_UNLIKELY (srcfunc || sinkfunc)) {
2403     /* custom link functions, execute them */
2404     GST_OBJECT_UNLOCK (sinkpad);
2405     GST_OBJECT_UNLOCK (srcpad);
2406
2407     if (srcfunc) {
2408       GstObject *tmpparent;
2409
2410       ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2411       /* this one will call the peer link function */
2412       result = srcfunc (srcpad, tmpparent, sinkpad);
2413       RELEASE_PARENT (tmpparent);
2414     } else if (sinkfunc) {
2415       GstObject *tmpparent;
2416
2417       ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2418       /* if no source link function, we need to call the sink link
2419        * function ourselves. */
2420       result = sinkfunc (sinkpad, tmpparent, srcpad);
2421       RELEASE_PARENT (tmpparent);
2422     }
2423   no_parent:
2424
2425     GST_OBJECT_LOCK (srcpad);
2426     GST_OBJECT_LOCK (sinkpad);
2427
2428     /* we released the lock, check if the same pads are linked still */
2429     if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2430       goto concurrent_link;
2431
2432     if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2433       goto link_failed;
2434   }
2435   GST_OBJECT_UNLOCK (sinkpad);
2436   GST_OBJECT_UNLOCK (srcpad);
2437
2438   /* fire off a signal to each of the pads telling them
2439    * that they've been linked */
2440   g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2441   g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2442
2443   GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2444       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2445
2446   gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2447
2448 done:
2449   if (G_LIKELY (parent)) {
2450     gst_element_post_message (parent,
2451         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2452             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2453     gst_object_unref (parent);
2454   }
2455
2456   return result;
2457
2458   /* ERRORS */
2459 concurrent_link:
2460   {
2461     GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2462         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2463     GST_OBJECT_UNLOCK (sinkpad);
2464     GST_OBJECT_UNLOCK (srcpad);
2465
2466     /* The other link operation succeeded first */
2467     result = GST_PAD_LINK_WAS_LINKED;
2468     goto done;
2469   }
2470 link_failed:
2471   {
2472     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2473         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2474         gst_pad_link_get_name (result));
2475
2476     GST_PAD_PEER (srcpad) = NULL;
2477     GST_PAD_PEER (sinkpad) = NULL;
2478
2479     GST_OBJECT_UNLOCK (sinkpad);
2480     GST_OBJECT_UNLOCK (srcpad);
2481
2482     goto done;
2483   }
2484 }
2485
2486 /**
2487  * gst_pad_link:
2488  * @srcpad: the source #GstPad to link.
2489  * @sinkpad: the sink #GstPad to link.
2490  *
2491  * Links the source pad and the sink pad.
2492  *
2493  * Returns: A result code indicating if the connection worked or
2494  *          what went wrong.
2495  *
2496  * MT Safe.
2497  */
2498 GstPadLinkReturn
2499 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2500 {
2501   GstPadLinkReturn ret;
2502
2503   GST_TRACER_PAD_LINK_PRE (srcpad, sinkpad);
2504   ret = gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2505   GST_TRACER_PAD_LINK_POST (srcpad, sinkpad, ret);
2506
2507   return ret;
2508 }
2509
2510 static void
2511 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2512 {
2513   GstPadTemplate **template_p;
2514
2515   /* this function would need checks if it weren't static */
2516
2517   GST_OBJECT_LOCK (pad);
2518   template_p = &pad->padtemplate;
2519   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2520   GST_OBJECT_UNLOCK (pad);
2521
2522   if (templ)
2523     gst_pad_template_pad_created (templ, pad);
2524 }
2525
2526 /**
2527  * gst_pad_get_pad_template:
2528  * @pad: a #GstPad.
2529  *
2530  * Gets the template for @pad.
2531  *
2532  * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2533  *     this pad was instantiated, or %NULL if this pad has no
2534  *     template. Unref after usage.
2535  */
2536 GstPadTemplate *
2537 gst_pad_get_pad_template (GstPad * pad)
2538 {
2539   GstPadTemplate *templ;
2540
2541   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2542
2543   templ = GST_PAD_PAD_TEMPLATE (pad);
2544
2545   return (templ ? gst_object_ref (templ) : NULL);
2546 }
2547
2548 /**
2549  * gst_pad_has_current_caps:
2550  * @pad: a  #GstPad to check
2551  *
2552  * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2553  *
2554  * Returns: %TRUE when @pad has caps associated with it.
2555  */
2556 gboolean
2557 gst_pad_has_current_caps (GstPad * pad)
2558 {
2559   gboolean result;
2560   GstCaps *caps;
2561
2562   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2563
2564   GST_OBJECT_LOCK (pad);
2565   caps = get_pad_caps (pad);
2566   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2567       "check current pad caps %" GST_PTR_FORMAT, caps);
2568   result = (caps != NULL);
2569   GST_OBJECT_UNLOCK (pad);
2570
2571   return result;
2572 }
2573
2574 /**
2575  * gst_pad_get_current_caps:
2576  * @pad: a  #GstPad to get the current capabilities of.
2577  *
2578  * Gets the capabilities currently configured on @pad with the last
2579  * #GST_EVENT_CAPS event.
2580  *
2581  * Returns: the current caps of the pad with incremented ref-count.
2582  */
2583 GstCaps *
2584 gst_pad_get_current_caps (GstPad * pad)
2585 {
2586   GstCaps *result;
2587
2588   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2589
2590   GST_OBJECT_LOCK (pad);
2591   if ((result = get_pad_caps (pad)))
2592     gst_caps_ref (result);
2593   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2594       "get current pad caps %" GST_PTR_FORMAT, result);
2595   GST_OBJECT_UNLOCK (pad);
2596
2597   return result;
2598 }
2599
2600 /**
2601  * gst_pad_get_pad_template_caps:
2602  * @pad: a #GstPad to get the template capabilities from.
2603  *
2604  * Gets the capabilities for @pad's template.
2605  *
2606  * Returns: (transfer full): the #GstCaps of this pad template.
2607  * Unref after usage.
2608  */
2609 GstCaps *
2610 gst_pad_get_pad_template_caps (GstPad * pad)
2611 {
2612   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2613
2614   if (GST_PAD_PAD_TEMPLATE (pad))
2615     return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2616
2617   return gst_caps_ref (GST_CAPS_ANY);
2618 }
2619
2620 /**
2621  * gst_pad_get_peer:
2622  * @pad: a #GstPad to get the peer of.
2623  *
2624  * Gets the peer of @pad. This function refs the peer pad so
2625  * you need to unref it after use.
2626  *
2627  * Returns: (transfer full): the peer #GstPad. Unref after usage.
2628  *
2629  * MT safe.
2630  */
2631 GstPad *
2632 gst_pad_get_peer (GstPad * pad)
2633 {
2634   GstPad *result;
2635
2636   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2637
2638   GST_OBJECT_LOCK (pad);
2639   result = GST_PAD_PEER (pad);
2640   if (result)
2641     gst_object_ref (result);
2642   GST_OBJECT_UNLOCK (pad);
2643
2644   return result;
2645 }
2646
2647 /**
2648  * gst_pad_get_allowed_caps:
2649  * @pad: a #GstPad.
2650  *
2651  * Gets the capabilities of the allowed media types that can flow through
2652  * @pad and its peer.
2653  *
2654  * The allowed capabilities is calculated as the intersection of the results of
2655  * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2656  * on the resulting caps.
2657  *
2658  * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2659  *     pad link. Unref the caps when you no longer need it. This
2660  *     function returns %NULL when @pad has no peer.
2661  *
2662  * MT safe.
2663  */
2664 GstCaps *
2665 gst_pad_get_allowed_caps (GstPad * pad)
2666 {
2667   GstCaps *mycaps;
2668   GstCaps *caps = NULL;
2669   GstQuery *query;
2670
2671   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2672
2673   GST_OBJECT_LOCK (pad);
2674   if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2675     goto no_peer;
2676   GST_OBJECT_UNLOCK (pad);
2677
2678   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2679
2680   mycaps = gst_pad_query_caps (pad, NULL);
2681
2682   /* Query peer caps */
2683   query = gst_query_new_caps (mycaps);
2684   gst_pad_peer_query (pad, query);
2685   gst_query_parse_caps_result (query, &caps);
2686   gst_caps_ref (caps);
2687   gst_query_unref (query);
2688
2689   gst_caps_unref (mycaps);
2690
2691   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2692       caps);
2693
2694   return caps;
2695
2696 no_peer:
2697   {
2698     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2699     GST_OBJECT_UNLOCK (pad);
2700
2701     return NULL;
2702   }
2703 }
2704
2705 /**
2706  * gst_pad_iterate_internal_links_default:
2707  * @pad: the #GstPad to get the internal links of.
2708  * @parent: (allow-none): the parent of @pad or %NULL
2709  *
2710  * Iterate the list of pads to which the given pad is linked to inside of
2711  * the parent element.
2712  * This is the default handler, and thus returns an iterator of all of the
2713  * pads inside the parent element with opposite direction.
2714  *
2715  * The caller must free this iterator after use with gst_iterator_free().
2716  *
2717  * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2718  * has no parent. Unref each returned pad with gst_object_unref().
2719  */
2720 GstIterator *
2721 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2722 {
2723   GstIterator *res;
2724   GList **padlist;
2725   guint32 *cookie;
2726   GMutex *lock;
2727   gpointer owner;
2728   GstElement *eparent;
2729
2730   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2731
2732   if (parent != NULL && GST_IS_ELEMENT (parent)) {
2733     eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2734   } else {
2735     GST_OBJECT_LOCK (pad);
2736     eparent = GST_PAD_PARENT (pad);
2737     if (!eparent || !GST_IS_ELEMENT (eparent))
2738       goto no_parent;
2739
2740     gst_object_ref (eparent);
2741     GST_OBJECT_UNLOCK (pad);
2742   }
2743
2744   if (pad->direction == GST_PAD_SRC)
2745     padlist = &eparent->sinkpads;
2746   else
2747     padlist = &eparent->srcpads;
2748
2749   GST_DEBUG_OBJECT (pad, "Making iterator");
2750
2751   cookie = &eparent->pads_cookie;
2752   owner = eparent;
2753   lock = GST_OBJECT_GET_LOCK (eparent);
2754
2755   res = gst_iterator_new_list (GST_TYPE_PAD,
2756       lock, cookie, padlist, (GObject *) owner, NULL);
2757
2758   gst_object_unref (owner);
2759
2760   return res;
2761
2762   /* ERRORS */
2763 no_parent:
2764   {
2765     GST_OBJECT_UNLOCK (pad);
2766     GST_DEBUG_OBJECT (pad, "no parent element");
2767     return NULL;
2768   }
2769 }
2770
2771 /**
2772  * gst_pad_iterate_internal_links:
2773  * @pad: the GstPad to get the internal links of.
2774  *
2775  * Gets an iterator for the pads to which the given pad is linked to inside
2776  * of the parent element.
2777  *
2778  * Each #GstPad element yielded by the iterator will have its refcount increased,
2779  * so unref after use.
2780  *
2781  * Free-function: gst_iterator_free
2782  *
2783  * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2784  *     or %NULL when the pad does not have an iterator function
2785  *     configured. Use gst_iterator_free() after usage.
2786  */
2787 GstIterator *
2788 gst_pad_iterate_internal_links (GstPad * pad)
2789 {
2790   GstIterator *res = NULL;
2791   GstObject *parent;
2792
2793   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2794
2795   GST_OBJECT_LOCK (pad);
2796   ACQUIRE_PARENT (pad, parent, no_parent);
2797   GST_OBJECT_UNLOCK (pad);
2798
2799   if (GST_PAD_ITERINTLINKFUNC (pad))
2800     res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2801
2802   RELEASE_PARENT (parent);
2803
2804   return res;
2805
2806   /* ERRORS */
2807 no_parent:
2808   {
2809     GST_DEBUG_OBJECT (pad, "no parent");
2810     GST_OBJECT_UNLOCK (pad);
2811     return NULL;
2812   }
2813 }
2814
2815 /**
2816  * gst_pad_forward:
2817  * @pad: a #GstPad
2818  * @forward: (scope call): a #GstPadForwardFunction
2819  * @user_data: user data passed to @forward
2820  *
2821  * Calls @forward for all internally linked pads of @pad. This function deals with
2822  * dynamically changing internal pads and will make sure that the @forward
2823  * function is only called once for each pad.
2824  *
2825  * When @forward returns %TRUE, no further pads will be processed.
2826  *
2827  * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2828  */
2829 gboolean
2830 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2831     gpointer user_data)
2832 {
2833   gboolean result = FALSE;
2834   GstIterator *iter;
2835   gboolean done = FALSE;
2836   GValue item = { 0, };
2837   GList *pushed_pads = NULL;
2838
2839   iter = gst_pad_iterate_internal_links (pad);
2840
2841   if (!iter)
2842     goto no_iter;
2843
2844   while (!done) {
2845     switch (gst_iterator_next (iter, &item)) {
2846       case GST_ITERATOR_OK:
2847       {
2848         GstPad *intpad;
2849
2850         intpad = g_value_get_object (&item);
2851
2852         /* if already pushed, skip. FIXME, find something faster to tag pads */
2853         if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2854           g_value_reset (&item);
2855           break;
2856         }
2857
2858         GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2859             GST_DEBUG_PAD_NAME (intpad));
2860         done = result = forward (intpad, user_data);
2861
2862         pushed_pads = g_list_prepend (pushed_pads, intpad);
2863
2864         g_value_reset (&item);
2865         break;
2866       }
2867       case GST_ITERATOR_RESYNC:
2868         /* We don't reset the result here because we don't push the event
2869          * again on pads that got the event already and because we need
2870          * to consider the result of the previous pushes */
2871         gst_iterator_resync (iter);
2872         break;
2873       case GST_ITERATOR_ERROR:
2874         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2875         done = TRUE;
2876         break;
2877       case GST_ITERATOR_DONE:
2878         done = TRUE;
2879         break;
2880     }
2881   }
2882   g_value_unset (&item);
2883   gst_iterator_free (iter);
2884
2885   g_list_free (pushed_pads);
2886
2887 no_iter:
2888   return result;
2889 }
2890
2891 typedef struct
2892 {
2893   GstEvent *event;
2894   gboolean result;
2895   gboolean dispatched;
2896 } EventData;
2897
2898 static gboolean
2899 event_forward_func (GstPad * pad, EventData * data)
2900 {
2901   /* for each pad we send to, we should ref the event; it's up
2902    * to downstream to unref again when handled. */
2903   GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2904       data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2905
2906   data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2907
2908   data->dispatched = TRUE;
2909
2910   /* don't stop */
2911   return FALSE;
2912 }
2913
2914 /**
2915  * gst_pad_event_default:
2916  * @pad: a #GstPad to call the default event handler on.
2917  * @parent: (allow-none): the parent of @pad or %NULL
2918  * @event: (transfer full): the #GstEvent to handle.
2919  *
2920  * Invokes the default event handler for the given pad.
2921  *
2922  * The EOS event will pause the task associated with @pad before it is forwarded
2923  * to all internally linked pads,
2924  *
2925  * The event is sent to all pads internally linked to @pad. This function
2926  * takes ownership of @event.
2927  *
2928  * Returns: %TRUE if the event was sent successfully.
2929  */
2930 gboolean
2931 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2932 {
2933   gboolean result, forward = TRUE;
2934
2935   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2936   g_return_val_if_fail (event != NULL, FALSE);
2937
2938   GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2939       event);
2940
2941   switch (GST_EVENT_TYPE (event)) {
2942     case GST_EVENT_CAPS:
2943       forward = GST_PAD_IS_PROXY_CAPS (pad);
2944       result = TRUE;
2945       break;
2946     default:
2947       break;
2948   }
2949
2950   if (forward) {
2951     EventData data;
2952
2953     data.event = event;
2954     data.dispatched = FALSE;
2955     data.result = FALSE;
2956
2957     gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2958
2959     /* for sinkpads without a parent element or without internal links, nothing
2960      * will be dispatched but we still want to return TRUE. */
2961     if (data.dispatched)
2962       result = data.result;
2963     else
2964       result = TRUE;
2965   }
2966
2967   gst_event_unref (event);
2968
2969   return result;
2970 }
2971
2972 /* Default accept caps implementation just checks against
2973  * the allowed caps for the pad */
2974 static gboolean
2975 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2976 {
2977   /* get the caps and see if it intersects to something not empty */
2978   GstCaps *caps, *allowed;
2979   gboolean result;
2980
2981   GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2982
2983   /* first forward the query to internally linked pads when we are dealing with
2984    * a PROXY CAPS */
2985   if (GST_PAD_IS_PROXY_CAPS (pad)) {
2986     result = gst_pad_proxy_query_accept_caps (pad, query);
2987     goto done;
2988   }
2989
2990   GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2991       "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2992
2993   gst_query_parse_accept_caps (query, &caps);
2994   if (GST_PAD_IS_ACCEPT_TEMPLATE (pad))
2995     allowed = gst_pad_get_pad_template_caps (pad);
2996   else
2997     allowed = gst_pad_query_caps (pad, caps);
2998
2999   if (allowed) {
3000     if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
3001       GST_DEBUG_OBJECT (pad,
3002           "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
3003           allowed, caps);
3004       result = gst_caps_can_intersect (caps, allowed);
3005     } else {
3006       GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
3007           GST_PTR_FORMAT, allowed, caps);
3008       result = gst_caps_is_subset (caps, allowed);
3009     }
3010     gst_caps_unref (allowed);
3011   } else {
3012     GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
3013     result = FALSE;
3014   }
3015   gst_query_set_accept_caps_result (query, result);
3016
3017 done:
3018   return TRUE;
3019 }
3020
3021 /* Default caps implementation */
3022 static gboolean
3023 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3024 {
3025   GstCaps *result = NULL, *filter;
3026   GstPadTemplate *templ;
3027   gboolean fixed_caps;
3028
3029   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3030       query);
3031
3032   /* first try to proxy if we must */
3033   if (GST_PAD_IS_PROXY_CAPS (pad)) {
3034     if ((gst_pad_proxy_query_caps (pad, query))) {
3035       goto done;
3036     }
3037   }
3038
3039   gst_query_parse_caps (query, &filter);
3040
3041   /* no proxy or it failed, do default handling */
3042   fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3043
3044   GST_OBJECT_LOCK (pad);
3045   if (fixed_caps) {
3046     /* fixed caps, try the negotiated caps first */
3047     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3048     if ((result = get_pad_caps (pad)))
3049       goto filter_done_unlock;
3050   }
3051
3052   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3053     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3054     if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3055       goto filter_done_unlock;
3056   }
3057
3058   if (!fixed_caps) {
3059     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3060         "non-fixed pad caps: trying pad caps");
3061     /* non fixed caps, try the negotiated caps */
3062     if ((result = get_pad_caps (pad)))
3063       goto filter_done_unlock;
3064   }
3065
3066   /* this almost never happens */
3067   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3068   result = GST_CAPS_ANY;
3069
3070 filter_done_unlock:
3071   GST_OBJECT_UNLOCK (pad);
3072
3073   /* run the filter on the result */
3074   if (filter) {
3075     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3076         "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3077         GST_PTR_FORMAT, result, result, filter, filter);
3078     result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3079     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3080         result, result);
3081   } else {
3082     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3083         "using caps %p %" GST_PTR_FORMAT, result, result);
3084     result = gst_caps_ref (result);
3085   }
3086   gst_query_set_caps_result (query, result);
3087   gst_caps_unref (result);
3088
3089 done:
3090   return TRUE;
3091 }
3092
3093 /* Default latency implementation */
3094 typedef struct
3095 {
3096   gboolean live;
3097   GstClockTime min, max;
3098 } LatencyFoldData;
3099
3100 static gboolean
3101 query_latency_default_fold (const GValue * item, GValue * ret,
3102     gpointer user_data)
3103 {
3104   GstPad *pad = g_value_get_object (item), *peer;
3105   LatencyFoldData *fold_data = user_data;
3106   GstQuery *query;
3107   gboolean res = FALSE;
3108
3109   query = gst_query_new_latency ();
3110
3111   peer = gst_pad_get_peer (pad);
3112   if (peer) {
3113     res = gst_pad_peer_query (pad, query);
3114   } else {
3115     GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3116   }
3117
3118   if (res) {
3119     gboolean live;
3120     GstClockTime min, max;
3121
3122     gst_query_parse_latency (query, &live, &min, &max);
3123
3124     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3125         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3126
3127     if (live) {
3128       if (min > fold_data->min)
3129         fold_data->min = min;
3130
3131       if (fold_data->max == GST_CLOCK_TIME_NONE)
3132         fold_data->max = max;
3133       else if (max < fold_data->max)
3134         fold_data->max = max;
3135
3136       fold_data->live = TRUE;
3137     }
3138   } else if (peer) {
3139     GST_DEBUG_OBJECT (pad, "latency query failed");
3140     g_value_set_boolean (ret, FALSE);
3141   }
3142
3143   gst_query_unref (query);
3144   if (peer)
3145     gst_object_unref (peer);
3146
3147   return TRUE;
3148 }
3149
3150 static gboolean
3151 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3152 {
3153   GstIterator *it;
3154   GstIteratorResult res;
3155   GValue ret = G_VALUE_INIT;
3156   gboolean query_ret;
3157   LatencyFoldData fold_data;
3158
3159   it = gst_pad_iterate_internal_links (pad);
3160   if (!it) {
3161     GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3162     return FALSE;
3163   }
3164
3165   g_value_init (&ret, G_TYPE_BOOLEAN);
3166
3167 retry:
3168   fold_data.live = FALSE;
3169   fold_data.min = 0;
3170   fold_data.max = GST_CLOCK_TIME_NONE;
3171
3172   g_value_set_boolean (&ret, TRUE);
3173   res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3174   switch (res) {
3175     case GST_ITERATOR_OK:
3176       g_assert_not_reached ();
3177       break;
3178     case GST_ITERATOR_DONE:
3179       break;
3180     case GST_ITERATOR_ERROR:
3181       g_value_set_boolean (&ret, FALSE);
3182       break;
3183     case GST_ITERATOR_RESYNC:
3184       gst_iterator_resync (it);
3185       goto retry;
3186     default:
3187       g_assert_not_reached ();
3188       break;
3189   }
3190   gst_iterator_free (it);
3191
3192   query_ret = g_value_get_boolean (&ret);
3193   if (query_ret) {
3194     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3195         " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3196         fold_data.min, fold_data.max);
3197
3198     if (fold_data.min > fold_data.max) {
3199       GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3200     }
3201
3202     gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3203   } else {
3204     GST_LOG_OBJECT (pad, "latency query failed");
3205   }
3206
3207   return query_ret;
3208 }
3209
3210 typedef struct
3211 {
3212   GstQuery *query;
3213   gboolean result;
3214   gboolean dispatched;
3215 } QueryData;
3216
3217 static gboolean
3218 query_forward_func (GstPad * pad, QueryData * data)
3219 {
3220   GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3221       data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3222
3223   data->result |= gst_pad_peer_query (pad, data->query);
3224
3225   data->dispatched = TRUE;
3226
3227   /* stop on first successful reply */
3228   return data->result;
3229 }
3230
3231 /**
3232  * gst_pad_query_default:
3233  * @pad: a #GstPad to call the default query handler on.
3234  * @parent: (allow-none): the parent of @pad or %NULL
3235  * @query: (transfer none): the #GstQuery to handle.
3236  *
3237  * Invokes the default query handler for the given pad.
3238  * The query is sent to all pads internally linked to @pad. Note that
3239  * if there are many possible sink pads that are internally linked to
3240  * @pad, only one will be sent the query.
3241  * Multi-sinkpad elements should implement custom query handlers.
3242  *
3243  * Returns: %TRUE if the query was performed successfully.
3244  */
3245 gboolean
3246 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3247 {
3248   gboolean forward, ret = FALSE;
3249
3250   switch (GST_QUERY_TYPE (query)) {
3251     case GST_QUERY_SCHEDULING:
3252       forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3253       break;
3254     case GST_QUERY_ALLOCATION:
3255       forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3256       break;
3257     case GST_QUERY_ACCEPT_CAPS:
3258       ret = gst_pad_query_accept_caps_default (pad, query);
3259       forward = FALSE;
3260       break;
3261     case GST_QUERY_CAPS:
3262       ret = gst_pad_query_caps_default (pad, query);
3263       forward = FALSE;
3264       break;
3265     case GST_QUERY_LATENCY:
3266       ret = gst_pad_query_latency_default (pad, query);
3267       forward = FALSE;
3268       break;
3269     case GST_QUERY_POSITION:
3270     case GST_QUERY_SEEKING:
3271     case GST_QUERY_FORMATS:
3272     case GST_QUERY_JITTER:
3273     case GST_QUERY_RATE:
3274     case GST_QUERY_CONVERT:
3275     default:
3276       forward = TRUE;
3277       break;
3278   }
3279
3280   GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3281       query, GST_QUERY_TYPE_NAME (query));
3282
3283   if (forward) {
3284     QueryData data;
3285
3286     data.query = query;
3287     data.dispatched = FALSE;
3288     data.result = FALSE;
3289
3290     gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3291
3292     if (data.dispatched) {
3293       ret = data.result;
3294     } else {
3295       /* nothing dispatched, assume drained */
3296       if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3297         ret = TRUE;
3298       else
3299         ret = FALSE;
3300     }
3301   }
3302   return ret;
3303 }
3304
3305 static void
3306 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3307 {
3308   GstPad *pad = data->pad;
3309   GstPadProbeInfo *info = data->info;
3310   GstPadProbeType type, flags;
3311   GstPadProbeCallback callback;
3312   GstPadProbeReturn ret;
3313   gpointer original_data;
3314
3315   /* if we have called this callback, do nothing */
3316   if (PROBE_COOKIE (hook) == data->cookie) {
3317     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3318         "hook %lu, cookie %u already called", hook->hook_id,
3319         PROBE_COOKIE (hook));
3320     return;
3321   }
3322
3323   PROBE_COOKIE (hook) = data->cookie;
3324
3325   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3326   type = info->type;
3327   original_data = info->data;
3328
3329   /* one of the data types for non-idle probes */
3330   if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3331       && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3332     goto no_match;
3333   /* one of the scheduling types */
3334   if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3335     goto no_match;
3336   /* one of the blocking types must match */
3337   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3338       (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3339     goto no_match;
3340   if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3341       (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3342     goto no_match;
3343   /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3344   if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3345       (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3346     goto no_match;
3347
3348   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3349       "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3350       PROBE_COOKIE (hook), flags);
3351
3352   data->marshalled = TRUE;
3353
3354   callback = (GstPadProbeCallback) hook->func;
3355   if (callback == NULL)
3356     return;
3357
3358   info->id = hook->hook_id;
3359
3360   GST_OBJECT_UNLOCK (pad);
3361
3362   ret = callback (pad, info, hook->data);
3363
3364   GST_OBJECT_LOCK (pad);
3365
3366   if (original_data != NULL && info->data == NULL) {
3367     GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3368     info->type = GST_PAD_PROBE_TYPE_INVALID;
3369     data->dropped = TRUE;
3370   }
3371
3372   switch (ret) {
3373     case GST_PAD_PROBE_REMOVE:
3374       /* remove the probe */
3375       GST_DEBUG_OBJECT (pad, "asked to remove hook");
3376       cleanup_hook (pad, hook);
3377       break;
3378     case GST_PAD_PROBE_DROP:
3379       /* need to drop the data, make sure other probes don't get called
3380        * anymore */
3381       GST_DEBUG_OBJECT (pad, "asked to drop item");
3382       info->type = GST_PAD_PROBE_TYPE_INVALID;
3383       data->dropped = TRUE;
3384       break;
3385     case GST_PAD_PROBE_HANDLED:
3386       GST_DEBUG_OBJECT (pad, "probe handled data");
3387       data->handled = TRUE;
3388       break;
3389     case GST_PAD_PROBE_PASS:
3390       /* inform the pad block to let things pass */
3391       GST_DEBUG_OBJECT (pad, "asked to pass item");
3392       data->pass = TRUE;
3393       break;
3394     case GST_PAD_PROBE_OK:
3395       GST_DEBUG_OBJECT (pad, "probe returned OK");
3396       break;
3397     default:
3398       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3399       break;
3400   }
3401   return;
3402
3403 no_match:
3404   {
3405     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3406         "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3407         hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3408     return;
3409   }
3410 }
3411
3412 /* a probe that does not take or return any data */
3413 #define PROBE_NO_DATA(pad,mask,label,defaultval)                \
3414   G_STMT_START {                                                \
3415     if (G_UNLIKELY (pad->num_probes)) {                         \
3416       GstFlowReturn pval = defaultval;                          \
3417       /* pass NULL as the data item */                          \
3418       GstPadProbeInfo info = { mask, 0, NULL, 0, 0 };           \
3419       info.ABI.abi.flow_ret = defaultval;                       \
3420       ret = do_probe_callbacks (pad, &info, defaultval);        \
3421       if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK))       \
3422         goto label;                                             \
3423     }                                                           \
3424   } G_STMT_END
3425
3426 #define PROBE_FULL(pad,mask,data,offs,size,label,handleable,handle_label) \
3427   G_STMT_START {                                                        \
3428     if (G_UNLIKELY (pad->num_probes)) {                                 \
3429       /* pass the data item */                                          \
3430       GstPadProbeInfo info = { mask, 0, data, offs, size };             \
3431       info.ABI.abi.flow_ret = GST_FLOW_OK;                              \
3432       ret = do_probe_callbacks (pad, &info, GST_FLOW_OK);               \
3433       /* store the possibly updated data item */                        \
3434       data = GST_PAD_PROBE_INFO_DATA (&info);                           \
3435       /* if something went wrong, exit */                               \
3436       if (G_UNLIKELY (ret != GST_FLOW_OK)) {                            \
3437         if (handleable && ret == GST_FLOW_CUSTOM_SUCCESS_1) {           \
3438           ret = info.ABI.abi.flow_ret;                                          \
3439           goto handle_label;                                            \
3440         }                                                               \
3441         goto label;                                                     \
3442       }                                                                 \
3443     }                                                                   \
3444   } G_STMT_END
3445
3446 #define PROBE_PUSH(pad,mask,data,label)         \
3447   PROBE_FULL(pad, mask, data, -1, -1, label, FALSE, label);
3448 #define PROBE_HANDLE(pad,mask,data,label,handle_label)  \
3449   PROBE_FULL(pad, mask, data, -1, -1, label, TRUE, handle_label);
3450 #define PROBE_PULL(pad,mask,data,offs,size,label)               \
3451   PROBE_FULL(pad, mask, data, offs, size, label, FALSE, label);
3452
3453 static GstFlowReturn
3454 do_pad_idle_probe_wait (GstPad * pad)
3455 {
3456   while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3457     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3458         "waiting idle probe to be removed");
3459     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3460     GST_PAD_BLOCK_WAIT (pad);
3461     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3462     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3463
3464     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3465       return GST_FLOW_FLUSHING;
3466   }
3467   return GST_FLOW_OK;
3468 }
3469
3470 #define PROBE_TYPE_IS_SERIALIZED(i) \
3471     ( \
3472       ( \
3473         (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3474         GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3475         GST_EVENT_IS_SERIALIZED ((i)->data)) \
3476       ) || ( \
3477         (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3478         GST_QUERY_IS_SERIALIZED ((i)->data)) \
3479       ) || ( \
3480         ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3481         GST_PAD_PROBE_TYPE_BUFFER_LIST))  \
3482       ) \
3483     )
3484
3485 static GstFlowReturn
3486 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3487     GstFlowReturn defaultval)
3488 {
3489   ProbeMarshall data;
3490   guint cookie;
3491   gboolean is_block;
3492
3493   data.pad = pad;
3494   data.info = info;
3495   data.pass = FALSE;
3496   data.handled = FALSE;
3497   data.marshalled = FALSE;
3498   data.dropped = FALSE;
3499   data.cookie = ++pad->priv->probe_cookie;
3500
3501   is_block =
3502       (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3503
3504   if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3505     if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3506       goto flushing;
3507   }
3508
3509 again:
3510   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3511       "do probes cookie %u", data.cookie);
3512   cookie = pad->priv->probe_list_cookie;
3513
3514   g_hook_list_marshal (&pad->probes, TRUE,
3515       (GHookMarshaller) probe_hook_marshal, &data);
3516
3517   /* if the list changed, call the new callbacks (they will not have their
3518    * cookie set to data.cookie */
3519   if (cookie != pad->priv->probe_list_cookie) {
3520     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3521         "probe list changed, restarting");
3522     goto again;
3523   }
3524
3525   /* the first item that dropped will stop the hooks and then we drop here */
3526   if (data.dropped)
3527     goto dropped;
3528
3529   /* If one handler took care of it, let the the item pass */
3530   if (data.handled) {
3531     goto handled;
3532   }
3533
3534   /* if no handler matched and we are blocking, let the item pass */
3535   if (!data.marshalled && is_block)
3536     goto passed;
3537
3538   /* At this point, all handlers returned either OK or PASS. If one handler
3539    * returned PASS, let the item pass */
3540   if (data.pass)
3541     goto passed;
3542
3543   if (is_block) {
3544     while (GST_PAD_IS_BLOCKED (pad)) {
3545       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3546           "we are blocked %d times", pad->num_blocked);
3547
3548       /* we might have released the lock */
3549       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3550         goto flushing;
3551
3552       /* now we block the streaming thread. It can be unlocked when we
3553        * deactivate the pad (which will also set the FLUSHING flag) or
3554        * when the pad is unblocked. A flushing event will also unblock
3555        * the pad after setting the FLUSHING flag. */
3556       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3557           "Waiting to be unblocked or set flushing");
3558       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3559       GST_PAD_BLOCK_WAIT (pad);
3560       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3561       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3562
3563       /* if the list changed, call the new callbacks (they will not have their
3564        * cookie set to data.cookie */
3565       if (cookie != pad->priv->probe_list_cookie) {
3566         GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3567             "probe list changed, restarting");
3568         goto again;
3569       }
3570
3571       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3572         goto flushing;
3573     }
3574   }
3575
3576   return defaultval;
3577
3578   /* ERRORS */
3579 flushing:
3580   {
3581     GST_DEBUG_OBJECT (pad, "pad is flushing");
3582     return GST_FLOW_FLUSHING;
3583   }
3584 dropped:
3585   {
3586     GST_DEBUG_OBJECT (pad, "data is dropped");
3587     return GST_FLOW_CUSTOM_SUCCESS;
3588   }
3589 passed:
3590   {
3591     /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3592     GST_DEBUG_OBJECT (pad, "data is passed");
3593     return GST_FLOW_OK;
3594   }
3595 handled:
3596   {
3597     GST_DEBUG_OBJECT (pad, "data was handled");
3598     return GST_FLOW_CUSTOM_SUCCESS_1;
3599   }
3600 }
3601
3602 /* pad offsets */
3603
3604 /**
3605  * gst_pad_get_offset:
3606  * @pad: a #GstPad
3607  *
3608  * Get the offset applied to the running time of @pad. @pad has to be a source
3609  * pad.
3610  *
3611  * Returns: the offset.
3612  */
3613 gint64
3614 gst_pad_get_offset (GstPad * pad)
3615 {
3616   gint64 result;
3617
3618   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3619
3620   GST_OBJECT_LOCK (pad);
3621   result = pad->offset;
3622   GST_OBJECT_UNLOCK (pad);
3623
3624   return result;
3625 }
3626
3627 static gboolean
3628 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3629 {
3630   ev->received = FALSE;
3631   return TRUE;
3632 }
3633
3634 /**
3635  * gst_pad_set_offset:
3636  * @pad: a #GstPad
3637  * @offset: the offset
3638  *
3639  * Set the offset that will be applied to the running time of @pad.
3640  */
3641 void
3642 gst_pad_set_offset (GstPad * pad, gint64 offset)
3643 {
3644   g_return_if_fail (GST_IS_PAD (pad));
3645
3646   GST_OBJECT_LOCK (pad);
3647   /* if nothing changed, do nothing */
3648   if (pad->offset == offset)
3649     goto done;
3650
3651   pad->offset = offset;
3652   GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3653
3654   /* resend all sticky events with updated offset on next buffer push */
3655   events_foreach (pad, mark_event_not_received, NULL);
3656   GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3657
3658 done:
3659   GST_OBJECT_UNLOCK (pad);
3660 }
3661
3662 typedef struct
3663 {
3664   GstFlowReturn ret;
3665
3666   /* If TRUE and ret is not OK this means
3667    * that pushing the EOS event failed
3668    */
3669   gboolean was_eos;
3670
3671   /* If called for an event this is
3672    * the event that would be pushed
3673    * next. Don't forward sticky events
3674    * that would come after that */
3675   GstEvent *event;
3676 } PushStickyData;
3677
3678 /* should be called with pad LOCK */
3679 static gboolean
3680 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3681 {
3682   PushStickyData *data = user_data;
3683   GstEvent *event = ev->event;
3684
3685   if (ev->received) {
3686     GST_DEBUG_OBJECT (pad, "event %s was already received",
3687         GST_EVENT_TYPE_NAME (event));
3688     return TRUE;
3689   }
3690
3691   /* If we're called because of an sticky event, only forward
3692    * events that would come before this new event and the
3693    * event itself */
3694   if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3695       GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3696       GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3697     data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3698   } else {
3699     data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3700         GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3701   }
3702
3703   switch (data->ret) {
3704     case GST_FLOW_OK:
3705       ev->received = TRUE;
3706       GST_DEBUG_OBJECT (pad, "event %s marked received",
3707           GST_EVENT_TYPE_NAME (event));
3708       break;
3709     case GST_FLOW_CUSTOM_SUCCESS:
3710       /* we can't assume the event is received when it was dropped */
3711       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3712           GST_EVENT_TYPE_NAME (event));
3713       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3714       data->ret = GST_FLOW_OK;
3715       break;
3716     case GST_FLOW_CUSTOM_SUCCESS_1:
3717       /* event was ignored and should be sent later */
3718       GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3719           GST_EVENT_TYPE_NAME (event));
3720       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3721       data->ret = GST_FLOW_OK;
3722       break;
3723     case GST_FLOW_NOT_LINKED:
3724       /* not linked is not a problem, we are sticky so the event will be
3725        * sent later but only for non-EOS events */
3726       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3727       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3728         data->ret = GST_FLOW_OK;
3729       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3730       break;
3731     default:
3732       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3733           gst_flow_get_name (data->ret));
3734       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3735       break;
3736   }
3737
3738   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3739     data->was_eos = TRUE;
3740
3741   return data->ret == GST_FLOW_OK;
3742 }
3743
3744 /* check sticky events and push them when needed. should be called
3745  * with pad LOCK */
3746 static inline GstFlowReturn
3747 check_sticky (GstPad * pad, GstEvent * event)
3748 {
3749   PushStickyData data = { GST_FLOW_OK, FALSE, event };
3750
3751   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3752     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3753
3754     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3755     events_foreach (pad, push_sticky, &data);
3756
3757     /* If there's an EOS event we must push it downstream
3758      * even if sending a previous sticky event failed.
3759      * Otherwise the pipeline might wait forever for EOS.
3760      *
3761      * Only do this if pushing another event than the EOS
3762      * event failed.
3763      */
3764     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3765       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3766
3767       if (ev && !ev->received) {
3768         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3769             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3770         /* the event could have been dropped. Because this can only
3771          * happen if the user asked for it, it's not an error */
3772         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3773           data.ret = GST_FLOW_OK;
3774       }
3775     }
3776   }
3777   return data.ret;
3778 }
3779
3780
3781 /**
3782  * gst_pad_query:
3783  * @pad: a #GstPad to invoke the default query on.
3784  * @query: (transfer none): the #GstQuery to perform.
3785  *
3786  * Dispatches a query to a pad. The query should have been allocated by the
3787  * caller via one of the type-specific allocation functions. The element that
3788  * the pad belongs to is responsible for filling the query with an appropriate
3789  * response, which should then be parsed with a type-specific query parsing
3790  * function.
3791  *
3792  * Again, the caller is responsible for both the allocation and deallocation of
3793  * the query structure.
3794  *
3795  * Please also note that some queries might need a running pipeline to work.
3796  *
3797  * Returns: %TRUE if the query could be performed.
3798  */
3799 gboolean
3800 gst_pad_query (GstPad * pad, GstQuery * query)
3801 {
3802   GstObject *parent;
3803   gboolean res, serialized;
3804   GstPadQueryFunction func;
3805   GstPadProbeType type;
3806   GstFlowReturn ret;
3807
3808   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3809   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3810
3811   if (GST_PAD_IS_SRC (pad)) {
3812     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3813       goto wrong_direction;
3814     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3815   } else if (GST_PAD_IS_SINK (pad)) {
3816     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3817       goto wrong_direction;
3818     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3819   } else
3820     goto unknown_direction;
3821
3822   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3823       GST_QUERY_TYPE_NAME (query));
3824   GST_TRACER_PAD_QUERY_PRE (pad, query);
3825
3826   serialized = GST_QUERY_IS_SERIALIZED (query);
3827   if (G_UNLIKELY (serialized))
3828     GST_PAD_STREAM_LOCK (pad);
3829
3830   GST_OBJECT_LOCK (pad);
3831   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3832       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3833   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3834
3835   ACQUIRE_PARENT (pad, parent, no_parent);
3836   GST_OBJECT_UNLOCK (pad);
3837
3838   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3839     goto no_func;
3840
3841   res = func (pad, parent, query);
3842
3843   RELEASE_PARENT (parent);
3844
3845   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3846       GST_QUERY_TYPE_NAME (query), res);
3847   GST_TRACER_PAD_QUERY_POST (pad, res, query);
3848
3849   if (res != TRUE)
3850     goto query_failed;
3851
3852   GST_OBJECT_LOCK (pad);
3853   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3854   GST_OBJECT_UNLOCK (pad);
3855
3856   if (G_UNLIKELY (serialized))
3857     GST_PAD_STREAM_UNLOCK (pad);
3858
3859   return res;
3860
3861   /* ERRORS */
3862 wrong_direction:
3863   {
3864     g_warning ("pad %s:%s query %s in wrong direction",
3865         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3866     return FALSE;
3867   }
3868 unknown_direction:
3869   {
3870     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3871     return FALSE;
3872   }
3873 no_parent:
3874   {
3875     GST_DEBUG_OBJECT (pad, "had no parent");
3876     GST_OBJECT_UNLOCK (pad);
3877     if (G_UNLIKELY (serialized))
3878       GST_PAD_STREAM_UNLOCK (pad);
3879     return FALSE;
3880   }
3881 no_func:
3882   {
3883     GST_DEBUG_OBJECT (pad, "had no query function");
3884     RELEASE_PARENT (parent);
3885     if (G_UNLIKELY (serialized))
3886       GST_PAD_STREAM_UNLOCK (pad);
3887     return FALSE;
3888   }
3889 query_failed:
3890   {
3891     GST_DEBUG_OBJECT (pad, "query failed");
3892     if (G_UNLIKELY (serialized))
3893       GST_PAD_STREAM_UNLOCK (pad);
3894     return FALSE;
3895   }
3896 probe_stopped:
3897   {
3898     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3899     GST_OBJECT_UNLOCK (pad);
3900     if (G_UNLIKELY (serialized))
3901       GST_PAD_STREAM_UNLOCK (pad);
3902
3903     /* if a probe dropped without handling, we don't sent it further but assume
3904      * that the probe did not answer the query and return FALSE */
3905     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
3906       res = FALSE;
3907     else
3908       res = TRUE;
3909
3910     return res;
3911   }
3912 }
3913
3914 /**
3915  * gst_pad_peer_query:
3916  * @pad: a #GstPad to invoke the peer query on.
3917  * @query: (transfer none): the #GstQuery to perform.
3918  *
3919  * Performs gst_pad_query() on the peer of @pad.
3920  *
3921  * The caller is responsible for both the allocation and deallocation of
3922  * the query structure.
3923  *
3924  * Returns: %TRUE if the query could be performed. This function returns %FALSE
3925  * if @pad has no peer.
3926  */
3927 gboolean
3928 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3929 {
3930   GstPad *peerpad;
3931   GstPadProbeType type;
3932   gboolean res, serialized;
3933   GstFlowReturn ret;
3934
3935   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3936   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3937
3938   if (GST_PAD_IS_SRC (pad)) {
3939     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3940       goto wrong_direction;
3941     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3942   } else if (GST_PAD_IS_SINK (pad)) {
3943     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3944       goto wrong_direction;
3945     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3946   } else
3947     goto unknown_direction;
3948
3949   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3950       GST_QUERY_TYPE_NAME (query));
3951
3952   serialized = GST_QUERY_IS_SERIALIZED (query);
3953
3954   GST_OBJECT_LOCK (pad);
3955   if (GST_PAD_IS_SRC (pad) && serialized) {
3956     /* all serialized queries on the srcpad trigger push of
3957      * sticky events */
3958     if (check_sticky (pad, NULL) != GST_FLOW_OK)
3959       goto sticky_failed;
3960   }
3961
3962   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3963       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3964   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3965
3966   peerpad = GST_PAD_PEER (pad);
3967   if (G_UNLIKELY (peerpad == NULL))
3968     goto no_peer;
3969
3970   gst_object_ref (peerpad);
3971   GST_OBJECT_UNLOCK (pad);
3972
3973   res = gst_pad_query (peerpad, query);
3974
3975   gst_object_unref (peerpad);
3976
3977   if (res != TRUE)
3978     goto query_failed;
3979
3980   GST_OBJECT_LOCK (pad);
3981   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3982   GST_OBJECT_UNLOCK (pad);
3983
3984   return res;
3985
3986   /* ERRORS */
3987 wrong_direction:
3988   {
3989     g_warning ("pad %s:%s query %s in wrong direction",
3990         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3991     return FALSE;
3992   }
3993 unknown_direction:
3994   {
3995     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3996     return FALSE;
3997   }
3998 sticky_failed:
3999   {
4000     GST_WARNING_OBJECT (pad, "could not send sticky events");
4001     GST_OBJECT_UNLOCK (pad);
4002     return FALSE;
4003   }
4004 no_peer:
4005   {
4006     GST_INFO_OBJECT (pad, "pad has no peer");
4007     GST_OBJECT_UNLOCK (pad);
4008     return FALSE;
4009   }
4010 query_failed:
4011   {
4012     GST_DEBUG_OBJECT (pad, "query failed");
4013     return FALSE;
4014   }
4015 probe_stopped:
4016   {
4017     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4018     GST_OBJECT_UNLOCK (pad);
4019
4020     /* if a probe dropped without handling, we don't sent it further but
4021      * assume that the probe did not answer the query and return FALSE */
4022     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4023       res = FALSE;
4024     else
4025       res = TRUE;
4026
4027     return res;
4028   }
4029 }
4030
4031 /**********************************************************************
4032  * Data passing functions
4033  */
4034
4035 /* this is the chain function that does not perform the additional argument
4036  * checking for that little extra speed.
4037  */
4038 static inline GstFlowReturn
4039 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4040 {
4041   GstFlowReturn ret;
4042   GstObject *parent;
4043   gboolean handled = FALSE;
4044
4045   GST_PAD_STREAM_LOCK (pad);
4046
4047   GST_OBJECT_LOCK (pad);
4048   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4049     goto flushing;
4050
4051   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4052     goto eos;
4053
4054   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4055     goto wrong_mode;
4056
4057 #ifndef G_DISABLE_ASSERT
4058   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4059     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4060       g_warning (G_STRLOC
4061           ":%s:<%s:%s> Got data flow before stream-start event",
4062           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4063     }
4064     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4065       g_warning (G_STRLOC
4066           ":%s:<%s:%s> Got data flow before segment event",
4067           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4068     }
4069     pad->priv->last_cookie = pad->priv->events_cookie;
4070   }
4071 #endif
4072
4073   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4074       probe_handled);
4075
4076   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4077
4078   ACQUIRE_PARENT (pad, parent, no_parent);
4079   GST_OBJECT_UNLOCK (pad);
4080
4081   /* NOTE: we read the chainfunc unlocked.
4082    * we cannot hold the lock for the pad so we might send
4083    * the data to the wrong function. This is not really a
4084    * problem since functions are assigned at creation time
4085    * and don't change that often... */
4086   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4087     GstPadChainFunction chainfunc;
4088
4089     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4090       goto no_function;
4091
4092     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4093         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4094         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4095
4096     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4097
4098     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4099         "called chainfunction &%s with buffer %p, returned %s",
4100         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4101   } else {
4102     GstPadChainListFunction chainlistfunc;
4103
4104     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4105       goto no_function;
4106
4107     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4108         "calling chainlistfunction &%s",
4109         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4110
4111     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4112
4113     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4114         "called chainlistfunction &%s, returned %s",
4115         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4116   }
4117
4118   RELEASE_PARENT (parent);
4119
4120   GST_PAD_STREAM_UNLOCK (pad);
4121
4122   return ret;
4123
4124   /* ERRORS */
4125 flushing:
4126   {
4127     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4128         "chaining, but pad was flushing");
4129     GST_OBJECT_UNLOCK (pad);
4130     GST_PAD_STREAM_UNLOCK (pad);
4131     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4132     return GST_FLOW_FLUSHING;
4133   }
4134 eos:
4135   {
4136     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4137     GST_OBJECT_UNLOCK (pad);
4138     GST_PAD_STREAM_UNLOCK (pad);
4139     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4140     return GST_FLOW_EOS;
4141   }
4142 wrong_mode:
4143   {
4144     g_critical ("chain on pad %s:%s but it was not in push mode",
4145         GST_DEBUG_PAD_NAME (pad));
4146     GST_OBJECT_UNLOCK (pad);
4147     GST_PAD_STREAM_UNLOCK (pad);
4148     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4149     return GST_FLOW_ERROR;
4150   }
4151 probe_handled:
4152   handled = TRUE;
4153   /* PASSTHROUGH */
4154 probe_stopped:
4155   {
4156     GST_OBJECT_UNLOCK (pad);
4157     GST_PAD_STREAM_UNLOCK (pad);
4158     /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4159     if (!handled)
4160       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4161
4162     switch (ret) {
4163       case GST_FLOW_CUSTOM_SUCCESS:
4164       case GST_FLOW_CUSTOM_SUCCESS_1:
4165         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4166         ret = GST_FLOW_OK;
4167         break;
4168       default:
4169         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4170         break;
4171     }
4172     return ret;
4173   }
4174 no_parent:
4175   {
4176     GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4177     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4178     GST_OBJECT_UNLOCK (pad);
4179     GST_PAD_STREAM_UNLOCK (pad);
4180     return GST_FLOW_FLUSHING;
4181   }
4182 no_function:
4183   {
4184     RELEASE_PARENT (parent);
4185     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4186     g_critical ("chain on pad %s:%s but it has no chainfunction",
4187         GST_DEBUG_PAD_NAME (pad));
4188     GST_PAD_STREAM_UNLOCK (pad);
4189     return GST_FLOW_NOT_SUPPORTED;
4190   }
4191 }
4192
4193 /**
4194  * gst_pad_chain:
4195  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4196  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4197  *     if not.
4198  *
4199  * Chain a buffer to @pad.
4200  *
4201  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4202  *
4203  * If the buffer type is not acceptable for @pad (as negotiated with a
4204  * preceding GST_EVENT_CAPS event), this function returns
4205  * #GST_FLOW_NOT_NEGOTIATED.
4206  *
4207  * The function proceeds calling the chain function installed on @pad (see
4208  * gst_pad_set_chain_function()) and the return value of that function is
4209  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4210  * chain function.
4211  *
4212  * In all cases, success or failure, the caller loses its reference to @buffer
4213  * after calling this function.
4214  *
4215  * Returns: a #GstFlowReturn from the pad.
4216  *
4217  * MT safe.
4218  */
4219 GstFlowReturn
4220 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4221 {
4222   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4223   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4224   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4225
4226   return gst_pad_chain_data_unchecked (pad,
4227       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4228 }
4229
4230 static GstFlowReturn
4231 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4232     GstBufferList * list)
4233 {
4234   guint i, len;
4235   GstBuffer *buffer;
4236   GstFlowReturn ret;
4237
4238   GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4239
4240   len = gst_buffer_list_length (list);
4241
4242   ret = GST_FLOW_OK;
4243   for (i = 0; i < len; i++) {
4244     buffer = gst_buffer_list_get (list, i);
4245     ret =
4246         gst_pad_chain_data_unchecked (pad,
4247         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4248         gst_buffer_ref (buffer));
4249     if (ret != GST_FLOW_OK)
4250       break;
4251   }
4252   gst_buffer_list_unref (list);
4253
4254   return ret;
4255 }
4256
4257 /**
4258  * gst_pad_chain_list:
4259  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4260  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4261  *     if not.
4262  *
4263  * Chain a bufferlist to @pad.
4264  *
4265  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4266  *
4267  * If @pad was not negotiated properly with a CAPS event, this function
4268  * returns #GST_FLOW_NOT_NEGOTIATED.
4269  *
4270  * The function proceeds calling the chainlist function installed on @pad (see
4271  * gst_pad_set_chain_list_function()) and the return value of that function is
4272  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4273  * chainlist function.
4274  *
4275  * In all cases, success or failure, the caller loses its reference to @list
4276  * after calling this function.
4277  *
4278  * MT safe.
4279  *
4280  * Returns: a #GstFlowReturn from the pad.
4281  */
4282 GstFlowReturn
4283 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4284 {
4285   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4286   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4287   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4288
4289   return gst_pad_chain_data_unchecked (pad,
4290       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4291 }
4292
4293 static GstFlowReturn
4294 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4295 {
4296   GstPad *peer;
4297   GstFlowReturn ret;
4298   gboolean handled = FALSE;
4299
4300   GST_OBJECT_LOCK (pad);
4301   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4302     goto flushing;
4303
4304   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4305     goto eos;
4306
4307   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4308     goto wrong_mode;
4309
4310 #ifndef G_DISABLE_ASSERT
4311   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4312     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4313       g_warning (G_STRLOC
4314           ":%s:<%s:%s> Got data flow before stream-start event",
4315           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4316     }
4317     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4318       g_warning (G_STRLOC
4319           ":%s:<%s:%s> Got data flow before segment event",
4320           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4321     }
4322     pad->priv->last_cookie = pad->priv->events_cookie;
4323   }
4324 #endif
4325
4326   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4327     goto events_error;
4328
4329   /* do block probes */
4330   PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4331       probe_handled);
4332
4333   /* recheck sticky events because the probe might have cause a relink */
4334   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4335     goto events_error;
4336
4337   /* do post-blocking probes */
4338   PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4339
4340   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4341     goto not_linked;
4342
4343   /* take ref to peer pad before releasing the lock */
4344   gst_object_ref (peer);
4345   pad->priv->using++;
4346   GST_OBJECT_UNLOCK (pad);
4347
4348   ret = gst_pad_chain_data_unchecked (peer, type, data);
4349   data = NULL;
4350
4351   gst_object_unref (peer);
4352
4353   GST_OBJECT_LOCK (pad);
4354   pad->ABI.abi.last_flowret = ret;
4355   pad->priv->using--;
4356   if (pad->priv->using == 0) {
4357     /* pad is not active anymore, trigger idle callbacks */
4358     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4359         probe_stopped, ret);
4360   }
4361   GST_OBJECT_UNLOCK (pad);
4362
4363   return ret;
4364
4365   /* ERROR recovery here */
4366   /* ERRORS */
4367 flushing:
4368   {
4369     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4370         "pushing, but pad was flushing");
4371     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4372     GST_OBJECT_UNLOCK (pad);
4373     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4374     return GST_FLOW_FLUSHING;
4375   }
4376 eos:
4377   {
4378     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4379     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4380     GST_OBJECT_UNLOCK (pad);
4381     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4382     return GST_FLOW_EOS;
4383   }
4384 wrong_mode:
4385   {
4386     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4387         GST_DEBUG_PAD_NAME (pad));
4388     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4389     GST_OBJECT_UNLOCK (pad);
4390     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4391     return GST_FLOW_ERROR;
4392   }
4393 events_error:
4394   {
4395     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4396         "error pushing events, return %s", gst_flow_get_name (ret));
4397     pad->ABI.abi.last_flowret = ret;
4398     GST_OBJECT_UNLOCK (pad);
4399     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4400     return ret;
4401   }
4402 probe_handled:
4403   handled = TRUE;
4404   /* PASSTHROUGH */
4405 probe_stopped:
4406   {
4407     GST_OBJECT_UNLOCK (pad);
4408     if (data != NULL && !handled)
4409       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4410
4411     switch (ret) {
4412       case GST_FLOW_CUSTOM_SUCCESS:
4413       case GST_FLOW_CUSTOM_SUCCESS_1:
4414         GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4415         ret = GST_FLOW_OK;
4416         break;
4417       default:
4418         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4419         break;
4420     }
4421     pad->ABI.abi.last_flowret = ret;
4422     return ret;
4423   }
4424 not_linked:
4425   {
4426     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4427         "pushing, but it was not linked");
4428     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4429     GST_OBJECT_UNLOCK (pad);
4430     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4431     return GST_FLOW_NOT_LINKED;
4432   }
4433 }
4434
4435 /**
4436  * gst_pad_push:
4437  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4438  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4439  *     if not.
4440  *
4441  * Pushes a buffer to the peer of @pad.
4442  *
4443  * This function will call installed block probes before triggering any
4444  * installed data probes.
4445  *
4446  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4447  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4448  * be returned.
4449  *
4450  * In all cases, success or failure, the caller loses its reference to @buffer
4451  * after calling this function.
4452  *
4453  * Returns: a #GstFlowReturn from the peer pad.
4454  *
4455  * MT safe.
4456  */
4457 GstFlowReturn
4458 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4459 {
4460   GstFlowReturn res;
4461
4462   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4463   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4464   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4465
4466   GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4467   res = gst_pad_push_data (pad,
4468       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4469   GST_TRACER_PAD_PUSH_POST (pad, res);
4470   return res;
4471 }
4472
4473 /**
4474  * gst_pad_push_list:
4475  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4476  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4477  *     if not.
4478  *
4479  * Pushes a buffer list to the peer of @pad.
4480  *
4481  * This function will call installed block probes before triggering any
4482  * installed data probes.
4483  *
4484  * The function proceeds calling the chain function on the peer pad and returns
4485  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4486  * be returned. If the peer pad does not have any installed chainlist function
4487  * every group buffer of the list will be merged into a normal #GstBuffer and
4488  * chained via gst_pad_chain().
4489  *
4490  * In all cases, success or failure, the caller loses its reference to @list
4491  * after calling this function.
4492  *
4493  * Returns: a #GstFlowReturn from the peer pad.
4494  *
4495  * MT safe.
4496  */
4497 GstFlowReturn
4498 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4499 {
4500   GstFlowReturn res;
4501
4502   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4503   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4504   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4505
4506   GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4507   res = gst_pad_push_data (pad,
4508       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4509   GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4510   return res;
4511 }
4512
4513 static GstFlowReturn
4514 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4515     GstBuffer ** buffer)
4516 {
4517   GstFlowReturn ret;
4518   GstPadGetRangeFunction getrangefunc;
4519   GstObject *parent;
4520   GstBuffer *res_buf;
4521
4522   GST_PAD_STREAM_LOCK (pad);
4523
4524   GST_OBJECT_LOCK (pad);
4525   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4526     goto flushing;
4527
4528   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4529     goto wrong_mode;
4530
4531   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4532     goto events_error;
4533
4534   res_buf = *buffer;
4535
4536   /* when one of the probes returns DROPPED, probe_stopped will be called
4537    * and we skip calling the getrange function, res_buf should then contain a
4538    * valid result buffer */
4539   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4540       res_buf, offset, size, probe_stopped);
4541
4542   /* recheck sticky events because the probe might have cause a relink */
4543   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4544     goto events_error;
4545
4546   ACQUIRE_PARENT (pad, parent, no_parent);
4547   GST_OBJECT_UNLOCK (pad);
4548
4549   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4550     goto no_function;
4551
4552   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4553       "calling getrangefunc %s, offset %"
4554       G_GUINT64_FORMAT ", size %u",
4555       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4556
4557   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4558
4559   RELEASE_PARENT (parent);
4560
4561   GST_OBJECT_LOCK (pad);
4562   if (G_UNLIKELY (ret != GST_FLOW_OK))
4563     goto get_range_failed;
4564
4565   /* can only fire the signal if we have a valid buffer */
4566 probed_data:
4567   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4568       res_buf, offset, size, probe_stopped_unref);
4569   pad->ABI.abi.last_flowret = ret;
4570   GST_OBJECT_UNLOCK (pad);
4571
4572   GST_PAD_STREAM_UNLOCK (pad);
4573
4574   *buffer = res_buf;
4575
4576   return ret;
4577
4578   /* ERRORS */
4579 flushing:
4580   {
4581     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4582         "getrange, but pad was flushing");
4583     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4584     GST_OBJECT_UNLOCK (pad);
4585     GST_PAD_STREAM_UNLOCK (pad);
4586     return GST_FLOW_FLUSHING;
4587   }
4588 wrong_mode:
4589   {
4590     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4591         GST_DEBUG_PAD_NAME (pad));
4592     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4593     GST_OBJECT_UNLOCK (pad);
4594     GST_PAD_STREAM_UNLOCK (pad);
4595     return GST_FLOW_ERROR;
4596   }
4597 events_error:
4598   {
4599     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4600     pad->ABI.abi.last_flowret = ret;
4601     GST_OBJECT_UNLOCK (pad);
4602     GST_PAD_STREAM_UNLOCK (pad);
4603     return ret;
4604   }
4605 no_parent:
4606   {
4607     GST_DEBUG_OBJECT (pad, "no parent");
4608     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4609     GST_OBJECT_UNLOCK (pad);
4610     GST_PAD_STREAM_UNLOCK (pad);
4611     return GST_FLOW_FLUSHING;
4612   }
4613 no_function:
4614   {
4615     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4616         GST_DEBUG_PAD_NAME (pad));
4617     RELEASE_PARENT (parent);
4618     GST_PAD_STREAM_UNLOCK (pad);
4619     return GST_FLOW_NOT_SUPPORTED;
4620   }
4621 probe_stopped:
4622   {
4623     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4624         "probe returned %s", gst_flow_get_name (ret));
4625     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4626       if (res_buf) {
4627         /* the probe filled the buffer and asks us to not call the getrange
4628          * anymore, we continue with the post probes then. */
4629         GST_DEBUG_OBJECT (pad, "handled buffer");
4630         ret = GST_FLOW_OK;
4631         goto probed_data;
4632       } else {
4633         /* no buffer, we are EOS */
4634         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4635         ret = GST_FLOW_EOS;
4636       }
4637     }
4638     pad->ABI.abi.last_flowret = ret;
4639     GST_OBJECT_UNLOCK (pad);
4640     GST_PAD_STREAM_UNLOCK (pad);
4641
4642     return ret;
4643   }
4644 probe_stopped_unref:
4645   {
4646     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4647         "probe returned %s", gst_flow_get_name (ret));
4648     /* if we drop here, it signals EOS */
4649     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4650       ret = GST_FLOW_EOS;
4651     pad->ABI.abi.last_flowret = ret;
4652     GST_OBJECT_UNLOCK (pad);
4653     GST_PAD_STREAM_UNLOCK (pad);
4654     if (*buffer == NULL)
4655       gst_buffer_unref (res_buf);
4656     return ret;
4657   }
4658 get_range_failed:
4659   {
4660     pad->ABI.abi.last_flowret = ret;
4661     GST_OBJECT_UNLOCK (pad);
4662     GST_PAD_STREAM_UNLOCK (pad);
4663     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4664         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4665         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4666     return ret;
4667   }
4668 }
4669
4670 /**
4671  * gst_pad_get_range:
4672  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4673  * @offset: The start offset of the buffer
4674  * @size: The length of the buffer
4675  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4676  *     returns #GST_FLOW_ERROR if %NULL.
4677  *
4678  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4679  * immediately and @buffer is %NULL.
4680  *
4681  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4682  * description of a getrange function. If @pad has no getrange function
4683  * installed (see gst_pad_set_getrange_function()) this function returns
4684  * #GST_FLOW_NOT_SUPPORTED.
4685  *
4686  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4687  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4688  * must be freed with gst_buffer_unref() after usage.
4689  *
4690  * When @buffer points to a variable that points to a valid #GstBuffer, the
4691  * buffer will be filled with the result data when this function returns
4692  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4693  * @size bytes will be filled in the result buffer and its size will be updated
4694  * accordingly.
4695  *
4696  * Note that less than @size bytes can be returned in @buffer when, for example,
4697  * an EOS condition is near or when @buffer is not large enough to hold @size
4698  * bytes. The caller should check the result buffer size to get the result size.
4699  *
4700  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4701  * will be unchanged.
4702  *
4703  * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4704  *
4705  * Returns: a #GstFlowReturn from the pad.
4706  *
4707  * MT safe.
4708  */
4709 GstFlowReturn
4710 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4711     GstBuffer ** buffer)
4712 {
4713   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4714   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4715   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4716   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4717           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4718
4719   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4720 }
4721
4722 /**
4723  * gst_pad_pull_range:
4724  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4725  * @offset: The start offset of the buffer
4726  * @size: The length of the buffer
4727  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4728  *     GST_FLOW_ERROR if %NULL.
4729  *
4730  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4731  *
4732  * This function will first trigger the pad block signal if it was
4733  * installed.
4734  *
4735  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4736  * function returns the result of gst_pad_get_range() on the peer pad.
4737  * See gst_pad_get_range() for a list of return values and for the
4738  * semantics of the arguments of this function.
4739  *
4740  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4741  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4742  * must be freed with gst_buffer_unref() after usage. When this function
4743  * returns any other result value, @buffer will still point to %NULL.
4744  *
4745  * When @buffer points to a variable that points to a valid #GstBuffer, the
4746  * buffer will be filled with the result data when this function returns
4747  * #GST_FLOW_OK. When this function returns any other result value,
4748  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4749  * @size bytes will be filled in the result buffer and its size will be updated
4750  * accordingly.
4751  *
4752  * Note that less than @size bytes can be returned in @buffer when, for example,
4753  * an EOS condition is near or when @buffer is not large enough to hold @size
4754  * bytes. The caller should check the result buffer size to get the result size.
4755  *
4756  * Returns: a #GstFlowReturn from the peer pad.
4757  *
4758  * MT safe.
4759  */
4760 GstFlowReturn
4761 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4762     GstBuffer ** buffer)
4763 {
4764   GstPad *peer;
4765   GstFlowReturn ret;
4766   GstBuffer *res_buf;
4767
4768   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4769   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4770   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4771   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4772           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4773
4774   GST_TRACER_PAD_PULL_RANGE_PRE (pad, offset, size);
4775
4776   GST_OBJECT_LOCK (pad);
4777   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4778     goto flushing;
4779
4780   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4781     goto wrong_mode;
4782
4783   res_buf = *buffer;
4784
4785   /* when one of the probes returns DROPPED, probe_stopped will be
4786    * called and we skip calling the peer getrange function. *buffer should then
4787    * contain a valid buffer */
4788   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4789       res_buf, offset, size, probe_stopped);
4790
4791   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4792     goto not_linked;
4793
4794   gst_object_ref (peer);
4795   pad->priv->using++;
4796   GST_OBJECT_UNLOCK (pad);
4797
4798   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4799
4800   gst_object_unref (peer);
4801
4802   GST_OBJECT_LOCK (pad);
4803   pad->priv->using--;
4804   pad->ABI.abi.last_flowret = ret;
4805   if (pad->priv->using == 0) {
4806     /* pad is not active anymore, trigger idle callbacks */
4807     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4808         probe_stopped_unref, ret);
4809   }
4810
4811   if (G_UNLIKELY (ret != GST_FLOW_OK))
4812     goto pull_range_failed;
4813
4814 probed_data:
4815   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4816       res_buf, offset, size, probe_stopped_unref);
4817
4818   GST_OBJECT_UNLOCK (pad);
4819
4820   *buffer = res_buf;
4821
4822   GST_TRACER_PAD_PULL_RANGE_POST (pad, *buffer, ret);
4823   return ret;
4824
4825   /* ERROR recovery here */
4826 flushing:
4827   {
4828     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4829         "pullrange, but pad was flushing");
4830     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4831     GST_OBJECT_UNLOCK (pad);
4832     ret = GST_FLOW_FLUSHING;
4833     goto done;
4834   }
4835 wrong_mode:
4836   {
4837     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4838         GST_DEBUG_PAD_NAME (pad));
4839     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4840     GST_OBJECT_UNLOCK (pad);
4841     ret = GST_FLOW_ERROR;
4842     goto done;
4843   }
4844 probe_stopped:
4845   {
4846     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4847         gst_flow_get_name (ret));
4848     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4849       if (res_buf) {
4850         /* the probe filled the buffer and asks us to not forward to the peer
4851          * anymore, we continue with the post probes then */
4852         GST_DEBUG_OBJECT (pad, "handled buffer");
4853         ret = GST_FLOW_OK;
4854         goto probed_data;
4855       } else {
4856         /* no buffer, we are EOS then */
4857         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4858         ret = GST_FLOW_EOS;
4859       }
4860     }
4861     pad->ABI.abi.last_flowret = ret;
4862     GST_OBJECT_UNLOCK (pad);
4863     goto done;
4864   }
4865 not_linked:
4866   {
4867     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4868         "pulling range, but it was not linked");
4869     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4870     GST_OBJECT_UNLOCK (pad);
4871     ret = GST_FLOW_NOT_LINKED;
4872     goto done;
4873   }
4874 pull_range_failed:
4875   {
4876     pad->ABI.abi.last_flowret = ret;
4877     GST_OBJECT_UNLOCK (pad);
4878     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4879         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4880         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4881     goto done;
4882   }
4883 probe_stopped_unref:
4884   {
4885     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4886         "post probe returned %s", gst_flow_get_name (ret));
4887
4888     /* if we drop here, it signals EOS */
4889     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4890       ret = GST_FLOW_EOS;
4891
4892     pad->ABI.abi.last_flowret = ret;
4893     GST_OBJECT_UNLOCK (pad);
4894
4895     if (*buffer == NULL)
4896       gst_buffer_unref (res_buf);
4897     goto done;
4898   }
4899 done:
4900   GST_TRACER_PAD_PULL_RANGE_POST (pad, NULL, ret);
4901   return ret;
4902 }
4903
4904 /* must be called with pad object lock */
4905 static GstFlowReturn
4906 store_sticky_event (GstPad * pad, GstEvent * event)
4907 {
4908   guint i, len;
4909   GstEventType type;
4910   GArray *events;
4911   gboolean res = FALSE;
4912   const gchar *name = NULL;
4913   gboolean insert = TRUE;
4914
4915   type = GST_EVENT_TYPE (event);
4916
4917   /* Store all sticky events except SEGMENT/EOS when we're flushing,
4918    * otherwise they can be dropped and nothing would ever resend them.
4919    * Only do that for activated pads though, everything else is a bug!
4920    */
4921   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4922           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4923                   || type == GST_EVENT_EOS))))
4924     goto flushed;
4925
4926   /* Unset the EOS flag when received STREAM_START event, so pad can
4927    * store sticky event and then push it later */
4928   if (type == GST_EVENT_STREAM_START) {
4929     GST_LOG_OBJECT (pad, "Removing pending EOS events");
4930     remove_event_by_type (pad, GST_EVENT_EOS);
4931     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4932   }
4933
4934   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4935     goto eos;
4936
4937   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4938     name = gst_structure_get_name (gst_event_get_structure (event));
4939
4940   events = pad->priv->events;
4941   len = events->len;
4942
4943   for (i = 0; i < len; i++) {
4944     PadEvent *ev = &g_array_index (events, PadEvent, i);
4945
4946     if (ev->event == NULL)
4947       continue;
4948
4949     if (type == GST_EVENT_TYPE (ev->event)) {
4950       /* matching types, check matching name if needed */
4951       if (name && !gst_event_has_name (ev->event, name))
4952         continue;
4953
4954       /* overwrite */
4955       if ((res = gst_event_replace (&ev->event, event)))
4956         ev->received = FALSE;
4957
4958       insert = FALSE;
4959       break;
4960     }
4961
4962     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4963             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4964       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4965        * storing the sticky ordered we can check that this is respected. */
4966       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4967               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4968         g_warning (G_STRLOC
4969             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4970             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4971             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4972             gst_event_type_get_name (type));
4973       break;
4974     }
4975   }
4976   if (insert) {
4977     PadEvent ev;
4978     ev.event = gst_event_ref (event);
4979     ev.received = FALSE;
4980     g_array_insert_val (events, i, ev);
4981     res = TRUE;
4982   }
4983
4984   if (res) {
4985     pad->priv->events_cookie++;
4986     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4987
4988     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4989
4990     switch (GST_EVENT_TYPE (event)) {
4991       case GST_EVENT_CAPS:
4992         GST_OBJECT_UNLOCK (pad);
4993
4994         GST_DEBUG_OBJECT (pad, "notify caps");
4995         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4996
4997         GST_OBJECT_LOCK (pad);
4998         break;
4999       default:
5000         break;
5001     }
5002   }
5003   if (type == GST_EVENT_EOS) {
5004     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
5005     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
5006   }
5007
5008   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
5009
5010   /* ERRORS */
5011 flushed:
5012   {
5013     GST_DEBUG_OBJECT (pad, "pad is flushing");
5014     return GST_FLOW_FLUSHING;
5015   }
5016 eos:
5017   {
5018     GST_DEBUG_OBJECT (pad, "pad is EOS");
5019     return GST_FLOW_EOS;
5020   }
5021 }
5022
5023 /**
5024  * gst_pad_store_sticky_event:
5025  * @pad: a #GstPad
5026  * @event: a #GstEvent
5027  *
5028  * Store the sticky @event on @pad
5029  *
5030  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5031  * was flushing or #GST_FLOW_EOS when the pad was EOS.
5032  *
5033  * Since: 1.2
5034  */
5035 GstFlowReturn
5036 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5037 {
5038   GstFlowReturn ret;
5039
5040   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5041   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5042
5043   GST_OBJECT_LOCK (pad);
5044   ret = store_sticky_event (pad, event);
5045   GST_OBJECT_UNLOCK (pad);
5046
5047   return ret;
5048 }
5049
5050 static gboolean
5051 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5052 {
5053   PushStickyData *data = user_data;
5054
5055   /* Forward all sticky events before our current one that are pending */
5056   if (ev->event != data->event
5057       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5058     return push_sticky (pad, ev, data);
5059
5060   return TRUE;
5061 }
5062
5063 /* should be called with pad LOCK */
5064 static GstFlowReturn
5065 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5066     GstPadProbeType type)
5067 {
5068   GstFlowReturn ret;
5069   GstPad *peerpad;
5070   GstEventType event_type;
5071
5072   /* pass the adjusted event on. We need to do this even if
5073    * there is no peer pad because of the probes. */
5074   event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5075
5076   /* Two checks to be made:
5077    * . (un)set the FLUSHING flag for flushing events,
5078    * . handle pad blocking */
5079   event_type = GST_EVENT_TYPE (event);
5080   switch (event_type) {
5081     case GST_EVENT_FLUSH_START:
5082       GST_PAD_SET_FLUSHING (pad);
5083
5084       GST_PAD_BLOCK_BROADCAST (pad);
5085       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5086       break;
5087     case GST_EVENT_FLUSH_STOP:
5088       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5089         goto inactive;
5090
5091       GST_PAD_UNSET_FLUSHING (pad);
5092
5093       /* Remove sticky EOS events */
5094       GST_LOG_OBJECT (pad, "Removing pending EOS events");
5095       remove_event_by_type (pad, GST_EVENT_EOS);
5096       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5097       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5098       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5099
5100       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5101       break;
5102     default:
5103     {
5104       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5105         goto flushed;
5106
5107       /* No need to check for EOS here as either the caller (gst_pad_push_event())
5108        * checked already or this is called as part of pushing sticky events,
5109        * in which case we still want to forward the EOS event downstream.
5110        */
5111
5112       switch (GST_EVENT_TYPE (event)) {
5113         case GST_EVENT_RECONFIGURE:
5114           if (GST_PAD_IS_SINK (pad))
5115             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5116           break;
5117         default:
5118           break;
5119       }
5120       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5121           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5122       break;
5123     }
5124   }
5125
5126   /* send probes after modifying the events above */
5127   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5128
5129   /* recheck sticky events because the probe might have cause a relink */
5130   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5131       && (GST_EVENT_IS_SERIALIZED (event)
5132           || GST_EVENT_IS_STICKY (event))) {
5133     PushStickyData data = { GST_FLOW_OK, FALSE, event };
5134     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5135
5136     /* Push all sticky events before our current one
5137      * that have changed */
5138     events_foreach (pad, sticky_changed, &data);
5139   }
5140
5141   /* now check the peer pad */
5142   peerpad = GST_PAD_PEER (pad);
5143   if (peerpad == NULL)
5144     goto not_linked;
5145
5146   gst_object_ref (peerpad);
5147   pad->priv->using++;
5148   GST_OBJECT_UNLOCK (pad);
5149
5150   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5151       event, gst_event_type_get_name (event_type), peerpad);
5152
5153   ret = gst_pad_send_event_unchecked (peerpad, event, type);
5154
5155   /* Note: we gave away ownership of the event at this point but we can still
5156    * print the old pointer */
5157   GST_LOG_OBJECT (pad,
5158       "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5159       gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5160
5161   gst_object_unref (peerpad);
5162
5163   GST_OBJECT_LOCK (pad);
5164   pad->priv->using--;
5165   if (pad->priv->using == 0) {
5166     /* pad is not active anymore, trigger idle callbacks */
5167     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5168         idle_probe_stopped, ret);
5169   }
5170   return ret;
5171
5172   /* ERROR handling */
5173 flushed:
5174   {
5175     GST_DEBUG_OBJECT (pad, "We're flushing");
5176     gst_event_unref (event);
5177     return GST_FLOW_FLUSHING;
5178   }
5179 inactive:
5180   {
5181     GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5182     gst_event_unref (event);
5183     return GST_FLOW_FLUSHING;
5184   }
5185 probe_stopped:
5186   {
5187     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5188     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5189       gst_event_unref (event);
5190
5191     switch (ret) {
5192       case GST_FLOW_CUSTOM_SUCCESS_1:
5193         GST_DEBUG_OBJECT (pad, "handled event");
5194         break;
5195       case GST_FLOW_CUSTOM_SUCCESS:
5196         GST_DEBUG_OBJECT (pad, "dropped event");
5197         break;
5198       default:
5199         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5200         break;
5201     }
5202     return ret;
5203   }
5204 not_linked:
5205   {
5206     GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5207         gst_event_type_get_name (GST_EVENT_TYPE (event)));
5208     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5209     gst_event_unref (event);
5210
5211     /* unlinked pads should not influence latency configuration */
5212     if (event_type == GST_EVENT_LATENCY)
5213       return GST_FLOW_OK;
5214
5215     return GST_FLOW_NOT_LINKED;
5216   }
5217 idle_probe_stopped:
5218   {
5219     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5220     return ret;
5221   }
5222 }
5223
5224 /**
5225  * gst_pad_push_event:
5226  * @pad: a #GstPad to push the event to.
5227  * @event: (transfer full): the #GstEvent to send to the pad.
5228  *
5229  * Sends the event to the peer of the given pad. This function is
5230  * mainly used by elements to send events to their peer
5231  * elements.
5232  *
5233  * This function takes ownership of the provided event so you should
5234  * gst_event_ref() it if you want to reuse the event after this call.
5235  *
5236  * Returns: %TRUE if the event was handled.
5237  *
5238  * MT safe.
5239  */
5240 gboolean
5241 gst_pad_push_event (GstPad * pad, GstEvent * event)
5242 {
5243   gboolean res = FALSE;
5244   GstPadProbeType type;
5245   gboolean sticky, serialized;
5246
5247   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5248   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5249
5250   GST_TRACER_PAD_PUSH_EVENT_PRE (pad, event);
5251
5252   if (GST_PAD_IS_SRC (pad)) {
5253     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5254       goto wrong_direction;
5255     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5256   } else if (GST_PAD_IS_SINK (pad)) {
5257     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5258       goto wrong_direction;
5259     /* events pushed on sinkpad never are sticky */
5260     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5261   } else
5262     goto unknown_direction;
5263
5264   GST_OBJECT_LOCK (pad);
5265   sticky = GST_EVENT_IS_STICKY (event);
5266   serialized = GST_EVENT_IS_SERIALIZED (event);
5267
5268   if (sticky) {
5269     /* srcpad sticky events are stored immediately, the received flag is set
5270      * to FALSE and will be set to TRUE when we can successfully push the
5271      * event to the peer pad */
5272     switch (store_sticky_event (pad, event)) {
5273       case GST_FLOW_FLUSHING:
5274         goto flushed;
5275       case GST_FLOW_EOS:
5276         goto eos;
5277       default:
5278         break;
5279     }
5280   }
5281   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5282     /* all serialized or sticky events on the srcpad trigger push of
5283      * sticky events */
5284     res = (check_sticky (pad, event) == GST_FLOW_OK);
5285   }
5286   if (!sticky) {
5287     GstFlowReturn ret;
5288
5289     /* other events are pushed right away */
5290     ret = gst_pad_push_event_unchecked (pad, event, type);
5291     /* dropped events by a probe are not an error */
5292     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5293         || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5294   } else {
5295     /* Errors in sticky event pushing are no problem and ignored here
5296      * as they will cause more meaningful errors during data flow.
5297      * For EOS events, that are not followed by data flow, we still
5298      * return FALSE here though.
5299      */
5300     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5301       res = TRUE;
5302     gst_event_unref (event);
5303   }
5304   GST_OBJECT_UNLOCK (pad);
5305
5306   GST_TRACER_PAD_PUSH_EVENT_POST (pad, res);
5307   return res;
5308
5309   /* ERROR handling */
5310 wrong_direction:
5311   {
5312     g_warning ("pad %s:%s pushing %s event in wrong direction",
5313         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5314     gst_event_unref (event);
5315     goto done;
5316   }
5317 unknown_direction:
5318   {
5319     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5320     gst_event_unref (event);
5321     goto done;
5322   }
5323 flushed:
5324   {
5325     GST_DEBUG_OBJECT (pad, "We're flushing");
5326     GST_OBJECT_UNLOCK (pad);
5327     gst_event_unref (event);
5328     goto done;
5329   }
5330 eos:
5331   {
5332     GST_DEBUG_OBJECT (pad, "We're EOS");
5333     GST_OBJECT_UNLOCK (pad);
5334     goto done;
5335   }
5336 done:
5337   GST_TRACER_PAD_PUSH_EVENT_POST (pad, FALSE);
5338   return FALSE;
5339 }
5340
5341 /* Check if we can call the event function with the given event */
5342 static GstFlowReturn
5343 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5344 {
5345   GstCaps *caps;
5346
5347   switch (GST_EVENT_TYPE (event)) {
5348     case GST_EVENT_CAPS:
5349     {
5350       /* backwards compatibility mode for caps */
5351       gst_event_parse_caps (event, &caps);
5352
5353       if (!gst_pad_query_accept_caps (pad, caps))
5354         goto not_accepted;
5355       break;
5356     }
5357     default:
5358       break;
5359   }
5360   return GST_FLOW_OK;
5361
5362   /* ERRORS */
5363 not_accepted:
5364   {
5365     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5366         "caps %" GST_PTR_FORMAT " not accepted", caps);
5367     return GST_FLOW_NOT_NEGOTIATED;
5368   }
5369 }
5370
5371 static GstFlowReturn
5372 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5373     GstPadProbeType type)
5374 {
5375   GstFlowReturn ret;
5376   GstEventType event_type;
5377   gboolean serialized, need_unlock = FALSE, sticky;
5378   GstPadEventFunction eventfunc;
5379   GstObject *parent;
5380
5381   GST_OBJECT_LOCK (pad);
5382
5383   event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5384
5385   if (GST_PAD_IS_SINK (pad))
5386     serialized = GST_EVENT_IS_SERIALIZED (event);
5387   else
5388     serialized = FALSE;
5389   sticky = GST_EVENT_IS_STICKY (event);
5390   event_type = GST_EVENT_TYPE (event);
5391   switch (event_type) {
5392     case GST_EVENT_FLUSH_START:
5393       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5394           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5395
5396       /* can't even accept a flush begin event when flushing */
5397       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5398         goto flushing;
5399
5400       GST_PAD_SET_FLUSHING (pad);
5401       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5402       break;
5403     case GST_EVENT_FLUSH_STOP:
5404       /* we can't accept flush-stop on inactive pads else the flushing flag
5405        * would be cleared and it would look like the pad can accept data.
5406        * Also, some elements restart a streaming thread in flush-stop which we
5407        * can't allow on inactive pads */
5408       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5409         goto inactive;
5410
5411       GST_PAD_UNSET_FLUSHING (pad);
5412       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5413       /* Remove pending EOS events */
5414       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5415       remove_event_by_type (pad, GST_EVENT_EOS);
5416       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5417       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5418       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5419
5420       GST_OBJECT_UNLOCK (pad);
5421       /* grab stream lock */
5422       GST_PAD_STREAM_LOCK (pad);
5423       need_unlock = TRUE;
5424       GST_OBJECT_LOCK (pad);
5425       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5426         goto flushing;
5427       break;
5428     case GST_EVENT_RECONFIGURE:
5429       if (GST_PAD_IS_SRC (pad))
5430         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5431     default:
5432       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5433           "have event type %" GST_PTR_FORMAT, event);
5434
5435       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5436         goto flushing;
5437
5438       switch (event_type) {
5439         case GST_EVENT_STREAM_START:
5440           /* Remove sticky EOS events */
5441           GST_LOG_OBJECT (pad, "Removing pending EOS events");
5442           remove_event_by_type (pad, GST_EVENT_EOS);
5443           GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5444           break;
5445         default:
5446           break;
5447       }
5448
5449       if (serialized) {
5450         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5451           goto eos;
5452
5453         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5454         GST_OBJECT_UNLOCK (pad);
5455         GST_PAD_STREAM_LOCK (pad);
5456         need_unlock = TRUE;
5457         GST_OBJECT_LOCK (pad);
5458         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5459           goto flushing;
5460
5461         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5462           goto eos;
5463       }
5464       break;
5465   }
5466
5467   /* now do the probe */
5468   PROBE_PUSH (pad,
5469       type | GST_PAD_PROBE_TYPE_PUSH |
5470       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5471
5472   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5473
5474   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5475     goto no_function;
5476
5477   ACQUIRE_PARENT (pad, parent, no_parent);
5478   GST_OBJECT_UNLOCK (pad);
5479
5480   ret = pre_eventfunc_check (pad, event);
5481   if (G_UNLIKELY (ret != GST_FLOW_OK))
5482     goto precheck_failed;
5483
5484   if (sticky)
5485     gst_event_ref (event);
5486
5487   if (eventfunc (pad, parent, event)) {
5488     ret = GST_FLOW_OK;
5489   } else {
5490     /* something went wrong */
5491     switch (event_type) {
5492       case GST_EVENT_CAPS:
5493         ret = GST_FLOW_NOT_NEGOTIATED;
5494         break;
5495       default:
5496         ret = GST_FLOW_ERROR;
5497         break;
5498     }
5499   }
5500   RELEASE_PARENT (parent);
5501
5502   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5503
5504   if (sticky) {
5505     if (ret == GST_FLOW_OK) {
5506       GST_OBJECT_LOCK (pad);
5507       /* after the event function accepted the event, we can store the sticky
5508        * event on the pad */
5509       switch (store_sticky_event (pad, event)) {
5510         case GST_FLOW_FLUSHING:
5511           goto flushing;
5512         case GST_FLOW_EOS:
5513           goto eos;
5514         default:
5515           break;
5516       }
5517       GST_OBJECT_UNLOCK (pad);
5518     }
5519     gst_event_unref (event);
5520   }
5521
5522   if (need_unlock)
5523     GST_PAD_STREAM_UNLOCK (pad);
5524
5525   return ret;
5526
5527   /* ERROR handling */
5528 flushing:
5529   {
5530     GST_OBJECT_UNLOCK (pad);
5531     if (need_unlock)
5532       GST_PAD_STREAM_UNLOCK (pad);
5533     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5534         "Received event on flushing pad. Discarding");
5535     gst_event_unref (event);
5536     return GST_FLOW_FLUSHING;
5537   }
5538 inactive:
5539   {
5540     GST_OBJECT_UNLOCK (pad);
5541     if (need_unlock)
5542       GST_PAD_STREAM_UNLOCK (pad);
5543     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5544         "Received flush-stop on inactive pad. Discarding");
5545     gst_event_unref (event);
5546     return GST_FLOW_FLUSHING;
5547   }
5548 eos:
5549   {
5550     GST_OBJECT_UNLOCK (pad);
5551     if (need_unlock)
5552       GST_PAD_STREAM_UNLOCK (pad);
5553     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5554         "Received event on EOS pad. Discarding");
5555     gst_event_unref (event);
5556     return GST_FLOW_EOS;
5557   }
5558 probe_stopped:
5559   {
5560     GST_OBJECT_UNLOCK (pad);
5561     if (need_unlock)
5562       GST_PAD_STREAM_UNLOCK (pad);
5563     /* Only unref if unhandled */
5564     if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5565       gst_event_unref (event);
5566
5567     switch (ret) {
5568       case GST_FLOW_CUSTOM_SUCCESS_1:
5569       case GST_FLOW_CUSTOM_SUCCESS:
5570         GST_DEBUG_OBJECT (pad, "dropped or handled event");
5571         ret = GST_FLOW_OK;
5572         break;
5573       default:
5574         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5575         break;
5576     }
5577     return ret;
5578   }
5579 no_function:
5580   {
5581     g_warning ("pad %s:%s has no event handler, file a bug.",
5582         GST_DEBUG_PAD_NAME (pad));
5583     GST_OBJECT_UNLOCK (pad);
5584     if (need_unlock)
5585       GST_PAD_STREAM_UNLOCK (pad);
5586     gst_event_unref (event);
5587     return GST_FLOW_NOT_SUPPORTED;
5588   }
5589 no_parent:
5590   {
5591     GST_DEBUG_OBJECT (pad, "no parent");
5592     GST_OBJECT_UNLOCK (pad);
5593     if (need_unlock)
5594       GST_PAD_STREAM_UNLOCK (pad);
5595     gst_event_unref (event);
5596     return GST_FLOW_FLUSHING;
5597   }
5598 precheck_failed:
5599   {
5600     GST_DEBUG_OBJECT (pad, "pre event check failed");
5601     RELEASE_PARENT (parent);
5602     if (need_unlock)
5603       GST_PAD_STREAM_UNLOCK (pad);
5604     gst_event_unref (event);
5605     return ret;
5606   }
5607 }
5608
5609 /**
5610  * gst_pad_send_event:
5611  * @pad: a #GstPad to send the event to.
5612  * @event: (transfer full): the #GstEvent to send to the pad.
5613  *
5614  * Sends the event to the pad. This function can be used
5615  * by applications to send events in the pipeline.
5616  *
5617  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5618  * sink pad, @event should be a downstream event. For example, you would not
5619  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5620  * Furthermore, some downstream events have to be serialized with data flow,
5621  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5622  * the event needs to be serialized with data flow, this function will take the
5623  * pad's stream lock while calling its event function.
5624  *
5625  * To find out whether an event type is upstream, downstream, or downstream and
5626  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5627  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5628  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5629  * plugin doesn't need to bother itself with this information; the core handles
5630  * all necessary locks and checks.
5631  *
5632  * This function takes ownership of the provided event so you should
5633  * gst_event_ref() it if you want to reuse the event after this call.
5634  *
5635  * Returns: %TRUE if the event was handled.
5636  */
5637 gboolean
5638 gst_pad_send_event (GstPad * pad, GstEvent * event)
5639 {
5640   gboolean result;
5641   GstPadProbeType type;
5642
5643   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5644   g_return_val_if_fail (event != NULL, FALSE);
5645
5646   if (GST_PAD_IS_SINK (pad)) {
5647     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5648       goto wrong_direction;
5649     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5650   } else if (GST_PAD_IS_SRC (pad)) {
5651     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5652       goto wrong_direction;
5653     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5654   } else
5655     goto unknown_direction;
5656
5657   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5658     result = FALSE;
5659   else
5660     result = TRUE;
5661
5662   return result;
5663
5664   /* ERROR handling */
5665 wrong_direction:
5666   {
5667     g_warning ("pad %s:%s sending %s event in wrong direction",
5668         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5669     gst_event_unref (event);
5670     return FALSE;
5671   }
5672 unknown_direction:
5673   {
5674     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5675     gst_event_unref (event);
5676     return FALSE;
5677   }
5678 }
5679
5680 /**
5681  * gst_pad_set_element_private:
5682  * @pad: the #GstPad to set the private data of.
5683  * @priv: The private data to attach to the pad.
5684  *
5685  * Set the given private data gpointer on the pad.
5686  * This function can only be used by the element that owns the pad.
5687  * No locking is performed in this function.
5688  */
5689 void
5690 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5691 {
5692   pad->element_private = priv;
5693 }
5694
5695 /**
5696  * gst_pad_get_element_private:
5697  * @pad: the #GstPad to get the private data of.
5698  *
5699  * Gets the private data of a pad.
5700  * No locking is performed in this function.
5701  *
5702  * Returns: (transfer none): a #gpointer to the private data.
5703  */
5704 gpointer
5705 gst_pad_get_element_private (GstPad * pad)
5706 {
5707   return pad->element_private;
5708 }
5709
5710 /**
5711  * gst_pad_get_sticky_event:
5712  * @pad: the #GstPad to get the event from.
5713  * @event_type: the #GstEventType that should be retrieved.
5714  * @idx: the index of the event
5715  *
5716  * Returns a new reference of the sticky event of type @event_type
5717  * from the event.
5718  *
5719  * Returns: (transfer full) (nullable): a #GstEvent of type
5720  * @event_type or %NULL when no event of @event_type was on
5721  * @pad. Unref after usage.
5722  */
5723 GstEvent *
5724 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5725 {
5726   GstEvent *event = NULL;
5727   PadEvent *ev;
5728
5729   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5730   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5731
5732   GST_OBJECT_LOCK (pad);
5733   ev = find_event_by_type (pad, event_type, idx);
5734   if (ev && (event = ev->event))
5735     gst_event_ref (event);
5736   GST_OBJECT_UNLOCK (pad);
5737
5738   return event;
5739 }
5740
5741 typedef struct
5742 {
5743   GstPadStickyEventsForeachFunction func;
5744   gpointer user_data;
5745 } ForeachDispatch;
5746
5747 static gboolean
5748 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5749 {
5750   ForeachDispatch *data = user_data;
5751   gboolean ret = TRUE;
5752
5753   if (ev->event) {
5754     GST_OBJECT_UNLOCK (pad);
5755
5756     ret = data->func (pad, &ev->event, data->user_data);
5757
5758     GST_OBJECT_LOCK (pad);
5759   }
5760
5761   return ret;
5762 }
5763
5764 /**
5765  * gst_pad_sticky_events_foreach:
5766  * @pad: the #GstPad that should be used for iteration.
5767  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5768  *                should be called for every event.
5769  * @user_data: (closure): the optional user data.
5770  *
5771  * Iterates all sticky events on @pad and calls @foreach_func for every
5772  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5773  */
5774 void
5775 gst_pad_sticky_events_foreach (GstPad * pad,
5776     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5777 {
5778   ForeachDispatch data;
5779
5780   g_return_if_fail (GST_IS_PAD (pad));
5781   g_return_if_fail (foreach_func != NULL);
5782
5783   data.func = foreach_func;
5784   data.user_data = user_data;
5785
5786   GST_OBJECT_LOCK (pad);
5787   events_foreach (pad, foreach_dispatch_function, &data);
5788   GST_OBJECT_UNLOCK (pad);
5789 }
5790
5791 static void
5792 do_stream_status (GstPad * pad, GstStreamStatusType type,
5793     GThread * thread, GstTask * task)
5794 {
5795   GstElement *parent;
5796
5797   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5798
5799   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5800     if (GST_IS_ELEMENT (parent)) {
5801       GstMessage *message;
5802       GValue value = { 0 };
5803
5804       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5805         gchar *tname, *ename, *pname;
5806
5807         /* create a good task name */
5808         ename = gst_element_get_name (parent);
5809         pname = gst_pad_get_name (pad);
5810         tname = g_strdup_printf ("%s:%s", ename, pname);
5811         g_free (ename);
5812         g_free (pname);
5813
5814         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5815         g_free (tname);
5816       }
5817
5818       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5819           type, parent);
5820
5821       g_value_init (&value, GST_TYPE_TASK);
5822       g_value_set_object (&value, task);
5823       gst_message_set_stream_status_object (message, &value);
5824       g_value_unset (&value);
5825
5826       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5827       gst_element_post_message (parent, message);
5828     }
5829     gst_object_unref (parent);
5830   }
5831 }
5832
5833 static void
5834 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5835 {
5836   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5837       thread, task);
5838 }
5839
5840 static void
5841 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5842 {
5843   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5844       thread, task);
5845 }
5846
5847 /**
5848  * gst_pad_start_task:
5849  * @pad: the #GstPad to start the task of
5850  * @func: the task function to call
5851  * @user_data: user data passed to the task function
5852  * @notify: called when @user_data is no longer referenced
5853  *
5854  * Starts a task that repeatedly calls @func with @user_data. This function
5855  * is mostly used in pad activation functions to start the dataflow.
5856  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5857  * before @func is called.
5858  *
5859  * Returns: a %TRUE if the task could be started.
5860  */
5861 gboolean
5862 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5863     GDestroyNotify notify)
5864 {
5865   GstTask *task;
5866   gboolean res;
5867
5868   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5869   g_return_val_if_fail (func != NULL, FALSE);
5870
5871   GST_DEBUG_OBJECT (pad, "start task");
5872
5873   GST_OBJECT_LOCK (pad);
5874   task = GST_PAD_TASK (pad);
5875   if (task == NULL) {
5876     task = gst_task_new (func, user_data, notify);
5877     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5878     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5879     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5880     GST_INFO_OBJECT (pad, "created task %p", task);
5881     GST_PAD_TASK (pad) = task;
5882     gst_object_ref (task);
5883     /* release lock to post the message */
5884     GST_OBJECT_UNLOCK (pad);
5885
5886     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5887
5888     gst_object_unref (task);
5889
5890     GST_OBJECT_LOCK (pad);
5891     /* nobody else is supposed to have changed the pad now */
5892     if (GST_PAD_TASK (pad) != task)
5893       goto concurrent_stop;
5894   }
5895   res = gst_task_set_state (task, GST_TASK_STARTED);
5896   GST_OBJECT_UNLOCK (pad);
5897
5898   return res;
5899
5900   /* ERRORS */
5901 concurrent_stop:
5902   {
5903     GST_OBJECT_UNLOCK (pad);
5904     return TRUE;
5905   }
5906 }
5907
5908 /**
5909  * gst_pad_pause_task:
5910  * @pad: the #GstPad to pause the task of
5911  *
5912  * Pause the task of @pad. This function will also wait until the
5913  * function executed by the task is finished if this function is not
5914  * called from the task function.
5915  *
5916  * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5917  * has no task.
5918  */
5919 gboolean
5920 gst_pad_pause_task (GstPad * pad)
5921 {
5922   GstTask *task;
5923   gboolean res;
5924
5925   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5926
5927   GST_DEBUG_OBJECT (pad, "pause task");
5928
5929   GST_OBJECT_LOCK (pad);
5930   task = GST_PAD_TASK (pad);
5931   if (task == NULL)
5932     goto no_task;
5933   res = gst_task_set_state (task, GST_TASK_PAUSED);
5934   GST_OBJECT_UNLOCK (pad);
5935
5936   /* wait for task function to finish, this lock is recursive so it does nothing
5937    * when the pause is called from the task itself */
5938   GST_PAD_STREAM_LOCK (pad);
5939   GST_PAD_STREAM_UNLOCK (pad);
5940
5941   return res;
5942
5943 no_task:
5944   {
5945     GST_DEBUG_OBJECT (pad, "pad has no task");
5946     GST_OBJECT_UNLOCK (pad);
5947     return FALSE;
5948   }
5949 }
5950
5951 /**
5952  * gst_pad_stop_task:
5953  * @pad: the #GstPad to stop the task of
5954  *
5955  * Stop the task of @pad. This function will also make sure that the
5956  * function executed by the task will effectively stop if not called
5957  * from the GstTaskFunction.
5958  *
5959  * This function will deadlock if called from the GstTaskFunction of
5960  * the task. Use gst_task_pause() instead.
5961  *
5962  * Regardless of whether the pad has a task, the stream lock is acquired and
5963  * released so as to ensure that streaming through this pad has finished.
5964  *
5965  * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5966  */
5967 gboolean
5968 gst_pad_stop_task (GstPad * pad)
5969 {
5970   GstTask *task;
5971   gboolean res;
5972
5973   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5974
5975   GST_DEBUG_OBJECT (pad, "stop task");
5976
5977   GST_OBJECT_LOCK (pad);
5978   task = GST_PAD_TASK (pad);
5979   if (task == NULL)
5980     goto no_task;
5981   GST_PAD_TASK (pad) = NULL;
5982   res = gst_task_set_state (task, GST_TASK_STOPPED);
5983   GST_OBJECT_UNLOCK (pad);
5984
5985   GST_PAD_STREAM_LOCK (pad);
5986   GST_PAD_STREAM_UNLOCK (pad);
5987
5988   if (!gst_task_join (task))
5989     goto join_failed;
5990
5991   gst_object_unref (task);
5992
5993   return res;
5994
5995 no_task:
5996   {
5997     GST_DEBUG_OBJECT (pad, "no task");
5998     GST_OBJECT_UNLOCK (pad);
5999
6000     GST_PAD_STREAM_LOCK (pad);
6001     GST_PAD_STREAM_UNLOCK (pad);
6002
6003     /* this is not an error */
6004     return TRUE;
6005   }
6006 join_failed:
6007   {
6008     /* this is bad, possibly the application tried to join the task from
6009      * the task's thread. We install the task again so that it will be stopped
6010      * again from the right thread next time hopefully. */
6011     GST_OBJECT_LOCK (pad);
6012     GST_DEBUG_OBJECT (pad, "join failed");
6013     /* we can only install this task if there was no other task */
6014     if (GST_PAD_TASK (pad) == NULL)
6015       GST_PAD_TASK (pad) = task;
6016     GST_OBJECT_UNLOCK (pad);
6017
6018     return FALSE;
6019   }
6020 }
6021
6022 /**
6023  * gst_pad_probe_info_get_event:
6024  * @info: a #GstPadProbeInfo
6025  *
6026  * Returns: (transfer none): The #GstEvent from the probe
6027  */
6028
6029 GstEvent *
6030 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6031 {
6032   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6033           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6034
6035   return GST_PAD_PROBE_INFO_EVENT (info);
6036 }
6037
6038
6039 /**
6040  * gst_pad_probe_info_get_query:
6041  * @info: a #GstPadProbeInfo
6042  *
6043  * Returns: (transfer none): The #GstQuery from the probe
6044  */
6045
6046 GstQuery *
6047 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6048 {
6049   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6050           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6051
6052   return GST_PAD_PROBE_INFO_QUERY (info);
6053 }
6054
6055 /**
6056  * gst_pad_probe_info_get_buffer:
6057  * @info: a #GstPadProbeInfo
6058  *
6059  * Returns: (transfer none): The #GstBuffer from the probe
6060  */
6061
6062 GstBuffer *
6063 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6064 {
6065   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6066
6067   return GST_PAD_PROBE_INFO_BUFFER (info);
6068 }
6069
6070 /**
6071  * gst_pad_probe_info_get_bufferlist:
6072  * @info: a #GstPadProbeInfo
6073  *
6074  * Returns: (transfer none): The #GstBufferlist from the probe
6075  */
6076
6077 GstBufferList *
6078 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6079 {
6080   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6081
6082   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6083 }
6084
6085 /**
6086  * gst_pad_get_last_flow_return:
6087  * @pad: the #GstPad
6088  *
6089  * Gets the #GstFlowReturn return from the last data passed by this pad.
6090  *
6091  * Since: 1.4
6092  */
6093 GstFlowReturn
6094 gst_pad_get_last_flow_return (GstPad * pad)
6095 {
6096   GstFlowReturn ret;
6097
6098   GST_OBJECT_LOCK (pad);
6099   ret = GST_PAD_LAST_FLOW_RETURN (pad);
6100   GST_OBJECT_UNLOCK (pad);
6101
6102   return ret;
6103 }