pad: Add a filter to the caps_query done by acceptcaps
[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   gst_query_parse_accept_caps (query, &caps);
2824   allowed = gst_pad_query_caps (pad, 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 compatible 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
3290   /* If called for an event this is
3291    * the event that would be pushed
3292    * next. Don't forward sticky events
3293    * that would come after that */
3294   GstEvent *event;
3295 } PushStickyData;
3296
3297 /* should be called with pad LOCK */
3298 static gboolean
3299 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3300 {
3301   PushStickyData *data = user_data;
3302   GstEvent *event = ev->event;
3303
3304   if (ev->received) {
3305     GST_DEBUG_OBJECT (pad, "event %s was already received",
3306         GST_EVENT_TYPE_NAME (event));
3307     return TRUE;
3308   }
3309
3310   /* If we're called because of an sticky event, only forward
3311    * events that would come before this new event and the
3312    * event itself */
3313   if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3314       GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3315       GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3316     data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3317   } else {
3318     data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3319         GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3320   }
3321
3322   switch (data->ret) {
3323     case GST_FLOW_OK:
3324       ev->received = TRUE;
3325       GST_DEBUG_OBJECT (pad, "event %s marked received",
3326           GST_EVENT_TYPE_NAME (event));
3327       break;
3328     case GST_FLOW_CUSTOM_SUCCESS:
3329       /* we can't assume the event is received when it was dropped */
3330       GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3331           GST_EVENT_TYPE_NAME (event));
3332       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3333       data->ret = GST_FLOW_OK;
3334       break;
3335     case GST_FLOW_CUSTOM_SUCCESS_1:
3336       /* event was ignored and should be sent later */
3337       GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3338           GST_EVENT_TYPE_NAME (event));
3339       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3340       data->ret = GST_FLOW_OK;
3341       break;
3342     case GST_FLOW_NOT_LINKED:
3343       /* not linked is not a problem, we are sticky so the event will be
3344        * sent later but only for non-EOS events */
3345       GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3346       if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3347         data->ret = GST_FLOW_OK;
3348       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3349       break;
3350     default:
3351       GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3352           gst_flow_get_name (data->ret));
3353       GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3354       break;
3355   }
3356
3357   if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3358     data->was_eos = TRUE;
3359
3360   return data->ret == GST_FLOW_OK;
3361 }
3362
3363 /* check sticky events and push them when needed. should be called
3364  * with pad LOCK */
3365 static inline GstFlowReturn
3366 check_sticky (GstPad * pad, GstEvent * event)
3367 {
3368   PushStickyData data = { GST_FLOW_OK, FALSE, event };
3369
3370   if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3371     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3372
3373     GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3374     events_foreach (pad, push_sticky, &data);
3375
3376     /* If there's an EOS event we must push it downstream
3377      * even if sending a previous sticky event failed.
3378      * Otherwise the pipeline might wait forever for EOS.
3379      *
3380      * Only do this if pushing another event than the EOS
3381      * event failed.
3382      */
3383     if (data.ret != GST_FLOW_OK && !data.was_eos) {
3384       PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3385
3386       if (ev && !ev->received) {
3387         data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3388             GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3389         /* the event could have been dropped. Because this can only
3390          * happen if the user asked for it, it's not an error */
3391         if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3392           data.ret = GST_FLOW_OK;
3393       }
3394     }
3395   }
3396   return data.ret;
3397 }
3398
3399
3400 /**
3401  * gst_pad_query:
3402  * @pad: a #GstPad to invoke the default query on.
3403  * @query: (transfer none): the #GstQuery to perform.
3404  *
3405  * Dispatches a query to a pad. The query should have been allocated by the
3406  * caller via one of the type-specific allocation functions. The element that
3407  * the pad belongs to is responsible for filling the query with an appropriate
3408  * response, which should then be parsed with a type-specific query parsing
3409  * function.
3410  *
3411  * Again, the caller is responsible for both the allocation and deallocation of
3412  * the query structure.
3413  *
3414  * Please also note that some queries might need a running pipeline to work.
3415  *
3416  * Returns: TRUE if the query could be performed.
3417  */
3418 gboolean
3419 gst_pad_query (GstPad * pad, GstQuery * query)
3420 {
3421   GstObject *parent;
3422   gboolean res, serialized;
3423   GstPadQueryFunction func;
3424   GstPadProbeType type;
3425   GstFlowReturn ret;
3426
3427   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3428   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3429
3430   if (GST_PAD_IS_SRC (pad)) {
3431     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3432       goto wrong_direction;
3433     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3434   } else if (GST_PAD_IS_SINK (pad)) {
3435     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3436       goto wrong_direction;
3437     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3438   } else
3439     goto unknown_direction;
3440
3441   GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3442       GST_QUERY_TYPE_NAME (query));
3443
3444   serialized = GST_QUERY_IS_SERIALIZED (query);
3445   if (G_UNLIKELY (serialized))
3446     GST_PAD_STREAM_LOCK (pad);
3447
3448   GST_OBJECT_LOCK (pad);
3449   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3450       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3451   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3452
3453   ACQUIRE_PARENT (pad, parent, no_parent);
3454   GST_OBJECT_UNLOCK (pad);
3455
3456   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3457     goto no_func;
3458
3459   res = func (pad, parent, query);
3460
3461   RELEASE_PARENT (parent);
3462
3463   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3464       GST_QUERY_TYPE_NAME (query), res);
3465
3466   if (res != TRUE)
3467     goto query_failed;
3468
3469   GST_OBJECT_LOCK (pad);
3470   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3471   GST_OBJECT_UNLOCK (pad);
3472
3473   if (G_UNLIKELY (serialized))
3474     GST_PAD_STREAM_UNLOCK (pad);
3475
3476   return res;
3477
3478   /* ERRORS */
3479 wrong_direction:
3480   {
3481     g_warning ("pad %s:%s query %s in wrong direction",
3482         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3483     return FALSE;
3484   }
3485 unknown_direction:
3486   {
3487     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3488     return FALSE;
3489   }
3490 no_parent:
3491   {
3492     GST_DEBUG_OBJECT (pad, "had no parent");
3493     GST_OBJECT_UNLOCK (pad);
3494     if (G_UNLIKELY (serialized))
3495       GST_PAD_STREAM_UNLOCK (pad);
3496     return FALSE;
3497   }
3498 no_func:
3499   {
3500     GST_DEBUG_OBJECT (pad, "had no query function");
3501     RELEASE_PARENT (parent);
3502     if (G_UNLIKELY (serialized))
3503       GST_PAD_STREAM_UNLOCK (pad);
3504     return FALSE;
3505   }
3506 query_failed:
3507   {
3508     GST_DEBUG_OBJECT (pad, "query failed");
3509     if (G_UNLIKELY (serialized))
3510       GST_PAD_STREAM_UNLOCK (pad);
3511     return FALSE;
3512   }
3513 probe_stopped:
3514   {
3515     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3516     GST_OBJECT_UNLOCK (pad);
3517     if (G_UNLIKELY (serialized))
3518       GST_PAD_STREAM_UNLOCK (pad);
3519
3520     /* if a probe dropped, we don't sent it further but assume that the probe
3521      * answered the query and return TRUE */
3522     if (ret == GST_FLOW_CUSTOM_SUCCESS)
3523       res = TRUE;
3524     else
3525       res = FALSE;
3526
3527     return res;
3528   }
3529 }
3530
3531 /**
3532  * gst_pad_peer_query:
3533  * @pad: a #GstPad to invoke the peer query on.
3534  * @query: (transfer none): the #GstQuery to perform.
3535  *
3536  * Performs gst_pad_query() on the peer of @pad.
3537  *
3538  * The caller is responsible for both the allocation and deallocation of
3539  * the query structure.
3540  *
3541  * Returns: TRUE if the query could be performed. This function returns %FALSE
3542  * if @pad has no peer.
3543  */
3544 gboolean
3545 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3546 {
3547   GstPad *peerpad;
3548   GstPadProbeType type;
3549   gboolean res, serialized;
3550   GstFlowReturn ret;
3551
3552   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3553   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3554
3555   if (GST_PAD_IS_SRC (pad)) {
3556     if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3557       goto wrong_direction;
3558     type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3559   } else if (GST_PAD_IS_SINK (pad)) {
3560     if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3561       goto wrong_direction;
3562     type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3563   } else
3564     goto unknown_direction;
3565
3566   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3567       GST_QUERY_TYPE_NAME (query));
3568
3569   serialized = GST_QUERY_IS_SERIALIZED (query);
3570
3571   GST_OBJECT_LOCK (pad);
3572   if (GST_PAD_IS_SRC (pad) && serialized) {
3573     /* all serialized queries on the srcpad trigger push of
3574      * sticky events */
3575     if (!check_sticky (pad, NULL) == GST_FLOW_OK)
3576       goto sticky_failed;
3577   }
3578
3579   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3580       GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3581   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3582
3583   peerpad = GST_PAD_PEER (pad);
3584   if (G_UNLIKELY (peerpad == NULL))
3585     goto no_peer;
3586
3587   gst_object_ref (peerpad);
3588   GST_OBJECT_UNLOCK (pad);
3589
3590   res = gst_pad_query (peerpad, query);
3591
3592   gst_object_unref (peerpad);
3593
3594   if (res != TRUE)
3595     goto query_failed;
3596
3597   GST_OBJECT_LOCK (pad);
3598   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3599   GST_OBJECT_UNLOCK (pad);
3600
3601   return res;
3602
3603   /* ERRORS */
3604 wrong_direction:
3605   {
3606     g_warning ("pad %s:%s query %s in wrong direction",
3607         GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3608     return FALSE;
3609   }
3610 unknown_direction:
3611   {
3612     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3613     return FALSE;
3614   }
3615 sticky_failed:
3616   {
3617     GST_WARNING_OBJECT (pad, "could not send sticky events");
3618     GST_OBJECT_UNLOCK (pad);
3619     return FALSE;
3620   }
3621 no_peer:
3622   {
3623     GST_INFO_OBJECT (pad, "pad has no peer");
3624     GST_OBJECT_UNLOCK (pad);
3625     return FALSE;
3626   }
3627 query_failed:
3628   {
3629     GST_DEBUG_OBJECT (pad, "query failed");
3630     return FALSE;
3631   }
3632 probe_stopped:
3633   {
3634     GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3635     GST_OBJECT_UNLOCK (pad);
3636
3637     /* if a probe dropped, we don't sent it further but assume that the probe
3638      * answered the query and return TRUE */
3639     if (ret == GST_FLOW_CUSTOM_SUCCESS)
3640       res = TRUE;
3641     else
3642       res = FALSE;
3643
3644     return res;
3645   }
3646 }
3647
3648 /**********************************************************************
3649  * Data passing functions
3650  */
3651
3652 /* this is the chain function that does not perform the additional argument
3653  * checking for that little extra speed.
3654  */
3655 static inline GstFlowReturn
3656 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3657 {
3658   GstFlowReturn ret;
3659   GstObject *parent;
3660
3661   GST_PAD_STREAM_LOCK (pad);
3662
3663   GST_OBJECT_LOCK (pad);
3664   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3665     goto flushing;
3666
3667   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3668     goto eos;
3669
3670   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3671     goto wrong_mode;
3672
3673 #ifndef G_DISABLE_ASSERT
3674   if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
3675     g_warning (G_STRLOC
3676         ":%s:<%s:%s> Got data flow before stream-start event",
3677         G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3678   }
3679   if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
3680     g_warning (G_STRLOC
3681         ":%s:<%s:%s> Got data flow before segment event",
3682         G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3683   }
3684 #endif
3685
3686   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3687
3688   PROBE_PUSH (pad, type, data, probe_stopped);
3689
3690   parent = GST_OBJECT_PARENT (pad);
3691   GST_OBJECT_UNLOCK (pad);
3692
3693   /* NOTE: we read the chainfunc unlocked.
3694    * we cannot hold the lock for the pad so we might send
3695    * the data to the wrong function. This is not really a
3696    * problem since functions are assigned at creation time
3697    * and don't change that often... */
3698   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3699     GstPadChainFunction chainfunc;
3700
3701     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3702       goto no_function;
3703
3704     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3705         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3706         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3707
3708     ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3709
3710     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3711         "called chainfunction &%s with buffer %p, returned %s",
3712         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3713   } else {
3714     GstPadChainListFunction chainlistfunc;
3715
3716     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3717       goto no_function;
3718
3719     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3720         "calling chainlistfunction &%s",
3721         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3722
3723     ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3724
3725     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3726         "called chainlistfunction &%s, returned %s",
3727         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3728   }
3729
3730   GST_PAD_STREAM_UNLOCK (pad);
3731
3732   return ret;
3733
3734   /* ERRORS */
3735 flushing:
3736   {
3737     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3738         "chaining, but pad was flushing");
3739     GST_OBJECT_UNLOCK (pad);
3740     GST_PAD_STREAM_UNLOCK (pad);
3741     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3742     return GST_FLOW_FLUSHING;
3743   }
3744 eos:
3745   {
3746     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
3747     GST_OBJECT_UNLOCK (pad);
3748     GST_PAD_STREAM_UNLOCK (pad);
3749     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3750     return GST_FLOW_EOS;
3751   }
3752 wrong_mode:
3753   {
3754     g_critical ("chain on pad %s:%s but it was not in push mode",
3755         GST_DEBUG_PAD_NAME (pad));
3756     GST_OBJECT_UNLOCK (pad);
3757     GST_PAD_STREAM_UNLOCK (pad);
3758     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3759     return GST_FLOW_ERROR;
3760   }
3761 probe_stopped:
3762   {
3763     GST_OBJECT_UNLOCK (pad);
3764     GST_PAD_STREAM_UNLOCK (pad);
3765     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3766
3767     switch (ret) {
3768       case GST_FLOW_CUSTOM_SUCCESS:
3769         GST_DEBUG_OBJECT (pad, "dropped buffer");
3770         ret = GST_FLOW_OK;
3771         break;
3772       default:
3773         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3774         break;
3775     }
3776     return ret;
3777   }
3778 no_function:
3779   {
3780     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3781     g_critical ("chain on pad %s:%s but it has no chainfunction",
3782         GST_DEBUG_PAD_NAME (pad));
3783     GST_PAD_STREAM_UNLOCK (pad);
3784     return GST_FLOW_NOT_SUPPORTED;
3785   }
3786 }
3787
3788 /**
3789  * gst_pad_chain:
3790  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3791  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3792  *     if not.
3793  *
3794  * Chain a buffer to @pad.
3795  *
3796  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3797  *
3798  * If the buffer type is not acceptable for @pad (as negotiated with a
3799  * preceeding GST_EVENT_CAPS event), this function returns
3800  * #GST_FLOW_NOT_NEGOTIATED.
3801  *
3802  * The function proceeds calling the chain function installed on @pad (see
3803  * gst_pad_set_chain_function()) and the return value of that function is
3804  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3805  * chain function.
3806  *
3807  * In all cases, success or failure, the caller loses its reference to @buffer
3808  * after calling this function.
3809  *
3810  * Returns: a #GstFlowReturn from the pad.
3811  *
3812  * MT safe.
3813  */
3814 GstFlowReturn
3815 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3816 {
3817   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3818   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3819   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3820
3821   return gst_pad_chain_data_unchecked (pad,
3822       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3823 }
3824
3825 static GstFlowReturn
3826 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3827     GstBufferList * list)
3828 {
3829   guint i, len;
3830   GstBuffer *buffer;
3831   GstFlowReturn ret;
3832
3833   GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3834
3835   len = gst_buffer_list_length (list);
3836
3837   ret = GST_FLOW_OK;
3838   for (i = 0; i < len; i++) {
3839     buffer = gst_buffer_list_get (list, i);
3840     ret =
3841         gst_pad_chain_data_unchecked (pad,
3842         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3843         gst_buffer_ref (buffer));
3844     if (ret != GST_FLOW_OK)
3845       break;
3846   }
3847   gst_buffer_list_unref (list);
3848
3849   return ret;
3850 }
3851
3852 /**
3853  * gst_pad_chain_list:
3854  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3855  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3856  *     if not.
3857  *
3858  * Chain a bufferlist to @pad.
3859  *
3860  * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3861  *
3862  * If @pad was not negotiated properly with a CAPS event, this function
3863  * returns #GST_FLOW_NOT_NEGOTIATED.
3864  *
3865  * The function proceeds calling the chainlist function installed on @pad (see
3866  * gst_pad_set_chain_list_function()) and the return value of that function is
3867  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3868  * chainlist function.
3869  *
3870  * In all cases, success or failure, the caller loses its reference to @list
3871  * after calling this function.
3872  *
3873  * MT safe.
3874  *
3875  * Returns: a #GstFlowReturn from the pad.
3876  */
3877 GstFlowReturn
3878 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3879 {
3880   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3881   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3882   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3883
3884   return gst_pad_chain_data_unchecked (pad,
3885       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3886 }
3887
3888 static GstFlowReturn
3889 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3890 {
3891   GstPad *peer;
3892   GstFlowReturn ret;
3893
3894   GST_OBJECT_LOCK (pad);
3895   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3896     goto flushing;
3897
3898   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3899     goto eos;
3900
3901   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3902     goto wrong_mode;
3903
3904 #ifndef G_DISABLE_ASSERT
3905   if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
3906     g_warning (G_STRLOC
3907         ":%s:<%s:%s> Got data flow before stream-start event",
3908         G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3909   }
3910   if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
3911     g_warning (G_STRLOC
3912         ":%s:<%s:%s> Got data flow before segment event",
3913         G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
3914   }
3915 #endif
3916
3917   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
3918     goto events_error;
3919
3920   /* do block probes */
3921   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3922
3923   /* recheck sticky events because the probe might have cause a relink */
3924   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
3925     goto events_error;
3926
3927   /* do post-blocking probes */
3928   PROBE_PUSH (pad, type, data, probe_stopped);
3929
3930   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3931     goto not_linked;
3932
3933   /* take ref to peer pad before releasing the lock */
3934   gst_object_ref (peer);
3935   pad->priv->using++;
3936   GST_OBJECT_UNLOCK (pad);
3937
3938   ret = gst_pad_chain_data_unchecked (peer, type, data);
3939
3940   gst_object_unref (peer);
3941
3942   GST_OBJECT_LOCK (pad);
3943   pad->priv->using--;
3944   if (pad->priv->using == 0) {
3945     /* pad is not active anymore, trigger idle callbacks */
3946     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3947         probe_stopped, ret);
3948   }
3949   GST_OBJECT_UNLOCK (pad);
3950
3951   return ret;
3952
3953   /* ERROR recovery here */
3954   /* ERRORS */
3955 flushing:
3956   {
3957     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3958         "pushing, but pad was flushing");
3959     GST_OBJECT_UNLOCK (pad);
3960     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3961     return GST_FLOW_FLUSHING;
3962   }
3963 eos:
3964   {
3965     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
3966     GST_OBJECT_UNLOCK (pad);
3967     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3968     return GST_FLOW_EOS;
3969   }
3970 wrong_mode:
3971   {
3972     g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3973         GST_DEBUG_PAD_NAME (pad));
3974     GST_OBJECT_UNLOCK (pad);
3975     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3976     return GST_FLOW_ERROR;
3977   }
3978 events_error:
3979   {
3980     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3981         "error pushing events, return %s", gst_flow_get_name (ret));
3982     GST_OBJECT_UNLOCK (pad);
3983     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3984     return ret;
3985   }
3986 probe_stopped:
3987   {
3988     GST_OBJECT_UNLOCK (pad);
3989     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3990
3991     switch (ret) {
3992       case GST_FLOW_CUSTOM_SUCCESS:
3993         GST_DEBUG_OBJECT (pad, "dropped buffer");
3994         ret = GST_FLOW_OK;
3995         break;
3996       default:
3997         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3998         break;
3999     }
4000     return ret;
4001   }
4002 not_linked:
4003   {
4004     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4005         "pushing, but it was not linked");
4006     GST_OBJECT_UNLOCK (pad);
4007     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4008     return GST_FLOW_NOT_LINKED;
4009   }
4010 }
4011
4012 /**
4013  * gst_pad_push:
4014  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4015  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4016  *     if not.
4017  *
4018  * Pushes a buffer to the peer of @pad.
4019  *
4020  * This function will call installed block probes before triggering any
4021  * installed data probes.
4022  *
4023  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4024  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4025  * be returned.
4026  *
4027  * In all cases, success or failure, the caller loses its reference to @buffer
4028  * after calling this function.
4029  *
4030  * Returns: a #GstFlowReturn from the peer pad.
4031  *
4032  * MT safe.
4033  */
4034 GstFlowReturn
4035 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4036 {
4037   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4038   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4039   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4040
4041   return gst_pad_push_data (pad,
4042       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4043 }
4044
4045 /**
4046  * gst_pad_push_list:
4047  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4048  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4049  *     if not.
4050  *
4051  * Pushes a buffer list to the peer of @pad.
4052  *
4053  * This function will call installed block probes before triggering any
4054  * installed data probes.
4055  *
4056  * The function proceeds calling the chain function on the peer pad and returns
4057  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4058  * be returned. If the peer pad does not have any installed chainlist function
4059  * every group buffer of the list will be merged into a normal #GstBuffer and
4060  * chained via gst_pad_chain().
4061  *
4062  * In all cases, success or failure, the caller loses its reference to @list
4063  * after calling this function.
4064  *
4065  * Returns: a #GstFlowReturn from the peer pad.
4066  *
4067  * MT safe.
4068  */
4069 GstFlowReturn
4070 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4071 {
4072   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4073   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4074   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4075
4076   return gst_pad_push_data (pad,
4077       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4078 }
4079
4080 static GstFlowReturn
4081 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4082     GstBuffer ** buffer)
4083 {
4084   GstFlowReturn ret;
4085   GstPadGetRangeFunction getrangefunc;
4086   GstObject *parent;
4087   GstBuffer *res_buf;
4088
4089   GST_PAD_STREAM_LOCK (pad);
4090
4091   GST_OBJECT_LOCK (pad);
4092   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4093     goto flushing;
4094
4095   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4096     goto wrong_mode;
4097
4098   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4099     goto events_error;
4100
4101   res_buf = *buffer;
4102
4103   /* when one of the probes returns DROPPED, probe_stopped will be called
4104    * and we skip calling the getrange function, res_buf should then contain a
4105    * valid result buffer */
4106   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4107       res_buf, offset, size, probe_stopped);
4108
4109   /* recheck sticky events because the probe might have cause a relink */
4110   if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4111     goto events_error;
4112
4113   ACQUIRE_PARENT (pad, parent, no_parent);
4114   GST_OBJECT_UNLOCK (pad);
4115
4116   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4117     goto no_function;
4118
4119   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4120       "calling getrangefunc %s, offset %"
4121       G_GUINT64_FORMAT ", size %u",
4122       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4123
4124   ret = getrangefunc (pad, parent, offset, size, &res_buf);
4125
4126   RELEASE_PARENT (parent);
4127
4128   if (G_UNLIKELY (ret != GST_FLOW_OK))
4129     goto get_range_failed;
4130
4131   /* can only fire the signal if we have a valid buffer */
4132   GST_OBJECT_LOCK (pad);
4133 probed_data:
4134   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4135       res_buf, offset, size, probe_stopped_unref);
4136   GST_OBJECT_UNLOCK (pad);
4137
4138   GST_PAD_STREAM_UNLOCK (pad);
4139
4140   *buffer = res_buf;
4141
4142   return ret;
4143
4144   /* ERRORS */
4145 flushing:
4146   {
4147     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4148         "getrange, but pad was flushing");
4149     GST_OBJECT_UNLOCK (pad);
4150     GST_PAD_STREAM_UNLOCK (pad);
4151     return GST_FLOW_FLUSHING;
4152   }
4153 wrong_mode:
4154   {
4155     g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4156         GST_DEBUG_PAD_NAME (pad));
4157     GST_OBJECT_UNLOCK (pad);
4158     GST_PAD_STREAM_UNLOCK (pad);
4159     return GST_FLOW_ERROR;
4160   }
4161 events_error:
4162   {
4163     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4164     GST_OBJECT_UNLOCK (pad);
4165     GST_PAD_STREAM_UNLOCK (pad);
4166     return ret;
4167   }
4168 no_parent:
4169   {
4170     GST_DEBUG_OBJECT (pad, "no parent");
4171     GST_OBJECT_UNLOCK (pad);
4172     GST_PAD_STREAM_UNLOCK (pad);
4173     return GST_FLOW_FLUSHING;
4174   }
4175 no_function:
4176   {
4177     g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4178         GST_DEBUG_PAD_NAME (pad));
4179     RELEASE_PARENT (parent);
4180     GST_PAD_STREAM_UNLOCK (pad);
4181     return GST_FLOW_NOT_SUPPORTED;
4182   }
4183 probe_stopped:
4184   {
4185     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4186         "probe returned %s", gst_flow_get_name (ret));
4187     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4188       if (res_buf) {
4189         /* the probe filled the buffer and asks us to not call the getrange
4190          * anymore, we continue with the post probes then. */
4191         GST_DEBUG_OBJECT (pad, "handled buffer");
4192         ret = GST_FLOW_OK;
4193         goto probed_data;
4194       } else {
4195         /* no buffer, we are EOS */
4196         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4197         ret = GST_FLOW_EOS;
4198       }
4199     }
4200     GST_OBJECT_UNLOCK (pad);
4201     GST_PAD_STREAM_UNLOCK (pad);
4202
4203     return ret;
4204   }
4205 probe_stopped_unref:
4206   {
4207     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4208         "probe returned %s", gst_flow_get_name (ret));
4209     /* if we drop here, it signals EOS */
4210     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4211       ret = GST_FLOW_EOS;
4212     GST_OBJECT_UNLOCK (pad);
4213     GST_PAD_STREAM_UNLOCK (pad);
4214     if (*buffer == NULL)
4215       gst_buffer_unref (res_buf);
4216     return ret;
4217   }
4218 get_range_failed:
4219   {
4220     GST_PAD_STREAM_UNLOCK (pad);
4221     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4222         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4223         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4224     return ret;
4225   }
4226 }
4227
4228 /**
4229  * gst_pad_get_range:
4230  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4231  * @offset: The start offset of the buffer
4232  * @size: The length of the buffer
4233  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4234  *     returns #GST_FLOW_ERROR if %NULL.
4235  *
4236  * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4237  * immediately and @buffer is %NULL.
4238  *
4239  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4240  * description of a getrange function. If @pad has no getrange function
4241  * installed (see gst_pad_set_getrange_function()) this function returns
4242  * #GST_FLOW_NOT_SUPPORTED.
4243  *
4244  * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4245  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4246  * must be freed with gst_buffer_unref() after usage.
4247  *
4248  * When @buffer points to a variable that points to a valid #GstBuffer, the
4249  * buffer will be filled with the result data when this function returns
4250  * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4251  * @size bytes will be filled in the result buffer and its size will be updated
4252  * accordingly.
4253  *
4254  * Note that less than @size bytes can be returned in @buffer when, for example,
4255  * an EOS condition is near or when @buffer is not large enough to hold @size
4256  * bytes. The caller should check the result buffer size to get the result size.
4257  *
4258  * When this function returns any other result value than #GST_FLOW_OK, @buffer
4259  * will be unchanged.
4260  *
4261  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4262  *
4263  * Returns: a #GstFlowReturn from the pad.
4264  *
4265  * MT safe.
4266  */
4267 GstFlowReturn
4268 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4269     GstBuffer ** buffer)
4270 {
4271   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4272   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4273   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4274   g_return_val_if_fail (*buffer == NULL
4275       || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4276
4277   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4278 }
4279
4280 /**
4281  * gst_pad_pull_range:
4282  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4283  * @offset: The start offset of the buffer
4284  * @size: The length of the buffer
4285  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4286  *     GST_FLOW_ERROR if %NULL.
4287  *
4288  * Pulls a @buffer from the peer pad or fills up a provided buffer.
4289  *
4290  * This function will first trigger the pad block signal if it was
4291  * installed.
4292  *
4293  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4294  * function returns the result of gst_pad_get_range() on the peer pad.
4295  * See gst_pad_get_range() for a list of return values and for the
4296  * semantics of the arguments of this function.
4297  *
4298  * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4299  * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4300  * must be freed with gst_buffer_unref() after usage. When this function
4301  * returns any other result value, @buffer will still point to NULL.
4302  *
4303  * When @buffer points to a variable that points to a valid #GstBuffer, the
4304  * buffer will be filled with the result data when this function returns
4305  * #GST_FLOW_OK. When this function returns any other result value,
4306  * @buffer will be unchanged. If the provided buffer is larger than @size, only
4307  * @size bytes will be filled in the result buffer and its size will be updated
4308  * accordingly.
4309  *
4310  * Note that less than @size bytes can be returned in @buffer when, for example,
4311  * an EOS condition is near or when @buffer is not large enough to hold @size
4312  * bytes. The caller should check the result buffer size to get the result size.
4313  *
4314  * Returns: a #GstFlowReturn from the peer pad.
4315  *
4316  * MT safe.
4317  */
4318 GstFlowReturn
4319 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4320     GstBuffer ** buffer)
4321 {
4322   GstPad *peer;
4323   GstFlowReturn ret;
4324   GstBuffer *res_buf;
4325
4326   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4327   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4328   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4329   g_return_val_if_fail (*buffer == NULL
4330       || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4331
4332   GST_OBJECT_LOCK (pad);
4333   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4334     goto flushing;
4335
4336   if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4337     goto wrong_mode;
4338
4339   res_buf = *buffer;
4340
4341   /* when one of the probes returns DROPPED, probe_stopped will be
4342    * called and we skip calling the peer getrange function. *buffer should then
4343    * contain a valid buffer */
4344   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4345       res_buf, offset, size, probe_stopped);
4346
4347   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4348     goto not_linked;
4349
4350   gst_object_ref (peer);
4351   pad->priv->using++;
4352   GST_OBJECT_UNLOCK (pad);
4353
4354   ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4355
4356   gst_object_unref (peer);
4357
4358   GST_OBJECT_LOCK (pad);
4359   pad->priv->using--;
4360   if (pad->priv->using == 0) {
4361     /* pad is not active anymore, trigger idle callbacks */
4362     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4363         probe_stopped_unref, ret);
4364   }
4365
4366   if (G_UNLIKELY (ret != GST_FLOW_OK))
4367     goto pull_range_failed;
4368
4369 probed_data:
4370   PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4371       res_buf, offset, size, probe_stopped_unref);
4372
4373   GST_OBJECT_UNLOCK (pad);
4374
4375   *buffer = res_buf;
4376
4377   return ret;
4378
4379   /* ERROR recovery here */
4380 flushing:
4381   {
4382     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4383         "pullrange, but pad was flushing");
4384     GST_OBJECT_UNLOCK (pad);
4385     return GST_FLOW_FLUSHING;
4386   }
4387 wrong_mode:
4388   {
4389     g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4390         GST_DEBUG_PAD_NAME (pad));
4391     GST_OBJECT_UNLOCK (pad);
4392     return GST_FLOW_ERROR;
4393   }
4394 probe_stopped:
4395   {
4396     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4397         gst_flow_get_name (ret));
4398     if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4399       if (res_buf) {
4400         /* the probe filled the buffer and asks us to not forward to the peer
4401          * anymore, we continue with the post probes then */
4402         GST_DEBUG_OBJECT (pad, "handled buffer");
4403         ret = GST_FLOW_OK;
4404         goto probed_data;
4405       } else {
4406         /* no buffer, we are EOS then */
4407         GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4408         ret = GST_FLOW_EOS;
4409       }
4410     }
4411     GST_OBJECT_UNLOCK (pad);
4412     return ret;
4413   }
4414 not_linked:
4415   {
4416     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4417         "pulling range, but it was not linked");
4418     GST_OBJECT_UNLOCK (pad);
4419     return GST_FLOW_NOT_LINKED;
4420   }
4421 pull_range_failed:
4422   {
4423     GST_OBJECT_UNLOCK (pad);
4424     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4425         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4426         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4427     return ret;
4428   }
4429 probe_stopped_unref:
4430   {
4431     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4432         "post probe returned %s", gst_flow_get_name (ret));
4433     GST_OBJECT_UNLOCK (pad);
4434     /* if we drop here, it signals EOS */
4435     if (ret == GST_FLOW_CUSTOM_SUCCESS)
4436       ret = GST_FLOW_EOS;
4437     if (*buffer == NULL)
4438       gst_buffer_unref (res_buf);
4439     return ret;
4440   }
4441 }
4442
4443 /* must be called with pad object lock */
4444 static GstFlowReturn
4445 store_sticky_event (GstPad * pad, GstEvent * event)
4446 {
4447   guint i, len;
4448   GstEventType type;
4449   GArray *events;
4450   gboolean res = FALSE;
4451   const gchar *name = NULL;
4452   gboolean insert = TRUE;
4453
4454   type = GST_EVENT_TYPE (event);
4455
4456   /* Store all sticky events except SEGMENT/SEGMENT when we're flushing,
4457    * otherwise they can be dropped and nothing would ever resend them.
4458    * Only do that for activated pads though, everything else is a bug!
4459    */
4460   if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4461           || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4462                   || type == GST_EVENT_EOS))))
4463     goto flushed;
4464
4465   if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4466     goto eos;
4467
4468   if (type & GST_EVENT_TYPE_STICKY_MULTI)
4469     name = gst_structure_get_name (gst_event_get_structure (event));
4470
4471   events = pad->priv->events;
4472   len = events->len;
4473
4474   for (i = 0; i < len; i++) {
4475     PadEvent *ev = &g_array_index (events, PadEvent, i);
4476
4477     if (ev->event == NULL)
4478       continue;
4479
4480     if (type == GST_EVENT_TYPE (ev->event)) {
4481       /* matching types, check matching name if needed */
4482       if (name && !gst_event_has_name (ev->event, name))
4483         continue;
4484
4485       /* overwrite */
4486       if ((res = gst_event_replace (&ev->event, event)))
4487         ev->received = FALSE;
4488
4489       insert = FALSE;
4490       break;
4491     }
4492
4493     if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4494             && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4495       /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4496        * storing the sticky ordered we can check that this is respected. */
4497       if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4498               || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4499         g_warning (G_STRLOC
4500             ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4501             G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4502             gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4503             gst_event_type_get_name (type));
4504       break;
4505     }
4506   }
4507   if (insert) {
4508     PadEvent ev;
4509     ev.event = gst_event_ref (event);
4510     ev.received = FALSE;
4511     g_array_insert_val (events, i, ev);
4512     res = TRUE;
4513   }
4514
4515   if (res) {
4516     pad->priv->events_cookie++;
4517     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4518
4519     GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4520
4521     switch (GST_EVENT_TYPE (event)) {
4522       case GST_EVENT_CAPS:
4523         GST_OBJECT_UNLOCK (pad);
4524
4525         GST_DEBUG_OBJECT (pad, "notify caps");
4526         g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4527
4528         GST_OBJECT_LOCK (pad);
4529         break;
4530       default:
4531         break;
4532     }
4533   }
4534   if (type == GST_EVENT_EOS)
4535     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4536
4537   return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4538
4539   /* ERRORS */
4540 flushed:
4541   {
4542     GST_DEBUG_OBJECT (pad, "pad is flushing");
4543     return GST_FLOW_FLUSHING;
4544   }
4545 eos:
4546   {
4547     GST_DEBUG_OBJECT (pad, "pad is EOS");
4548     return GST_FLOW_EOS;
4549   }
4550 }
4551
4552 /**
4553  * gst_pad_store_sticky_event:
4554  * @pad: a #GstPad
4555  * @event: a #GstEvent
4556  *
4557  * Store the sticky @event on @pad
4558  *
4559  * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
4560  * was flushing or #GST_FLOW_EOS when the pad was EOS.
4561  *
4562  * Since: 1.2
4563  */
4564 GstFlowReturn
4565 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
4566 {
4567   GstFlowReturn ret;
4568
4569   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4570   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4571
4572   GST_OBJECT_LOCK (pad);
4573   ret = store_sticky_event (pad, event);
4574   GST_OBJECT_UNLOCK (pad);
4575
4576   return ret;
4577 }
4578
4579 static gboolean
4580 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
4581 {
4582   PushStickyData *data = user_data;
4583
4584   /* Forward all sticky events before our current one that are pending */
4585   if (ev->event != data->event
4586       && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
4587     return push_sticky (pad, ev, data);
4588
4589   return TRUE;
4590 }
4591
4592 /* should be called with pad LOCK */
4593 static GstFlowReturn
4594 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4595     GstPadProbeType type)
4596 {
4597   GstFlowReturn ret;
4598   GstPad *peerpad;
4599   GstEventType event_type;
4600
4601   /* Two checks to be made:
4602    * . (un)set the FLUSHING flag for flushing events,
4603    * . handle pad blocking */
4604   event_type = GST_EVENT_TYPE (event);
4605   switch (event_type) {
4606     case GST_EVENT_FLUSH_START:
4607       GST_PAD_SET_FLUSHING (pad);
4608
4609       GST_PAD_BLOCK_BROADCAST (pad);
4610       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4611       break;
4612     case GST_EVENT_FLUSH_STOP:
4613       GST_PAD_UNSET_FLUSHING (pad);
4614
4615       /* Remove sticky EOS events */
4616       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4617       remove_event_by_type (pad, GST_EVENT_EOS);
4618       remove_event_by_type (pad, GST_EVENT_SEGMENT);
4619       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4620
4621       type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4622       break;
4623     default:
4624     {
4625       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4626         goto flushed;
4627
4628       /* No need to check for EOS here as either the caller (gst_pad_push_event())
4629        * checked already or this is called as part of pushing sticky events,
4630        * in which case we still want to forward the EOS event downstream.
4631        */
4632
4633       switch (GST_EVENT_TYPE (event)) {
4634         case GST_EVENT_SEGMENT:
4635           /* pass the adjusted segment event on. We need to do this even if
4636            * there is no peer pad because of the probes. */
4637           event = apply_pad_offset (pad, event);
4638           break;
4639         case GST_EVENT_RECONFIGURE:
4640           if (GST_PAD_IS_SINK (pad))
4641             GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4642           break;
4643         default:
4644           break;
4645       }
4646       PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4647           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4648       break;
4649     }
4650   }
4651
4652   /* send probes after modifying the events above */
4653   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4654
4655   /* recheck sticky events because the probe might have cause a relink */
4656   if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
4657       && (GST_EVENT_IS_SERIALIZED (event)
4658           || GST_EVENT_IS_STICKY (event))) {
4659     PushStickyData data = { GST_FLOW_OK, FALSE, event };
4660     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4661
4662     /* Push all sticky events before our current one
4663      * that have changed */
4664     events_foreach (pad, sticky_changed, &data);
4665   }
4666
4667   /* now check the peer pad */
4668   peerpad = GST_PAD_PEER (pad);
4669   if (peerpad == NULL)
4670     goto not_linked;
4671
4672   gst_object_ref (peerpad);
4673   pad->priv->using++;
4674   GST_OBJECT_UNLOCK (pad);
4675
4676   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4677       event, GST_EVENT_TYPE_NAME (event), peerpad);
4678
4679   ret = gst_pad_send_event_unchecked (peerpad, event, type);
4680
4681   /* Note: we gave away ownership of the event at this point but we can still
4682    * print the old pointer */
4683   GST_LOG_OBJECT (pad, "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s",
4684       event, peerpad, gst_flow_get_name (ret));
4685
4686   gst_object_unref (peerpad);
4687
4688   GST_OBJECT_LOCK (pad);
4689   pad->priv->using--;
4690   if (pad->priv->using == 0) {
4691     /* pad is not active anymore, trigger idle callbacks */
4692     PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4693         idle_probe_stopped, ret);
4694   }
4695   return ret;
4696
4697   /* ERROR handling */
4698 flushed:
4699   {
4700     GST_DEBUG_OBJECT (pad, "We're flushing");
4701     gst_event_unref (event);
4702     return GST_FLOW_FLUSHING;
4703   }
4704 probe_stopped:
4705   {
4706     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4707     gst_event_unref (event);
4708
4709     switch (ret) {
4710       case GST_FLOW_CUSTOM_SUCCESS:
4711         GST_DEBUG_OBJECT (pad, "dropped event");
4712         break;
4713       default:
4714         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4715         break;
4716     }
4717     return ret;
4718   }
4719 not_linked:
4720   {
4721     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4722     GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4723     gst_event_unref (event);
4724
4725     /* unlinked pads should not influence latency configuration */
4726     if (event_type == GST_EVENT_LATENCY)
4727       return GST_FLOW_OK;
4728
4729     return GST_FLOW_NOT_LINKED;
4730   }
4731 idle_probe_stopped:
4732   {
4733     GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4734     return ret;
4735   }
4736 }
4737
4738 /**
4739  * gst_pad_push_event:
4740  * @pad: a #GstPad to push the event to.
4741  * @event: (transfer full): the #GstEvent to send to the pad.
4742  *
4743  * Sends the event to the peer of the given pad. This function is
4744  * mainly used by elements to send events to their peer
4745  * elements.
4746  *
4747  * This function takes owership of the provided event so you should
4748  * gst_event_ref() it if you want to reuse the event after this call.
4749  *
4750  * Returns: TRUE if the event was handled.
4751  *
4752  * MT safe.
4753  */
4754 gboolean
4755 gst_pad_push_event (GstPad * pad, GstEvent * event)
4756 {
4757   gboolean res = FALSE;
4758   GstPadProbeType type;
4759   gboolean sticky, serialized;
4760
4761   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4762   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4763
4764   if (GST_PAD_IS_SRC (pad)) {
4765     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4766       goto wrong_direction;
4767     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4768   } else if (GST_PAD_IS_SINK (pad)) {
4769     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4770       goto wrong_direction;
4771     /* events pushed on sinkpad never are sticky */
4772     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4773   } else
4774     goto unknown_direction;
4775
4776   GST_OBJECT_LOCK (pad);
4777   sticky = GST_EVENT_IS_STICKY (event);
4778   serialized = GST_EVENT_IS_SERIALIZED (event);
4779
4780   if (sticky) {
4781     /* srcpad sticky events are stored immediately, the received flag is set
4782      * to FALSE and will be set to TRUE when we can successfully push the
4783      * event to the peer pad */
4784     switch (store_sticky_event (pad, event)) {
4785       case GST_FLOW_FLUSHING:
4786         goto flushed;
4787       case GST_FLOW_EOS:
4788         goto eos;
4789       default:
4790         break;
4791     }
4792   }
4793   if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4794     /* all serialized or sticky events on the srcpad trigger push of
4795      * sticky events */
4796     res = (check_sticky (pad, event) == GST_FLOW_OK);
4797   }
4798   if (!sticky) {
4799     GstFlowReturn ret;
4800
4801     /* other events are pushed right away */
4802     ret = gst_pad_push_event_unchecked (pad, event, type);
4803     /* dropped events by a probe are not an error */
4804     res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS);
4805   } else {
4806     /* Errors in sticky event pushing are no problem and ignored here
4807      * as they will cause more meaningful errors during data flow.
4808      * For EOS events, that are not followed by data flow, we still
4809      * return FALSE here though.
4810      */
4811     if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
4812       res = TRUE;
4813     gst_event_unref (event);
4814   }
4815   GST_OBJECT_UNLOCK (pad);
4816
4817   return res;
4818
4819   /* ERROR handling */
4820 wrong_direction:
4821   {
4822     g_warning ("pad %s:%s pushing %s event in wrong direction",
4823         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4824     gst_event_unref (event);
4825     return FALSE;
4826   }
4827 unknown_direction:
4828   {
4829     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4830     gst_event_unref (event);
4831     return FALSE;
4832   }
4833 flushed:
4834   {
4835     GST_DEBUG_OBJECT (pad, "We're flushing");
4836     GST_OBJECT_UNLOCK (pad);
4837     gst_event_unref (event);
4838     return FALSE;
4839   }
4840 eos:
4841   {
4842     GST_DEBUG_OBJECT (pad, "We're EOS");
4843     GST_OBJECT_UNLOCK (pad);
4844     gst_event_unref (event);
4845     return FALSE;
4846   }
4847 }
4848
4849 /* Check if we can call the event function with the given event */
4850 static GstFlowReturn
4851 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4852 {
4853   GstCaps *caps;
4854
4855   switch (GST_EVENT_TYPE (event)) {
4856     case GST_EVENT_CAPS:
4857     {
4858       /* backwards compatibility mode for caps */
4859       gst_event_parse_caps (event, &caps);
4860
4861       if (!gst_pad_query_accept_caps (pad, caps))
4862         goto not_accepted;
4863       break;
4864     }
4865     default:
4866       break;
4867   }
4868   return GST_FLOW_OK;
4869
4870   /* ERRORS */
4871 not_accepted:
4872   {
4873     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4874         "caps %" GST_PTR_FORMAT " not accepted", caps);
4875     return GST_FLOW_NOT_NEGOTIATED;
4876   }
4877 }
4878
4879 static GstFlowReturn
4880 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4881     GstPadProbeType type)
4882 {
4883   GstFlowReturn ret;
4884   GstEventType event_type;
4885   gboolean serialized, need_unlock = FALSE, sticky;
4886   GstPadEventFunction eventfunc;
4887   GstObject *parent;
4888
4889   GST_OBJECT_LOCK (pad);
4890   if (GST_PAD_IS_SINK (pad))
4891     serialized = GST_EVENT_IS_SERIALIZED (event);
4892   else
4893     serialized = FALSE;
4894   sticky = GST_EVENT_IS_STICKY (event);
4895   event_type = GST_EVENT_TYPE (event);
4896   switch (event_type) {
4897     case GST_EVENT_FLUSH_START:
4898       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4899           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4900
4901       /* can't even accept a flush begin event when flushing */
4902       if (GST_PAD_IS_FLUSHING (pad))
4903         goto flushing;
4904
4905       GST_PAD_SET_FLUSHING (pad);
4906       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4907       break;
4908     case GST_EVENT_FLUSH_STOP:
4909       if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4910         GST_PAD_UNSET_FLUSHING (pad);
4911         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4912       }
4913       /* Remove pending EOS events */
4914       GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
4915       remove_event_by_type (pad, GST_EVENT_EOS);
4916       remove_event_by_type (pad, GST_EVENT_SEGMENT);
4917       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4918
4919       GST_OBJECT_UNLOCK (pad);
4920       /* grab stream lock */
4921       GST_PAD_STREAM_LOCK (pad);
4922       need_unlock = TRUE;
4923       GST_OBJECT_LOCK (pad);
4924       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4925         goto flushing;
4926       break;
4927     case GST_EVENT_RECONFIGURE:
4928       if (GST_PAD_IS_SRC (pad))
4929         GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4930     default:
4931       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4932           "have event type %" GST_PTR_FORMAT, event);
4933
4934       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4935         goto flushing;
4936
4937       if (serialized) {
4938         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4939           goto eos;
4940
4941         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4942         GST_OBJECT_UNLOCK (pad);
4943         GST_PAD_STREAM_LOCK (pad);
4944         need_unlock = TRUE;
4945         GST_OBJECT_LOCK (pad);
4946         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4947           goto flushing;
4948
4949         if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4950           goto eos;
4951       }
4952
4953       switch (GST_EVENT_TYPE (event)) {
4954         case GST_EVENT_SEGMENT:
4955           event = apply_pad_offset (pad, event);
4956           break;
4957         default:
4958           break;
4959       }
4960       break;
4961   }
4962
4963   /* now do the probe */
4964   PROBE_PUSH (pad,
4965       type | GST_PAD_PROBE_TYPE_PUSH |
4966       GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4967
4968   PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4969
4970   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4971     goto no_function;
4972
4973   ACQUIRE_PARENT (pad, parent, no_parent);
4974   GST_OBJECT_UNLOCK (pad);
4975
4976   ret = pre_eventfunc_check (pad, event);
4977   if (G_UNLIKELY (ret != GST_FLOW_OK))
4978     goto precheck_failed;
4979
4980   if (sticky)
4981     gst_event_ref (event);
4982
4983   if (eventfunc (pad, parent, event)) {
4984     ret = GST_FLOW_OK;
4985   } else {
4986     /* something went wrong */
4987     switch (event_type) {
4988       case GST_EVENT_CAPS:
4989         ret = GST_FLOW_NOT_NEGOTIATED;
4990         break;
4991       default:
4992         ret = GST_FLOW_ERROR;
4993         break;
4994     }
4995   }
4996   RELEASE_PARENT (parent);
4997
4998   GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4999
5000   if (sticky) {
5001     if (ret == GST_FLOW_OK) {
5002       GST_OBJECT_LOCK (pad);
5003       /* after the event function accepted the event, we can store the sticky
5004        * event on the pad */
5005       switch (store_sticky_event (pad, event)) {
5006         case GST_FLOW_FLUSHING:
5007           goto flushing;
5008         case GST_FLOW_EOS:
5009           goto eos;
5010         default:
5011           break;
5012       }
5013       GST_OBJECT_UNLOCK (pad);
5014     }
5015     gst_event_unref (event);
5016   }
5017
5018   if (need_unlock)
5019     GST_PAD_STREAM_UNLOCK (pad);
5020
5021   return ret;
5022
5023   /* ERROR handling */
5024 flushing:
5025   {
5026     GST_OBJECT_UNLOCK (pad);
5027     if (need_unlock)
5028       GST_PAD_STREAM_UNLOCK (pad);
5029     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5030         "Received event on flushing pad. Discarding");
5031     gst_event_unref (event);
5032     return GST_FLOW_FLUSHING;
5033   }
5034 eos:
5035   {
5036     GST_OBJECT_UNLOCK (pad);
5037     if (need_unlock)
5038       GST_PAD_STREAM_UNLOCK (pad);
5039     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5040         "Received event on EOS pad. Discarding");
5041     gst_event_unref (event);
5042     return GST_FLOW_EOS;
5043   }
5044 probe_stopped:
5045   {
5046     GST_OBJECT_UNLOCK (pad);
5047     if (need_unlock)
5048       GST_PAD_STREAM_UNLOCK (pad);
5049     gst_event_unref (event);
5050
5051     switch (ret) {
5052       case GST_FLOW_CUSTOM_SUCCESS:
5053         GST_DEBUG_OBJECT (pad, "dropped event");
5054         ret = GST_FLOW_OK;
5055         break;
5056       default:
5057         GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
5058         break;
5059     }
5060     return ret;
5061   }
5062 no_function:
5063   {
5064     g_warning ("pad %s:%s has no event handler, file a bug.",
5065         GST_DEBUG_PAD_NAME (pad));
5066     GST_OBJECT_UNLOCK (pad);
5067     if (need_unlock)
5068       GST_PAD_STREAM_UNLOCK (pad);
5069     gst_event_unref (event);
5070     return GST_FLOW_NOT_SUPPORTED;
5071   }
5072 no_parent:
5073   {
5074     GST_DEBUG_OBJECT (pad, "no parent");
5075     GST_OBJECT_UNLOCK (pad);
5076     if (need_unlock)
5077       GST_PAD_STREAM_UNLOCK (pad);
5078     gst_event_unref (event);
5079     return GST_FLOW_FLUSHING;
5080   }
5081 precheck_failed:
5082   {
5083     GST_DEBUG_OBJECT (pad, "pre event check failed");
5084     RELEASE_PARENT (parent);
5085     if (need_unlock)
5086       GST_PAD_STREAM_UNLOCK (pad);
5087     gst_event_unref (event);
5088     return ret;
5089   }
5090 }
5091
5092 /**
5093  * gst_pad_send_event:
5094  * @pad: a #GstPad to send the event to.
5095  * @event: (transfer full): the #GstEvent to send to the pad.
5096  *
5097  * Sends the event to the pad. This function can be used
5098  * by applications to send events in the pipeline.
5099  *
5100  * If @pad is a source pad, @event should be an upstream event. If @pad is a
5101  * sink pad, @event should be a downstream event. For example, you would not
5102  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5103  * Furthermore, some downstream events have to be serialized with data flow,
5104  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5105  * the event needs to be serialized with data flow, this function will take the
5106  * pad's stream lock while calling its event function.
5107  *
5108  * To find out whether an event type is upstream, downstream, or downstream and
5109  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5110  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5111  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5112  * plugin doesn't need to bother itself with this information; the core handles
5113  * all necessary locks and checks.
5114  *
5115  * This function takes owership of the provided event so you should
5116  * gst_event_ref() it if you want to reuse the event after this call.
5117  *
5118  * Returns: TRUE if the event was handled.
5119  */
5120 gboolean
5121 gst_pad_send_event (GstPad * pad, GstEvent * event)
5122 {
5123   gboolean result;
5124   GstPadProbeType type;
5125
5126   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5127   g_return_val_if_fail (event != NULL, FALSE);
5128
5129   if (GST_PAD_IS_SINK (pad)) {
5130     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5131       goto wrong_direction;
5132     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5133   } else if (GST_PAD_IS_SRC (pad)) {
5134     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5135       goto wrong_direction;
5136     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5137   } else
5138     goto unknown_direction;
5139
5140   if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5141     result = FALSE;
5142   else
5143     result = TRUE;
5144
5145   return result;
5146
5147   /* ERROR handling */
5148 wrong_direction:
5149   {
5150     g_warning ("pad %s:%s sending %s event in wrong direction",
5151         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5152     gst_event_unref (event);
5153     return FALSE;
5154   }
5155 unknown_direction:
5156   {
5157     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5158     gst_event_unref (event);
5159     return FALSE;
5160   }
5161 }
5162
5163 /**
5164  * gst_pad_set_element_private:
5165  * @pad: the #GstPad to set the private data of.
5166  * @priv: The private data to attach to the pad.
5167  *
5168  * Set the given private data gpointer on the pad.
5169  * This function can only be used by the element that owns the pad.
5170  * No locking is performed in this function.
5171  */
5172 void
5173 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5174 {
5175   pad->element_private = priv;
5176 }
5177
5178 /**
5179  * gst_pad_get_element_private:
5180  * @pad: the #GstPad to get the private data of.
5181  *
5182  * Gets the private data of a pad.
5183  * No locking is performed in this function.
5184  *
5185  * Returns: (transfer none): a #gpointer to the private data.
5186  */
5187 gpointer
5188 gst_pad_get_element_private (GstPad * pad)
5189 {
5190   return pad->element_private;
5191 }
5192
5193 /**
5194  * gst_pad_get_sticky_event:
5195  * @pad: the #GstPad to get the event from.
5196  * @event_type: the #GstEventType that should be retrieved.
5197  * @idx: the index of the event
5198  *
5199  * Returns a new reference of the sticky event of type @event_type
5200  * from the event.
5201  *
5202  * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
5203  * event of @event_type was on @pad. Unref after usage.
5204  */
5205 GstEvent *
5206 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5207 {
5208   GstEvent *event = NULL;
5209   PadEvent *ev;
5210
5211   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5212   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5213
5214   GST_OBJECT_LOCK (pad);
5215   ev = find_event_by_type (pad, event_type, idx);
5216   if (ev && (event = ev->event))
5217     gst_event_ref (event);
5218   GST_OBJECT_UNLOCK (pad);
5219
5220   return event;
5221 }
5222
5223 typedef struct
5224 {
5225   GstPadStickyEventsForeachFunction func;
5226   gpointer user_data;
5227 } ForeachDispatch;
5228
5229 static gboolean
5230 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5231 {
5232   ForeachDispatch *data = user_data;
5233   gboolean ret = TRUE;
5234
5235   if (ev->event) {
5236     GST_OBJECT_UNLOCK (pad);
5237
5238     ret = data->func (pad, &ev->event, data->user_data);
5239
5240     GST_OBJECT_LOCK (pad);
5241   }
5242
5243   return ret;
5244 }
5245
5246 /**
5247  * gst_pad_sticky_events_foreach:
5248  * @pad: the #GstPad that should be used for iteration.
5249  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5250  *                should be called for every event.
5251  * @user_data: (closure): the optional user data.
5252  *
5253  * Iterates all sticky events on @pad and calls @foreach_func for every
5254  * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5255  */
5256 void
5257 gst_pad_sticky_events_foreach (GstPad * pad,
5258     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5259 {
5260   ForeachDispatch data;
5261
5262   g_return_if_fail (GST_IS_PAD (pad));
5263   g_return_if_fail (foreach_func != NULL);
5264
5265   data.func = foreach_func;
5266   data.user_data = user_data;
5267
5268   GST_OBJECT_LOCK (pad);
5269   events_foreach (pad, foreach_dispatch_function, &data);
5270   GST_OBJECT_UNLOCK (pad);
5271 }
5272
5273 static void
5274 do_stream_status (GstPad * pad, GstStreamStatusType type,
5275     GThread * thread, GstTask * task)
5276 {
5277   GstElement *parent;
5278
5279   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5280
5281   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5282     if (GST_IS_ELEMENT (parent)) {
5283       GstMessage *message;
5284       GValue value = { 0 };
5285
5286       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5287         gchar *tname, *ename, *pname;
5288
5289         /* create a good task name */
5290         ename = gst_element_get_name (parent);
5291         pname = gst_pad_get_name (pad);
5292         tname = g_strdup_printf ("%s:%s", ename, pname);
5293         g_free (ename);
5294         g_free (pname);
5295
5296         gst_object_set_name (GST_OBJECT_CAST (task), tname);
5297         g_free (tname);
5298       }
5299
5300       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5301           type, parent);
5302
5303       g_value_init (&value, GST_TYPE_TASK);
5304       g_value_set_object (&value, task);
5305       gst_message_set_stream_status_object (message, &value);
5306       g_value_unset (&value);
5307
5308       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5309       gst_element_post_message (parent, message);
5310     }
5311     gst_object_unref (parent);
5312   }
5313 }
5314
5315 static void
5316 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5317 {
5318   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5319       thread, task);
5320 }
5321
5322 static void
5323 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5324 {
5325   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5326       thread, task);
5327 }
5328
5329 /**
5330  * gst_pad_start_task:
5331  * @pad: the #GstPad to start the task of
5332  * @func: the task function to call
5333  * @user_data: user data passed to the task function
5334  * @notify: called when @user_data is no longer referenced
5335  *
5336  * Starts a task that repeatedly calls @func with @user_data. This function
5337  * is mostly used in pad activation functions to start the dataflow.
5338  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5339  * before @func is called.
5340  *
5341  * Returns: a %TRUE if the task could be started.
5342  */
5343 gboolean
5344 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5345     GDestroyNotify notify)
5346 {
5347   GstTask *task;
5348   gboolean res;
5349
5350   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5351   g_return_val_if_fail (func != NULL, FALSE);
5352
5353   GST_DEBUG_OBJECT (pad, "start task");
5354
5355   GST_OBJECT_LOCK (pad);
5356   task = GST_PAD_TASK (pad);
5357   if (task == NULL) {
5358     task = gst_task_new (func, user_data, notify);
5359     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5360     gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5361     gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5362     GST_INFO_OBJECT (pad, "created task %p", task);
5363     GST_PAD_TASK (pad) = task;
5364     gst_object_ref (task);
5365     /* release lock to post the message */
5366     GST_OBJECT_UNLOCK (pad);
5367
5368     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5369
5370     gst_object_unref (task);
5371
5372     GST_OBJECT_LOCK (pad);
5373     /* nobody else is supposed to have changed the pad now */
5374     if (GST_PAD_TASK (pad) != task)
5375       goto concurrent_stop;
5376   }
5377   res = gst_task_set_state (task, GST_TASK_STARTED);
5378   GST_OBJECT_UNLOCK (pad);
5379
5380   return res;
5381
5382   /* ERRORS */
5383 concurrent_stop:
5384   {
5385     GST_OBJECT_UNLOCK (pad);
5386     return TRUE;
5387   }
5388 }
5389
5390 /**
5391  * gst_pad_pause_task:
5392  * @pad: the #GstPad to pause the task of
5393  *
5394  * Pause the task of @pad. This function will also wait until the
5395  * function executed by the task is finished if this function is not
5396  * called from the task function.
5397  *
5398  * Returns: a TRUE if the task could be paused or FALSE when the pad
5399  * has no task.
5400  */
5401 gboolean
5402 gst_pad_pause_task (GstPad * pad)
5403 {
5404   GstTask *task;
5405   gboolean res;
5406
5407   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5408
5409   GST_DEBUG_OBJECT (pad, "pause task");
5410
5411   GST_OBJECT_LOCK (pad);
5412   task = GST_PAD_TASK (pad);
5413   if (task == NULL)
5414     goto no_task;
5415   res = gst_task_set_state (task, GST_TASK_PAUSED);
5416   GST_OBJECT_UNLOCK (pad);
5417
5418   /* wait for task function to finish, this lock is recursive so it does nothing
5419    * when the pause is called from the task itself */
5420   GST_PAD_STREAM_LOCK (pad);
5421   GST_PAD_STREAM_UNLOCK (pad);
5422
5423   return res;
5424
5425 no_task:
5426   {
5427     GST_DEBUG_OBJECT (pad, "pad has no task");
5428     GST_OBJECT_UNLOCK (pad);
5429     return FALSE;
5430   }
5431 }
5432
5433 /**
5434  * gst_pad_stop_task:
5435  * @pad: the #GstPad to stop the task of
5436  *
5437  * Stop the task of @pad. This function will also make sure that the
5438  * function executed by the task will effectively stop if not called
5439  * from the GstTaskFunction.
5440  *
5441  * This function will deadlock if called from the GstTaskFunction of
5442  * the task. Use gst_task_pause() instead.
5443  *
5444  * Regardless of whether the pad has a task, the stream lock is acquired and
5445  * released so as to ensure that streaming through this pad has finished.
5446  *
5447  * Returns: a TRUE if the task could be stopped or FALSE on error.
5448  */
5449 gboolean
5450 gst_pad_stop_task (GstPad * pad)
5451 {
5452   GstTask *task;
5453   gboolean res;
5454
5455   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5456
5457   GST_DEBUG_OBJECT (pad, "stop task");
5458
5459   GST_OBJECT_LOCK (pad);
5460   task = GST_PAD_TASK (pad);
5461   if (task == NULL)
5462     goto no_task;
5463   GST_PAD_TASK (pad) = NULL;
5464   res = gst_task_set_state (task, GST_TASK_STOPPED);
5465   GST_OBJECT_UNLOCK (pad);
5466
5467   GST_PAD_STREAM_LOCK (pad);
5468   GST_PAD_STREAM_UNLOCK (pad);
5469
5470   if (!gst_task_join (task))
5471     goto join_failed;
5472
5473   gst_object_unref (task);
5474
5475   return res;
5476
5477 no_task:
5478   {
5479     GST_DEBUG_OBJECT (pad, "no task");
5480     GST_OBJECT_UNLOCK (pad);
5481
5482     GST_PAD_STREAM_LOCK (pad);
5483     GST_PAD_STREAM_UNLOCK (pad);
5484
5485     /* this is not an error */
5486     return TRUE;
5487   }
5488 join_failed:
5489   {
5490     /* this is bad, possibly the application tried to join the task from
5491      * the task's thread. We install the task again so that it will be stopped
5492      * again from the right thread next time hopefully. */
5493     GST_OBJECT_LOCK (pad);
5494     GST_DEBUG_OBJECT (pad, "join failed");
5495     /* we can only install this task if there was no other task */
5496     if (GST_PAD_TASK (pad) == NULL)
5497       GST_PAD_TASK (pad) = task;
5498     GST_OBJECT_UNLOCK (pad);
5499
5500     return FALSE;
5501   }
5502 }
5503
5504 /**
5505  * gst_pad_probe_info_get_event:
5506  * @info: a #GstPadProbeInfo
5507  *
5508  * Returns: (transfer none): The #GstEvent from the probe
5509  */
5510
5511 GstEvent *
5512 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
5513 {
5514   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
5515           GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
5516
5517   return GST_PAD_PROBE_INFO_EVENT (info);
5518 }
5519
5520
5521 /**
5522  * gst_pad_probe_info_get_query:
5523  * @info: a #GstPadProbeInfo
5524  *
5525  * Returns: (transfer none): The #GstQuery from the probe
5526  */
5527
5528 GstQuery *
5529 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
5530 {
5531   g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
5532           GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
5533
5534   return GST_PAD_PROBE_INFO_QUERY (info);
5535 }
5536
5537 /**
5538  * gst_pad_probe_info_get_buffer:
5539  * @info: a #GstPadProbeInfo
5540  *
5541  * Returns: (transfer none): The #GstBuffer from the probe
5542  */
5543
5544 GstBuffer *
5545 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
5546 {
5547   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
5548
5549   return GST_PAD_PROBE_INFO_BUFFER (info);
5550 }
5551
5552 /**
5553  * gst_pad_probe_info_get_bufferlist:
5554  * @info: a #GstPadProbeInfo
5555  *
5556  * Returns: (transfer none): The #GstBufferlist from the probe
5557  */
5558
5559 GstBufferList *
5560 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
5561 {
5562   g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
5563
5564   return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
5565 }