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