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