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