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