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