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