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