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