gst/: Fix internal data flow errors. Fixes #338711.
[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().
58  *
59  * Last reviewed on 2005-11-23 (0.9.5)
60  *
61  */
62
63 #include "gst_private.h"
64
65 #include "gstpad.h"
66 #include "gstghostpad.h"
67 #include "gstpadtemplate.h"
68 #include "gstenumtypes.h"
69 #include "gstmarshal.h"
70 #include "gstutils.h"
71 #include "gstinfo.h"
72 #include "gsterror.h"
73 #include "gstvalue.h"
74 #include "glib-compat-private.h"
75
76 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
77 #define GST_CAT_DEFAULT GST_CAT_PADS
78
79 /* Pad signals and args */
80 enum
81 {
82   PAD_LINKED,
83   PAD_UNLINKED,
84   PAD_REQUEST_LINK,
85   PAD_HAVE_DATA,
86   /* FILL ME */
87   LAST_SIGNAL
88 };
89
90 enum
91 {
92   PAD_PROP_0,
93   PAD_PROP_CAPS,
94   PAD_PROP_DIRECTION,
95   PAD_PROP_TEMPLATE,
96   /* FILL ME */
97 };
98
99 static void gst_pad_class_init (GstPadClass * klass);
100 static void gst_pad_init (GstPad * pad);
101 static void gst_pad_dispose (GObject * object);
102 static void gst_pad_finalize (GObject * object);
103 static void gst_pad_set_property (GObject * object, guint prop_id,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_pad_get_property (GObject * object, guint prop_id,
106     GValue * value, GParamSpec * pspec);
107
108 static GstFlowReturn handle_pad_block (GstPad * pad);
109 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
110 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
111 static gboolean gst_pad_activate_default (GstPad * pad);
112 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
113
114 #ifndef GST_DISABLE_LOADSAVE
115 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
116 #endif
117
118 static GstObjectClass *parent_class = NULL;
119 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
120
121 /* quarks for probe signals */
122 static GQuark buffer_quark;
123 static GQuark event_quark;
124
125 typedef struct
126 {
127   gint ret;
128   gchar *name;
129   GQuark quark;
130 } GstFlowQuarks;
131
132 static GstFlowQuarks flow_quarks[] = {
133   {GST_FLOW_RESEND, "resend", 0},
134   {GST_FLOW_OK, "ok", 0},
135   {GST_FLOW_NOT_LINKED, "not-linked", 0},
136   {GST_FLOW_WRONG_STATE, "wrong-state", 0},
137   {GST_FLOW_UNEXPECTED, "unexpected", 0},
138   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
139   {GST_FLOW_ERROR, "error", 0},
140   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
141
142   {0, NULL, 0}
143 };
144
145 /**
146  * gst_flow_get_name:
147  * @ret: a #GstFlowReturn to get the name of.
148  *
149  * Gets a string representing the given flow return.
150  *
151  * Returns: a string with the name of the flow return.
152  */
153 G_CONST_RETURN gchar *
154 gst_flow_get_name (GstFlowReturn ret)
155 {
156   gint i;
157
158   for (i = 0; flow_quarks[i].name; i++) {
159     if (ret == flow_quarks[i].ret)
160       return flow_quarks[i].name;
161   }
162   return "unknown";
163 }
164
165 /**
166  * gst_flow_to_quark:
167  * @ret: a #GstFlowReturn to get the quark of.
168  *
169  * Get the unique quark for the given GstFlowReturn.
170  *
171  * Returns: the quark associated with the flow return or 0 if an
172  * invalid return was specified.
173  */
174 GQuark
175 gst_flow_to_quark (GstFlowReturn ret)
176 {
177   gint i;
178
179   for (i = 0; flow_quarks[i].name; i++) {
180     if (ret == flow_quarks[i].ret)
181       return flow_quarks[i].quark;
182   }
183   return 0;
184 }
185
186 GType
187 gst_pad_get_type (void)
188 {
189   static GType gst_pad_type = 0;
190
191   if (G_UNLIKELY (gst_pad_type == 0)) {
192     static const GTypeInfo pad_info = {
193       sizeof (GstPadClass), NULL, NULL,
194       (GClassInitFunc) gst_pad_class_init, NULL, NULL,
195       sizeof (GstPad),
196       0,
197       (GInstanceInitFunc) gst_pad_init, NULL
198     };
199     gint i;
200
201     gst_pad_type = g_type_register_static (GST_TYPE_OBJECT, "GstPad",
202         &pad_info, 0);
203
204     buffer_quark = g_quark_from_static_string ("buffer");
205     event_quark = g_quark_from_static_string ("event");
206
207     for (i = 0; flow_quarks[i].name; i++) {
208       flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name);
209     }
210
211     GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW",
212         GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads");
213   }
214   return gst_pad_type;
215 }
216
217 static gboolean
218 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
219     GValue * return_accu, const GValue * handler_return, gpointer dummy)
220 {
221   gboolean ret = g_value_get_boolean (handler_return);
222
223   GST_DEBUG ("accumulated %d", ret);
224   g_value_set_boolean (return_accu, ret);
225
226   return ret;
227 }
228
229 static gboolean
230 default_have_data (GstPad * pad, GstMiniObject * o)
231 {
232   return TRUE;
233 }
234
235 static void
236 gst_pad_class_init (GstPadClass * klass)
237 {
238   GObjectClass *gobject_class;
239
240
241   GstObjectClass *gstobject_class;
242
243   gobject_class = (GObjectClass *) klass;
244   gstobject_class = (GstObjectClass *) klass;
245
246   parent_class = g_type_class_peek_parent (klass);
247
248   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
249   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pad_finalize);
250   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pad_set_property);
251   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pad_get_property);
252
253   /**
254    * GstPad::linked:
255    * @pad: the pad that emitted the signal
256    * @peer: the peer pad that has been connected
257    *
258    * Signals that a pad has been linked to the peer pad.
259    */
260   gst_pad_signals[PAD_LINKED] =
261       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
262       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
263       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
264   /**
265    * GstPad::unlinked:
266    * @pad: the pad that emitted the signal
267    * @peer: the peer pad that has been disconnected
268    *
269    * Signals that a pad has been unlinked from the peer pad.
270    */
271   gst_pad_signals[PAD_UNLINKED] =
272       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
273       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
274       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
275   /**
276    * GstPad::request-link:
277    * @pad: the pad that emitted the signal
278    * @peer: the peer pad for which a connection is requested
279    *
280    * Signals that a pad connection has been requested.
281    */
282   gst_pad_signals[PAD_REQUEST_LINK] =
283       g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
284       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
285       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
286
287   /**
288    * GstPad::have-data:
289    * @pad: the pad that emitted the signal
290    * @mini_obj: new data
291    *
292    * Signals that new data is available on the pad. This signal is used
293    * internally for implementing pad probes.
294    * See gst_pad_add_*_probe functions.
295    *
296    * Returns: %TRUE to keep the data, %FALSE to drop it
297    */
298   gst_pad_signals[PAD_HAVE_DATA] =
299       g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
300       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
301       G_STRUCT_OFFSET (GstPadClass, have_data),
302       _gst_do_pass_data_accumulator,
303       NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
304       GST_TYPE_MINI_OBJECT);
305
306   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_CAPS,
307       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
308           GST_TYPE_CAPS, G_PARAM_READABLE));
309   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_DIRECTION,
310       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
311           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
312           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
313   g_object_class_install_property (G_OBJECT_CLASS (klass), PAD_PROP_TEMPLATE,
314       g_param_spec_object ("template", "Template",
315           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
316           G_PARAM_READWRITE));
317
318 #ifndef GST_DISABLE_LOADSAVE
319   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
320 #endif
321   gstobject_class->path_string_separator = ".";
322
323   klass->have_data = default_have_data;
324 }
325
326 static void
327 gst_pad_init (GstPad * pad)
328 {
329   pad->direction = GST_PAD_UNKNOWN;
330   pad->peer = NULL;
331
332   pad->chainfunc = NULL;
333
334   pad->caps = NULL;
335
336   pad->linkfunc = NULL;
337   pad->getcapsfunc = NULL;
338
339   pad->activatefunc = GST_DEBUG_FUNCPTR (gst_pad_activate_default);
340   pad->eventfunc = GST_DEBUG_FUNCPTR (gst_pad_event_default);
341   pad->querytypefunc = GST_DEBUG_FUNCPTR (gst_pad_get_query_types_default);
342   pad->queryfunc = GST_DEBUG_FUNCPTR (gst_pad_query_default);
343   pad->intlinkfunc = GST_DEBUG_FUNCPTR (gst_pad_get_internal_links_default);
344 #if 0
345   GST_PAD_ACCEPTCAPSFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_acceptcaps_default);
346 #endif
347
348   pad->do_buffer_signals = 0;
349   pad->do_event_signals = 0;
350
351   GST_PAD_UNSET_FLUSHING (pad);
352
353   pad->preroll_lock = g_mutex_new ();
354   pad->preroll_cond = g_cond_new ();
355
356   pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
357   g_static_rec_mutex_init (pad->stream_rec_lock);
358
359   pad->block_cond = g_cond_new ();
360 }
361
362 static void
363 gst_pad_dispose (GObject * object)
364 {
365   GstPad *pad = GST_PAD (object);
366
367   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "dispose %s:%s",
368       GST_DEBUG_PAD_NAME (pad));
369
370   /* we don't hold a ref to the peer so we can just set the
371    * peer to NULL. */
372   GST_PAD_PEER (pad) = NULL;
373
374   /* clear the caps */
375   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
376
377   gst_pad_set_pad_template (pad, NULL);
378
379   G_OBJECT_CLASS (parent_class)->dispose (object);
380 }
381
382 static void
383 gst_pad_finalize (GObject * object)
384 {
385   GstPad *pad = GST_PAD (object);
386   GstTask *task;
387
388   /* in case the task is still around, clean it up */
389   if ((task = GST_PAD_TASK (pad))) {
390     gst_task_join (task);
391     GST_PAD_TASK (pad) = NULL;
392     gst_object_unref (task);
393   }
394
395   if (pad->stream_rec_lock) {
396     g_static_rec_mutex_free (pad->stream_rec_lock);
397     g_free (pad->stream_rec_lock);
398     pad->stream_rec_lock = NULL;
399   }
400   if (pad->preroll_lock) {
401     g_mutex_free (pad->preroll_lock);
402     g_cond_free (pad->preroll_cond);
403     pad->preroll_lock = NULL;
404     pad->preroll_cond = NULL;
405   }
406   if (pad->block_cond) {
407     g_cond_free (pad->block_cond);
408     pad->block_cond = NULL;
409   }
410
411   G_OBJECT_CLASS (parent_class)->finalize (object);
412 }
413
414 static void
415 gst_pad_set_property (GObject * object, guint prop_id,
416     const GValue * value, GParamSpec * pspec)
417 {
418   g_return_if_fail (GST_IS_PAD (object));
419
420   switch (prop_id) {
421     case PAD_PROP_DIRECTION:
422       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
423       break;
424     case PAD_PROP_TEMPLATE:
425       gst_pad_set_pad_template (GST_PAD_CAST (object),
426           (GstPadTemplate *) g_value_get_object (value));
427       break;
428     default:
429       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430       break;
431   }
432 }
433
434 static void
435 gst_pad_get_property (GObject * object, guint prop_id,
436     GValue * value, GParamSpec * pspec)
437 {
438   g_return_if_fail (GST_IS_PAD (object));
439
440   switch (prop_id) {
441     case PAD_PROP_CAPS:
442       g_value_set_boxed (value, GST_PAD_CAPS (object));
443       break;
444     case PAD_PROP_DIRECTION:
445       g_value_set_enum (value, GST_PAD_DIRECTION (object));
446       break;
447     case PAD_PROP_TEMPLATE:
448       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
449       break;
450     default:
451       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452       break;
453   }
454 }
455
456 /**
457  * gst_pad_new:
458  * @name: the name of the new pad.
459  * @direction: the #GstPadDirection of the pad.
460  *
461  * Creates a new pad with the given name in the given direction.
462  * If name is NULL, a guaranteed unique name (across all pads)
463  * will be assigned.
464  * This function makes a copy of the name so you can safely free the name.
465  *
466  * Returns: a new #GstPad, or NULL in case of an error.
467  *
468  * MT safe.
469  */
470 GstPad *
471 gst_pad_new (const gchar * name, GstPadDirection direction)
472 {
473   return g_object_new (GST_TYPE_PAD,
474       "name", name, "direction", direction, NULL);
475 }
476
477 /**
478  * gst_pad_new_from_template:
479  * @templ: the pad template to use
480  * @name: the name of the element
481  *
482  * Creates a new pad with the given name from the given template.
483  * If name is NULL, a guaranteed unique name (across all pads)
484  * will be assigned.
485  * This function makes a copy of the name so you can safely free the name.
486  *
487  * Returns: a new #GstPad, or NULL in case of an error.
488  */
489 GstPad *
490 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
491 {
492   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
493
494   return g_object_new (GST_TYPE_PAD,
495       "name", name, "direction", templ->direction, "template", templ, NULL);
496 }
497
498 /**
499  * gst_pad_new_from_static_template:
500  * @templ: the #GstStaticPadTemplate to use
501  * @name: the name of the element
502  *
503  * Creates a new pad with the given name from the given static template.
504  * If name is NULL, a guaranteed unique name (across all pads)
505  * will be assigned.
506  * This function makes a copy of the name so you can safely free the name.
507  *
508  * Returns: a new #GstPad, or NULL in case of an error.
509  */
510 GstPad *
511 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
512     const gchar * name)
513 {
514   GstPad *pad;
515   GstPadTemplate *template;
516
517   template = gst_static_pad_template_get (templ);
518   pad = gst_pad_new_from_template (template, name);
519   gst_object_unref (template);
520   return pad;
521 }
522
523 /**
524  * gst_pad_get_direction:
525  * @pad: a #GstPad to get the direction of.
526  *
527  * Gets the direction of the pad. The direction of the pad is
528  * decided at construction time so this function does not take
529  * the LOCK.
530  *
531  * Returns: the #GstPadDirection of the pad.
532  *
533  * MT safe.
534  */
535 GstPadDirection
536 gst_pad_get_direction (GstPad * pad)
537 {
538   GstPadDirection result;
539
540   /* PAD_UNKNOWN is a little silly but we need some sort of
541    * error return value */
542   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
543
544   GST_OBJECT_LOCK (pad);
545   result = GST_PAD_DIRECTION (pad);
546   GST_OBJECT_UNLOCK (pad);
547
548   return result;
549 }
550
551 static gboolean
552 gst_pad_activate_default (GstPad * pad)
553 {
554   return gst_pad_activate_push (pad, TRUE);
555 }
556
557 static void
558 pre_activate (GstPad * pad, GstActivateMode new_mode)
559 {
560   switch (new_mode) {
561     case GST_ACTIVATE_PUSH:
562     case GST_ACTIVATE_PULL:
563       GST_OBJECT_LOCK (pad);
564       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
565           new_mode);
566       GST_PAD_UNSET_FLUSHING (pad);
567       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
568       GST_OBJECT_UNLOCK (pad);
569       break;
570     case GST_ACTIVATE_NONE:
571       GST_OBJECT_LOCK (pad);
572       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing",
573           new_mode);
574       GST_PAD_SET_FLUSHING (pad);
575       /* unlock blocked pads so element can resume and stop */
576       GST_PAD_BLOCK_SIGNAL (pad);
577       GST_OBJECT_UNLOCK (pad);
578       break;
579   }
580 }
581
582 static void
583 post_activate (GstPad * pad, GstActivateMode new_mode)
584 {
585   switch (new_mode) {
586     case GST_ACTIVATE_PUSH:
587     case GST_ACTIVATE_PULL:
588       /* nop */
589       break;
590     case GST_ACTIVATE_NONE:
591       /* ensures that streaming stops */
592       GST_PAD_STREAM_LOCK (pad);
593       /* while we're at it set activation mode */
594       GST_OBJECT_LOCK (pad);
595       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d", new_mode);
596       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
597       GST_OBJECT_UNLOCK (pad);
598       GST_PAD_STREAM_UNLOCK (pad);
599       break;
600   }
601 }
602
603 /**
604  * gst_pad_set_active:
605  * @pad: the #GstPad to activate or deactivate.
606  * @active: whether or not the pad should be active.
607  *
608  * Activates or deactivates the given pad.
609  * Normally called from within core state change functions.
610  *
611  * If @active, makes sure the pad is active. If it is already active, either in
612  * push or pull mode, just return. Otherwise dispatches to the pad's activate
613  * function to perform the actual activation.
614  *
615  * If not @active, checks the pad's current mode and calls
616  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
617  * FALSE argument.
618  *
619  * Returns: #TRUE if the operation was successful.
620  *
621  * MT safe.
622  */
623 gboolean
624 gst_pad_set_active (GstPad * pad, gboolean active)
625 {
626   GstActivateMode old;
627   gboolean ret = FALSE;
628
629   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
630
631   GST_OBJECT_LOCK (pad);
632   old = GST_PAD_ACTIVATE_MODE (pad);
633   GST_OBJECT_UNLOCK (pad);
634
635   if (active) {
636     switch (old) {
637       case GST_ACTIVATE_PUSH:
638       case GST_ACTIVATE_PULL:
639         ret = TRUE;
640         break;
641       case GST_ACTIVATE_NONE:
642         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
643         break;
644     }
645   } else {
646     switch (old) {
647       case GST_ACTIVATE_PUSH:
648         ret = gst_pad_activate_push (pad, FALSE);
649         break;
650       case GST_ACTIVATE_PULL:
651         ret = gst_pad_activate_pull (pad, FALSE);
652         break;
653       case GST_ACTIVATE_NONE:
654         ret = TRUE;
655         break;
656     }
657   }
658
659   if (!active && !ret) {
660     GST_OBJECT_LOCK (pad);
661     g_critical ("Failed to deactivate pad %s:%s, very bad",
662         GST_DEBUG_PAD_NAME (pad));
663     GST_OBJECT_UNLOCK (pad);
664   }
665
666   return ret;
667 }
668
669 /**
670  * gst_pad_activate_pull:
671  * @pad: the #GstPad to activate or deactivate.
672  * @active: whether or not the pad should be active.
673  *
674  * Activates or deactivates the given pad in pull mode via dispatching to the
675  * pad's activatepullfunc. For use from within pad activation functions only.
676  * When called on sink pads, will first proxy the call to the peer pad, which is
677  * expected to activate its internally linked pads from within its activate_pull
678  * function.
679  *
680  * If you don't know what this is, you probably don't want to call it.
681  *
682  * Returns: TRUE if the operation was successful.
683  *
684  * MT safe.
685  */
686 gboolean
687 gst_pad_activate_pull (GstPad * pad, gboolean active)
688 {
689   GstActivateMode old, new;
690   GstPad *peer;
691
692   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
693
694   GST_OBJECT_LOCK (pad);
695   old = GST_PAD_ACTIVATE_MODE (pad);
696   GST_OBJECT_UNLOCK (pad);
697
698   if ((active && old == GST_ACTIVATE_PULL)
699       || (!active && old == GST_ACTIVATE_NONE))
700     goto was_ok;
701
702   if (active) {
703     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
704   } else {
705     g_return_val_if_fail (old == GST_ACTIVATE_PULL, FALSE);
706   }
707
708   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
709     if ((peer = gst_pad_get_peer (pad))) {
710       if (!gst_pad_activate_pull (peer, active))
711         goto peer_failed;
712       gst_object_unref (peer);
713     }
714   } else {
715     if (GST_PAD_GETRANGEFUNC (pad) == NULL)
716       goto failure;             /* Can't activate pull on a src without a 
717                                    getrange function */
718   }
719
720   new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
721   pre_activate (pad, new);
722
723   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
724     if (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active))
725       goto failure;
726   } else {
727     /* can happen for sinks of passthrough elements */
728   }
729
730   post_activate (pad, new);
731
732   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
733       active ? "activated" : "deactivated");
734   return TRUE;
735
736 was_ok:
737   {
738     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
739         active ? "activated" : "deactivated");
740     return TRUE;
741   }
742 peer_failed:
743   {
744     GST_OBJECT_LOCK (peer);
745     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
746         "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
747     GST_OBJECT_UNLOCK (peer);
748     gst_object_unref (peer);
749     return FALSE;
750   }
751 failure:
752   {
753     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
754         active ? "activate" : "deactivate");
755     pre_activate (pad, GST_ACTIVATE_NONE);
756     post_activate (pad, GST_ACTIVATE_NONE);
757     return FALSE;
758   }
759 }
760
761 /**
762  * gst_pad_activate_push:
763  * @pad: the #GstPad to activate or deactivate.
764  * @active: whether the pad should be active or not.
765  *
766  * Activates or deactivates the given pad in push mode via dispatching to the
767  * pad's activatepushfunc. For use from within pad activation functions only.
768  *
769  * If you don't know what this is, you probably don't want to call it.
770  *
771  * Returns: %TRUE if the operation was successful.
772  *
773  * MT safe.
774  */
775 gboolean
776 gst_pad_activate_push (GstPad * pad, gboolean active)
777 {
778   GstActivateMode old, new;
779
780   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
781   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
782       active ? "activated" : "deactivated");
783
784   GST_OBJECT_LOCK (pad);
785   old = GST_PAD_ACTIVATE_MODE (pad);
786   GST_OBJECT_UNLOCK (pad);
787
788   if ((active && old == GST_ACTIVATE_PUSH)
789       || (!active && old == GST_ACTIVATE_NONE))
790     goto was_ok;
791
792   if (active) {
793     g_return_val_if_fail (old == GST_ACTIVATE_NONE, FALSE);
794   } else {
795     g_return_val_if_fail (old == GST_ACTIVATE_PUSH, FALSE);
796   }
797
798   new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
799   pre_activate (pad, new);
800
801   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
802     if (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active)) {
803       goto failure;
804     }
805   } else {
806     /* quite ok, element relies on state change func to prepare itself */
807   }
808
809   post_activate (pad, new);
810
811   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
812       active ? "activated" : "deactivated");
813   return TRUE;
814
815 was_ok:
816   {
817     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
818         active ? "activated" : "deactivated");
819     return TRUE;
820   }
821 failure:
822   {
823     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
824         active ? "activate" : "deactivate");
825     pre_activate (pad, GST_ACTIVATE_NONE);
826     post_activate (pad, GST_ACTIVATE_NONE);
827     return FALSE;
828   }
829 }
830
831 /**
832  * gst_pad_is_active:
833  * @pad: the #GstPad to query
834  *
835  * Query if a pad is active
836  *
837  * Returns: TRUE if the pad is active.
838  *
839  * MT safe.
840  */
841 gboolean
842 gst_pad_is_active (GstPad * pad)
843 {
844   gboolean result = FALSE;
845
846   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
847
848   GST_OBJECT_LOCK (pad);
849   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
850   GST_OBJECT_UNLOCK (pad);
851
852   return result;
853 }
854
855 /**
856  * gst_pad_set_blocked_async:
857  * @pad: the #GstPad to block or unblock
858  * @blocked: boolean indicating whether the pad should be blocked or unblocked
859  * @callback: #GstPadBlockCallback that will be called when the
860  *            operation succeeds
861  * @user_data: user data passed to the callback
862  *
863  * Blocks or unblocks the dataflow on a pad. The provided callback
864  * is called when the operation succeeds; this happens right before the next
865  * attempt at pushing a buffer on the pad.
866  *
867  * This can take a while as the pad can only become blocked when real dataflow
868  * is happening.
869  * When the pipeline is stalled, for example in PAUSED, this can
870  * take an indeterminate amount of time.
871  * You can pass NULL as the callback to make this call block. Be careful with
872  * this blocking call as it might not return for reasons stated above.
873  *
874  * Returns: TRUE if the pad could be blocked. This function can fail
875  *   if wrong parameters were passed or the pad was already in the
876  *   requested state.
877  *
878  * MT safe.
879  */
880 gboolean
881 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
882     GstPadBlockCallback callback, gpointer user_data)
883 {
884   gboolean was_blocked, was_ghost = FALSE;
885
886   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
887
888   if (GST_IS_GHOST_PAD (pad)) {
889     pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
890     if (!pad) {
891       return FALSE;
892     }
893     was_ghost = TRUE;
894   }
895
896   GST_OBJECT_LOCK (pad);
897
898   was_blocked = GST_PAD_IS_BLOCKED (pad);
899
900   if (G_UNLIKELY (was_blocked == blocked))
901     goto had_right_state;
902
903   if (blocked) {
904     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad %s:%s",
905         GST_DEBUG_PAD_NAME (pad));
906
907     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
908     pad->block_callback = callback;
909     pad->block_data = user_data;
910     if (!callback) {
911       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
912       GST_PAD_BLOCK_WAIT (pad);
913       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
914     }
915   } else {
916     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad %s:%s",
917         GST_DEBUG_PAD_NAME (pad));
918
919     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
920
921     pad->block_callback = callback;
922     pad->block_data = user_data;
923
924     if (callback) {
925       GST_PAD_BLOCK_SIGNAL (pad);
926     } else {
927       GST_PAD_BLOCK_SIGNAL (pad);
928       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
929       GST_PAD_BLOCK_WAIT (pad);
930       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
931     }
932   }
933   GST_OBJECT_UNLOCK (pad);
934
935   if (was_ghost) {
936     gst_object_unref (pad);
937   }
938
939   return TRUE;
940
941 had_right_state:
942   {
943     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
944         "pad %s:%s was in right state (%d)", GST_DEBUG_PAD_NAME (pad),
945         was_blocked);
946     GST_OBJECT_UNLOCK (pad);
947
948     if (was_ghost) {
949       gst_object_unref (pad);
950     }
951     return FALSE;
952   }
953 }
954
955 /**
956  * gst_pad_set_blocked:
957  * @pad: the #GstPad to block or unblock
958  * @blocked: boolean indicating we should block or unblock
959  *
960  * Blocks or unblocks the dataflow on a pad. This function is
961  * a shortcut for gst_pad_set_blocked_async() with a NULL
962  * callback.
963  *
964  * Returns: TRUE if the pad could be blocked. This function can fail
965  *   wrong parameters were passed or the pad was already in the
966  *   requested state.
967  *
968  * MT safe.
969  */
970 gboolean
971 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
972 {
973   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
974 }
975
976 /**
977  * gst_pad_is_blocked:
978  * @pad: the #GstPad to query
979  *
980  * Checks if the pad is blocked or not. This function returns the
981  * last requested state of the pad. It is not certain that the pad
982  * is actually blocked at this point.
983  *
984  * Returns: TRUE if the pad is blocked.
985  *
986  * MT safe.
987  */
988 gboolean
989 gst_pad_is_blocked (GstPad * pad)
990 {
991   gboolean result = FALSE;
992
993   g_return_val_if_fail (GST_IS_PAD (pad), result);
994
995   GST_OBJECT_LOCK (pad);
996   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
997   GST_OBJECT_UNLOCK (pad);
998
999   return result;
1000 }
1001
1002 /**
1003  * gst_pad_set_activate_function:
1004  * @pad: a sink #GstPad.
1005  * @activate: the #GstPadActivateFunction to set.
1006  *
1007  * Sets the given activate function for the pad. The activate function will
1008  * dispatch to activate_push or activate_pull to perform the actual activation.
1009  * Only makes sense to set on sink pads.
1010  *
1011  * Call this function if your sink pad can start a pull-based task.
1012  */
1013 void
1014 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1015 {
1016   g_return_if_fail (GST_IS_PAD (pad));
1017
1018   GST_PAD_ACTIVATEFUNC (pad) = activate;
1019   GST_CAT_DEBUG (GST_CAT_PADS, "activatefunc for %s:%s set to %s",
1020       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activate));
1021 }
1022
1023 /**
1024  * gst_pad_set_activatepull_function:
1025  * @pad: a sink #GstPad.
1026  * @activatepull: the #GstPadActivateModeFunction to set.
1027  *
1028  * Sets the given activate_pull function for the pad. An activate_pull function
1029  * prepares the element and any upstream connections for pulling. See XXX
1030  * part-activation.txt for details.
1031  */
1032 void
1033 gst_pad_set_activatepull_function (GstPad * pad,
1034     GstPadActivateModeFunction activatepull)
1035 {
1036   g_return_if_fail (GST_IS_PAD (pad));
1037
1038   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1039   GST_CAT_DEBUG (GST_CAT_PADS, "activatepullfunc for %s:%s set to %s",
1040       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepull));
1041 }
1042
1043 /**
1044  * gst_pad_set_activatepush_function:
1045  * @pad: a sink #GstPad.
1046  * @activatepush: the #GstPadActivateModeFunction to set.
1047  *
1048  * Sets the given activate_push function for the pad. An activate_push function
1049  * prepares the element for pushing. See XXX part-activation.txt for details.
1050  */
1051 void
1052 gst_pad_set_activatepush_function (GstPad * pad,
1053     GstPadActivateModeFunction activatepush)
1054 {
1055   g_return_if_fail (GST_IS_PAD (pad));
1056
1057   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1058   GST_CAT_DEBUG (GST_CAT_PADS, "activatepushfunc for %s:%s set to %s",
1059       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (activatepush));
1060 }
1061
1062 /**
1063  * gst_pad_set_chain_function:
1064  * @pad: a sink #GstPad.
1065  * @chain: the #GstPadChainFunction to set.
1066  *
1067  * Sets the given chain function for the pad. The chain function is called to
1068  * process a #GstBuffer input buffer.
1069  */
1070 void
1071 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1072 {
1073   g_return_if_fail (GST_IS_PAD (pad));
1074   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
1075
1076   GST_PAD_CHAINFUNC (pad) = chain;
1077   GST_CAT_DEBUG (GST_CAT_PADS, "chainfunc for %s:%s set to %s",
1078       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (chain));
1079 }
1080
1081 /**
1082  * gst_pad_set_getrange_function:
1083  * @pad: a source #GstPad.
1084  * @get: the #GstPadGetRangeFunction to set.
1085  *
1086  * Sets the given getrange function for the pad. The getrange function is called to
1087  * produce a new #GstBuffer to start the processing pipeline. Getrange functions cannot
1088  * return %NULL.
1089  */
1090 void
1091 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1092 {
1093   g_return_if_fail (GST_IS_PAD (pad));
1094   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
1095
1096   GST_PAD_GETRANGEFUNC (pad) = get;
1097
1098   GST_CAT_DEBUG (GST_CAT_PADS, "getrangefunc for %s:%s  set to %s",
1099       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (get));
1100 }
1101
1102 /**
1103  * gst_pad_set_checkgetrange_function:
1104  * @pad: a source #GstPad.
1105  * @check: the #GstPadCheckGetRangeFunction to set.
1106  *
1107  * Sets the given checkgetrange function for the pad. Implement this function on
1108  * a pad if you dynamically support getrange based scheduling on the pad.
1109  */
1110 void
1111 gst_pad_set_checkgetrange_function (GstPad * pad,
1112     GstPadCheckGetRangeFunction check)
1113 {
1114   g_return_if_fail (GST_IS_PAD (pad));
1115   g_return_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC);
1116
1117   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1118
1119   GST_CAT_DEBUG (GST_CAT_PADS, "checkgetrangefunc for %s:%s  set to %s",
1120       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (check));
1121 }
1122
1123 /**
1124  * gst_pad_set_event_function:
1125  * @pad: a source #GstPad.
1126  * @event: the #GstPadEventFunction to set.
1127  *
1128  * Sets the given event handler for the pad.
1129  */
1130 void
1131 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1132 {
1133   g_return_if_fail (GST_IS_PAD (pad));
1134
1135   GST_PAD_EVENTFUNC (pad) = event;
1136
1137   GST_CAT_DEBUG (GST_CAT_PADS, "eventfunc for %s:%s  set to %s",
1138       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (event));
1139 }
1140
1141 /**
1142  * gst_pad_set_query_function:
1143  * @pad: a #GstPad of either direction.
1144  * @query: the #GstPadQueryFunction to set.
1145  *
1146  * Set the given query function for the pad.
1147  */
1148 void
1149 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1150 {
1151   g_return_if_fail (GST_IS_PAD (pad));
1152
1153   GST_PAD_QUERYFUNC (pad) = query;
1154
1155   GST_CAT_DEBUG (GST_CAT_PADS, "queryfunc for %s:%s  set to %s",
1156       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (query));
1157 }
1158
1159 /**
1160  * gst_pad_set_query_type_function:
1161  * @pad: a #GstPad of either direction.
1162  * @type_func: the #GstPadQueryTypeFunction to set.
1163  *
1164  * Set the given query type function for the pad.
1165  */
1166 void
1167 gst_pad_set_query_type_function (GstPad * pad,
1168     GstPadQueryTypeFunction type_func)
1169 {
1170   g_return_if_fail (GST_IS_PAD (pad));
1171
1172   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1173
1174   GST_CAT_DEBUG (GST_CAT_PADS, "querytypefunc for %s:%s  set to %s",
1175       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (type_func));
1176 }
1177
1178 /**
1179  * gst_pad_get_query_types:
1180  * @pad: a #GstPad.
1181  *
1182  * Get an array of supported queries that can be performed
1183  * on this pad.
1184  *
1185  * Returns: a zero-terminated array of #GstQueryType.
1186  */
1187 const GstQueryType *
1188 gst_pad_get_query_types (GstPad * pad)
1189 {
1190   GstPadQueryTypeFunction func;
1191
1192   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1193
1194   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1195     goto no_func;
1196
1197   return func (pad);
1198
1199 no_func:
1200   {
1201     return NULL;
1202   }
1203 }
1204
1205 static gboolean
1206 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1207 {
1208   *data = gst_pad_get_query_types (pad);
1209
1210   return TRUE;
1211 }
1212
1213 /**
1214  * gst_pad_get_query_types_default:
1215  * @pad: a #GstPad.
1216  *
1217  * Invoke the default dispatcher for the query types on
1218  * the pad.
1219  *
1220  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1221  * internally-linked pads has a query types function.
1222  */
1223 const GstQueryType *
1224 gst_pad_get_query_types_default (GstPad * pad)
1225 {
1226   GstQueryType *result = NULL;
1227
1228   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1229
1230   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1231       gst_pad_get_query_types_dispatcher, &result);
1232
1233   return result;
1234 }
1235
1236 /**
1237  * gst_pad_set_internal_link_function:
1238  * @pad: a #GstPad of either direction.
1239  * @intlink: the #GstPadIntLinkFunction to set.
1240  *
1241  * Sets the given internal link function for the pad.
1242  */
1243 void
1244 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1245 {
1246   g_return_if_fail (GST_IS_PAD (pad));
1247
1248   GST_PAD_INTLINKFUNC (pad) = intlink;
1249   GST_CAT_DEBUG (GST_CAT_PADS, "internal link for %s:%s  set to %s",
1250       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (intlink));
1251 }
1252
1253 /**
1254  * gst_pad_set_link_function:
1255  * @pad: a #GstPad.
1256  * @link: the #GstPadLinkFunction to set.
1257  *
1258  * Sets the given link function for the pad. It will be called when
1259  * the pad is linked with another pad.
1260  *
1261  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1262  * made.
1263  *
1264  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1265  * cannot be made for some reason.
1266  *
1267  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1268  * of the peer sink pad, if present.
1269  */
1270 void
1271 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1272 {
1273   g_return_if_fail (GST_IS_PAD (pad));
1274
1275   GST_PAD_LINKFUNC (pad) = link;
1276   GST_CAT_DEBUG (GST_CAT_PADS, "linkfunc for %s:%s set to %s",
1277       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (link));
1278 }
1279
1280 /**
1281  * gst_pad_set_unlink_function:
1282  * @pad: a #GstPad.
1283  * @unlink: the #GstPadUnlinkFunction to set.
1284  *
1285  * Sets the given unlink function for the pad. It will be called
1286  * when the pad is unlinked.
1287  */
1288 void
1289 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1290 {
1291   g_return_if_fail (GST_IS_PAD (pad));
1292
1293   GST_PAD_UNLINKFUNC (pad) = unlink;
1294   GST_CAT_DEBUG (GST_CAT_PADS, "unlinkfunc for %s:%s set to %s",
1295       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (unlink));
1296 }
1297
1298 /**
1299  * gst_pad_set_getcaps_function:
1300  * @pad: a #GstPad.
1301  * @getcaps: the #GstPadGetCapsFunction to set.
1302  *
1303  * Sets the given getcaps function for the pad. @getcaps should return the
1304  * allowable caps for a pad in the context of the element's state, its link to
1305  * other elements, and the devices or files it has opened. These caps must be a
1306  * subset of the pad template caps. In the NULL state with no links, @getcaps
1307  * should ideally return the same caps as the pad template. In rare
1308  * circumstances, an object property can affect the caps returned by @getcaps,
1309  * but this is discouraged.
1310  *
1311  * You do not need to call this function if @pad's allowed caps are always the
1312  * same as the pad template caps. This can only be true if the padtemplate
1313  * has fixed simple caps.
1314  *
1315  * For most filters, the caps returned by @getcaps is directly affected by the
1316  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1317  * the srcpad's getcaps function is directly related to the stream data. Again,
1318  * @getcaps should return the most specific caps it reasonably can, since this
1319  * helps with autoplugging.
1320  *
1321  * Note that the return value from @getcaps is owned by the caller, so the caller
1322  * should unref the caps after usage.
1323  */
1324 void
1325 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1326 {
1327   g_return_if_fail (GST_IS_PAD (pad));
1328
1329   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1330   GST_CAT_DEBUG (GST_CAT_PADS, "getcapsfunc for %s:%s set to %s",
1331       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (getcaps));
1332 }
1333
1334 /**
1335  * gst_pad_set_acceptcaps_function:
1336  * @pad: a #GstPad.
1337  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1338  *
1339  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1340  * will be called to check if the pad can accept the given caps.
1341  */
1342 /* Setting the
1343  * acceptcaps function to NULL restores the default behaviour of allowing 
1344  * any caps that matches the caps from gst_pad_get_caps.
1345  */
1346 void
1347 gst_pad_set_acceptcaps_function (GstPad * pad,
1348     GstPadAcceptCapsFunction acceptcaps)
1349 {
1350   g_return_if_fail (GST_IS_PAD (pad));
1351
1352   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1353   GST_CAT_DEBUG (GST_CAT_PADS, "acceptcapsfunc for %s:%s set to %s",
1354       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1355 }
1356
1357 /**
1358  * gst_pad_set_fixatecaps_function:
1359  * @pad: a #GstPad.
1360  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1361  *
1362  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1363  * will be called whenever the default values for a GstCaps needs to be
1364  * filled in.
1365  */
1366 void
1367 gst_pad_set_fixatecaps_function (GstPad * pad,
1368     GstPadFixateCapsFunction fixatecaps)
1369 {
1370   g_return_if_fail (GST_IS_PAD (pad));
1371
1372   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1373   GST_CAT_DEBUG (GST_CAT_PADS, "fixatecapsfunc for %s:%s set to %s",
1374       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1375 }
1376
1377 /**
1378  * gst_pad_set_setcaps_function:
1379  * @pad: a #GstPad.
1380  * @setcaps: the #GstPadSetCapsFunction to set.
1381  *
1382  * Sets the given setcaps function for the pad.  The setcaps function
1383  * will be called whenever a buffer with a new media type is pushed or
1384  * pulled from the pad. The pad/element needs to update its internal
1385  * structures to process the new media type. If this new type is not
1386  * acceptable, the setcaps function should return FALSE.
1387  */
1388 void
1389 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1390 {
1391   g_return_if_fail (GST_IS_PAD (pad));
1392
1393   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1394   GST_CAT_DEBUG (GST_CAT_PADS, "setcapsfunc for %s:%s set to %s",
1395       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (setcaps));
1396 }
1397
1398 /**
1399  * gst_pad_set_bufferalloc_function:
1400  * @pad: a sink #GstPad.
1401  * @bufalloc: the #GstPadBufferAllocFunction to set.
1402  *
1403  * Sets the given bufferalloc function for the pad. Note that the
1404  * bufferalloc function can only be set on sinkpads.
1405  */
1406 void
1407 gst_pad_set_bufferalloc_function (GstPad * pad,
1408     GstPadBufferAllocFunction bufalloc)
1409 {
1410   g_return_if_fail (GST_IS_PAD (pad));
1411   g_return_if_fail (GST_PAD_IS_SINK (pad));
1412
1413   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1414   GST_CAT_DEBUG (GST_CAT_PADS, "bufferallocfunc for %s:%s set to %s",
1415       GST_DEBUG_PAD_NAME (pad), GST_DEBUG_FUNCPTR_NAME (bufalloc));
1416 }
1417
1418 /**
1419  * gst_pad_unlink:
1420  * @srcpad: the source #GstPad to unlink.
1421  * @sinkpad: the sink #GstPad to unlink.
1422  *
1423  * Unlinks the source pad from the sink pad. Will emit the "unlinked" signal on
1424  * both pads.
1425  *
1426  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1427  * the pads were not linked together.
1428  *
1429  * MT safe.
1430  */
1431 gboolean
1432 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1433 {
1434   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1435   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1436
1437   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1438       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1439       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1440
1441   GST_OBJECT_LOCK (srcpad);
1442
1443   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1444     goto not_srcpad;
1445
1446   GST_OBJECT_LOCK (sinkpad);
1447
1448   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1449     goto not_sinkpad;
1450
1451   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1452     goto not_linked_together;
1453
1454   if (GST_PAD_UNLINKFUNC (srcpad)) {
1455     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1456   }
1457   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1458     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1459   }
1460
1461   /* first clear peers */
1462   GST_PAD_PEER (srcpad) = NULL;
1463   GST_PAD_PEER (sinkpad) = NULL;
1464
1465   GST_OBJECT_UNLOCK (sinkpad);
1466   GST_OBJECT_UNLOCK (srcpad);
1467
1468   /* fire off a signal to each of the pads telling them
1469    * that they've been unlinked */
1470   g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1471   g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1472
1473   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1474       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1475
1476   return TRUE;
1477
1478 not_srcpad:
1479   {
1480     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1481     GST_OBJECT_UNLOCK (srcpad);
1482     return FALSE;
1483   }
1484 not_sinkpad:
1485   {
1486     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1487     GST_OBJECT_UNLOCK (sinkpad);
1488     GST_OBJECT_UNLOCK (srcpad);
1489     return FALSE;
1490   }
1491 not_linked_together:
1492   {
1493     /* we do not emit a warning in this case because unlinking cannot
1494      * be made MT safe.*/
1495     GST_OBJECT_UNLOCK (sinkpad);
1496     GST_OBJECT_UNLOCK (srcpad);
1497     return FALSE;
1498   }
1499 }
1500
1501 /**
1502  * gst_pad_is_linked:
1503  * @pad: pad to check
1504  *
1505  * Checks if a @pad is linked to another pad or not.
1506  *
1507  * Returns: TRUE if the pad is linked, FALSE otherwise.
1508  *
1509  * MT safe.
1510  */
1511 gboolean
1512 gst_pad_is_linked (GstPad * pad)
1513 {
1514   gboolean result;
1515
1516   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1517
1518   GST_OBJECT_LOCK (pad);
1519   result = (GST_PAD_PEER (pad) != NULL);
1520   GST_OBJECT_UNLOCK (pad);
1521
1522   return result;
1523 }
1524
1525 /* get the caps from both pads and see if the intersection
1526  * is not empty.
1527  *
1528  * This function should be called with the pad LOCK on both
1529  * pads
1530  */
1531 static gboolean
1532 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1533 {
1534   GstCaps *srccaps;
1535   GstCaps *sinkcaps;
1536   GstCaps *icaps;
1537
1538   srccaps = gst_pad_get_caps_unlocked (src);
1539   sinkcaps = gst_pad_get_caps_unlocked (sink);
1540
1541   GST_CAT_DEBUG (GST_CAT_CAPS, "src caps %" GST_PTR_FORMAT, srccaps);
1542   GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1543
1544   /* if we have caps on both pads we can check the intersection. If one
1545    * of the caps is NULL, we return TRUE. */
1546   if (srccaps == NULL || sinkcaps == NULL)
1547     goto done;
1548
1549   icaps = gst_caps_intersect (srccaps, sinkcaps);
1550   gst_caps_unref (srccaps);
1551   gst_caps_unref (sinkcaps);
1552
1553   if (icaps == NULL)
1554     goto was_null;
1555
1556   GST_CAT_DEBUG (GST_CAT_CAPS,
1557       "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1558
1559   if (gst_caps_is_empty (icaps))
1560     goto was_empty;
1561
1562   gst_caps_unref (icaps);
1563
1564 done:
1565   return TRUE;
1566
1567   /* incompatible cases */
1568 was_null:
1569   {
1570     GST_CAT_DEBUG (GST_CAT_CAPS, "intersection gave NULL");
1571     return FALSE;
1572   }
1573 was_empty:
1574   {
1575     GST_CAT_DEBUG (GST_CAT_CAPS, "intersection is EMPTY");
1576     gst_caps_unref (icaps);
1577     return FALSE;
1578   }
1579 }
1580
1581 /* check if the grandparents of both pads are the same.
1582  * This check is required so that we don't try to link
1583  * pads from elements in different bins without ghostpads.
1584  *
1585  * The LOCK should be helt on both pads
1586  */
1587 static gboolean
1588 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1589 {
1590   GstObject *psrc, *psink;
1591   gboolean res = TRUE;
1592
1593   psrc = GST_OBJECT_PARENT (src);
1594   psink = GST_OBJECT_PARENT (sink);
1595
1596   /* if one of the pads has no parent, we allow the link */
1597   if (psrc && psink) {
1598     /* if the parents are the same, we have a loop */
1599     if (psrc == psink) {
1600       GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1601           psrc);
1602       res = FALSE;
1603       goto done;
1604     }
1605     /* if they both have a parent, we check the grandparents */
1606     psrc = gst_object_get_parent (psrc);
1607     psink = gst_object_get_parent (psink);
1608
1609     if (psrc != psink) {
1610       /* if they have grandparents but they are not the same */
1611       GST_CAT_DEBUG (GST_CAT_CAPS,
1612           "pads have different grandparents %" GST_PTR_FORMAT " and %"
1613           GST_PTR_FORMAT, psrc, psink);
1614       res = FALSE;
1615     }
1616     if (psrc)
1617       gst_object_unref (psrc);
1618     if (psink)
1619       gst_object_unref (psink);
1620   }
1621 done:
1622   return res;
1623 }
1624
1625 /* FIXME leftover from an attempt at refactoring... */
1626 /* call with the two pads unlocked */
1627 static GstPadLinkReturn
1628 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1629 {
1630   /* generic checks */
1631   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1632   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1633
1634   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1635       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1636
1637   GST_OBJECT_LOCK (srcpad);
1638
1639   if (G_UNLIKELY (GST_PAD_DIRECTION (srcpad) != GST_PAD_SRC))
1640     goto not_srcpad;
1641
1642   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1643     goto src_was_linked;
1644
1645   GST_OBJECT_LOCK (sinkpad);
1646
1647   if (G_UNLIKELY (GST_PAD_DIRECTION (sinkpad) != GST_PAD_SINK))
1648     goto not_sinkpad;
1649
1650   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1651     goto sink_was_linked;
1652
1653   /* check hierarchy, pads can only be linked if the grandparents
1654    * are the same. */
1655   if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1656     goto wrong_hierarchy;
1657
1658   /* check pad caps for non-empty intersection */
1659   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1660     goto no_format;
1661
1662   /* FIXME check pad scheduling for non-empty intersection */
1663
1664   return GST_PAD_LINK_OK;
1665
1666 not_srcpad:
1667   {
1668     g_critical ("pad %s is not a source pad", GST_PAD_NAME (srcpad));
1669     GST_OBJECT_UNLOCK (srcpad);
1670     return GST_PAD_LINK_WRONG_DIRECTION;
1671   }
1672 src_was_linked:
1673   {
1674     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked",
1675         GST_DEBUG_PAD_NAME (srcpad));
1676     /* we do not emit a warning in this case because unlinking cannot
1677      * be made MT safe.*/
1678     GST_OBJECT_UNLOCK (srcpad);
1679     return GST_PAD_LINK_WAS_LINKED;
1680   }
1681 not_sinkpad:
1682   {
1683     g_critical ("pad %s is not a sink pad", GST_PAD_NAME (sinkpad));
1684     GST_OBJECT_UNLOCK (sinkpad);
1685     GST_OBJECT_UNLOCK (srcpad);
1686     return GST_PAD_LINK_WRONG_DIRECTION;
1687   }
1688 sink_was_linked:
1689   {
1690     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked",
1691         GST_DEBUG_PAD_NAME (sinkpad));
1692     /* we do not emit a warning in this case because unlinking cannot
1693      * be made MT safe.*/
1694     GST_OBJECT_UNLOCK (sinkpad);
1695     GST_OBJECT_UNLOCK (srcpad);
1696     return GST_PAD_LINK_WAS_LINKED;
1697   }
1698 wrong_hierarchy:
1699   {
1700     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1701     GST_OBJECT_UNLOCK (sinkpad);
1702     GST_OBJECT_UNLOCK (srcpad);
1703     return GST_PAD_LINK_WRONG_HIERARCHY;
1704   }
1705 no_format:
1706   {
1707     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1708     GST_OBJECT_UNLOCK (sinkpad);
1709     GST_OBJECT_UNLOCK (srcpad);
1710     return GST_PAD_LINK_NOFORMAT;
1711   }
1712 }
1713
1714 /**
1715  * gst_pad_link:
1716  * @srcpad: the source #GstPad to link.
1717  * @sinkpad: the sink #GstPad to link.
1718  *
1719  * Links the source pad and the sink pad.
1720  *
1721  * Returns: A result code indicating if the connection worked or
1722  *          what went wrong.
1723  *
1724  * MT Safe.
1725  */
1726 GstPadLinkReturn
1727 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1728 {
1729   GstPadLinkReturn result;
1730
1731   /* prepare will also lock the two pads */
1732   result = gst_pad_link_prepare (srcpad, sinkpad);
1733
1734   if (result != GST_PAD_LINK_OK)
1735     goto prepare_failed;
1736
1737   /* must set peers before calling the link function */
1738   GST_PAD_PEER (srcpad) = sinkpad;
1739   GST_PAD_PEER (sinkpad) = srcpad;
1740
1741   GST_OBJECT_UNLOCK (sinkpad);
1742   GST_OBJECT_UNLOCK (srcpad);
1743
1744   /* FIXME released the locks here, concurrent thread might link
1745    * something else. */
1746   if (GST_PAD_LINKFUNC (srcpad)) {
1747     /* this one will call the peer link function */
1748     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
1749   } else if (GST_PAD_LINKFUNC (sinkpad)) {
1750     /* if no source link function, we need to call the sink link
1751      * function ourselves. */
1752     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
1753   } else {
1754     result = GST_PAD_LINK_OK;
1755   }
1756
1757   GST_OBJECT_LOCK (srcpad);
1758   GST_OBJECT_LOCK (sinkpad);
1759
1760   if (result == GST_PAD_LINK_OK) {
1761     GST_OBJECT_UNLOCK (sinkpad);
1762     GST_OBJECT_UNLOCK (srcpad);
1763
1764     /* fire off a signal to each of the pads telling them
1765      * that they've been linked */
1766     g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
1767     g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
1768
1769     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
1770         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1771   } else {
1772     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
1773         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1774
1775     GST_PAD_PEER (srcpad) = NULL;
1776     GST_PAD_PEER (sinkpad) = NULL;
1777
1778     GST_OBJECT_UNLOCK (sinkpad);
1779     GST_OBJECT_UNLOCK (srcpad);
1780   }
1781   return result;
1782
1783 prepare_failed:
1784   {
1785     return result;
1786   }
1787 }
1788
1789 static void
1790 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
1791 {
1792   GstPadTemplate **template_p;
1793
1794   /* this function would need checks if it weren't static */
1795
1796   GST_OBJECT_LOCK (pad);
1797   template_p = &pad->padtemplate;
1798   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
1799   GST_OBJECT_UNLOCK (pad);
1800
1801   if (templ)
1802     gst_pad_template_pad_created (templ, pad);
1803 }
1804
1805 /**
1806  * gst_pad_get_pad_template:
1807  * @pad: a #GstPad.
1808  *
1809  * Gets the template for @pad.
1810  *
1811  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
1812  * if this pad has no template.
1813  *
1814  * FIXME: currently returns an unrefcounted padtemplate.
1815  */
1816 GstPadTemplate *
1817 gst_pad_get_pad_template (GstPad * pad)
1818 {
1819   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1820
1821   return GST_PAD_PAD_TEMPLATE (pad);
1822 }
1823
1824
1825 /* should be called with the pad LOCK held */
1826 /* refs the caps, so caller is responsible for getting it unreffed */
1827 static GstCaps *
1828 gst_pad_get_caps_unlocked (GstPad * pad)
1829 {
1830   GstCaps *result = NULL;
1831
1832   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1833       GST_DEBUG_PAD_NAME (pad), pad);
1834
1835   if (GST_PAD_GETCAPSFUNC (pad)) {
1836     GST_CAT_DEBUG (GST_CAT_CAPS, "dispatching to pad getcaps function");
1837
1838     GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
1839     GST_OBJECT_UNLOCK (pad);
1840     result = GST_PAD_GETCAPSFUNC (pad) (pad);
1841     GST_OBJECT_LOCK (pad);
1842     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
1843
1844     if (result == NULL) {
1845       g_critical ("pad %s:%s returned NULL caps from getcaps function",
1846           GST_DEBUG_PAD_NAME (pad));
1847     } else {
1848       GST_CAT_DEBUG (GST_CAT_CAPS,
1849           "pad getcaps %s:%s returned %" GST_PTR_FORMAT,
1850           GST_DEBUG_PAD_NAME (pad), result);
1851 #ifndef G_DISABLE_ASSERT
1852       /* check that the returned caps are a real subset of the template caps */
1853       if (GST_PAD_PAD_TEMPLATE (pad)) {
1854         const GstCaps *templ_caps =
1855             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
1856         if (!gst_caps_is_subset (result, templ_caps)) {
1857           GstCaps *temp;
1858
1859           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
1860               "pad returned caps %" GST_PTR_FORMAT
1861               " which are not a real subset of its template caps %"
1862               GST_PTR_FORMAT, result, templ_caps);
1863           g_warning
1864               ("pad %s:%s returned caps that are not a real subset of its template caps",
1865               GST_DEBUG_PAD_NAME (pad));
1866           temp = gst_caps_intersect (templ_caps, result);
1867           gst_caps_unref (result);
1868           result = temp;
1869         }
1870       }
1871 #endif
1872       goto done;
1873     }
1874   }
1875   if (GST_PAD_PAD_TEMPLATE (pad)) {
1876     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1877
1878     result = GST_PAD_TEMPLATE_CAPS (templ);
1879     GST_CAT_DEBUG (GST_CAT_CAPS,
1880         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1881         result);
1882
1883     result = gst_caps_ref (result);
1884     goto done;
1885   }
1886   if (GST_PAD_CAPS (pad)) {
1887     result = GST_PAD_CAPS (pad);
1888
1889     GST_CAT_DEBUG (GST_CAT_CAPS,
1890         "using pad caps %p %" GST_PTR_FORMAT, result, result);
1891
1892     result = gst_caps_ref (result);
1893     goto done;
1894   }
1895
1896   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1897   result = gst_caps_new_empty ();
1898
1899 done:
1900   return result;
1901 }
1902
1903 /**
1904  * gst_pad_get_caps:
1905  * @pad: a  #GstPad to get the capabilities of.
1906  *
1907  * Gets the capabilities this pad can produce or consume.
1908  * Note that this method doesn't necessarily return the caps set by
1909  * gst_pad_set_caps() - use #GST_PAD_CAPS for that instead.
1910  * gst_pad_get_caps returns all possible caps a pad can operate with, using
1911  * the pad's get_caps function;
1912  * this returns the pad template caps if not explicitly set.
1913  *
1914  * Returns: a newly allocated copy of the #GstCaps of this pad.
1915  *
1916  * MT safe.
1917  */
1918 GstCaps *
1919 gst_pad_get_caps (GstPad * pad)
1920 {
1921   GstCaps *result = NULL;
1922
1923   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1924
1925   GST_OBJECT_LOCK (pad);
1926
1927   GST_CAT_DEBUG (GST_CAT_CAPS, "get pad caps of %s:%s (%p)",
1928       GST_DEBUG_PAD_NAME (pad), pad);
1929
1930   result = gst_pad_get_caps_unlocked (pad);
1931   GST_OBJECT_UNLOCK (pad);
1932
1933   return result;
1934 }
1935
1936 /**
1937  * gst_pad_peer_get_caps:
1938  * @pad: a  #GstPad to get the peer capabilities of.
1939  *
1940  * Gets the capabilities of the peer connected to this pad.
1941  *
1942  * Returns: the #GstCaps of the peer pad. This function returns a new caps, so use
1943  * gst_caps_unref to get rid of it. this function returns NULL if there is no
1944  * peer pad.
1945  */
1946 GstCaps *
1947 gst_pad_peer_get_caps (GstPad * pad)
1948 {
1949   GstPad *peerpad;
1950   GstCaps *result = NULL;
1951
1952   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1953
1954   GST_OBJECT_LOCK (pad);
1955
1956   GST_CAT_DEBUG (GST_CAT_CAPS, "get peer caps of %s:%s (%p)",
1957       GST_DEBUG_PAD_NAME (pad), pad);
1958
1959   peerpad = GST_PAD_PEER (pad);
1960   if (G_UNLIKELY (peerpad == NULL))
1961     goto no_peer;
1962
1963   gst_object_ref (peerpad);
1964   GST_OBJECT_UNLOCK (pad);
1965
1966   result = gst_pad_get_caps (peerpad);
1967
1968   gst_object_unref (peerpad);
1969
1970   return result;
1971
1972 no_peer:
1973   {
1974     GST_OBJECT_UNLOCK (pad);
1975     return NULL;
1976   }
1977 }
1978
1979 static gboolean
1980 fixate_value (GValue * dest, const GValue * src)
1981 {
1982   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
1983     g_value_init (dest, G_TYPE_INT);
1984     g_value_set_int (dest, gst_value_get_int_range_min (src));
1985   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
1986     g_value_init (dest, G_TYPE_DOUBLE);
1987     g_value_set_double (dest, gst_value_get_double_range_min (src));
1988   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
1989     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
1990   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1991     GValue temp = { 0 };
1992
1993     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
1994     if (!fixate_value (dest, &temp))
1995       gst_value_init_and_copy (dest, &temp);
1996     g_value_unset (&temp);
1997   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
1998     gboolean res = FALSE;
1999     guint n;
2000
2001     g_value_init (dest, GST_TYPE_ARRAY);
2002     for (n = 0; n < gst_value_array_get_size (src); n++) {
2003       GValue kid = { 0 };
2004       const GValue *orig_kid = gst_value_array_get_value (src, n);
2005
2006       if (!fixate_value (&kid, orig_kid))
2007         gst_value_init_and_copy (&kid, orig_kid);
2008       else
2009         res = TRUE;
2010       gst_value_array_append_value (dest, &kid);
2011       g_value_unset (&kid);
2012     }
2013
2014     if (!res)
2015       g_value_unset (dest);
2016
2017     return res;
2018   } else {
2019     return FALSE;
2020   }
2021
2022   return TRUE;
2023 }
2024
2025 static gboolean
2026 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2027 {
2028   GstStructure *s = data;
2029   GValue v = { 0 };
2030
2031   if (fixate_value (&v, value)) {
2032     gst_structure_id_set_value (s, field_id, &v);
2033     g_value_unset (&v);
2034   }
2035
2036   return TRUE;
2037 }
2038
2039 /**
2040  * gst_pad_fixate_caps:
2041  * @pad: a  #GstPad to fixate
2042  * @caps: the  #GstCaps to fixate
2043  *
2044  * Fixate a caps on the given pad. Modifies the caps in place, so you should
2045  * make sure that the caps are actually writable (see gst_caps_make_writable()).
2046  */
2047 void
2048 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2049 {
2050   GstPadFixateCapsFunction fixatefunc;
2051   guint n;
2052
2053   g_return_if_fail (GST_IS_PAD (pad));
2054   g_return_if_fail (caps != NULL);
2055
2056   if (gst_caps_is_fixed (caps))
2057     return;
2058
2059   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2060   if (fixatefunc) {
2061     fixatefunc (pad, caps);
2062   }
2063
2064   /* default fixation */
2065   for (n = 0; n < gst_caps_get_size (caps); n++) {
2066     GstStructure *s = gst_caps_get_structure (caps, n);
2067
2068     gst_structure_foreach (s, gst_pad_default_fixate, s);
2069   }
2070 }
2071
2072 /* Default accept caps implementation just checks against 
2073  * against the allowed caps for the pad */
2074 static gboolean
2075 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2076 {
2077   /* get the caps and see if it intersects to something
2078    * not empty */
2079   GstCaps *intersect;
2080   GstCaps *allowed;
2081   gboolean result = FALSE;
2082
2083   allowed = gst_pad_get_caps (pad);
2084   if (allowed) {
2085     intersect = gst_caps_intersect (allowed, caps);
2086
2087     result = !gst_caps_is_empty (intersect);
2088
2089     gst_caps_unref (allowed);
2090     gst_caps_unref (intersect);
2091   }
2092
2093   return result;
2094 }
2095
2096 /**
2097  * gst_pad_accept_caps:
2098  * @pad: a #GstPad to check
2099  * @caps: a #GstCaps to check on the pad
2100  *
2101  * Check if the given pad accepts the caps.
2102  *
2103  * Returns: TRUE if the pad can accept the caps.
2104  */
2105 gboolean
2106 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2107 {
2108   gboolean result;
2109   GstPadAcceptCapsFunction acceptfunc;
2110   GstCaps *existing = NULL;
2111
2112   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2113
2114   /* any pad can be unnegotiated */
2115   if (caps == NULL)
2116     return TRUE;
2117
2118   GST_OBJECT_LOCK (pad);
2119   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2120   if (GST_PAD_CAPS (pad) != NULL)
2121     existing = gst_caps_ref (GST_PAD_CAPS (pad));
2122
2123   GST_CAT_DEBUG (GST_CAT_CAPS, "pad accept caps of %s:%s (%p)",
2124       GST_DEBUG_PAD_NAME (pad), pad);
2125   GST_OBJECT_UNLOCK (pad);
2126
2127   /* The current caps on a pad are trivially acceptable */
2128   if (existing) {
2129     if (caps == existing || gst_caps_is_equal (caps, existing))
2130       goto is_same_caps;
2131     gst_caps_unref (existing);
2132   }
2133
2134   if (G_LIKELY (acceptfunc)) {
2135     /* we can call the function */
2136     result = acceptfunc (pad, caps);
2137   } else {
2138     /* Only null if the element explicitly unset it */
2139     result = gst_pad_acceptcaps_default (pad, caps);
2140   }
2141   return result;
2142
2143 is_same_caps:
2144   gst_caps_unref (existing);
2145   return TRUE;
2146 }
2147
2148 /**
2149  * gst_pad_peer_accept_caps:
2150  * @pad: a  #GstPad to check
2151  * @caps: a #GstCaps to check on the pad
2152  *
2153  * Check if the given pad accepts the caps.
2154  *
2155  * Returns: TRUE if the pad can accept the caps.
2156  */
2157 gboolean
2158 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2159 {
2160   GstPad *peerpad;
2161   gboolean result;
2162
2163   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2164
2165   GST_OBJECT_LOCK (pad);
2166
2167   GST_CAT_DEBUG (GST_CAT_CAPS, "peer accept caps of %s:%s (%p)",
2168       GST_DEBUG_PAD_NAME (pad), pad);
2169
2170   peerpad = GST_PAD_PEER (pad);
2171   if (G_UNLIKELY (peerpad == NULL))
2172     goto no_peer;
2173
2174   result = gst_pad_accept_caps (peerpad, caps);
2175   GST_OBJECT_UNLOCK (pad);
2176
2177   return result;
2178
2179 no_peer:
2180   {
2181     GST_OBJECT_UNLOCK (pad);
2182     return TRUE;
2183   }
2184 }
2185
2186 /**
2187  * gst_pad_set_caps:
2188  * @pad: a  #GstPad to set the capabilities of.
2189  * @caps: a #GstCaps to set.
2190  *
2191  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2192  * caps on the pad will be unreffed. This function refs the caps so you should
2193  * unref if as soon as you don't need it anymore.
2194  * It is possible to set NULL caps, which will make the pad unnegotiated
2195  * again.
2196  *
2197  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2198  * or bad parameters were provided to this function.
2199  *
2200  * MT safe.
2201  */
2202 gboolean
2203 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2204 {
2205   GstPadSetCapsFunction setcaps;
2206   GstCaps *existing;
2207
2208   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2209   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2210
2211   GST_OBJECT_LOCK (pad);
2212   setcaps = GST_PAD_SETCAPSFUNC (pad);
2213
2214   existing = GST_PAD_CAPS (pad);
2215   if (gst_caps_is_equal (caps, existing))
2216     goto setting_same_caps;
2217
2218   /* call setcaps function to configure the pad only if the
2219    * caps is not NULL */
2220   if (setcaps != NULL && caps) {
2221     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2222       GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2223       GST_OBJECT_UNLOCK (pad);
2224       if (!setcaps (pad, caps))
2225         goto could_not_set;
2226       GST_OBJECT_LOCK (pad);
2227       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2228     } else {
2229       GST_CAT_DEBUG (GST_CAT_CAPS, "pad %s:%s was dispatching",
2230           GST_DEBUG_PAD_NAME (pad));
2231     }
2232   }
2233
2234   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2235   GST_CAT_DEBUG (GST_CAT_CAPS, "%s:%s caps %" GST_PTR_FORMAT,
2236       GST_DEBUG_PAD_NAME (pad), caps);
2237   GST_OBJECT_UNLOCK (pad);
2238
2239   g_object_notify (G_OBJECT (pad), "caps");
2240
2241   return TRUE;
2242
2243 setting_same_caps:
2244   {
2245     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2246     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2247         "caps %" GST_PTR_FORMAT " same as existing, updating ptr only", caps);
2248     GST_OBJECT_UNLOCK (pad);
2249
2250     return TRUE;
2251   }
2252 /* errors */
2253 could_not_set:
2254   {
2255     GST_OBJECT_LOCK (pad);
2256     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2257     GST_CAT_DEBUG (GST_CAT_CAPS,
2258         "pad %s:%s, caps %" GST_PTR_FORMAT " could not be set",
2259         GST_DEBUG_PAD_NAME (pad), caps);
2260     GST_OBJECT_UNLOCK (pad);
2261
2262     return FALSE;
2263   }
2264 }
2265
2266 static gboolean
2267 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2268 {
2269   GstPadAcceptCapsFunction acceptcaps;
2270   GstPadSetCapsFunction setcaps;
2271   gboolean res;
2272
2273   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2274   setcaps = GST_PAD_SETCAPSFUNC (pad);
2275
2276   /* See if pad accepts the caps - only needed if 
2277    * no setcaps function */
2278   if (setcaps == NULL && acceptcaps != NULL)
2279     if (!acceptcaps (pad, caps))
2280       goto not_accepted;
2281
2282   /* set caps on pad if call succeeds */
2283   res = gst_pad_set_caps (pad, caps);
2284   /* no need to unref the caps here, set_caps takes a ref and
2285    * our ref goes away when we leave this function. */
2286
2287   return res;
2288
2289 not_accepted:
2290   {
2291     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2292     return FALSE;
2293   }
2294 }
2295
2296 /* returns TRUE if the src pad could be configured to accept the given caps */
2297 static gboolean
2298 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2299 {
2300   GstPadAcceptCapsFunction acceptcaps;
2301   GstPadSetCapsFunction setcaps;
2302   gboolean res;
2303
2304   acceptcaps = GST_PAD_ACCEPTCAPSFUNC (pad);
2305   setcaps = GST_PAD_SETCAPSFUNC (pad);
2306
2307   /* See if pad accepts the caps - only needed if 
2308    * no setcaps function */
2309   if (setcaps == NULL && acceptcaps != NULL)
2310     if (!acceptcaps (pad, caps))
2311       goto not_accepted;
2312
2313   if (dosetcaps)
2314     res = gst_pad_set_caps (pad, caps);
2315   else
2316     res = TRUE;
2317
2318   return res;
2319
2320 not_accepted:
2321   {
2322     GST_CAT_DEBUG (GST_CAT_CAPS, "caps %" GST_PTR_FORMAT " not accepted", caps);
2323     return FALSE;
2324   }
2325 }
2326
2327 /**
2328  * gst_pad_get_pad_template_caps:
2329  * @pad: a #GstPad to get the template capabilities from.
2330  *
2331  * Gets the capabilities for @pad's template.
2332  *
2333  * Returns: the #GstCaps of this pad template. If you intend to keep a reference
2334  * on the caps, make a copy (see gst_caps_copy ()).
2335  */
2336 const GstCaps *
2337 gst_pad_get_pad_template_caps (GstPad * pad)
2338 {
2339   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2340
2341   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2342
2343   if (GST_PAD_PAD_TEMPLATE (pad))
2344     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2345
2346   return gst_static_caps_get (&anycaps);
2347 }
2348
2349
2350 /**
2351  * gst_pad_get_peer:
2352  * @pad: a #GstPad to get the peer of.
2353  *
2354  * Gets the peer of @pad. This function refs the peer pad so
2355  * you need to unref it after use.
2356  *
2357  * Returns: the peer #GstPad. Unref after usage.
2358  *
2359  * MT safe.
2360  */
2361 GstPad *
2362 gst_pad_get_peer (GstPad * pad)
2363 {
2364   GstPad *result;
2365
2366   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2367
2368   GST_OBJECT_LOCK (pad);
2369   result = GST_PAD_PEER (pad);
2370   if (result)
2371     gst_object_ref (result);
2372   GST_OBJECT_UNLOCK (pad);
2373
2374   return result;
2375 }
2376
2377 /**
2378  * gst_pad_get_allowed_caps:
2379  * @srcpad: a #GstPad, it must a a source pad.
2380  *
2381  * Gets the capabilities of the allowed media types that can flow through
2382  * @srcpad and its peer. The pad must be a source pad.
2383  * The caller must free the resulting caps.
2384  *
2385  * Returns: the allowed #GstCaps of the pad link.  Free the caps when
2386  * you no longer need it. This function returns NULL when the @srcpad has no
2387  * peer.
2388  *
2389  * MT safe.
2390  */
2391 GstCaps *
2392 gst_pad_get_allowed_caps (GstPad * srcpad)
2393 {
2394   GstCaps *mycaps;
2395   GstCaps *caps;
2396   GstCaps *peercaps;
2397   GstPad *peer;
2398
2399   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
2400   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
2401
2402   GST_OBJECT_LOCK (srcpad);
2403
2404   peer = GST_PAD_PEER (srcpad);
2405   if (G_UNLIKELY (peer == NULL))
2406     goto no_peer;
2407
2408   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting allowed caps",
2409       GST_DEBUG_PAD_NAME (srcpad));
2410
2411   gst_object_ref (peer);
2412   GST_OBJECT_UNLOCK (srcpad);
2413   mycaps = gst_pad_get_caps (srcpad);
2414
2415   peercaps = gst_pad_get_caps (peer);
2416   gst_object_unref (peer);
2417
2418   caps = gst_caps_intersect (mycaps, peercaps);
2419   gst_caps_unref (peercaps);
2420   gst_caps_unref (mycaps);
2421
2422   GST_CAT_DEBUG (GST_CAT_CAPS, "allowed caps %" GST_PTR_FORMAT, caps);
2423
2424   return caps;
2425
2426 no_peer:
2427   {
2428     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2429         GST_DEBUG_PAD_NAME (srcpad));
2430     GST_OBJECT_UNLOCK (srcpad);
2431
2432     return NULL;
2433   }
2434 }
2435
2436 /**
2437  * gst_pad_get_negotiated_caps:
2438  * @pad: a #GstPad.
2439  *
2440  * Gets the capabilities of the media type that currently flows through @pad
2441  * and its peer.
2442  *
2443  * This function can be used on both src and sinkpads. Note that srcpads are
2444  * always negotiated before sinkpads so it is possible that the negotiated caps
2445  * on the srcpad do not match the negotiated caps of the peer.
2446  *
2447  * Returns: the negotiated #GstCaps of the pad link.  Free the caps when
2448  * you no longer need it. This function returns NULL when the @pad has no
2449  * peer or is not negotiated yet.
2450  *
2451  * MT safe.
2452  */
2453 GstCaps *
2454 gst_pad_get_negotiated_caps (GstPad * pad)
2455 {
2456   GstCaps *caps;
2457   GstPad *peer;
2458
2459   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2460
2461   GST_OBJECT_LOCK (pad);
2462
2463   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2464     goto no_peer;
2465
2466   GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: getting negotiated caps",
2467       GST_DEBUG_PAD_NAME (pad));
2468
2469   caps = GST_PAD_CAPS (pad);
2470   if (caps)
2471     gst_caps_ref (caps);
2472   GST_OBJECT_UNLOCK (pad);
2473
2474   GST_CAT_DEBUG (GST_CAT_CAPS, "negotiated caps %" GST_PTR_FORMAT, caps);
2475
2476   return caps;
2477
2478 no_peer:
2479   {
2480     GST_CAT_DEBUG (GST_CAT_PROPERTIES, "%s:%s: no peer",
2481         GST_DEBUG_PAD_NAME (pad));
2482     GST_OBJECT_UNLOCK (pad);
2483
2484     return NULL;
2485   }
2486 }
2487
2488 static GstFlowReturn
2489 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2490     GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
2491 {
2492   GstPad *peer;
2493   GstFlowReturn ret;
2494   GstPadBufferAllocFunction bufferallocfunc;
2495   gboolean caps_changed;
2496
2497   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2498   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2499   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2500
2501   GST_OBJECT_LOCK (pad);
2502   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2503     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
2504       goto flushed;
2505
2506   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2507     goto no_peer;
2508
2509   gst_object_ref (peer);
2510   GST_OBJECT_UNLOCK (pad);
2511
2512   if (G_LIKELY ((bufferallocfunc = peer->bufferallocfunc) == NULL))
2513     goto fallback;
2514
2515   GST_OBJECT_LOCK (peer);
2516   /* when the peer is flushing we cannot give a buffer */
2517   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (peer)))
2518     goto flushing;
2519
2520   if (offset == GST_BUFFER_OFFSET_NONE) {
2521     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2522         "calling bufferallocfunc &%s (@%p) of peer pad %s:%s for size %d offset NONE",
2523         GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2524         &bufferallocfunc, GST_DEBUG_PAD_NAME (peer), size);
2525   } else {
2526     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2527         "calling bufferallocfunc &%s (@%p) of peer pad %s:%s for size %d offset %"
2528         G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2529         &bufferallocfunc, GST_DEBUG_PAD_NAME (peer), size, offset);
2530   }
2531   GST_OBJECT_UNLOCK (peer);
2532
2533   ret = bufferallocfunc (peer, offset, size, caps, buf);
2534
2535   if (G_UNLIKELY (ret != GST_FLOW_OK))
2536     goto peer_error;
2537   if (G_UNLIKELY (*buf == NULL))
2538     goto fallback;
2539
2540   /* If the buffer alloc function didn't set up the caps like it should,
2541    * do it for it */
2542   if (caps && (GST_BUFFER_CAPS (*buf) == NULL)) {
2543     GST_WARNING ("Buffer allocation function for pad % " GST_PTR_FORMAT
2544         " did not set up caps. Setting", peer);
2545
2546     gst_buffer_set_caps (*buf, caps);
2547   }
2548
2549 do_caps:
2550   gst_object_unref (peer);
2551
2552   /* FIXME, move capnego this into a base class? */
2553   caps = GST_BUFFER_CAPS (*buf);
2554   caps_changed = caps && caps != GST_PAD_CAPS (pad);
2555   /* we got a new datatype on the pad, see if it can handle it */
2556   if (G_UNLIKELY (caps_changed)) {
2557     GST_DEBUG_OBJECT (pad, "caps changed to %" GST_PTR_FORMAT, caps);
2558     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, setcaps)))
2559       goto not_negotiated;
2560   }
2561   return ret;
2562
2563 flushed:
2564   {
2565     GST_CAT_DEBUG (GST_CAT_PADS, "%s:%s pad block stopped by flush",
2566         GST_DEBUG_PAD_NAME (pad));
2567     GST_OBJECT_UNLOCK (pad);
2568     return ret;
2569   }
2570 no_peer:
2571   {
2572     /* pad has no peer */
2573     GST_CAT_DEBUG (GST_CAT_PADS,
2574         "%s:%s called bufferallocfunc but had no peer",
2575         GST_DEBUG_PAD_NAME (pad));
2576     GST_OBJECT_UNLOCK (pad);
2577     return GST_FLOW_NOT_LINKED;
2578   }
2579 flushing:
2580   {
2581     /* peer was flushing */
2582     GST_OBJECT_UNLOCK (peer);
2583     gst_object_unref (peer);
2584     GST_CAT_DEBUG (GST_CAT_PADS,
2585         "%s:%s called bufferallocfunc but peer was flushing",
2586         GST_DEBUG_PAD_NAME (pad));
2587     return GST_FLOW_WRONG_STATE;
2588   }
2589   /* fallback case, allocate a buffer of our own, add pad caps. */
2590 fallback:
2591   {
2592     GST_CAT_DEBUG (GST_CAT_PADS,
2593         "%s:%s fallback buffer alloc", GST_DEBUG_PAD_NAME (pad));
2594     *buf = gst_buffer_new_and_alloc (size);
2595     GST_BUFFER_OFFSET (*buf) = offset;
2596     gst_buffer_set_caps (*buf, caps);
2597
2598     ret = GST_FLOW_OK;
2599
2600     goto do_caps;
2601   }
2602 not_negotiated:
2603   {
2604     gst_buffer_unref (*buf);
2605     *buf = NULL;
2606     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2607         "alloc function returned unacceptable buffer");
2608     return GST_FLOW_NOT_NEGOTIATED;
2609   }
2610 peer_error:
2611   {
2612     gst_object_unref (peer);
2613     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2614         "alloc function returned error %s", gst_flow_get_name (ret));
2615     return ret;
2616   }
2617 }
2618
2619 /**
2620  * gst_pad_alloc_buffer:
2621  * @pad: a source #GstPad
2622  * @offset: the offset of the new buffer in the stream
2623  * @size: the size of the new buffer
2624  * @caps: the caps of the new buffer
2625  * @buf: a newly allocated buffer
2626  *
2627  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2628  * function only works if @pad is a source pad and has a peer.
2629  *
2630  * You need to check the caps of the buffer after performing this
2631  * function and renegotiate to the format if needed.
2632  *
2633  * A new, empty #GstBuffer will be put in the @buf argument.
2634  *
2635  * Returns: a result code indicating success of the operation. Any
2636  * result code other than GST_FLOW_OK is an error and @buf should
2637  * not be used.
2638  * An error can occur if the pad is not connected or when the downstream
2639  * peer elements cannot provide an acceptable buffer.
2640  *
2641  * MT safe.
2642  */
2643 GstFlowReturn
2644 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
2645     GstBuffer ** buf)
2646 {
2647   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
2648 }
2649
2650 /**
2651  * gst_pad_alloc_buffer_and_set_caps:
2652  * @pad: a source #GstPad
2653  * @offset: the offset of the new buffer in the stream
2654  * @size: the size of the new buffer
2655  * @caps: the caps of the new buffer
2656  * @buf: a newly allocated buffer
2657  *
2658  * In addition to the function gst_pad_alloc_buffer(), this function
2659  * automatically calls gst_pad_set_caps() when the caps of the
2660  * newly allocated buffer are different from the @pad caps.
2661  *
2662  * Returns: a result code indicating success of the operation. Any
2663  * result code other than GST_FLOW_OK is an error and @buf should
2664  * not be used.
2665  * An error can occur if the pad is not connected or when the downstream
2666  * peer elements cannot provide an acceptable buffer.
2667  *
2668  * MT safe.
2669  */
2670 GstFlowReturn
2671 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
2672     GstCaps * caps, GstBuffer ** buf)
2673 {
2674   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
2675 }
2676
2677 /**
2678  * gst_pad_get_internal_links_default:
2679  * @pad: the #GstPad to get the internal links of.
2680  *
2681  * Gets a list of pads to which the given pad is linked to
2682  * inside of the parent element.
2683  * This is the default handler, and thus returns a list of all of the
2684  * pads inside the parent element with opposite direction.
2685  * The caller must free this list after use.
2686  *
2687  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
2688  *
2689  * Not MT safe.
2690  */
2691 GList *
2692 gst_pad_get_internal_links_default (GstPad * pad)
2693 {
2694   GList *res = NULL;
2695   GstElement *parent;
2696   GList *parent_pads;
2697   GstPadDirection direction;
2698
2699   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2700
2701   direction = pad->direction;
2702
2703   parent = GST_PAD_PARENT (pad);
2704   if (!parent)
2705     return NULL;
2706
2707   parent_pads = parent->pads;
2708
2709   while (parent_pads) {
2710     GstPad *parent_pad = GST_PAD_CAST (parent_pads->data);
2711
2712     if (parent_pad->direction != direction) {
2713       res = g_list_prepend (res, parent_pad);
2714     }
2715
2716     parent_pads = g_list_next (parent_pads);
2717   }
2718
2719   return res;
2720 }
2721
2722 /**
2723  * gst_pad_get_internal_links:
2724  * @pad: the #GstPad to get the internal links of.
2725  *
2726  * Gets a list of pads to which the given pad is linked to
2727  * inside of the parent element.
2728  * The caller must free this list after use.
2729  *
2730  * Returns: a newly allocated #GList of pads.
2731  *
2732  * Not MT safe.
2733  */
2734 GList *
2735 gst_pad_get_internal_links (GstPad * pad)
2736 {
2737   GList *res = NULL;
2738
2739   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2740
2741   if (GST_PAD_INTLINKFUNC (pad))
2742     res = GST_PAD_INTLINKFUNC (pad) (pad);
2743
2744   return res;
2745 }
2746
2747
2748 static gboolean
2749 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2750 {
2751   GList *orig, *pads;
2752   gboolean result;
2753
2754   GST_INFO_OBJECT (pad, "Sending event %p to all internally linked pads",
2755       event);
2756
2757   result = (GST_PAD_DIRECTION (pad) == GST_PAD_SINK);
2758
2759   orig = pads = gst_pad_get_internal_links (pad);
2760
2761   while (pads) {
2762     GstPad *eventpad = GST_PAD_CAST (pads->data);
2763
2764     pads = g_list_next (pads);
2765
2766     if (GST_PAD_DIRECTION (eventpad) == GST_PAD_SRC) {
2767       /* for each pad we send to, we should ref the event; it's up
2768        * to downstream to unref again when handled. */
2769       GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
2770           event, GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (eventpad));
2771       gst_event_ref (event);
2772       gst_pad_push_event (eventpad, event);
2773     } else {
2774       /* we only send the event on one pad, multi-sinkpad elements
2775        * should implement a handler */
2776       GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
2777           event, GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (eventpad));
2778       result = gst_pad_push_event (eventpad, event);
2779       goto done;
2780     }
2781   }
2782   /* we handled the incoming event so we unref once */
2783   GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
2784   gst_event_unref (event);
2785
2786 done:
2787   g_list_free (orig);
2788
2789   return result;
2790 }
2791
2792 /**
2793  * gst_pad_event_default:
2794  * @pad: a #GstPad to call the default event handler on.
2795  * @event: the #GstEvent to handle.
2796  *
2797  * Invokes the default event handler for the given pad. End-of-stream and
2798  * discontinuity events are handled specially, and then the event is sent to all
2799  * pads internally linked to @pad. Note that if there are many possible sink
2800  * pads that are internally linked to @pad, only one will be sent an event.
2801  * Multi-sinkpad elements should implement custom event handlers.
2802  *
2803  * Returns: TRUE if the event was sent succesfully.
2804  */
2805 gboolean
2806 gst_pad_event_default (GstPad * pad, GstEvent * event)
2807 {
2808   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2809   g_return_val_if_fail (event != NULL, FALSE);
2810
2811   switch (GST_EVENT_TYPE (event)) {
2812     case GST_EVENT_EOS:
2813     {
2814       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2815       gst_pad_pause_task (pad);
2816     }
2817     default:
2818       break;
2819   }
2820
2821   return gst_pad_event_default_dispatch (pad, event);
2822 }
2823
2824 /**
2825  * gst_pad_dispatcher:
2826  * @pad: a #GstPad to dispatch.
2827  * @dispatch: the #GstDispatcherFunction to call.
2828  * @data: gpointer user data passed to the dispatcher function.
2829  *
2830  * Invokes the given dispatcher function on all pads that are
2831  * internally linked to the given pad.
2832  * The GstPadDispatcherFunction should return TRUE when no further pads
2833  * need to be processed.
2834  *
2835  * Returns: TRUE if one of the dispatcher functions returned TRUE.
2836  */
2837 gboolean
2838 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
2839     gpointer data)
2840 {
2841   gboolean res = FALSE;
2842   GList *int_pads, *orig;
2843
2844   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2845   g_return_val_if_fail (dispatch != NULL, FALSE);
2846
2847   orig = int_pads = gst_pad_get_internal_links (pad);
2848
2849   while (int_pads) {
2850     GstPad *int_pad = GST_PAD_CAST (int_pads->data);
2851     GstPad *int_peer = GST_PAD_PEER (int_pad);
2852
2853     if (int_peer) {
2854       res = dispatch (int_peer, data);
2855       if (res)
2856         break;
2857     }
2858     int_pads = g_list_next (int_pads);
2859   }
2860
2861   g_list_free (orig);
2862
2863   return res;
2864 }
2865
2866 /**
2867  * gst_pad_query:
2868  * @pad: a #GstPad to invoke the default query on.
2869  * @query: the #GstQuery to perform.
2870  *
2871  * Dispatches a query to a pad. The query should have been allocated by the
2872  * caller via one of the type-specific allocation functions in gstquery.h. The
2873  * element is responsible for filling the query with an appropriate response,
2874  * which should then be parsed with a type-specific query parsing function.
2875  *
2876  * Again, the caller is responsible for both the allocation and deallocation of
2877  * the query structure.
2878  *
2879  * Returns: TRUE if the query could be performed.
2880  */
2881 gboolean
2882 gst_pad_query (GstPad * pad, GstQuery * query)
2883 {
2884   GstPadQueryFunction func;
2885
2886   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2887   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2888
2889   GST_DEBUG ("sending query %p to pad %s:%s", query, GST_DEBUG_PAD_NAME (pad));
2890
2891   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
2892     goto no_func;
2893
2894   return func (pad, query);
2895
2896 no_func:
2897   {
2898     GST_DEBUG ("pad had no query function");
2899     return FALSE;
2900   }
2901 }
2902
2903 /**
2904  * gst_pad_query_default:
2905  * @pad: a #GstPad to call the default query handler on.
2906  * @query: the #GstQuery to handle.
2907  *
2908  * Invokes the default query handler for the given pad. 
2909  * The query is sent to all pads internally linked to @pad. Note that 
2910  * if there are many possible sink pads that are internally linked to 
2911  * @pad, only one will be sent the query.
2912  * Multi-sinkpad elements should implement custom query handlers.
2913  *
2914  * Returns: TRUE if the query was performed succesfully.
2915  */
2916 gboolean
2917 gst_pad_query_default (GstPad * pad, GstQuery * query)
2918 {
2919   switch (GST_QUERY_TYPE (query)) {
2920     case GST_QUERY_POSITION:
2921     case GST_QUERY_SEEKING:
2922     case GST_QUERY_FORMATS:
2923     case GST_QUERY_LATENCY:
2924     case GST_QUERY_JITTER:
2925     case GST_QUERY_RATE:
2926     case GST_QUERY_CONVERT:
2927     default:
2928       return gst_pad_dispatcher
2929           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
2930   }
2931 }
2932
2933 #ifndef GST_DISABLE_LOADSAVE
2934 /* FIXME: why isn't this on a GstElement ? */
2935 /**
2936  * gst_pad_load_and_link:
2937  * @self: an #xmlNodePtr to read the description from.
2938  * @parent: the #GstObject element that owns the pad.
2939  *
2940  * Reads the pad definition from the XML node and links the given pad
2941  * in the element to a pad of an element up in the hierarchy.
2942  */
2943 void
2944 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
2945 {
2946   xmlNodePtr field = self->xmlChildrenNode;
2947   GstPad *pad = NULL, *targetpad;
2948   gchar *peer = NULL;
2949   gchar **split;
2950   GstElement *target;
2951   GstObject *grandparent;
2952   gchar *name = NULL;
2953
2954   while (field) {
2955     if (!strcmp ((char *) field->name, "name")) {
2956       name = (gchar *) xmlNodeGetContent (field);
2957       pad = gst_element_get_pad (GST_ELEMENT (parent), name);
2958       g_free (name);
2959     } else if (!strcmp ((char *) field->name, "peer")) {
2960       peer = (gchar *) xmlNodeGetContent (field);
2961     }
2962     field = field->next;
2963   }
2964   g_return_if_fail (pad != NULL);
2965
2966   if (peer == NULL)
2967     return;
2968
2969   split = g_strsplit (peer, ".", 2);
2970
2971   if (split[0] == NULL || split[1] == NULL) {
2972     GST_CAT_DEBUG (GST_CAT_XML,
2973         "Could not parse peer '%s' for pad %s:%s, leaving unlinked",
2974         peer, GST_DEBUG_PAD_NAME (pad));
2975
2976     g_free (peer);
2977     return;
2978   }
2979   g_free (peer);
2980
2981   g_return_if_fail (split[0] != NULL);
2982   g_return_if_fail (split[1] != NULL);
2983
2984   grandparent = gst_object_get_parent (parent);
2985
2986   if (grandparent && GST_IS_BIN (grandparent)) {
2987     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
2988   } else
2989     goto cleanup;
2990
2991   if (target == NULL)
2992     goto cleanup;
2993
2994   targetpad = gst_element_get_pad (target, split[1]);
2995
2996   if (targetpad == NULL)
2997     goto cleanup;
2998
2999   gst_pad_link (pad, targetpad);
3000
3001 cleanup:
3002   g_strfreev (split);
3003 }
3004
3005 /**
3006  * gst_pad_save_thyself:
3007  * @pad: a #GstPad to save.
3008  * @parent: the parent #xmlNodePtr to save the description in.
3009  *
3010  * Saves the pad into an xml representation.
3011  *
3012  * Returns: the #xmlNodePtr representation of the pad.
3013  */
3014 static xmlNodePtr
3015 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3016 {
3017   GstPad *pad;
3018   GstPad *peer;
3019
3020   g_return_val_if_fail (GST_IS_PAD (object), NULL);
3021
3022   pad = GST_PAD (object);
3023
3024   xmlNewChild (parent, NULL, (xmlChar *) "name",
3025       (xmlChar *) GST_PAD_NAME (pad));
3026   if (GST_PAD_PEER (pad) != NULL) {
3027     gchar *content;
3028
3029     peer = GST_PAD_PEER (pad);
3030     /* first check to see if the peer's parent's parent is the same */
3031     /* we just save it off */
3032     content = g_strdup_printf ("%s.%s",
3033         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3034     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3035     g_free (content);
3036   } else
3037     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3038
3039   return parent;
3040 }
3041
3042 #if 0
3043 /**
3044  * gst_ghost_pad_save_thyself:
3045  * @pad: a ghost #GstPad to save.
3046  * @parent: the parent #xmlNodePtr to save the description in.
3047  *
3048  * Saves the ghost pad into an xml representation.
3049  *
3050  * Returns: the #xmlNodePtr representation of the pad.
3051  */
3052 xmlNodePtr
3053 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3054 {
3055   xmlNodePtr self;
3056
3057   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3058
3059   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3060   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3061   xmlNewChild (self, NULL, (xmlChar *) "parent",
3062       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3063
3064   /* FIXME FIXME FIXME! */
3065
3066   return self;
3067 }
3068 #endif /* 0 */
3069 #endif /* GST_DISABLE_LOADSAVE */
3070
3071 /*
3072  * should be called with pad lock held
3073  *
3074  * MT safe.
3075  */
3076 static GstFlowReturn
3077 handle_pad_block (GstPad * pad)
3078 {
3079   GstPadBlockCallback callback;
3080   gpointer user_data;
3081   GstFlowReturn ret = GST_FLOW_OK;
3082
3083   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3084       "signal block taken on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3085
3086   /* need to grab extra ref for the callbacks */
3087   gst_object_ref (pad);
3088
3089   callback = pad->block_callback;
3090   if (callback) {
3091     user_data = pad->block_data;
3092     GST_OBJECT_UNLOCK (pad);
3093     callback (pad, TRUE, user_data);
3094     GST_OBJECT_LOCK (pad);
3095   } else {
3096     GST_PAD_BLOCK_SIGNAL (pad);
3097   }
3098
3099   while (GST_PAD_IS_BLOCKED (pad)) {
3100     if (GST_PAD_IS_FLUSHING (pad))
3101       goto flushing;
3102     GST_PAD_BLOCK_WAIT (pad);
3103     if (GST_PAD_IS_FLUSHING (pad))
3104       goto flushing;
3105   }
3106
3107   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3108
3109   callback = pad->block_callback;
3110   if (callback) {
3111     user_data = pad->block_data;
3112     GST_OBJECT_UNLOCK (pad);
3113     callback (pad, FALSE, user_data);
3114     GST_OBJECT_LOCK (pad);
3115   } else {
3116     GST_PAD_BLOCK_SIGNAL (pad);
3117   }
3118
3119   gst_object_unref (pad);
3120
3121   return ret;
3122
3123 flushing:
3124   {
3125     gst_object_unref (pad);
3126     return GST_FLOW_WRONG_STATE;
3127   }
3128 }
3129
3130 /**********************************************************************
3131  * Data passing functions
3132  */
3133
3134 static gboolean
3135 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3136 {
3137   GValue ret = { 0 };
3138   GValue args[2] = { {0}, {0} };
3139   gboolean res;
3140   GQuark detail;
3141
3142   /* init */
3143   g_value_init (&ret, G_TYPE_BOOLEAN);
3144   g_value_set_boolean (&ret, TRUE);
3145   g_value_init (&args[0], GST_TYPE_PAD);
3146   g_value_set_object (&args[0], pad);
3147   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);        // G_TYPE_POINTER);
3148   gst_value_set_mini_object (&args[1], obj);
3149
3150   if (GST_IS_EVENT (obj))
3151     detail = event_quark;
3152   else
3153     detail = buffer_quark;
3154
3155   /* actually emit */
3156   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3157   res = g_value_get_boolean (&ret);
3158
3159   /* clean up */
3160   g_value_unset (&ret);
3161   g_value_unset (&args[0]);
3162   g_value_unset (&args[1]);
3163
3164   return res;
3165 }
3166
3167 /**
3168  * gst_pad_chain:
3169  * @pad: a sink #GstPad.
3170  * @buffer: the #GstBuffer to send.
3171  *
3172  * Chain a buffer to @pad.
3173  *
3174  * Returns: a #GstFlowReturn from the pad.
3175  *
3176  * MT safe.
3177  */
3178 GstFlowReturn
3179 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3180 {
3181   GstCaps *caps;
3182   gboolean caps_changed;
3183   GstPadChainFunction chainfunc;
3184   GstFlowReturn ret;
3185   gboolean emit_signal;
3186
3187   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3188   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
3189       GST_FLOW_ERROR);
3190   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3191   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3192
3193   GST_PAD_STREAM_LOCK (pad);
3194
3195   GST_OBJECT_LOCK (pad);
3196   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3197     goto flushing;
3198
3199   caps = GST_BUFFER_CAPS (buffer);
3200   caps_changed = caps && caps != GST_PAD_CAPS (pad);
3201
3202   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3203   GST_OBJECT_UNLOCK (pad);
3204
3205   /* see if the signal should be emited, we emit before caps nego as
3206    * we might drop the buffer and do capsnego for nothing. */
3207   if (G_UNLIKELY (emit_signal)) {
3208     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer)))
3209       goto dropping;
3210   }
3211
3212   /* we got a new datatype on the pad, see if it can handle it */
3213   if (G_UNLIKELY (caps_changed)) {
3214     GST_DEBUG_OBJECT (pad, "caps changed to %" GST_PTR_FORMAT, caps);
3215     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
3216       goto not_negotiated;
3217   }
3218
3219   /* NOTE: we read the chainfunc unlocked.
3220    * we cannot hold the lock for the pad so we might send
3221    * the data to the wrong function. This is not really a
3222    * problem since functions are assigned at creation time
3223    * and don't change that often... */
3224   if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3225     goto no_function;
3226
3227   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3228       "calling chainfunction &%s of pad %s:%s",
3229       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad));
3230
3231   ret = chainfunc (pad, buffer);
3232
3233   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3234       "called chainfunction &%s of pad %s:%s, returned %s",
3235       GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_DEBUG_PAD_NAME (pad),
3236       gst_flow_get_name (ret));
3237
3238   GST_PAD_STREAM_UNLOCK (pad);
3239
3240   return ret;
3241
3242   /* ERRORS */
3243 flushing:
3244   {
3245     gst_buffer_unref (buffer);
3246     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3247         "pushing, but pad was flushing");
3248     GST_OBJECT_UNLOCK (pad);
3249     GST_PAD_STREAM_UNLOCK (pad);
3250     return GST_FLOW_WRONG_STATE;
3251   }
3252 dropping:
3253   {
3254     gst_buffer_unref (buffer);
3255     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3256     GST_PAD_STREAM_UNLOCK (pad);
3257     return GST_FLOW_OK;
3258   }
3259 not_negotiated:
3260   {
3261     gst_buffer_unref (buffer);
3262     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3263         "pushing buffer but pad did not accept");
3264     GST_PAD_STREAM_UNLOCK (pad);
3265     return GST_FLOW_NOT_NEGOTIATED;
3266   }
3267 no_function:
3268   {
3269     gst_buffer_unref (buffer);
3270     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3271         "pushing, but not chainhandler");
3272     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3273         ("push on pad %s:%s but it has no chainfunction",
3274             GST_DEBUG_PAD_NAME (pad)));
3275     GST_PAD_STREAM_UNLOCK (pad);
3276     return GST_FLOW_ERROR;
3277   }
3278 }
3279
3280 /**
3281  * gst_pad_push:
3282  * @pad: a source #GstPad.
3283  * @buffer: the #GstBuffer to push.
3284  *
3285  * Pushes a buffer to the peer of @pad.
3286  * buffer probes will be triggered before the buffer gets pushed.
3287  *
3288  * Returns: a #GstFlowReturn from the peer pad.
3289  *
3290  * MT safe.
3291  */
3292 GstFlowReturn
3293 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3294 {
3295   GstPad *peer;
3296   GstFlowReturn ret;
3297
3298 #if 0
3299   GstCaps *caps;
3300   gboolean caps_changed;
3301 #endif
3302
3303   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3304   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3305   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3306   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3307
3308   GST_OBJECT_LOCK (pad);
3309
3310   /* FIXME: this check can go away; pad_set_blocked could be implemented with
3311    * probes completely */
3312   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3313     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3314       goto flushed;
3315
3316   /* we emit signals on the pad arg, the peer will have a chance to
3317    * emit in the _chain() function */
3318   if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
3319     /* unlock before emitting */
3320     GST_OBJECT_UNLOCK (pad);
3321
3322     /* if the signal handler returned FALSE, it means we should just drop the
3323      * buffer */
3324     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (buffer)))
3325       goto dropped;
3326
3327     GST_OBJECT_LOCK (pad);
3328   }
3329
3330   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3331     goto not_linked;
3332   gst_object_ref (peer);
3333
3334 #if 0
3335   /* FIXME, disabled for 0.10.5 release because it caused to much
3336    * regressions */
3337   /* Before pushing the buffer to the peer pad, ensure that caps 
3338    * are set on this pad */
3339   caps = GST_BUFFER_CAPS (buffer);
3340   caps_changed = caps && caps != GST_PAD_CAPS (pad);
3341 #endif
3342
3343   GST_OBJECT_UNLOCK (pad);
3344
3345 #if 0
3346   /* we got a new datatype from the pad, it had better handle it */
3347   if (G_UNLIKELY (caps_changed)) {
3348     GST_DEBUG_OBJECT (pad, "caps changed to %" GST_PTR_FORMAT, caps);
3349     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
3350       goto not_negotiated;
3351   }
3352 #endif
3353
3354   ret = gst_pad_chain (peer, buffer);
3355
3356   gst_object_unref (peer);
3357
3358   return ret;
3359
3360   /* ERROR recovery here */
3361 flushed:
3362   {
3363     gst_buffer_unref (buffer);
3364     GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
3365     GST_OBJECT_UNLOCK (pad);
3366     return ret;
3367   }
3368 dropped:
3369   {
3370     gst_buffer_unref (buffer);
3371     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3372     return GST_FLOW_OK;
3373   }
3374 not_linked:
3375   {
3376     gst_buffer_unref (buffer);
3377     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3378         "pushing, but it was not linked");
3379     GST_OBJECT_UNLOCK (pad);
3380     return GST_FLOW_NOT_LINKED;
3381   }
3382 #if 0
3383 not_negotiated:
3384   {
3385     gst_buffer_unref (buffer);
3386     gst_object_unref (peer);
3387     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
3388         "element pushed buffer then refused to accept the caps");
3389     return GST_FLOW_NOT_NEGOTIATED;
3390   }
3391 #endif
3392 }
3393
3394 /**
3395  * gst_pad_check_pull_range:
3396  * @pad: a sink #GstPad.
3397  *
3398  * Checks if a gst_pad_pull_range() can be performed on the peer
3399  * source pad. This function is used by plugins that want to check
3400  * if they can use random access on the peer source pad.
3401  *
3402  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
3403  * if it needs to perform some logic to determine if pull_range is
3404  * possible.
3405  *
3406  * Returns: a gboolean with the result.
3407  *
3408  * MT safe.
3409  */
3410 gboolean
3411 gst_pad_check_pull_range (GstPad * pad)
3412 {
3413   GstPad *peer;
3414   gboolean ret;
3415   GstPadCheckGetRangeFunction checkgetrangefunc;
3416
3417   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3418
3419   GST_OBJECT_LOCK (pad);
3420   if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
3421     goto wrong_direction;
3422
3423   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3424     goto not_connected;
3425
3426   gst_object_ref (peer);
3427   GST_OBJECT_UNLOCK (pad);
3428
3429   /* see note in above function */
3430   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
3431     /* FIXME, kindoff ghetto */
3432     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
3433   } else {
3434     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3435         "calling checkgetrangefunc %s of peer pad %s:%s",
3436         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
3437
3438     ret = checkgetrangefunc (peer);
3439   }
3440
3441   gst_object_unref (peer);
3442
3443   return ret;
3444
3445   /* ERROR recovery here */
3446 wrong_direction:
3447   {
3448     GST_OBJECT_UNLOCK (pad);
3449     return FALSE;
3450   }
3451 not_connected:
3452   {
3453     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3454         "checking pull range, but it was not linked");
3455     GST_OBJECT_UNLOCK (pad);
3456     return FALSE;
3457   }
3458 }
3459
3460 /**
3461  * gst_pad_get_range:
3462  * @pad: a src #GstPad.
3463  * @offset: The start offset of the buffer
3464  * @size: The length of the buffer
3465  * @buffer: a pointer to hold the #GstBuffer.
3466  *
3467  * Calls the getrange function of @pad.
3468  *
3469  * Returns: a #GstFlowReturn from the pad.
3470  *
3471  * MT safe.
3472  */
3473 GstFlowReturn
3474 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3475     GstBuffer ** buffer)
3476 {
3477   GstFlowReturn ret;
3478   GstPadGetRangeFunction getrangefunc;
3479   gboolean emit_signal;
3480
3481   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3482   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SRC, GST_FLOW_ERROR);
3483   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3484
3485   GST_PAD_STREAM_LOCK (pad);
3486
3487   GST_OBJECT_LOCK (pad);
3488   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3489     goto flushing;
3490
3491   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3492   GST_OBJECT_UNLOCK (pad);
3493
3494   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3495     goto no_function;
3496
3497   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3498       "calling getrangefunc %s of peer pad %s:%s, offset %"
3499       G_GUINT64_FORMAT ", size %u",
3500       GST_DEBUG_FUNCPTR_NAME (getrangefunc), GST_DEBUG_PAD_NAME (pad),
3501       offset, size);
3502
3503   ret = getrangefunc (pad, offset, size, buffer);
3504
3505   /* can only fire the signal if we have a valid buffer */
3506   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3507     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3508       goto dropping;
3509   }
3510
3511   GST_PAD_STREAM_UNLOCK (pad);
3512
3513   return ret;
3514
3515   /* ERRORS */
3516 flushing:
3517   {
3518     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3519         "pulling range, but pad was flushing");
3520     GST_OBJECT_UNLOCK (pad);
3521     GST_PAD_STREAM_UNLOCK (pad);
3522     return GST_FLOW_WRONG_STATE;
3523   }
3524 no_function:
3525   {
3526     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3527         ("pullrange on pad %s:%s but it has no getrangefunction",
3528             GST_DEBUG_PAD_NAME (pad)));
3529     GST_PAD_STREAM_UNLOCK (pad);
3530     return GST_FLOW_ERROR;
3531   }
3532 dropping:
3533   {
3534     GST_DEBUG ("Dropping data after FALSE probe return");
3535     GST_PAD_STREAM_UNLOCK (pad);
3536     gst_buffer_unref (*buffer);
3537     *buffer = NULL;
3538     return GST_FLOW_UNEXPECTED;
3539   }
3540 }
3541
3542
3543 /**
3544  * gst_pad_pull_range:
3545  * @pad: a sink #GstPad.
3546  * @offset: The start offset of the buffer
3547  * @size: The length of the buffer
3548  * @buffer: a pointer to hold the #GstBuffer.
3549  *
3550  * Pulls a buffer from the peer pad. @pad must be a linked
3551  * sinkpad.
3552  *
3553  * Returns: a #GstFlowReturn from the peer pad.
3554  *
3555  * MT safe.
3556  */
3557 GstFlowReturn
3558 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3559     GstBuffer ** buffer)
3560 {
3561   GstPad *peer;
3562   GstFlowReturn ret;
3563   gboolean emit_signal;
3564
3565   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3566   g_return_val_if_fail (GST_PAD_DIRECTION (pad) == GST_PAD_SINK,
3567       GST_FLOW_ERROR);
3568   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3569
3570   GST_OBJECT_LOCK (pad);
3571
3572   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3573     handle_pad_block (pad);
3574
3575   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3576     goto not_connected;
3577
3578   /* signal emision for the pad, peer has chance to emit when
3579    * we call _get_range() */
3580   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3581
3582   gst_object_ref (peer);
3583   GST_OBJECT_UNLOCK (pad);
3584
3585   ret = gst_pad_get_range (peer, offset, size, buffer);
3586
3587   gst_object_unref (peer);
3588
3589   /* can only fire the signal if we have a valid buffer */
3590   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
3591     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
3592       goto dropping;
3593   }
3594   return ret;
3595
3596   /* ERROR recovery here */
3597 not_connected:
3598   {
3599     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3600         "pulling range, but it was not linked");
3601     GST_OBJECT_UNLOCK (pad);
3602     return GST_FLOW_NOT_LINKED;
3603   }
3604 dropping:
3605   {
3606     GST_DEBUG ("Dropping data after FALSE probe return");
3607     gst_buffer_unref (*buffer);
3608     *buffer = NULL;
3609     return GST_FLOW_UNEXPECTED;
3610   }
3611 }
3612
3613 /**
3614  * gst_pad_push_event:
3615  * @pad: a #GstPad to push the event to.
3616  * @event: the #GstEvent to send to the pad.
3617  *
3618  * Sends the event to the peer of the given pad. This function is
3619  * mainly used by elements to send events to their peer
3620  * elements.
3621  *
3622  * This function takes owership of the provided event so you should
3623  * gst_event_ref() it if you want to reuse the event after this call.
3624  *
3625  * Returns: TRUE if the event was handled.
3626  *
3627  * MT safe.
3628  */
3629 gboolean
3630 gst_pad_push_event (GstPad * pad, GstEvent * event)
3631 {
3632   GstPad *peerpad;
3633   gboolean result;
3634
3635   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3636   g_return_val_if_fail (event != NULL, FALSE);
3637   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
3638
3639   GST_OBJECT_LOCK (pad);
3640   switch (GST_EVENT_TYPE (event)) {
3641     case GST_EVENT_FLUSH_START:
3642       GST_PAD_SET_FLUSHING (pad);
3643       break;
3644     case GST_EVENT_FLUSH_STOP:
3645       GST_PAD_UNSET_FLUSHING (pad);
3646       break;
3647     default:
3648       break;
3649   }
3650
3651   if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3652     if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
3653       GST_PAD_BLOCK_SIGNAL (pad);
3654     }
3655   }
3656
3657   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
3658     GST_OBJECT_UNLOCK (pad);
3659
3660     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
3661       goto dropping;
3662
3663     GST_OBJECT_LOCK (pad);
3664   }
3665   peerpad = GST_PAD_PEER (pad);
3666   if (peerpad == NULL)
3667     goto not_linked;
3668
3669   GST_LOG_OBJECT (peerpad, "sending event on peerpad");
3670   gst_object_ref (peerpad);
3671   GST_OBJECT_UNLOCK (pad);
3672
3673   result = gst_pad_send_event (peerpad, event);
3674
3675   gst_object_unref (peerpad);
3676   GST_LOG_OBJECT (peerpad, "sent event on peerpad");
3677
3678   return result;
3679
3680   /* ERROR handling */
3681 dropping:
3682   {
3683     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
3684     gst_event_unref (event);
3685     return FALSE;
3686   }
3687 not_linked:
3688   {
3689     gst_event_unref (event);
3690     GST_OBJECT_UNLOCK (pad);
3691     return FALSE;
3692   }
3693 }
3694
3695 /**
3696  * gst_pad_send_event:
3697  * @pad: a #GstPad to send the event to.
3698  * @event: the #GstEvent to send to the pad.
3699  *
3700  * Sends the event to the pad. This function can be used
3701  * by applications to send events in the pipeline.
3702  *
3703  * If @pad is a source pad, @event should be an upstream event. If @pad is a
3704  * sink pad, @event should be a downstream event. For example, you would not
3705  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
3706  * Furthermore, some downstream events have to be serialized with data flow,
3707  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
3708  * the event needs to be serialized with data flow, this function will take the
3709  * pad's stream lock while calling its event function.
3710  *
3711  * To find out whether an event type is upstream, downstream, or downstream and
3712  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
3713  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
3714  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or plugin
3715  * doesn't need to bother itself with this information; the core handles all
3716  * necessary locks and checks.
3717  *
3718  * This function takes owership of the provided event so you should
3719  * gst_event_ref() it if you want to reuse the event after this call.
3720  *
3721  * Returns: TRUE if the event was handled.
3722  */
3723 gboolean
3724 gst_pad_send_event (GstPad * pad, GstEvent * event)
3725 {
3726   gboolean result = FALSE;
3727   GstPadEventFunction eventfunc;
3728   gboolean serialized, need_unlock = FALSE;
3729
3730   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3731   g_return_val_if_fail (event != NULL, FALSE);
3732
3733   GST_OBJECT_LOCK (pad);
3734   if (GST_PAD_IS_SINK (pad)) {
3735     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
3736       goto wrong_direction;
3737     serialized = GST_EVENT_IS_SERIALIZED (event);
3738   } else if (GST_PAD_IS_SRC (pad)) {
3739     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
3740       goto wrong_direction;
3741     /* events on srcpad never are serialized */
3742     serialized = FALSE;
3743   } else
3744     goto unknown_direction;
3745
3746   if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
3747     GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
3748     GST_EVENT_SRC (event) = gst_object_ref (pad);
3749   }
3750
3751   /* pad signals */
3752   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
3753     GST_OBJECT_UNLOCK (pad);
3754
3755     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
3756       goto dropping;
3757
3758     GST_OBJECT_LOCK (pad);
3759   }
3760
3761   switch (GST_EVENT_TYPE (event)) {
3762     case GST_EVENT_FLUSH_START:
3763       GST_CAT_DEBUG (GST_CAT_EVENT,
3764           "have event type %d (FLUSH_START) on pad %s:%s",
3765           GST_EVENT_TYPE (event), GST_DEBUG_PAD_NAME (pad));
3766
3767       /* can't even accept a flush begin event when flushing */
3768       if (GST_PAD_IS_FLUSHING (pad))
3769         goto flushing;
3770       GST_PAD_SET_FLUSHING (pad);
3771       GST_CAT_DEBUG (GST_CAT_EVENT, "set flush flag");
3772       break;
3773     case GST_EVENT_FLUSH_STOP:
3774       GST_PAD_UNSET_FLUSHING (pad);
3775       GST_CAT_DEBUG (GST_CAT_EVENT, "cleared flush flag");
3776       GST_OBJECT_UNLOCK (pad);
3777       /* grab stream lock */
3778       GST_PAD_STREAM_LOCK (pad);
3779       need_unlock = TRUE;
3780       GST_OBJECT_LOCK (pad);
3781       break;
3782     default:
3783       GST_CAT_DEBUG (GST_CAT_EVENT, "have event type %s on pad %s:%s",
3784           GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad));
3785
3786       /* make this a little faster, no point in grabbing the lock
3787        * if the pad is allready flushing. */
3788       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3789         goto flushing;
3790
3791       if (serialized) {
3792         /* lock order: STREAM_LOCK, LOCK */
3793         GST_OBJECT_UNLOCK (pad);
3794         GST_PAD_STREAM_LOCK (pad);
3795         need_unlock = TRUE;
3796         GST_OBJECT_LOCK (pad);
3797         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3798           goto flushing;
3799       }
3800       break;
3801   }
3802   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
3803     goto no_function;
3804
3805   GST_OBJECT_UNLOCK (pad);
3806
3807   result = eventfunc (pad, event);
3808
3809   if (need_unlock)
3810     GST_PAD_STREAM_UNLOCK (pad);
3811
3812   return result;
3813
3814   /* ERROR handling */
3815 wrong_direction:
3816   {
3817     g_warning ("pad %s:%s sending %s event in wrong direction",
3818         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
3819     GST_OBJECT_UNLOCK (pad);
3820     gst_event_unref (event);
3821     return FALSE;
3822   }
3823 unknown_direction:
3824   {
3825     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3826     GST_OBJECT_UNLOCK (pad);
3827     gst_event_unref (event);
3828     return FALSE;
3829   }
3830 no_function:
3831   {
3832     g_warning ("pad %s:%s has no event handler, file a bug.",
3833         GST_DEBUG_PAD_NAME (pad));
3834     GST_OBJECT_UNLOCK (pad);
3835     gst_event_unref (event);
3836     return FALSE;
3837   }
3838 flushing:
3839   {
3840     GST_OBJECT_UNLOCK (pad);
3841     if (need_unlock)
3842       GST_PAD_STREAM_UNLOCK (pad);
3843     GST_CAT_INFO (GST_CAT_EVENT, "Received event on flushing pad. Discarding");
3844     gst_event_unref (event);
3845     return FALSE;
3846   }
3847 dropping:
3848   {
3849     GST_DEBUG ("Dropping event after FALSE probe return");
3850     gst_event_unref (event);
3851     return FALSE;
3852   }
3853 }
3854
3855 /**
3856  * gst_pad_set_element_private:
3857  * @pad: the #GstPad to set the private data of.
3858  * @priv: The private data to attach to the pad.
3859  *
3860  * Set the given private data gpointer on the pad.
3861  * This function can only be used by the element that owns the pad.
3862  * No locking is performed in this function.
3863  */
3864 void
3865 gst_pad_set_element_private (GstPad * pad, gpointer priv)
3866 {
3867   pad->element_private = priv;
3868 }
3869
3870 /**
3871  * gst_pad_get_element_private:
3872  * @pad: the #GstPad to get the private data of.
3873  *
3874  * Gets the private data of a pad.
3875  * No locking is performed in this function.
3876  *
3877  * Returns: a #gpointer to the private data.
3878  */
3879 gpointer
3880 gst_pad_get_element_private (GstPad * pad)
3881 {
3882   return pad->element_private;
3883 }
3884
3885 /**
3886  * gst_pad_start_task:
3887  * @pad: the #GstPad to start the task of
3888  * @func: the task function to call
3889  * @data: data passed to the task function
3890  *
3891  * Starts a task that repeadedly calls @func with @data. This function
3892  * is nostly used in the pad activation function to start the
3893  * dataflow. This function will automatically acquire the STREAM_LOCK of
3894  * the pad before calling @func.
3895  *
3896  * Returns: a TRUE if the task could be started.
3897  */
3898 gboolean
3899 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
3900 {
3901   GstTask *task;
3902
3903   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3904   g_return_val_if_fail (func != NULL, FALSE);
3905
3906   GST_OBJECT_LOCK (pad);
3907   task = GST_PAD_TASK (pad);
3908   if (task == NULL) {
3909     task = gst_task_create (func, data);
3910     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
3911     GST_PAD_TASK (pad) = task;
3912   }
3913   gst_task_start (task);
3914   GST_OBJECT_UNLOCK (pad);
3915
3916   return TRUE;
3917 }
3918
3919 /**
3920  * gst_pad_pause_task:
3921  * @pad: the #GstPad to pause the task of
3922  *
3923  * Pause the task of @pad. This function will also make sure that the
3924  * function executed by the task will effectively stop.
3925  *
3926  * Returns: a TRUE if the task could be paused or FALSE when the pad
3927  * has no task.
3928  */
3929 gboolean
3930 gst_pad_pause_task (GstPad * pad)
3931 {
3932   GstTask *task;
3933
3934   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3935
3936   GST_OBJECT_LOCK (pad);
3937   task = GST_PAD_TASK (pad);
3938   if (task == NULL)
3939     goto no_task;
3940   gst_task_pause (task);
3941   GST_OBJECT_UNLOCK (pad);
3942
3943   GST_PAD_STREAM_LOCK (pad);
3944   GST_PAD_STREAM_UNLOCK (pad);
3945
3946   return TRUE;
3947
3948 no_task:
3949   {
3950     GST_DEBUG_OBJECT (pad, "pad has no task");
3951     GST_OBJECT_UNLOCK (pad);
3952     return FALSE;
3953   }
3954 }
3955
3956 /**
3957  * gst_pad_stop_task:
3958  * @pad: the #GstPad to stop the task of
3959  *
3960  * Stop the task of @pad. This function will also make sure that the
3961  * function executed by the task will effectively stop if not called
3962  * from the GstTaskFunction.
3963  *
3964  * This function will deadlock if called from the GstTaskFunction of
3965  * the task. Use gst_task_pause() instead.
3966  *
3967  * Regardless of whether the pad has a task, the stream lock is acquired and
3968  * released so as to ensure that streaming through this pad has finished.
3969  *
3970  * Returns: a TRUE if the task could be stopped or FALSE on error.
3971  */
3972 gboolean
3973 gst_pad_stop_task (GstPad * pad)
3974 {
3975   GstTask *task;
3976
3977   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3978
3979   GST_OBJECT_LOCK (pad);
3980   task = GST_PAD_TASK (pad);
3981   if (task == NULL)
3982     goto no_task;
3983   GST_PAD_TASK (pad) = NULL;
3984   gst_task_stop (task);
3985   GST_OBJECT_UNLOCK (pad);
3986
3987   GST_PAD_STREAM_LOCK (pad);
3988   GST_PAD_STREAM_UNLOCK (pad);
3989
3990   gst_task_join (task);
3991
3992   gst_object_unref (task);
3993
3994   return TRUE;
3995
3996 no_task:
3997   {
3998     GST_OBJECT_UNLOCK (pad);
3999
4000     GST_PAD_STREAM_LOCK (pad);
4001     GST_PAD_STREAM_UNLOCK (pad);
4002
4003     return TRUE;
4004   }
4005 }