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