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