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