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