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