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