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