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