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