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