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