pad: use new gst_value_fixate instead
[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   GstPadLinkFunction srcfunc, sinkfunc;
2045
2046   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2047   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2048   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2049   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2050       GST_PAD_LINK_WRONG_DIRECTION);
2051
2052   /* Notify the parent early. See gst_pad_unlink for details. */
2053   if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2054     if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2055       gst_element_post_message (parent,
2056           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2057               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2058     } else {
2059       gst_object_unref (parent);
2060       parent = NULL;
2061     }
2062   }
2063
2064   /* prepare will also lock the two pads */
2065   result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2066
2067   if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2068     goto done;
2069
2070   /* must set peers before calling the link function */
2071   GST_PAD_PEER (srcpad) = sinkpad;
2072   GST_PAD_PEER (sinkpad) = srcpad;
2073
2074   /* make sure we update events */
2075   prepare_event_update (srcpad, sinkpad);
2076
2077   /* get the link functions */
2078   srcfunc = GST_PAD_LINKFUNC (srcpad);
2079   sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2080
2081   if (G_UNLIKELY (srcfunc || sinkfunc)) {
2082     /* custom link functions, execute them */
2083     GST_OBJECT_UNLOCK (sinkpad);
2084     GST_OBJECT_UNLOCK (srcpad);
2085
2086     if (srcfunc) {
2087       /* this one will call the peer link function */
2088       result = srcfunc (srcpad, sinkpad);
2089     } else if (sinkfunc) {
2090       /* if no source link function, we need to call the sink link
2091        * function ourselves. */
2092       result = sinkfunc (sinkpad, srcpad);
2093     }
2094
2095     GST_OBJECT_LOCK (srcpad);
2096     GST_OBJECT_LOCK (sinkpad);
2097
2098     /* we released the lock, check if the same pads are linked still */
2099     if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2100       goto concurrent_link;
2101
2102     if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2103       goto link_failed;
2104   }
2105   GST_OBJECT_UNLOCK (sinkpad);
2106   GST_OBJECT_UNLOCK (srcpad);
2107
2108   /* fire off a signal to each of the pads telling them
2109    * that they've been linked */
2110   g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2111   g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2112
2113   GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2114       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2115
2116   gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2117
2118 done:
2119   if (G_LIKELY (parent)) {
2120     gst_element_post_message (parent,
2121         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2122             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2123     gst_object_unref (parent);
2124   }
2125
2126   return result;
2127
2128   /* ERRORS */
2129 concurrent_link:
2130   {
2131     GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2132         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2133     GST_OBJECT_UNLOCK (sinkpad);
2134     GST_OBJECT_UNLOCK (srcpad);
2135
2136     /* The other link operation succeeded first */
2137     result = GST_PAD_LINK_WAS_LINKED;
2138     goto done;
2139   }
2140 link_failed:
2141   {
2142     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2143         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2144
2145     GST_PAD_PEER (srcpad) = NULL;
2146     GST_PAD_PEER (sinkpad) = NULL;
2147
2148     GST_OBJECT_UNLOCK (sinkpad);
2149     GST_OBJECT_UNLOCK (srcpad);
2150
2151     goto done;
2152   }
2153 }
2154
2155 /**
2156  * gst_pad_link:
2157  * @srcpad: the source #GstPad to link.
2158  * @sinkpad: the sink #GstPad to link.
2159  *
2160  * Links the source pad and the sink pad.
2161  *
2162  * Returns: A result code indicating if the connection worked or
2163  *          what went wrong.
2164  *
2165  * MT Safe.
2166  */
2167 GstPadLinkReturn
2168 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2169 {
2170   return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2171 }
2172
2173 static void
2174 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2175 {
2176   GstPadTemplate **template_p;
2177
2178   /* this function would need checks if it weren't static */
2179
2180   GST_OBJECT_LOCK (pad);
2181   template_p = &pad->padtemplate;
2182   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2183   GST_OBJECT_UNLOCK (pad);
2184
2185   if (templ)
2186     gst_pad_template_pad_created (templ, pad);
2187 }
2188
2189 /**
2190  * gst_pad_get_pad_template:
2191  * @pad: a #GstPad.
2192  *
2193  * Gets the template for @pad.
2194  *
2195  * Returns: (transfer full): the #GstPadTemplate from which this pad was
2196  *     instantiated, or %NULL if this pad has no template. Unref after
2197  *     usage.
2198  */
2199 GstPadTemplate *
2200 gst_pad_get_pad_template (GstPad * pad)
2201 {
2202   GstPadTemplate *templ;
2203
2204   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2205
2206   templ = GST_PAD_PAD_TEMPLATE (pad);
2207
2208   return (templ ? gst_object_ref (templ) : NULL);
2209 }
2210
2211 /* should be called with the pad LOCK held */
2212 /* refs the caps, so caller is responsible for getting it unreffed */
2213 static GstCaps *
2214 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2215 {
2216   GstCaps *result = NULL;
2217   GstPadTemplate *templ;
2218   gboolean fixed_caps;
2219
2220   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2221
2222   fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2223
2224   if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2225     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2226         "dispatching to pad getcaps function with "
2227         "filter %" GST_PTR_FORMAT, filter);
2228
2229     GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2230     GST_OBJECT_UNLOCK (pad);
2231     result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2232     GST_OBJECT_LOCK (pad);
2233     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2234
2235     if (result == NULL) {
2236       g_critical ("pad %s:%s returned NULL caps from getcaps function",
2237           GST_DEBUG_PAD_NAME (pad));
2238     } else {
2239       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2240           "pad getcaps returned %" GST_PTR_FORMAT, result);
2241 #ifndef G_DISABLE_ASSERT
2242       /* check that the returned caps are a real subset of the template caps */
2243       if (GST_PAD_PAD_TEMPLATE (pad)) {
2244         const GstCaps *templ_caps =
2245             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2246         if (!gst_caps_is_subset (result, templ_caps)) {
2247           GstCaps *temp;
2248
2249           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2250               "pad returned caps %" GST_PTR_FORMAT
2251               " which are not a real subset of its template caps %"
2252               GST_PTR_FORMAT, result, templ_caps);
2253           g_warning
2254               ("pad %s:%s returned caps which are not a real "
2255               "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2256           temp = gst_caps_intersect (templ_caps, result);
2257           gst_caps_unref (result);
2258           result = temp;
2259         }
2260       }
2261       if (filter) {
2262         if (!gst_caps_is_subset (result, filter)) {
2263           GstCaps *temp;
2264
2265           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2266               "pad returned caps %" GST_PTR_FORMAT
2267               " which are not a real subset of the filter caps %"
2268               GST_PTR_FORMAT, result, filter);
2269           g_warning ("pad %s:%s returned caps which are not a real "
2270               "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2271           /* FIXME: Order? But shouldn't happen anyway... */
2272           temp =
2273               gst_caps_intersect_full (filter, result,
2274               GST_CAPS_INTERSECT_FIRST);
2275           gst_caps_unref (result);
2276           result = temp;
2277         }
2278       }
2279 #endif
2280       goto done;
2281     }
2282   }
2283   if (fixed_caps && (result = get_pad_caps (pad))) {
2284     if (filter) {
2285       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2286           "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2287           GST_PTR_FORMAT, result, result, filter, filter);
2288       result =
2289           gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2290       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2291           result);
2292     } else {
2293       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2294           "using pad caps %p %" GST_PTR_FORMAT, result, result);
2295       result = gst_caps_ref (result);
2296     }
2297     goto done;
2298   }
2299   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2300     result = GST_PAD_TEMPLATE_CAPS (templ);
2301
2302     if (filter) {
2303       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2304           "using pad template %p with caps %p %" GST_PTR_FORMAT
2305           " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2306           filter);
2307       result =
2308           gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2309       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2310           result);
2311     } else {
2312       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2313           "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2314           result);
2315       result = gst_caps_ref (result);
2316     }
2317
2318     goto done;
2319   }
2320   if (!fixed_caps && (result = get_pad_caps (pad))) {
2321     if (filter) {
2322       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2323           "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2324           GST_PTR_FORMAT, result, result, filter, filter);
2325       result =
2326           gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2327       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2328           result);
2329     } else {
2330       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2331           "using pad caps %p %" GST_PTR_FORMAT, result, result);
2332       result = gst_caps_ref (result);
2333     }
2334
2335     goto done;
2336   }
2337
2338   /* this almost never happens */
2339   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2340   result = gst_caps_new_empty ();
2341
2342 done:
2343   return result;
2344 }
2345
2346 /**
2347  * gst_pad_has_current_caps:
2348  * @pad: a  #GstPad to check
2349  *
2350  * Check if @pad has caps set on it with gst_pad_set_caps().
2351  *
2352  * Returns: TRUE when @pad has caps associated with it.
2353  */
2354 gboolean
2355 gst_pad_has_current_caps (GstPad * pad)
2356 {
2357   gboolean result;
2358
2359   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2360
2361   GST_OBJECT_LOCK (pad);
2362   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2363   result = (get_pad_caps (pad) != NULL);
2364   GST_OBJECT_UNLOCK (pad);
2365
2366   return result;
2367 }
2368
2369 /**
2370  * gst_pad_get_current_caps:
2371  * @pad: a  #GstPad to get the current capabilities of.
2372  *
2373  * Gets the capabilities currently configured on @pad with the last call to
2374  * gst_pad_set_caps().
2375  *
2376  * Returns: the current caps of the pad with incremented ref-count.
2377  */
2378 GstCaps *
2379 gst_pad_get_current_caps (GstPad * pad)
2380 {
2381   GstCaps *result;
2382
2383   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2384
2385   GST_OBJECT_LOCK (pad);
2386   if ((result = get_pad_caps (pad)))
2387     gst_caps_ref (result);
2388   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2389       "get current pad caps %" GST_PTR_FORMAT, result);
2390   GST_OBJECT_UNLOCK (pad);
2391
2392   return result;
2393 }
2394
2395 /**
2396  * gst_pad_get_caps:
2397  * @pad: a  #GstPad to get the capabilities of.
2398  * @filter: suggested #GstCaps.
2399  *
2400  * Gets the capabilities this pad can produce or consume.
2401  * Note that this method doesn't necessarily return the caps set by
2402  * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2403  * gst_pad_get_caps returns all possible caps a pad can operate with, using
2404  * the pad's get_caps function;
2405  * this returns the pad template caps if not explicitly set.
2406  *
2407  * When called on sinkpads @filter contains the caps that
2408  * upstream could produce in the order preferred by upstream. When
2409  * called on srcpads @filter contains the caps accepted by
2410  * downstream in the preffered order. @filter might be %NULL but
2411  * if it is not %NULL the returned caps will be a subset of @filter.
2412  *
2413  * Note that this function does not return writable #GstCaps, use
2414  * gst_caps_make_writable() before modifying the caps.
2415  *
2416  * Returns: the caps of the pad with incremented ref-count.
2417  */
2418 GstCaps *
2419 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2420 {
2421   GstCaps *result = NULL;
2422
2423   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2424   g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2425
2426   GST_OBJECT_LOCK (pad);
2427
2428   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2429
2430   result = gst_pad_get_caps_unlocked (pad, filter);
2431
2432   GST_OBJECT_UNLOCK (pad);
2433
2434   return result;
2435 }
2436
2437
2438 /**
2439  * gst_pad_peer_get_caps:
2440  * @pad: a  #GstPad to get the capabilities of.
2441  * @filter: a #GstCaps filter.
2442  *
2443  * Gets the capabilities of the peer connected to this pad. Similar to
2444  * gst_pad_get_caps().
2445  *
2446  * When called on srcpads @filter contains the caps that
2447  * upstream could produce in the order preferred by upstream. When
2448  * called on sinkpads @filter contains the caps accepted by
2449  * downstream in the preffered order. @filter might be %NULL but
2450  * if it is not %NULL the returned caps will be a subset of @filter.
2451  *
2452  * Returns: the caps of the peer pad with incremented ref-count. This function
2453  * returns %NULL when there is no peer pad.
2454  */
2455 GstCaps *
2456 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2457 {
2458   GstPad *peerpad;
2459   GstCaps *result = NULL;
2460
2461   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2462   g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2463
2464   GST_OBJECT_LOCK (pad);
2465
2466   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2467
2468   peerpad = GST_PAD_PEER (pad);
2469   if (G_UNLIKELY (peerpad == NULL))
2470     goto no_peer;
2471
2472   gst_object_ref (peerpad);
2473   GST_OBJECT_UNLOCK (pad);
2474
2475   result = gst_pad_get_caps (peerpad, filter);
2476
2477   gst_object_unref (peerpad);
2478
2479   return result;
2480
2481 no_peer:
2482   {
2483     GST_OBJECT_UNLOCK (pad);
2484     return NULL;
2485   }
2486 }
2487
2488 static gboolean
2489 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2490 {
2491   GstStructure *s = data;
2492   GValue v = { 0 };
2493
2494   if (gst_value_fixate (&v, value)) {
2495     gst_structure_id_set_value (s, field_id, &v);
2496     g_value_unset (&v);
2497   }
2498   return TRUE;
2499 }
2500
2501 static void
2502 gst_pad_default_fixate (GstPad * pad, GstCaps * caps)
2503 {
2504   GstStructure *s;
2505
2506   /* default fixation */
2507   gst_caps_truncate (caps);
2508   s = gst_caps_get_structure (caps, 0);
2509   gst_structure_foreach (s, default_fixate, s);
2510 }
2511
2512 /**
2513  * gst_pad_fixate_caps:
2514  * @pad: a  #GstPad to fixate
2515  * @caps: the  #GstCaps to fixate
2516  *
2517  * Fixate a caps on the given pad. Modifies the caps in place, so you should
2518  * make sure that the caps are actually writable (see gst_caps_make_writable()).
2519  */
2520 void
2521 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2522 {
2523   GstPadFixateCapsFunction fixatefunc;
2524
2525   g_return_if_fail (GST_IS_PAD (pad));
2526   g_return_if_fail (caps != NULL);
2527   g_return_if_fail (!gst_caps_is_empty (caps));
2528   g_return_if_fail (!gst_caps_is_any (caps));
2529
2530   if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2531     return;
2532
2533   g_return_if_fail (gst_caps_is_writable (caps));
2534
2535   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2536   if (fixatefunc) {
2537     fixatefunc (pad, caps);
2538   }
2539   gst_pad_default_fixate (pad, caps);
2540 }
2541
2542 /* Default accept caps implementation just checks against
2543  * against the allowed caps for the pad */
2544 static gboolean
2545 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2546 {
2547   /* get the caps and see if it intersects to something not empty */
2548   GstCaps *allowed;
2549   gboolean result = FALSE;
2550
2551   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2552
2553   allowed = gst_pad_get_caps (pad, NULL);
2554   if (!allowed)
2555     goto nothing_allowed;
2556
2557   GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2558
2559   result = gst_caps_can_intersect (allowed, caps);
2560
2561   gst_caps_unref (allowed);
2562
2563   return result;
2564
2565   /* ERRORS */
2566 nothing_allowed:
2567   {
2568     GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2569     return FALSE;
2570   }
2571 }
2572
2573 /**
2574  * gst_pad_accept_caps:
2575  * @pad: a #GstPad to check
2576  * @caps: a #GstCaps to check on the pad
2577  *
2578  * Check if the given pad accepts the caps.
2579  *
2580  * Returns: TRUE if the pad can accept the caps.
2581  */
2582 gboolean
2583 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2584 {
2585   gboolean result;
2586   GstPadAcceptCapsFunction acceptfunc;
2587 #if 0
2588   GstCaps *existing = NULL;
2589 #endif
2590
2591   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2592
2593   /* any pad can be unnegotiated */
2594   if (caps == NULL)
2595     return TRUE;
2596
2597   /* lock for checking the existing caps */
2598   GST_OBJECT_LOCK (pad);
2599   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2600 #if 0
2601   /* The current caps on a pad are trivially acceptable */
2602   if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2603     if (caps == existing || gst_caps_is_equal (caps, existing))
2604       goto is_same_caps;
2605   }
2606 #endif
2607   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2608   GST_OBJECT_UNLOCK (pad);
2609
2610   if (G_LIKELY (acceptfunc)) {
2611     /* we can call the function */
2612     result = acceptfunc (pad, caps);
2613     GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2614   } else {
2615     /* Only null if the element explicitly unset it */
2616     result = gst_pad_acceptcaps_default (pad, caps);
2617     GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2618   }
2619   return result;
2620
2621 #if 0
2622 is_same_caps:
2623   {
2624     GST_DEBUG_OBJECT (pad, "pad had same caps");
2625     GST_OBJECT_UNLOCK (pad);
2626     return TRUE;
2627   }
2628 #endif
2629 }
2630
2631 /**
2632  * gst_pad_peer_accept_caps:
2633  * @pad: a  #GstPad to check the peer of
2634  * @caps: a #GstCaps to check on the pad
2635  *
2636  * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2637  * returns TRUE.
2638  *
2639  * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2640  */
2641 gboolean
2642 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2643 {
2644   GstPad *peerpad;
2645   gboolean result;
2646
2647   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2648
2649   GST_OBJECT_LOCK (pad);
2650
2651   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2652
2653   peerpad = GST_PAD_PEER (pad);
2654   if (G_UNLIKELY (peerpad == NULL))
2655     goto no_peer;
2656
2657   gst_object_ref (peerpad);
2658   /* release lock before calling external methods but keep ref to pad */
2659   GST_OBJECT_UNLOCK (pad);
2660
2661   result = gst_pad_accept_caps (peerpad, caps);
2662
2663   gst_object_unref (peerpad);
2664
2665   return result;
2666
2667 no_peer:
2668   {
2669     GST_OBJECT_UNLOCK (pad);
2670     return TRUE;
2671   }
2672 }
2673
2674 /**
2675  * gst_pad_set_caps:
2676  * @pad: a  #GstPad to set the capabilities of.
2677  * @caps: (transfer none): a #GstCaps to set.
2678  *
2679  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2680  * caps on the pad will be unreffed. This function refs the caps so you should
2681  * unref if as soon as you don't need it anymore.
2682  * It is possible to set NULL caps, which will make the pad unnegotiated
2683  * again.
2684  *
2685  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2686  * or bad parameters were provided to this function.
2687  *
2688  * MT safe.
2689  */
2690 gboolean
2691 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2692 {
2693   GstEvent *event;
2694   gboolean res = TRUE;
2695
2696   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2697   g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2698
2699   event = gst_event_new_caps (caps);
2700
2701   if (GST_PAD_IS_SRC (pad))
2702     res = gst_pad_push_event (pad, event);
2703   else
2704     res = gst_pad_send_event (pad, event);
2705
2706   return res;
2707 }
2708
2709 static gboolean
2710 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2711 {
2712   GstPadSetCapsFunction setcaps;
2713
2714   GST_OBJECT_LOCK (pad);
2715   setcaps = GST_PAD_SETCAPSFUNC (pad);
2716
2717   /* call setcaps function to configure the pad only if the
2718    * caps is not NULL */
2719   if (setcaps != NULL) {
2720     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2721       GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2722       GST_OBJECT_UNLOCK (pad);
2723       if (!setcaps (pad, caps))
2724         goto setcaps_failed;
2725       GST_OBJECT_LOCK (pad);
2726       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2727     } else {
2728       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2729     }
2730   }
2731   GST_OBJECT_UNLOCK (pad);
2732
2733   g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2734
2735   return TRUE;
2736
2737   /* ERRORS */
2738 setcaps_failed:
2739   {
2740     return FALSE;
2741   }
2742 }
2743
2744 static gboolean
2745 do_event_function (GstPad * pad, GstEvent * event,
2746     GstPadEventFunction eventfunc)
2747 {
2748   gboolean result = TRUE;
2749   GstCaps *caps, *templ;
2750
2751   switch (GST_EVENT_TYPE (event)) {
2752     case GST_EVENT_CAPS:
2753     {
2754       /* backwards compatibility mode for caps */
2755       gst_event_parse_caps (event, &caps);
2756
2757       /* See if pad accepts the caps */
2758       templ = gst_pad_get_pad_template_caps (pad);
2759       if (!gst_caps_can_intersect (caps, templ))
2760         goto not_accepted;
2761
2762       if (!gst_pad_call_setcaps (pad, caps))
2763         goto not_accepted;
2764
2765       gst_caps_unref (templ);
2766       break;
2767     }
2768     default:
2769       break;
2770   }
2771
2772   GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2773   result = eventfunc (pad, event);
2774
2775   return result;
2776
2777   /* ERRORS */
2778 not_accepted:
2779   {
2780     gst_caps_unref (templ);
2781     gst_event_unref (event);
2782     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2783         "caps %" GST_PTR_FORMAT " not accepted", caps);
2784     return FALSE;
2785   }
2786 }
2787
2788 /* function to send all pending events on the sinkpad to the event
2789  * function and collect the results. This function should be called with
2790  * the object lock. The object lock might be released by this function.
2791  */
2792 static GstFlowReturn
2793 gst_pad_update_events (GstPad * pad)
2794 {
2795   GstFlowReturn ret = GST_FLOW_OK;
2796   guint i;
2797   GstPadEventFunction eventfunc;
2798   GstEvent *event;
2799
2800   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2801     goto no_function;
2802
2803 restart:
2804   for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2805     gboolean res;
2806     PadEvent *ev;
2807
2808     ev = &pad->priv->events[i];
2809
2810     /* skip without pending event */
2811     if ((event = ev->pending) == NULL)
2812       continue;
2813
2814     gst_event_ref (event);
2815     GST_OBJECT_UNLOCK (pad);
2816
2817     res = do_event_function (pad, event, eventfunc);
2818
2819     GST_OBJECT_LOCK (pad);
2820     /* things could have changed while we release the lock, check if we still
2821      * are handling the same event, if we don't something changed and we have
2822      * to try again. FIXME. we need a cookie here. */
2823     if (event != ev->pending) {
2824       GST_DEBUG_OBJECT (pad, "events changed, restarting");
2825       goto restart;
2826     }
2827
2828     /* remove the event from the pending entry in all cases */
2829     ev->pending = NULL;
2830
2831     if (res) {
2832       /* make the event active */
2833       if (ev->event)
2834         gst_event_unref (ev->event);
2835       ev->event = event;
2836     } else {
2837       gst_event_unref (event);
2838       ret = GST_FLOW_ERROR;
2839     }
2840   }
2841   /* when we get here all events were successfully updated. */
2842
2843   return ret;
2844
2845   /* ERRORS */
2846 no_function:
2847   {
2848     g_warning ("pad %s:%s has no event handler, file a bug.",
2849         GST_DEBUG_PAD_NAME (pad));
2850     return GST_FLOW_NOT_SUPPORTED;
2851   }
2852 }
2853
2854 /**
2855  * gst_pad_get_pad_template_caps:
2856  * @pad: a #GstPad to get the template capabilities from.
2857  *
2858  * Gets the capabilities for @pad's template.
2859  *
2860  * Returns: (transfer full): the #GstCaps of this pad template.
2861  * Unref after usage.
2862  */
2863 GstCaps *
2864 gst_pad_get_pad_template_caps (GstPad * pad)
2865 {
2866   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2867
2868   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2869
2870   if (GST_PAD_PAD_TEMPLATE (pad))
2871     return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2872
2873   return gst_static_caps_get (&anycaps);
2874 }
2875
2876 /**
2877  * gst_pad_get_peer:
2878  * @pad: a #GstPad to get the peer of.
2879  *
2880  * Gets the peer of @pad. This function refs the peer pad so
2881  * you need to unref it after use.
2882  *
2883  * Returns: (transfer full): the peer #GstPad. Unref after usage.
2884  *
2885  * MT safe.
2886  */
2887 GstPad *
2888 gst_pad_get_peer (GstPad * pad)
2889 {
2890   GstPad *result;
2891
2892   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2893
2894   GST_OBJECT_LOCK (pad);
2895   result = GST_PAD_PEER (pad);
2896   if (result)
2897     gst_object_ref (result);
2898   GST_OBJECT_UNLOCK (pad);
2899
2900   return result;
2901 }
2902
2903 /**
2904  * gst_pad_get_allowed_caps:
2905  * @pad: a #GstPad.
2906  *
2907  * Gets the capabilities of the allowed media types that can flow through
2908  * @pad and its peer.
2909  *
2910  * The allowed capabilities is calculated as the intersection of the results of
2911  * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2912  * on the resulting caps.
2913  *
2914  * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2915  *     caps when you no longer need it. This function returns NULL when @pad
2916  *     has no peer.
2917  *
2918  * MT safe.
2919  */
2920 GstCaps *
2921 gst_pad_get_allowed_caps (GstPad * pad)
2922 {
2923   GstCaps *mycaps;
2924   GstCaps *caps;
2925   GstCaps *peercaps;
2926   GstPad *peer;
2927
2928   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2929
2930   GST_OBJECT_LOCK (pad);
2931   peer = GST_PAD_PEER (pad);
2932   if (G_UNLIKELY (peer == NULL))
2933     goto no_peer;
2934
2935   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2936
2937   gst_object_ref (peer);
2938   GST_OBJECT_UNLOCK (pad);
2939   mycaps = gst_pad_get_caps (pad, NULL);
2940
2941   peercaps = gst_pad_get_caps (peer, NULL);
2942   gst_object_unref (peer);
2943
2944   caps = gst_caps_intersect (mycaps, peercaps);
2945   gst_caps_unref (peercaps);
2946   gst_caps_unref (mycaps);
2947
2948   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2949       caps);
2950
2951   return caps;
2952
2953 no_peer:
2954   {
2955     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2956     GST_OBJECT_UNLOCK (pad);
2957
2958     return NULL;
2959   }
2960 }
2961
2962 /**
2963  * gst_pad_get_negotiated_caps:
2964  * @pad: a #GstPad.
2965  *
2966  * Gets the capabilities of the media type that currently flows through @pad
2967  * and its peer.
2968  *
2969  * This function can be used on both src and sinkpads. Note that srcpads are
2970  * always negotiated before sinkpads so it is possible that the negotiated caps
2971  * on the srcpad do not match the negotiated caps of the peer.
2972  *
2973  * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2974  *     the caps when you no longer need it. This function returns NULL when
2975  *     the @pad has no peer or is not negotiated yet.
2976  *
2977  * MT safe.
2978  */
2979 GstCaps *
2980 gst_pad_get_negotiated_caps (GstPad * pad)
2981 {
2982   GstCaps *caps = NULL;
2983   GstPad *peer;
2984
2985   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2986
2987   GST_OBJECT_LOCK (pad);
2988
2989   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2990     goto no_peer;
2991
2992   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2993
2994   if ((caps = get_pad_caps (pad)))
2995     gst_caps_ref (caps);
2996
2997   GST_OBJECT_UNLOCK (pad);
2998
2999   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
3000       caps);
3001
3002   return caps;
3003
3004 no_peer:
3005   {
3006     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3007     GST_OBJECT_UNLOCK (pad);
3008     return NULL;
3009   }
3010 }
3011
3012 /**
3013  * gst_pad_iterate_internal_links_default:
3014  * @pad: the #GstPad to get the internal links of.
3015  *
3016  * Iterate the list of pads to which the given pad is linked to inside of
3017  * the parent element.
3018  * This is the default handler, and thus returns an iterator of all of the
3019  * pads inside the parent element with opposite direction.
3020  *
3021  * The caller must free this iterator after use with gst_iterator_free().
3022  *
3023  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3024  * returned pad with gst_object_unref().
3025  *
3026  * Since: 0.10.21
3027  */
3028 GstIterator *
3029 gst_pad_iterate_internal_links_default (GstPad * pad)
3030 {
3031   GstIterator *res;
3032   GList **padlist;
3033   guint32 *cookie;
3034   GMutex *lock;
3035   gpointer owner;
3036
3037   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3038
3039   {
3040     GstElement *parent;
3041
3042     GST_OBJECT_LOCK (pad);
3043     parent = GST_PAD_PARENT (pad);
3044     if (!parent || !GST_IS_ELEMENT (parent))
3045       goto no_parent;
3046
3047     gst_object_ref (parent);
3048     GST_OBJECT_UNLOCK (pad);
3049
3050     if (pad->direction == GST_PAD_SRC)
3051       padlist = &parent->sinkpads;
3052     else
3053       padlist = &parent->srcpads;
3054
3055     GST_DEBUG_OBJECT (pad, "Making iterator");
3056
3057     cookie = &parent->pads_cookie;
3058     owner = parent;
3059     lock = GST_OBJECT_GET_LOCK (parent);
3060   }
3061
3062   res = gst_iterator_new_list (GST_TYPE_PAD,
3063       lock, cookie, padlist, (GObject *) owner, NULL);
3064
3065   gst_object_unref (owner);
3066
3067   return res;
3068
3069   /* ERRORS */
3070 no_parent:
3071   {
3072     GST_OBJECT_UNLOCK (pad);
3073     GST_DEBUG_OBJECT (pad, "no parent element");
3074     return NULL;
3075   }
3076 }
3077
3078 /**
3079  * gst_pad_iterate_internal_links:
3080  * @pad: the GstPad to get the internal links of.
3081  *
3082  * Gets an iterator for the pads to which the given pad is linked to inside
3083  * of the parent element.
3084  *
3085  * Each #GstPad element yielded by the iterator will have its refcount increased,
3086  * so unref after use.
3087  *
3088  * Free-function: gst_iterator_free
3089  *
3090  * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3091  *     pad does not have an iterator function configured. Use
3092  *     gst_iterator_free() after usage.
3093  *
3094  * Since: 0.10.21
3095  */
3096 GstIterator *
3097 gst_pad_iterate_internal_links (GstPad * pad)
3098 {
3099   GstIterator *res = NULL;
3100
3101   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3102
3103   if (GST_PAD_ITERINTLINKFUNC (pad))
3104     res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3105
3106   return res;
3107 }
3108
3109 static gboolean
3110 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3111 {
3112   gboolean result = FALSE;
3113   GstIterator *iter;
3114   gboolean done = FALSE;
3115   GValue item = { 0, };
3116   GstPad *eventpad;
3117   GList *pushed_pads = NULL;
3118
3119   GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3120       event, GST_EVENT_TYPE_NAME (event));
3121
3122   iter = gst_pad_iterate_internal_links (pad);
3123
3124   if (!iter)
3125     goto no_iter;
3126
3127   while (!done) {
3128     switch (gst_iterator_next (iter, &item)) {
3129       case GST_ITERATOR_OK:
3130         eventpad = g_value_get_object (&item);
3131
3132         /* if already pushed,  skip */
3133         if (g_list_find (pushed_pads, eventpad)) {
3134           g_value_reset (&item);
3135           break;
3136         }
3137
3138         if (GST_PAD_IS_SRC (eventpad)) {
3139           /* for each pad we send to, we should ref the event; it's up
3140            * to downstream to unref again when handled. */
3141           GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3142               event, GST_EVENT_TYPE_NAME (event),
3143               GST_DEBUG_PAD_NAME (eventpad));
3144           gst_event_ref (event);
3145           result |= gst_pad_push_event (eventpad, event);
3146         } else {
3147           /* we only send the event on one pad, multi-sinkpad elements
3148            * should implement a handler */
3149           GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3150               event, GST_EVENT_TYPE_NAME (event),
3151               GST_DEBUG_PAD_NAME (eventpad));
3152           result = gst_pad_push_event (eventpad, event);
3153           done = TRUE;
3154           event = NULL;
3155         }
3156
3157         pushed_pads = g_list_prepend (pushed_pads, eventpad);
3158
3159         g_value_reset (&item);
3160         break;
3161       case GST_ITERATOR_RESYNC:
3162         /* We don't reset the result here because we don't push the event
3163          * again on pads that got the event already and because we need
3164          * to consider the result of the previous pushes */
3165         gst_iterator_resync (iter);
3166         break;
3167       case GST_ITERATOR_ERROR:
3168         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3169         done = TRUE;
3170         break;
3171       case GST_ITERATOR_DONE:
3172         done = TRUE;
3173         break;
3174     }
3175   }
3176   g_value_unset (&item);
3177   gst_iterator_free (iter);
3178
3179 no_iter:
3180
3181   /* If this is a sinkpad and we don't have pads to send the event to, we
3182    * return TRUE. This is so that when using the default handler on a sink
3183    * element, we don't fail to push it. */
3184   if (!pushed_pads)
3185     result = GST_PAD_IS_SINK (pad);
3186
3187   g_list_free (pushed_pads);
3188
3189   /* we handled the incoming event so we unref once */
3190   if (event) {
3191     GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3192     gst_event_unref (event);
3193   }
3194
3195   return result;
3196 }
3197
3198 /**
3199  * gst_pad_event_default:
3200  * @pad: a #GstPad to call the default event handler on.
3201  * @event: (transfer full): the #GstEvent to handle.
3202  *
3203  * Invokes the default event handler for the given pad. End-of-stream and
3204  * discontinuity events are handled specially, and then the event is sent to all
3205  * pads internally linked to @pad. Note that if there are many possible sink
3206  * pads that are internally linked to @pad, only one will be sent an event.
3207  * Multi-sinkpad elements should implement custom event handlers.
3208  *
3209  * Returns: TRUE if the event was sent succesfully.
3210  */
3211 gboolean
3212 gst_pad_event_default (GstPad * pad, GstEvent * event)
3213 {
3214   gboolean result = TRUE, forward = TRUE;
3215
3216   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3217   g_return_val_if_fail (event != NULL, FALSE);
3218
3219   GST_LOG_OBJECT (pad, "default event handler");
3220
3221   switch (GST_EVENT_TYPE (event)) {
3222     case GST_EVENT_EOS:
3223     {
3224       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3225       gst_pad_pause_task (pad);
3226       break;
3227     }
3228     case GST_EVENT_CAPS:
3229     {
3230       /* don't forward by default */
3231       forward = FALSE;
3232       break;
3233     }
3234     default:
3235       break;
3236   }
3237
3238   if (forward)
3239     result = gst_pad_event_default_dispatch (pad, event);
3240   else
3241     gst_event_unref (event);
3242
3243   return result;
3244 }
3245
3246 /**
3247  * gst_pad_dispatcher:
3248  * @pad: a #GstPad to dispatch.
3249  * @dispatch: the #GstPadDispatcherFunction to call.
3250  * @data: (closure): gpointer user data passed to the dispatcher function.
3251  *
3252  * Invokes the given dispatcher function on each respective peer of
3253  * all pads that are internally linked to the given pad.
3254  * The GstPadDispatcherFunction should return TRUE when no further pads
3255  * need to be processed.
3256  *
3257  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3258  */
3259 gboolean
3260 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3261     gpointer data)
3262 {
3263   gboolean res = FALSE;
3264   GstIterator *iter = NULL;
3265   gboolean done = FALSE;
3266   GValue item = { 0, };
3267
3268   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3269   g_return_val_if_fail (dispatch != NULL, FALSE);
3270
3271   iter = gst_pad_iterate_internal_links (pad);
3272
3273   if (!iter)
3274     goto no_iter;
3275
3276   while (!done) {
3277     switch (gst_iterator_next (iter, &item)) {
3278       case GST_ITERATOR_OK:
3279       {
3280         GstPad *int_pad = g_value_get_object (&item);
3281         GstPad *int_peer = gst_pad_get_peer (int_pad);
3282
3283         if (int_peer) {
3284           GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3285               GST_DEBUG_PAD_NAME (int_peer));
3286           done = res = dispatch (int_peer, data);
3287           gst_object_unref (int_peer);
3288         } else {
3289           GST_DEBUG_OBJECT (int_pad, "no peer");
3290         }
3291         g_value_reset (&item);
3292       }
3293         break;
3294       case GST_ITERATOR_RESYNC:
3295         gst_iterator_resync (iter);
3296         break;
3297       case GST_ITERATOR_ERROR:
3298         done = TRUE;
3299         GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3300         break;
3301       case GST_ITERATOR_DONE:
3302         done = TRUE;
3303         break;
3304     }
3305   }
3306   g_value_unset (&item);
3307   gst_iterator_free (iter);
3308
3309   GST_DEBUG_OBJECT (pad, "done, result %d", res);
3310
3311 no_iter:
3312
3313   return res;
3314 }
3315
3316 /**
3317  * gst_pad_query:
3318  * @pad: a #GstPad to invoke the default query on.
3319  * @query: (transfer none): the #GstQuery to perform.
3320  *
3321  * Dispatches a query to a pad. The query should have been allocated by the
3322  * caller via one of the type-specific allocation functions. The element that
3323  * the pad belongs to is responsible for filling the query with an appropriate
3324  * response, which should then be parsed with a type-specific query parsing
3325  * function.
3326  *
3327  * Again, the caller is responsible for both the allocation and deallocation of
3328  * the query structure.
3329  *
3330  * Please also note that some queries might need a running pipeline to work.
3331  *
3332  * Returns: TRUE if the query could be performed.
3333  */
3334 gboolean
3335 gst_pad_query (GstPad * pad, GstQuery * query)
3336 {
3337   gboolean res;
3338   GstPadQueryFunction func;
3339
3340   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3341   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3342
3343   GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3344       GST_QUERY_TYPE_NAME (query));
3345
3346   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3347     goto no_func;
3348
3349   res = func (pad, query);
3350
3351   GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3352       GST_QUERY_TYPE_NAME (query), res);
3353
3354   return res;
3355
3356 no_func:
3357   {
3358     GST_DEBUG_OBJECT (pad, "had no query function");
3359     return FALSE;
3360   }
3361 }
3362
3363 /**
3364  * gst_pad_peer_query:
3365  * @pad: a #GstPad to invoke the peer query on.
3366  * @query: (transfer none): the #GstQuery to perform.
3367  *
3368  * Performs gst_pad_query() on the peer of @pad.
3369  *
3370  * The caller is responsible for both the allocation and deallocation of
3371  * the query structure.
3372  *
3373  * Returns: TRUE if the query could be performed. This function returns %FALSE
3374  * if @pad has no peer.
3375  *
3376  * Since: 0.10.15
3377  */
3378 gboolean
3379 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3380 {
3381   GstPad *peerpad;
3382   gboolean result;
3383
3384   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3385   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3386
3387   GST_OBJECT_LOCK (pad);
3388
3389   GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3390       GST_QUERY_TYPE_NAME (query));
3391
3392   peerpad = GST_PAD_PEER (pad);
3393   if (G_UNLIKELY (peerpad == NULL))
3394     goto no_peer;
3395
3396   gst_object_ref (peerpad);
3397   GST_OBJECT_UNLOCK (pad);
3398
3399   result = gst_pad_query (peerpad, query);
3400
3401   gst_object_unref (peerpad);
3402
3403   return result;
3404
3405   /* ERRORS */
3406 no_peer:
3407   {
3408     GST_WARNING_OBJECT (pad, "pad has no peer");
3409     GST_OBJECT_UNLOCK (pad);
3410     return FALSE;
3411   }
3412 }
3413
3414 /**
3415  * gst_pad_query_default:
3416  * @pad: a #GstPad to call the default query handler on.
3417  * @query: (transfer none): the #GstQuery to handle.
3418  *
3419  * Invokes the default query handler for the given pad.
3420  * The query is sent to all pads internally linked to @pad. Note that
3421  * if there are many possible sink pads that are internally linked to
3422  * @pad, only one will be sent the query.
3423  * Multi-sinkpad elements should implement custom query handlers.
3424  *
3425  * Returns: TRUE if the query was performed succesfully.
3426  */
3427 gboolean
3428 gst_pad_query_default (GstPad * pad, GstQuery * query)
3429 {
3430   switch (GST_QUERY_TYPE (query)) {
3431     case GST_QUERY_POSITION:
3432     case GST_QUERY_SEEKING:
3433     case GST_QUERY_FORMATS:
3434     case GST_QUERY_LATENCY:
3435     case GST_QUERY_JITTER:
3436     case GST_QUERY_RATE:
3437     case GST_QUERY_CONVERT:
3438     default:
3439       return gst_pad_dispatcher
3440           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3441   }
3442 }
3443
3444 static void
3445 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3446 {
3447   GstPad *pad = data->pad;
3448   GstProbeType flags;
3449   GstPadProbeCallback callback;
3450   GstProbeReturn ret;
3451
3452   /* if we have called this callback, do nothing */
3453   if (PROBE_COOKIE (hook) == data->cookie)
3454     return;
3455
3456   PROBE_COOKIE (hook) = data->cookie;
3457
3458   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3459
3460   /* check if type matches */
3461   if ((flags & GST_PROBE_TYPE_DATA & data->mask) == 0)
3462     return;
3463
3464   if ((flags & 0xc0 & data->mask) == 0)
3465     return;
3466
3467   if ((flags & 0x6) != (data->mask & 0x6))
3468     return;
3469
3470   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3471       "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3472
3473   callback = (GstPadProbeCallback) hook->func;
3474   if (callback == NULL)
3475     return;
3476
3477   GST_OBJECT_UNLOCK (pad);
3478
3479   ret = callback (pad, data->mask, data->type_data, hook->data);
3480
3481   GST_OBJECT_LOCK (pad);
3482
3483   switch (ret) {
3484     case GST_PROBE_REMOVE:
3485       /* remove the probe */
3486       GST_DEBUG_OBJECT (pad, "asked to remove hook");
3487       cleanup_hook (pad, hook);
3488       break;
3489     case GST_PROBE_DROP:
3490       /* need to drop the data, make sure other probes don't get called
3491        * anymore */
3492       GST_DEBUG_OBJECT (pad, "asked to drop item");
3493       data->mask = GST_PROBE_TYPE_INVALID;
3494       data->ret = GST_PROBE_DROP;
3495       break;
3496     case GST_PROBE_PASS:
3497       /* inform the pad block to let things pass */
3498       GST_DEBUG_OBJECT (pad, "asked to pass item");
3499       data->pass = TRUE;
3500       break;
3501     default:
3502       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3503       break;
3504   }
3505 }
3506
3507 #define PROBE(pad,mask,data,label)                \
3508   G_STMT_START {                                  \
3509     if (G_UNLIKELY (pad->num_probes)) {           \
3510       ret = do_probe_callbacks (pad, mask, data); \
3511       if (G_UNLIKELY (ret != GST_FLOW_OK))        \
3512         goto label;                               \
3513     }                                             \
3514   } G_STMT_END
3515
3516 static GstFlowReturn
3517 do_probe_callbacks (GstPad * pad, GstProbeType mask, gpointer type_data)
3518 {
3519   ProbeMarshall data;
3520   guint cookie;
3521
3522   data.pad = pad;
3523   data.mask = mask;
3524   data.type_data = type_data;
3525   data.ret = GST_PROBE_OK;
3526   data.pass = FALSE;
3527   data.cookie = pad->priv->probe_cookie++;
3528
3529 again:
3530   cookie = pad->priv->probe_cookie;
3531
3532   g_hook_list_marshal (&pad->probes, FALSE,
3533       (GHookMarshaller) probe_hook_marshal, &data);
3534
3535   /* if the list changed, call the new callbacks (they will not have their
3536    * cookie set to data.cookie */
3537   if (cookie != pad->priv->probe_cookie) {
3538     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3539         "probe list changed, restarting");
3540     goto again;
3541   }
3542
3543   if (data.ret == GST_PROBE_DROP)
3544     goto dropped;
3545
3546   if (data.pass)
3547     goto passed;
3548
3549   if (mask & GST_PROBE_TYPE_BLOCK) {
3550     while (GST_PAD_IS_BLOCKED (pad)) {
3551       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3552           "we are blocked %d times", pad->num_blocked);
3553
3554       /* we might have released the lock */
3555       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3556         goto flushing;
3557
3558       /* now we block the streaming thread. It can be unlocked when we
3559        * deactivate the pad (which will also set the FLUSHING flag) or
3560        * when the pad is unblocked. A flushing event will also unblock
3561        * the pad after setting the FLUSHING flag. */
3562       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3563           "Waiting to be unblocked or set flushing");
3564       GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3565       GST_PAD_BLOCK_WAIT (pad);
3566       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3567       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3568
3569       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3570         goto flushing;
3571     }
3572   }
3573
3574   return GST_FLOW_OK;
3575
3576   /* ERRORS */
3577 flushing:
3578   {
3579     GST_DEBUG_OBJECT (pad, "pad is flushing");
3580     return GST_FLOW_WRONG_STATE;
3581   }
3582 dropped:
3583   {
3584     GST_DEBUG_OBJECT (pad, "data is dropped");
3585     return GST_FLOW_CUSTOM_SUCCESS;
3586   }
3587 passed:
3588   {
3589     GST_DEBUG_OBJECT (pad, "data is passed");
3590     return GST_FLOW_OK;
3591   }
3592 }
3593
3594 /* pad offsets */
3595
3596 /**
3597  * gst_pad_get_offset:
3598  * @pad: a #GstPad
3599  *
3600  * Get the offset applied to the running time of @pad. @pad has to be a source
3601  * pad.
3602  *
3603  * Returns: the offset.
3604  */
3605 gint64
3606 gst_pad_get_offset (GstPad * pad)
3607 {
3608   gint64 result;
3609
3610   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3611
3612   GST_OBJECT_LOCK (pad);
3613   result = pad->offset;
3614   GST_OBJECT_UNLOCK (pad);
3615
3616   return result;
3617 }
3618
3619 /**
3620  * gst_pad_set_offset:
3621  * @pad: a #GstPad
3622  * @offset: the offset
3623  *
3624  * Set the offset that will be applied to the running time of @pad.
3625  */
3626 void
3627 gst_pad_set_offset (GstPad * pad, gint64 offset)
3628 {
3629   guint idx;
3630   GstPad *peer;
3631   GstPad *tmp = NULL;
3632
3633   g_return_if_fail (GST_IS_PAD (pad));
3634
3635   GST_OBJECT_LOCK (pad);
3636   /* if nothing changed, do nothing */
3637   if (pad->offset == offset)
3638     goto done;
3639
3640   pad->offset = offset;
3641
3642   /* if no peer, we just updated the offset */
3643   if ((peer = GST_PAD_PEER (pad)) == NULL)
3644     goto done;
3645
3646   /* switch pads around when dealing with a sinkpad */
3647   if (GST_PAD_IS_SINK (pad)) {
3648     /* ref the peer so it doesn't go away when we release the lock */
3649     tmp = gst_object_ref (peer);
3650     /* make sure we get the peer (the srcpad) */
3651     GST_OBJECT_UNLOCK (pad);
3652
3653     /* swap pads */
3654     peer = pad;
3655     pad = tmp;
3656
3657     GST_OBJECT_LOCK (pad);
3658     /* check if the pad didn't get relinked */
3659     if (GST_PAD_PEER (pad) != peer)
3660       goto done;
3661
3662     /* we can release the ref now */
3663     gst_object_unref (peer);
3664   }
3665
3666   /* the index of the segment event in the array */
3667   idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3668
3669   /* lock order is srcpad >> sinkpad */
3670   GST_OBJECT_LOCK (peer);
3671   /* take the current segment event, adjust it and then place
3672    * it on the sinkpad. events on the srcpad are always active. */
3673   if (replace_event (pad, peer, idx))
3674     GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3675
3676   GST_OBJECT_UNLOCK (peer);
3677
3678 done:
3679   GST_OBJECT_UNLOCK (pad);
3680 }
3681
3682
3683 /**********************************************************************
3684  * Data passing functions
3685  */
3686
3687 /* this is the chain function that does not perform the additional argument
3688  * checking for that little extra speed.
3689  */
3690 static inline GstFlowReturn
3691 gst_pad_chain_data_unchecked (GstPad * pad, GstProbeType type, void *data)
3692 {
3693   GstFlowReturn ret;
3694   gboolean needs_events;
3695
3696   GST_PAD_STREAM_LOCK (pad);
3697
3698   GST_OBJECT_LOCK (pad);
3699   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3700     goto flushing;
3701
3702   needs_events = GST_PAD_NEEDS_EVENTS (pad);
3703   if (G_UNLIKELY (needs_events)) {
3704     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3705
3706     GST_DEBUG_OBJECT (pad, "need to update all events");
3707     ret = gst_pad_update_events (pad);
3708     if (G_UNLIKELY (ret != GST_FLOW_OK))
3709       goto events_error;
3710   }
3711
3712   PROBE (pad, GST_PROBE_TYPE_PUSH | type, data, probe_stopped);
3713
3714   GST_OBJECT_UNLOCK (pad);
3715
3716   /* NOTE: we read the chainfunc unlocked.
3717    * we cannot hold the lock for the pad so we might send
3718    * the data to the wrong function. This is not really a
3719    * problem since functions are assigned at creation time
3720    * and don't change that often... */
3721   if (G_LIKELY (type & GST_PROBE_TYPE_BUFFER)) {
3722     GstPadChainFunction chainfunc;
3723
3724     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3725       goto no_function;
3726
3727     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3728         "calling chainfunction &%s with buffer %p",
3729         GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3730
3731     ret = chainfunc (pad, GST_BUFFER_CAST (data));
3732
3733     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3734         "called chainfunction &%s with buffer %p, returned %s",
3735         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3736   } else {
3737     GstPadChainListFunction chainlistfunc;
3738
3739     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3740       goto no_function;
3741
3742     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3743         "calling chainlistfunction &%s",
3744         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3745
3746     ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3747
3748     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3749         "called chainlistfunction &%s, returned %s",
3750         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3751   }
3752
3753   GST_PAD_STREAM_UNLOCK (pad);
3754
3755   return ret;
3756
3757   /* ERRORS */
3758 flushing:
3759   {
3760     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3761         "chaining, but pad was flushing");
3762     GST_OBJECT_UNLOCK (pad);
3763     GST_PAD_STREAM_UNLOCK (pad);
3764     return GST_FLOW_WRONG_STATE;
3765   }
3766 events_error:
3767   {
3768     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3769     GST_OBJECT_UNLOCK (pad);
3770     GST_PAD_STREAM_UNLOCK (pad);
3771     return ret;
3772   }
3773 probe_stopped:
3774   {
3775     GST_OBJECT_UNLOCK (pad);
3776     GST_PAD_STREAM_UNLOCK (pad);
3777     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3778
3779     switch (ret) {
3780       case GST_FLOW_CUSTOM_SUCCESS:
3781         GST_DEBUG_OBJECT (pad, "dropped buffer");
3782         ret = GST_FLOW_OK;
3783         break;
3784       default:
3785         GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3786         break;
3787     }
3788     return ret;
3789   }
3790 no_function:
3791   {
3792     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3793     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3794         "pushing, but not chainhandler");
3795     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3796         ("push on pad %s:%s but it has no chainfunction",
3797             GST_DEBUG_PAD_NAME (pad)));
3798     GST_PAD_STREAM_UNLOCK (pad);
3799     return GST_FLOW_NOT_SUPPORTED;
3800   }
3801 }
3802
3803 /**
3804  * gst_pad_chain:
3805  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3806  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3807  *     if not.
3808  *
3809  * Chain a buffer to @pad.
3810  *
3811  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3812  *
3813  * If the caps on @buffer are different from the current caps on @pad, this
3814  * function will call any setcaps function (see gst_pad_set_setcaps_function())
3815  * installed on @pad. If the new caps are not acceptable for @pad, this
3816  * function returns #GST_FLOW_NOT_NEGOTIATED.
3817  *
3818  * The function proceeds calling the chain function installed on @pad (see
3819  * gst_pad_set_chain_function()) and the return value of that function is
3820  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3821  * chain function.
3822  *
3823  * In all cases, success or failure, the caller loses its reference to @buffer
3824  * after calling this function.
3825  *
3826  * Returns: a #GstFlowReturn from the pad.
3827  *
3828  * MT safe.
3829  */
3830 GstFlowReturn
3831 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3832 {
3833   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3834   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3835   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3836
3837   return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER, buffer);
3838 }
3839
3840 static GstFlowReturn
3841 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3842 {
3843   guint i, len;
3844   GstBuffer *buffer;
3845   GstFlowReturn ret;
3846
3847   GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3848
3849   len = gst_buffer_list_len (list);
3850
3851   ret = GST_FLOW_OK;
3852   for (i = 0; i < len; i++) {
3853     buffer = gst_buffer_list_get (list, i);
3854     ret =
3855         gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER,
3856         gst_buffer_ref (buffer));
3857     if (ret != GST_FLOW_OK)
3858       break;
3859   }
3860   gst_buffer_list_unref (list);
3861
3862   return ret;
3863 }
3864
3865 /**
3866  * gst_pad_chain_list:
3867  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3868  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3869  *     if not.
3870  *
3871  * Chain a bufferlist to @pad.
3872  *
3873  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3874  *
3875  * If the caps on the first buffer of @list are different from the current
3876  * caps on @pad, this function will call any setcaps function
3877  * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3878  * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3879  *
3880  * The function proceeds calling the chainlist function installed on @pad (see
3881  * gst_pad_set_chain_list_function()) and the return value of that function is
3882  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3883  * chainlist function.
3884  *
3885  * In all cases, success or failure, the caller loses its reference to @list
3886  * after calling this function.
3887  *
3888  * MT safe.
3889  *
3890  * Returns: a #GstFlowReturn from the pad.
3891  *
3892  * Since: 0.10.24
3893  */
3894 GstFlowReturn
3895 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3896 {
3897   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3898   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3899   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3900
3901   return gst_pad_chain_data_unchecked (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
3902 }
3903
3904 static GstFlowReturn
3905 gst_pad_push_data (GstPad * pad, GstProbeType type, void *data)
3906 {
3907   GstPad *peer;
3908   GstFlowReturn ret;
3909
3910   GST_OBJECT_LOCK (pad);
3911   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3912     goto flushing;
3913
3914   type |= GST_PROBE_TYPE_PUSH;
3915
3916   /* do block probes */
3917   PROBE (pad, type | GST_PROBE_TYPE_BLOCK, data, probe_stopped);
3918
3919   /* do post-blocking probes */
3920   PROBE (pad, type, data, probe_stopped);
3921
3922   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3923     goto not_linked;
3924
3925   /* take ref to peer pad before releasing the lock */
3926   gst_object_ref (peer);
3927   pad->priv->using++;
3928   GST_OBJECT_UNLOCK (pad);
3929
3930   ret = gst_pad_chain_data_unchecked (peer, type, data);
3931
3932   gst_object_unref (peer);
3933
3934   GST_OBJECT_LOCK (pad);
3935   pad->priv->using--;
3936   if (pad->priv->using == 0) {
3937     /* pad is not active anymore, trigger idle callbacks */
3938     PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
3939   }
3940   GST_OBJECT_UNLOCK (pad);
3941
3942   return ret;
3943
3944   /* ERROR recovery here */
3945   /* ERRORS */
3946 flushing:
3947   {
3948     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3949         "pushing, but pad was flushing");
3950     GST_OBJECT_UNLOCK (pad);
3951     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3952     return GST_FLOW_WRONG_STATE;
3953   }
3954 probe_stopped:
3955   {
3956     GST_OBJECT_UNLOCK (pad);
3957     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3958
3959     switch (ret) {
3960       case GST_FLOW_CUSTOM_SUCCESS:
3961         GST_DEBUG_OBJECT (pad, "dropped buffer");
3962         ret = GST_FLOW_OK;
3963         break;
3964       default:
3965         GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3966         break;
3967     }
3968     return ret;
3969   }
3970 not_linked:
3971   {
3972     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3973         "pushing, but it was not linked");
3974     GST_OBJECT_UNLOCK (pad);
3975     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3976     return GST_FLOW_NOT_LINKED;
3977   }
3978 }
3979
3980 /**
3981  * gst_pad_push:
3982  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3983  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3984  *     if not.
3985  *
3986  * Pushes a buffer to the peer of @pad.
3987  *
3988  * This function will call an installed pad block before triggering any
3989  * installed pad probes.
3990  *
3991  * If the caps on @buffer are different from the currently configured caps on
3992  * @pad, this function will call any installed setcaps function on @pad (see
3993  * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
3994  * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
3995  *
3996  * The function proceeds calling gst_pad_chain() on the peer pad and returns
3997  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3998  * be returned.
3999  *
4000  * In all cases, success or failure, the caller loses its reference to @buffer
4001  * after calling this function.
4002  *
4003  * Returns: a #GstFlowReturn from the peer pad.
4004  *
4005  * MT safe.
4006  */
4007 GstFlowReturn
4008 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4009 {
4010   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4011   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4012   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4013
4014   return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER, buffer);
4015 }
4016
4017 /**
4018  * gst_pad_push_list:
4019  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4020  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4021  *     if not.
4022  *
4023  * Pushes a buffer list to the peer of @pad.
4024  *
4025  * This function will call an installed pad block before triggering any
4026  * installed pad probes.
4027  *
4028  * If the caps on the first buffer in the first group of @list are different
4029  * from the currently configured caps on @pad, this function will call any
4030  * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4031  * case of failure to renegotiate the new format, this function returns
4032  * #GST_FLOW_NOT_NEGOTIATED.
4033  *
4034  * If there are any probes installed on @pad every group of the buffer list
4035  * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4036  * buffer list will be unreffed.
4037  *
4038  * The function proceeds calling the chain function on the peer pad and returns
4039  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4040  * be returned. If the peer pad does not have any installed chainlist function
4041  * every group buffer of the list will be merged into a normal #GstBuffer and
4042  * chained via gst_pad_chain().
4043  *
4044  * In all cases, success or failure, the caller loses its reference to @list
4045  * after calling this function.
4046  *
4047  * Returns: a #GstFlowReturn from the peer pad.
4048  *
4049  * MT safe.
4050  *
4051  * Since: 0.10.24
4052  */
4053 GstFlowReturn
4054 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4055 {
4056   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4057   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4058   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4059
4060   return gst_pad_push_data (pad, GST_PROBE_TYPE_BUFFER_LIST, list);
4061 }
4062
4063 static GstFlowReturn
4064 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4065     GstBuffer ** buffer)
4066 {
4067   GstFlowReturn ret;
4068   GstPadGetRangeFunction getrangefunc;
4069
4070   GST_PAD_STREAM_LOCK (pad);
4071
4072   GST_OBJECT_LOCK (pad);
4073   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4074     goto flushing;
4075   GST_OBJECT_UNLOCK (pad);
4076
4077   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4078     goto no_function;
4079
4080   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4081       "calling getrangefunc %s, offset %"
4082       G_GUINT64_FORMAT ", size %u",
4083       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4084
4085   ret = getrangefunc (pad, offset, size, buffer);
4086
4087   if (G_UNLIKELY (ret != GST_FLOW_OK))
4088     goto get_range_failed;
4089
4090   /* can only fire the signal if we have a valid buffer */
4091   GST_OBJECT_LOCK (pad);
4092   PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, *buffer,
4093       probe_stopped);
4094   GST_OBJECT_UNLOCK (pad);
4095
4096   GST_PAD_STREAM_UNLOCK (pad);
4097
4098   return ret;
4099
4100   /* ERRORS */
4101 flushing:
4102   {
4103     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4104         "pulling range, but pad was flushing");
4105     GST_OBJECT_UNLOCK (pad);
4106     GST_PAD_STREAM_UNLOCK (pad);
4107     return GST_FLOW_WRONG_STATE;
4108   }
4109 no_function:
4110   {
4111     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4112         ("pullrange on pad %s:%s but it has no getrangefunction",
4113             GST_DEBUG_PAD_NAME (pad)));
4114     GST_PAD_STREAM_UNLOCK (pad);
4115     return GST_FLOW_NOT_SUPPORTED;
4116   }
4117 probe_stopped:
4118   {
4119     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4120         "probe returned %s", gst_flow_get_name (ret));
4121     GST_OBJECT_UNLOCK (pad);
4122     GST_PAD_STREAM_UNLOCK (pad);
4123     gst_buffer_unref (*buffer);
4124     *buffer = NULL;
4125     return ret;
4126   }
4127 get_range_failed:
4128   {
4129     GST_PAD_STREAM_UNLOCK (pad);
4130     *buffer = NULL;
4131     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4132         (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4133         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4134     return ret;
4135   }
4136 }
4137
4138 /**
4139  * gst_pad_get_range:
4140  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4141  * @offset: The start offset of the buffer
4142  * @size: The length of the buffer
4143  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4144  *     returns #GST_FLOW_ERROR if %NULL.
4145  *
4146  * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4147  * immediatly and @buffer is %NULL.
4148  *
4149  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4150  * description of a getrange function. If @pad has no getrange function
4151  * installed (see gst_pad_set_getrange_function()) this function returns
4152  * #GST_FLOW_NOT_SUPPORTED.
4153  *
4154  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4155  *
4156  * Returns: a #GstFlowReturn from the pad.
4157  *
4158  * MT safe.
4159  */
4160 GstFlowReturn
4161 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4162     GstBuffer ** buffer)
4163 {
4164   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4165   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4166   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4167
4168   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4169 }
4170
4171 /**
4172  * gst_pad_pull_range:
4173  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4174  * @offset: The start offset of the buffer
4175  * @size: The length of the buffer
4176  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4177  *     GST_FLOW_ERROR if %NULL.
4178  *
4179  * Pulls a @buffer from the peer pad.
4180  *
4181  * This function will first trigger the pad block signal if it was
4182  * installed.
4183  *
4184  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4185  * function returns the result of gst_pad_get_range() on the peer pad.
4186  * See gst_pad_get_range() for a list of return values and for the
4187  * semantics of the arguments of this function.
4188  *
4189  * @buffer's caps must either be unset or the same as what is already
4190  * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4191  * supported.
4192  *
4193  * Returns: a #GstFlowReturn from the peer pad.
4194  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4195  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4196  * @buffer may not be used or freed when any other return value than
4197  * #GST_FLOW_OK is returned.
4198  *
4199  * MT safe.
4200  */
4201 GstFlowReturn
4202 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4203     GstBuffer ** buffer)
4204 {
4205   GstPad *peer;
4206   GstFlowReturn ret;
4207   gboolean needs_events;
4208
4209   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4210   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4211   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4212
4213   GST_OBJECT_LOCK (pad);
4214
4215   PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BLOCK, NULL,
4216       pre_probe_stopped);
4217
4218   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4219     goto not_linked;
4220
4221   gst_object_ref (peer);
4222   pad->priv->using++;
4223   GST_OBJECT_UNLOCK (pad);
4224
4225   ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4226
4227   gst_object_unref (peer);
4228
4229   GST_OBJECT_LOCK (pad);
4230   pad->priv->using--;
4231   if (pad->priv->using == 0) {
4232     /* pad is not active anymore, trigger idle callbacks */
4233     PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_IDLE, NULL,
4234         post_probe_stopped);
4235   }
4236
4237   if (G_UNLIKELY (ret != GST_FLOW_OK))
4238     goto pull_range_failed;
4239
4240   PROBE (pad, GST_PROBE_TYPE_PULL | GST_PROBE_TYPE_BUFFER, buffer,
4241       post_probe_stopped);
4242
4243   needs_events = GST_PAD_NEEDS_EVENTS (pad);
4244   if (G_UNLIKELY (needs_events)) {
4245     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4246
4247     GST_DEBUG_OBJECT (pad, "we need to update the events");
4248     ret = gst_pad_update_events (pad);
4249     if (G_UNLIKELY (ret != GST_FLOW_OK))
4250       goto events_error;
4251   }
4252   GST_OBJECT_UNLOCK (pad);
4253
4254   return ret;
4255
4256   /* ERROR recovery here */
4257 pre_probe_stopped:
4258   {
4259     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4260         gst_flow_get_name (ret));
4261     GST_OBJECT_UNLOCK (pad);
4262     return ret;
4263   }
4264 not_linked:
4265   {
4266     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4267         "pulling range, but it was not linked");
4268     GST_OBJECT_UNLOCK (pad);
4269     return GST_FLOW_NOT_LINKED;
4270   }
4271 pull_range_failed:
4272   {
4273     *buffer = NULL;
4274     GST_OBJECT_UNLOCK (pad);
4275     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4276         (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4277         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4278     return ret;
4279   }
4280 post_probe_stopped:
4281   {
4282     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4283         "post probe returned %s", gst_flow_get_name (ret));
4284     GST_OBJECT_UNLOCK (pad);
4285     if (ret == GST_FLOW_OK)
4286       gst_buffer_unref (*buffer);
4287     *buffer = NULL;
4288     return ret;
4289   }
4290 events_error:
4291   {
4292     GST_OBJECT_UNLOCK (pad);
4293     gst_buffer_unref (*buffer);
4294     *buffer = NULL;
4295     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4296         "pullrange returned events that were not accepted");
4297     return ret;
4298   }
4299 }
4300
4301 /**
4302  * gst_pad_push_event:
4303  * @pad: a #GstPad to push the event to.
4304  * @event: (transfer full): the #GstEvent to send to the pad.
4305  *
4306  * Sends the event to the peer of the given pad. This function is
4307  * mainly used by elements to send events to their peer
4308  * elements.
4309  *
4310  * This function takes owership of the provided event so you should
4311  * gst_event_ref() it if you want to reuse the event after this call.
4312  *
4313  * Returns: TRUE if the event was handled.
4314  *
4315  * MT safe.
4316  */
4317 gboolean
4318 gst_pad_push_event (GstPad * pad, GstEvent * event)
4319 {
4320   GstFlowReturn ret;
4321   GstPad *peerpad;
4322   gboolean result, do_event_actions = TRUE;
4323   gboolean stored = FALSE;
4324
4325   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4326   g_return_val_if_fail (event != NULL, FALSE);
4327   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4328
4329   GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4330
4331 again:
4332   GST_OBJECT_LOCK (pad);
4333
4334   peerpad = GST_PAD_PEER (pad);
4335
4336   /* Two checks to be made:
4337    * . (un)set the FLUSHING flag for flushing events,
4338    * . handle pad blocking */
4339   switch (GST_EVENT_TYPE (event)) {
4340     case GST_EVENT_FLUSH_START:
4341       GST_PAD_SET_FLUSHING (pad);
4342
4343       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4344         /* flush start will have set the FLUSHING flag and will then
4345          * unlock all threads doing a GCond wait on the blocking pad. This
4346          * will typically unblock the STREAMING thread blocked on a pad. */
4347         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4348             "doing block signal.");
4349         GST_PAD_BLOCK_BROADCAST (pad);
4350         goto flushed;
4351       }
4352       break;
4353     case GST_EVENT_FLUSH_STOP:
4354       GST_PAD_UNSET_FLUSHING (pad);
4355       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4356         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4357         goto flushed;
4358       }
4359       break;
4360     default:
4361     {
4362       /* store the event on the pad, but only on srcpads */
4363       if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4364         guint idx;
4365
4366         idx = GST_EVENT_STICKY_IDX (event);
4367         GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4368             GST_EVENT_TYPE_NAME (event), idx);
4369
4370         /* srcpad sticky events always become active immediately */
4371         gst_event_replace (&pad->priv->events[idx].event, event);
4372
4373         stored = TRUE;
4374       }
4375
4376       /* backwards compatibility mode for caps */
4377       if (do_event_actions) {
4378         do_event_actions = FALSE;
4379
4380         switch (GST_EVENT_TYPE (event)) {
4381           case GST_EVENT_CAPS:
4382           {
4383             GstCaps *caps;
4384
4385             GST_OBJECT_UNLOCK (pad);
4386
4387             gst_event_parse_caps (event, &caps);
4388             /* FIXME, this is awkward because we don't check flushing here which means
4389              * that we can call the setcaps functions on flushing pads, this is not
4390              * quite what we want, otoh, this code should just go away and elements
4391              * that set caps on their srcpad should just setup stuff themselves. */
4392             gst_pad_call_setcaps (pad, caps);
4393
4394             /* recheck everything, we released the lock */
4395             goto again;
4396           }
4397           case GST_EVENT_SEGMENT:
4398           {
4399             gint64 offset;
4400
4401             offset = pad->offset;
4402             /* check if we need to adjust the segment */
4403             if (offset != 0 && (peerpad != NULL)) {
4404               GstSegment segment;
4405
4406               /* copy segment values */
4407               gst_event_copy_segment (event, &segment);
4408               gst_event_unref (event);
4409
4410               /* adjust and make a new event with the offset applied */
4411               segment.base += offset;
4412               event = gst_event_new_segment (&segment);
4413             }
4414             break;
4415           }
4416           case GST_EVENT_RECONFIGURE:
4417             if (GST_PAD_IS_SINK (pad))
4418               GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4419             break;
4420           default:
4421             break;
4422         }
4423       }
4424       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4425         goto flushed;
4426
4427       PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT
4428           | GST_PROBE_TYPE_BLOCK, event, probe_stopped);
4429
4430       break;
4431     }
4432   }
4433
4434   /* send probes after modifying the events above */
4435   PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event, probe_stopped);
4436
4437   /* now check the peer pad */
4438   if (peerpad == NULL)
4439     goto not_linked;
4440
4441   gst_object_ref (peerpad);
4442   pad->priv->using++;
4443   GST_OBJECT_UNLOCK (pad);
4444
4445   GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4446       GST_EVENT_TYPE_NAME (event), peerpad);
4447
4448   result = gst_pad_send_event (peerpad, event);
4449
4450   /* Note: we gave away ownership of the event at this point */
4451   GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4452       peerpad, result);
4453
4454   gst_object_unref (peerpad);
4455
4456   GST_OBJECT_LOCK (pad);
4457   pad->priv->using--;
4458   if (pad->priv->using == 0) {
4459     /* pad is not active anymore, trigger idle callbacks */
4460     PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_IDLE, NULL, probe_stopped);
4461   }
4462   GST_OBJECT_UNLOCK (pad);
4463
4464   return result | stored;
4465
4466   /* ERROR handling */
4467 flushed:
4468   {
4469     GST_DEBUG_OBJECT (pad, "We're flushing");
4470     GST_OBJECT_UNLOCK (pad);
4471     gst_event_unref (event);
4472     return stored;
4473   }
4474 probe_stopped:
4475   {
4476     GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4477     GST_OBJECT_UNLOCK (pad);
4478     gst_event_unref (event);
4479     return stored;
4480   }
4481 not_linked:
4482   {
4483     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4484     GST_OBJECT_UNLOCK (pad);
4485     gst_event_unref (event);
4486     return stored;
4487   }
4488 }
4489
4490 /**
4491  * gst_pad_send_event:
4492  * @pad: a #GstPad to send the event to.
4493  * @event: (transfer full): the #GstEvent to send to the pad.
4494  *
4495  * Sends the event to the pad. This function can be used
4496  * by applications to send events in the pipeline.
4497  *
4498  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4499  * sink pad, @event should be a downstream event. For example, you would not
4500  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4501  * Furthermore, some downstream events have to be serialized with data flow,
4502  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4503  * the event needs to be serialized with data flow, this function will take the
4504  * pad's stream lock while calling its event function.
4505  *
4506  * To find out whether an event type is upstream, downstream, or downstream and
4507  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4508  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4509  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4510  * plugin doesn't need to bother itself with this information; the core handles
4511  * all necessary locks and checks.
4512  *
4513  * This function takes owership of the provided event so you should
4514  * gst_event_ref() it if you want to reuse the event after this call.
4515  *
4516  * Returns: TRUE if the event was handled.
4517  */
4518 gboolean
4519 gst_pad_send_event (GstPad * pad, GstEvent * event)
4520 {
4521   GstFlowReturn ret;
4522   gboolean result = FALSE;
4523   gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4524
4525   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4526   g_return_val_if_fail (event != NULL, FALSE);
4527
4528   GST_OBJECT_LOCK (pad);
4529   if (GST_PAD_IS_SINK (pad)) {
4530     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4531       goto wrong_direction;
4532     serialized = GST_EVENT_IS_SERIALIZED (event);
4533     sticky = GST_EVENT_IS_STICKY (event);
4534   } else if (GST_PAD_IS_SRC (pad)) {
4535     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4536       goto wrong_direction;
4537     /* events on srcpad never are serialized and sticky */
4538     serialized = sticky = FALSE;
4539   } else
4540     goto unknown_direction;
4541
4542   /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4543    * event. */
4544   needs_events = GST_PAD_NEEDS_EVENTS (pad);
4545
4546   switch (GST_EVENT_TYPE (event)) {
4547     case GST_EVENT_FLUSH_START:
4548       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4549           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4550
4551       /* can't even accept a flush begin event when flushing */
4552       if (GST_PAD_IS_FLUSHING (pad))
4553         goto flushing;
4554
4555       GST_PAD_SET_FLUSHING (pad);
4556       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4557       needs_events = FALSE;
4558       break;
4559     case GST_EVENT_FLUSH_STOP:
4560       if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4561         GST_PAD_UNSET_FLUSHING (pad);
4562         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4563       }
4564       GST_OBJECT_UNLOCK (pad);
4565       /* grab stream lock */
4566       GST_PAD_STREAM_LOCK (pad);
4567       need_unlock = TRUE;
4568       GST_OBJECT_LOCK (pad);
4569       needs_events = FALSE;
4570       break;
4571     case GST_EVENT_RECONFIGURE:
4572       if (GST_PAD_IS_SRC (pad))
4573         GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4574     default:
4575       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4576           GST_EVENT_TYPE_NAME (event));
4577
4578       if (serialized) {
4579         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4580         GST_OBJECT_UNLOCK (pad);
4581         GST_PAD_STREAM_LOCK (pad);
4582         need_unlock = TRUE;
4583         GST_OBJECT_LOCK (pad);
4584       } else {
4585         /* don't forward events on non-serialized events */
4586         needs_events = FALSE;
4587       }
4588
4589       /* store the event on the pad, but only on srcpads. We need to store the
4590        * event before checking the flushing flag. */
4591       if (sticky) {
4592         guint idx;
4593         PadEvent *ev;
4594
4595         switch (GST_EVENT_TYPE (event)) {
4596           case GST_EVENT_SEGMENT:
4597             if (pad->offset != 0) {
4598               GstSegment segment;
4599
4600               /* copy segment values */
4601               gst_event_copy_segment (event, &segment);
4602               gst_event_unref (event);
4603
4604               /* adjust and make a new event with the offset applied */
4605               segment.base += pad->offset;
4606               event = gst_event_new_segment (&segment);
4607             }
4608             break;
4609           default:
4610             break;
4611         }
4612
4613         idx = GST_EVENT_STICKY_IDX (event);
4614         ev = &pad->priv->events[idx];
4615
4616         GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4617             GST_EVENT_TYPE_NAME (event), idx);
4618
4619         if (ev->event != event) {
4620           gst_event_replace (&ev->pending, event);
4621           /* set the flag so that we update the events next time. We would
4622            * usually update below but we might be flushing too. */
4623           GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4624           needs_events = TRUE;
4625         }
4626       }
4627       /* now do the probe */
4628       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4629         goto flushing;
4630
4631       PROBE (pad, GST_PROBE_TYPE_PUSH | GST_PROBE_TYPE_EVENT, event,
4632           probe_stopped);
4633
4634       break;
4635   }
4636
4637   if (G_UNLIKELY (needs_events)) {
4638     GstFlowReturn ret;
4639
4640     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4641
4642     GST_DEBUG_OBJECT (pad, "need to update all events");
4643     ret = gst_pad_update_events (pad);
4644     if (ret != GST_FLOW_OK)
4645       goto update_failed;
4646     GST_OBJECT_UNLOCK (pad);
4647
4648     gst_event_unref (event);
4649
4650     result = TRUE;
4651   } else {
4652     GstPadEventFunction eventfunc;
4653
4654     if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4655       goto no_function;
4656
4657     GST_OBJECT_UNLOCK (pad);
4658
4659     result = do_event_function (pad, event, eventfunc);
4660   }
4661
4662   if (need_unlock)
4663     GST_PAD_STREAM_UNLOCK (pad);
4664
4665   GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4666
4667   return result;
4668
4669   /* ERROR handling */
4670 wrong_direction:
4671   {
4672     g_warning ("pad %s:%s sending %s event in wrong direction",
4673         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4674     GST_OBJECT_UNLOCK (pad);
4675     gst_event_unref (event);
4676     return FALSE;
4677   }
4678 unknown_direction:
4679   {
4680     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4681     GST_OBJECT_UNLOCK (pad);
4682     gst_event_unref (event);
4683     return FALSE;
4684   }
4685 no_function:
4686   {
4687     g_warning ("pad %s:%s has no event handler, file a bug.",
4688         GST_DEBUG_PAD_NAME (pad));
4689     GST_OBJECT_UNLOCK (pad);
4690     if (need_unlock)
4691       GST_PAD_STREAM_UNLOCK (pad);
4692     gst_event_unref (event);
4693     return FALSE;
4694   }
4695 flushing:
4696   {
4697     GST_OBJECT_UNLOCK (pad);
4698     if (need_unlock)
4699       GST_PAD_STREAM_UNLOCK (pad);
4700     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4701         "Received event on flushing pad. Discarding");
4702     gst_event_unref (event);
4703     return FALSE;
4704   }
4705 probe_stopped:
4706   {
4707     GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4708     GST_OBJECT_UNLOCK (pad);
4709     if (need_unlock)
4710       GST_PAD_STREAM_UNLOCK (pad);
4711     gst_event_unref (event);
4712     return FALSE;
4713   }
4714 update_failed:
4715   {
4716     GST_OBJECT_UNLOCK (pad);
4717     if (need_unlock)
4718       GST_PAD_STREAM_UNLOCK (pad);
4719     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4720     gst_event_unref (event);
4721     return FALSE;
4722   }
4723 }
4724
4725 /**
4726  * gst_pad_set_element_private:
4727  * @pad: the #GstPad to set the private data of.
4728  * @priv: The private data to attach to the pad.
4729  *
4730  * Set the given private data gpointer on the pad.
4731  * This function can only be used by the element that owns the pad.
4732  * No locking is performed in this function.
4733  */
4734 void
4735 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4736 {
4737   pad->element_private = priv;
4738 }
4739
4740 /**
4741  * gst_pad_get_element_private:
4742  * @pad: the #GstPad to get the private data of.
4743  *
4744  * Gets the private data of a pad.
4745  * No locking is performed in this function.
4746  *
4747  * Returns: (transfer none): a #gpointer to the private data.
4748  */
4749 gpointer
4750 gst_pad_get_element_private (GstPad * pad)
4751 {
4752   return pad->element_private;
4753 }
4754
4755 /**
4756  * gst_pad_get_sticky_event:
4757  * @pad: the #GstPad to get the event from.
4758  * @event_type: the #GstEventType that should be retrieved.
4759  *
4760  * Returns a new reference of the sticky event of type @event_type
4761  * from the event.
4762  *
4763  * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4764  */
4765 GstEvent *
4766 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4767 {
4768   GstEvent *event = NULL;
4769   guint idx;
4770
4771   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4772   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4773
4774   idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4775
4776   GST_OBJECT_LOCK (pad);
4777   if ((event = pad->priv->events[idx].event)) {
4778     gst_event_ref (event);
4779   }
4780   GST_OBJECT_UNLOCK (pad);
4781
4782   return event;
4783 }
4784
4785 /**
4786  * gst_pad_sticky_events_foreach:
4787  * @pad: the #GstPad that should be used for iteration.
4788  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4789  * @user_data: (closure): the optional user data.
4790  *
4791  * Iterates all active sticky events on @pad and calls @foreach_func for every
4792  * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4793  * is immediately stopped.
4794  *
4795  * Returns: GST_FLOW_OK if iteration was successful
4796  */
4797 GstFlowReturn
4798 gst_pad_sticky_events_foreach (GstPad * pad,
4799     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4800 {
4801   GstFlowReturn ret = GST_FLOW_OK;
4802   guint i;
4803   GstEvent *event;
4804
4805   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4806   g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4807
4808   GST_OBJECT_LOCK (pad);
4809
4810 restart:
4811   for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4812     gboolean res;
4813     PadEvent *ev;
4814
4815     ev = &pad->priv->events[i];
4816
4817     /* skip without active event */
4818     if ((event = ev->event) == NULL)
4819       continue;
4820
4821     gst_event_ref (event);
4822     GST_OBJECT_UNLOCK (pad);
4823
4824     res = foreach_func (pad, event, user_data);
4825
4826     GST_OBJECT_LOCK (pad);
4827     gst_event_unref (event);
4828
4829     if (res != GST_FLOW_OK) {
4830       ret = res;
4831       break;
4832     }
4833
4834     /* things could have changed while we release the lock, check if we still
4835      * are handling the same event, if we don't something changed and we have
4836      * to try again. FIXME. we need a cookie here. */
4837     if (event != ev->event) {
4838       GST_DEBUG_OBJECT (pad, "events changed, restarting");
4839       goto restart;
4840     }
4841   }
4842   GST_OBJECT_UNLOCK (pad);
4843
4844   return ret;
4845 }
4846
4847 static void
4848 do_stream_status (GstPad * pad, GstStreamStatusType type,
4849     GThread * thread, GstTask * task)
4850 {
4851   GstElement *parent;
4852
4853   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4854
4855   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4856     if (GST_IS_ELEMENT (parent)) {
4857       GstMessage *message;
4858       GValue value = { 0 };
4859
4860       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4861         gchar *tname, *ename, *pname;
4862
4863         /* create a good task name */
4864         ename = gst_element_get_name (parent);
4865         pname = gst_pad_get_name (pad);
4866         tname = g_strdup_printf ("%s:%s", ename, pname);
4867         g_free (ename);
4868         g_free (pname);
4869
4870         gst_object_set_name (GST_OBJECT_CAST (task), tname);
4871         g_free (tname);
4872       }
4873
4874       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4875           type, parent);
4876
4877       g_value_init (&value, GST_TYPE_TASK);
4878       g_value_set_object (&value, task);
4879       gst_message_set_stream_status_object (message, &value);
4880       g_value_unset (&value);
4881
4882       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4883       gst_element_post_message (parent, message);
4884     }
4885     gst_object_unref (parent);
4886   }
4887 }
4888
4889 static void
4890 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4891 {
4892   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4893       thread, task);
4894 }
4895
4896 static void
4897 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4898 {
4899   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4900       thread, task);
4901 }
4902
4903 static GstTaskThreadCallbacks thr_callbacks = {
4904   pad_enter_thread,
4905   pad_leave_thread,
4906 };
4907
4908 /**
4909  * gst_pad_start_task:
4910  * @pad: the #GstPad to start the task of
4911  * @func: the task function to call
4912  * @data: data passed to the task function
4913  *
4914  * Starts a task that repeatedly calls @func with @data. This function
4915  * is mostly used in pad activation functions to start the dataflow.
4916  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4917  * before @func is called.
4918  *
4919  * Returns: a %TRUE if the task could be started.
4920  */
4921 gboolean
4922 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4923 {
4924   GstTask *task;
4925   gboolean res;
4926
4927   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4928   g_return_val_if_fail (func != NULL, FALSE);
4929
4930   GST_DEBUG_OBJECT (pad, "start task");
4931
4932   GST_OBJECT_LOCK (pad);
4933   task = GST_PAD_TASK (pad);
4934   if (task == NULL) {
4935     task = gst_task_create (func, data);
4936     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4937     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4938     GST_DEBUG_OBJECT (pad, "created task");
4939     GST_PAD_TASK (pad) = task;
4940     gst_object_ref (task);
4941     /* release lock to post the message */
4942     GST_OBJECT_UNLOCK (pad);
4943
4944     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4945
4946     gst_object_unref (task);
4947
4948     GST_OBJECT_LOCK (pad);
4949     /* nobody else is supposed to have changed the pad now */
4950     if (GST_PAD_TASK (pad) != task)
4951       goto concurrent_stop;
4952   }
4953   res = gst_task_set_state (task, GST_TASK_STARTED);
4954   GST_OBJECT_UNLOCK (pad);
4955
4956   return res;
4957
4958   /* ERRORS */
4959 concurrent_stop:
4960   {
4961     GST_OBJECT_UNLOCK (pad);
4962     return TRUE;
4963   }
4964 }
4965
4966 /**
4967  * gst_pad_pause_task:
4968  * @pad: the #GstPad to pause the task of
4969  *
4970  * Pause the task of @pad. This function will also wait until the
4971  * function executed by the task is finished if this function is not
4972  * called from the task function.
4973  *
4974  * Returns: a TRUE if the task could be paused or FALSE when the pad
4975  * has no task.
4976  */
4977 gboolean
4978 gst_pad_pause_task (GstPad * pad)
4979 {
4980   GstTask *task;
4981   gboolean res;
4982
4983   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4984
4985   GST_DEBUG_OBJECT (pad, "pause task");
4986
4987   GST_OBJECT_LOCK (pad);
4988   task = GST_PAD_TASK (pad);
4989   if (task == NULL)
4990     goto no_task;
4991   res = gst_task_set_state (task, GST_TASK_PAUSED);
4992   GST_OBJECT_UNLOCK (pad);
4993
4994   /* wait for task function to finish, this lock is recursive so it does nothing
4995    * when the pause is called from the task itself */
4996   GST_PAD_STREAM_LOCK (pad);
4997   GST_PAD_STREAM_UNLOCK (pad);
4998
4999   return res;
5000
5001 no_task:
5002   {
5003     GST_DEBUG_OBJECT (pad, "pad has no task");
5004     GST_OBJECT_UNLOCK (pad);
5005     return FALSE;
5006   }
5007 }
5008
5009 /**
5010  * gst_pad_stop_task:
5011  * @pad: the #GstPad to stop the task of
5012  *
5013  * Stop the task of @pad. This function will also make sure that the
5014  * function executed by the task will effectively stop if not called
5015  * from the GstTaskFunction.
5016  *
5017  * This function will deadlock if called from the GstTaskFunction of
5018  * the task. Use gst_task_pause() instead.
5019  *
5020  * Regardless of whether the pad has a task, the stream lock is acquired and
5021  * released so as to ensure that streaming through this pad has finished.
5022  *
5023  * Returns: a TRUE if the task could be stopped or FALSE on error.
5024  */
5025 gboolean
5026 gst_pad_stop_task (GstPad * pad)
5027 {
5028   GstTask *task;
5029   gboolean res;
5030
5031   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5032
5033   GST_DEBUG_OBJECT (pad, "stop task");
5034
5035   GST_OBJECT_LOCK (pad);
5036   task = GST_PAD_TASK (pad);
5037   if (task == NULL)
5038     goto no_task;
5039   GST_PAD_TASK (pad) = NULL;
5040   res = gst_task_set_state (task, GST_TASK_STOPPED);
5041   GST_OBJECT_UNLOCK (pad);
5042
5043   GST_PAD_STREAM_LOCK (pad);
5044   GST_PAD_STREAM_UNLOCK (pad);
5045
5046   if (!gst_task_join (task))
5047     goto join_failed;
5048
5049   gst_object_unref (task);
5050
5051   return res;
5052
5053 no_task:
5054   {
5055     GST_DEBUG_OBJECT (pad, "no task");
5056     GST_OBJECT_UNLOCK (pad);
5057
5058     GST_PAD_STREAM_LOCK (pad);
5059     GST_PAD_STREAM_UNLOCK (pad);
5060
5061     /* this is not an error */
5062     return TRUE;
5063   }
5064 join_failed:
5065   {
5066     /* this is bad, possibly the application tried to join the task from
5067      * the task's thread. We install the task again so that it will be stopped
5068      * again from the right thread next time hopefully. */
5069     GST_OBJECT_LOCK (pad);
5070     GST_DEBUG_OBJECT (pad, "join failed");
5071     /* we can only install this task if there was no other task */
5072     if (GST_PAD_TASK (pad) == NULL)
5073       GST_PAD_TASK (pad) = task;
5074     GST_OBJECT_UNLOCK (pad);
5075
5076     return FALSE;
5077   }
5078 }