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