pad: fix invalid unref after IDLE probe on non-OK flow return
[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   ACQUIRE_PARENT (pad, parent, no_parent);
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   RELEASE_PARENT (parent);
4061
4062   GST_PAD_STREAM_UNLOCK (pad);
4063
4064   return ret;
4065
4066   /* ERRORS */
4067 flushing:
4068   {
4069     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4070         "chaining, but pad was flushing");
4071     GST_OBJECT_UNLOCK (pad);
4072     GST_PAD_STREAM_UNLOCK (pad);
4073     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4074     return GST_FLOW_FLUSHING;
4075   }
4076 eos:
4077   {
4078     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4079     GST_OBJECT_UNLOCK (pad);
4080     GST_PAD_STREAM_UNLOCK (pad);
4081     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4082     return GST_FLOW_EOS;
4083   }
4084 wrong_mode:
4085   {
4086     g_critical ("chain on pad %s:%s but it was not in push mode",
4087         GST_DEBUG_PAD_NAME (pad));
4088     GST_OBJECT_UNLOCK (pad);
4089     GST_PAD_STREAM_UNLOCK (pad);
4090     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4091     return GST_FLOW_ERROR;
4092   }
4093 probe_stopped:
4094   {
4095     GST_OBJECT_UNLOCK (pad);
4096     GST_PAD_STREAM_UNLOCK (pad);
4097     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4098
4099     switch (ret) {
4100       case GST_FLOW_CUSTOM_SUCCESS:
4101         GST_DEBUG_OBJECT (pad, "dropped buffer");
4102         ret = GST_FLOW_OK;
4103         break;
4104       default:
4105         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4106         break;
4107     }
4108     return ret;
4109   }
4110 no_parent:
4111   {
4112     GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4113     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4114     GST_OBJECT_UNLOCK (pad);
4115     GST_PAD_STREAM_UNLOCK (pad);
4116     return GST_FLOW_FLUSHING;
4117   }
4118 no_function:
4119   {
4120     RELEASE_PARENT (parent);
4121     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4122     g_critical ("chain on pad %s:%s but it has no chainfunction",
4123         GST_DEBUG_PAD_NAME (pad));
4124     GST_PAD_STREAM_UNLOCK (pad);
4125     return GST_FLOW_NOT_SUPPORTED;
4126   }
4127 }
4128
4129 /**
4130  * gst_pad_chain:
4131  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4132  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4133  *     if not.
4134  *
4135  * Chain a buffer to @pad.
4136  *
4137  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4138  *
4139  * If the buffer type is not acceptable for @pad (as negotiated with a
4140  * preceding GST_EVENT_CAPS event), this function returns
4141  * #GST_FLOW_NOT_NEGOTIATED.
4142  *
4143  * The function proceeds calling the chain function installed on @pad (see
4144  * gst_pad_set_chain_function()) and the return value of that function is
4145  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4146  * chain function.
4147  *
4148  * In all cases, success or failure, the caller loses its reference to @buffer
4149  * after calling this function.
4150  *
4151  * Returns: a #GstFlowReturn from the pad.
4152  *
4153  * MT safe.
4154  */
4155 GstFlowReturn
4156 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4157 {
4158   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4159   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4160   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4161
4162   return gst_pad_chain_data_unchecked (pad,
4163       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4164 }
4165
4166 static GstFlowReturn
4167 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4168     GstBufferList * list)
4169 {
4170   guint i, len;
4171   GstBuffer *buffer;
4172   GstFlowReturn ret;
4173
4174   GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4175
4176   len = gst_buffer_list_length (list);
4177
4178   ret = GST_FLOW_OK;
4179   for (i = 0; i < len; i++) {
4180     buffer = gst_buffer_list_get (list, i);
4181     ret =
4182         gst_pad_chain_data_unchecked (pad,
4183         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4184         gst_buffer_ref (buffer));
4185     if (ret != GST_FLOW_OK)
4186       break;
4187   }
4188   gst_buffer_list_unref (list);
4189
4190   return ret;
4191 }
4192
4193 /**
4194  * gst_pad_chain_list:
4195  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4196  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4197  *     if not.
4198  *
4199  * Chain a bufferlist to @pad.
4200  *
4201  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4202  *
4203  * If @pad was not negotiated properly with a CAPS event, this function
4204  * returns #GST_FLOW_NOT_NEGOTIATED.
4205  *
4206  * The function proceeds calling the chainlist function installed on @pad (see
4207  * gst_pad_set_chain_list_function()) and the return value of that function is
4208  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4209  * chainlist function.
4210  *
4211  * In all cases, success or failure, the caller loses its reference to @list
4212  * after calling this function.
4213  *
4214  * MT safe.
4215  *
4216  * Returns: a #GstFlowReturn from the pad.
4217  */
4218 GstFlowReturn
4219 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4220 {
4221   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4222   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4223   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4224
4225   return gst_pad_chain_data_unchecked (pad,
4226       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4227 }
4228
4229 static GstFlowReturn
4230 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4231 {
4232   GstPad *peer;
4233   GstFlowReturn ret;
4234
4235   GST_OBJECT_LOCK (pad);
4236   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4237     goto flushing;
4238
4239   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4240     goto eos;
4241
4242   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4243     goto wrong_mode;
4244
4245 #ifndef G_DISABLE_ASSERT
4246   if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4247     if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4248       g_warning (G_STRLOC
4249           ":%s:<%s:%s> Got data flow before stream-start event",
4250           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4251     }
4252     if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4253       g_warning (G_STRLOC
4254           ":%s:<%s:%s> Got data flow before segment event",
4255           G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4256     }
4257     pad->priv->last_cookie = pad->priv->events_cookie;
4258   }
4259 #endif
4260
4261   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4262     goto events_error;
4263
4264   /* do block probes */
4265   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
4266
4267   /* recheck sticky events because the probe might have cause a relink */
4268   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4269     goto events_error;
4270
4271   /* do post-blocking probes */
4272   PROBE_PUSH (pad, type, data, probe_stopped);
4273
4274   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4275     goto not_linked;
4276
4277   /* take ref to peer pad before releasing the lock */
4278   gst_object_ref (peer);
4279   pad->priv->using++;
4280   GST_OBJECT_UNLOCK (pad);
4281
4282   ret = gst_pad_chain_data_unchecked (peer, type, data);
4283   data = NULL;
4284
4285   gst_object_unref (peer);
4286
4287   GST_OBJECT_LOCK (pad);
4288   pad->ABI.abi.last_flowret = ret;
4289   pad->priv->using--;
4290   if (pad->priv->using == 0) {
4291     /* pad is not active anymore, trigger idle callbacks */
4292     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4293         probe_stopped, ret);
4294   }
4295   GST_OBJECT_UNLOCK (pad);
4296
4297   return ret;
4298
4299   /* ERROR recovery here */
4300   /* ERRORS */
4301 flushing:
4302   {
4303     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4304         "pushing, but pad was flushing");
4305     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4306     GST_OBJECT_UNLOCK (pad);
4307     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4308     return GST_FLOW_FLUSHING;
4309   }
4310 eos:
4311   {
4312     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4313     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4314     GST_OBJECT_UNLOCK (pad);
4315     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4316     return GST_FLOW_EOS;
4317   }
4318 wrong_mode:
4319   {
4320     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4321         GST_DEBUG_PAD_NAME (pad));
4322     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4323     GST_OBJECT_UNLOCK (pad);
4324     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4325     return GST_FLOW_ERROR;
4326   }
4327 events_error:
4328   {
4329     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4330         "error pushing events, return %s", gst_flow_get_name (ret));
4331     pad->ABI.abi.last_flowret = ret;
4332     GST_OBJECT_UNLOCK (pad);
4333     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4334     return ret;
4335   }
4336 probe_stopped:
4337   {
4338     GST_OBJECT_UNLOCK (pad);
4339     pad->ABI.abi.last_flowret =
4340         ret == GST_FLOW_CUSTOM_SUCCESS ? GST_FLOW_OK : ret;
4341     if (data != NULL)
4342       gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4343
4344     switch (ret) {
4345       case GST_FLOW_CUSTOM_SUCCESS:
4346         GST_DEBUG_OBJECT (pad, "dropped buffer");
4347         ret = GST_FLOW_OK;
4348         break;
4349       default:
4350         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4351         break;
4352     }
4353     return ret;
4354   }
4355 not_linked:
4356   {
4357     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4358         "pushing, but it was not linked");
4359     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4360     GST_OBJECT_UNLOCK (pad);
4361     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4362     return GST_FLOW_NOT_LINKED;
4363   }
4364 }
4365
4366 /**
4367  * gst_pad_push:
4368  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4369  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4370  *     if not.
4371  *
4372  * Pushes a buffer to the peer of @pad.
4373  *
4374  * This function will call installed block probes before triggering any
4375  * installed data probes.
4376  *
4377  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4378  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4379  * be returned.
4380  *
4381  * In all cases, success or failure, the caller loses its reference to @buffer
4382  * after calling this function.
4383  *
4384  * Returns: a #GstFlowReturn from the peer pad.
4385  *
4386  * MT safe.
4387  */
4388 GstFlowReturn
4389 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4390 {
4391   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4392   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4393   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4394
4395   return gst_pad_push_data (pad,
4396       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4397 }
4398
4399 /**
4400  * gst_pad_push_list:
4401  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4402  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4403  *     if not.
4404  *
4405  * Pushes a buffer list to the peer of @pad.
4406  *
4407  * This function will call installed block probes before triggering any
4408  * installed data probes.
4409  *
4410  * The function proceeds calling the chain function on the peer pad and returns
4411  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4412  * be returned. If the peer pad does not have any installed chainlist function
4413  * every group buffer of the list will be merged into a normal #GstBuffer and
4414  * chained via gst_pad_chain().
4415  *
4416  * In all cases, success or failure, the caller loses its reference to @list
4417  * after calling this function.
4418  *
4419  * Returns: a #GstFlowReturn from the peer pad.
4420  *
4421  * MT safe.
4422  */
4423 GstFlowReturn
4424 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4425 {
4426   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4427   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4428   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4429
4430   return gst_pad_push_data (pad,
4431       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4432 }
4433
4434 static GstFlowReturn
4435 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4436     GstBuffer ** buffer)
4437 {
4438   GstFlowReturn ret;
4439   GstPadGetRangeFunction getrangefunc;
4440   GstObject *parent;
4441   GstBuffer *res_buf;
4442
4443   GST_PAD_STREAM_LOCK (pad);
4444
4445   GST_OBJECT_LOCK (pad);
4446   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4447     goto flushing;
4448
4449   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4450     goto wrong_mode;
4451
4452   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4453     goto events_error;
4454
4455   res_buf = *buffer;
4456
4457   /* when one of the probes returns DROPPED, probe_stopped will be called
4458    * and we skip calling the getrange function, res_buf should then contain a
4459    * valid result buffer */
4460   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4461       res_buf, offset, size, probe_stopped);
4462
4463   /* recheck sticky events because the probe might have cause a relink */
4464   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4465     goto events_error;
4466
4467   ACQUIRE_PARENT (pad, parent, no_parent);
4468   GST_OBJECT_UNLOCK (pad);
4469
4470   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4471     goto no_function;
4472
4473   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4474       "calling getrangefunc %s, offset %"
4475       G_GUINT64_FORMAT ", size %u",
4476       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4477
4478   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4479
4480   RELEASE_PARENT (parent);
4481
4482   GST_OBJECT_LOCK (pad);
4483   if (G_UNLIKELY (ret != GST_FLOW_OK))
4484     goto get_range_failed;
4485
4486   /* can only fire the signal if we have a valid buffer */
4487 probed_data:
4488   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4489       res_buf, offset, size, probe_stopped_unref);
4490   pad->ABI.abi.last_flowret = ret;
4491   GST_OBJECT_UNLOCK (pad);
4492
4493   GST_PAD_STREAM_UNLOCK (pad);
4494
4495   *buffer = res_buf;
4496
4497   return ret;
4498
4499   /* ERRORS */
4500 flushing:
4501   {
4502     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4503         "getrange, but pad was flushing");
4504     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4505     GST_OBJECT_UNLOCK (pad);
4506     GST_PAD_STREAM_UNLOCK (pad);
4507     return GST_FLOW_FLUSHING;
4508   }
4509 wrong_mode:
4510   {
4511     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4512         GST_DEBUG_PAD_NAME (pad));
4513     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4514     GST_OBJECT_UNLOCK (pad);
4515     GST_PAD_STREAM_UNLOCK (pad);
4516     return GST_FLOW_ERROR;
4517   }
4518 events_error:
4519   {
4520     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4521     pad->ABI.abi.last_flowret = ret;
4522     GST_OBJECT_UNLOCK (pad);
4523     GST_PAD_STREAM_UNLOCK (pad);
4524     return ret;
4525   }
4526 no_parent:
4527   {
4528     GST_DEBUG_OBJECT (pad, "no parent");
4529     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4530     GST_OBJECT_UNLOCK (pad);
4531     GST_PAD_STREAM_UNLOCK (pad);
4532     return GST_FLOW_FLUSHING;
4533   }
4534 no_function:
4535   {
4536     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4537         GST_DEBUG_PAD_NAME (pad));
4538     RELEASE_PARENT (parent);
4539     GST_PAD_STREAM_UNLOCK (pad);
4540     return GST_FLOW_NOT_SUPPORTED;
4541   }
4542 probe_stopped:
4543   {
4544     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4545         "probe returned %s", gst_flow_get_name (ret));
4546     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4547       if (res_buf) {
4548         /* the probe filled the buffer and asks us to not call the getrange
4549          * anymore, we continue with the post probes then. */
4550         GST_DEBUG_OBJECT (pad, "handled buffer");
4551         ret = GST_FLOW_OK;
4552         goto probed_data;
4553       } else {
4554         /* no buffer, we are EOS */
4555         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4556         ret = GST_FLOW_EOS;
4557       }
4558     }
4559     pad->ABI.abi.last_flowret = ret;
4560     GST_OBJECT_UNLOCK (pad);
4561     GST_PAD_STREAM_UNLOCK (pad);
4562
4563     return ret;
4564   }
4565 probe_stopped_unref:
4566   {
4567     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4568         "probe returned %s", gst_flow_get_name (ret));
4569     /* if we drop here, it signals EOS */
4570     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4571       ret = GST_FLOW_EOS;
4572     pad->ABI.abi.last_flowret = ret;
4573     GST_OBJECT_UNLOCK (pad);
4574     GST_PAD_STREAM_UNLOCK (pad);
4575     if (*buffer == NULL)
4576       gst_buffer_unref (res_buf);
4577     return ret;
4578   }
4579 get_range_failed:
4580   {
4581     pad->ABI.abi.last_flowret = ret;
4582     GST_OBJECT_UNLOCK (pad);
4583     GST_PAD_STREAM_UNLOCK (pad);
4584     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4585         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4586         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4587     return ret;
4588   }
4589 }
4590
4591 /**
4592  * gst_pad_get_range:
4593  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4594  * @offset: The start offset of the buffer
4595  * @size: The length of the buffer
4596  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4597  *     returns #GST_FLOW_ERROR if %NULL.
4598  *
4599  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4600  * immediately and @buffer is %NULL.
4601  *
4602  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4603  * description of a getrange function. If @pad has no getrange function
4604  * installed (see gst_pad_set_getrange_function()) this function returns
4605  * #GST_FLOW_NOT_SUPPORTED.
4606  *
4607  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4608  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4609  * must be freed with gst_buffer_unref() after usage.
4610  *
4611  * When @buffer points to a variable that points to a valid #GstBuffer, the
4612  * buffer will be filled with the result data when this function returns
4613  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4614  * @size bytes will be filled in the result buffer and its size will be updated
4615  * accordingly.
4616  *
4617  * Note that less than @size bytes can be returned in @buffer when, for example,
4618  * an EOS condition is near or when @buffer is not large enough to hold @size
4619  * bytes. The caller should check the result buffer size to get the result size.
4620  *
4621  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4622  * will be unchanged.
4623  *
4624  * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4625  *
4626  * Returns: a #GstFlowReturn from the pad.
4627  *
4628  * MT safe.
4629  */
4630 GstFlowReturn
4631 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4632     GstBuffer ** buffer)
4633 {
4634   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4635   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4636   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4637   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4638           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4639
4640   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4641 }
4642
4643 /**
4644  * gst_pad_pull_range:
4645  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4646  * @offset: The start offset of the buffer
4647  * @size: The length of the buffer
4648  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4649  *     GST_FLOW_ERROR if %NULL.
4650  *
4651  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4652  *
4653  * This function will first trigger the pad block signal if it was
4654  * installed.
4655  *
4656  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4657  * function returns the result of gst_pad_get_range() on the peer pad.
4658  * See gst_pad_get_range() for a list of return values and for the
4659  * semantics of the arguments of this function.
4660  *
4661  * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4662  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4663  * must be freed with gst_buffer_unref() after usage. When this function
4664  * returns any other result value, @buffer will still point to %NULL.
4665  *
4666  * When @buffer points to a variable that points to a valid #GstBuffer, the
4667  * buffer will be filled with the result data when this function returns
4668  * #GST_FLOW_OK. When this function returns any other result value,
4669  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4670  * @size bytes will be filled in the result buffer and its size will be updated
4671  * accordingly.
4672  *
4673  * Note that less than @size bytes can be returned in @buffer when, for example,
4674  * an EOS condition is near or when @buffer is not large enough to hold @size
4675  * bytes. The caller should check the result buffer size to get the result size.
4676  *
4677  * Returns: a #GstFlowReturn from the peer pad.
4678  *
4679  * MT safe.
4680  */
4681 GstFlowReturn
4682 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4683     GstBuffer ** buffer)
4684 {
4685   GstPad *peer;
4686   GstFlowReturn ret;
4687   GstBuffer *res_buf;
4688
4689   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4690   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4691   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4692   g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4693           && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4694
4695   GST_OBJECT_LOCK (pad);
4696   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4697     goto flushing;
4698
4699   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4700     goto wrong_mode;
4701
4702   res_buf = *buffer;
4703
4704   /* when one of the probes returns DROPPED, probe_stopped will be
4705    * called and we skip calling the peer getrange function. *buffer should then
4706    * contain a valid buffer */
4707   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4708       res_buf, offset, size, probe_stopped);
4709
4710   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4711     goto not_linked;
4712
4713   gst_object_ref (peer);
4714   pad->priv->using++;
4715   GST_OBJECT_UNLOCK (pad);
4716
4717   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4718
4719   gst_object_unref (peer);
4720
4721   GST_OBJECT_LOCK (pad);
4722   pad->priv->using--;
4723   pad->ABI.abi.last_flowret = ret;
4724   if (pad->priv->using == 0) {
4725     /* pad is not active anymore, trigger idle callbacks */
4726     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4727         probe_stopped_unref, ret);
4728   }
4729
4730   if (G_UNLIKELY (ret != GST_FLOW_OK))
4731     goto pull_range_failed;
4732
4733 probed_data:
4734   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4735       res_buf, offset, size, probe_stopped_unref);
4736
4737   GST_OBJECT_UNLOCK (pad);
4738
4739   *buffer = res_buf;
4740
4741   return ret;
4742
4743   /* ERROR recovery here */
4744 flushing:
4745   {
4746     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4747         "pullrange, but pad was flushing");
4748     pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4749     GST_OBJECT_UNLOCK (pad);
4750     return GST_FLOW_FLUSHING;
4751   }
4752 wrong_mode:
4753   {
4754     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4755         GST_DEBUG_PAD_NAME (pad));
4756     pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4757     GST_OBJECT_UNLOCK (pad);
4758     return GST_FLOW_ERROR;
4759   }
4760 probe_stopped:
4761   {
4762     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4763         gst_flow_get_name (ret));
4764     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4765       if (res_buf) {
4766         /* the probe filled the buffer and asks us to not forward to the peer
4767          * anymore, we continue with the post probes then */
4768         GST_DEBUG_OBJECT (pad, "handled buffer");
4769         ret = GST_FLOW_OK;
4770         goto probed_data;
4771       } else {
4772         /* no buffer, we are EOS then */
4773         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4774         ret = GST_FLOW_EOS;
4775       }
4776     }
4777     pad->ABI.abi.last_flowret = ret;
4778     GST_OBJECT_UNLOCK (pad);
4779     return ret;
4780   }
4781 not_linked:
4782   {
4783     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4784         "pulling range, but it was not linked");
4785     pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4786     GST_OBJECT_UNLOCK (pad);
4787     return GST_FLOW_NOT_LINKED;
4788   }
4789 pull_range_failed:
4790   {
4791     pad->ABI.abi.last_flowret = ret;
4792     GST_OBJECT_UNLOCK (pad);
4793     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4794         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4795         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4796     return ret;
4797   }
4798 probe_stopped_unref:
4799   {
4800     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4801         "post probe returned %s", gst_flow_get_name (ret));
4802
4803     /* if we drop here, it signals EOS */
4804     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4805       ret = GST_FLOW_EOS;
4806
4807     pad->ABI.abi.last_flowret = ret;
4808     GST_OBJECT_UNLOCK (pad);
4809
4810     if (*buffer == NULL)
4811       gst_buffer_unref (res_buf);
4812     return ret;
4813   }
4814 }
4815
4816 /* must be called with pad object lock */
4817 static GstFlowReturn
4818 store_sticky_event (GstPad * pad, GstEvent * event)
4819 {
4820   guint i, len;
4821   GstEventType type;
4822   GArray *events;
4823   gboolean res = FALSE;
4824   const gchar *name = NULL;
4825   gboolean insert = TRUE;
4826
4827   type = GST_EVENT_TYPE (event);
4828
4829   /* Store all sticky events except SEGMENT/EOS when we're flushing,
4830    * otherwise they can be dropped and nothing would ever resend them.
4831    * Only do that for activated pads though, everything else is a bug!
4832    */
4833   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4834           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4835                   || type == GST_EVENT_EOS))))
4836     goto flushed;
4837
4838   /* Unset the EOS flag when received STREAM_START event, so pad can
4839    * store sticky event and then push it later */
4840   if (type == GST_EVENT_STREAM_START) {
4841     GST_LOG_OBJECT (pad, "Removing pending EOS events");
4842     remove_event_by_type (pad, GST_EVENT_EOS);
4843     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4844   }
4845
4846   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4847     goto eos;
4848
4849   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4850     name = gst_structure_get_name (gst_event_get_structure (event));
4851
4852   events = pad->priv->events;
4853   len = events->len;
4854
4855   for (i = 0; i < len; i++) {
4856     PadEvent *ev = &g_array_index (events, PadEvent, i);
4857
4858     if (ev->event == NULL)
4859       continue;
4860
4861     if (type == GST_EVENT_TYPE (ev->event)) {
4862       /* matching types, check matching name if needed */
4863       if (name && !gst_event_has_name (ev->event, name))
4864         continue;
4865
4866       /* overwrite */
4867       if ((res = gst_event_replace (&ev->event, event)))
4868         ev->received = FALSE;
4869
4870       insert = FALSE;
4871       break;
4872     }
4873
4874     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4875             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4876       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4877        * storing the sticky ordered we can check that this is respected. */
4878       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4879               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4880         g_warning (G_STRLOC
4881             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4882             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4883             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4884             gst_event_type_get_name (type));
4885       break;
4886     }
4887   }
4888   if (insert) {
4889     PadEvent ev;
4890     ev.event = gst_event_ref (event);
4891     ev.received = FALSE;
4892     g_array_insert_val (events, i, ev);
4893     res = TRUE;
4894   }
4895
4896   if (res) {
4897     pad->priv->events_cookie++;
4898     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4899
4900     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4901
4902     switch (GST_EVENT_TYPE (event)) {
4903       case GST_EVENT_CAPS:
4904         GST_OBJECT_UNLOCK (pad);
4905
4906         GST_DEBUG_OBJECT (pad, "notify caps");
4907         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4908
4909         GST_OBJECT_LOCK (pad);
4910         break;
4911       default:
4912         break;
4913     }
4914   }
4915   if (type == GST_EVENT_EOS) {
4916     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4917     pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4918   }
4919
4920   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4921
4922   /* ERRORS */
4923 flushed:
4924   {
4925     GST_DEBUG_OBJECT (pad, "pad is flushing");
4926     return GST_FLOW_FLUSHING;
4927   }
4928 eos:
4929   {
4930     GST_DEBUG_OBJECT (pad, "pad is EOS");
4931     return GST_FLOW_EOS;
4932   }
4933 }
4934
4935 /**
4936  * gst_pad_store_sticky_event:
4937  * @pad: a #GstPad
4938  * @event: a #GstEvent
4939  *
4940  * Store the sticky @event on @pad
4941  *
4942  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
4943  * was flushing or #GST_FLOW_EOS when the pad was EOS.
4944  *
4945  * Since: 1.2
4946  */
4947 GstFlowReturn
4948 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4949 {
4950   GstFlowReturn ret;
4951
4952   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4953   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4954
4955   GST_OBJECT_LOCK (pad);
4956   ret = store_sticky_event (pad, event);
4957   GST_OBJECT_UNLOCK (pad);
4958
4959   return ret;
4960 }
4961
4962 static gboolean
4963 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
4964 {
4965   PushStickyData *data = user_data;
4966
4967   /* Forward all sticky events before our current one that are pending */
4968   if (ev->event != data->event
4969       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
4970     return push_sticky (pad, ev, data);
4971
4972   return TRUE;
4973 }
4974
4975 /* should be called with pad LOCK */
4976 static GstFlowReturn
4977 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4978     GstPadProbeType type)
4979 {
4980   GstFlowReturn ret;
4981   GstPad *peerpad;
4982   GstEventType event_type;
4983
4984   /* pass the adjusted event on. We need to do this even if
4985    * there is no peer pad because of the probes. */
4986   event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
4987
4988   /* Two checks to be made:
4989    * . (un)set the FLUSHING flag for flushing events,
4990    * . handle pad blocking */
4991   event_type = GST_EVENT_TYPE (event);
4992   switch (event_type) {
4993     case GST_EVENT_FLUSH_START:
4994       GST_PAD_SET_FLUSHING (pad);
4995
4996       GST_PAD_BLOCK_BROADCAST (pad);
4997       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4998       break;
4999     case GST_EVENT_FLUSH_STOP:
5000       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5001         goto inactive;
5002
5003       GST_PAD_UNSET_FLUSHING (pad);
5004
5005       /* Remove sticky EOS events */
5006       GST_LOG_OBJECT (pad, "Removing pending EOS events");
5007       remove_event_by_type (pad, GST_EVENT_EOS);
5008       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5009       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5010       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5011
5012       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5013       break;
5014     default:
5015     {
5016       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5017         goto flushed;
5018
5019       /* No need to check for EOS here as either the caller (gst_pad_push_event())
5020        * checked already or this is called as part of pushing sticky events,
5021        * in which case we still want to forward the EOS event downstream.
5022        */
5023
5024       switch (GST_EVENT_TYPE (event)) {
5025         case GST_EVENT_RECONFIGURE:
5026           if (GST_PAD_IS_SINK (pad))
5027             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5028           break;
5029         default:
5030           break;
5031       }
5032       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5033           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5034       break;
5035     }
5036   }
5037
5038   /* send probes after modifying the events above */
5039   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5040
5041   /* recheck sticky events because the probe might have cause a relink */
5042   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5043       && (GST_EVENT_IS_SERIALIZED (event)
5044           || GST_EVENT_IS_STICKY (event))) {
5045     PushStickyData data = { GST_FLOW_OK, FALSE, event };
5046     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5047
5048     /* Push all sticky events before our current one
5049      * that have changed */
5050     events_foreach (pad, sticky_changed, &data);
5051   }
5052
5053   /* now check the peer pad */
5054   peerpad = GST_PAD_PEER (pad);
5055   if (peerpad == NULL)
5056     goto not_linked;
5057
5058   gst_object_ref (peerpad);
5059   pad->priv->using++;
5060   GST_OBJECT_UNLOCK (pad);
5061
5062   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5063       event, gst_event_type_get_name (event_type), peerpad);
5064
5065   ret = gst_pad_send_event_unchecked (peerpad, event, type);
5066
5067   /* Note: we gave away ownership of the event at this point but we can still
5068    * print the old pointer */
5069   GST_LOG_OBJECT (pad,
5070       "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5071       gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5072
5073   gst_object_unref (peerpad);
5074
5075   GST_OBJECT_LOCK (pad);
5076   pad->priv->using--;
5077   if (pad->priv->using == 0) {
5078     /* pad is not active anymore, trigger idle callbacks */
5079     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5080         idle_probe_stopped, ret);
5081   }
5082   return ret;
5083
5084   /* ERROR handling */
5085 flushed:
5086   {
5087     GST_DEBUG_OBJECT (pad, "We're flushing");
5088     gst_event_unref (event);
5089     return GST_FLOW_FLUSHING;
5090   }
5091 inactive:
5092   {
5093     GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5094     gst_event_unref (event);
5095     return GST_FLOW_FLUSHING;
5096   }
5097 probe_stopped:
5098   {
5099     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5100     gst_event_unref (event);
5101
5102     switch (ret) {
5103       case GST_FLOW_CUSTOM_SUCCESS:
5104         GST_DEBUG_OBJECT (pad, "dropped event");
5105         break;
5106       default:
5107         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5108         break;
5109     }
5110     return ret;
5111   }
5112 not_linked:
5113   {
5114     GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5115         gst_event_type_get_name (GST_EVENT_TYPE (event)));
5116     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5117     gst_event_unref (event);
5118
5119     /* unlinked pads should not influence latency configuration */
5120     if (event_type == GST_EVENT_LATENCY)
5121       return GST_FLOW_OK;
5122
5123     return GST_FLOW_NOT_LINKED;
5124   }
5125 idle_probe_stopped:
5126   {
5127     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5128     return ret;
5129   }
5130 }
5131
5132 /**
5133  * gst_pad_push_event:
5134  * @pad: a #GstPad to push the event to.
5135  * @event: (transfer full): the #GstEvent to send to the pad.
5136  *
5137  * Sends the event to the peer of the given pad. This function is
5138  * mainly used by elements to send events to their peer
5139  * elements.
5140  *
5141  * This function takes ownership of the provided event so you should
5142  * gst_event_ref() it if you want to reuse the event after this call.
5143  *
5144  * Returns: %TRUE if the event was handled.
5145  *
5146  * MT safe.
5147  */
5148 gboolean
5149 gst_pad_push_event (GstPad * pad, GstEvent * event)
5150 {
5151   gboolean res = FALSE;
5152   GstPadProbeType type;
5153   gboolean sticky, serialized;
5154
5155   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5156   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5157
5158   if (GST_PAD_IS_SRC (pad)) {
5159     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5160       goto wrong_direction;
5161     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5162   } else if (GST_PAD_IS_SINK (pad)) {
5163     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5164       goto wrong_direction;
5165     /* events pushed on sinkpad never are sticky */
5166     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5167   } else
5168     goto unknown_direction;
5169
5170   GST_OBJECT_LOCK (pad);
5171   sticky = GST_EVENT_IS_STICKY (event);
5172   serialized = GST_EVENT_IS_SERIALIZED (event);
5173
5174   if (sticky) {
5175     /* srcpad sticky events are stored immediately, the received flag is set
5176      * to FALSE and will be set to TRUE when we can successfully push the
5177      * event to the peer pad */
5178     switch (store_sticky_event (pad, event)) {
5179       case GST_FLOW_FLUSHING:
5180         goto flushed;
5181       case GST_FLOW_EOS:
5182         goto eos;
5183       default:
5184         break;
5185     }
5186   }
5187   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5188     /* all serialized or sticky events on the srcpad trigger push of
5189      * sticky events */
5190     res = (check_sticky (pad, event) == GST_FLOW_OK);
5191   }
5192   if (!sticky) {
5193     GstFlowReturn ret;
5194
5195     /* other events are pushed right away */
5196     ret = gst_pad_push_event_unchecked (pad, event, type);
5197     /* dropped events by a probe are not an error */
5198     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
5199   } else {
5200     /* Errors in sticky event pushing are no problem and ignored here
5201      * as they will cause more meaningful errors during data flow.
5202      * For EOS events, that are not followed by data flow, we still
5203      * return FALSE here though.
5204      */
5205     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5206       res = TRUE;
5207     gst_event_unref (event);
5208   }
5209   GST_OBJECT_UNLOCK (pad);
5210
5211   return res;
5212
5213   /* ERROR handling */
5214 wrong_direction:
5215   {
5216     g_warning ("pad %s:%s pushing %s event in wrong direction",
5217         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5218     gst_event_unref (event);
5219     return FALSE;
5220   }
5221 unknown_direction:
5222   {
5223     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5224     gst_event_unref (event);
5225     return FALSE;
5226   }
5227 flushed:
5228   {
5229     GST_DEBUG_OBJECT (pad, "We're flushing");
5230     GST_OBJECT_UNLOCK (pad);
5231     gst_event_unref (event);
5232     return FALSE;
5233   }
5234 eos:
5235   {
5236     GST_DEBUG_OBJECT (pad, "We're EOS");
5237     GST_OBJECT_UNLOCK (pad);
5238     gst_event_unref (event);
5239     return FALSE;
5240   }
5241 }
5242
5243 /* Check if we can call the event function with the given event */
5244 static GstFlowReturn
5245 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5246 {
5247   GstCaps *caps;
5248
5249   switch (GST_EVENT_TYPE (event)) {
5250     case GST_EVENT_CAPS:
5251     {
5252       /* backwards compatibility mode for caps */
5253       gst_event_parse_caps (event, &caps);
5254
5255       if (!gst_pad_query_accept_caps (pad, caps))
5256         goto not_accepted;
5257       break;
5258     }
5259     default:
5260       break;
5261   }
5262   return GST_FLOW_OK;
5263
5264   /* ERRORS */
5265 not_accepted:
5266   {
5267     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5268         "caps %" GST_PTR_FORMAT " not accepted", caps);
5269     return GST_FLOW_NOT_NEGOTIATED;
5270   }
5271 }
5272
5273 static GstFlowReturn
5274 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5275     GstPadProbeType type)
5276 {
5277   GstFlowReturn ret;
5278   GstEventType event_type;
5279   gboolean serialized, need_unlock = FALSE, sticky;
5280   GstPadEventFunction eventfunc;
5281   GstObject *parent;
5282
5283   GST_OBJECT_LOCK (pad);
5284
5285   event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5286
5287   if (GST_PAD_IS_SINK (pad))
5288     serialized = GST_EVENT_IS_SERIALIZED (event);
5289   else
5290     serialized = FALSE;
5291   sticky = GST_EVENT_IS_STICKY (event);
5292   event_type = GST_EVENT_TYPE (event);
5293   switch (event_type) {
5294     case GST_EVENT_FLUSH_START:
5295       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5296           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5297
5298       /* can't even accept a flush begin event when flushing */
5299       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5300         goto flushing;
5301
5302       GST_PAD_SET_FLUSHING (pad);
5303       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5304       break;
5305     case GST_EVENT_FLUSH_STOP:
5306       /* we can't accept flush-stop on inactive pads else the flushing flag
5307        * would be cleared and it would look like the pad can accept data.
5308        * Also, some elements restart a streaming thread in flush-stop which we
5309        * can't allow on inactive pads */
5310       if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5311         goto inactive;
5312
5313       GST_PAD_UNSET_FLUSHING (pad);
5314       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5315       /* Remove pending EOS events */
5316       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5317       remove_event_by_type (pad, GST_EVENT_EOS);
5318       remove_event_by_type (pad, GST_EVENT_SEGMENT);
5319       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5320       pad->ABI.abi.last_flowret = GST_FLOW_OK;
5321
5322       GST_OBJECT_UNLOCK (pad);
5323       /* grab stream lock */
5324       GST_PAD_STREAM_LOCK (pad);
5325       need_unlock = TRUE;
5326       GST_OBJECT_LOCK (pad);
5327       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5328         goto flushing;
5329       break;
5330     case GST_EVENT_RECONFIGURE:
5331       if (GST_PAD_IS_SRC (pad))
5332         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5333     default:
5334       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5335           "have event type %" GST_PTR_FORMAT, event);
5336
5337       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5338         goto flushing;
5339
5340       switch (event_type) {
5341         case GST_EVENT_STREAM_START:
5342           /* Remove sticky EOS events */
5343           GST_LOG_OBJECT (pad, "Removing pending EOS events");
5344           remove_event_by_type (pad, GST_EVENT_EOS);
5345           GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5346           break;
5347         default:
5348           break;
5349       }
5350
5351       if (serialized) {
5352         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5353           goto eos;
5354
5355         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5356         GST_OBJECT_UNLOCK (pad);
5357         GST_PAD_STREAM_LOCK (pad);
5358         need_unlock = TRUE;
5359         GST_OBJECT_LOCK (pad);
5360         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5361           goto flushing;
5362
5363         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5364           goto eos;
5365       }
5366       break;
5367   }
5368
5369   /* now do the probe */
5370   PROBE_PUSH (pad,
5371       type | GST_PAD_PROBE_TYPE_PUSH |
5372       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5373
5374   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5375
5376   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5377     goto no_function;
5378
5379   ACQUIRE_PARENT (pad, parent, no_parent);
5380   GST_OBJECT_UNLOCK (pad);
5381
5382   ret = pre_eventfunc_check (pad, event);
5383   if (G_UNLIKELY (ret != GST_FLOW_OK))
5384     goto precheck_failed;
5385
5386   if (sticky)
5387     gst_event_ref (event);
5388
5389   if (eventfunc (pad, parent, event)) {
5390     ret = GST_FLOW_OK;
5391   } else {
5392     /* something went wrong */
5393     switch (event_type) {
5394       case GST_EVENT_CAPS:
5395         ret = GST_FLOW_NOT_NEGOTIATED;
5396         break;
5397       default:
5398         ret = GST_FLOW_ERROR;
5399         break;
5400     }
5401   }
5402   RELEASE_PARENT (parent);
5403
5404   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5405
5406   if (sticky) {
5407     if (ret == GST_FLOW_OK) {
5408       GST_OBJECT_LOCK (pad);
5409       /* after the event function accepted the event, we can store the sticky
5410        * event on the pad */
5411       switch (store_sticky_event (pad, event)) {
5412         case GST_FLOW_FLUSHING:
5413           goto flushing;
5414         case GST_FLOW_EOS:
5415           goto eos;
5416         default:
5417           break;
5418       }
5419       GST_OBJECT_UNLOCK (pad);
5420     }
5421     gst_event_unref (event);
5422   }
5423
5424   if (need_unlock)
5425     GST_PAD_STREAM_UNLOCK (pad);
5426
5427   return ret;
5428
5429   /* ERROR handling */
5430 flushing:
5431   {
5432     GST_OBJECT_UNLOCK (pad);
5433     if (need_unlock)
5434       GST_PAD_STREAM_UNLOCK (pad);
5435     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5436         "Received event on flushing pad. Discarding");
5437     gst_event_unref (event);
5438     return GST_FLOW_FLUSHING;
5439   }
5440 inactive:
5441   {
5442     GST_OBJECT_UNLOCK (pad);
5443     if (need_unlock)
5444       GST_PAD_STREAM_UNLOCK (pad);
5445     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5446         "Received flush-stop on inactive pad. Discarding");
5447     gst_event_unref (event);
5448     return GST_FLOW_FLUSHING;
5449   }
5450 eos:
5451   {
5452     GST_OBJECT_UNLOCK (pad);
5453     if (need_unlock)
5454       GST_PAD_STREAM_UNLOCK (pad);
5455     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5456         "Received event on EOS pad. Discarding");
5457     gst_event_unref (event);
5458     return GST_FLOW_EOS;
5459   }
5460 probe_stopped:
5461   {
5462     GST_OBJECT_UNLOCK (pad);
5463     if (need_unlock)
5464       GST_PAD_STREAM_UNLOCK (pad);
5465     gst_event_unref (event);
5466
5467     switch (ret) {
5468       case GST_FLOW_CUSTOM_SUCCESS:
5469         GST_DEBUG_OBJECT (pad, "dropped event");
5470         ret = GST_FLOW_OK;
5471         break;
5472       default:
5473         GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5474         break;
5475     }
5476     return ret;
5477   }
5478 no_function:
5479   {
5480     g_warning ("pad %s:%s has no event handler, file a bug.",
5481         GST_DEBUG_PAD_NAME (pad));
5482     GST_OBJECT_UNLOCK (pad);
5483     if (need_unlock)
5484       GST_PAD_STREAM_UNLOCK (pad);
5485     gst_event_unref (event);
5486     return GST_FLOW_NOT_SUPPORTED;
5487   }
5488 no_parent:
5489   {
5490     GST_DEBUG_OBJECT (pad, "no parent");
5491     GST_OBJECT_UNLOCK (pad);
5492     if (need_unlock)
5493       GST_PAD_STREAM_UNLOCK (pad);
5494     gst_event_unref (event);
5495     return GST_FLOW_FLUSHING;
5496   }
5497 precheck_failed:
5498   {
5499     GST_DEBUG_OBJECT (pad, "pre event check failed");
5500     RELEASE_PARENT (parent);
5501     if (need_unlock)
5502       GST_PAD_STREAM_UNLOCK (pad);
5503     gst_event_unref (event);
5504     return ret;
5505   }
5506 }
5507
5508 /**
5509  * gst_pad_send_event:
5510  * @pad: a #GstPad to send the event to.
5511  * @event: (transfer full): the #GstEvent to send to the pad.
5512  *
5513  * Sends the event to the pad. This function can be used
5514  * by applications to send events in the pipeline.
5515  *
5516  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5517  * sink pad, @event should be a downstream event. For example, you would not
5518  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5519  * Furthermore, some downstream events have to be serialized with data flow,
5520  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5521  * the event needs to be serialized with data flow, this function will take the
5522  * pad's stream lock while calling its event function.
5523  *
5524  * To find out whether an event type is upstream, downstream, or downstream and
5525  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5526  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5527  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5528  * plugin doesn't need to bother itself with this information; the core handles
5529  * all necessary locks and checks.
5530  *
5531  * This function takes ownership of the provided event so you should
5532  * gst_event_ref() it if you want to reuse the event after this call.
5533  *
5534  * Returns: %TRUE if the event was handled.
5535  */
5536 gboolean
5537 gst_pad_send_event (GstPad * pad, GstEvent * event)
5538 {
5539   gboolean result;
5540   GstPadProbeType type;
5541
5542   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5543   g_return_val_if_fail (event != NULL, FALSE);
5544
5545   if (GST_PAD_IS_SINK (pad)) {
5546     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5547       goto wrong_direction;
5548     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5549   } else if (GST_PAD_IS_SRC (pad)) {
5550     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5551       goto wrong_direction;
5552     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5553   } else
5554     goto unknown_direction;
5555
5556   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5557     result = FALSE;
5558   else
5559     result = TRUE;
5560
5561   return result;
5562
5563   /* ERROR handling */
5564 wrong_direction:
5565   {
5566     g_warning ("pad %s:%s sending %s event in wrong direction",
5567         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5568     gst_event_unref (event);
5569     return FALSE;
5570   }
5571 unknown_direction:
5572   {
5573     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5574     gst_event_unref (event);
5575     return FALSE;
5576   }
5577 }
5578
5579 /**
5580  * gst_pad_set_element_private:
5581  * @pad: the #GstPad to set the private data of.
5582  * @priv: The private data to attach to the pad.
5583  *
5584  * Set the given private data gpointer on the pad.
5585  * This function can only be used by the element that owns the pad.
5586  * No locking is performed in this function.
5587  */
5588 void
5589 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5590 {
5591   pad->element_private = priv;
5592 }
5593
5594 /**
5595  * gst_pad_get_element_private:
5596  * @pad: the #GstPad to get the private data of.
5597  *
5598  * Gets the private data of a pad.
5599  * No locking is performed in this function.
5600  *
5601  * Returns: (transfer none): a #gpointer to the private data.
5602  */
5603 gpointer
5604 gst_pad_get_element_private (GstPad * pad)
5605 {
5606   return pad->element_private;
5607 }
5608
5609 /**
5610  * gst_pad_get_sticky_event:
5611  * @pad: the #GstPad to get the event from.
5612  * @event_type: the #GstEventType that should be retrieved.
5613  * @idx: the index of the event
5614  *
5615  * Returns a new reference of the sticky event of type @event_type
5616  * from the event.
5617  *
5618  * Returns: (transfer full) (nullable): a #GstEvent of type
5619  * @event_type or %NULL when no event of @event_type was on
5620  * @pad. Unref after usage.
5621  */
5622 GstEvent *
5623 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5624 {
5625   GstEvent *event = NULL;
5626   PadEvent *ev;
5627
5628   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5629   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5630
5631   GST_OBJECT_LOCK (pad);
5632   ev = find_event_by_type (pad, event_type, idx);
5633   if (ev && (event = ev->event))
5634     gst_event_ref (event);
5635   GST_OBJECT_UNLOCK (pad);
5636
5637   return event;
5638 }
5639
5640 typedef struct
5641 {
5642   GstPadStickyEventsForeachFunction func;
5643   gpointer user_data;
5644 } ForeachDispatch;
5645
5646 static gboolean
5647 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5648 {
5649   ForeachDispatch *data = user_data;
5650   gboolean ret = TRUE;
5651
5652   if (ev->event) {
5653     GST_OBJECT_UNLOCK (pad);
5654
5655     ret = data->func (pad, &ev->event, data->user_data);
5656
5657     GST_OBJECT_LOCK (pad);
5658   }
5659
5660   return ret;
5661 }
5662
5663 /**
5664  * gst_pad_sticky_events_foreach:
5665  * @pad: the #GstPad that should be used for iteration.
5666  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5667  *                should be called for every event.
5668  * @user_data: (closure): the optional user data.
5669  *
5670  * Iterates all sticky events on @pad and calls @foreach_func for every
5671  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5672  */
5673 void
5674 gst_pad_sticky_events_foreach (GstPad * pad,
5675     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5676 {
5677   ForeachDispatch data;
5678
5679   g_return_if_fail (GST_IS_PAD (pad));
5680   g_return_if_fail (foreach_func != NULL);
5681
5682   data.func = foreach_func;
5683   data.user_data = user_data;
5684
5685   GST_OBJECT_LOCK (pad);
5686   events_foreach (pad, foreach_dispatch_function, &data);
5687   GST_OBJECT_UNLOCK (pad);
5688 }
5689
5690 static void
5691 do_stream_status (GstPad * pad, GstStreamStatusType type,
5692     GThread * thread, GstTask * task)
5693 {
5694   GstElement *parent;
5695
5696   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5697
5698   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5699     if (GST_IS_ELEMENT (parent)) {
5700       GstMessage *message;
5701       GValue value = { 0 };
5702
5703       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5704         gchar *tname, *ename, *pname;
5705
5706         /* create a good task name */
5707         ename = gst_element_get_name (parent);
5708         pname = gst_pad_get_name (pad);
5709         tname = g_strdup_printf ("%s:%s", ename, pname);
5710         g_free (ename);
5711         g_free (pname);
5712
5713         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5714         g_free (tname);
5715       }
5716
5717       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5718           type, parent);
5719
5720       g_value_init (&value, GST_TYPE_TASK);
5721       g_value_set_object (&value, task);
5722       gst_message_set_stream_status_object (message, &value);
5723       g_value_unset (&value);
5724
5725       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5726       gst_element_post_message (parent, message);
5727     }
5728     gst_object_unref (parent);
5729   }
5730 }
5731
5732 static void
5733 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5734 {
5735   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5736       thread, task);
5737 }
5738
5739 static void
5740 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5741 {
5742   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5743       thread, task);
5744 }
5745
5746 /**
5747  * gst_pad_start_task:
5748  * @pad: the #GstPad to start the task of
5749  * @func: the task function to call
5750  * @user_data: user data passed to the task function
5751  * @notify: called when @user_data is no longer referenced
5752  *
5753  * Starts a task that repeatedly calls @func with @user_data. This function
5754  * is mostly used in pad activation functions to start the dataflow.
5755  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5756  * before @func is called.
5757  *
5758  * Returns: a %TRUE if the task could be started.
5759  */
5760 gboolean
5761 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5762     GDestroyNotify notify)
5763 {
5764   GstTask *task;
5765   gboolean res;
5766
5767   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5768   g_return_val_if_fail (func != NULL, FALSE);
5769
5770   GST_DEBUG_OBJECT (pad, "start task");
5771
5772   GST_OBJECT_LOCK (pad);
5773   task = GST_PAD_TASK (pad);
5774   if (task == NULL) {
5775     task = gst_task_new (func, user_data, notify);
5776     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5777     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5778     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5779     GST_INFO_OBJECT (pad, "created task %p", task);
5780     GST_PAD_TASK (pad) = task;
5781     gst_object_ref (task);
5782     /* release lock to post the message */
5783     GST_OBJECT_UNLOCK (pad);
5784
5785     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5786
5787     gst_object_unref (task);
5788
5789     GST_OBJECT_LOCK (pad);
5790     /* nobody else is supposed to have changed the pad now */
5791     if (GST_PAD_TASK (pad) != task)
5792       goto concurrent_stop;
5793   }
5794   res = gst_task_set_state (task, GST_TASK_STARTED);
5795   GST_OBJECT_UNLOCK (pad);
5796
5797   return res;
5798
5799   /* ERRORS */
5800 concurrent_stop:
5801   {
5802     GST_OBJECT_UNLOCK (pad);
5803     return TRUE;
5804   }
5805 }
5806
5807 /**
5808  * gst_pad_pause_task:
5809  * @pad: the #GstPad to pause the task of
5810  *
5811  * Pause the task of @pad. This function will also wait until the
5812  * function executed by the task is finished if this function is not
5813  * called from the task function.
5814  *
5815  * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5816  * has no task.
5817  */
5818 gboolean
5819 gst_pad_pause_task (GstPad * pad)
5820 {
5821   GstTask *task;
5822   gboolean res;
5823
5824   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5825
5826   GST_DEBUG_OBJECT (pad, "pause task");
5827
5828   GST_OBJECT_LOCK (pad);
5829   task = GST_PAD_TASK (pad);
5830   if (task == NULL)
5831     goto no_task;
5832   res = gst_task_set_state (task, GST_TASK_PAUSED);
5833   GST_OBJECT_UNLOCK (pad);
5834
5835   /* wait for task function to finish, this lock is recursive so it does nothing
5836    * when the pause is called from the task itself */
5837   GST_PAD_STREAM_LOCK (pad);
5838   GST_PAD_STREAM_UNLOCK (pad);
5839
5840   return res;
5841
5842 no_task:
5843   {
5844     GST_DEBUG_OBJECT (pad, "pad has no task");
5845     GST_OBJECT_UNLOCK (pad);
5846     return FALSE;
5847   }
5848 }
5849
5850 /**
5851  * gst_pad_stop_task:
5852  * @pad: the #GstPad to stop the task of
5853  *
5854  * Stop the task of @pad. This function will also make sure that the
5855  * function executed by the task will effectively stop if not called
5856  * from the GstTaskFunction.
5857  *
5858  * This function will deadlock if called from the GstTaskFunction of
5859  * the task. Use gst_task_pause() instead.
5860  *
5861  * Regardless of whether the pad has a task, the stream lock is acquired and
5862  * released so as to ensure that streaming through this pad has finished.
5863  *
5864  * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5865  */
5866 gboolean
5867 gst_pad_stop_task (GstPad * pad)
5868 {
5869   GstTask *task;
5870   gboolean res;
5871
5872   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5873
5874   GST_DEBUG_OBJECT (pad, "stop task");
5875
5876   GST_OBJECT_LOCK (pad);
5877   task = GST_PAD_TASK (pad);
5878   if (task == NULL)
5879     goto no_task;
5880   GST_PAD_TASK (pad) = NULL;
5881   res = gst_task_set_state (task, GST_TASK_STOPPED);
5882   GST_OBJECT_UNLOCK (pad);
5883
5884   GST_PAD_STREAM_LOCK (pad);
5885   GST_PAD_STREAM_UNLOCK (pad);
5886
5887   if (!gst_task_join (task))
5888     goto join_failed;
5889
5890   gst_object_unref (task);
5891
5892   return res;
5893
5894 no_task:
5895   {
5896     GST_DEBUG_OBJECT (pad, "no task");
5897     GST_OBJECT_UNLOCK (pad);
5898
5899     GST_PAD_STREAM_LOCK (pad);
5900     GST_PAD_STREAM_UNLOCK (pad);
5901
5902     /* this is not an error */
5903     return TRUE;
5904   }
5905 join_failed:
5906   {
5907     /* this is bad, possibly the application tried to join the task from
5908      * the task's thread. We install the task again so that it will be stopped
5909      * again from the right thread next time hopefully. */
5910     GST_OBJECT_LOCK (pad);
5911     GST_DEBUG_OBJECT (pad, "join failed");
5912     /* we can only install this task if there was no other task */
5913     if (GST_PAD_TASK (pad) == NULL)
5914       GST_PAD_TASK (pad) = task;
5915     GST_OBJECT_UNLOCK (pad);
5916
5917     return FALSE;
5918   }
5919 }
5920
5921 /**
5922  * gst_pad_probe_info_get_event:
5923  * @info: a #GstPadProbeInfo
5924  *
5925  * Returns: (transfer none): The #GstEvent from the probe
5926  */
5927
5928 GstEvent *
5929 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5930 {
5931   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5932           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5933
5934   return GST_PAD_PROBE_INFO_EVENT (info);
5935 }
5936
5937
5938 /**
5939  * gst_pad_probe_info_get_query:
5940  * @info: a #GstPadProbeInfo
5941  *
5942  * Returns: (transfer none): The #GstQuery from the probe
5943  */
5944
5945 GstQuery *
5946 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5947 {
5948   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5949           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5950
5951   return GST_PAD_PROBE_INFO_QUERY (info);
5952 }
5953
5954 /**
5955  * gst_pad_probe_info_get_buffer:
5956  * @info: a #GstPadProbeInfo
5957  *
5958  * Returns: (transfer none): The #GstBuffer from the probe
5959  */
5960
5961 GstBuffer *
5962 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5963 {
5964   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5965
5966   return GST_PAD_PROBE_INFO_BUFFER (info);
5967 }
5968
5969 /**
5970  * gst_pad_probe_info_get_bufferlist:
5971  * @info: a #GstPadProbeInfo
5972  *
5973  * Returns: (transfer none): The #GstBufferlist from the probe
5974  */
5975
5976 GstBufferList *
5977 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5978 {
5979   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5980
5981   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5982 }
5983
5984 /**
5985  * gst_pad_get_last_flow_return:
5986  * @pad: the #GstPad
5987  *
5988  * Gets the #GstFlowReturn return from the last data passed by this pad.
5989  *
5990  * Since: 1.4
5991  */
5992 GstFlowReturn
5993 gst_pad_get_last_flow_return (GstPad * pad)
5994 {
5995   GstFlowReturn ret;
5996
5997   GST_OBJECT_LOCK (pad);
5998   ret = GST_PAD_LAST_FLOW_RETURN (pad);
5999   GST_OBJECT_UNLOCK (pad);
6000
6001   return ret;
6002 }