pad: rework pad probes
[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   guint probe_list_cookie;
114   guint probe_cookie;
115 };
116
117 typedef struct
118 {
119   GHook hook;
120   guint cookie;
121 } GstProbe;
122
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
124
125 typedef struct
126 {
127   GstPad *pad;
128   GstPadProbeType mask;
129   gpointer type_data;
130   gboolean dropped;
131   gboolean pass;
132   gboolean marshalled;
133   guint cookie;
134 } ProbeMarshall;
135
136 static void gst_pad_dispose (GObject * object);
137 static void gst_pad_finalize (GObject * object);
138 static void gst_pad_set_property (GObject * object, guint prop_id,
139     const GValue * value, GParamSpec * pspec);
140 static void gst_pad_get_property (GObject * object, guint prop_id,
141     GValue * value, GParamSpec * pspec);
142
143 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
144 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
145 static gboolean gst_pad_activate_default (GstPad * pad);
146 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
147 static void gst_pad_fixate_caps_default (GstPad * pad, GstCaps * caps);
148 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
149     GstBufferList * list);
150
151 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
152
153 static GParamSpec *pspec_caps = NULL;
154
155 /* quarks for probe signals */
156 static GQuark buffer_quark;
157 static GQuark buffer_list_quark;
158 static GQuark event_quark;
159
160 typedef struct
161 {
162   const gint ret;
163   const gchar *name;
164   GQuark quark;
165 } GstFlowQuarks;
166
167 static GstFlowQuarks flow_quarks[] = {
168   {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
169   {GST_FLOW_RESEND, "resend", 0},
170   {GST_FLOW_OK, "ok", 0},
171   {GST_FLOW_NOT_LINKED, "not-linked", 0},
172   {GST_FLOW_WRONG_STATE, "wrong-state", 0},
173   {GST_FLOW_EOS, "eos", 0},
174   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
175   {GST_FLOW_ERROR, "error", 0},
176   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
177   {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
178 };
179
180 /**
181  * gst_flow_get_name:
182  * @ret: a #GstFlowReturn to get the name of.
183  *
184  * Gets a string representing the given flow return.
185  *
186  * Returns: a static string with the name of the flow return.
187  */
188 const gchar *
189 gst_flow_get_name (GstFlowReturn ret)
190 {
191   gint i;
192
193   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
194
195   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
196     if (ret == flow_quarks[i].ret)
197       return flow_quarks[i].name;
198   }
199   return "unknown";
200 }
201
202 /**
203  * gst_flow_to_quark:
204  * @ret: a #GstFlowReturn to get the quark of.
205  *
206  * Get the unique quark for the given GstFlowReturn.
207  *
208  * Returns: the quark associated with the flow return or 0 if an
209  * invalid return was specified.
210  */
211 GQuark
212 gst_flow_to_quark (GstFlowReturn ret)
213 {
214   gint i;
215
216   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
217
218   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
219     if (ret == flow_quarks[i].ret)
220       return flow_quarks[i].quark;
221   }
222   return 0;
223 }
224
225 #define _do_init \
226 { \
227   gint i; \
228   \
229   buffer_quark = g_quark_from_static_string ("buffer"); \
230   buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
231   event_quark = g_quark_from_static_string ("event"); \
232   \
233   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {                    \
234     flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
235   } \
236   \
237   GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
239 }
240
241 #define gst_pad_parent_class parent_class
242 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
243
244 static void
245 gst_pad_class_init (GstPadClass * klass)
246 {
247   GObjectClass *gobject_class;
248   GstObjectClass *gstobject_class;
249
250   gobject_class = G_OBJECT_CLASS (klass);
251   gstobject_class = GST_OBJECT_CLASS (klass);
252
253   g_type_class_add_private (klass, sizeof (GstPadPrivate));
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 static void
338 clear_event (PadEvent events[], guint idx)
339 {
340   gst_event_replace (&events[idx].event, NULL);
341   gst_event_replace (&events[idx].pending, NULL);
342 }
343
344 /* called when setting the pad inactive. It removes all sticky events from
345  * the pad */
346 static void
347 clear_events (PadEvent events[])
348 {
349   guint i;
350
351   for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
352     clear_event (events, i);
353 }
354
355 /* The sticky event with @idx from the srcpad is copied to the
356  * pending event on the sinkpad (when different).
357  * This function applies the pad offsets in case of segment events.
358  * This will make sure that we send the event to the sinkpad event
359  * function when the next buffer of event arrives.
360  * Should be called with the OBJECT lock of both pads.
361  * This function returns TRUE when there is a pending event on the
362  * sinkpad */
363 static gboolean
364 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
365 {
366   PadEvent *srcev, *sinkev;
367   GstEvent *event;
368   gboolean pending = FALSE;
369
370   srcev = &srcpad->priv->events[idx];
371
372   if ((event = srcev->event)) {
373     sinkev = &sinkpad->priv->events[idx];
374
375     switch (GST_EVENT_TYPE (event)) {
376       case GST_EVENT_SEGMENT:
377       {
378         GstSegment segment;
379         gint64 offset;
380
381         offset = srcpad->offset + sinkpad->offset;
382         if (offset != 0) {
383           gst_event_copy_segment (event, &segment);
384           /* adjust the base time. FIXME, check negative times, try to tweak the
385            * start to do clipping on negative times */
386           segment.base += offset;
387           /* make a new event from the updated segment */
388           event = gst_event_new_segment (&segment);
389         }
390         break;
391       }
392       default:
393         break;
394     }
395     if (sinkev->event != event) {
396       /* put in the pending entry when different */
397       GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, srcpad,
398           "Putting event %p (%s) on pad %s:%s", event,
399           GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (sinkpad));
400       gst_event_replace (&sinkev->pending, event);
401       pending = TRUE;
402     }
403   }
404   return pending;
405 }
406
407
408 static void
409 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
410 {
411   gboolean pending;
412   gint i;
413
414   /* make sure we push the events from the source to this new peer, for this we
415    * copy the events on the sinkpad and mark EVENTS_PENDING */
416   pending = FALSE;
417   for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
418     pending |= replace_event (srcpad, sinkpad, i);
419
420   /* we had some new pending events, set our flag */
421   if (pending)
422     GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
423 }
424
425 /* should be called with the OBJECT_LOCK */
426 static GstCaps *
427 get_pad_caps (GstPad * pad)
428 {
429   GstCaps *caps = NULL;
430   GstEvent *event;
431   guint idx;
432
433   idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
434   /* we can only use the caps when we have successfully send the caps
435    * event to the event function and is thus in the active entry */
436   if ((event = pad->priv->events[idx].event))
437     gst_event_parse_caps (event, &caps);
438
439   return caps;
440 }
441
442 static void
443 gst_pad_dispose (GObject * object)
444 {
445   GstPad *pad = GST_PAD_CAST (object);
446   GstPad *peer;
447
448   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
449
450   /* unlink the peer pad */
451   if ((peer = gst_pad_get_peer (pad))) {
452     /* window for MT unsafeness, someone else could unlink here
453      * and then we call unlink with wrong pads. The unlink
454      * function would catch this and safely return failed. */
455     if (GST_PAD_IS_SRC (pad))
456       gst_pad_unlink (pad, peer);
457     else
458       gst_pad_unlink (peer, pad);
459
460     gst_object_unref (peer);
461   }
462
463   gst_pad_set_pad_template (pad, NULL);
464
465   clear_events (pad->priv->events);
466
467   g_hook_list_clear (&pad->probes);
468
469   G_OBJECT_CLASS (parent_class)->dispose (object);
470 }
471
472 static void
473 gst_pad_finalize (GObject * object)
474 {
475   GstPad *pad = GST_PAD_CAST (object);
476   GstTask *task;
477
478   /* in case the task is still around, clean it up */
479   if ((task = GST_PAD_TASK (pad))) {
480     gst_task_join (task);
481     GST_PAD_TASK (pad) = NULL;
482     gst_object_unref (task);
483   }
484
485   g_static_rec_mutex_free (&pad->stream_rec_lock);
486   g_cond_free (pad->block_cond);
487
488   G_OBJECT_CLASS (parent_class)->finalize (object);
489 }
490
491 static void
492 gst_pad_set_property (GObject * object, guint prop_id,
493     const GValue * value, GParamSpec * pspec)
494 {
495   g_return_if_fail (GST_IS_PAD (object));
496
497   switch (prop_id) {
498     case PAD_PROP_DIRECTION:
499       GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
500       break;
501     case PAD_PROP_TEMPLATE:
502       gst_pad_set_pad_template (GST_PAD_CAST (object),
503           (GstPadTemplate *) g_value_get_object (value));
504       break;
505     default:
506       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507       break;
508   }
509 }
510
511 static void
512 gst_pad_get_property (GObject * object, guint prop_id,
513     GValue * value, GParamSpec * pspec)
514 {
515   g_return_if_fail (GST_IS_PAD (object));
516
517   switch (prop_id) {
518     case PAD_PROP_CAPS:
519       GST_OBJECT_LOCK (object);
520       g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
521       GST_OBJECT_UNLOCK (object);
522       break;
523     case PAD_PROP_DIRECTION:
524       g_value_set_enum (value, GST_PAD_DIRECTION (object));
525       break;
526     case PAD_PROP_TEMPLATE:
527       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
528       break;
529     default:
530       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531       break;
532   }
533 }
534
535 /**
536  * gst_pad_new:
537  * @name: the name of the new pad.
538  * @direction: the #GstPadDirection of the pad.
539  *
540  * Creates a new pad with the given name in the given direction.
541  * If name is NULL, a guaranteed unique name (across all pads)
542  * will be assigned.
543  * This function makes a copy of the name so you can safely free the name.
544  *
545  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
546  *
547  * MT safe.
548  */
549 GstPad *
550 gst_pad_new (const gchar * name, GstPadDirection direction)
551 {
552   return g_object_new (GST_TYPE_PAD,
553       "name", name, "direction", direction, NULL);
554 }
555
556 /**
557  * gst_pad_new_from_template:
558  * @templ: the pad template to use
559  * @name: the name of the element
560  *
561  * Creates a new pad with the given name from the given template.
562  * If name is NULL, a guaranteed unique name (across all pads)
563  * will be assigned.
564  * This function makes a copy of the name so you can safely free the name.
565  *
566  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
567  */
568 GstPad *
569 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
570 {
571   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
572
573   return g_object_new (GST_TYPE_PAD,
574       "name", name, "direction", templ->direction, "template", templ, NULL);
575 }
576
577 /**
578  * gst_pad_new_from_static_template:
579  * @templ: the #GstStaticPadTemplate to use
580  * @name: the name of the element
581  *
582  * Creates a new pad with the given name from the given static template.
583  * If name is NULL, a guaranteed unique name (across all pads)
584  * will be assigned.
585  * This function makes a copy of the name so you can safely free the name.
586  *
587  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
588  */
589 GstPad *
590 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
591     const gchar * name)
592 {
593   GstPad *pad;
594   GstPadTemplate *template;
595
596   template = gst_static_pad_template_get (templ);
597   pad = gst_pad_new_from_template (template, name);
598   gst_object_unref (template);
599   return pad;
600 }
601
602 /**
603  * gst_pad_get_direction:
604  * @pad: a #GstPad to get the direction of.
605  *
606  * Gets the direction of the pad. The direction of the pad is
607  * decided at construction time so this function does not take
608  * the LOCK.
609  *
610  * Returns: the #GstPadDirection of the pad.
611  *
612  * MT safe.
613  */
614 GstPadDirection
615 gst_pad_get_direction (GstPad * pad)
616 {
617   GstPadDirection result;
618
619   /* PAD_UNKNOWN is a little silly but we need some sort of
620    * error return value */
621   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
622
623   result = GST_PAD_DIRECTION (pad);
624
625   return result;
626 }
627
628 static gboolean
629 gst_pad_activate_default (GstPad * pad)
630 {
631   return gst_pad_activate_push (pad, TRUE);
632 }
633
634 static void
635 pre_activate (GstPad * pad, GstPadActivateMode new_mode)
636 {
637   switch (new_mode) {
638     case GST_PAD_ACTIVATE_PUSH:
639     case GST_PAD_ACTIVATE_PULL:
640       GST_OBJECT_LOCK (pad);
641       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
642           new_mode);
643       GST_PAD_UNSET_FLUSHING (pad);
644       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
645       GST_OBJECT_UNLOCK (pad);
646       break;
647     case GST_PAD_ACTIVATE_NONE:
648       GST_OBJECT_LOCK (pad);
649       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
650       GST_PAD_SET_FLUSHING (pad);
651       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
652       /* unlock blocked pads so element can resume and stop */
653       GST_PAD_BLOCK_BROADCAST (pad);
654       GST_OBJECT_UNLOCK (pad);
655       break;
656   }
657 }
658
659 static void
660 post_activate (GstPad * pad, GstPadActivateMode new_mode)
661 {
662   switch (new_mode) {
663     case GST_PAD_ACTIVATE_PUSH:
664     case GST_PAD_ACTIVATE_PULL:
665       /* nop */
666       break;
667     case GST_PAD_ACTIVATE_NONE:
668       /* ensures that streaming stops */
669       GST_PAD_STREAM_LOCK (pad);
670       GST_DEBUG_OBJECT (pad, "stopped streaming");
671       GST_OBJECT_LOCK (pad);
672       clear_events (pad->priv->events);
673       GST_OBJECT_UNLOCK (pad);
674       GST_PAD_STREAM_UNLOCK (pad);
675       break;
676   }
677 }
678
679 /**
680  * gst_pad_set_active:
681  * @pad: the #GstPad to activate or deactivate.
682  * @active: whether or not the pad should be active.
683  *
684  * Activates or deactivates the given pad.
685  * Normally called from within core state change functions.
686  *
687  * If @active, makes sure the pad is active. If it is already active, either in
688  * push or pull mode, just return. Otherwise dispatches to the pad's activate
689  * function to perform the actual activation.
690  *
691  * If not @active, checks the pad's current mode and calls
692  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
693  * FALSE argument.
694  *
695  * Returns: #TRUE if the operation was successful.
696  *
697  * MT safe.
698  */
699 gboolean
700 gst_pad_set_active (GstPad * pad, gboolean active)
701 {
702   GstPadActivateMode old;
703   gboolean ret = FALSE;
704
705   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
706
707   GST_OBJECT_LOCK (pad);
708   old = GST_PAD_ACTIVATE_MODE (pad);
709   GST_OBJECT_UNLOCK (pad);
710
711   if (active) {
712     switch (old) {
713       case GST_PAD_ACTIVATE_PUSH:
714         GST_DEBUG_OBJECT (pad, "activating pad from push");
715         ret = TRUE;
716         break;
717       case GST_PAD_ACTIVATE_PULL:
718         GST_DEBUG_OBJECT (pad, "activating pad from pull");
719         ret = TRUE;
720         break;
721       case GST_PAD_ACTIVATE_NONE:
722         GST_DEBUG_OBJECT (pad, "activating pad from none");
723         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
724         break;
725       default:
726         GST_DEBUG_OBJECT (pad, "unknown activation mode!");
727         break;
728     }
729   } else {
730     switch (old) {
731       case GST_PAD_ACTIVATE_PUSH:
732         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
733         ret = gst_pad_activate_push (pad, FALSE);
734         break;
735       case GST_PAD_ACTIVATE_PULL:
736         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
737         ret = gst_pad_activate_pull (pad, FALSE);
738         break;
739       case GST_PAD_ACTIVATE_NONE:
740         GST_DEBUG_OBJECT (pad, "deactivating pad from none");
741         ret = TRUE;
742         break;
743       default:
744         GST_DEBUG_OBJECT (pad, "unknown activation mode!");
745         break;
746     }
747   }
748
749   if (!ret) {
750     GST_OBJECT_LOCK (pad);
751     if (!active) {
752       g_critical ("Failed to deactivate pad %s:%s, very bad",
753           GST_DEBUG_PAD_NAME (pad));
754     } else {
755       GST_WARNING_OBJECT (pad, "Failed to activate pad");
756     }
757     GST_OBJECT_UNLOCK (pad);
758   } else {
759     if (!active) {
760       GST_OBJECT_LOCK (pad);
761       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
762       GST_OBJECT_UNLOCK (pad);
763     }
764   }
765
766   return ret;
767 }
768
769 /**
770  * gst_pad_activate_pull:
771  * @pad: the #GstPad to activate or deactivate.
772  * @active: whether or not the pad should be active.
773  *
774  * Activates or deactivates the given pad in pull mode via dispatching to the
775  * pad's activatepullfunc. For use from within pad activation functions only.
776  * When called on sink pads, will first proxy the call to the peer pad, which
777  * is expected to activate its internally linked pads from within its
778  * activate_pull function.
779  *
780  * If you don't know what this is, you probably don't want to call it.
781  *
782  * Returns: TRUE if the operation was successful.
783  *
784  * MT safe.
785  */
786 gboolean
787 gst_pad_activate_pull (GstPad * pad, gboolean active)
788 {
789   GstPadActivateMode old, new;
790   GstPad *peer;
791
792   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
793
794   GST_OBJECT_LOCK (pad);
795   old = GST_PAD_ACTIVATE_MODE (pad);
796   GST_OBJECT_UNLOCK (pad);
797
798   if (active) {
799     switch (old) {
800       case GST_PAD_ACTIVATE_PULL:
801         GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
802         goto was_ok;
803       case GST_PAD_ACTIVATE_PUSH:
804         GST_DEBUG_OBJECT (pad,
805             "activating pad from push, deactivate push first");
806         /* pad was activate in the wrong direction, deactivate it
807          * and reactivate it in pull mode */
808         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
809           goto deactivate_failed;
810         /* fallthrough, pad is deactivated now. */
811       case GST_PAD_ACTIVATE_NONE:
812         GST_DEBUG_OBJECT (pad, "activating pad from none");
813         break;
814     }
815   } else {
816     switch (old) {
817       case GST_PAD_ACTIVATE_NONE:
818         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
819         goto was_ok;
820       case GST_PAD_ACTIVATE_PUSH:
821         GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
822         /* pad was activated in the other direction, deactivate it
823          * in push mode, this should not happen... */
824         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
825           goto deactivate_failed;
826         /* everything is fine now */
827         goto was_ok;
828       case GST_PAD_ACTIVATE_PULL:
829         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
830         break;
831     }
832   }
833
834   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
835     if ((peer = gst_pad_get_peer (pad))) {
836       GST_DEBUG_OBJECT (pad, "calling peer");
837       if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
838         goto peer_failed;
839       gst_object_unref (peer);
840     } else {
841       /* there is no peer, this is only fatal when we activate. When we
842        * deactivate, we must assume the application has unlinked the peer and
843        * will deactivate it eventually. */
844       if (active)
845         goto not_linked;
846       else
847         GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
848     }
849   } else {
850     if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
851       goto failure;             /* Can't activate pull on a src without a
852                                    getrange function */
853   }
854
855   new = active ? GST_PAD_ACTIVATE_PULL : GST_PAD_ACTIVATE_NONE;
856   pre_activate (pad, new);
857
858   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
859     if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
860       goto failure;
861   } else {
862     /* can happen for sinks of passthrough elements */
863   }
864
865   post_activate (pad, new);
866
867   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
868       active ? "activated" : "deactivated");
869
870   return TRUE;
871
872 was_ok:
873   {
874     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
875         active ? "activated" : "deactivated");
876     return TRUE;
877   }
878 deactivate_failed:
879   {
880     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
881         "failed to %s in switch to pull from mode %d",
882         (active ? "activate" : "deactivate"), old);
883     return FALSE;
884   }
885 peer_failed:
886   {
887     GST_OBJECT_LOCK (peer);
888     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
889         "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
890     GST_OBJECT_UNLOCK (peer);
891     gst_object_unref (peer);
892     return FALSE;
893   }
894 not_linked:
895   {
896     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
897         "pad in pull mode");
898     return FALSE;
899   }
900 failure:
901   {
902     GST_OBJECT_LOCK (pad);
903     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
904         active ? "activate" : "deactivate");
905     GST_PAD_SET_FLUSHING (pad);
906     GST_PAD_ACTIVATE_MODE (pad) = old;
907     GST_OBJECT_UNLOCK (pad);
908     return FALSE;
909   }
910 }
911
912 /**
913  * gst_pad_activate_push:
914  * @pad: the #GstPad to activate or deactivate.
915  * @active: whether the pad should be active or not.
916  *
917  * Activates or deactivates the given pad in push mode via dispatching to the
918  * pad's activatepushfunc. For use from within pad activation functions only.
919  *
920  * If you don't know what this is, you probably don't want to call it.
921  *
922  * Returns: %TRUE if the operation was successful.
923  *
924  * MT safe.
925  */
926 gboolean
927 gst_pad_activate_push (GstPad * pad, gboolean active)
928 {
929   GstPadActivateMode old, new;
930
931   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
932   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
933       active ? "activated" : "deactivated");
934
935   GST_OBJECT_LOCK (pad);
936   old = GST_PAD_ACTIVATE_MODE (pad);
937   GST_OBJECT_UNLOCK (pad);
938
939   if (active) {
940     switch (old) {
941       case GST_PAD_ACTIVATE_PUSH:
942         GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
943         goto was_ok;
944       case GST_PAD_ACTIVATE_PULL:
945         GST_DEBUG_OBJECT (pad,
946             "activating pad from push, deactivating pull first");
947         /* pad was activate in the wrong direction, deactivate it
948          * an reactivate it in push mode */
949         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
950           goto deactivate_failed;
951         /* fallthrough, pad is deactivated now. */
952       case GST_PAD_ACTIVATE_NONE:
953         GST_DEBUG_OBJECT (pad, "activating pad from none");
954         break;
955     }
956   } else {
957     switch (old) {
958       case GST_PAD_ACTIVATE_NONE:
959         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
960         goto was_ok;
961       case GST_PAD_ACTIVATE_PULL:
962         GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
963         /* pad was activated in the other direction, deactivate it
964          * in pull mode, this should not happen... */
965         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
966           goto deactivate_failed;
967         /* everything is fine now */
968         goto was_ok;
969       case GST_PAD_ACTIVATE_PUSH:
970         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
971         break;
972     }
973   }
974
975   new = active ? GST_PAD_ACTIVATE_PUSH : GST_PAD_ACTIVATE_NONE;
976   pre_activate (pad, new);
977
978   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
979     if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
980       goto failure;
981     }
982   } else {
983     /* quite ok, element relies on state change func to prepare itself */
984   }
985
986   post_activate (pad, new);
987
988   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
989       active ? "activated" : "deactivated");
990   return TRUE;
991
992 was_ok:
993   {
994     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
995         active ? "activated" : "deactivated");
996     return TRUE;
997   }
998 deactivate_failed:
999   {
1000     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1001         "failed to %s in switch to push from mode %d",
1002         (active ? "activate" : "deactivate"), old);
1003     return FALSE;
1004   }
1005 failure:
1006   {
1007     GST_OBJECT_LOCK (pad);
1008     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1009         active ? "activate" : "deactivate");
1010     GST_PAD_SET_FLUSHING (pad);
1011     GST_PAD_ACTIVATE_MODE (pad) = old;
1012     GST_OBJECT_UNLOCK (pad);
1013     return FALSE;
1014   }
1015 }
1016
1017 /**
1018  * gst_pad_is_active:
1019  * @pad: the #GstPad to query
1020  *
1021  * Query if a pad is active
1022  *
1023  * Returns: TRUE if the pad is active.
1024  *
1025  * MT safe.
1026  */
1027 gboolean
1028 gst_pad_is_active (GstPad * pad)
1029 {
1030   gboolean result = FALSE;
1031
1032   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1033
1034   GST_OBJECT_LOCK (pad);
1035   result = GST_PAD_IS_ACTIVE (pad);
1036   GST_OBJECT_UNLOCK (pad);
1037
1038   return result;
1039 }
1040
1041 /**
1042  * gst_pad_add_probe:
1043  * @pad: the #GstPad to add the probe to
1044  * @mask: the probe mask
1045  * @callback: #GstPadProbeCallback that will be called with notifications of
1046  *           the pad state
1047  * @user_data: (closure): user data passed to the callback
1048  * @destroy_data: #GDestroyNotify for user_data
1049  *
1050  * Be notified of different states of pads. The provided callback is called for
1051  * every state that matches @mask.
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, GstPadProbeType 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
1071   /* make a new probe */
1072   hook = g_hook_alloc (&pad->probes);
1073
1074   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1075       mask);
1076
1077   /* when no contraints are given for the types, assume all types are
1078    * acceptable */
1079   if ((mask & GST_PAD_PROBE_TYPE_DATA_BOTH) == 0)
1080     mask |= GST_PAD_PROBE_TYPE_DATA_BOTH;
1081   if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1082     mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1083
1084   /* store our flags and other fields */
1085   hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1086   hook->func = callback;
1087   hook->data = user_data;
1088   hook->destroy = destroy_data;
1089   PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1090
1091   /* add the probe */
1092   g_hook_prepend (&pad->probes, hook);
1093   pad->num_probes++;
1094   /* incremenent cookie so that the new hook get's called */
1095   pad->priv->probe_list_cookie++;
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_PAD_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_PAD_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_PAD_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   GstPadProbeType type;
1138
1139   type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1140
1141   if (type & GST_PAD_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   GstPadProbeType flags;
3324   GstPadProbeCallback callback;
3325   GstPadProbeReturn ret;
3326
3327   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3328       "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
3329
3330   /* if we have called this callback, do nothing */
3331   if (PROBE_COOKIE (hook) == data->cookie) {
3332     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3333         "hook %lu, cookie %u already called", hook->hook_id,
3334         PROBE_COOKIE (hook));
3335     return;
3336   }
3337
3338   PROBE_COOKIE (hook) = data->cookie;
3339
3340   flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3341
3342   /* one of the data types */
3343   if ((flags & GST_PAD_PROBE_TYPE_DATA_BOTH & data->mask) == 0)
3344     goto no_match;
3345   /* one of the scheduling types */
3346   if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & data->mask) == 0)
3347     goto no_match;
3348   /* all of the blocking types must match */
3349   if ((flags & GST_PAD_PROBE_TYPE_BLOCKING) !=
3350       (data->mask & GST_PAD_PROBE_TYPE_BLOCKING))
3351     goto no_match;
3352
3353   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3354       "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3355
3356   callback = (GstPadProbeCallback) hook->func;
3357   if (callback == NULL)
3358     return;
3359
3360   GST_OBJECT_UNLOCK (pad);
3361
3362   ret = callback (pad, data->mask, data->type_data, hook->data);
3363
3364   GST_OBJECT_LOCK (pad);
3365   data->marshalled = TRUE;
3366
3367   switch (ret) {
3368     case GST_PAD_PROBE_REMOVE:
3369       /* remove the probe */
3370       GST_DEBUG_OBJECT (pad, "asked to remove hook");
3371       cleanup_hook (pad, hook);
3372       break;
3373     case GST_PAD_PROBE_DROP:
3374       /* need to drop the data, make sure other probes don't get called
3375        * anymore */
3376       GST_DEBUG_OBJECT (pad, "asked to drop item");
3377       data->mask = GST_PAD_PROBE_TYPE_INVALID;
3378       data->dropped = TRUE;
3379       break;
3380     case GST_PAD_PROBE_PASS:
3381       /* inform the pad block to let things pass */
3382       GST_DEBUG_OBJECT (pad, "asked to pass item");
3383       data->pass = TRUE;
3384       break;
3385     default:
3386       GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3387       break;
3388   }
3389   return;
3390
3391 no_match:
3392   {
3393     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3394         "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
3395         flags, data->mask);
3396     return;
3397   }
3398 }
3399
3400 #define PROBE_FULL(pad,mask,data,label,defaultval)              \
3401   G_STMT_START {                                                \
3402     if (G_UNLIKELY (pad->num_probes)) {                         \
3403       ret = do_probe_callbacks (pad, mask, data, defaultval);   \
3404       if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3405         goto label;                                             \
3406     }                                                           \
3407   } G_STMT_END
3408
3409 #define PROBE(pad,mask,data,label)              \
3410   PROBE_FULL(pad, mask, data, label, GST_FLOW_OK);
3411
3412 static GstFlowReturn
3413 do_probe_callbacks (GstPad * pad, GstPadProbeType mask, gpointer type_data,
3414     GstFlowReturn defaultval)
3415 {
3416   ProbeMarshall data;
3417   guint cookie;
3418   gboolean is_block;
3419
3420   is_block = (mask & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3421
3422   data.pad = pad;
3423   data.mask = mask;
3424   data.type_data = type_data;
3425   data.pass = FALSE;
3426   data.marshalled = FALSE;
3427   data.dropped = FALSE;
3428   data.cookie = ++pad->priv->probe_cookie;
3429
3430 again:
3431   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3432       "do probes cookie %u", data.cookie);
3433   cookie = pad->priv->probe_list_cookie;
3434
3435   g_hook_list_marshal (&pad->probes, TRUE,
3436       (GHookMarshaller) probe_hook_marshal, &data);
3437
3438   /* if the list changed, call the new callbacks (they will not have their
3439    * cookie set to data.cookie */
3440   if (cookie != pad->priv->probe_list_cookie) {
3441     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3442         "probe list changed, restarting");
3443     goto again;
3444   }
3445
3446   /* the first item that dropped will stop the hooks and then we drop here */
3447   if (data.dropped)
3448     goto dropped;
3449
3450   /* if no handler matched and we are blocking, let the item pass */
3451   if (!data.marshalled && is_block)
3452     goto passed;
3453
3454   /* At this point, all handlers returned either OK or PASS. If one handler
3455    * returned PASS, let the item pass */
3456   if (data.pass)
3457     goto passed;
3458
3459   if (is_block) {
3460     while (GST_PAD_IS_BLOCKED (pad)) {
3461       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3462           "we are blocked %d times", pad->num_blocked);
3463
3464       /* we might have released the lock */
3465       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3466         goto flushing;
3467
3468       /* now we block the streaming thread. It can be unlocked when we
3469        * deactivate the pad (which will also set the FLUSHING flag) or
3470        * when the pad is unblocked. A flushing event will also unblock
3471        * the pad after setting the FLUSHING flag. */
3472       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3473           "Waiting to be unblocked or set flushing");
3474       GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3475       GST_PAD_BLOCK_WAIT (pad);
3476       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3477       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3478
3479       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3480         goto flushing;
3481     }
3482   }
3483
3484   return defaultval;
3485
3486   /* ERRORS */
3487 flushing:
3488   {
3489     GST_DEBUG_OBJECT (pad, "pad is flushing");
3490     return GST_FLOW_WRONG_STATE;
3491   }
3492 dropped:
3493   {
3494     GST_DEBUG_OBJECT (pad, "data is dropped");
3495     return GST_FLOW_CUSTOM_SUCCESS;
3496   }
3497 passed:
3498   {
3499     /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3500     GST_DEBUG_OBJECT (pad, "data is passed");
3501     return GST_FLOW_OK;
3502   }
3503 }
3504
3505 /* pad offsets */
3506
3507 /**
3508  * gst_pad_get_offset:
3509  * @pad: a #GstPad
3510  *
3511  * Get the offset applied to the running time of @pad. @pad has to be a source
3512  * pad.
3513  *
3514  * Returns: the offset.
3515  */
3516 gint64
3517 gst_pad_get_offset (GstPad * pad)
3518 {
3519   gint64 result;
3520
3521   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3522
3523   GST_OBJECT_LOCK (pad);
3524   result = pad->offset;
3525   GST_OBJECT_UNLOCK (pad);
3526
3527   return result;
3528 }
3529
3530 /**
3531  * gst_pad_set_offset:
3532  * @pad: a #GstPad
3533  * @offset: the offset
3534  *
3535  * Set the offset that will be applied to the running time of @pad.
3536  */
3537 void
3538 gst_pad_set_offset (GstPad * pad, gint64 offset)
3539 {
3540   guint idx;
3541   GstPad *peer;
3542   GstPad *tmp = NULL;
3543
3544   g_return_if_fail (GST_IS_PAD (pad));
3545
3546   GST_OBJECT_LOCK (pad);
3547   /* if nothing changed, do nothing */
3548   if (pad->offset == offset)
3549     goto done;
3550
3551   pad->offset = offset;
3552
3553   /* if no peer, we just updated the offset */
3554   if ((peer = GST_PAD_PEER (pad)) == NULL)
3555     goto done;
3556
3557   /* switch pads around when dealing with a sinkpad */
3558   if (GST_PAD_IS_SINK (pad)) {
3559     /* ref the peer so it doesn't go away when we release the lock */
3560     tmp = gst_object_ref (peer);
3561     /* make sure we get the peer (the srcpad) */
3562     GST_OBJECT_UNLOCK (pad);
3563
3564     /* swap pads */
3565     peer = pad;
3566     pad = tmp;
3567
3568     GST_OBJECT_LOCK (pad);
3569     /* check if the pad didn't get relinked */
3570     if (GST_PAD_PEER (pad) != peer)
3571       goto done;
3572
3573     /* we can release the ref now */
3574     gst_object_unref (peer);
3575   }
3576
3577   /* the index of the segment event in the array */
3578   idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3579
3580   /* lock order is srcpad >> sinkpad */
3581   GST_OBJECT_LOCK (peer);
3582   /* take the current segment event, adjust it and then place
3583    * it on the sinkpad. events on the srcpad are always active. */
3584   if (replace_event (pad, peer, idx))
3585     GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3586
3587   GST_OBJECT_UNLOCK (peer);
3588
3589 done:
3590   GST_OBJECT_UNLOCK (pad);
3591 }
3592
3593
3594 /**********************************************************************
3595  * Data passing functions
3596  */
3597
3598 /* this is the chain function that does not perform the additional argument
3599  * checking for that little extra speed.
3600  */
3601 static inline GstFlowReturn
3602 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3603 {
3604   GstFlowReturn ret;
3605   gboolean needs_events;
3606
3607   GST_PAD_STREAM_LOCK (pad);
3608
3609   GST_OBJECT_LOCK (pad);
3610   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3611     goto flushing;
3612
3613   needs_events = GST_PAD_NEEDS_EVENTS (pad);
3614   if (G_UNLIKELY (needs_events)) {
3615     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3616
3617     GST_DEBUG_OBJECT (pad, "need to update all events");
3618     ret = gst_pad_update_events (pad);
3619     if (G_UNLIKELY (ret != GST_FLOW_OK))
3620       goto events_error;
3621   }
3622
3623   PROBE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3624
3625   PROBE (pad, type, data, probe_stopped);
3626
3627   GST_OBJECT_UNLOCK (pad);
3628
3629   /* NOTE: we read the chainfunc unlocked.
3630    * we cannot hold the lock for the pad so we might send
3631    * the data to the wrong function. This is not really a
3632    * problem since functions are assigned at creation time
3633    * and don't change that often... */
3634   if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3635     GstPadChainFunction chainfunc;
3636
3637     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3638       goto no_function;
3639
3640     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3641         "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3642         GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3643
3644     ret = chainfunc (pad, GST_BUFFER_CAST (data));
3645
3646     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3647         "called chainfunction &%s with buffer %p, returned %s",
3648         GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3649   } else {
3650     GstPadChainListFunction chainlistfunc;
3651
3652     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3653       goto no_function;
3654
3655     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3656         "calling chainlistfunction &%s",
3657         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3658
3659     ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3660
3661     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3662         "called chainlistfunction &%s, returned %s",
3663         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3664   }
3665
3666   GST_PAD_STREAM_UNLOCK (pad);
3667
3668   return ret;
3669
3670   /* ERRORS */
3671 flushing:
3672   {
3673     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3674         "chaining, but pad was flushing");
3675     GST_OBJECT_UNLOCK (pad);
3676     GST_PAD_STREAM_UNLOCK (pad);
3677     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3678     return GST_FLOW_WRONG_STATE;
3679   }
3680 events_error:
3681   {
3682     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3683     GST_OBJECT_UNLOCK (pad);
3684     GST_PAD_STREAM_UNLOCK (pad);
3685     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3686     return ret;
3687   }
3688 probe_stopped:
3689   {
3690     GST_OBJECT_UNLOCK (pad);
3691     GST_PAD_STREAM_UNLOCK (pad);
3692     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3693
3694     switch (ret) {
3695       case GST_FLOW_CUSTOM_SUCCESS:
3696         GST_DEBUG_OBJECT (pad, "dropped buffer");
3697         ret = GST_FLOW_OK;
3698         break;
3699       default:
3700         GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3701         break;
3702     }
3703     return ret;
3704   }
3705 no_function:
3706   {
3707     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3708     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3709         "pushing, but not chainhandler");
3710     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3711         ("push on pad %s:%s but it has no chainfunction",
3712             GST_DEBUG_PAD_NAME (pad)));
3713     GST_PAD_STREAM_UNLOCK (pad);
3714     return GST_FLOW_NOT_SUPPORTED;
3715   }
3716 }
3717
3718 /**
3719  * gst_pad_chain:
3720  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3721  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3722  *     if not.
3723  *
3724  * Chain a buffer to @pad.
3725  *
3726  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3727  *
3728  * If the buffer type is not acceptable for @pad (as negotiated with a
3729  * preceeding GST_EVENT_CAPS event), this function returns
3730  * #GST_FLOW_NOT_NEGOTIATED.
3731  *
3732  * The function proceeds calling the chain function installed on @pad (see
3733  * gst_pad_set_chain_function()) and the return value of that function is
3734  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3735  * chain function.
3736  *
3737  * In all cases, success or failure, the caller loses its reference to @buffer
3738  * after calling this function.
3739  *
3740  * Returns: a #GstFlowReturn from the pad.
3741  *
3742  * MT safe.
3743  */
3744 GstFlowReturn
3745 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3746 {
3747   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3748   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3749   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3750
3751   return gst_pad_chain_data_unchecked (pad,
3752       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3753 }
3754
3755 static GstFlowReturn
3756 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3757 {
3758   guint i, len;
3759   GstBuffer *buffer;
3760   GstFlowReturn ret;
3761
3762   GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3763
3764   len = gst_buffer_list_length (list);
3765
3766   ret = GST_FLOW_OK;
3767   for (i = 0; i < len; i++) {
3768     buffer = gst_buffer_list_get (list, i);
3769     ret =
3770         gst_pad_chain_data_unchecked (pad,
3771         GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3772         gst_buffer_ref (buffer));
3773     if (ret != GST_FLOW_OK)
3774       break;
3775   }
3776   gst_buffer_list_unref (list);
3777
3778   return ret;
3779 }
3780
3781 /**
3782  * gst_pad_chain_list:
3783  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3784  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3785  *     if not.
3786  *
3787  * Chain a bufferlist to @pad.
3788  *
3789  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3790  *
3791  * If @pad was not negotiated properly with a CAPS event, this function
3792  * returns #GST_FLOW_NOT_NEGOTIATED.
3793  *
3794  * The function proceeds calling the chainlist function installed on @pad (see
3795  * gst_pad_set_chain_list_function()) and the return value of that function is
3796  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3797  * chainlist function.
3798  *
3799  * In all cases, success or failure, the caller loses its reference to @list
3800  * after calling this function.
3801  *
3802  * MT safe.
3803  *
3804  * Returns: a #GstFlowReturn from the pad.
3805  *
3806  * Since: 0.10.24
3807  */
3808 GstFlowReturn
3809 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3810 {
3811   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3812   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3813   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3814
3815   return gst_pad_chain_data_unchecked (pad,
3816       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3817 }
3818
3819 static GstFlowReturn
3820 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3821 {
3822   GstPad *peer;
3823   GstFlowReturn ret;
3824
3825   GST_OBJECT_LOCK (pad);
3826   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3827     goto flushing;
3828
3829   /* do block probes */
3830   PROBE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3831
3832   /* do post-blocking probes */
3833   PROBE (pad, type, data, probe_stopped);
3834
3835   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3836     goto not_linked;
3837
3838   /* take ref to peer pad before releasing the lock */
3839   gst_object_ref (peer);
3840   pad->priv->using++;
3841   GST_OBJECT_UNLOCK (pad);
3842
3843   ret = gst_pad_chain_data_unchecked (peer, type, data);
3844
3845   gst_object_unref (peer);
3846
3847   GST_OBJECT_LOCK (pad);
3848   pad->priv->using--;
3849   if (pad->priv->using == 0) {
3850     /* pad is not active anymore, trigger idle callbacks */
3851     PROBE_FULL (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
3852         probe_stopped, ret);
3853   }
3854   GST_OBJECT_UNLOCK (pad);
3855
3856   return ret;
3857
3858   /* ERROR recovery here */
3859   /* ERRORS */
3860 flushing:
3861   {
3862     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3863         "pushing, but pad was flushing");
3864     GST_OBJECT_UNLOCK (pad);
3865     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3866     return GST_FLOW_WRONG_STATE;
3867   }
3868 probe_stopped:
3869   {
3870     GST_OBJECT_UNLOCK (pad);
3871     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3872
3873     switch (ret) {
3874       case GST_FLOW_CUSTOM_SUCCESS:
3875         GST_DEBUG_OBJECT (pad, "dropped buffer");
3876         ret = GST_FLOW_OK;
3877         break;
3878       default:
3879         GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3880         break;
3881     }
3882     return ret;
3883   }
3884 not_linked:
3885   {
3886     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3887         "pushing, but it was not linked");
3888     GST_OBJECT_UNLOCK (pad);
3889     gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3890     return GST_FLOW_NOT_LINKED;
3891   }
3892 }
3893
3894 /**
3895  * gst_pad_push:
3896  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3897  * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3898  *     if not.
3899  *
3900  * Pushes a buffer to the peer of @pad.
3901  *
3902  * This function will call installed block probes before triggering any
3903  * installed data probes.
3904  *
3905  * The function proceeds calling gst_pad_chain() on the peer pad and returns
3906  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3907  * be returned.
3908  *
3909  * In all cases, success or failure, the caller loses its reference to @buffer
3910  * after calling this function.
3911  *
3912  * Returns: a #GstFlowReturn from the peer pad.
3913  *
3914  * MT safe.
3915  */
3916 GstFlowReturn
3917 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3918 {
3919   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3920   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3921   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3922
3923   return gst_pad_push_data (pad,
3924       GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3925 }
3926
3927 /**
3928  * gst_pad_push_list:
3929  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3930  * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3931  *     if not.
3932  *
3933  * Pushes a buffer list to the peer of @pad.
3934  *
3935  * This function will call installed block probes before triggering any
3936  * installed data probes.
3937  *
3938  * The function proceeds calling the chain function on the peer pad and returns
3939  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3940  * be returned. If the peer pad does not have any installed chainlist function
3941  * every group buffer of the list will be merged into a normal #GstBuffer and
3942  * chained via gst_pad_chain().
3943  *
3944  * In all cases, success or failure, the caller loses its reference to @list
3945  * after calling this function.
3946  *
3947  * Returns: a #GstFlowReturn from the peer pad.
3948  *
3949  * MT safe.
3950  *
3951  * Since: 0.10.24
3952  */
3953 GstFlowReturn
3954 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3955 {
3956   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3957   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3958   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3959
3960   return gst_pad_push_data (pad,
3961       GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3962 }
3963
3964 static GstFlowReturn
3965 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3966     GstBuffer ** buffer)
3967 {
3968   GstFlowReturn ret;
3969   GstPadGetRangeFunction getrangefunc;
3970
3971   GST_PAD_STREAM_LOCK (pad);
3972
3973   GST_OBJECT_LOCK (pad);
3974   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3975     goto flushing;
3976
3977   PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK, NULL,
3978       probe_stopped);
3979   GST_OBJECT_UNLOCK (pad);
3980
3981   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3982     goto no_function;
3983
3984   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3985       "calling getrangefunc %s, offset %"
3986       G_GUINT64_FORMAT ", size %u",
3987       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3988
3989   ret = getrangefunc (pad, offset, size, buffer);
3990
3991   if (G_UNLIKELY (ret != GST_FLOW_OK))
3992     goto get_range_failed;
3993
3994   /* can only fire the signal if we have a valid buffer */
3995   GST_OBJECT_LOCK (pad);
3996   PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, *buffer,
3997       probe_stopped_unref);
3998   GST_OBJECT_UNLOCK (pad);
3999
4000   GST_PAD_STREAM_UNLOCK (pad);
4001
4002   return ret;
4003
4004   /* ERRORS */
4005 flushing:
4006   {
4007     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4008         "getrange, but pad was flushing");
4009     GST_OBJECT_UNLOCK (pad);
4010     GST_PAD_STREAM_UNLOCK (pad);
4011     return GST_FLOW_WRONG_STATE;
4012   }
4013 no_function:
4014   {
4015     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4016         ("getrange on pad %s:%s but it has no getrangefunction",
4017             GST_DEBUG_PAD_NAME (pad)));
4018     GST_PAD_STREAM_UNLOCK (pad);
4019     return GST_FLOW_NOT_SUPPORTED;
4020   }
4021 probe_stopped:
4022   {
4023     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4024         "probe returned %s", gst_flow_get_name (ret));
4025     GST_OBJECT_UNLOCK (pad);
4026     GST_PAD_STREAM_UNLOCK (pad);
4027     return ret;
4028   }
4029 probe_stopped_unref:
4030   {
4031     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4032         "probe returned %s", gst_flow_get_name (ret));
4033     GST_OBJECT_UNLOCK (pad);
4034     GST_PAD_STREAM_UNLOCK (pad);
4035     gst_buffer_unref (*buffer);
4036     *buffer = NULL;
4037     return ret;
4038   }
4039 get_range_failed:
4040   {
4041     GST_PAD_STREAM_UNLOCK (pad);
4042     *buffer = NULL;
4043     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4044         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4045         pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4046     return ret;
4047   }
4048 }
4049
4050 /**
4051  * gst_pad_get_range:
4052  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4053  * @offset: The start offset of the buffer
4054  * @size: The length of the buffer
4055  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4056  *     returns #GST_FLOW_ERROR if %NULL.
4057  *
4058  * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4059  * immediately and @buffer is %NULL.
4060  *
4061  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4062  * description of a getrange function. If @pad has no getrange function
4063  * installed (see gst_pad_set_getrange_function()) this function returns
4064  * #GST_FLOW_NOT_SUPPORTED.
4065  *
4066  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4067  *
4068  * Returns: a #GstFlowReturn from the pad.
4069  *
4070  * MT safe.
4071  */
4072 GstFlowReturn
4073 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4074     GstBuffer ** buffer)
4075 {
4076   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4077   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4078   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4079
4080   return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4081 }
4082
4083 /**
4084  * gst_pad_pull_range:
4085  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4086  * @offset: The start offset of the buffer
4087  * @size: The length of the buffer
4088  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4089  *     GST_FLOW_ERROR if %NULL.
4090  *
4091  * Pulls a @buffer from the peer pad.
4092  *
4093  * This function will first trigger the pad block signal if it was
4094  * installed.
4095  *
4096  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4097  * function returns the result of gst_pad_get_range() on the peer pad.
4098  * See gst_pad_get_range() for a list of return values and for the
4099  * semantics of the arguments of this function.
4100  *
4101  * Returns: a #GstFlowReturn from the peer pad.
4102  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4103  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4104  * @buffer may not be used or freed when any other return value than
4105  * #GST_FLOW_OK is returned.
4106  *
4107  * MT safe.
4108  */
4109 GstFlowReturn
4110 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4111     GstBuffer ** buffer)
4112 {
4113   GstPad *peer;
4114   GstFlowReturn ret;
4115   gboolean needs_events;
4116
4117   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4118   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4119   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4120
4121   GST_OBJECT_LOCK (pad);
4122   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4123     goto flushing;
4124
4125   PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK, NULL,
4126       pre_probe_stopped);
4127
4128   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4129     goto not_linked;
4130
4131   gst_object_ref (peer);
4132   pad->priv->using++;
4133   GST_OBJECT_UNLOCK (pad);
4134
4135   ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4136
4137   gst_object_unref (peer);
4138
4139   GST_OBJECT_LOCK (pad);
4140   pad->priv->using--;
4141   if (pad->priv->using == 0) {
4142     /* pad is not active anymore, trigger idle callbacks */
4143     PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE, NULL,
4144         post_probe_stopped);
4145   }
4146
4147   if (G_UNLIKELY (ret != GST_FLOW_OK))
4148     goto pull_range_failed;
4149
4150   PROBE (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER, buffer,
4151       post_probe_stopped);
4152
4153   needs_events = GST_PAD_NEEDS_EVENTS (pad);
4154   if (G_UNLIKELY (needs_events)) {
4155     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4156
4157     GST_DEBUG_OBJECT (pad, "we need to update the events");
4158     ret = gst_pad_update_events (pad);
4159     if (G_UNLIKELY (ret != GST_FLOW_OK))
4160       goto events_error;
4161   }
4162   GST_OBJECT_UNLOCK (pad);
4163
4164   return ret;
4165
4166   /* ERROR recovery here */
4167 flushing:
4168   {
4169     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4170         "pullrange, but pad was flushing");
4171     GST_OBJECT_UNLOCK (pad);
4172     return GST_FLOW_WRONG_STATE;
4173   }
4174 pre_probe_stopped:
4175   {
4176     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4177         gst_flow_get_name (ret));
4178     GST_OBJECT_UNLOCK (pad);
4179     return ret;
4180   }
4181 not_linked:
4182   {
4183     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4184         "pulling range, but it was not linked");
4185     GST_OBJECT_UNLOCK (pad);
4186     return GST_FLOW_NOT_LINKED;
4187   }
4188 pull_range_failed:
4189   {
4190     *buffer = NULL;
4191     GST_OBJECT_UNLOCK (pad);
4192     GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4193         (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4194         pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4195     return ret;
4196   }
4197 post_probe_stopped:
4198   {
4199     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4200         "post probe returned %s", gst_flow_get_name (ret));
4201     GST_OBJECT_UNLOCK (pad);
4202     if (ret == GST_FLOW_OK)
4203       gst_buffer_unref (*buffer);
4204     *buffer = NULL;
4205     return ret;
4206   }
4207 events_error:
4208   {
4209     GST_OBJECT_UNLOCK (pad);
4210     gst_buffer_unref (*buffer);
4211     *buffer = NULL;
4212     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4213         "pullrange returned events that were not accepted");
4214     return ret;
4215   }
4216 }
4217
4218 /**
4219  * gst_pad_push_event:
4220  * @pad: a #GstPad to push the event to.
4221  * @event: (transfer full): the #GstEvent to send to the pad.
4222  *
4223  * Sends the event to the peer of the given pad. This function is
4224  * mainly used by elements to send events to their peer
4225  * elements.
4226  *
4227  * This function takes owership of the provided event so you should
4228  * gst_event_ref() it if you want to reuse the event after this call.
4229  *
4230  * Returns: TRUE if the event was handled.
4231  *
4232  * MT safe.
4233  */
4234 gboolean
4235 gst_pad_push_event (GstPad * pad, GstEvent * event)
4236 {
4237   GstFlowReturn ret;
4238   GstPad *peerpad;
4239   gboolean result;
4240   gboolean stored = FALSE;
4241   GstPadProbeType type;
4242
4243   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4244   g_return_val_if_fail (event != NULL, FALSE);
4245   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4246
4247   if (GST_EVENT_IS_DOWNSTREAM (event))
4248     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4249   else
4250     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4251
4252   GST_OBJECT_LOCK (pad);
4253
4254   peerpad = GST_PAD_PEER (pad);
4255
4256   /* Two checks to be made:
4257    * . (un)set the FLUSHING flag for flushing events,
4258    * . handle pad blocking */
4259   switch (GST_EVENT_TYPE (event)) {
4260     case GST_EVENT_FLUSH_START:
4261       GST_PAD_SET_FLUSHING (pad);
4262
4263       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4264         /* flush start will have set the FLUSHING flag and will then
4265          * unlock all threads doing a GCond wait on the blocking pad. This
4266          * will typically unblock the STREAMING thread blocked on a pad. */
4267         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4268             "doing block signal.");
4269         GST_PAD_BLOCK_BROADCAST (pad);
4270         goto flushed;
4271       }
4272       break;
4273     case GST_EVENT_FLUSH_STOP:
4274       GST_PAD_UNSET_FLUSHING (pad);
4275
4276       /* Remove sticky EOS events */
4277       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4278       clear_event (pad->priv->events,
4279           GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4280
4281       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4282         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4283         goto flushed;
4284       }
4285       break;
4286     default:
4287     {
4288       /* store the event on the pad, but only on srcpads */
4289       if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4290         guint idx;
4291
4292         idx = GST_EVENT_STICKY_IDX (event);
4293         GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4294             GST_EVENT_TYPE_NAME (event), idx);
4295
4296         /* srcpad sticky events always become active immediately */
4297         gst_event_replace (&pad->priv->events[idx].event, event);
4298
4299         stored = TRUE;
4300       }
4301
4302       /* backwards compatibility mode for caps */
4303       switch (GST_EVENT_TYPE (event)) {
4304         case GST_EVENT_CAPS:
4305         {
4306           GST_OBJECT_UNLOCK (pad);
4307
4308           g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4309
4310           GST_OBJECT_LOCK (pad);
4311           /* the peerpad might have changed. Things we checked above could not
4312            * have changed. */
4313           peerpad = GST_PAD_PEER (pad);
4314           break;
4315         }
4316         case GST_EVENT_SEGMENT:
4317         {
4318           gint64 offset;
4319
4320           offset = pad->offset;
4321           /* check if we need to adjust the segment */
4322           if (offset != 0 && (peerpad != NULL)) {
4323             GstSegment segment;
4324
4325             /* copy segment values */
4326             gst_event_copy_segment (event, &segment);
4327             gst_event_unref (event);
4328
4329             /* adjust and make a new event with the offset applied */
4330             segment.base += offset;
4331             event = gst_event_new_segment (&segment);
4332           }
4333           break;
4334         }
4335         case GST_EVENT_RECONFIGURE:
4336           if (GST_PAD_IS_SINK (pad))
4337             GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4338           break;
4339         default:
4340           break;
4341       }
4342
4343       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4344         goto flushed;
4345
4346       PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4347           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4348
4349       break;
4350     }
4351   }
4352
4353   /* send probes after modifying the events above */
4354   PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4355
4356   /* now check the peer pad */
4357   if (peerpad == NULL)
4358     goto not_linked;
4359
4360   gst_object_ref (peerpad);
4361   pad->priv->using++;
4362   GST_OBJECT_UNLOCK (pad);
4363
4364   GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4365       event, GST_EVENT_TYPE_NAME (event), peerpad);
4366
4367   result = gst_pad_send_event (peerpad, event);
4368
4369   /* Note: we gave away ownership of the event at this point but we can still
4370    * print the old pointer */
4371   GST_LOG_OBJECT (pad,
4372       "sent event %p to peerpad %" GST_PTR_FORMAT ", result %d", event, peerpad,
4373       result);
4374
4375   gst_object_unref (peerpad);
4376
4377   GST_OBJECT_LOCK (pad);
4378   pad->priv->using--;
4379   if (pad->priv->using == 0) {
4380     /* pad is not active anymore, trigger idle callbacks */
4381     PROBE (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE, NULL,
4382         probe_stopped);
4383   }
4384   GST_OBJECT_UNLOCK (pad);
4385
4386   return result | stored;
4387
4388   /* ERROR handling */
4389 flushed:
4390   {
4391     GST_DEBUG_OBJECT (pad, "We're flushing");
4392     GST_OBJECT_UNLOCK (pad);
4393     gst_event_unref (event);
4394     return stored;
4395   }
4396 probe_stopped:
4397   {
4398     GST_DEBUG_OBJECT (pad, "Probe returned %s", gst_flow_get_name (ret));
4399     GST_OBJECT_UNLOCK (pad);
4400     gst_event_unref (event);
4401     return stored;
4402   }
4403 not_linked:
4404   {
4405     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4406     GST_OBJECT_UNLOCK (pad);
4407     gst_event_unref (event);
4408     return stored;
4409   }
4410 }
4411
4412 /**
4413  * gst_pad_send_event:
4414  * @pad: a #GstPad to send the event to.
4415  * @event: (transfer full): the #GstEvent to send to the pad.
4416  *
4417  * Sends the event to the pad. This function can be used
4418  * by applications to send events in the pipeline.
4419  *
4420  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4421  * sink pad, @event should be a downstream event. For example, you would not
4422  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4423  * Furthermore, some downstream events have to be serialized with data flow,
4424  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4425  * the event needs to be serialized with data flow, this function will take the
4426  * pad's stream lock while calling its event function.
4427  *
4428  * To find out whether an event type is upstream, downstream, or downstream and
4429  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4430  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4431  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4432  * plugin doesn't need to bother itself with this information; the core handles
4433  * all necessary locks and checks.
4434  *
4435  * This function takes owership of the provided event so you should
4436  * gst_event_ref() it if you want to reuse the event after this call.
4437  *
4438  * Returns: TRUE if the event was handled.
4439  */
4440 gboolean
4441 gst_pad_send_event (GstPad * pad, GstEvent * event)
4442 {
4443   GstFlowReturn ret;
4444   gboolean result = FALSE;
4445   gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4446   GstPadProbeType type;
4447
4448   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4449   g_return_val_if_fail (event != NULL, FALSE);
4450
4451   GST_OBJECT_LOCK (pad);
4452   if (GST_PAD_IS_SINK (pad)) {
4453     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4454       goto wrong_direction;
4455     serialized = GST_EVENT_IS_SERIALIZED (event);
4456     sticky = GST_EVENT_IS_STICKY (event);
4457     type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4458   } else if (GST_PAD_IS_SRC (pad)) {
4459     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4460       goto wrong_direction;
4461     /* events on srcpad never are serialized and sticky */
4462     serialized = sticky = FALSE;
4463     type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4464   } else
4465     goto unknown_direction;
4466
4467   /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4468    * event. */
4469   needs_events = GST_PAD_NEEDS_EVENTS (pad);
4470
4471   switch (GST_EVENT_TYPE (event)) {
4472     case GST_EVENT_FLUSH_START:
4473       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4474           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4475
4476       /* can't even accept a flush begin event when flushing */
4477       if (GST_PAD_IS_FLUSHING (pad))
4478         goto flushing;
4479
4480       GST_PAD_SET_FLUSHING (pad);
4481       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4482       needs_events = FALSE;
4483       break;
4484     case GST_EVENT_FLUSH_STOP:
4485       if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_PAD_ACTIVATE_NONE)) {
4486         GST_PAD_UNSET_FLUSHING (pad);
4487         GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4488       }
4489       /* Remove pending EOS events */
4490       GST_LOG_OBJECT (pad, "Removing pending EOS events");
4491       clear_event (pad->priv->events,
4492           GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_EOS));
4493
4494       GST_OBJECT_UNLOCK (pad);
4495       /* grab stream lock */
4496       GST_PAD_STREAM_LOCK (pad);
4497       need_unlock = TRUE;
4498       GST_OBJECT_LOCK (pad);
4499       needs_events = FALSE;
4500       break;
4501     case GST_EVENT_RECONFIGURE:
4502       if (GST_PAD_IS_SRC (pad))
4503         GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4504     default:
4505       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4506           GST_EVENT_TYPE_NAME (event));
4507
4508       if (serialized) {
4509         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4510         GST_OBJECT_UNLOCK (pad);
4511         GST_PAD_STREAM_LOCK (pad);
4512         need_unlock = TRUE;
4513         GST_OBJECT_LOCK (pad);
4514       } else {
4515         /* don't forward events on non-serialized events */
4516         needs_events = FALSE;
4517       }
4518
4519       /* store the event on the pad, but only on srcpads. We need to store the
4520        * event before checking the flushing flag. */
4521       if (sticky) {
4522         guint idx;
4523         PadEvent *ev;
4524
4525         switch (GST_EVENT_TYPE (event)) {
4526           case GST_EVENT_SEGMENT:
4527             if (pad->offset != 0) {
4528               GstSegment segment;
4529
4530               /* copy segment values */
4531               gst_event_copy_segment (event, &segment);
4532               gst_event_unref (event);
4533
4534               /* adjust and make a new event with the offset applied */
4535               segment.base += pad->offset;
4536               event = gst_event_new_segment (&segment);
4537             }
4538             break;
4539           default:
4540             break;
4541         }
4542
4543         idx = GST_EVENT_STICKY_IDX (event);
4544         ev = &pad->priv->events[idx];
4545
4546         if (ev->event != event) {
4547           GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4548               GST_EVENT_TYPE_NAME (event), idx);
4549           gst_event_replace (&ev->pending, event);
4550           /* set the flag so that we update the events next time. We would
4551            * usually update below but we might be flushing too. */
4552           GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4553           needs_events = TRUE;
4554         }
4555       }
4556       /* now do the probe */
4557       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4558         goto flushing;
4559
4560       PROBE (pad,
4561           type | GST_PAD_PROBE_TYPE_PUSH |
4562           GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4563
4564       PROBE (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4565
4566       break;
4567   }
4568
4569   if (G_UNLIKELY (needs_events)) {
4570     GstFlowReturn ret;
4571
4572     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4573
4574     GST_DEBUG_OBJECT (pad, "need to update all events");
4575     ret = gst_pad_update_events (pad);
4576     if (ret != GST_FLOW_OK)
4577       goto update_failed;
4578     GST_OBJECT_UNLOCK (pad);
4579
4580     gst_event_unref (event);
4581
4582     result = TRUE;
4583   }
4584
4585   /* ensure to pass on event;
4586    * note that a sticky event has already been updated above */
4587   if (G_LIKELY (!needs_events || !sticky)) {
4588     GstPadEventFunction eventfunc;
4589
4590     if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4591       goto no_function;
4592
4593     GST_OBJECT_UNLOCK (pad);
4594
4595     result = eventfunc (pad, event);
4596   }
4597
4598   if (need_unlock)
4599     GST_PAD_STREAM_UNLOCK (pad);
4600
4601   GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4602
4603   return result;
4604
4605   /* ERROR handling */
4606 wrong_direction:
4607   {
4608     g_warning ("pad %s:%s sending %s event in wrong direction",
4609         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4610     GST_OBJECT_UNLOCK (pad);
4611     gst_event_unref (event);
4612     return FALSE;
4613   }
4614 unknown_direction:
4615   {
4616     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4617     GST_OBJECT_UNLOCK (pad);
4618     gst_event_unref (event);
4619     return FALSE;
4620   }
4621 no_function:
4622   {
4623     g_warning ("pad %s:%s has no event handler, file a bug.",
4624         GST_DEBUG_PAD_NAME (pad));
4625     GST_OBJECT_UNLOCK (pad);
4626     if (need_unlock)
4627       GST_PAD_STREAM_UNLOCK (pad);
4628     gst_event_unref (event);
4629     return FALSE;
4630   }
4631 flushing:
4632   {
4633     GST_OBJECT_UNLOCK (pad);
4634     if (need_unlock)
4635       GST_PAD_STREAM_UNLOCK (pad);
4636     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4637         "Received event on flushing pad. Discarding");
4638     gst_event_unref (event);
4639     return FALSE;
4640   }
4641 probe_stopped:
4642   {
4643     GST_DEBUG_OBJECT (pad, "probe returned %s", gst_flow_get_name (ret));
4644     GST_OBJECT_UNLOCK (pad);
4645     if (need_unlock)
4646       GST_PAD_STREAM_UNLOCK (pad);
4647     gst_event_unref (event);
4648     return FALSE;
4649   }
4650 update_failed:
4651   {
4652     GST_OBJECT_UNLOCK (pad);
4653     if (need_unlock)
4654       GST_PAD_STREAM_UNLOCK (pad);
4655     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4656     gst_event_unref (event);
4657     return FALSE;
4658   }
4659 }
4660
4661 /**
4662  * gst_pad_set_element_private:
4663  * @pad: the #GstPad to set the private data of.
4664  * @priv: The private data to attach to the pad.
4665  *
4666  * Set the given private data gpointer on the pad.
4667  * This function can only be used by the element that owns the pad.
4668  * No locking is performed in this function.
4669  */
4670 void
4671 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4672 {
4673   pad->element_private = priv;
4674 }
4675
4676 /**
4677  * gst_pad_get_element_private:
4678  * @pad: the #GstPad to get the private data of.
4679  *
4680  * Gets the private data of a pad.
4681  * No locking is performed in this function.
4682  *
4683  * Returns: (transfer none): a #gpointer to the private data.
4684  */
4685 gpointer
4686 gst_pad_get_element_private (GstPad * pad)
4687 {
4688   return pad->element_private;
4689 }
4690
4691 /**
4692  * gst_pad_get_sticky_event:
4693  * @pad: the #GstPad to get the event from.
4694  * @event_type: the #GstEventType that should be retrieved.
4695  *
4696  * Returns a new reference of the sticky event of type @event_type
4697  * from the event.
4698  *
4699  * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4700  */
4701 GstEvent *
4702 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4703 {
4704   GstEvent *event = NULL;
4705   guint idx;
4706
4707   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4708   g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4709
4710   idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4711
4712   GST_OBJECT_LOCK (pad);
4713   if ((event = pad->priv->events[idx].event)) {
4714     gst_event_ref (event);
4715   }
4716   GST_OBJECT_UNLOCK (pad);
4717
4718   return event;
4719 }
4720
4721 /**
4722  * gst_pad_sticky_events_foreach:
4723  * @pad: the #GstPad that should be used for iteration.
4724  * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4725  * @user_data: (closure): the optional user data.
4726  *
4727  * Iterates all active sticky events on @pad and calls @foreach_func for every
4728  * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4729  * is immediately stopped.
4730  *
4731  * Returns: GST_FLOW_OK if iteration was successful
4732  */
4733 GstFlowReturn
4734 gst_pad_sticky_events_foreach (GstPad * pad,
4735     GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4736 {
4737   GstFlowReturn ret = GST_FLOW_OK;
4738   guint i;
4739   GstEvent *event;
4740
4741   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4742   g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4743
4744   GST_OBJECT_LOCK (pad);
4745
4746 restart:
4747   for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4748     gboolean res;
4749     PadEvent *ev;
4750
4751     ev = &pad->priv->events[i];
4752
4753     /* skip without active event */
4754     if ((event = ev->event) == NULL)
4755       continue;
4756
4757     gst_event_ref (event);
4758     GST_OBJECT_UNLOCK (pad);
4759
4760     res = foreach_func (pad, event, user_data);
4761
4762     GST_OBJECT_LOCK (pad);
4763     gst_event_unref (event);
4764
4765     if (res != GST_FLOW_OK) {
4766       ret = res;
4767       break;
4768     }
4769
4770     /* things could have changed while we release the lock, check if we still
4771      * are handling the same event, if we don't something changed and we have
4772      * to try again. FIXME. we need a cookie here. */
4773     if (event != ev->event) {
4774       GST_DEBUG_OBJECT (pad, "events changed, restarting");
4775       goto restart;
4776     }
4777   }
4778   GST_OBJECT_UNLOCK (pad);
4779
4780   return ret;
4781 }
4782
4783 static void
4784 do_stream_status (GstPad * pad, GstStreamStatusType type,
4785     GThread * thread, GstTask * task)
4786 {
4787   GstElement *parent;
4788
4789   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4790
4791   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4792     if (GST_IS_ELEMENT (parent)) {
4793       GstMessage *message;
4794       GValue value = { 0 };
4795
4796       if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4797         gchar *tname, *ename, *pname;
4798
4799         /* create a good task name */
4800         ename = gst_element_get_name (parent);
4801         pname = gst_pad_get_name (pad);
4802         tname = g_strdup_printf ("%s:%s", ename, pname);
4803         g_free (ename);
4804         g_free (pname);
4805
4806         gst_object_set_name (GST_OBJECT_CAST (task), tname);
4807         g_free (tname);
4808       }
4809
4810       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4811           type, parent);
4812
4813       g_value_init (&value, GST_TYPE_TASK);
4814       g_value_set_object (&value, task);
4815       gst_message_set_stream_status_object (message, &value);
4816       g_value_unset (&value);
4817
4818       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4819       gst_element_post_message (parent, message);
4820     }
4821     gst_object_unref (parent);
4822   }
4823 }
4824
4825 static void
4826 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4827 {
4828   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4829       thread, task);
4830 }
4831
4832 static void
4833 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4834 {
4835   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4836       thread, task);
4837 }
4838
4839 static GstTaskThreadCallbacks thr_callbacks = {
4840   pad_enter_thread,
4841   pad_leave_thread,
4842 };
4843
4844 /**
4845  * gst_pad_start_task:
4846  * @pad: the #GstPad to start the task of
4847  * @func: the task function to call
4848  * @data: data passed to the task function
4849  *
4850  * Starts a task that repeatedly calls @func with @data. This function
4851  * is mostly used in pad activation functions to start the dataflow.
4852  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4853  * before @func is called.
4854  *
4855  * Returns: a %TRUE if the task could be started.
4856  */
4857 gboolean
4858 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4859 {
4860   GstTask *task;
4861   gboolean res;
4862
4863   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4864   g_return_val_if_fail (func != NULL, FALSE);
4865
4866   GST_DEBUG_OBJECT (pad, "start task");
4867
4868   GST_OBJECT_LOCK (pad);
4869   task = GST_PAD_TASK (pad);
4870   if (task == NULL) {
4871     task = gst_task_new (func, data);
4872     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4873     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4874     GST_DEBUG_OBJECT (pad, "created task");
4875     GST_PAD_TASK (pad) = task;
4876     gst_object_ref (task);
4877     /* release lock to post the message */
4878     GST_OBJECT_UNLOCK (pad);
4879
4880     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4881
4882     gst_object_unref (task);
4883
4884     GST_OBJECT_LOCK (pad);
4885     /* nobody else is supposed to have changed the pad now */
4886     if (GST_PAD_TASK (pad) != task)
4887       goto concurrent_stop;
4888   }
4889   res = gst_task_set_state (task, GST_TASK_STARTED);
4890   GST_OBJECT_UNLOCK (pad);
4891
4892   return res;
4893
4894   /* ERRORS */
4895 concurrent_stop:
4896   {
4897     GST_OBJECT_UNLOCK (pad);
4898     return TRUE;
4899   }
4900 }
4901
4902 /**
4903  * gst_pad_pause_task:
4904  * @pad: the #GstPad to pause the task of
4905  *
4906  * Pause the task of @pad. This function will also wait until the
4907  * function executed by the task is finished if this function is not
4908  * called from the task function.
4909  *
4910  * Returns: a TRUE if the task could be paused or FALSE when the pad
4911  * has no task.
4912  */
4913 gboolean
4914 gst_pad_pause_task (GstPad * pad)
4915 {
4916   GstTask *task;
4917   gboolean res;
4918
4919   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4920
4921   GST_DEBUG_OBJECT (pad, "pause task");
4922
4923   GST_OBJECT_LOCK (pad);
4924   task = GST_PAD_TASK (pad);
4925   if (task == NULL)
4926     goto no_task;
4927   res = gst_task_set_state (task, GST_TASK_PAUSED);
4928   GST_OBJECT_UNLOCK (pad);
4929
4930   /* wait for task function to finish, this lock is recursive so it does nothing
4931    * when the pause is called from the task itself */
4932   GST_PAD_STREAM_LOCK (pad);
4933   GST_PAD_STREAM_UNLOCK (pad);
4934
4935   return res;
4936
4937 no_task:
4938   {
4939     GST_DEBUG_OBJECT (pad, "pad has no task");
4940     GST_OBJECT_UNLOCK (pad);
4941     return FALSE;
4942   }
4943 }
4944
4945 /**
4946  * gst_pad_stop_task:
4947  * @pad: the #GstPad to stop the task of
4948  *
4949  * Stop the task of @pad. This function will also make sure that the
4950  * function executed by the task will effectively stop if not called
4951  * from the GstTaskFunction.
4952  *
4953  * This function will deadlock if called from the GstTaskFunction of
4954  * the task. Use gst_task_pause() instead.
4955  *
4956  * Regardless of whether the pad has a task, the stream lock is acquired and
4957  * released so as to ensure that streaming through this pad has finished.
4958  *
4959  * Returns: a TRUE if the task could be stopped or FALSE on error.
4960  */
4961 gboolean
4962 gst_pad_stop_task (GstPad * pad)
4963 {
4964   GstTask *task;
4965   gboolean res;
4966
4967   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4968
4969   GST_DEBUG_OBJECT (pad, "stop task");
4970
4971   GST_OBJECT_LOCK (pad);
4972   task = GST_PAD_TASK (pad);
4973   if (task == NULL)
4974     goto no_task;
4975   GST_PAD_TASK (pad) = NULL;
4976   res = gst_task_set_state (task, GST_TASK_STOPPED);
4977   GST_OBJECT_UNLOCK (pad);
4978
4979   GST_PAD_STREAM_LOCK (pad);
4980   GST_PAD_STREAM_UNLOCK (pad);
4981
4982   if (!gst_task_join (task))
4983     goto join_failed;
4984
4985   gst_object_unref (task);
4986
4987   return res;
4988
4989 no_task:
4990   {
4991     GST_DEBUG_OBJECT (pad, "no task");
4992     GST_OBJECT_UNLOCK (pad);
4993
4994     GST_PAD_STREAM_LOCK (pad);
4995     GST_PAD_STREAM_UNLOCK (pad);
4996
4997     /* this is not an error */
4998     return TRUE;
4999   }
5000 join_failed:
5001   {
5002     /* this is bad, possibly the application tried to join the task from
5003      * the task's thread. We install the task again so that it will be stopped
5004      * again from the right thread next time hopefully. */
5005     GST_OBJECT_LOCK (pad);
5006     GST_DEBUG_OBJECT (pad, "join failed");
5007     /* we can only install this task if there was no other task */
5008     if (GST_PAD_TASK (pad) == NULL)
5009       GST_PAD_TASK (pad) = task;
5010     GST_OBJECT_UNLOCK (pad);
5011
5012     return FALSE;
5013   }
5014 }