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