]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstpad.c
4dc3dc92599779e019b5a18271d0c2e0db232199
[glsdk/gstreamer0-10.git] / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpad.c: Pads for linking elements together
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:gstpad
24  * @short_description: Object contained by elements that allows links to
25  *                     other elements
26  * @see_also: #GstPadTemplate, #GstElement, #GstEvent
27  *
28  * A #GstElement is linked to other elements via "pads", which are extremely
29  * light-weight generic link points.
30  * After two pads are retrieved from an element with gst_element_get_pad(),
31  * the pads can be link with gst_pad_link(). (For quick links,
32  * you can also use gst_element_link(), which will make the obvious
33  * link for you if it's straightforward.)
34  *
35  * Pads are typically created from a #GstPadTemplate with
36  * gst_pad_new_from_template().
37  *
38  * Pads have #GstCaps attached to it to describe the media type they are
39  * capable of dealing with.  gst_pad_get_caps() and gst_pad_set_caps() are
40  * used to manipulate the caps of the pads.
41  * Pads created from a pad template cannot set capabilities that are
42  * incompatible with the pad template capabilities.
43  *
44  * Pads without pad templates can be created with gst_pad_new(),
45  * which takes a direction and a name as an argument.  If the name is NULL,
46  * then a guaranteed unique name will be assigned to it.
47  *
48  * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
49  *
50  * A #GstElement creating a pad will typically use the various
51  * gst_pad_set_*_function() calls to register callbacks for various events
52  * on the pads.
53  *
54  * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55  * or pull in a buffer.
56  *
57  * To send a #GstEvent on a pad, use gst_pad_send_event() and
58  * gst_pad_push_event().
59  *
60  * Last reviewed on 2006-07-06 (0.10.9)
61  */
63 #include "gst_private.h"
65 #include "gstpad.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
69 #include "gstutils.h"
70 #include "gstinfo.h"
71 #include "gsterror.h"
72 #include "gstvalue.h"
73 #include "glib-compat-private.h"
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
78 /* Pad signals and args */
79 enum
80 {
81   PAD_LINKED,
82   PAD_UNLINKED,
83   PAD_REQUEST_LINK,
84   PAD_HAVE_DATA,
85   /* FILL ME */
86   LAST_SIGNAL
87 };
89 enum
90 {
91   PAD_PROP_0,
92   PAD_PROP_CAPS,
93   PAD_PROP_DIRECTION,
94   PAD_PROP_TEMPLATE,
95   /* FILL ME */
96 };
98 #define GST_PAD_GET_PRIVATE(obj)  \
99    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
101 #define GST_PAD_CHAINLISTFUNC(pad) ((pad)->abidata.ABI.priv->chainlistfunc)
103 struct _GstPadPrivate
105   GstPadChainListFunction chainlistfunc;
106 };
108 static void gst_pad_dispose (GObject * object);
109 static void gst_pad_finalize (GObject * object);
110 static void gst_pad_set_property (GObject * object, guint prop_id,
111     const GValue * value, GParamSpec * pspec);
112 static void gst_pad_get_property (GObject * object, guint prop_id,
113     GValue * value, GParamSpec * pspec);
115 static GstFlowReturn handle_pad_block (GstPad * pad);
116 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
117 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
118 static gboolean gst_pad_activate_default (GstPad * pad);
119 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
121 #ifndef GST_DISABLE_LOADSAVE
122 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
123 #endif
125 /* Some deprecated stuff that we need inside here for
126  * backwards compatibility */
127 #ifdef GST_DISABLE_DEPRECATED
128 #ifndef GST_REMOVE_DEPRECATED
129 #define GST_PAD_INTLINKFUNC(pad)        (GST_PAD_CAST(pad)->intlinkfunc)
130 GList *gst_pad_get_internal_links_default (GstPad * pad);
131 #endif
132 #endif
134 static GstObjectClass *parent_class = NULL;
135 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
137 /* quarks for probe signals */
138 static GQuark buffer_quark;
139 static GQuark event_quark;
141 typedef struct
143   const gint ret;
144   const gchar *name;
145   GQuark quark;
146 } GstFlowQuarks;
148 static GstFlowQuarks flow_quarks[] = {
149   {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
150   {GST_FLOW_RESEND, "resend", 0},
151   {GST_FLOW_OK, "ok", 0},
152   {GST_FLOW_NOT_LINKED, "not-linked", 0},
153   {GST_FLOW_WRONG_STATE, "wrong-state", 0},
154   {GST_FLOW_UNEXPECTED, "unexpected", 0},
155   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
156   {GST_FLOW_ERROR, "error", 0},
157   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
158   {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
159 };
161 /**
162  * gst_flow_get_name:
163  * @ret: a #GstFlowReturn to get the name of.
164  *
165  * Gets a string representing the given flow return.
166  *
167  * Returns: a static string with the name of the flow return.
168  */
169 G_CONST_RETURN gchar *
170 gst_flow_get_name (GstFlowReturn ret)
172   gint i;
174   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
176   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
177     if (ret == flow_quarks[i].ret)
178       return flow_quarks[i].name;
179   }
180   return "unknown";
183 /**
184  * gst_flow_to_quark:
185  * @ret: a #GstFlowReturn to get the quark of.
186  *
187  * Get the unique quark for the given GstFlowReturn.
188  *
189  * Returns: the quark associated with the flow return or 0 if an
190  * invalid return was specified.
191  */
192 GQuark
193 gst_flow_to_quark (GstFlowReturn ret)
195   gint i;
197   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
199   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
200     if (ret == flow_quarks[i].ret)
201       return flow_quarks[i].quark;
202   }
203   return 0;
206 #define _do_init \
207 { \
208   gint i; \
209   \
210   buffer_quark = g_quark_from_static_string ("buffer"); \
211   event_quark = g_quark_from_static_string ("event"); \
212   \
213   for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {                    \
214     flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
215   } \
216   \
217   GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
218       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
221 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
223 static gboolean
224 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
225     GValue * return_accu, const GValue * handler_return, gpointer dummy)
227   gboolean ret = g_value_get_boolean (handler_return);
229   GST_DEBUG ("accumulated %d", ret);
230   g_value_set_boolean (return_accu, ret);
232   return ret;
235 static gboolean
236 default_have_data (GstPad * pad, GstMiniObject * o)
238   return TRUE;
241 static void
242 gst_pad_class_init (GstPadClass * klass)
244   GObjectClass *gobject_class;
245   GstObjectClass *gstobject_class;
247   gobject_class = G_OBJECT_CLASS (klass);
248   gstobject_class = GST_OBJECT_CLASS (klass);
250   g_type_class_add_private (klass, sizeof (GstPadPrivate));
252   parent_class = g_type_class_peek_parent (klass);
254   gobject_class->dispose = gst_pad_dispose;
255   gobject_class->finalize = gst_pad_finalize;
256   gobject_class->set_property = gst_pad_set_property;
257   gobject_class->get_property = gst_pad_get_property;
259   /**
260    * GstPad::linked:
261    * @pad: the pad that emitted the signal
262    * @peer: the peer pad that has been connected
263    *
264    * Signals that a pad has been linked to the peer pad.
265    */
266   gst_pad_signals[PAD_LINKED] =
267       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
268       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
269       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
270   /**
271    * GstPad::unlinked:
272    * @pad: the pad that emitted the signal
273    * @peer: the peer pad that has been disconnected
274    *
275    * Signals that a pad has been unlinked from the peer pad.
276    */
277   gst_pad_signals[PAD_UNLINKED] =
278       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
279       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
280       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
281   /**
282    * GstPad::request-link:
283    * @pad: the pad that emitted the signal
284    * @peer: the peer pad for which a connection is requested
285    *
286    * Signals that a pad connection has been requested.
287    */
288   gst_pad_signals[PAD_REQUEST_LINK] =
289       g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
290       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
291       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
293   /**
294    * GstPad::have-data:
295    * @pad: the pad that emitted the signal
296    * @mini_obj: new data
297    *
298    * Signals that new data is available on the pad. This signal is used
299    * internally for implementing pad probes.
300    * See gst_pad_add_*_probe functions.
301    *
302    * Returns: %TRUE to keep the data, %FALSE to drop it
303    */
304   gst_pad_signals[PAD_HAVE_DATA] =
305       g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
306       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
307       G_STRUCT_OFFSET (GstPadClass, have_data),
308       _gst_do_pass_data_accumulator,
309       NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
310       GST_TYPE_MINI_OBJECT);
312   g_object_class_install_property (gobject_class, PAD_PROP_CAPS,
313       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
314           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315   g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
316       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
317           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
318           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
319   /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
320   g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
321       g_param_spec_object ("template", "Template",
322           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
323           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 #ifndef GST_DISABLE_LOADSAVE
326   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
327 #endif
328   gstobject_class->path_string_separator = ".";
330   klass->have_data = default_have_data;
333 static void
334 gst_pad_init (GstPad * pad)
336   pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
338   GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
339   GST_PAD_PEER (pad) = NULL;
341   GST_PAD_CHAINFUNC (pad) = NULL;
343   GST_PAD_LINKFUNC (pad) = NULL;
345   GST_PAD_CAPS (pad) = NULL;
346   GST_PAD_GETCAPSFUNC (pad) = NULL;
348   GST_PAD_ACTIVATEFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_activate_default);
349   GST_PAD_EVENTFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_event_default);
350   GST_PAD_QUERYTYPEFUNC (pad) =
351       GST_DEBUG_FUNCPTR (gst_pad_get_query_types_default);
352   GST_PAD_QUERYFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_query_default);
353 #ifndef GST_REMOVE_DEPRECATED
354   GST_PAD_INTLINKFUNC (pad) =
355       GST_DEBUG_FUNCPTR (gst_pad_get_internal_links_default);
356 #endif
357   GST_PAD_ITERINTLINKFUNC (pad) =
358       GST_DEBUG_FUNCPTR (gst_pad_iterate_internal_links_default);
360   GST_PAD_ACCEPTCAPSFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_acceptcaps_default);
362   pad->do_buffer_signals = 0;
363   pad->do_event_signals = 0;
365   GST_PAD_SET_FLUSHING (pad);
367   pad->preroll_lock = g_mutex_new ();
368   pad->preroll_cond = g_cond_new ();
370   pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
371   g_static_rec_mutex_init (pad->stream_rec_lock);
373   pad->block_cond = g_cond_new ();
376 static void
377 gst_pad_dispose (GObject * object)
379   GstPad *pad = GST_PAD (object);
380   GstPad *peer;
382   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
384   /* unlink the peer pad */
385   if ((peer = gst_pad_get_peer (pad))) {
386     /* window for MT unsafeness, someone else could unlink here
387      * and then we call unlink with wrong pads. The unlink
388      * function would catch this and safely return failed. */
389     if (GST_PAD_IS_SRC (pad))
390       gst_pad_unlink (pad, peer);
391     else
392       gst_pad_unlink (peer, pad);
394     gst_object_unref (peer);
395   }
397   /* clear the caps */
398   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
400   gst_pad_set_pad_template (pad, NULL);
402   if (pad->block_destroy_data && pad->block_data) {
403     pad->block_destroy_data (pad->block_data);
404     pad->block_data = NULL;
405   }
407   G_OBJECT_CLASS (parent_class)->dispose (object);
410 static void
411 gst_pad_finalize (GObject * object)
413   GstPad *pad = GST_PAD (object);
414   GstTask *task;
416   /* in case the task is still around, clean it up */
417   if ((task = GST_PAD_TASK (pad))) {
418     gst_task_join (task);
419     GST_PAD_TASK (pad) = NULL;
420     gst_object_unref (task);
421   }
423   if (pad->stream_rec_lock) {
424     g_static_rec_mutex_free (pad->stream_rec_lock);
425     g_free (pad->stream_rec_lock);
426     pad->stream_rec_lock = NULL;
427   }
428   if (pad->preroll_lock) {
429     g_mutex_free (pad->preroll_lock);
430     g_cond_free (pad->preroll_cond);
431     pad->preroll_lock = NULL;
432     pad->preroll_cond = NULL;
433   }
434   if (pad->block_cond) {
435     g_cond_free (pad->block_cond);
436     pad->block_cond = NULL;
437   }
439   G_OBJECT_CLASS (parent_class)->finalize (object);
442 static void
443 gst_pad_set_property (GObject * object, guint prop_id,
444     const GValue * value, GParamSpec * pspec)
446   g_return_if_fail (GST_IS_PAD (object));
448   switch (prop_id) {
449     case PAD_PROP_DIRECTION:
450       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
451       break;
452     case PAD_PROP_TEMPLATE:
453       gst_pad_set_pad_template (GST_PAD_CAST (object),
454           (GstPadTemplate *) g_value_get_object (value));
455       break;
456     default:
457       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458       break;
459   }
462 static void
463 gst_pad_get_property (GObject * object, guint prop_id,
464     GValue * value, GParamSpec * pspec)
466   g_return_if_fail (GST_IS_PAD (object));
468   switch (prop_id) {
469     case PAD_PROP_CAPS:
470       GST_OBJECT_LOCK (object);
471       g_value_set_boxed (value, GST_PAD_CAPS (object));
472       GST_OBJECT_UNLOCK (object);
473       break;
474     case PAD_PROP_DIRECTION:
475       g_value_set_enum (value, GST_PAD_DIRECTION (object));
476       break;
477     case PAD_PROP_TEMPLATE:
478       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
479       break;
480     default:
481       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482       break;
483   }
486 /**
487  * gst_pad_new:
488  * @name: the name of the new pad.
489  * @direction: the #GstPadDirection of the pad.
490  *
491  * Creates a new pad with the given name in the given direction.
492  * If name is NULL, a guaranteed unique name (across all pads)
493  * will be assigned.
494  * This function makes a copy of the name so you can safely free the name.
495  *
496  * Returns: a new #GstPad, or NULL in case of an error.
497  *
498  * MT safe.
499  */
500 GstPad *
501 gst_pad_new (const gchar * name, GstPadDirection direction)
503   return g_object_new (GST_TYPE_PAD,
504       "name", name, "direction", direction, NULL);
507 /**
508  * gst_pad_new_from_template:
509  * @templ: the pad template to use
510  * @name: the name of the element
511  *
512  * Creates a new pad with the given name from the given template.
513  * If name is NULL, a guaranteed unique name (across all pads)
514  * will be assigned.
515  * This function makes a copy of the name so you can safely free the name.
516  *
517  * Returns: a new #GstPad, or NULL in case of an error.
518  */
519 GstPad *
520 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
522   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
524   return g_object_new (GST_TYPE_PAD,
525       "name", name, "direction", templ->direction, "template", templ, NULL);
528 /**
529  * gst_pad_new_from_static_template:
530  * @templ: the #GstStaticPadTemplate to use
531  * @name: the name of the element
532  *
533  * Creates a new pad with the given name from the given static template.
534  * If name is NULL, a guaranteed unique name (across all pads)
535  * will be assigned.
536  * This function makes a copy of the name so you can safely free the name.
537  *
538  * Returns: a new #GstPad, or NULL in case of an error.
539  */
540 GstPad *
541 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
542     const gchar * name)
544   GstPad *pad;
545   GstPadTemplate *template;
547   template = gst_static_pad_template_get (templ);
548   pad = gst_pad_new_from_template (template, name);
549   gst_object_unref (template);
550   return pad;
553 /**
554  * gst_pad_get_direction:
555  * @pad: a #GstPad to get the direction of.
556  *
557  * Gets the direction of the pad. The direction of the pad is
558  * decided at construction time so this function does not take
559  * the LOCK.
560  *
561  * Returns: the #GstPadDirection of the pad.
562  *
563  * MT safe.
564  */
565 GstPadDirection
566 gst_pad_get_direction (GstPad * pad)
568   GstPadDirection result;
570   /* PAD_UNKNOWN is a little silly but we need some sort of
571    * error return value */
572   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
574   result = GST_PAD_DIRECTION (pad);
576   return result;
579 static gboolean
580 gst_pad_activate_default (GstPad * pad)
582   return gst_pad_activate_push (pad, TRUE);
585 static void
586 pre_activate (GstPad * pad, GstActivateMode new_mode)
588   switch (new_mode) {
589     case GST_ACTIVATE_PUSH:
590     case GST_ACTIVATE_PULL:
591       GST_OBJECT_LOCK (pad);
592       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
593           new_mode);
594       GST_PAD_UNSET_FLUSHING (pad);
595       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
596       GST_OBJECT_UNLOCK (pad);
597       break;
598     case GST_ACTIVATE_NONE:
599       GST_OBJECT_LOCK (pad);
600       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
601       GST_PAD_SET_FLUSHING (pad);
602       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
603       /* unlock blocked pads so element can resume and stop */
604       GST_PAD_BLOCK_BROADCAST (pad);
605       GST_OBJECT_UNLOCK (pad);
606       break;
607   }
610 static void
611 post_activate (GstPad * pad, GstActivateMode new_mode)
613   switch (new_mode) {
614     case GST_ACTIVATE_PUSH:
615     case GST_ACTIVATE_PULL:
616       /* nop */
617       break;
618     case GST_ACTIVATE_NONE:
619       /* ensures that streaming stops */
620       GST_PAD_STREAM_LOCK (pad);
621       GST_DEBUG_OBJECT (pad, "stopped streaming");
622       GST_PAD_STREAM_UNLOCK (pad);
623       break;
624   }
627 /**
628  * gst_pad_set_active:
629  * @pad: the #GstPad to activate or deactivate.
630  * @active: whether or not the pad should be active.
631  *
632  * Activates or deactivates the given pad.
633  * Normally called from within core state change functions.
634  *
635  * If @active, makes sure the pad is active. If it is already active, either in
636  * push or pull mode, just return. Otherwise dispatches to the pad's activate
637  * function to perform the actual activation.
638  *
639  * If not @active, checks the pad's current mode and calls
640  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
641  * FALSE argument.
642  *
643  * Returns: #TRUE if the operation was successful.
644  *
645  * MT safe.
646  */
647 gboolean
648 gst_pad_set_active (GstPad * pad, gboolean active)
650   GstActivateMode old;
651   gboolean ret = FALSE;
653   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
655   GST_OBJECT_LOCK (pad);
656   old = GST_PAD_ACTIVATE_MODE (pad);
657   GST_OBJECT_UNLOCK (pad);
659   if (active) {
660     switch (old) {
661       case GST_ACTIVATE_PUSH:
662         GST_DEBUG_OBJECT (pad, "activating pad from push");
663         ret = TRUE;
664         break;
665       case GST_ACTIVATE_PULL:
666         GST_DEBUG_OBJECT (pad, "activating pad from pull");
667         ret = TRUE;
668         break;
669       case GST_ACTIVATE_NONE:
670         GST_DEBUG_OBJECT (pad, "activating pad from none");
671         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
672         break;
673     }
674   } else {
675     switch (old) {
676       case GST_ACTIVATE_PUSH:
677         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
678         ret = gst_pad_activate_push (pad, FALSE);
679         break;
680       case GST_ACTIVATE_PULL:
681         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
682         ret = gst_pad_activate_pull (pad, FALSE);
683         break;
684       case GST_ACTIVATE_NONE:
685         GST_DEBUG_OBJECT (pad, "deactivating pad from none");
686         ret = TRUE;
687         break;
688     }
689   }
691   if (!ret) {
692     GST_OBJECT_LOCK (pad);
693     if (!active) {
694       g_critical ("Failed to deactivate pad %s:%s, very bad",
695           GST_DEBUG_PAD_NAME (pad));
696     } else {
697       GST_WARNING_OBJECT (pad, "Failed to activate pad");
698     }
699     GST_OBJECT_UNLOCK (pad);
700   }
702   return ret;
705 /**
706  * gst_pad_activate_pull:
707  * @pad: the #GstPad to activate or deactivate.
708  * @active: whether or not the pad should be active.
709  *
710  * Activates or deactivates the given pad in pull mode via dispatching to the
711  * pad's activatepullfunc. For use from within pad activation functions only.
712  * When called on sink pads, will first proxy the call to the peer pad, which
713  * is expected to activate its internally linked pads from within its
714  * activate_pull function.
715  *
716  * If you don't know what this is, you probably don't want to call it.
717  *
718  * Returns: TRUE if the operation was successful.
719  *
720  * MT safe.
721  */
722 gboolean
723 gst_pad_activate_pull (GstPad * pad, gboolean active)
725   GstActivateMode old, new;
726   GstPad *peer;
728   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
730   GST_OBJECT_LOCK (pad);
731   old = GST_PAD_ACTIVATE_MODE (pad);
732   GST_OBJECT_UNLOCK (pad);
734   if (active) {
735     switch (old) {
736       case GST_ACTIVATE_PULL:
737         GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
738         goto was_ok;
739       case GST_ACTIVATE_PUSH:
740         GST_DEBUG_OBJECT (pad,
741             "activating pad from push, deactivate push first");
742         /* pad was activate in the wrong direction, deactivate it
743          * and reactivate it in pull mode */
744         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
745           goto deactivate_failed;
746         /* fallthrough, pad is deactivated now. */
747       case GST_ACTIVATE_NONE:
748         GST_DEBUG_OBJECT (pad, "activating pad from none");
749         break;
750     }
751   } else {
752     switch (old) {
753       case GST_ACTIVATE_NONE:
754         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
755         goto was_ok;
756       case GST_ACTIVATE_PUSH:
757         GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
758         /* pad was activated in the other direction, deactivate it
759          * in push mode, this should not happen... */
760         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
761           goto deactivate_failed;
762         /* everything is fine now */
763         goto was_ok;
764       case GST_ACTIVATE_PULL:
765         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
766         break;
767     }
768   }
770   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
771     if ((peer = gst_pad_get_peer (pad))) {
772       GST_DEBUG_OBJECT (pad, "calling peer");
773       if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
774         goto peer_failed;
775       gst_object_unref (peer);
776     } else {
777       /* there is no peer, this is only fatal when we activate. When we
778        * deactivate, we must assume the application has unlinked the peer and
779        * will deactivate it eventually. */
780       if (active)
781         goto not_linked;
782       else
783         GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
784     }
785   } else {
786     if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
787       goto failure;             /* Can't activate pull on a src without a
788                                    getrange function */
789   }
791   new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
792   pre_activate (pad, new);
794   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
795     if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
796       goto failure;
797   } else {
798     /* can happen for sinks of passthrough elements */
799   }
801   post_activate (pad, new);
803   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
804       active ? "activated" : "deactivated");
806   return TRUE;
808 was_ok:
809   {
810     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
811         active ? "activated" : "deactivated");
812     return TRUE;
813   }
814 deactivate_failed:
815   {
816     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
817         "failed to %s in switch to pull from mode %d",
818         (active ? "activate" : "deactivate"), old);
819     return FALSE;
820   }
821 peer_failed:
822   {
823     GST_OBJECT_LOCK (peer);
824     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
825         "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
826     GST_OBJECT_UNLOCK (peer);
827     gst_object_unref (peer);
828     return FALSE;
829   }
830 not_linked:
831   {
832     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
833         "pad in pull mode");
834     return FALSE;
835   }
836 failure:
837   {
838     GST_OBJECT_LOCK (pad);
839     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
840         active ? "activate" : "deactivate");
841     GST_PAD_SET_FLUSHING (pad);
842     GST_PAD_ACTIVATE_MODE (pad) = old;
843     GST_OBJECT_UNLOCK (pad);
844     return FALSE;
845   }
848 /**
849  * gst_pad_activate_push:
850  * @pad: the #GstPad to activate or deactivate.
851  * @active: whether the pad should be active or not.
852  *
853  * Activates or deactivates the given pad in push mode via dispatching to the
854  * pad's activatepushfunc. For use from within pad activation functions only.
855  *
856  * If you don't know what this is, you probably don't want to call it.
857  *
858  * Returns: %TRUE if the operation was successful.
859  *
860  * MT safe.
861  */
862 gboolean
863 gst_pad_activate_push (GstPad * pad, gboolean active)
865   GstActivateMode old, new;
867   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
868   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
869       active ? "activated" : "deactivated");
871   GST_OBJECT_LOCK (pad);
872   old = GST_PAD_ACTIVATE_MODE (pad);
873   GST_OBJECT_UNLOCK (pad);
875   if (active) {
876     switch (old) {
877       case GST_ACTIVATE_PUSH:
878         GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
879         goto was_ok;
880       case GST_ACTIVATE_PULL:
881         GST_DEBUG_OBJECT (pad,
882             "activating pad from push, deactivating pull first");
883         /* pad was activate in the wrong direction, deactivate it
884          * an reactivate it in push mode */
885         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
886           goto deactivate_failed;
887         /* fallthrough, pad is deactivated now. */
888       case GST_ACTIVATE_NONE:
889         GST_DEBUG_OBJECT (pad, "activating pad from none");
890         break;
891     }
892   } else {
893     switch (old) {
894       case GST_ACTIVATE_NONE:
895         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
896         goto was_ok;
897       case GST_ACTIVATE_PULL:
898         GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
899         /* pad was activated in the other direction, deactivate it
900          * in pull mode, this should not happen... */
901         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
902           goto deactivate_failed;
903         /* everything is fine now */
904         goto was_ok;
905       case GST_ACTIVATE_PUSH:
906         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
907         break;
908     }
909   }
911   new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
912   pre_activate (pad, new);
914   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
915     if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
916       goto failure;
917     }
918   } else {
919     /* quite ok, element relies on state change func to prepare itself */
920   }
922   post_activate (pad, new);
924   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
925       active ? "activated" : "deactivated");
926   return TRUE;
928 was_ok:
929   {
930     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
931         active ? "activated" : "deactivated");
932     return TRUE;
933   }
934 deactivate_failed:
935   {
936     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
937         "failed to %s in switch to push from mode %d",
938         (active ? "activate" : "deactivate"), old);
939     return FALSE;
940   }
941 failure:
942   {
943     GST_OBJECT_LOCK (pad);
944     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
945         active ? "activate" : "deactivate");
946     GST_PAD_SET_FLUSHING (pad);
947     GST_PAD_ACTIVATE_MODE (pad) = old;
948     GST_OBJECT_UNLOCK (pad);
949     return FALSE;
950   }
953 /**
954  * gst_pad_is_active:
955  * @pad: the #GstPad to query
956  *
957  * Query if a pad is active
958  *
959  * Returns: TRUE if the pad is active.
960  *
961  * MT safe.
962  */
963 gboolean
964 gst_pad_is_active (GstPad * pad)
966   gboolean result = FALSE;
968   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
970   GST_OBJECT_LOCK (pad);
971   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
972   GST_OBJECT_UNLOCK (pad);
974   return result;
977 /**
978  * gst_pad_set_blocked_async_full:
979  * @pad: the #GstPad to block or unblock
980  * @blocked: boolean indicating whether the pad should be blocked or unblocked
981  * @callback: #GstPadBlockCallback that will be called when the
982  *            operation succeeds
983  * @user_data: user data passed to the callback
984  * @destroy_data: #GDestroyNotify for user_data
985  *
986  * Blocks or unblocks the dataflow on a pad. The provided callback
987  * is called when the operation succeeds; this happens right before the next
988  * attempt at pushing a buffer on the pad.
989  *
990  * This can take a while as the pad can only become blocked when real dataflow
991  * is happening.
992  * When the pipeline is stalled, for example in PAUSED, this can
993  * take an indeterminate amount of time.
994  * You can pass NULL as the callback to make this call block. Be careful with
995  * this blocking call as it might not return for reasons stated above.
996  *
997  * Returns: TRUE if the pad could be blocked. This function can fail if the
998  * wrong parameters were passed or the pad was already in the requested state.
999  *
1000  * MT safe.
1001  *
1002  * Since: 0.10.23
1003  */
1004 gboolean
1005 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1006     GstPadBlockCallback callback, gpointer user_data,
1007     GDestroyNotify destroy_data)
1009   gboolean was_blocked = FALSE;
1011   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1013   GST_OBJECT_LOCK (pad);
1015   was_blocked = GST_PAD_IS_BLOCKED (pad);
1017   if (G_UNLIKELY (was_blocked == blocked))
1018     goto had_right_state;
1020   if (blocked) {
1021     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1023     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1025     if (pad->block_destroy_data && pad->block_data &&
1026         pad->block_data != user_data)
1027       pad->block_destroy_data (pad->block_data);
1029     pad->block_callback = callback;
1030     pad->block_data = user_data;
1031     pad->block_destroy_data = destroy_data;
1032     pad->abidata.ABI.block_callback_called = FALSE;
1033     if (!callback) {
1034       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1035       GST_PAD_BLOCK_WAIT (pad);
1036       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1037     }
1038   } else {
1039     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1041     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1043     if (pad->block_destroy_data && pad->block_data &&
1044         pad->block_data != user_data)
1045       pad->block_destroy_data (pad->block_data);
1047     pad->block_callback = callback;
1048     pad->block_data = user_data;
1049     pad->block_destroy_data = destroy_data;
1050     pad->abidata.ABI.block_callback_called = FALSE;
1052     GST_PAD_BLOCK_BROADCAST (pad);
1053     if (!callback) {
1054       /* no callback, wait for the unblock to happen */
1055       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1056       GST_PAD_BLOCK_WAIT (pad);
1057       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1058     }
1059   }
1060   GST_OBJECT_UNLOCK (pad);
1062   return TRUE;
1064 had_right_state:
1065   {
1066     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1067         "pad was in right state (%d)", was_blocked);
1068     GST_OBJECT_UNLOCK (pad);
1070     return FALSE;
1071   }
1074 /**
1075  * gst_pad_set_blocked_async:
1076  * @pad: the #GstPad to block or unblock
1077  * @blocked: boolean indicating whether the pad should be blocked or unblocked
1078  * @callback: #GstPadBlockCallback that will be called when the
1079  *            operation succeeds
1080  * @user_data: user data passed to the callback
1081  *
1082  * Blocks or unblocks the dataflow on a pad. The provided callback
1083  * is called when the operation succeeds; this happens right before the next
1084  * attempt at pushing a buffer on the pad.
1085  *
1086  * This can take a while as the pad can only become blocked when real dataflow
1087  * is happening.
1088  * When the pipeline is stalled, for example in PAUSED, this can
1089  * take an indeterminate amount of time.
1090  * You can pass NULL as the callback to make this call block. Be careful with
1091  * this blocking call as it might not return for reasons stated above.
1092  *
1093  * Returns: TRUE if the pad could be blocked. This function can fail if the
1094  * wrong parameters were passed or the pad was already in the requested state.
1095  *
1096  * MT safe.
1097  */
1098 gboolean
1099 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1100     GstPadBlockCallback callback, gpointer user_data)
1102   return gst_pad_set_blocked_async_full (pad, blocked,
1103       callback, user_data, NULL);
1106 /**
1107  * gst_pad_set_blocked:
1108  * @pad: the #GstPad to block or unblock
1109  * @blocked: boolean indicating we should block or unblock
1110  *
1111  * Blocks or unblocks the dataflow on a pad. This function is
1112  * a shortcut for gst_pad_set_blocked_async() with a NULL
1113  * callback.
1114  *
1115  * Returns: TRUE if the pad could be blocked. This function can fail if the
1116  * wrong parameters were passed or the pad was already in the requested state.
1117  *
1118  * MT safe.
1119  */
1120 gboolean
1121 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1123   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1126 /**
1127  * gst_pad_is_blocked:
1128  * @pad: the #GstPad to query
1129  *
1130  * Checks if the pad is blocked or not. This function returns the
1131  * last requested state of the pad. It is not certain that the pad
1132  * is actually blocking at this point (see gst_pad_is_blocking()).
1133  *
1134  * Returns: TRUE if the pad is blocked.
1135  *
1136  * MT safe.
1137  */
1138 gboolean
1139 gst_pad_is_blocked (GstPad * pad)
1141   gboolean result = FALSE;
1143   g_return_val_if_fail (GST_IS_PAD (pad), result);
1145   GST_OBJECT_LOCK (pad);
1146   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1147   GST_OBJECT_UNLOCK (pad);
1149   return result;
1152 /**
1153  * gst_pad_is_blocking:
1154  * @pad: the #GstPad to query
1155  *
1156  * Checks if the pad is blocking or not. This is a guaranteed state
1157  * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1158  *
1159  * Returns: TRUE if the pad is blocking.
1160  *
1161  * MT safe.
1162  *
1163  * Since: 0.10.11
1164  */
1165 gboolean
1166 gst_pad_is_blocking (GstPad * pad)
1168   gboolean result = FALSE;
1170   g_return_val_if_fail (GST_IS_PAD (pad), result);
1172   GST_OBJECT_LOCK (pad);
1173   /* the blocking flag is only valid if the pad is not flushing */
1174   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1175       !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1176   GST_OBJECT_UNLOCK (pad);
1178   return result;
1181 /**
1182  * gst_pad_set_activate_function:
1183  * @pad: a #GstPad.
1184  * @activate: the #GstPadActivateFunction to set.
1185  *
1186  * Sets the given activate function for @pad. The activate function will
1187  * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1188  * the actual activation. Only makes sense to set on sink pads.
1189  *
1190  * Call this function if your sink pad can start a pull-based task.
1191  */
1192 void
1193 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1195   g_return_if_fail (GST_IS_PAD (pad));
1197   GST_PAD_ACTIVATEFUNC (pad) = activate;
1198   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1199       GST_DEBUG_FUNCPTR_NAME (activate));
1202 /**
1203  * gst_pad_set_activatepull_function:
1204  * @pad: a #GstPad.
1205  * @activatepull: the #GstPadActivateModeFunction to set.
1206  *
1207  * Sets the given activate_pull function for the pad. An activate_pull function
1208  * prepares the element and any upstream connections for pulling. See XXX
1209  * part-activation.txt for details.
1210  */
1211 void
1212 gst_pad_set_activatepull_function (GstPad * pad,
1213     GstPadActivateModeFunction activatepull)
1215   g_return_if_fail (GST_IS_PAD (pad));
1217   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1218   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1219       GST_DEBUG_FUNCPTR_NAME (activatepull));
1222 /**
1223  * gst_pad_set_activatepush_function:
1224  * @pad: a #GstPad.
1225  * @activatepush: the #GstPadActivateModeFunction to set.
1226  *
1227  * Sets the given activate_push function for the pad. An activate_push function
1228  * prepares the element for pushing. See XXX part-activation.txt for details.
1229  */
1230 void
1231 gst_pad_set_activatepush_function (GstPad * pad,
1232     GstPadActivateModeFunction activatepush)
1234   g_return_if_fail (GST_IS_PAD (pad));
1236   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1237   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1238       GST_DEBUG_FUNCPTR_NAME (activatepush));
1241 /**
1242  * gst_pad_set_chain_function:
1243  * @pad: a sink #GstPad.
1244  * @chain: the #GstPadChainFunction to set.
1245  *
1246  * Sets the given chain function for the pad. The chain function is called to
1247  * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1248  */
1249 void
1250 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1252   g_return_if_fail (GST_IS_PAD (pad));
1253   g_return_if_fail (GST_PAD_IS_SINK (pad));
1255   GST_PAD_CHAINFUNC (pad) = chain;
1256   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1257       GST_DEBUG_FUNCPTR_NAME (chain));
1260 /**
1261  * gst_pad_set_chain_list_function:
1262  * @pad: a sink #GstPad.
1263  * @chainlist: the #GstPadChainListFunction to set.
1264  *
1265  * Sets the given chain list function for the pad. The chainlist function is
1266  * called to process a #GstBufferList input buffer list. See
1267  * #GstPadChainListFunction for more details.
1268  *
1269  * Since: 0.10.24
1270  */
1271 void
1272 gst_pad_set_chain_list_function (GstPad * pad,
1273     GstPadChainListFunction chainlist)
1275   g_return_if_fail (GST_IS_PAD (pad));
1276   g_return_if_fail (GST_PAD_IS_SINK (pad));
1278   GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1279   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1280       GST_DEBUG_FUNCPTR_NAME (chainlist));
1283 /**
1284  * gst_pad_set_getrange_function:
1285  * @pad: a source #GstPad.
1286  * @get: the #GstPadGetRangeFunction to set.
1287  *
1288  * Sets the given getrange function for the pad. The getrange function is
1289  * called to produce a new #GstBuffer to start the processing pipeline. see
1290  * #GstPadGetRangeFunction for a description of the getrange function.
1291  */
1292 void
1293 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1295   g_return_if_fail (GST_IS_PAD (pad));
1296   g_return_if_fail (GST_PAD_IS_SRC (pad));
1298   GST_PAD_GETRANGEFUNC (pad) = get;
1300   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1301       GST_DEBUG_FUNCPTR_NAME (get));
1304 /**
1305  * gst_pad_set_checkgetrange_function:
1306  * @pad: a source #GstPad.
1307  * @check: the #GstPadCheckGetRangeFunction to set.
1308  *
1309  * Sets the given checkgetrange function for the pad. Implement this function
1310  * on a pad if you dynamically support getrange based scheduling on the pad.
1311  */
1312 void
1313 gst_pad_set_checkgetrange_function (GstPad * pad,
1314     GstPadCheckGetRangeFunction check)
1316   g_return_if_fail (GST_IS_PAD (pad));
1317   g_return_if_fail (GST_PAD_IS_SRC (pad));
1319   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1321   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1322       GST_DEBUG_FUNCPTR_NAME (check));
1325 /**
1326  * gst_pad_set_event_function:
1327  * @pad: a #GstPad of either direction.
1328  * @event: the #GstPadEventFunction to set.
1329  *
1330  * Sets the given event handler for the pad.
1331  */
1332 void
1333 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1335   g_return_if_fail (GST_IS_PAD (pad));
1337   GST_PAD_EVENTFUNC (pad) = event;
1339   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1340       GST_DEBUG_FUNCPTR_NAME (event));
1343 /**
1344  * gst_pad_set_query_function:
1345  * @pad: a #GstPad of either direction.
1346  * @query: the #GstPadQueryFunction to set.
1347  *
1348  * Set the given query function for the pad.
1349  */
1350 void
1351 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1353   g_return_if_fail (GST_IS_PAD (pad));
1355   GST_PAD_QUERYFUNC (pad) = query;
1357   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1358       GST_DEBUG_FUNCPTR_NAME (query));
1361 /**
1362  * gst_pad_set_query_type_function:
1363  * @pad: a #GstPad of either direction.
1364  * @type_func: the #GstPadQueryTypeFunction to set.
1365  *
1366  * Set the given query type function for the pad.
1367  */
1368 void
1369 gst_pad_set_query_type_function (GstPad * pad,
1370     GstPadQueryTypeFunction type_func)
1372   g_return_if_fail (GST_IS_PAD (pad));
1374   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1376   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1377       GST_DEBUG_FUNCPTR_NAME (type_func));
1380 /**
1381  * gst_pad_get_query_types:
1382  * @pad: a #GstPad.
1383  *
1384  * Get an array of supported queries that can be performed
1385  * on this pad.
1386  *
1387  * Returns: a zero-terminated array of #GstQueryType.
1388  */
1389 const GstQueryType *
1390 gst_pad_get_query_types (GstPad * pad)
1392   GstPadQueryTypeFunction func;
1394   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1396   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1397     goto no_func;
1399   return func (pad);
1401 no_func:
1402   {
1403     return NULL;
1404   }
1407 static gboolean
1408 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1410   *data = gst_pad_get_query_types (pad);
1412   return TRUE;
1415 /**
1416  * gst_pad_get_query_types_default:
1417  * @pad: a #GstPad.
1418  *
1419  * Invoke the default dispatcher for the query types on
1420  * the pad.
1421  *
1422  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1423  * internally-linked pads has a query types function.
1424  */
1425 const GstQueryType *
1426 gst_pad_get_query_types_default (GstPad * pad)
1428   GstQueryType *result = NULL;
1430   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1432   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1433       gst_pad_get_query_types_dispatcher, &result);
1435   return result;
1438 /**
1439  * gst_pad_set_iterate_internal_links_function:
1440  * @pad: a #GstPad of either direction.
1441  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1442  *
1443  * Sets the given internal link iterator function for the pad.
1444  *
1445  * Since: 0.10.21
1446  */
1447 void
1448 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1449     GstPadIterIntLinkFunction iterintlink)
1451   g_return_if_fail (GST_IS_PAD (pad));
1453   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1454   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1455       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1458 /**
1459  * gst_pad_set_internal_link_function:
1460  * @pad: a #GstPad of either direction.
1461  * @intlink: the #GstPadIntLinkFunction to set.
1462  *
1463  * Sets the given internal link function for the pad.
1464  *
1465  * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1466  */
1467 #ifndef GST_REMOVE_DEPRECATED
1468 void
1469 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1471   g_return_if_fail (GST_IS_PAD (pad));
1473   GST_PAD_INTLINKFUNC (pad) = intlink;
1474   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1475       GST_DEBUG_FUNCPTR_NAME (intlink));
1477 #endif /* GST_REMOVE_DEPRECATED */
1479 /**
1480  * gst_pad_set_link_function:
1481  * @pad: a #GstPad.
1482  * @link: the #GstPadLinkFunction to set.
1483  *
1484  * Sets the given link function for the pad. It will be called when
1485  * the pad is linked with another pad.
1486  *
1487  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1488  * made.
1489  *
1490  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1491  * cannot be made for some reason.
1492  *
1493  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1494  * of the peer sink pad, if present.
1495  */
1496 void
1497 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1499   g_return_if_fail (GST_IS_PAD (pad));
1501   GST_PAD_LINKFUNC (pad) = link;
1502   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1503       GST_DEBUG_FUNCPTR_NAME (link));
1506 /**
1507  * gst_pad_set_unlink_function:
1508  * @pad: a #GstPad.
1509  * @unlink: the #GstPadUnlinkFunction to set.
1510  *
1511  * Sets the given unlink function for the pad. It will be called
1512  * when the pad is unlinked.
1513  */
1514 void
1515 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1517   g_return_if_fail (GST_IS_PAD (pad));
1519   GST_PAD_UNLINKFUNC (pad) = unlink;
1520   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1521       GST_DEBUG_FUNCPTR_NAME (unlink));
1524 /**
1525  * gst_pad_set_getcaps_function:
1526  * @pad: a #GstPad.
1527  * @getcaps: the #GstPadGetCapsFunction to set.
1528  *
1529  * Sets the given getcaps function for the pad. @getcaps should return the
1530  * allowable caps for a pad in the context of the element's state, its link to
1531  * other elements, and the devices or files it has opened. These caps must be a
1532  * subset of the pad template caps. In the NULL state with no links, @getcaps
1533  * should ideally return the same caps as the pad template. In rare
1534  * circumstances, an object property can affect the caps returned by @getcaps,
1535  * but this is discouraged.
1536  *
1537  * You do not need to call this function if @pad's allowed caps are always the
1538  * same as the pad template caps. This can only be true if the padtemplate
1539  * has fixed simple caps.
1540  *
1541  * For most filters, the caps returned by @getcaps is directly affected by the
1542  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1543  * the srcpad's getcaps function is directly related to the stream data. Again,
1544  * @getcaps should return the most specific caps it reasonably can, since this
1545  * helps with autoplugging.
1546  *
1547  * Note that the return value from @getcaps is owned by the caller, so the
1548  * caller should unref the caps after usage.
1549  */
1550 void
1551 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1553   g_return_if_fail (GST_IS_PAD (pad));
1555   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1556   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1557       GST_DEBUG_FUNCPTR_NAME (getcaps));
1560 /**
1561  * gst_pad_set_acceptcaps_function:
1562  * @pad: a #GstPad.
1563  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1564  *
1565  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1566  * will be called to check if the pad can accept the given caps. Setting the
1567  * acceptcaps function to NULL restores the default behaviour of allowing
1568  * any caps that matches the caps from gst_pad_get_caps.
1569  */
1570 void
1571 gst_pad_set_acceptcaps_function (GstPad * pad,
1572     GstPadAcceptCapsFunction acceptcaps)
1574   g_return_if_fail (GST_IS_PAD (pad));
1576   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1577   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1578       GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1581 /**
1582  * gst_pad_set_fixatecaps_function:
1583  * @pad: a #GstPad.
1584  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1585  *
1586  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1587  * will be called whenever the default values for a GstCaps needs to be
1588  * filled in.
1589  */
1590 void
1591 gst_pad_set_fixatecaps_function (GstPad * pad,
1592     GstPadFixateCapsFunction fixatecaps)
1594   g_return_if_fail (GST_IS_PAD (pad));
1596   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1597   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1598       GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1601 /**
1602  * gst_pad_set_setcaps_function:
1603  * @pad: a #GstPad.
1604  * @setcaps: the #GstPadSetCapsFunction to set.
1605  *
1606  * Sets the given setcaps function for the pad.  The setcaps function
1607  * will be called whenever a buffer with a new media type is pushed or
1608  * pulled from the pad. The pad/element needs to update its internal
1609  * structures to process the new media type. If this new type is not
1610  * acceptable, the setcaps function should return FALSE.
1611  */
1612 void
1613 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1615   g_return_if_fail (GST_IS_PAD (pad));
1617   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1618   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1619       GST_DEBUG_FUNCPTR_NAME (setcaps));
1622 /**
1623  * gst_pad_set_bufferalloc_function:
1624  * @pad: a sink #GstPad.
1625  * @bufalloc: the #GstPadBufferAllocFunction to set.
1626  *
1627  * Sets the given bufferalloc function for the pad. Note that the
1628  * bufferalloc function can only be set on sinkpads.
1629  */
1630 void
1631 gst_pad_set_bufferalloc_function (GstPad * pad,
1632     GstPadBufferAllocFunction bufalloc)
1634   g_return_if_fail (GST_IS_PAD (pad));
1635   g_return_if_fail (GST_PAD_IS_SINK (pad));
1637   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1638   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1639       GST_DEBUG_FUNCPTR_NAME (bufalloc));
1642 /**
1643  * gst_pad_unlink:
1644  * @srcpad: the source #GstPad to unlink.
1645  * @sinkpad: the sink #GstPad to unlink.
1646  *
1647  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1648  * signal on both pads.
1649  *
1650  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1651  * the pads were not linked together.
1652  *
1653  * MT safe.
1654  */
1655 gboolean
1656 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1658   gboolean result = FALSE;
1659   GstElement *parent = NULL;
1661   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1662   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1663   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1664   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1666   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1667       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1668       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1670   /* We need to notify the parent before taking any pad locks as the bin in
1671    * question might be waiting for a lock on the pad while holding its lock
1672    * that our message will try to take. */
1673   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1674     if (GST_IS_ELEMENT (parent)) {
1675       gst_element_post_message (parent,
1676           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1677               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1678     } else {
1679       gst_object_unref (parent);
1680       parent = NULL;
1681     }
1682   }
1684   GST_OBJECT_LOCK (srcpad);
1686   GST_OBJECT_LOCK (sinkpad);
1688   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1689     goto not_linked_together;
1691   if (GST_PAD_UNLINKFUNC (srcpad)) {
1692     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1693   }
1694   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1695     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1696   }
1698   /* first clear peers */
1699   GST_PAD_PEER (srcpad) = NULL;
1700   GST_PAD_PEER (sinkpad) = NULL;
1702   GST_OBJECT_UNLOCK (sinkpad);
1703   GST_OBJECT_UNLOCK (srcpad);
1705   /* fire off a signal to each of the pads telling them
1706    * that they've been unlinked */
1707   g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1708   g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1710   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1711       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1713   result = TRUE;
1715 done:
1716   if (parent != NULL) {
1717     gst_element_post_message (parent,
1718         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1719             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1720     gst_object_unref (parent);
1721   }
1722   return result;
1724   /* ERRORS */
1725 not_linked_together:
1726   {
1727     /* we do not emit a warning in this case because unlinking cannot
1728      * be made MT safe.*/
1729     GST_OBJECT_UNLOCK (sinkpad);
1730     GST_OBJECT_UNLOCK (srcpad);
1731     goto done;
1732   }
1735 /**
1736  * gst_pad_is_linked:
1737  * @pad: pad to check
1738  *
1739  * Checks if a @pad is linked to another pad or not.
1740  *
1741  * Returns: TRUE if the pad is linked, FALSE otherwise.
1742  *
1743  * MT safe.
1744  */
1745 gboolean
1746 gst_pad_is_linked (GstPad * pad)
1748   gboolean result;
1750   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1752   GST_OBJECT_LOCK (pad);
1753   result = (GST_PAD_PEER (pad) != NULL);
1754   GST_OBJECT_UNLOCK (pad);
1756   return result;
1759 /* get the caps from both pads and see if the intersection
1760  * is not empty.
1761  *
1762  * This function should be called with the pad LOCK on both
1763  * pads
1764  */
1765 static gboolean
1766 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1768   GstCaps *srccaps;
1769   GstCaps *sinkcaps;
1770   gboolean compatible = FALSE;
1772   srccaps = gst_pad_get_caps_unlocked (src);
1773   sinkcaps = gst_pad_get_caps_unlocked (sink);
1775   GST_CAT_DEBUG (GST_CAT_CAPS, "src caps %" GST_PTR_FORMAT, srccaps);
1776   GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1778   /* if we have caps on both pads we can check the intersection. If one
1779    * of the caps is NULL, we return TRUE. */
1780   if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1781     if (srccaps)
1782       gst_caps_unref (srccaps);
1783     if (sinkcaps)
1784       gst_caps_unref (sinkcaps);
1785     goto done;
1786   }
1788   compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1789   gst_caps_unref (srccaps);
1790   gst_caps_unref (sinkcaps);
1792 done:
1793   GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1794       (compatible ? "" : "not"));
1796   return compatible;
1799 /* check if the grandparents of both pads are the same.
1800  * This check is required so that we don't try to link
1801  * pads from elements in different bins without ghostpads.
1802  *
1803  * The LOCK should be held on both pads
1804  */
1805 static gboolean
1806 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1808   GstObject *psrc, *psink;
1810   psrc = GST_OBJECT_PARENT (src);
1811   psink = GST_OBJECT_PARENT (sink);
1813   /* if one of the pads has no parent, we allow the link */
1814   if (G_UNLIKELY (psrc == NULL || psink == NULL))
1815     goto no_parent;
1817   /* only care about parents that are elements */
1818   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1819     goto no_element_parent;
1821   /* if the parents are the same, we have a loop */
1822   if (G_UNLIKELY (psrc == psink))
1823     goto same_parents;
1825   /* if they both have a parent, we check the grandparents. We can not lock
1826    * the parent because we hold on the child (pad) and the locking order is
1827    * parent >> child. */
1828   psrc = GST_OBJECT_PARENT (psrc);
1829   psink = GST_OBJECT_PARENT (psink);
1831   /* if they have grandparents but they are not the same */
1832   if (G_UNLIKELY (psrc != psink))
1833     goto wrong_grandparents;
1835   return TRUE;
1837   /* ERRORS */
1838 no_parent:
1839   {
1840     GST_CAT_DEBUG (GST_CAT_CAPS,
1841         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1842         GST_PTR_FORMAT, psrc, psink);
1843     return TRUE;
1844   }
1845 no_element_parent:
1846   {
1847     GST_CAT_DEBUG (GST_CAT_CAPS,
1848         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1849         GST_PTR_FORMAT, psrc, psink);
1850     return TRUE;
1851   }
1852 same_parents:
1853   {
1854     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1855         psrc);
1856     return FALSE;
1857   }
1858 wrong_grandparents:
1859   {
1860     GST_CAT_DEBUG (GST_CAT_CAPS,
1861         "pads have different grandparents %" GST_PTR_FORMAT " and %"
1862         GST_PTR_FORMAT, psrc, psink);
1863     return FALSE;
1864   }
1867 /* FIXME leftover from an attempt at refactoring... */
1868 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1869  * the two pads will be locked in the srcpad, sinkpad order. */
1870 static GstPadLinkReturn
1871 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1873   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1874       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1876   GST_OBJECT_LOCK (srcpad);
1878   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1879     goto src_was_linked;
1881   GST_OBJECT_LOCK (sinkpad);
1883   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1884     goto sink_was_linked;
1886   /* check hierarchy, pads can only be linked if the grandparents
1887    * are the same. */
1888   if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1889     goto wrong_hierarchy;
1891   /* check pad caps for non-empty intersection */
1892   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1893     goto no_format;
1895   /* FIXME check pad scheduling for non-empty intersection */
1897   return GST_PAD_LINK_OK;
1899 src_was_linked:
1900   {
1901     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1902         GST_DEBUG_PAD_NAME (srcpad),
1903         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1904     /* we do not emit a warning in this case because unlinking cannot
1905      * be made MT safe.*/
1906     GST_OBJECT_UNLOCK (srcpad);
1907     return GST_PAD_LINK_WAS_LINKED;
1908   }
1909 sink_was_linked:
1910   {
1911     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1912         GST_DEBUG_PAD_NAME (sinkpad),
1913         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1914     /* we do not emit a warning in this case because unlinking cannot
1915      * be made MT safe.*/
1916     GST_OBJECT_UNLOCK (sinkpad);
1917     GST_OBJECT_UNLOCK (srcpad);
1918     return GST_PAD_LINK_WAS_LINKED;
1919   }
1920 wrong_hierarchy:
1921   {
1922     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1923     GST_OBJECT_UNLOCK (sinkpad);
1924     GST_OBJECT_UNLOCK (srcpad);
1925     return GST_PAD_LINK_WRONG_HIERARCHY;
1926   }
1927 no_format:
1928   {
1929     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1930     GST_OBJECT_UNLOCK (sinkpad);
1931     GST_OBJECT_UNLOCK (srcpad);
1932     return GST_PAD_LINK_NOFORMAT;
1933   }
1936 /**
1937  * gst_pad_can_link:
1938  * @srcpad: the source #GstPad.
1939  * @sinkpad: the sink #GstPad.
1940  *
1941  * Checks if the source pad and the sink pad are compatible so they can be
1942  * linked. 
1943  *
1944  * Returns: TRUE if the pads can be linked.
1945  */
1946 gboolean
1947 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1949   GstPadLinkReturn result;
1951   /* generic checks */
1952   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1953   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1955   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1956       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1958   /* gst_pad_link_prepare does everything for us, we only release the locks
1959    * on the pads that it gets us. If this function returns !OK the locks are not
1960    * taken anymore. */
1961   result = gst_pad_link_prepare (srcpad, sinkpad);
1962   if (result != GST_PAD_LINK_OK)
1963     goto done;
1965   GST_OBJECT_UNLOCK (srcpad);
1966   GST_OBJECT_UNLOCK (sinkpad);
1968 done:
1969   return result == GST_PAD_LINK_OK;
1972 /**
1973  * gst_pad_link:
1974  * @srcpad: the source #GstPad to link.
1975  * @sinkpad: the sink #GstPad to link.
1976  *
1977  * Links the source pad and the sink pad.
1978  *
1979  * Returns: A result code indicating if the connection worked or
1980  *          what went wrong.
1981  *
1982  * MT Safe.
1983  */
1984 GstPadLinkReturn
1985 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1987   GstPadLinkReturn result;
1988   GstElement *parent;
1990   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1991   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1992   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1993   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1994       GST_PAD_LINK_WRONG_DIRECTION);
1996   /* Notify the parent early. See gst_pad_unlink for details. */
1997   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1998     if (GST_IS_ELEMENT (parent)) {
1999       gst_element_post_message (parent,
2000           gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2001               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2002     } else {
2003       gst_object_unref (parent);
2004       parent = NULL;
2005     }
2006   }
2008   /* prepare will also lock the two pads */
2009   result = gst_pad_link_prepare (srcpad, sinkpad);
2011   if (result != GST_PAD_LINK_OK)
2012     goto done;
2014   /* must set peers before calling the link function */
2015   GST_PAD_PEER (srcpad) = sinkpad;
2016   GST_PAD_PEER (sinkpad) = srcpad;
2018   GST_OBJECT_UNLOCK (sinkpad);
2019   GST_OBJECT_UNLOCK (srcpad);
2021   /* FIXME released the locks here, concurrent thread might link
2022    * something else. */
2023   if (GST_PAD_LINKFUNC (srcpad)) {
2024     /* this one will call the peer link function */
2025     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2026   } else if (GST_PAD_LINKFUNC (sinkpad)) {
2027     /* if no source link function, we need to call the sink link
2028      * function ourselves. */
2029     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2030   } else {
2031     result = GST_PAD_LINK_OK;
2032   }
2034   GST_OBJECT_LOCK (srcpad);
2035   GST_OBJECT_LOCK (sinkpad);
2037   if (result == GST_PAD_LINK_OK) {
2038     GST_OBJECT_UNLOCK (sinkpad);
2039     GST_OBJECT_UNLOCK (srcpad);
2041     /* fire off a signal to each of the pads telling them
2042      * that they've been linked */
2043     g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2044     g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2046     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2047         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2048   } else {
2049     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2050         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2052     GST_PAD_PEER (srcpad) = NULL;
2053     GST_PAD_PEER (sinkpad) = NULL;
2055     GST_OBJECT_UNLOCK (sinkpad);
2056     GST_OBJECT_UNLOCK (srcpad);
2057   }
2059 done:
2060   if (parent) {
2061     gst_element_post_message (parent,
2062         gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2063             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2064     gst_object_unref (parent);
2065   }
2067   return result;
2070 static void
2071 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2073   GstPadTemplate **template_p;
2075   /* this function would need checks if it weren't static */
2077   GST_OBJECT_LOCK (pad);
2078   template_p = &pad->padtemplate;
2079   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2080   GST_OBJECT_UNLOCK (pad);
2082   if (templ)
2083     gst_pad_template_pad_created (templ, pad);
2086 /**
2087  * gst_pad_get_pad_template:
2088  * @pad: a #GstPad.
2089  *
2090  * Gets the template for @pad.
2091  *
2092  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2093  * if this pad has no template.
2094  *
2095  * FIXME: currently returns an unrefcounted padtemplate.
2096  */
2097 GstPadTemplate *
2098 gst_pad_get_pad_template (GstPad * pad)
2100   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2102   return GST_PAD_PAD_TEMPLATE (pad);
2106 /* should be called with the pad LOCK held */
2107 /* refs the caps, so caller is responsible for getting it unreffed */
2108 static GstCaps *
2109 gst_pad_get_caps_unlocked (GstPad * pad)
2111   GstCaps *result = NULL;
2112   GstPadTemplate *templ;
2114   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2116   if (GST_PAD_GETCAPSFUNC (pad)) {
2117     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2118         "dispatching to pad getcaps function");
2120     GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2121     GST_OBJECT_UNLOCK (pad);
2122     result = GST_PAD_GETCAPSFUNC (pad) (pad);
2123     GST_OBJECT_LOCK (pad);
2124     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2126     if (result == NULL) {
2127       g_critical ("pad %s:%s returned NULL caps from getcaps function",
2128           GST_DEBUG_PAD_NAME (pad));
2129     } else {
2130       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2131           "pad getcaps returned %" GST_PTR_FORMAT, result);
2132 #ifndef G_DISABLE_ASSERT
2133       /* check that the returned caps are a real subset of the template caps */
2134       if (GST_PAD_PAD_TEMPLATE (pad)) {
2135         const GstCaps *templ_caps =
2136             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2137         if (!gst_caps_is_subset (result, templ_caps)) {
2138           GstCaps *temp;
2140           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2141               "pad returned caps %" GST_PTR_FORMAT
2142               " which are not a real subset of its template caps %"
2143               GST_PTR_FORMAT, result, templ_caps);
2144           g_warning
2145               ("pad %s:%s returned caps which are not a real "
2146               "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2147           temp = gst_caps_intersect (templ_caps, result);
2148           gst_caps_unref (result);
2149           result = temp;
2150         }
2151       }
2152 #endif
2153       goto done;
2154     }
2155   }
2156   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2157     result = GST_PAD_TEMPLATE_CAPS (templ);
2158     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2159         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2160         result);
2162     result = gst_caps_ref (result);
2163     goto done;
2164   }
2165   if ((result = GST_PAD_CAPS (pad))) {
2166     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2167         "using pad caps %p %" GST_PTR_FORMAT, result, result);
2169     result = gst_caps_ref (result);
2170     goto done;
2171   }
2173   /* this almost never happens */
2174   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2175   result = gst_caps_new_empty ();
2177 done:
2178   return result;
2181 /* FIXME-0.11: what about making this the default and using
2182  * gst_caps_make_writable() explicitely where needed
2183  */
2184 /**
2185  * gst_pad_get_caps_refed:
2186  * @pad: a  #GstPad to get the capabilities of.
2187  *
2188  * Gets the capabilities this pad can produce or consume. Preferred function if
2189  * one only wants to read or intersect the caps.
2190  *
2191  * Returns: the caps of the pad with incremented ref-count.
2192  *
2193  * Since: 0.10.26
2194  */
2195 GstCaps *
2196 gst_pad_get_caps_refed (GstPad * pad)
2198   GstCaps *result = NULL;
2200   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2202   GST_OBJECT_LOCK (pad);
2204   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2206   result = gst_pad_get_caps_unlocked (pad);
2208   GST_OBJECT_UNLOCK (pad);
2210   return result;
2213 /**
2214  * gst_pad_get_caps:
2215  * @pad: a  #GstPad to get the capabilities of.
2216  *
2217  * Gets the capabilities this pad can produce or consume.
2218  * Note that this method doesn't necessarily return the caps set by
2219  * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2220  * gst_pad_get_caps returns all possible caps a pad can operate with, using
2221  * the pad's get_caps function;
2222  * this returns the pad template caps if not explicitly set.
2223  *
2224  * Returns: a newly allocated copy of the #GstCaps of this pad.
2225  *
2226  * MT safe.
2227  */
2228 GstCaps *
2229 gst_pad_get_caps (GstPad * pad)
2231   GstCaps *result = gst_pad_get_caps_refed (pad);
2233   /* be sure that we have a copy */
2234   if (G_LIKELY (result))
2235     result = gst_caps_make_writable (result);
2237   return result;
2240 /* FIXME-0.11: what about making this the default and using
2241  * gst_caps_make_writable() explicitely where needed
2242  */
2243 /**
2244  * gst_pad_peer_get_caps_refed:
2245  * @pad: a  #GstPad to get the capabilities of.
2246  *
2247  * Gets the capabilities of the peer connected to this pad. Preferred function
2248  * if one only wants to read or intersect the caps.
2249  *
2250  * Returns: the caps of the pad with incremented ref-count.
2251  *
2252  * Since: 0.10.26
2253  */
2254 GstCaps *
2255 gst_pad_peer_get_caps_refed (GstPad * pad)
2257   GstPad *peerpad;
2258   GstCaps *result = NULL;
2260   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2262   GST_OBJECT_LOCK (pad);
2264   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2266   peerpad = GST_PAD_PEER (pad);
2267   if (G_UNLIKELY (peerpad == NULL))
2268     goto no_peer;
2270   gst_object_ref (peerpad);
2271   GST_OBJECT_UNLOCK (pad);
2273   result = gst_pad_get_caps_refed (peerpad);
2275   gst_object_unref (peerpad);
2277   return result;
2279 no_peer:
2280   {
2281     GST_OBJECT_UNLOCK (pad);
2282     return NULL;
2283   }
2286 /**
2287  * gst_pad_peer_get_caps:
2288  * @pad: a  #GstPad to get the peer capabilities of.
2289  *
2290  * Gets the capabilities of the peer connected to this pad. Similar to
2291  * gst_pad_get_caps().
2292  *
2293  * Returns: a newly allocated copy of the #GstCaps of the peer pad. Use
2294  * gst_caps_unref() to get rid of it. This function returns %NULL if there is
2295  * no peer pad.
2296  */
2297 GstCaps *
2298 gst_pad_peer_get_caps (GstPad * pad)
2300   GstPad *peerpad;
2301   GstCaps *result = NULL;
2303   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2305   GST_OBJECT_LOCK (pad);
2307   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2309   peerpad = GST_PAD_PEER (pad);
2310   if (G_UNLIKELY (peerpad == NULL))
2311     goto no_peer;
2313   gst_object_ref (peerpad);
2314   GST_OBJECT_UNLOCK (pad);
2316   result = gst_pad_get_caps (peerpad);
2318   gst_object_unref (peerpad);
2320   return result;
2322 no_peer:
2323   {
2324     GST_OBJECT_UNLOCK (pad);
2325     return NULL;
2326   }
2329 static gboolean
2330 fixate_value (GValue * dest, const GValue * src)
2332   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2333     g_value_init (dest, G_TYPE_INT);
2334     g_value_set_int (dest, gst_value_get_int_range_min (src));
2335   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2336     g_value_init (dest, G_TYPE_DOUBLE);
2337     g_value_set_double (dest, gst_value_get_double_range_min (src));
2338   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2339     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2340   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2341     GValue temp = { 0 };
2343     /* list could be empty */
2344     if (gst_value_list_get_size (src) <= 0)
2345       return FALSE;
2347     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2349     if (!fixate_value (dest, &temp))
2350       gst_value_init_and_copy (dest, &temp);
2351     g_value_unset (&temp);
2352   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2353     gboolean res = FALSE;
2354     guint n, len;
2356     len = gst_value_array_get_size (src);
2357     g_value_init (dest, GST_TYPE_ARRAY);
2358     for (n = 0; n < len; n++) {
2359       GValue kid = { 0 };
2360       const GValue *orig_kid = gst_value_array_get_value (src, n);
2362       if (!fixate_value (&kid, orig_kid))
2363         gst_value_init_and_copy (&kid, orig_kid);
2364       else
2365         res = TRUE;
2366       gst_value_array_append_value (dest, &kid);
2367       g_value_unset (&kid);
2368     }
2370     if (!res)
2371       g_value_unset (dest);
2373     return res;
2374   } else {
2375     return FALSE;
2376   }
2378   return TRUE;
2381 static gboolean
2382 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2384   GstStructure *s = data;
2385   GValue v = { 0 };
2387   if (fixate_value (&v, value)) {
2388     gst_structure_id_set_value (s, field_id, &v);
2389     g_value_unset (&v);
2390   }
2392   return TRUE;
2395 /**
2396  * gst_pad_fixate_caps:
2397  * @pad: a  #GstPad to fixate
2398  * @caps: the  #GstCaps to fixate
2399  *
2400  * Fixate a caps on the given pad. Modifies the caps in place, so you should
2401  * make sure that the caps are actually writable (see gst_caps_make_writable()).
2402  */
2403 void
2404 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2406   GstPadFixateCapsFunction fixatefunc;
2407   guint n, len;
2409   g_return_if_fail (GST_IS_PAD (pad));
2410   g_return_if_fail (caps != NULL);
2412   if (gst_caps_is_fixed (caps))
2413     return;
2415   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2416   if (fixatefunc) {
2417     fixatefunc (pad, caps);
2418   }
2420   /* default fixation */
2421   len = gst_caps_get_size (caps);
2422   for (n = 0; n < len; n++) {
2423     GstStructure *s = gst_caps_get_structure (caps, n);
2425     gst_structure_foreach (s, gst_pad_default_fixate, s);
2426   }
2429 /* Default accept caps implementation just checks against
2430  * against the allowed caps for the pad */
2431 static gboolean
2432 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2434   /* get the caps and see if it intersects to something not empty */
2435   GstCaps *allowed;
2436   gboolean result = FALSE;
2438   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2440   allowed = gst_pad_get_caps_refed (pad);
2441   if (!allowed)
2442     goto nothing_allowed;
2444   GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2446   result = gst_caps_can_intersect (allowed, caps);
2448   gst_caps_unref (allowed);
2450   return result;
2452   /* ERRORS */
2453 nothing_allowed:
2454   {
2455     GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2456     return FALSE;
2457   }
2460 /**
2461  * gst_pad_accept_caps:
2462  * @pad: a #GstPad to check
2463  * @caps: a #GstCaps to check on the pad
2464  *
2465  * Check if the given pad accepts the caps.
2466  *
2467  * Returns: TRUE if the pad can accept the caps.
2468  */
2469 gboolean
2470 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2472   gboolean result;
2473   GstPadAcceptCapsFunction acceptfunc;
2474   GstCaps *existing = NULL;
2476   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2478   /* any pad can be unnegotiated */
2479   if (caps == NULL)
2480     return TRUE;
2482   /* lock for checking the existing caps */
2483   GST_OBJECT_LOCK (pad);
2484   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2485   /* The current caps on a pad are trivially acceptable */
2486   if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2487     if (caps == existing || gst_caps_is_equal (caps, existing))
2488       goto is_same_caps;
2489   }
2490   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2491   GST_OBJECT_UNLOCK (pad);
2493   if (G_LIKELY (acceptfunc)) {
2494     /* we can call the function */
2495     result = acceptfunc (pad, caps);
2496     GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2497   } else {
2498     /* Only null if the element explicitly unset it */
2499     result = gst_pad_acceptcaps_default (pad, caps);
2500     GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2501   }
2502   return result;
2504 is_same_caps:
2505   {
2506     GST_DEBUG_OBJECT (pad, "pad had same caps");
2507     GST_OBJECT_UNLOCK (pad);
2508     return TRUE;
2509   }
2512 /**
2513  * gst_pad_peer_accept_caps:
2514  * @pad: a  #GstPad to check the peer of
2515  * @caps: a #GstCaps to check on the pad
2516  *
2517  * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2518  * returns TRUE.
2519  *
2520  * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2521  */
2522 gboolean
2523 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2525   GstPad *peerpad;
2526   gboolean result;
2528   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2530   GST_OBJECT_LOCK (pad);
2532   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2534   peerpad = GST_PAD_PEER (pad);
2535   if (G_UNLIKELY (peerpad == NULL))
2536     goto no_peer;
2538   gst_object_ref (peerpad);
2539   /* release lock before calling external methods but keep ref to pad */
2540   GST_OBJECT_UNLOCK (pad);
2542   result = gst_pad_accept_caps (peerpad, caps);
2544   gst_object_unref (peerpad);
2546   return result;
2548 no_peer:
2549   {
2550     GST_OBJECT_UNLOCK (pad);
2551     return TRUE;
2552   }
2555 /**
2556  * gst_pad_set_caps:
2557  * @pad: a  #GstPad to set the capabilities of.
2558  * @caps: a #GstCaps to set.
2559  *
2560  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2561  * caps on the pad will be unreffed. This function refs the caps so you should
2562  * unref if as soon as you don't need it anymore.
2563  * It is possible to set NULL caps, which will make the pad unnegotiated
2564  * again.
2565  *
2566  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2567  * or bad parameters were provided to this function.
2568  *
2569  * MT safe.
2570  */
2571 gboolean
2572 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2574   GstPadSetCapsFunction setcaps;
2575   GstCaps *existing;
2577   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2578   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2580   GST_OBJECT_LOCK (pad);
2581   existing = GST_PAD_CAPS (pad);
2582   if (existing == caps)
2583     goto was_ok;
2585   if (gst_caps_is_equal (caps, existing))
2586     goto setting_same_caps;
2588   setcaps = GST_PAD_SETCAPSFUNC (pad);
2590   /* call setcaps function to configure the pad only if the
2591    * caps is not NULL */
2592   if (setcaps != NULL && caps) {
2593     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2594       GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2595       GST_OBJECT_UNLOCK (pad);
2596       if (!setcaps (pad, caps))
2597         goto could_not_set;
2598       GST_OBJECT_LOCK (pad);
2599       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2600     } else {
2601       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2602     }
2603   }
2605   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2606   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2607       caps);
2608   GST_OBJECT_UNLOCK (pad);
2610   g_object_notify (G_OBJECT (pad), "caps");
2612   return TRUE;
2614 was_ok:
2615   {
2616     GST_OBJECT_UNLOCK (pad);
2617     return TRUE;
2618   }
2619 setting_same_caps:
2620   {
2621     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2622     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2623         "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2624         caps);
2625     GST_OBJECT_UNLOCK (pad);
2626     return TRUE;
2627   }
2629   /* ERRORS */
2630 could_not_set:
2631   {
2632     GST_OBJECT_LOCK (pad);
2633     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2634     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2635         "caps %" GST_PTR_FORMAT " could not be set", caps);
2636     GST_OBJECT_UNLOCK (pad);
2638     return FALSE;
2639   }
2642 static gboolean
2643 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2645   gboolean res;
2647   /* See if pad accepts the caps */
2648   if (!gst_pad_accept_caps (pad, caps))
2649     goto not_accepted;
2651   /* set caps on pad if call succeeds */
2652   res = gst_pad_set_caps (pad, caps);
2653   /* no need to unref the caps here, set_caps takes a ref and
2654    * our ref goes away when we leave this function. */
2656   return res;
2658 not_accepted:
2659   {
2660     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2661         "caps %" GST_PTR_FORMAT " not accepted", caps);
2662     return FALSE;
2663   }
2666 /* returns TRUE if the src pad could be configured to accept the given caps */
2667 static gboolean
2668 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2670   gboolean res;
2672   if (dosetcaps) {
2673     /* See if pad accepts the caps */
2674     if (!gst_pad_accept_caps (pad, caps))
2675       goto not_accepted;
2677     res = gst_pad_set_caps (pad, caps);
2678   } else {
2679     res = TRUE;
2680   }
2681   return res;
2683 not_accepted:
2684   {
2685     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2686         "caps %" GST_PTR_FORMAT " not accepted", caps);
2687     return FALSE;
2688   }
2691 /**
2692  * gst_pad_get_pad_template_caps:
2693  * @pad: a #GstPad to get the template capabilities from.
2694  *
2695  * Gets the capabilities for @pad's template.
2696  *
2697  * Returns: the #GstCaps of this pad template. If you intend to keep a
2698  * reference on the caps, make a copy (see gst_caps_copy ()).
2699  */
2700 const GstCaps *
2701 gst_pad_get_pad_template_caps (GstPad * pad)
2703   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2705   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2707   if (GST_PAD_PAD_TEMPLATE (pad))
2708     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2710   return gst_static_caps_get (&anycaps);
2713 /**
2714  * gst_pad_get_peer:
2715  * @pad: a #GstPad to get the peer of.
2716  *
2717  * Gets the peer of @pad. This function refs the peer pad so
2718  * you need to unref it after use.
2719  *
2720  * Returns: the peer #GstPad. Unref after usage.
2721  *
2722  * MT safe.
2723  */
2724 GstPad *
2725 gst_pad_get_peer (GstPad * pad)
2727   GstPad *result;
2729   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2731   GST_OBJECT_LOCK (pad);
2732   result = GST_PAD_PEER (pad);
2733   if (result)
2734     gst_object_ref (result);
2735   GST_OBJECT_UNLOCK (pad);
2737   return result;
2740 /**
2741  * gst_pad_get_allowed_caps:
2742  * @pad: a #GstPad.
2743  *
2744  * Gets the capabilities of the allowed media types that can flow through
2745  * @pad and its peer.
2746  *
2747  * The allowed capabilities is calculated as the intersection of the results of
2748  * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2749  * on the resulting caps.
2750  *
2751  * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2752  * longer need it. This function returns NULL when @pad has no peer.
2753  *
2754  * MT safe.
2755  */
2756 GstCaps *
2757 gst_pad_get_allowed_caps (GstPad * pad)
2759   GstCaps *mycaps;
2760   GstCaps *caps;
2761   GstCaps *peercaps;
2762   GstPad *peer;
2764   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2766   GST_OBJECT_LOCK (pad);
2768   peer = GST_PAD_PEER (pad);
2769   if (G_UNLIKELY (peer == NULL))
2770     goto no_peer;
2772   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2774   gst_object_ref (peer);
2775   GST_OBJECT_UNLOCK (pad);
2776   mycaps = gst_pad_get_caps_refed (pad);
2778   peercaps = gst_pad_get_caps_refed (peer);
2779   gst_object_unref (peer);
2781   caps = gst_caps_intersect (mycaps, peercaps);
2782   gst_caps_unref (peercaps);
2783   gst_caps_unref (mycaps);
2785   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2786       caps);
2788   return caps;
2790 no_peer:
2791   {
2792     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2793     GST_OBJECT_UNLOCK (pad);
2795     return NULL;
2796   }
2799 /**
2800  * gst_pad_get_negotiated_caps:
2801  * @pad: a #GstPad.
2802  *
2803  * Gets the capabilities of the media type that currently flows through @pad
2804  * and its peer.
2805  *
2806  * This function can be used on both src and sinkpads. Note that srcpads are
2807  * always negotiated before sinkpads so it is possible that the negotiated caps
2808  * on the srcpad do not match the negotiated caps of the peer.
2809  *
2810  * Returns: the negotiated #GstCaps of the pad link.  Unref the caps when
2811  * you no longer need it. This function returns NULL when the @pad has no
2812  * peer or is not negotiated yet.
2813  *
2814  * MT safe.
2815  */
2816 GstCaps *
2817 gst_pad_get_negotiated_caps (GstPad * pad)
2819   GstCaps *caps;
2820   GstPad *peer;
2822   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2824   GST_OBJECT_LOCK (pad);
2826   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2827     goto no_peer;
2829   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2831   caps = GST_PAD_CAPS (pad);
2832   if (caps)
2833     gst_caps_ref (caps);
2834   GST_OBJECT_UNLOCK (pad);
2836   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2837       caps);
2839   return caps;
2841 no_peer:
2842   {
2843     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2844     GST_OBJECT_UNLOCK (pad);
2846     return NULL;
2847   }
2850 /* calls the buffer_alloc function on the given pad */
2851 static GstFlowReturn
2852 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2853     GstCaps * caps, GstBuffer ** buf)
2855   GstFlowReturn ret;
2856   GstPadBufferAllocFunction bufferallocfunc;
2858   GST_OBJECT_LOCK (pad);
2859   /* when the pad is flushing we cannot give a buffer */
2860   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2861     goto flushing;
2863   bufferallocfunc = pad->bufferallocfunc;
2865   if (offset == GST_BUFFER_OFFSET_NONE) {
2866     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2867         "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2868         GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2869   } else {
2870     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2871         "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2872         G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2873         bufferallocfunc, size, offset);
2874   }
2875   GST_OBJECT_UNLOCK (pad);
2877   /* G_LIKELY for now since most elements don't implement a buffer alloc
2878    * function and there is no default alloc proxy function as this is usually
2879    * not possible. */
2880   if (G_LIKELY (bufferallocfunc == NULL))
2881     goto fallback;
2883   ret = bufferallocfunc (pad, offset, size, caps, buf);
2885   if (G_UNLIKELY (ret != GST_FLOW_OK))
2886     goto error;
2888   /* no error, but NULL buffer means fallback to the default */
2889   if (G_UNLIKELY (*buf == NULL))
2890     goto fallback;
2892   /* If the buffer alloc function didn't set up the caps like it should,
2893    * do it for it */
2894   if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2895     GST_WARNING_OBJECT (pad,
2896         "Buffer allocation function did not set caps. Setting");
2897     gst_buffer_set_caps (*buf, caps);
2898   }
2899   return ret;
2901 flushing:
2902   {
2903     /* pad was flushing */
2904     GST_OBJECT_UNLOCK (pad);
2905     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2906     return GST_FLOW_WRONG_STATE;
2907   }
2908 error:
2909   {
2910     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2911         "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2912     return ret;
2913   }
2914 fallback:
2915   {
2916     /* fallback case, allocate a buffer of our own, add pad caps. */
2917     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2919     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2920       GST_BUFFER_OFFSET (*buf) = offset;
2921       gst_buffer_set_caps (*buf, caps);
2922       return GST_FLOW_OK;
2923     } else {
2924       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2925           "out of memory allocating %d bytes", size);
2926       return GST_FLOW_ERROR;
2927     }
2928   }
2931 /* FIXME 0.11: size should be unsigned */
2932 static GstFlowReturn
2933 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2934     GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
2936   GstPad *peer;
2937   GstFlowReturn ret;
2938   GstCaps *newcaps;
2939   gboolean caps_changed;
2941   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2942   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2943   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2944   g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
2946   GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
2947       GST_PTR_FORMAT, offset, size, caps);
2949   GST_OBJECT_LOCK (pad);
2950   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2951     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
2952       goto flushed;
2954   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2955     goto no_peer;
2957   gst_object_ref (peer);
2958   GST_OBJECT_UNLOCK (pad);
2960   ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
2961   gst_object_unref (peer);
2963   if (G_UNLIKELY (ret != GST_FLOW_OK))
2964     goto peer_error;
2966   /* FIXME, move capnego this into a base class? */
2967   newcaps = GST_BUFFER_CAPS (*buf);
2969   /* Lock for checking caps, pretty pointless as the _pad_push() function might
2970    * change it concurrently, one of the problems with automatic caps setting in
2971    * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
2972    * when there is heavy renegotiation going on in both directions. */
2973   GST_OBJECT_LOCK (pad);
2974   caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
2975   GST_OBJECT_UNLOCK (pad);
2977   /* we got a new datatype on the pad, see if it can handle it */
2978   if (G_UNLIKELY (caps_changed)) {
2979     GST_DEBUG_OBJECT (pad,
2980         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
2981         GST_PAD_CAPS (pad), newcaps, newcaps);
2982     if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
2983       goto not_negotiated;
2984   }
2986   /* sanity check (only if caps are the same) */
2987   if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
2988     goto wrong_size_fallback;
2990   return ret;
2992 flushed:
2993   {
2994     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
2995     GST_OBJECT_UNLOCK (pad);
2996     return ret;
2997   }
2998 no_peer:
2999   {
3000     /* pad has no peer */
3001     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3002         "called bufferallocfunc but had no peer");
3003     GST_OBJECT_UNLOCK (pad);
3004     return GST_FLOW_NOT_LINKED;
3005   }
3006 peer_error:
3007   {
3008     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3009         "alloc function returned error %s", gst_flow_get_name (ret));
3010     return ret;
3011   }
3012 not_negotiated:
3013   {
3014     gst_buffer_unref (*buf);
3015     *buf = NULL;
3016     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3017         "alloc function returned unacceptable buffer");
3018     return GST_FLOW_NOT_NEGOTIATED;
3019   }
3020 wrong_size_fallback:
3021   {
3022     GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3023         "function is too small (%u < %d), doing fallback buffer alloc",
3024         GST_BUFFER_SIZE (*buf), size);
3026     gst_buffer_unref (*buf);
3028     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3029       GST_BUFFER_OFFSET (*buf) = offset;
3030       gst_buffer_set_caps (*buf, caps);
3031       return GST_FLOW_OK;
3032     } else {
3033       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3034           "out of memory allocating %d bytes", size);
3035       return GST_FLOW_ERROR;
3036     }
3037   }
3040 /**
3041  * gst_pad_alloc_buffer:
3042  * @pad: a source #GstPad
3043  * @offset: the offset of the new buffer in the stream
3044  * @size: the size of the new buffer
3045  * @caps: the caps of the new buffer
3046  * @buf: a newly allocated buffer
3047  *
3048  * Allocates a new, empty buffer optimized to push to pad @pad.  This
3049  * function only works if @pad is a source pad and has a peer.
3050  *
3051  * A new, empty #GstBuffer will be put in the @buf argument.
3052  * You need to check the caps of the buffer after performing this
3053  * function and renegotiate to the format if needed. If the caps changed, it is
3054  * possible that the buffer returned in @buf is not of the right size for the
3055  * new format, @buf needs to be unreffed and reallocated if this is the case.
3056  *
3057  * Returns: a result code indicating success of the operation. Any
3058  * result code other than #GST_FLOW_OK is an error and @buf should
3059  * not be used.
3060  * An error can occur if the pad is not connected or when the downstream
3061  * peer elements cannot provide an acceptable buffer.
3062  *
3063  * MT safe.
3064  */
3066 /* FIXME 0.11: size should be unsigned */
3067 GstFlowReturn
3068 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3069     GstBuffer ** buf)
3071   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3074 /**
3075  * gst_pad_alloc_buffer_and_set_caps:
3076  * @pad: a source #GstPad
3077  * @offset: the offset of the new buffer in the stream
3078  * @size: the size of the new buffer
3079  * @caps: the caps of the new buffer
3080  * @buf: a newly allocated buffer
3081  *
3082  * In addition to the function gst_pad_alloc_buffer(), this function
3083  * automatically calls gst_pad_set_caps() when the caps of the
3084  * newly allocated buffer are different from the @pad caps.
3085  *
3086  * After a renegotiation, the size of the new buffer returned in @buf could
3087  * be of the wrong size for the new format and must be unreffed an reallocated
3088  * in that case.
3089  *
3090  * Returns: a result code indicating success of the operation. Any
3091  * result code other than #GST_FLOW_OK is an error and @buf should
3092  * not be used.
3093  * An error can occur if the pad is not connected or when the downstream
3094  * peer elements cannot provide an acceptable buffer.
3095  *
3096  * MT safe.
3097  */
3099 /* FIXME 0.11: size should be unsigned */
3100 GstFlowReturn
3101 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3102     GstCaps * caps, GstBuffer ** buf)
3104   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3108 #ifndef GST_REMOVE_DEPRECATED
3109 typedef struct
3111   GList *list;
3112   guint32 cookie;
3113 } IntLinkIterData;
3115 static void
3116 int_link_iter_data_free (IntLinkIterData * data)
3118   g_list_free (data->list);
3119   g_free (data);
3121 #endif
3123 static GstIteratorItem
3124 iterate_pad (GstIterator * it, GstPad * pad)
3126   gst_object_ref (pad);
3127   return GST_ITERATOR_ITEM_PASS;
3130 /**
3131  * gst_pad_iterate_internal_links_default:
3132  * @pad: the #GstPad to get the internal links of.
3133  *
3134  * Iterate the list of pads to which the given pad is linked to inside of
3135  * the parent element.
3136  * This is the default handler, and thus returns an iterator of all of the
3137  * pads inside the parent element with opposite direction.
3138  *
3139  * The caller must free this iterator after use with gst_iterator_free().
3140  *
3141  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3142  * returned pad with gst_object_unref().
3143  *
3144  * Since: 0.10.21
3145  */
3146 GstIterator *
3147 gst_pad_iterate_internal_links_default (GstPad * pad)
3149   GstIterator *res;
3150   GList **padlist;
3151   guint32 *cookie;
3152   GMutex *lock;
3153   gpointer owner;
3154   GstIteratorDisposeFunction dispose;
3156   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3158 #ifndef GST_REMOVE_DEPRECATED
3159   /* when we get here, the default handler for the iterate links is called,
3160    * which means that the user has not installed a custom one. We first check if
3161    * there is maybe a custom legacy function we can call. */
3162   if (GST_PAD_INTLINKFUNC (pad) &&
3163       GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3164     IntLinkIterData *data;
3166     /* make an iterator for the list. We can't protect the list with a
3167      * cookie. If we would take the cookie of the parent element, we need to
3168      * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3169      * cache the per-pad list and invalidate the list when a new call to
3170      * INTLINKFUNC() returned a different list but then this would only work if
3171      * two concurrent iterators were used and the last iterator would still be
3172      * thread-unsafe. Just don't use this method anymore. */
3173     data = g_new0 (IntLinkIterData, 1);
3174     data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3175     data->cookie = 0;
3177     GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3179     cookie = &data->cookie;
3180     padlist = &data->list;
3181     owner = data;
3182     dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3183     /* reuse the pad lock, it's all we have here */
3184     lock = GST_OBJECT_GET_LOCK (pad);
3185   } else
3186 #endif
3187   {
3188     GstElement *parent;
3190     GST_OBJECT_LOCK (pad);
3191     parent = GST_PAD_PARENT (pad);
3192     if (!parent || !GST_IS_ELEMENT (parent))
3193       goto no_parent;
3195     gst_object_ref (parent);
3196     GST_OBJECT_UNLOCK (pad);
3198     if (pad->direction == GST_PAD_SRC)
3199       padlist = &parent->sinkpads;
3200     else
3201       padlist = &parent->srcpads;
3203     GST_DEBUG_OBJECT (pad, "Making iterator");
3205     cookie = &parent->pads_cookie;
3206     owner = parent;
3207     dispose = (GstIteratorDisposeFunction) gst_object_unref;
3208     lock = GST_OBJECT_GET_LOCK (parent);
3209   }
3211   res = gst_iterator_new_list (GST_TYPE_PAD,
3212       lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3213       dispose);
3215   return res;
3217   /* ERRORS */
3218 no_parent:
3219   {
3220     GST_OBJECT_UNLOCK (pad);
3221     GST_DEBUG_OBJECT (pad, "no parent element");
3222     return NULL;
3223   }
3226 /**
3227  * gst_pad_iterate_internal_links:
3228  * @pad: the GstPad to get the internal links of.
3229  *
3230  * Gets an iterator for the pads to which the given pad is linked to inside
3231  * of the parent element.
3232  *
3233  * Each #GstPad element yielded by the iterator will have its refcount increased,
3234  * so unref after use.
3235  *
3236  * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3237  * iterator function configured. Use gst_iterator_free() after usage.
3238  *
3239  * Since: 0.10.21
3240  */
3241 GstIterator *
3242 gst_pad_iterate_internal_links (GstPad * pad)
3244   GstIterator *res = NULL;
3246   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3248   if (GST_PAD_ITERINTLINKFUNC (pad))
3249     res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3251   return res;
3254 #ifndef GST_REMOVE_DEPRECATED
3255 static void
3256 add_unref_pad_to_list (GstPad * pad, GList * list)
3258   list = g_list_prepend (list, pad);
3259   gst_object_unref (pad);
3261 #endif
3263 /**
3264  * gst_pad_get_internal_links_default:
3265  * @pad: the #GstPad to get the internal links of.
3266  *
3267  * Gets a list of pads to which the given pad is linked to
3268  * inside of the parent element.
3269  * This is the default handler, and thus returns a list of all of the
3270  * pads inside the parent element with opposite direction.
3271  *
3272  * The caller must free this list after use with g_list_free().
3273  *
3274  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3275  *
3276  * Not MT safe.
3277  *
3278  * Deprecated: This function does not ref the pads in the list so that they
3279  * could become invalid by the time the application accesses them. It's also
3280  * possible that the list changes while handling the pads, which the caller of
3281  * this function is unable to know. Use the thread-safe 
3282  * gst_pad_iterate_internal_links_default() instead.
3283  */
3284 #ifndef GST_REMOVE_DEPRECATED
3285 GList *
3286 gst_pad_get_internal_links_default (GstPad * pad)
3288   GList *res = NULL;
3289   GstElement *parent;
3291   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3293   GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3295   /* when we get here, the default handler for get_internal_links is called,
3296    * which means that the user has not installed a custom one. We first check if
3297    * there is maybe a custom iterate function we can call. */
3298   if (GST_PAD_ITERINTLINKFUNC (pad) &&
3299       GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3300     GstIterator *it;
3301     GstIteratorResult ires;
3302     gboolean done = FALSE;
3304     it = gst_pad_iterate_internal_links (pad);
3305     /* loop over the iterator and put all elements into a list, we also
3306      * immediatly unref them, which is bad. */
3307     do {
3308       ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, res);
3309       switch (ires) {
3310         case GST_ITERATOR_OK:
3311         case GST_ITERATOR_DONE:
3312         case GST_ITERATOR_ERROR:
3313           done = TRUE;
3314           break;
3315         case GST_ITERATOR_RESYNC:
3316           /* restart, discard previous list */
3317           gst_iterator_resync (it);
3318           g_list_free (res);
3319           res = NULL;
3320           break;
3321       }
3322     } while (!done);
3324     gst_iterator_free (it);
3325   } else {
3326     /* lock pad, check and ref parent */
3327     GST_OBJECT_LOCK (pad);
3328     parent = GST_PAD_PARENT (pad);
3329     if (!parent || !GST_IS_ELEMENT (parent))
3330       goto no_parent;
3332     parent = gst_object_ref (parent);
3333     GST_OBJECT_UNLOCK (pad);
3335     /* now lock the parent while we copy the pads */
3336     GST_OBJECT_LOCK (parent);
3337     if (pad->direction == GST_PAD_SRC)
3338       res = g_list_copy (parent->sinkpads);
3339     else
3340       res = g_list_copy (parent->srcpads);
3341     GST_OBJECT_UNLOCK (parent);
3343     gst_object_unref (parent);
3344   }
3346   /* At this point pads can be changed and unreffed. Nothing we can do about it
3347    * because for compatibility reasons this function cannot ref the pads or
3348    * notify the app that the list changed. */
3350   return res;
3352 no_parent:
3353   {
3354     GST_DEBUG_OBJECT (pad, "no parent");
3355     GST_OBJECT_UNLOCK (pad);
3356     return NULL;
3357   }
3359 #endif /* GST_REMOVE_DEPRECATED */
3361 /**
3362  * gst_pad_get_internal_links:
3363  * @pad: the #GstPad to get the internal links of.
3364  *
3365  * Gets a list of pads to which the given pad is linked to
3366  * inside of the parent element.
3367  * The caller must free this list after use.
3368  *
3369  * Not MT safe.
3370  *
3371  * Returns: a newly allocated #GList of pads, free with g_list_free().
3372  * 
3373  * Deprecated: This function does not ref the pads in the list so that they
3374  * could become invalid by the time the application accesses them. It's also
3375  * possible that the list changes while handling the pads, which the caller of
3376  * this function is unable to know. Use the thread-safe 
3377  * gst_pad_iterate_internal_links() instead.
3378  */
3379 #ifndef GST_REMOVE_DEPRECATED
3380 GList *
3381 gst_pad_get_internal_links (GstPad * pad)
3383   GList *res = NULL;
3385   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3387   GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3389   if (GST_PAD_INTLINKFUNC (pad))
3390     res = GST_PAD_INTLINKFUNC (pad) (pad);
3392   return res;
3394 #endif /* GST_REMOVE_DEPRECATED */
3396 static gboolean
3397 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3399   gboolean result = FALSE;
3400   GstIterator *iter;
3401   gboolean done = FALSE;
3402   gpointer item;
3403   GstPad *eventpad;
3404   GList *pushed_pads = NULL;
3406   GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3407       event, GST_EVENT_TYPE_NAME (event));
3409   iter = gst_pad_iterate_internal_links (pad);
3411   if (!iter)
3412     goto no_iter;
3414   while (!done) {
3415     switch (gst_iterator_next (iter, &item)) {
3416       case GST_ITERATOR_OK:
3417         eventpad = GST_PAD (item);
3419         /* if already pushed,  skip */
3420         if (g_list_find (pushed_pads, eventpad)) {
3421           gst_object_unref (item);
3422           break;
3423         }
3425         if (GST_PAD_IS_SRC (eventpad)) {
3426           /* for each pad we send to, we should ref the event; it's up
3427            * to downstream to unref again when handled. */
3428           GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3429               event, GST_EVENT_TYPE_NAME (event),
3430               GST_DEBUG_PAD_NAME (eventpad));
3431           gst_event_ref (event);
3432           result |= gst_pad_push_event (eventpad, event);
3433         } else {
3434           /* we only send the event on one pad, multi-sinkpad elements
3435            * should implement a handler */
3436           GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3437               event, GST_EVENT_TYPE_NAME (event),
3438               GST_DEBUG_PAD_NAME (eventpad));
3439           result = gst_pad_push_event (eventpad, event);
3440           done = TRUE;
3441           event = NULL;
3442         }
3444         pushed_pads = g_list_prepend (pushed_pads, eventpad);
3446         gst_object_unref (item);
3447         break;
3448       case GST_ITERATOR_RESYNC:
3449         /* FIXME, if we want to reset the result value we need to remember which
3450          * pads pushed with which result */
3451         gst_iterator_resync (iter);
3452         break;
3453       case GST_ITERATOR_ERROR:
3454         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3455         done = TRUE;
3456         break;
3457       case GST_ITERATOR_DONE:
3458         done = TRUE;
3459         break;
3460     }
3461   }
3462   gst_iterator_free (iter);
3464 no_iter:
3466   /* If this is a sinkpad and we don't have pads to send the event to, we
3467    * return TRUE. This is so that when using the default handler on a sink
3468    * element, we don't fail to push it. */
3469   if (!pushed_pads)
3470     result = GST_PAD_IS_SINK (pad);
3472   g_list_free (pushed_pads);
3474   /* we handled the incoming event so we unref once */
3475   if (event) {
3476     GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3477     gst_event_unref (event);
3478   }
3480   return result;
3483 /**
3484  * gst_pad_event_default:
3485  * @pad: a #GstPad to call the default event handler on.
3486  * @event: the #GstEvent to handle.
3487  *
3488  * Invokes the default event handler for the given pad. End-of-stream and
3489  * discontinuity events are handled specially, and then the event is sent to all
3490  * pads internally linked to @pad. Note that if there are many possible sink
3491  * pads that are internally linked to @pad, only one will be sent an event.
3492  * Multi-sinkpad elements should implement custom event handlers.
3493  *
3494  * Returns: TRUE if the event was sent succesfully.
3495  */
3496 gboolean
3497 gst_pad_event_default (GstPad * pad, GstEvent * event)
3499   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3500   g_return_val_if_fail (event != NULL, FALSE);
3502   GST_LOG_OBJECT (pad, "default event handler");
3504   switch (GST_EVENT_TYPE (event)) {
3505     case GST_EVENT_EOS:
3506     {
3507       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3508       gst_pad_pause_task (pad);
3509     }
3510       /* fall thru */
3511     default:
3512       break;
3513   }
3515   return gst_pad_event_default_dispatch (pad, event);
3518 /**
3519  * gst_pad_dispatcher:
3520  * @pad: a #GstPad to dispatch.
3521  * @dispatch: the #GstDispatcherFunction to call.
3522  * @data: gpointer user data passed to the dispatcher function.
3523  *
3524  * Invokes the given dispatcher function on each respective peer of
3525  * all pads that are internally linked to the given pad.
3526  * The GstPadDispatcherFunction should return TRUE when no further pads
3527  * need to be processed.
3528  *
3529  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3530  */
3531 gboolean
3532 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3533     gpointer data)
3535   gboolean res = FALSE;
3536   GstIterator *iter = NULL;
3537   gboolean done = FALSE;
3538   gpointer item;
3540   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3541   g_return_val_if_fail (dispatch != NULL, FALSE);
3543   iter = gst_pad_iterate_internal_links (pad);
3545   if (!iter)
3546     goto no_iter;
3548   while (!done) {
3549     switch (gst_iterator_next (iter, &item)) {
3550       case GST_ITERATOR_OK:
3551       {
3552         GstPad *int_pad = GST_PAD_CAST (item);
3553         GstPad *int_peer = gst_pad_get_peer (int_pad);
3555         if (int_peer) {
3556           GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3557               GST_DEBUG_PAD_NAME (int_peer));
3558           done = res = dispatch (int_peer, data);
3559           gst_object_unref (int_peer);
3560         } else {
3561           GST_DEBUG_OBJECT (int_pad, "no peer");
3562         }
3563       }
3564         gst_object_unref (item);
3565         break;
3566       case GST_ITERATOR_RESYNC:
3567         gst_iterator_resync (iter);
3568         break;
3569       case GST_ITERATOR_ERROR:
3570         done = TRUE;
3571         GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3572         break;
3573       case GST_ITERATOR_DONE:
3574         done = TRUE;
3575         break;
3576     }
3577   }
3578   gst_iterator_free (iter);
3580   GST_DEBUG_OBJECT (pad, "done, result %d", res);
3582 no_iter:
3584   return res;
3587 /**
3588  * gst_pad_query:
3589  * @pad: a #GstPad to invoke the default query on.
3590  * @query: the #GstQuery to perform.
3591  *
3592  * Dispatches a query to a pad. The query should have been allocated by the
3593  * caller via one of the type-specific allocation functions in gstquery.h. The
3594  * element is responsible for filling the query with an appropriate response,
3595  * which should then be parsed with a type-specific query parsing function.
3596  *
3597  * Again, the caller is responsible for both the allocation and deallocation of
3598  * the query structure.
3599  *
3600  * Returns: TRUE if the query could be performed.
3601  */
3602 gboolean
3603 gst_pad_query (GstPad * pad, GstQuery * query)
3605   GstPadQueryFunction func;
3607   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3608   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3610   GST_DEBUG_OBJECT (pad, "sending query %p", query);
3612   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3613     goto no_func;
3615   return func (pad, query);
3617 no_func:
3618   {
3619     GST_DEBUG_OBJECT (pad, "had no query function");
3620     return FALSE;
3621   }
3624 /**
3625  * gst_pad_peer_query:
3626  * @pad: a #GstPad to invoke the peer query on.
3627  * @query: the #GstQuery to perform.
3628  *
3629  * Performs gst_pad_query() on the peer of @pad.
3630  *
3631  * The caller is responsible for both the allocation and deallocation of
3632  * the query structure.
3633  *
3634  * Returns: TRUE if the query could be performed. This function returns %FALSE
3635  * if @pad has no peer.
3636  *
3637  * Since: 0.10.15
3638  */
3639 gboolean
3640 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3642   GstPad *peerpad;
3643   gboolean result;
3645   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3646   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3648   GST_OBJECT_LOCK (pad);
3650   GST_DEBUG_OBJECT (pad, "peer query");
3652   peerpad = GST_PAD_PEER (pad);
3653   if (G_UNLIKELY (peerpad == NULL))
3654     goto no_peer;
3656   gst_object_ref (peerpad);
3657   GST_OBJECT_UNLOCK (pad);
3659   result = gst_pad_query (peerpad, query);
3661   gst_object_unref (peerpad);
3663   return result;
3665   /* ERRORS */
3666 no_peer:
3667   {
3668     GST_WARNING_OBJECT (pad, "pad has no peer");
3669     GST_OBJECT_UNLOCK (pad);
3670     return FALSE;
3671   }
3674 /**
3675  * gst_pad_query_default:
3676  * @pad: a #GstPad to call the default query handler on.
3677  * @query: the #GstQuery to handle.
3678  *
3679  * Invokes the default query handler for the given pad.
3680  * The query is sent to all pads internally linked to @pad. Note that
3681  * if there are many possible sink pads that are internally linked to
3682  * @pad, only one will be sent the query.
3683  * Multi-sinkpad elements should implement custom query handlers.
3684  *
3685  * Returns: TRUE if the query was performed succesfully.
3686  */
3687 gboolean
3688 gst_pad_query_default (GstPad * pad, GstQuery * query)
3690   switch (GST_QUERY_TYPE (query)) {
3691     case GST_QUERY_POSITION:
3692     case GST_QUERY_SEEKING:
3693     case GST_QUERY_FORMATS:
3694     case GST_QUERY_LATENCY:
3695     case GST_QUERY_JITTER:
3696     case GST_QUERY_RATE:
3697     case GST_QUERY_CONVERT:
3698     default:
3699       return gst_pad_dispatcher
3700           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3701   }
3704 #ifndef GST_DISABLE_LOADSAVE
3705 /* FIXME: why isn't this on a GstElement ? */
3706 /**
3707  * gst_pad_load_and_link:
3708  * @self: an #xmlNodePtr to read the description from.
3709  * @parent: the #GstObject element that owns the pad.
3710  *
3711  * Reads the pad definition from the XML node and links the given pad
3712  * in the element to a pad of an element up in the hierarchy.
3713  */
3714 void
3715 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3717   xmlNodePtr field = self->xmlChildrenNode;
3718   GstPad *pad = NULL, *targetpad;
3719   GstPadTemplate *tmpl;
3720   gchar *peer = NULL;
3721   gchar **split;
3722   GstElement *target;
3723   GstObject *grandparent;
3724   gchar *name = NULL;
3726   while (field) {
3727     if (!strcmp ((char *) field->name, "name")) {
3728       name = (gchar *) xmlNodeGetContent (field);
3729       pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3730       if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3731               && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3732         pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3733       g_free (name);
3734     } else if (!strcmp ((char *) field->name, "peer")) {
3735       peer = (gchar *) xmlNodeGetContent (field);
3736     }
3737     field = field->next;
3738   }
3739   g_return_if_fail (pad != NULL);
3741   if (peer == NULL)
3742     return;
3744   split = g_strsplit (peer, ".", 2);
3746   if (split[0] == NULL || split[1] == NULL) {
3747     GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3748         "Could not parse peer '%s', leaving unlinked", peer);
3750     g_free (peer);
3751     return;
3752   }
3753   g_free (peer);
3755   g_return_if_fail (split[0] != NULL);
3756   g_return_if_fail (split[1] != NULL);
3758   grandparent = gst_object_get_parent (parent);
3760   if (grandparent && GST_IS_BIN (grandparent)) {
3761     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3762   } else
3763     goto cleanup;
3765   if (target == NULL)
3766     goto cleanup;
3768   targetpad = gst_element_get_static_pad (target, split[1]);
3769   if (!targetpad)
3770     targetpad = gst_element_get_request_pad (target, split[1]);
3772   if (targetpad == NULL)
3773     goto cleanup;
3775   if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3776     gst_pad_link (pad, targetpad);
3777   else
3778     gst_pad_link (targetpad, pad);
3780 cleanup:
3781   g_strfreev (split);
3784 /**
3785  * gst_pad_save_thyself:
3786  * @pad: a #GstPad to save.
3787  * @parent: the parent #xmlNodePtr to save the description in.
3788  *
3789  * Saves the pad into an xml representation.
3790  *
3791  * Returns: the #xmlNodePtr representation of the pad.
3792  */
3793 static xmlNodePtr
3794 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3796   GstPad *pad;
3797   GstPad *peer;
3799   g_return_val_if_fail (GST_IS_PAD (object), NULL);
3801   pad = GST_PAD (object);
3803   xmlNewChild (parent, NULL, (xmlChar *) "name",
3804       (xmlChar *) GST_PAD_NAME (pad));
3806   if (GST_PAD_IS_SRC (pad)) {
3807     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3808   } else if (GST_PAD_IS_SINK (pad)) {
3809     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3810   } else {
3811     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3812   }
3814   if (GST_PAD_PEER (pad) != NULL) {
3815     gchar *content;
3817     peer = GST_PAD_PEER (pad);
3818     /* first check to see if the peer's parent's parent is the same */
3819     /* we just save it off */
3820     content = g_strdup_printf ("%s.%s",
3821         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3822     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3823     g_free (content);
3824   } else
3825     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3827   return parent;
3830 #if 0
3831 /**
3832  * gst_ghost_pad_save_thyself:
3833  * @pad: a ghost #GstPad to save.
3834  * @parent: the parent #xmlNodePtr to save the description in.
3835  *
3836  * Saves the ghost pad into an xml representation.
3837  *
3838  * Returns: the #xmlNodePtr representation of the pad.
3839  */
3840 xmlNodePtr
3841 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3843   xmlNodePtr self;
3845   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3847   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3848   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3849   xmlNewChild (self, NULL, (xmlChar *) "parent",
3850       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3852   /* FIXME FIXME FIXME! */
3854   return self;
3856 #endif /* 0 */
3857 #endif /* GST_DISABLE_LOADSAVE */
3859 /*
3860  * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3861  * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3862  * called.
3863  *
3864  * This function performs the pad blocking when an event, buffer push
3865  * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3866  * streaming thread after informing the pad has been blocked.
3867  *
3868  * An application can with this method wait and block any streaming
3869  * thread and perform operations such as seeking or linking.
3870  *
3871  * Two methods are available for notifying the application of the
3872  * block:
3873  * - the callback method, which happens in the STREAMING thread with
3874  *   the STREAM_LOCK held. With this method, the most useful way of
3875  *   dealing with the callback is to post a message to the main thread
3876  *   where the pad block can then be handled outside of the streaming
3877  *   thread. With the last method one can perform all operations such
3878  *   as doing a state change, linking, unblocking, seeking etc on the
3879  *   pad.
3880  * - the GCond signal method, which makes any thread unblock when
3881  *   the pad block happens.
3882  *
3883  * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3884  * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3885  *
3886  * MT safe.
3887  */
3888 static GstFlowReturn
3889 handle_pad_block (GstPad * pad)
3891   GstPadBlockCallback callback;
3892   gpointer user_data;
3893   GstFlowReturn ret = GST_FLOW_OK;
3895   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3897   /* flushing, don't bother trying to block and return WRONG_STATE
3898    * right away */
3899   if (GST_PAD_IS_FLUSHING (pad))
3900     goto flushingnonref;
3902   /* we grab an extra ref for the callbacks, this is probably not
3903    * needed (callback code does not have a ref and cannot unref). I
3904    * think this was done to make it possible to unref the element in
3905    * the callback, which is in the end totally impossible as it
3906    * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3907    * all taken when calling this function. */
3908   gst_object_ref (pad);
3910   while (GST_PAD_IS_BLOCKED (pad)) {
3911     do {
3912       /* we either have a callback installed to notify the block or
3913        * some other thread is doing a GCond wait. */
3914       callback = pad->block_callback;
3915       pad->abidata.ABI.block_callback_called = TRUE;
3916       if (callback) {
3917         /* there is a callback installed, call it. We release the
3918          * lock so that the callback can do something usefull with the
3919          * pad */
3920         user_data = pad->block_data;
3921         GST_OBJECT_UNLOCK (pad);
3922         callback (pad, TRUE, user_data);
3923         GST_OBJECT_LOCK (pad);
3925         /* we released the lock, recheck flushing */
3926         if (GST_PAD_IS_FLUSHING (pad))
3927           goto flushing;
3928       } else {
3929         /* no callback, signal the thread that is doing a GCond wait
3930          * if any. */
3931         GST_PAD_BLOCK_BROADCAST (pad);
3932       }
3933     } while (pad->abidata.ABI.block_callback_called == FALSE
3934         && GST_PAD_IS_BLOCKED (pad));
3936     /* OBJECT_LOCK could have been released when we did the callback, which
3937      * then could have made the pad unblock so we need to check the blocking
3938      * condition again.   */
3939     if (!GST_PAD_IS_BLOCKED (pad))
3940       break;
3942     /* now we block the streaming thread. It can be unlocked when we
3943      * deactivate the pad (which will also set the FLUSHING flag) or
3944      * when the pad is unblocked. A flushing event will also unblock
3945      * the pad after setting the FLUSHING flag. */
3946     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3947         "Waiting to be unblocked or set flushing");
3948     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3949     GST_PAD_BLOCK_WAIT (pad);
3950     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3952     /* see if we got unblocked by a flush or not */
3953     if (GST_PAD_IS_FLUSHING (pad))
3954       goto flushing;
3955   }
3957   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3959   /* when we get here, the pad is unblocked again and we perform
3960    * the needed unblock code. */
3961   callback = pad->block_callback;
3962   if (callback) {
3963     /* we need to call the callback */
3964     user_data = pad->block_data;
3965     GST_OBJECT_UNLOCK (pad);
3966     callback (pad, FALSE, user_data);
3967     GST_OBJECT_LOCK (pad);
3968   } else {
3969     /* we need to signal the thread waiting on the GCond */
3970     GST_PAD_BLOCK_BROADCAST (pad);
3971   }
3973   gst_object_unref (pad);
3975   return ret;
3977 flushingnonref:
3978   {
3979     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3980     return GST_FLOW_WRONG_STATE;
3981   }
3982 flushing:
3983   {
3984     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3985     gst_object_unref (pad);
3986     return GST_FLOW_WRONG_STATE;
3987   }
3990 /**********************************************************************
3991  * Data passing functions
3992  */
3994 static gboolean
3995 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3997   GValue ret = { 0 };
3998   GValue args[2] = { {0}, {0} };
3999   gboolean res;
4000   GQuark detail;
4002   /* init */
4003   g_value_init (&ret, G_TYPE_BOOLEAN);
4004   g_value_set_boolean (&ret, TRUE);
4005   g_value_init (&args[0], GST_TYPE_PAD);
4006   g_value_set_object (&args[0], pad);
4007   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4008   gst_value_set_mini_object (&args[1], obj);
4010   if (GST_IS_EVENT (obj))
4011     detail = event_quark;
4012   else
4013     detail = buffer_quark;
4015   /* actually emit */
4016   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4017   res = g_value_get_boolean (&ret);
4019   /* clean up */
4020   g_value_unset (&ret);
4021   g_value_unset (&args[0]);
4022   g_value_unset (&args[1]);
4024   return res;
4027 static void
4028 gst_pad_data_unref (gboolean is_buffer, void *data)
4030   if (G_LIKELY (is_buffer)) {
4031     gst_buffer_unref (data);
4032   } else {
4033     gst_buffer_list_unref (data);
4034   }
4037 static GstCaps *
4038 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4040   GstCaps *caps;
4042   if (G_LIKELY (is_buffer)) {
4043     caps = GST_BUFFER_CAPS (data);
4044   } else {
4045     GstBuffer *buf;
4047     if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4048       caps = GST_BUFFER_CAPS (buf);
4049     else
4050       caps = NULL;
4051   }
4052   return caps;
4055 /* this is the chain function that does not perform the additional argument
4056  * checking for that little extra speed.
4057  */
4058 static inline GstFlowReturn
4059 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
4061   GstCaps *caps;
4062   gboolean caps_changed;
4063   GstFlowReturn ret;
4064   gboolean emit_signal;
4066   GST_PAD_STREAM_LOCK (pad);
4068   GST_OBJECT_LOCK (pad);
4069   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4070     goto flushing;
4072   caps = gst_pad_data_get_caps (is_buffer, data);
4073   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4075   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4076   GST_OBJECT_UNLOCK (pad);
4078   /* see if the signal should be emited, we emit before caps nego as
4079    * we might drop the buffer and do capsnego for nothing. */
4080   if (G_UNLIKELY (emit_signal)) {
4081     if (G_LIKELY (is_buffer)) {
4082       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4083         goto dropping;
4084     } else {
4085       /* chain all groups in the buffer list one by one to avoid problems with
4086        * buffer probes that push buffers or events */
4087       goto chain_groups;
4088     }
4089   }
4091   /* we got a new datatype on the pad, see if it can handle it */
4092   if (G_UNLIKELY (caps_changed)) {
4093     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4094     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4095       goto not_negotiated;
4096   }
4098   /* NOTE: we read the chainfunc unlocked.
4099    * we cannot hold the lock for the pad so we might send
4100    * the data to the wrong function. This is not really a
4101    * problem since functions are assigned at creation time
4102    * and don't change that often... */
4103   if (G_LIKELY (is_buffer)) {
4104     GstPadChainFunction chainfunc;
4106     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4107       goto no_function;
4109     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4110         "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4112     ret = chainfunc (pad, GST_BUFFER_CAST (data));
4114     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4115         "called chainfunction &%s, returned %s",
4116         GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4117   } else {
4118     GstPadChainListFunction chainlistfunc;
4120     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4121       goto chain_groups;
4123     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4124         "calling chainlistfunction &%s",
4125         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4127     ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4129     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4130         "called chainlistfunction &%s, returned %s",
4131         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4132   }
4134   GST_PAD_STREAM_UNLOCK (pad);
4136   return ret;
4138 chain_groups:
4139   {
4140     GstBufferList *list;
4141     GstBufferListIterator *it;
4142     GstBuffer *group;
4144     GST_PAD_STREAM_UNLOCK (pad);
4146     GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4148     list = GST_BUFFER_LIST_CAST (data);
4149     it = gst_buffer_list_iterate (list);
4151     if (gst_buffer_list_iterator_next_group (it)) {
4152       do {
4153         group = gst_buffer_list_iterator_merge_group (it);
4154         if (group == NULL) {
4155           group = gst_buffer_new ();
4156           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4157         } else {
4158           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4159         }
4160         ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4161       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4162     } else {
4163       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4164       ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4165     }
4167     gst_buffer_list_iterator_free (it);
4168     gst_buffer_list_unref (list);
4170     return ret;
4171   }
4173   /* ERRORS */
4174 flushing:
4175   {
4176     gst_pad_data_unref (is_buffer, data);
4177     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4178         "pushing, but pad was flushing");
4179     GST_OBJECT_UNLOCK (pad);
4180     GST_PAD_STREAM_UNLOCK (pad);
4181     return GST_FLOW_WRONG_STATE;
4182   }
4183 dropping:
4184   {
4185     gst_pad_data_unref (is_buffer, data);
4186     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4187     GST_PAD_STREAM_UNLOCK (pad);
4188     return GST_FLOW_OK;
4189   }
4190 not_negotiated:
4191   {
4192     gst_pad_data_unref (is_buffer, data);
4193     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4194         "pushing data but pad did not accept");
4195     GST_PAD_STREAM_UNLOCK (pad);
4196     return GST_FLOW_NOT_NEGOTIATED;
4197   }
4198 no_function:
4199   {
4200     gst_pad_data_unref (is_buffer, data);
4201     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4202         "pushing, but not chainhandler");
4203     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4204         ("push on pad %s:%s but it has no chainfunction",
4205             GST_DEBUG_PAD_NAME (pad)));
4206     GST_PAD_STREAM_UNLOCK (pad);
4207     return GST_FLOW_NOT_SUPPORTED;
4208   }
4211 /**
4212  * gst_pad_chain:
4213  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4214  * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4215  *
4216  * Chain a buffer to @pad.
4217  *
4218  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4219  *
4220  * If the caps on @buffer are different from the current caps on @pad, this
4221  * function will call any setcaps function (see gst_pad_set_setcaps_function())
4222  * installed on @pad. If the new caps are not acceptable for @pad, this
4223  * function returns #GST_FLOW_NOT_NEGOTIATED.
4224  *
4225  * The function proceeds calling the chain function installed on @pad (see
4226  * gst_pad_set_chain_function()) and the return value of that function is
4227  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4228  * chain function.
4229  *
4230  * In all cases, success or failure, the caller loses its reference to @buffer
4231  * after calling this function.
4232  *
4233  * Returns: a #GstFlowReturn from the pad.
4234  *
4235  * MT safe.
4236  */
4237 GstFlowReturn
4238 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4240   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4241   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4242   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4244   return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4247 /**
4248  * gst_pad_chain_list:
4249  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4250  * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4251  *
4252  * Chain a bufferlist to @pad.
4253  *
4254  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4255  *
4256  * If the caps on the first buffer of @list are different from the current
4257  * caps on @pad, this function will call any setcaps function
4258  * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4259  * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4260  *
4261  * The function proceeds calling the chainlist function installed on @pad (see
4262  * gst_pad_set_chain_list_function()) and the return value of that function is
4263  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4264  * chainlist function.
4265  *
4266  * In all cases, success or failure, the caller loses its reference to @list
4267  * after calling this function.
4268  *
4269  * MT safe.
4270  *
4271  * Returns: a #GstFlowReturn from the pad.
4272  *
4273  * Since: 0.10.24
4274  */
4275 GstFlowReturn
4276 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4278   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4279   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4280   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4282   return gst_pad_chain_data_unchecked (pad, FALSE, list);
4285 static GstFlowReturn
4286 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4288   GstPad *peer;
4289   GstFlowReturn ret;
4290   GstCaps *caps;
4291   gboolean caps_changed;
4293   GST_OBJECT_LOCK (pad);
4295   /* FIXME: this check can go away; pad_set_blocked could be implemented with
4296    * probes completely or probes with an extended pad block. */
4297   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4298     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4299       goto flushed;
4301   /* we emit signals on the pad arg, the peer will have a chance to
4302    * emit in the _chain() function */
4303   if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4304     /* unlock before emitting */
4305     GST_OBJECT_UNLOCK (pad);
4307     if (G_LIKELY (is_buffer)) {
4308       /* if the signal handler returned FALSE, it means we should just drop the
4309        * buffer */
4310       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4311         goto dropped;
4312     } else {
4313       /* push all buffers in the list */
4314       goto push_groups;
4315     }
4316     GST_OBJECT_LOCK (pad);
4317   }
4319   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4320     goto not_linked;
4322   /* Before pushing the buffer to the peer pad, ensure that caps
4323    * are set on this pad */
4324   caps = gst_pad_data_get_caps (is_buffer, data);
4325   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4327   /* take ref to peer pad before releasing the lock */
4328   gst_object_ref (peer);
4330   GST_OBJECT_UNLOCK (pad);
4332   /* we got a new datatype from the pad, it had better handle it */
4333   if (G_UNLIKELY (caps_changed)) {
4334     GST_DEBUG_OBJECT (pad,
4335         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4336         GST_PAD_CAPS (pad), caps, caps);
4337     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4338       goto not_negotiated;
4339   }
4341   ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4343   gst_object_unref (peer);
4345   return ret;
4347 push_groups:
4348   {
4349     GstBufferList *list;
4350     GstBufferListIterator *it;
4351     GstBuffer *group;
4353     GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4355     list = GST_BUFFER_LIST_CAST (data);
4356     it = gst_buffer_list_iterate (list);
4358     if (gst_buffer_list_iterator_next_group (it)) {
4359       do {
4360         group = gst_buffer_list_iterator_merge_group (it);
4361         if (group == NULL) {
4362           group = gst_buffer_new ();
4363           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4364         } else {
4365           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4366         }
4367         ret = gst_pad_push_data (pad, TRUE, group);
4368       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4369     } else {
4370       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4371       ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4372     }
4374     gst_buffer_list_iterator_free (it);
4375     gst_buffer_list_unref (list);
4377     return ret;
4378   }
4380   /* ERROR recovery here */
4381 flushed:
4382   {
4383     gst_pad_data_unref (is_buffer, data);
4384     GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4385     GST_OBJECT_UNLOCK (pad);
4386     return ret;
4387   }
4388 dropped:
4389   {
4390     gst_pad_data_unref (is_buffer, data);
4391     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4392     return GST_FLOW_OK;
4393   }
4394 not_linked:
4395   {
4396     gst_pad_data_unref (is_buffer, data);
4397     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4398         "pushing, but it was not linked");
4399     GST_OBJECT_UNLOCK (pad);
4400     return GST_FLOW_NOT_LINKED;
4401   }
4402 not_negotiated:
4403   {
4404     gst_pad_data_unref (is_buffer, data);
4405     gst_object_unref (peer);
4406     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4407         "element pushed data then refused to accept the caps");
4408     return GST_FLOW_NOT_NEGOTIATED;
4409   }
4412 /**
4413  * gst_pad_push:
4414  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4415  * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4416  *
4417  * Pushes a buffer to the peer of @pad.
4418  *
4419  * This function will call an installed pad block before triggering any
4420  * installed pad probes.
4421  *
4422  * If the caps on @buffer are different from the currently configured caps on
4423  * @pad, this function will call any installed setcaps function on @pad (see
4424  * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4425  * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4426  *
4427  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4428  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4429  * be returned.
4430  *
4431  * In all cases, success or failure, the caller loses its reference to @buffer
4432  * after calling this function.
4433  *
4434  * Returns: a #GstFlowReturn from the peer pad.
4435  *
4436  * MT safe.
4437  */
4438 GstFlowReturn
4439 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4441   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4442   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4443   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4445   return gst_pad_push_data (pad, TRUE, buffer);
4448 /**
4449  * gst_pad_push_list:
4450  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4451  * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4452  *
4453  * Pushes a buffer list to the peer of @pad.
4454  *
4455  * This function will call an installed pad block before triggering any
4456  * installed pad probes.
4457  *
4458  * If the caps on the first buffer in the first group of @list are different
4459  * from the currently configured caps on @pad, this function will call any
4460  * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4461  * case of failure to renegotiate the new format, this function returns
4462  * #GST_FLOW_NOT_NEGOTIATED.
4463  *
4464  * If there are any probes installed on @pad every group of the buffer list
4465  * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4466  * buffer list will be unreffed.
4467  *
4468  * The function proceeds calling the chain function on the peer pad and returns
4469  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4470  * be returned. If the peer pad does not have any installed chainlist function
4471  * every group buffer of the list will be merged into a normal #GstBuffer and
4472  * chained via gst_pad_chain().
4473  *
4474  * In all cases, success or failure, the caller loses its reference to @list
4475  * after calling this function.
4476  *
4477  * Returns: a #GstFlowReturn from the peer pad.
4478  *
4479  * MT safe.
4480  *
4481  * Since: 0.10.24
4482  */
4483 GstFlowReturn
4484 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4486   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4487   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4488   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4490   return gst_pad_push_data (pad, FALSE, list);
4493 /**
4494  * gst_pad_check_pull_range:
4495  * @pad: a sink #GstPad.
4496  *
4497  * Checks if a gst_pad_pull_range() can be performed on the peer
4498  * source pad. This function is used by plugins that want to check
4499  * if they can use random access on the peer source pad.
4500  *
4501  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4502  * if it needs to perform some logic to determine if pull_range is
4503  * possible.
4504  *
4505  * Returns: a gboolean with the result.
4506  *
4507  * MT safe.
4508  */
4509 gboolean
4510 gst_pad_check_pull_range (GstPad * pad)
4512   GstPad *peer;
4513   gboolean ret;
4514   GstPadCheckGetRangeFunction checkgetrangefunc;
4516   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4518   GST_OBJECT_LOCK (pad);
4519   if (!GST_PAD_IS_SINK (pad))
4520     goto wrong_direction;
4522   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4523     goto not_connected;
4525   gst_object_ref (peer);
4526   GST_OBJECT_UNLOCK (pad);
4528   /* see note in above function */
4529   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4530     /* FIXME, kindoff ghetto */
4531     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4532     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4533         "no checkgetrangefunc, assuming %d", ret);
4534   } else {
4535     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4536         "calling checkgetrangefunc %s of peer pad %s:%s",
4537         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4539     ret = checkgetrangefunc (peer);
4540   }
4542   gst_object_unref (peer);
4544   return ret;
4546   /* ERROR recovery here */
4547 wrong_direction:
4548   {
4549     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4550         "checking pull range, but pad must be a sinkpad");
4551     GST_OBJECT_UNLOCK (pad);
4552     return FALSE;
4553   }
4554 not_connected:
4555   {
4556     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4557         "checking pull range, but it was not linked");
4558     GST_OBJECT_UNLOCK (pad);
4559     return FALSE;
4560   }
4563 /**
4564  * gst_pad_get_range:
4565  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4566  * @offset: The start offset of the buffer
4567  * @size: The length of the buffer
4568  * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4569  *
4570  * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4571  * immediatly.
4572  *
4573  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4574  * description of a getrange function. If @pad has no getrange function
4575  * installed (see gst_pad_set_getrange_function()) this function returns
4576  * #GST_FLOW_NOT_SUPPORTED.
4577  *
4578  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4579  *
4580  * Returns: a #GstFlowReturn from the pad.
4581  *
4582  * MT safe.
4583  */
4584 GstFlowReturn
4585 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4586     GstBuffer ** buffer)
4588   GstFlowReturn ret;
4589   GstPadGetRangeFunction getrangefunc;
4590   gboolean emit_signal;
4591   GstCaps *caps;
4592   gboolean caps_changed;
4594   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4595   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4596   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4598   GST_PAD_STREAM_LOCK (pad);
4600   GST_OBJECT_LOCK (pad);
4601   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4602     goto flushing;
4604   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4605   GST_OBJECT_UNLOCK (pad);
4607   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4608     goto no_function;
4610   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4611       "calling getrangefunc %s, offset %"
4612       G_GUINT64_FORMAT ", size %u",
4613       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4615   ret = getrangefunc (pad, offset, size, buffer);
4617   /* can only fire the signal if we have a valid buffer */
4618   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4619     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4620       goto dropping;
4621   }
4623   GST_PAD_STREAM_UNLOCK (pad);
4625   if (G_UNLIKELY (ret != GST_FLOW_OK))
4626     goto get_range_failed;
4628   GST_OBJECT_LOCK (pad);
4629   /* Before pushing the buffer to the peer pad, ensure that caps
4630    * are set on this pad */
4631   caps = GST_BUFFER_CAPS (*buffer);
4632   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4633   GST_OBJECT_UNLOCK (pad);
4635   if (G_UNLIKELY (caps_changed)) {
4636     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4637     /* this should usually work because the element produced the buffer */
4638     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4639       goto not_negotiated;
4640   }
4641   return ret;
4643   /* ERRORS */
4644 flushing:
4645   {
4646     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4647         "pulling range, but pad was flushing");
4648     GST_OBJECT_UNLOCK (pad);
4649     GST_PAD_STREAM_UNLOCK (pad);
4650     return GST_FLOW_WRONG_STATE;
4651   }
4652 no_function:
4653   {
4654     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4655         ("pullrange on pad %s:%s but it has no getrangefunction",
4656             GST_DEBUG_PAD_NAME (pad)));
4657     GST_PAD_STREAM_UNLOCK (pad);
4658     return GST_FLOW_NOT_SUPPORTED;
4659   }
4660 dropping:
4661   {
4662     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4663         "Dropping data after FALSE probe return");
4664     GST_PAD_STREAM_UNLOCK (pad);
4665     gst_buffer_unref (*buffer);
4666     *buffer = NULL;
4667     return GST_FLOW_UNEXPECTED;
4668   }
4669 get_range_failed:
4670   {
4671     *buffer = NULL;
4672     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4673         "getrange failed %s", gst_flow_get_name (ret));
4674     return ret;
4675   }
4676 not_negotiated:
4677   {
4678     gst_buffer_unref (*buffer);
4679     *buffer = NULL;
4680     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4681         "getrange returned buffer of unaccaptable caps");
4682     return GST_FLOW_NOT_NEGOTIATED;
4683   }
4687 /**
4688  * gst_pad_pull_range:
4689  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4690  * @offset: The start offset of the buffer
4691  * @size: The length of the buffer
4692  * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4693  *
4694  * Pulls a @buffer from the peer pad.
4695  *
4696  * This function will first trigger the pad block signal if it was
4697  * installed.
4698  *
4699  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4700  * function returns the result of gst_pad_get_range() on the peer pad.
4701  * See gst_pad_get_range() for a list of return values and for the
4702  * semantics of the arguments of this function.
4703  *
4704  * @buffer's caps must either be unset or the same as what is already
4705  * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4706  * supported.
4707  *
4708  * Returns: a #GstFlowReturn from the peer pad.
4709  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4710  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4711  * @buffer may not be used or freed when any other return value than
4712  * #GST_FLOW_OK is returned.
4713  *
4714  * MT safe.
4715  */
4716 GstFlowReturn
4717 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4718     GstBuffer ** buffer)
4720   GstPad *peer;
4721   GstFlowReturn ret;
4722   gboolean emit_signal;
4723   GstCaps *caps;
4724   gboolean caps_changed;
4726   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4727   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4728   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4730   GST_OBJECT_LOCK (pad);
4732   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4733     handle_pad_block (pad);
4735   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4736     goto not_connected;
4738   /* signal emision for the pad, peer has chance to emit when
4739    * we call _get_range() */
4740   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4742   gst_object_ref (peer);
4743   GST_OBJECT_UNLOCK (pad);
4745   ret = gst_pad_get_range (peer, offset, size, buffer);
4747   gst_object_unref (peer);
4749   if (G_UNLIKELY (ret != GST_FLOW_OK))
4750     goto pull_range_failed;
4752   /* can only fire the signal if we have a valid buffer */
4753   if (G_UNLIKELY (emit_signal)) {
4754     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4755       goto dropping;
4756   }
4758   GST_OBJECT_LOCK (pad);
4759   /* Before pushing the buffer to the peer pad, ensure that caps
4760    * are set on this pad */
4761   caps = GST_BUFFER_CAPS (*buffer);
4762   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4763   GST_OBJECT_UNLOCK (pad);
4765   /* we got a new datatype on the pad, see if it can handle it */
4766   if (G_UNLIKELY (caps_changed)) {
4767     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4768     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4769       goto not_negotiated;
4770   }
4771   return ret;
4773   /* ERROR recovery here */
4774 not_connected:
4775   {
4776     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4777         "pulling range, but it was not linked");
4778     GST_OBJECT_UNLOCK (pad);
4779     return GST_FLOW_NOT_LINKED;
4780   }
4781 pull_range_failed:
4782   {
4783     *buffer = NULL;
4784     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4785         "pullrange failed %s", gst_flow_get_name (ret));
4786     return ret;
4787   }
4788 dropping:
4789   {
4790     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4791         "Dropping data after FALSE probe return");
4792     gst_buffer_unref (*buffer);
4793     *buffer = NULL;
4794     return GST_FLOW_UNEXPECTED;
4795   }
4796 not_negotiated:
4797   {
4798     gst_buffer_unref (*buffer);
4799     *buffer = NULL;
4800     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4801         "pullrange returned buffer of different caps");
4802     return GST_FLOW_NOT_NEGOTIATED;
4803   }
4806 /**
4807  * gst_pad_push_event:
4808  * @pad: a #GstPad to push the event to.
4809  * @event: the #GstEvent to send to the pad.
4810  *
4811  * Sends the event to the peer of the given pad. This function is
4812  * mainly used by elements to send events to their peer
4813  * elements.
4814  *
4815  * This function takes owership of the provided event so you should
4816  * gst_event_ref() it if you want to reuse the event after this call.
4817  *
4818  * Returns: TRUE if the event was handled.
4819  *
4820  * MT safe.
4821  */
4822 gboolean
4823 gst_pad_push_event (GstPad * pad, GstEvent * event)
4825   GstPad *peerpad;
4826   gboolean result;
4828   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4829   g_return_val_if_fail (event != NULL, FALSE);
4830   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4832   GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4834   GST_OBJECT_LOCK (pad);
4836   /* Two checks to be made:
4837    * . (un)set the FLUSHING flag for flushing events,
4838    * . handle pad blocking */
4839   switch (GST_EVENT_TYPE (event)) {
4840     case GST_EVENT_FLUSH_START:
4841       GST_PAD_SET_FLUSHING (pad);
4843       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4844         /* flush start will have set the FLUSHING flag and will then
4845          * unlock all threads doing a GCond wait on the blocking pad. This
4846          * will typically unblock the STREAMING thread blocked on a pad. */
4847         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4848             "doing block signal.");
4849         GST_PAD_BLOCK_BROADCAST (pad);
4850         goto flushed;
4851       }
4852       break;
4853     case GST_EVENT_FLUSH_STOP:
4854       GST_PAD_UNSET_FLUSHING (pad);
4856       /* if we are blocked, flush away the FLUSH_STOP event */
4857       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4858         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4859         goto flushed;
4860       }
4861       break;
4862     default:
4863       while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4864         /* block the event as long as the pad is blocked */
4865         if (handle_pad_block (pad) != GST_FLOW_OK)
4866           goto flushed;
4867       }
4868       break;
4869   }
4871   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4872     GST_OBJECT_UNLOCK (pad);
4874     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4875       goto dropping;
4877     GST_OBJECT_LOCK (pad);
4878   }
4879   peerpad = GST_PAD_PEER (pad);
4880   if (peerpad == NULL)
4881     goto not_linked;
4883   GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4884       GST_EVENT_TYPE_NAME (event), peerpad);
4885   gst_object_ref (peerpad);
4886   GST_OBJECT_UNLOCK (pad);
4888   result = gst_pad_send_event (peerpad, event);
4890   /* Note: we gave away ownership of the event at this point */
4891   GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4892       peerpad, result);
4893   gst_object_unref (peerpad);
4895   return result;
4897   /* ERROR handling */
4898 dropping:
4899   {
4900     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4901     gst_event_unref (event);
4902     return FALSE;
4903   }
4904 not_linked:
4905   {
4906     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4907     gst_event_unref (event);
4908     GST_OBJECT_UNLOCK (pad);
4909     return FALSE;
4910   }
4911 flushed:
4912   {
4913     GST_DEBUG_OBJECT (pad,
4914         "Not forwarding event since we're flushing and blocking");
4915     gst_event_unref (event);
4916     GST_OBJECT_UNLOCK (pad);
4917     return TRUE;
4918   }
4921 /**
4922  * gst_pad_send_event:
4923  * @pad: a #GstPad to send the event to.
4924  * @event: the #GstEvent to send to the pad.
4925  *
4926  * Sends the event to the pad. This function can be used
4927  * by applications to send events in the pipeline.
4928  *
4929  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4930  * sink pad, @event should be a downstream event. For example, you would not
4931  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4932  * Furthermore, some downstream events have to be serialized with data flow,
4933  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4934  * the event needs to be serialized with data flow, this function will take the
4935  * pad's stream lock while calling its event function.
4936  *
4937  * To find out whether an event type is upstream, downstream, or downstream and
4938  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4939  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4940  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4941  * plugin doesn't need to bother itself with this information; the core handles
4942  * all necessary locks and checks.
4943  *
4944  * This function takes owership of the provided event so you should
4945  * gst_event_ref() it if you want to reuse the event after this call.
4946  *
4947  * Returns: TRUE if the event was handled.
4948  */
4949 gboolean
4950 gst_pad_send_event (GstPad * pad, GstEvent * event)
4952   gboolean result = FALSE;
4953   GstPadEventFunction eventfunc;
4954   gboolean serialized, need_unlock = FALSE;
4956   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4957   g_return_val_if_fail (event != NULL, FALSE);
4959   GST_OBJECT_LOCK (pad);
4960   if (GST_PAD_IS_SINK (pad)) {
4961     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4962       goto wrong_direction;
4963     serialized = GST_EVENT_IS_SERIALIZED (event);
4964   } else if (GST_PAD_IS_SRC (pad)) {
4965     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4966       goto wrong_direction;
4967     /* events on srcpad never are serialized */
4968     serialized = FALSE;
4969   } else
4970     goto unknown_direction;
4972   if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4973     GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4974     GST_EVENT_SRC (event) = gst_object_ref (pad);
4975   }
4977   /* pad signals */
4978   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4979     GST_OBJECT_UNLOCK (pad);
4981     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4982       goto dropping;
4984     GST_OBJECT_LOCK (pad);
4985   }
4987   switch (GST_EVENT_TYPE (event)) {
4988     case GST_EVENT_FLUSH_START:
4989       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4990           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4992       /* can't even accept a flush begin event when flushing */
4993       if (GST_PAD_IS_FLUSHING (pad))
4994         goto flushing;
4995       GST_PAD_SET_FLUSHING (pad);
4996       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4997       break;
4998     case GST_EVENT_FLUSH_STOP:
4999       GST_PAD_UNSET_FLUSHING (pad);
5000       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5001       GST_OBJECT_UNLOCK (pad);
5002       /* grab stream lock */
5003       GST_PAD_STREAM_LOCK (pad);
5004       need_unlock = TRUE;
5005       GST_OBJECT_LOCK (pad);
5006       break;
5007     default:
5008       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5009           GST_EVENT_TYPE_NAME (event));
5011       /* make this a little faster, no point in grabbing the lock
5012        * if the pad is allready flushing. */
5013       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5014         goto flushing;
5016       if (serialized) {
5017         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5018         GST_OBJECT_UNLOCK (pad);
5019         GST_PAD_STREAM_LOCK (pad);
5020         need_unlock = TRUE;
5021         GST_OBJECT_LOCK (pad);
5022         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5023           goto flushing;
5024       }
5025       break;
5026   }
5027   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5028     goto no_function;
5030   GST_OBJECT_UNLOCK (pad);
5032   result = eventfunc (pad, event);
5034   if (need_unlock)
5035     GST_PAD_STREAM_UNLOCK (pad);
5037   GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5039   return result;
5041   /* ERROR handling */
5042 wrong_direction:
5043   {
5044     g_warning ("pad %s:%s sending %s event in wrong direction",
5045         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5046     GST_OBJECT_UNLOCK (pad);
5047     gst_event_unref (event);
5048     return FALSE;
5049   }
5050 unknown_direction:
5051   {
5052     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5053     GST_OBJECT_UNLOCK (pad);
5054     gst_event_unref (event);
5055     return FALSE;
5056   }
5057 no_function:
5058   {
5059     g_warning ("pad %s:%s has no event handler, file a bug.",
5060         GST_DEBUG_PAD_NAME (pad));
5061     GST_OBJECT_UNLOCK (pad);
5062     if (need_unlock)
5063       GST_PAD_STREAM_UNLOCK (pad);
5064     gst_event_unref (event);
5065     return FALSE;
5066   }
5067 flushing:
5068   {
5069     GST_OBJECT_UNLOCK (pad);
5070     if (need_unlock)
5071       GST_PAD_STREAM_UNLOCK (pad);
5072     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5073         "Received event on flushing pad. Discarding");
5074     gst_event_unref (event);
5075     return FALSE;
5076   }
5077 dropping:
5078   {
5079     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5080     gst_event_unref (event);
5081     return FALSE;
5082   }
5085 /**
5086  * gst_pad_set_element_private:
5087  * @pad: the #GstPad to set the private data of.
5088  * @priv: The private data to attach to the pad.
5089  *
5090  * Set the given private data gpointer on the pad.
5091  * This function can only be used by the element that owns the pad.
5092  * No locking is performed in this function.
5093  */
5094 void
5095 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5097   pad->element_private = priv;
5100 /**
5101  * gst_pad_get_element_private:
5102  * @pad: the #GstPad to get the private data of.
5103  *
5104  * Gets the private data of a pad.
5105  * No locking is performed in this function.
5106  *
5107  * Returns: a #gpointer to the private data.
5108  */
5109 gpointer
5110 gst_pad_get_element_private (GstPad * pad)
5112   return pad->element_private;
5115 static void
5116 do_stream_status (GstPad * pad, GstStreamStatusType type,
5117     GThread * thread, GstTask * task)
5119   GstElement *parent;
5121   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5123   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5124     if (GST_IS_ELEMENT (parent)) {
5125       GstMessage *message;
5126       GValue value = { 0 };
5128       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5129           type, parent);
5131       g_value_init (&value, GST_TYPE_TASK);
5132       g_value_set_object (&value, task);
5133       gst_message_set_stream_status_object (message, &value);
5134       g_value_unset (&value);
5136       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5137       gst_element_post_message (parent, message);
5138     }
5139     gst_object_unref (parent);
5140   }
5143 static void
5144 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5146   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5147       thread, task);
5150 static void
5151 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5153   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5154       thread, task);
5157 static GstTaskThreadCallbacks thr_callbacks = {
5158   pad_enter_thread,
5159   pad_leave_thread,
5160 };
5162 /**
5163  * gst_pad_start_task:
5164  * @pad: the #GstPad to start the task of
5165  * @func: the task function to call
5166  * @data: data passed to the task function
5167  *
5168  * Starts a task that repeatedly calls @func with @data. This function
5169  * is mostly used in pad activation functions to start the dataflow.
5170  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5171  * before @func is called.
5172  *
5173  * Returns: a %TRUE if the task could be started.
5174  */
5175 gboolean
5176 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5178   GstTask *task;
5179   gboolean res;
5181   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5182   g_return_val_if_fail (func != NULL, FALSE);
5184   GST_DEBUG_OBJECT (pad, "start task");
5186   GST_OBJECT_LOCK (pad);
5187   task = GST_PAD_TASK (pad);
5188   if (task == NULL) {
5189     task = gst_task_create (func, data);
5190     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5191     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5192     GST_DEBUG_OBJECT (pad, "created task");
5193     GST_PAD_TASK (pad) = task;
5194     gst_object_ref (task);
5195     /* release lock to post the message */
5196     GST_OBJECT_UNLOCK (pad);
5198     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5200     gst_object_unref (task);
5202     GST_OBJECT_LOCK (pad);
5203     /* nobody else is supposed to have changed the pad now */
5204     if (GST_PAD_TASK (pad) != task)
5205       goto concurrent_stop;
5206   }
5207   res = gst_task_set_state (task, GST_TASK_STARTED);
5208   GST_OBJECT_UNLOCK (pad);
5210   return res;
5212   /* ERRORS */
5213 concurrent_stop:
5214   {
5215     GST_OBJECT_UNLOCK (pad);
5216     return TRUE;
5217   }
5220 /**
5221  * gst_pad_pause_task:
5222  * @pad: the #GstPad to pause the task of
5223  *
5224  * Pause the task of @pad. This function will also wait until the
5225  * function executed by the task is finished if this function is not
5226  * called from the task function.
5227  *
5228  * Returns: a TRUE if the task could be paused or FALSE when the pad
5229  * has no task.
5230  */
5231 gboolean
5232 gst_pad_pause_task (GstPad * pad)
5234   GstTask *task;
5235   gboolean res;
5237   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5239   GST_DEBUG_OBJECT (pad, "pause task");
5241   GST_OBJECT_LOCK (pad);
5242   task = GST_PAD_TASK (pad);
5243   if (task == NULL)
5244     goto no_task;
5245   res = gst_task_set_state (task, GST_TASK_PAUSED);
5246   GST_OBJECT_UNLOCK (pad);
5248   /* wait for task function to finish, this lock is recursive so it does nothing
5249    * when the pause is called from the task itself */
5250   GST_PAD_STREAM_LOCK (pad);
5251   GST_PAD_STREAM_UNLOCK (pad);
5253   return res;
5255 no_task:
5256   {
5257     GST_DEBUG_OBJECT (pad, "pad has no task");
5258     GST_OBJECT_UNLOCK (pad);
5259     return FALSE;
5260   }
5263 /**
5264  * gst_pad_stop_task:
5265  * @pad: the #GstPad to stop the task of
5266  *
5267  * Stop the task of @pad. This function will also make sure that the
5268  * function executed by the task will effectively stop if not called
5269  * from the GstTaskFunction.
5270  *
5271  * This function will deadlock if called from the GstTaskFunction of
5272  * the task. Use gst_task_pause() instead.
5273  *
5274  * Regardless of whether the pad has a task, the stream lock is acquired and
5275  * released so as to ensure that streaming through this pad has finished.
5276  *
5277  * Returns: a TRUE if the task could be stopped or FALSE on error.
5278  */
5279 gboolean
5280 gst_pad_stop_task (GstPad * pad)
5282   GstTask *task;
5283   gboolean res;
5285   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5287   GST_DEBUG_OBJECT (pad, "stop task");
5289   GST_OBJECT_LOCK (pad);
5290   task = GST_PAD_TASK (pad);
5291   if (task == NULL)
5292     goto no_task;
5293   GST_PAD_TASK (pad) = NULL;
5294   res = gst_task_set_state (task, GST_TASK_STOPPED);
5295   GST_OBJECT_UNLOCK (pad);
5297   GST_PAD_STREAM_LOCK (pad);
5298   GST_PAD_STREAM_UNLOCK (pad);
5300   if (!gst_task_join (task))
5301     goto join_failed;
5303   gst_object_unref (task);
5305   return res;
5307 no_task:
5308   {
5309     GST_DEBUG_OBJECT (pad, "no task");
5310     GST_OBJECT_UNLOCK (pad);
5312     GST_PAD_STREAM_LOCK (pad);
5313     GST_PAD_STREAM_UNLOCK (pad);
5315     /* this is not an error */
5316     return TRUE;
5317   }
5318 join_failed:
5319   {
5320     /* this is bad, possibly the application tried to join the task from
5321      * the task's thread. We install the task again so that it will be stopped
5322      * again from the right thread next time hopefully. */
5323     GST_OBJECT_LOCK (pad);
5324     GST_DEBUG_OBJECT (pad, "join failed");
5325     /* we can only install this task if there was no other task */
5326     if (GST_PAD_TASK (pad) == NULL)
5327       GST_PAD_TASK (pad) = task;
5328     GST_OBJECT_UNLOCK (pad);
5330     return FALSE;
5331   }