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