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