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