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