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