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