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