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