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