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