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