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