2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
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.
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.
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.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
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.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
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.
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.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.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 */
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;
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 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
122 #ifdef GST_DISABLE_DEPRECATED
123 #include <libxml/parser.h>
125 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
126 void gst_pad_load_and_link (xmlNodePtr self, GstObject * parent);
129 /* Some deprecated stuff that we need inside here for
130 * backwards compatibility */
131 #ifdef GST_DISABLE_DEPRECATED
132 #ifndef GST_REMOVE_DEPRECATED
133 #define GST_PAD_INTLINKFUNC(pad) (GST_PAD_CAST(pad)->intlinkfunc)
134 GList *gst_pad_get_internal_links_default (GstPad * pad);
138 static GstObjectClass *parent_class = NULL;
139 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
141 /* quarks for probe signals */
142 static GQuark buffer_quark;
143 static GQuark event_quark;
152 static GstFlowQuarks flow_quarks[] = {
153 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
154 {GST_FLOW_RESEND, "resend", 0},
155 {GST_FLOW_OK, "ok", 0},
156 {GST_FLOW_NOT_LINKED, "not-linked", 0},
157 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
158 {GST_FLOW_UNEXPECTED, "unexpected", 0},
159 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
160 {GST_FLOW_ERROR, "error", 0},
161 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
162 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
167 * @ret: a #GstFlowReturn to get the name of.
169 * Gets a string representing the given flow return.
171 * Returns: a static string with the name of the flow return.
173 G_CONST_RETURN gchar *
174 gst_flow_get_name (GstFlowReturn ret)
178 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
180 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
181 if (ret == flow_quarks[i].ret)
182 return flow_quarks[i].name;
189 * @ret: a #GstFlowReturn to get the quark of.
191 * Get the unique quark for the given GstFlowReturn.
193 * Returns: the quark associated with the flow return or 0 if an
194 * invalid return was specified.
197 gst_flow_to_quark (GstFlowReturn ret)
201 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
203 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
204 if (ret == flow_quarks[i].ret)
205 return flow_quarks[i].quark;
214 buffer_quark = g_quark_from_static_string ("buffer"); \
215 event_quark = g_quark_from_static_string ("event"); \
217 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
218 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
221 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
222 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
225 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
228 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
229 GValue * return_accu, const GValue * handler_return, gpointer dummy)
231 gboolean ret = g_value_get_boolean (handler_return);
233 GST_DEBUG ("accumulated %d", ret);
234 g_value_set_boolean (return_accu, ret);
240 default_have_data (GstPad * pad, GstMiniObject * o)
246 gst_pad_class_init (GstPadClass * klass)
248 GObjectClass *gobject_class;
249 GstObjectClass *gstobject_class;
251 gobject_class = G_OBJECT_CLASS (klass);
252 gstobject_class = GST_OBJECT_CLASS (klass);
254 g_type_class_add_private (klass, sizeof (GstPadPrivate));
256 parent_class = g_type_class_peek_parent (klass);
258 gobject_class->dispose = gst_pad_dispose;
259 gobject_class->finalize = gst_pad_finalize;
260 gobject_class->set_property = gst_pad_set_property;
261 gobject_class->get_property = gst_pad_get_property;
265 * @pad: the pad that emitted the signal
266 * @peer: the peer pad that has been connected
268 * Signals that a pad has been linked to the peer pad.
270 gst_pad_signals[PAD_LINKED] =
271 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
272 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
273 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
276 * @pad: the pad that emitted the signal
277 * @peer: the peer pad that has been disconnected
279 * Signals that a pad has been unlinked from the peer pad.
281 gst_pad_signals[PAD_UNLINKED] =
282 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
283 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
284 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
286 * GstPad::request-link:
287 * @pad: the pad that emitted the signal
288 * @peer: the peer pad for which a connection is requested
290 * Signals that a pad connection has been requested.
292 gst_pad_signals[PAD_REQUEST_LINK] =
293 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
294 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
295 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
299 * @pad: the pad that emitted the signal
300 * @mini_obj: new data
302 * Signals that new data is available on the pad. This signal is used
303 * internally for implementing pad probes.
304 * See gst_pad_add_*_probe functions.
306 * Returns: %TRUE to keep the data, %FALSE to drop it
308 gst_pad_signals[PAD_HAVE_DATA] =
309 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
310 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
311 G_STRUCT_OFFSET (GstPadClass, have_data),
312 _gst_do_pass_data_accumulator,
313 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
314 GST_TYPE_MINI_OBJECT);
316 g_object_class_install_property (gobject_class, PAD_PROP_CAPS,
317 g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
318 GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
320 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
321 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
322 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
323 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
324 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
325 g_param_spec_object ("template", "Template",
326 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
330 gstobject_class->save_thyself =
331 ((gpointer (*)(GstObject * object,
332 gpointer self)) * GST_DEBUG_FUNCPTR (gst_pad_save_thyself));
334 gstobject_class->path_string_separator = ".";
336 /* Register common function pointer descriptions */
337 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
338 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
339 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
340 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
341 #ifndef GST_REMOVE_DEPRECATED
342 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_internal_links_default);
344 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
345 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
347 klass->have_data = default_have_data;
351 gst_pad_init (GstPad * pad)
353 pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
355 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
356 GST_PAD_PEER (pad) = NULL;
358 GST_PAD_CHAINFUNC (pad) = NULL;
360 GST_PAD_LINKFUNC (pad) = NULL;
362 GST_PAD_CAPS (pad) = NULL;
363 GST_PAD_GETCAPSFUNC (pad) = NULL;
365 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
366 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
367 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
368 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
369 #ifndef GST_REMOVE_DEPRECATED
370 GST_PAD_INTLINKFUNC (pad) = gst_pad_get_internal_links_default;
372 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
374 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
376 pad->do_buffer_signals = 0;
377 pad->do_event_signals = 0;
379 GST_PAD_SET_FLUSHING (pad);
381 pad->preroll_lock = g_mutex_new ();
382 pad->preroll_cond = g_cond_new ();
384 /* FIXME 0.11: Store this directly in the instance struct */
385 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
386 g_static_rec_mutex_init (pad->stream_rec_lock);
388 pad->block_cond = g_cond_new ();
392 gst_pad_dispose (GObject * object)
394 GstPad *pad = GST_PAD_CAST (object);
397 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
399 /* unlink the peer pad */
400 if ((peer = gst_pad_get_peer (pad))) {
401 /* window for MT unsafeness, someone else could unlink here
402 * and then we call unlink with wrong pads. The unlink
403 * function would catch this and safely return failed. */
404 if (GST_PAD_IS_SRC (pad))
405 gst_pad_unlink (pad, peer);
407 gst_pad_unlink (peer, pad);
409 gst_object_unref (peer);
413 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
415 gst_pad_set_pad_template (pad, NULL);
417 if (pad->block_destroy_data && pad->block_data) {
418 pad->block_destroy_data (pad->block_data);
419 pad->block_data = NULL;
422 G_OBJECT_CLASS (parent_class)->dispose (object);
426 gst_pad_finalize (GObject * object)
428 GstPad *pad = GST_PAD_CAST (object);
431 /* in case the task is still around, clean it up */
432 if ((task = GST_PAD_TASK (pad))) {
433 gst_task_join (task);
434 GST_PAD_TASK (pad) = NULL;
435 gst_object_unref (task);
438 if (pad->stream_rec_lock) {
439 g_static_rec_mutex_free (pad->stream_rec_lock);
440 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
441 pad->stream_rec_lock = NULL;
443 if (pad->preroll_lock) {
444 g_mutex_free (pad->preroll_lock);
445 g_cond_free (pad->preroll_cond);
446 pad->preroll_lock = NULL;
447 pad->preroll_cond = NULL;
449 if (pad->block_cond) {
450 g_cond_free (pad->block_cond);
451 pad->block_cond = NULL;
454 G_OBJECT_CLASS (parent_class)->finalize (object);
458 gst_pad_set_property (GObject * object, guint prop_id,
459 const GValue * value, GParamSpec * pspec)
461 g_return_if_fail (GST_IS_PAD (object));
464 case PAD_PROP_DIRECTION:
465 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
467 case PAD_PROP_TEMPLATE:
468 gst_pad_set_pad_template (GST_PAD_CAST (object),
469 (GstPadTemplate *) g_value_get_object (value));
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478 gst_pad_get_property (GObject * object, guint prop_id,
479 GValue * value, GParamSpec * pspec)
481 g_return_if_fail (GST_IS_PAD (object));
485 GST_OBJECT_LOCK (object);
486 g_value_set_boxed (value, GST_PAD_CAPS (object));
487 GST_OBJECT_UNLOCK (object);
489 case PAD_PROP_DIRECTION:
490 g_value_set_enum (value, GST_PAD_DIRECTION (object));
492 case PAD_PROP_TEMPLATE:
493 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503 * @name: the name of the new pad.
504 * @direction: the #GstPadDirection of the pad.
506 * Creates a new pad with the given name in the given direction.
507 * If name is NULL, a guaranteed unique name (across all pads)
509 * This function makes a copy of the name so you can safely free the name.
511 * Returns: a new #GstPad, or NULL in case of an error.
516 gst_pad_new (const gchar * name, GstPadDirection direction)
518 return g_object_new (GST_TYPE_PAD,
519 "name", name, "direction", direction, NULL);
523 * gst_pad_new_from_template:
524 * @templ: the pad template to use
525 * @name: the name of the element
527 * Creates a new pad with the given name from the given template.
528 * If name is NULL, a guaranteed unique name (across all pads)
530 * This function makes a copy of the name so you can safely free the name.
532 * Returns: a new #GstPad, or NULL in case of an error.
535 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
537 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
539 return g_object_new (GST_TYPE_PAD,
540 "name", name, "direction", templ->direction, "template", templ, NULL);
544 * gst_pad_new_from_static_template:
545 * @templ: the #GstStaticPadTemplate to use
546 * @name: the name of the element
548 * Creates a new pad with the given name from the given static template.
549 * If name is NULL, a guaranteed unique name (across all pads)
551 * This function makes a copy of the name so you can safely free the name.
553 * Returns: a new #GstPad, or NULL in case of an error.
556 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
560 GstPadTemplate *template;
562 template = gst_static_pad_template_get (templ);
563 pad = gst_pad_new_from_template (template, name);
564 gst_object_unref (template);
569 * gst_pad_get_direction:
570 * @pad: a #GstPad to get the direction of.
572 * Gets the direction of the pad. The direction of the pad is
573 * decided at construction time so this function does not take
576 * Returns: the #GstPadDirection of the pad.
581 gst_pad_get_direction (GstPad * pad)
583 GstPadDirection result;
585 /* PAD_UNKNOWN is a little silly but we need some sort of
586 * error return value */
587 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
589 result = GST_PAD_DIRECTION (pad);
595 gst_pad_activate_default (GstPad * pad)
597 return gst_pad_activate_push (pad, TRUE);
601 pre_activate (GstPad * pad, GstActivateMode new_mode)
604 case GST_ACTIVATE_PUSH:
605 case GST_ACTIVATE_PULL:
606 GST_OBJECT_LOCK (pad);
607 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
609 GST_PAD_UNSET_FLUSHING (pad);
610 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
611 GST_OBJECT_UNLOCK (pad);
613 case GST_ACTIVATE_NONE:
614 GST_OBJECT_LOCK (pad);
615 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
616 GST_PAD_SET_FLUSHING (pad);
617 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
618 /* unlock blocked pads so element can resume and stop */
619 GST_PAD_BLOCK_BROADCAST (pad);
620 GST_OBJECT_UNLOCK (pad);
626 post_activate (GstPad * pad, GstActivateMode new_mode)
629 case GST_ACTIVATE_PUSH:
630 case GST_ACTIVATE_PULL:
633 case GST_ACTIVATE_NONE:
634 /* ensures that streaming stops */
635 GST_PAD_STREAM_LOCK (pad);
636 GST_DEBUG_OBJECT (pad, "stopped streaming");
637 GST_PAD_STREAM_UNLOCK (pad);
643 * gst_pad_set_active:
644 * @pad: the #GstPad to activate or deactivate.
645 * @active: whether or not the pad should be active.
647 * Activates or deactivates the given pad.
648 * Normally called from within core state change functions.
650 * If @active, makes sure the pad is active. If it is already active, either in
651 * push or pull mode, just return. Otherwise dispatches to the pad's activate
652 * function to perform the actual activation.
654 * If not @active, checks the pad's current mode and calls
655 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
658 * Returns: #TRUE if the operation was successful.
663 gst_pad_set_active (GstPad * pad, gboolean active)
666 gboolean ret = FALSE;
668 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
670 GST_OBJECT_LOCK (pad);
671 old = GST_PAD_ACTIVATE_MODE (pad);
672 GST_OBJECT_UNLOCK (pad);
676 case GST_ACTIVATE_PUSH:
677 GST_DEBUG_OBJECT (pad, "activating pad from push");
680 case GST_ACTIVATE_PULL:
681 GST_DEBUG_OBJECT (pad, "activating pad from pull");
684 case GST_ACTIVATE_NONE:
685 GST_DEBUG_OBJECT (pad, "activating pad from none");
686 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
691 case GST_ACTIVATE_PUSH:
692 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
693 ret = gst_pad_activate_push (pad, FALSE);
695 case GST_ACTIVATE_PULL:
696 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
697 ret = gst_pad_activate_pull (pad, FALSE);
699 case GST_ACTIVATE_NONE:
700 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
707 GST_OBJECT_LOCK (pad);
709 g_critical ("Failed to deactivate pad %s:%s, very bad",
710 GST_DEBUG_PAD_NAME (pad));
712 GST_WARNING_OBJECT (pad, "Failed to activate pad");
714 GST_OBJECT_UNLOCK (pad);
721 * gst_pad_activate_pull:
722 * @pad: the #GstPad to activate or deactivate.
723 * @active: whether or not the pad should be active.
725 * Activates or deactivates the given pad in pull mode via dispatching to the
726 * pad's activatepullfunc. For use from within pad activation functions only.
727 * When called on sink pads, will first proxy the call to the peer pad, which
728 * is expected to activate its internally linked pads from within its
729 * activate_pull function.
731 * If you don't know what this is, you probably don't want to call it.
733 * Returns: TRUE if the operation was successful.
738 gst_pad_activate_pull (GstPad * pad, gboolean active)
740 GstActivateMode old, new;
743 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
745 GST_OBJECT_LOCK (pad);
746 old = GST_PAD_ACTIVATE_MODE (pad);
747 GST_OBJECT_UNLOCK (pad);
751 case GST_ACTIVATE_PULL:
752 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
754 case GST_ACTIVATE_PUSH:
755 GST_DEBUG_OBJECT (pad,
756 "activating pad from push, deactivate push first");
757 /* pad was activate in the wrong direction, deactivate it
758 * and reactivate it in pull mode */
759 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
760 goto deactivate_failed;
761 /* fallthrough, pad is deactivated now. */
762 case GST_ACTIVATE_NONE:
763 GST_DEBUG_OBJECT (pad, "activating pad from none");
768 case GST_ACTIVATE_NONE:
769 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
771 case GST_ACTIVATE_PUSH:
772 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
773 /* pad was activated in the other direction, deactivate it
774 * in push mode, this should not happen... */
775 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
776 goto deactivate_failed;
777 /* everything is fine now */
779 case GST_ACTIVATE_PULL:
780 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
785 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
786 if ((peer = gst_pad_get_peer (pad))) {
787 GST_DEBUG_OBJECT (pad, "calling peer");
788 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
790 gst_object_unref (peer);
792 /* there is no peer, this is only fatal when we activate. When we
793 * deactivate, we must assume the application has unlinked the peer and
794 * will deactivate it eventually. */
798 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
801 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
802 goto failure; /* Can't activate pull on a src without a
806 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
807 pre_activate (pad, new);
809 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
810 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
813 /* can happen for sinks of passthrough elements */
816 post_activate (pad, new);
818 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
819 active ? "activated" : "deactivated");
825 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
826 active ? "activated" : "deactivated");
831 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
832 "failed to %s in switch to pull from mode %d",
833 (active ? "activate" : "deactivate"), old);
838 GST_OBJECT_LOCK (peer);
839 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
840 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
841 GST_OBJECT_UNLOCK (peer);
842 gst_object_unref (peer);
847 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
853 GST_OBJECT_LOCK (pad);
854 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
855 active ? "activate" : "deactivate");
856 GST_PAD_SET_FLUSHING (pad);
857 GST_PAD_ACTIVATE_MODE (pad) = old;
858 GST_OBJECT_UNLOCK (pad);
864 * gst_pad_activate_push:
865 * @pad: the #GstPad to activate or deactivate.
866 * @active: whether the pad should be active or not.
868 * Activates or deactivates the given pad in push mode via dispatching to the
869 * pad's activatepushfunc. For use from within pad activation functions only.
871 * If you don't know what this is, you probably don't want to call it.
873 * Returns: %TRUE if the operation was successful.
878 gst_pad_activate_push (GstPad * pad, gboolean active)
880 GstActivateMode old, new;
882 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
883 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
884 active ? "activated" : "deactivated");
886 GST_OBJECT_LOCK (pad);
887 old = GST_PAD_ACTIVATE_MODE (pad);
888 GST_OBJECT_UNLOCK (pad);
892 case GST_ACTIVATE_PUSH:
893 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
895 case GST_ACTIVATE_PULL:
896 GST_DEBUG_OBJECT (pad,
897 "activating pad from push, deactivating pull first");
898 /* pad was activate in the wrong direction, deactivate it
899 * an reactivate it in push mode */
900 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
901 goto deactivate_failed;
902 /* fallthrough, pad is deactivated now. */
903 case GST_ACTIVATE_NONE:
904 GST_DEBUG_OBJECT (pad, "activating pad from none");
909 case GST_ACTIVATE_NONE:
910 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
912 case GST_ACTIVATE_PULL:
913 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
914 /* pad was activated in the other direction, deactivate it
915 * in pull mode, this should not happen... */
916 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
917 goto deactivate_failed;
918 /* everything is fine now */
920 case GST_ACTIVATE_PUSH:
921 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
926 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
927 pre_activate (pad, new);
929 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
930 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
934 /* quite ok, element relies on state change func to prepare itself */
937 post_activate (pad, new);
939 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
940 active ? "activated" : "deactivated");
945 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
946 active ? "activated" : "deactivated");
951 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
952 "failed to %s in switch to push from mode %d",
953 (active ? "activate" : "deactivate"), old);
958 GST_OBJECT_LOCK (pad);
959 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
960 active ? "activate" : "deactivate");
961 GST_PAD_SET_FLUSHING (pad);
962 GST_PAD_ACTIVATE_MODE (pad) = old;
963 GST_OBJECT_UNLOCK (pad);
970 * @pad: the #GstPad to query
972 * Query if a pad is active
974 * Returns: TRUE if the pad is active.
979 gst_pad_is_active (GstPad * pad)
981 gboolean result = FALSE;
983 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
985 GST_OBJECT_LOCK (pad);
986 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
987 GST_OBJECT_UNLOCK (pad);
993 * gst_pad_set_blocked_async_full:
994 * @pad: the #GstPad to block or unblock
995 * @blocked: boolean indicating whether the pad should be blocked or unblocked
996 * @callback: #GstPadBlockCallback that will be called when the
998 * @user_data: user data passed to the callback
999 * @destroy_data: #GDestroyNotify for user_data
1001 * Blocks or unblocks the dataflow on a pad. The provided callback
1002 * is called when the operation succeeds; this happens right before the next
1003 * attempt at pushing a buffer on the pad.
1005 * This can take a while as the pad can only become blocked when real dataflow
1007 * When the pipeline is stalled, for example in PAUSED, this can
1008 * take an indeterminate amount of time.
1009 * You can pass NULL as the callback to make this call block. Be careful with
1010 * this blocking call as it might not return for reasons stated above.
1012 * Returns: TRUE if the pad could be blocked. This function can fail if the
1013 * wrong parameters were passed or the pad was already in the requested state.
1020 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1021 GstPadBlockCallback callback, gpointer user_data,
1022 GDestroyNotify destroy_data)
1024 gboolean was_blocked = FALSE;
1026 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1028 GST_OBJECT_LOCK (pad);
1030 was_blocked = GST_PAD_IS_BLOCKED (pad);
1032 if (G_UNLIKELY (was_blocked == blocked))
1033 goto had_right_state;
1036 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1038 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1040 if (pad->block_destroy_data && pad->block_data)
1041 pad->block_destroy_data (pad->block_data);
1043 pad->block_callback = callback;
1044 pad->block_data = user_data;
1045 pad->block_destroy_data = destroy_data;
1046 pad->abidata.ABI.block_callback_called = FALSE;
1048 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1049 GST_PAD_BLOCK_WAIT (pad);
1050 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1053 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1055 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1057 if (pad->block_destroy_data && pad->block_data)
1058 pad->block_destroy_data (pad->block_data);
1060 pad->block_callback = callback;
1061 pad->block_data = user_data;
1062 pad->block_destroy_data = destroy_data;
1063 pad->abidata.ABI.block_callback_called = FALSE;
1065 GST_PAD_BLOCK_BROADCAST (pad);
1067 /* no callback, wait for the unblock to happen */
1068 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1069 GST_PAD_BLOCK_WAIT (pad);
1070 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1073 GST_OBJECT_UNLOCK (pad);
1079 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1080 "pad was in right state (%d)", was_blocked);
1081 GST_OBJECT_UNLOCK (pad);
1088 * gst_pad_set_blocked_async:
1089 * @pad: the #GstPad to block or unblock
1090 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1091 * @callback: #GstPadBlockCallback that will be called when the
1092 * operation succeeds
1093 * @user_data: user data passed to the callback
1095 * Blocks or unblocks the dataflow on a pad. The provided callback
1096 * is called when the operation succeeds; this happens right before the next
1097 * attempt at pushing a buffer on the pad.
1099 * This can take a while as the pad can only become blocked when real dataflow
1101 * When the pipeline is stalled, for example in PAUSED, this can
1102 * take an indeterminate amount of time.
1103 * You can pass NULL as the callback to make this call block. Be careful with
1104 * this blocking call as it might not return for reasons stated above.
1106 * Returns: TRUE if the pad could be blocked. This function can fail if the
1107 * wrong parameters were passed or the pad was already in the requested state.
1112 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1113 GstPadBlockCallback callback, gpointer user_data)
1115 return gst_pad_set_blocked_async_full (pad, blocked,
1116 callback, user_data, NULL);
1120 * gst_pad_set_blocked:
1121 * @pad: the #GstPad to block or unblock
1122 * @blocked: boolean indicating we should block or unblock
1124 * Blocks or unblocks the dataflow on a pad. This function is
1125 * a shortcut for gst_pad_set_blocked_async() with a NULL
1128 * Returns: TRUE if the pad could be blocked. This function can fail if the
1129 * wrong parameters were passed or the pad was already in the requested state.
1134 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1136 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1140 * gst_pad_is_blocked:
1141 * @pad: the #GstPad to query
1143 * Checks if the pad is blocked or not. This function returns the
1144 * last requested state of the pad. It is not certain that the pad
1145 * is actually blocking at this point (see gst_pad_is_blocking()).
1147 * Returns: TRUE if the pad is blocked.
1152 gst_pad_is_blocked (GstPad * pad)
1154 gboolean result = FALSE;
1156 g_return_val_if_fail (GST_IS_PAD (pad), result);
1158 GST_OBJECT_LOCK (pad);
1159 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1160 GST_OBJECT_UNLOCK (pad);
1166 * gst_pad_is_blocking:
1167 * @pad: the #GstPad to query
1169 * Checks if the pad is blocking or not. This is a guaranteed state
1170 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1172 * Returns: TRUE if the pad is blocking.
1179 gst_pad_is_blocking (GstPad * pad)
1181 gboolean result = FALSE;
1183 g_return_val_if_fail (GST_IS_PAD (pad), result);
1185 GST_OBJECT_LOCK (pad);
1186 /* the blocking flag is only valid if the pad is not flushing */
1187 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1188 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1189 GST_OBJECT_UNLOCK (pad);
1195 * gst_pad_set_activate_function:
1197 * @activate: the #GstPadActivateFunction to set.
1199 * Sets the given activate function for @pad. The activate function will
1200 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1201 * the actual activation. Only makes sense to set on sink pads.
1203 * Call this function if your sink pad can start a pull-based task.
1206 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1208 g_return_if_fail (GST_IS_PAD (pad));
1210 GST_PAD_ACTIVATEFUNC (pad) = activate;
1211 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1212 GST_DEBUG_FUNCPTR_NAME (activate));
1216 * gst_pad_set_activatepull_function:
1218 * @activatepull: the #GstPadActivateModeFunction to set.
1220 * Sets the given activate_pull function for the pad. An activate_pull function
1221 * prepares the element and any upstream connections for pulling. See XXX
1222 * part-activation.txt for details.
1225 gst_pad_set_activatepull_function (GstPad * pad,
1226 GstPadActivateModeFunction activatepull)
1228 g_return_if_fail (GST_IS_PAD (pad));
1230 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1231 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1232 GST_DEBUG_FUNCPTR_NAME (activatepull));
1236 * gst_pad_set_activatepush_function:
1238 * @activatepush: the #GstPadActivateModeFunction to set.
1240 * Sets the given activate_push function for the pad. An activate_push function
1241 * prepares the element for pushing. See XXX part-activation.txt for details.
1244 gst_pad_set_activatepush_function (GstPad * pad,
1245 GstPadActivateModeFunction activatepush)
1247 g_return_if_fail (GST_IS_PAD (pad));
1249 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1250 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1251 GST_DEBUG_FUNCPTR_NAME (activatepush));
1255 * gst_pad_set_chain_function:
1256 * @pad: a sink #GstPad.
1257 * @chain: the #GstPadChainFunction to set.
1259 * Sets the given chain function for the pad. The chain function is called to
1260 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1263 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1265 g_return_if_fail (GST_IS_PAD (pad));
1266 g_return_if_fail (GST_PAD_IS_SINK (pad));
1268 GST_PAD_CHAINFUNC (pad) = chain;
1269 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1270 GST_DEBUG_FUNCPTR_NAME (chain));
1274 * gst_pad_set_chain_list_function:
1275 * @pad: a sink #GstPad.
1276 * @chainlist: the #GstPadChainListFunction to set.
1278 * Sets the given chain list function for the pad. The chainlist function is
1279 * called to process a #GstBufferList input buffer list. See
1280 * #GstPadChainListFunction for more details.
1285 gst_pad_set_chain_list_function (GstPad * pad,
1286 GstPadChainListFunction chainlist)
1288 g_return_if_fail (GST_IS_PAD (pad));
1289 g_return_if_fail (GST_PAD_IS_SINK (pad));
1291 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1292 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1293 GST_DEBUG_FUNCPTR_NAME (chainlist));
1297 * gst_pad_set_getrange_function:
1298 * @pad: a source #GstPad.
1299 * @get: the #GstPadGetRangeFunction to set.
1301 * Sets the given getrange function for the pad. The getrange function is
1302 * called to produce a new #GstBuffer to start the processing pipeline. see
1303 * #GstPadGetRangeFunction for a description of the getrange function.
1306 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1308 g_return_if_fail (GST_IS_PAD (pad));
1309 g_return_if_fail (GST_PAD_IS_SRC (pad));
1311 GST_PAD_GETRANGEFUNC (pad) = get;
1313 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1314 GST_DEBUG_FUNCPTR_NAME (get));
1318 * gst_pad_set_checkgetrange_function:
1319 * @pad: a source #GstPad.
1320 * @check: the #GstPadCheckGetRangeFunction to set.
1322 * Sets the given checkgetrange function for the pad. Implement this function
1323 * on a pad if you dynamically support getrange based scheduling on the pad.
1326 gst_pad_set_checkgetrange_function (GstPad * pad,
1327 GstPadCheckGetRangeFunction check)
1329 g_return_if_fail (GST_IS_PAD (pad));
1330 g_return_if_fail (GST_PAD_IS_SRC (pad));
1332 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1334 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1335 GST_DEBUG_FUNCPTR_NAME (check));
1339 * gst_pad_set_event_function:
1340 * @pad: a #GstPad of either direction.
1341 * @event: the #GstPadEventFunction to set.
1343 * Sets the given event handler for the pad.
1346 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1348 g_return_if_fail (GST_IS_PAD (pad));
1350 GST_PAD_EVENTFUNC (pad) = event;
1352 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1353 GST_DEBUG_FUNCPTR_NAME (event));
1357 * gst_pad_set_query_function:
1358 * @pad: a #GstPad of either direction.
1359 * @query: the #GstPadQueryFunction to set.
1361 * Set the given query function for the pad.
1364 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1366 g_return_if_fail (GST_IS_PAD (pad));
1368 GST_PAD_QUERYFUNC (pad) = query;
1370 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1371 GST_DEBUG_FUNCPTR_NAME (query));
1375 * gst_pad_set_query_type_function:
1376 * @pad: a #GstPad of either direction.
1377 * @type_func: the #GstPadQueryTypeFunction to set.
1379 * Set the given query type function for the pad.
1382 gst_pad_set_query_type_function (GstPad * pad,
1383 GstPadQueryTypeFunction type_func)
1385 g_return_if_fail (GST_IS_PAD (pad));
1387 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1389 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1390 GST_DEBUG_FUNCPTR_NAME (type_func));
1394 * gst_pad_get_query_types:
1397 * Get an array of supported queries that can be performed
1400 * Returns: a zero-terminated array of #GstQueryType.
1402 const GstQueryType *
1403 gst_pad_get_query_types (GstPad * pad)
1405 GstPadQueryTypeFunction func;
1407 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1409 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1421 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1423 *data = gst_pad_get_query_types (pad);
1429 * gst_pad_get_query_types_default:
1432 * Invoke the default dispatcher for the query types on
1435 * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1436 * internally-linked pads has a query types function.
1438 const GstQueryType *
1439 gst_pad_get_query_types_default (GstPad * pad)
1441 GstQueryType *result = NULL;
1443 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1445 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1446 gst_pad_get_query_types_dispatcher, &result);
1452 * gst_pad_set_iterate_internal_links_function:
1453 * @pad: a #GstPad of either direction.
1454 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1456 * Sets the given internal link iterator function for the pad.
1461 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1462 GstPadIterIntLinkFunction iterintlink)
1464 g_return_if_fail (GST_IS_PAD (pad));
1466 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1467 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1468 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1472 * gst_pad_set_internal_link_function:
1473 * @pad: a #GstPad of either direction.
1474 * @intlink: the #GstPadIntLinkFunction to set.
1476 * Sets the given internal link function for the pad.
1478 * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1480 #ifndef GST_REMOVE_DEPRECATED
1481 #ifdef GST_DISABLE_DEPRECATED
1483 gst_pad_set_internal_link_function (GstPad * pad,
1484 GstPadIntLinkFunction intlink);
1487 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1489 g_return_if_fail (GST_IS_PAD (pad));
1491 GST_PAD_INTLINKFUNC (pad) = intlink;
1492 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1493 GST_DEBUG_FUNCPTR_NAME (intlink));
1495 #endif /* GST_REMOVE_DEPRECATED */
1498 * gst_pad_set_link_function:
1500 * @link: the #GstPadLinkFunction to set.
1502 * Sets the given link function for the pad. It will be called when
1503 * the pad is linked with another pad.
1505 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1508 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1509 * cannot be made for some reason.
1511 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1512 * of the peer sink pad, if present.
1515 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1517 g_return_if_fail (GST_IS_PAD (pad));
1519 GST_PAD_LINKFUNC (pad) = link;
1520 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1521 GST_DEBUG_FUNCPTR_NAME (link));
1525 * gst_pad_set_unlink_function:
1527 * @unlink: the #GstPadUnlinkFunction to set.
1529 * Sets the given unlink function for the pad. It will be called
1530 * when the pad is unlinked.
1533 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1535 g_return_if_fail (GST_IS_PAD (pad));
1537 GST_PAD_UNLINKFUNC (pad) = unlink;
1538 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1539 GST_DEBUG_FUNCPTR_NAME (unlink));
1543 * gst_pad_set_getcaps_function:
1545 * @getcaps: the #GstPadGetCapsFunction to set.
1547 * Sets the given getcaps function for the pad. @getcaps should return the
1548 * allowable caps for a pad in the context of the element's state, its link to
1549 * other elements, and the devices or files it has opened. These caps must be a
1550 * subset of the pad template caps. In the NULL state with no links, @getcaps
1551 * should ideally return the same caps as the pad template. In rare
1552 * circumstances, an object property can affect the caps returned by @getcaps,
1553 * but this is discouraged.
1555 * You do not need to call this function if @pad's allowed caps are always the
1556 * same as the pad template caps. This can only be true if the padtemplate
1557 * has fixed simple caps.
1559 * For most filters, the caps returned by @getcaps is directly affected by the
1560 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1561 * the srcpad's getcaps function is directly related to the stream data. Again,
1562 * @getcaps should return the most specific caps it reasonably can, since this
1563 * helps with autoplugging.
1565 * Note that the return value from @getcaps is owned by the caller, so the
1566 * caller should unref the caps after usage.
1569 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1571 g_return_if_fail (GST_IS_PAD (pad));
1573 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1574 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1575 GST_DEBUG_FUNCPTR_NAME (getcaps));
1579 * gst_pad_set_acceptcaps_function:
1581 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1583 * Sets the given acceptcaps function for the pad. The acceptcaps function
1584 * will be called to check if the pad can accept the given caps. Setting the
1585 * acceptcaps function to NULL restores the default behaviour of allowing
1586 * any caps that matches the caps from gst_pad_get_caps().
1589 gst_pad_set_acceptcaps_function (GstPad * pad,
1590 GstPadAcceptCapsFunction acceptcaps)
1592 g_return_if_fail (GST_IS_PAD (pad));
1594 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1595 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1596 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1600 * gst_pad_set_fixatecaps_function:
1602 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1604 * Sets the given fixatecaps function for the pad. The fixatecaps function
1605 * will be called whenever the default values for a GstCaps needs to be
1609 gst_pad_set_fixatecaps_function (GstPad * pad,
1610 GstPadFixateCapsFunction fixatecaps)
1612 g_return_if_fail (GST_IS_PAD (pad));
1614 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1615 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1616 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1620 * gst_pad_set_setcaps_function:
1622 * @setcaps: the #GstPadSetCapsFunction to set.
1624 * Sets the given setcaps function for the pad. The setcaps function
1625 * will be called whenever a buffer with a new media type is pushed or
1626 * pulled from the pad. The pad/element needs to update its internal
1627 * structures to process the new media type. If this new type is not
1628 * acceptable, the setcaps function should return FALSE.
1631 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1633 g_return_if_fail (GST_IS_PAD (pad));
1635 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1636 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1637 GST_DEBUG_FUNCPTR_NAME (setcaps));
1641 * gst_pad_set_bufferalloc_function:
1642 * @pad: a sink #GstPad.
1643 * @bufalloc: the #GstPadBufferAllocFunction to set.
1645 * Sets the given bufferalloc function for the pad. Note that the
1646 * bufferalloc function can only be set on sinkpads.
1649 gst_pad_set_bufferalloc_function (GstPad * pad,
1650 GstPadBufferAllocFunction bufalloc)
1652 g_return_if_fail (GST_IS_PAD (pad));
1653 g_return_if_fail (GST_PAD_IS_SINK (pad));
1655 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1656 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1657 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1662 * @srcpad: the source #GstPad to unlink.
1663 * @sinkpad: the sink #GstPad to unlink.
1665 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1666 * signal on both pads.
1668 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1669 * the pads were not linked together.
1674 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1676 gboolean result = FALSE;
1677 GstElement *parent = NULL;
1679 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1680 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1681 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1682 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1684 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1685 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1686 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1688 /* We need to notify the parent before taking any pad locks as the bin in
1689 * question might be waiting for a lock on the pad while holding its lock
1690 * that our message will try to take. */
1691 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1692 if (GST_IS_ELEMENT (parent)) {
1693 gst_element_post_message (parent,
1694 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1695 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1697 gst_object_unref (parent);
1702 GST_OBJECT_LOCK (srcpad);
1704 GST_OBJECT_LOCK (sinkpad);
1706 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1707 goto not_linked_together;
1709 if (GST_PAD_UNLINKFUNC (srcpad)) {
1710 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1712 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1713 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1716 /* first clear peers */
1717 GST_PAD_PEER (srcpad) = NULL;
1718 GST_PAD_PEER (sinkpad) = NULL;
1720 GST_OBJECT_UNLOCK (sinkpad);
1721 GST_OBJECT_UNLOCK (srcpad);
1723 /* fire off a signal to each of the pads telling them
1724 * that they've been unlinked */
1725 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1726 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1728 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1729 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1734 if (parent != NULL) {
1735 gst_element_post_message (parent,
1736 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1737 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1738 gst_object_unref (parent);
1743 not_linked_together:
1745 /* we do not emit a warning in this case because unlinking cannot
1746 * be made MT safe.*/
1747 GST_OBJECT_UNLOCK (sinkpad);
1748 GST_OBJECT_UNLOCK (srcpad);
1754 * gst_pad_is_linked:
1755 * @pad: pad to check
1757 * Checks if a @pad is linked to another pad or not.
1759 * Returns: TRUE if the pad is linked, FALSE otherwise.
1764 gst_pad_is_linked (GstPad * pad)
1768 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1770 GST_OBJECT_LOCK (pad);
1771 result = (GST_PAD_PEER (pad) != NULL);
1772 GST_OBJECT_UNLOCK (pad);
1777 /* get the caps from both pads and see if the intersection
1780 * This function should be called with the pad LOCK on both
1784 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1785 GstPadLinkCheck flags)
1787 GstCaps *srccaps = NULL;
1788 GstCaps *sinkcaps = NULL;
1789 gboolean compatible = FALSE;
1791 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1794 /* Doing the expensive caps checking takes priority over only checking the template caps */
1795 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1796 srccaps = gst_pad_get_caps_unlocked (src);
1797 sinkcaps = gst_pad_get_caps_unlocked (sink);
1799 if (GST_PAD_PAD_TEMPLATE (src))
1801 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1802 if (GST_PAD_PAD_TEMPLATE (sink))
1804 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1807 GST_CAT_DEBUG (GST_CAT_CAPS, "src caps %" GST_PTR_FORMAT, srccaps);
1808 GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1810 /* if we have caps on both pads we can check the intersection. If one
1811 * of the caps is NULL, we return TRUE. */
1812 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1814 gst_caps_unref (srccaps);
1816 gst_caps_unref (sinkcaps);
1820 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1821 gst_caps_unref (srccaps);
1822 gst_caps_unref (sinkcaps);
1825 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1826 (compatible ? "" : "not"));
1831 /* check if the grandparents of both pads are the same.
1832 * This check is required so that we don't try to link
1833 * pads from elements in different bins without ghostpads.
1835 * The LOCK should be held on both pads
1838 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1840 GstObject *psrc, *psink;
1842 psrc = GST_OBJECT_PARENT (src);
1843 psink = GST_OBJECT_PARENT (sink);
1845 /* if one of the pads has no parent, we allow the link */
1846 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1849 /* only care about parents that are elements */
1850 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1851 goto no_element_parent;
1853 /* if the parents are the same, we have a loop */
1854 if (G_UNLIKELY (psrc == psink))
1857 /* if they both have a parent, we check the grandparents. We can not lock
1858 * the parent because we hold on the child (pad) and the locking order is
1859 * parent >> child. */
1860 psrc = GST_OBJECT_PARENT (psrc);
1861 psink = GST_OBJECT_PARENT (psink);
1863 /* if they have grandparents but they are not the same */
1864 if (G_UNLIKELY (psrc != psink))
1865 goto wrong_grandparents;
1872 GST_CAT_DEBUG (GST_CAT_CAPS,
1873 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1874 GST_PTR_FORMAT, psrc, psink);
1879 GST_CAT_DEBUG (GST_CAT_CAPS,
1880 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1881 GST_PTR_FORMAT, psrc, psink);
1886 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1892 GST_CAT_DEBUG (GST_CAT_CAPS,
1893 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1894 GST_PTR_FORMAT, psrc, psink);
1899 /* FIXME leftover from an attempt at refactoring... */
1900 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1901 * the two pads will be locked in the srcpad, sinkpad order. */
1902 static GstPadLinkReturn
1903 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1905 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1906 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1908 GST_OBJECT_LOCK (srcpad);
1910 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1911 goto src_was_linked;
1913 GST_OBJECT_LOCK (sinkpad);
1915 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1916 goto sink_was_linked;
1918 /* check hierarchy, pads can only be linked if the grandparents
1920 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1921 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1922 goto wrong_hierarchy;
1924 /* check pad caps for non-empty intersection */
1925 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1928 /* FIXME check pad scheduling for non-empty intersection */
1930 return GST_PAD_LINK_OK;
1934 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1935 GST_DEBUG_PAD_NAME (srcpad),
1936 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1937 /* we do not emit a warning in this case because unlinking cannot
1938 * be made MT safe.*/
1939 GST_OBJECT_UNLOCK (srcpad);
1940 return GST_PAD_LINK_WAS_LINKED;
1944 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1945 GST_DEBUG_PAD_NAME (sinkpad),
1946 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1947 /* we do not emit a warning in this case because unlinking cannot
1948 * be made MT safe.*/
1949 GST_OBJECT_UNLOCK (sinkpad);
1950 GST_OBJECT_UNLOCK (srcpad);
1951 return GST_PAD_LINK_WAS_LINKED;
1955 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1956 GST_OBJECT_UNLOCK (sinkpad);
1957 GST_OBJECT_UNLOCK (srcpad);
1958 return GST_PAD_LINK_WRONG_HIERARCHY;
1962 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1963 GST_OBJECT_UNLOCK (sinkpad);
1964 GST_OBJECT_UNLOCK (srcpad);
1965 return GST_PAD_LINK_NOFORMAT;
1971 * @srcpad: the source #GstPad.
1972 * @sinkpad: the sink #GstPad.
1974 * Checks if the source pad and the sink pad are compatible so they can be
1977 * Returns: TRUE if the pads can be linked.
1980 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1982 GstPadLinkReturn result;
1984 /* generic checks */
1985 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1986 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1988 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1989 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1991 /* gst_pad_link_prepare does everything for us, we only release the locks
1992 * on the pads that it gets us. If this function returns !OK the locks are not
1994 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1995 if (result != GST_PAD_LINK_OK)
1998 GST_OBJECT_UNLOCK (srcpad);
1999 GST_OBJECT_UNLOCK (sinkpad);
2002 return result == GST_PAD_LINK_OK;
2006 * gst_pad_link_full:
2007 * @srcpad: the source #GstPad to link.
2008 * @sinkpad: the sink #GstPad to link.
2009 * @flags: the checks to validate when linking
2011 * Links the source pad and the sink pad.
2013 * This variant of #gst_pad_link provides a more granular control on the
2014 * checks being done when linking. While providing some considerable speedups
2015 * the caller of this method must be aware that wrong usage of those flags
2016 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2017 * for more information.
2019 * Returns: A result code indicating if the connection worked or
2027 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2029 GstPadLinkReturn result;
2032 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2033 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2034 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2035 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2036 GST_PAD_LINK_WRONG_DIRECTION);
2038 /* Notify the parent early. See gst_pad_unlink for details. */
2039 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2040 if (GST_IS_ELEMENT (parent)) {
2041 gst_element_post_message (parent,
2042 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2043 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2045 gst_object_unref (parent);
2050 /* prepare will also lock the two pads */
2051 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2053 if (result != GST_PAD_LINK_OK)
2056 /* must set peers before calling the link function */
2057 GST_PAD_PEER (srcpad) = sinkpad;
2058 GST_PAD_PEER (sinkpad) = srcpad;
2060 GST_OBJECT_UNLOCK (sinkpad);
2061 GST_OBJECT_UNLOCK (srcpad);
2063 /* FIXME released the locks here, concurrent thread might link
2064 * something else. */
2065 if (GST_PAD_LINKFUNC (srcpad)) {
2066 /* this one will call the peer link function */
2067 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2068 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2069 /* if no source link function, we need to call the sink link
2070 * function ourselves. */
2071 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2073 result = GST_PAD_LINK_OK;
2076 GST_OBJECT_LOCK (srcpad);
2077 GST_OBJECT_LOCK (sinkpad);
2079 if (result == GST_PAD_LINK_OK) {
2080 GST_OBJECT_UNLOCK (sinkpad);
2081 GST_OBJECT_UNLOCK (srcpad);
2083 /* fire off a signal to each of the pads telling them
2084 * that they've been linked */
2085 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2086 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2088 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2089 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2091 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2092 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2094 GST_PAD_PEER (srcpad) = NULL;
2095 GST_PAD_PEER (sinkpad) = NULL;
2097 GST_OBJECT_UNLOCK (sinkpad);
2098 GST_OBJECT_UNLOCK (srcpad);
2103 gst_element_post_message (parent,
2104 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2105 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2106 gst_object_unref (parent);
2114 * @srcpad: the source #GstPad to link.
2115 * @sinkpad: the sink #GstPad to link.
2117 * Links the source pad and the sink pad.
2119 * Returns: A result code indicating if the connection worked or
2125 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2127 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2131 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2133 GstPadTemplate **template_p;
2135 /* this function would need checks if it weren't static */
2137 GST_OBJECT_LOCK (pad);
2138 template_p = &pad->padtemplate;
2139 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2140 GST_OBJECT_UNLOCK (pad);
2143 gst_pad_template_pad_created (templ, pad);
2147 * gst_pad_get_pad_template:
2150 * Gets the template for @pad.
2152 * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2153 * if this pad has no template.
2155 * FIXME: currently returns an unrefcounted padtemplate.
2158 gst_pad_get_pad_template (GstPad * pad)
2160 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2162 return GST_PAD_PAD_TEMPLATE (pad);
2166 /* should be called with the pad LOCK held */
2167 /* refs the caps, so caller is responsible for getting it unreffed */
2169 gst_pad_get_caps_unlocked (GstPad * pad)
2171 GstCaps *result = NULL;
2172 GstPadTemplate *templ;
2174 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2176 if (GST_PAD_GETCAPSFUNC (pad)) {
2177 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2178 "dispatching to pad getcaps function");
2180 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2181 GST_OBJECT_UNLOCK (pad);
2182 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2183 GST_OBJECT_LOCK (pad);
2184 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2186 if (result == NULL) {
2187 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2188 GST_DEBUG_PAD_NAME (pad));
2190 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2191 "pad getcaps returned %" GST_PTR_FORMAT, result);
2192 #ifndef G_DISABLE_ASSERT
2193 /* check that the returned caps are a real subset of the template caps */
2194 if (GST_PAD_PAD_TEMPLATE (pad)) {
2195 const GstCaps *templ_caps =
2196 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2197 if (!gst_caps_is_subset (result, templ_caps)) {
2200 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2201 "pad returned caps %" GST_PTR_FORMAT
2202 " which are not a real subset of its template caps %"
2203 GST_PTR_FORMAT, result, templ_caps);
2205 ("pad %s:%s returned caps which are not a real "
2206 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2207 temp = gst_caps_intersect (templ_caps, result);
2208 gst_caps_unref (result);
2216 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2217 result = GST_PAD_TEMPLATE_CAPS (templ);
2218 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2219 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2222 result = gst_caps_ref (result);
2225 if ((result = GST_PAD_CAPS (pad))) {
2226 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2227 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2229 result = gst_caps_ref (result);
2233 /* this almost never happens */
2234 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2235 result = gst_caps_new_empty ();
2241 /* FIXME-0.11: what about making this the default and using
2242 * gst_caps_make_writable() explicitely where needed
2245 * gst_pad_get_caps_reffed:
2246 * @pad: a #GstPad to get the capabilities of.
2248 * Gets the capabilities this pad can produce or consume. Preferred function if
2249 * one only wants to read or intersect the caps.
2251 * Returns: the caps of the pad with incremented ref-count.
2256 gst_pad_get_caps_reffed (GstPad * pad)
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 pad caps");
2266 result = gst_pad_get_caps_unlocked (pad);
2268 GST_OBJECT_UNLOCK (pad);
2275 * @pad: a #GstPad to get the capabilities of.
2277 * Gets the capabilities this pad can produce or consume.
2278 * Note that this method doesn't necessarily return the caps set by
2279 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2280 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2281 * the pad's get_caps function;
2282 * this returns the pad template caps if not explicitly set.
2284 * Returns: a newly allocated copy of the #GstCaps of this pad.
2289 gst_pad_get_caps (GstPad * pad)
2291 GstCaps *result = gst_pad_get_caps_reffed (pad);
2293 /* be sure that we have a copy */
2294 if (G_LIKELY (result))
2295 result = gst_caps_make_writable (result);
2300 /* FIXME-0.11: what about making this the default and using
2301 * gst_caps_make_writable() explicitely where needed
2304 * gst_pad_peer_get_caps_reffed:
2305 * @pad: a #GstPad to get the capabilities of.
2307 * Gets the capabilities of the peer connected to this pad. Preferred function
2308 * if one only wants to read or intersect the caps.
2310 * Returns: the caps of the pad with incremented ref-count.
2315 gst_pad_peer_get_caps_reffed (GstPad * pad)
2318 GstCaps *result = NULL;
2320 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2322 GST_OBJECT_LOCK (pad);
2324 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2326 peerpad = GST_PAD_PEER (pad);
2327 if (G_UNLIKELY (peerpad == NULL))
2330 gst_object_ref (peerpad);
2331 GST_OBJECT_UNLOCK (pad);
2333 result = gst_pad_get_caps_reffed (peerpad);
2335 gst_object_unref (peerpad);
2341 GST_OBJECT_UNLOCK (pad);
2347 * gst_pad_peer_get_caps:
2348 * @pad: a #GstPad to get the peer capabilities of.
2350 * Gets the capabilities of the peer connected to this pad. Similar to
2351 * gst_pad_get_caps().
2353 * Returns: a newly allocated copy of the #GstCaps of the peer pad. Use
2354 * gst_caps_unref() to get rid of it. This function returns %NULL if there is
2358 gst_pad_peer_get_caps (GstPad * pad)
2361 GstCaps *result = NULL;
2363 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2365 GST_OBJECT_LOCK (pad);
2367 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2369 peerpad = GST_PAD_PEER (pad);
2370 if (G_UNLIKELY (peerpad == NULL))
2373 gst_object_ref (peerpad);
2374 GST_OBJECT_UNLOCK (pad);
2376 result = gst_pad_get_caps (peerpad);
2378 gst_object_unref (peerpad);
2384 GST_OBJECT_UNLOCK (pad);
2390 fixate_value (GValue * dest, const GValue * src)
2392 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2393 g_value_init (dest, G_TYPE_INT);
2394 g_value_set_int (dest, gst_value_get_int_range_min (src));
2395 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2396 g_value_init (dest, G_TYPE_DOUBLE);
2397 g_value_set_double (dest, gst_value_get_double_range_min (src));
2398 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2399 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2400 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2401 GValue temp = { 0 };
2403 /* list could be empty */
2404 if (gst_value_list_get_size (src) <= 0)
2407 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2409 if (!fixate_value (dest, &temp))
2410 gst_value_init_and_copy (dest, &temp);
2411 g_value_unset (&temp);
2412 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2413 gboolean res = FALSE;
2416 len = gst_value_array_get_size (src);
2417 g_value_init (dest, GST_TYPE_ARRAY);
2418 for (n = 0; n < len; n++) {
2420 const GValue *orig_kid = gst_value_array_get_value (src, n);
2422 if (!fixate_value (&kid, orig_kid))
2423 gst_value_init_and_copy (&kid, orig_kid);
2426 gst_value_array_append_value (dest, &kid);
2427 g_value_unset (&kid);
2431 g_value_unset (dest);
2442 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2444 GstStructure *s = data;
2447 if (fixate_value (&v, value)) {
2448 gst_structure_id_set_value (s, field_id, &v);
2456 * gst_pad_fixate_caps:
2457 * @pad: a #GstPad to fixate
2458 * @caps: the #GstCaps to fixate
2460 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2461 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2464 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2466 GstPadFixateCapsFunction fixatefunc;
2469 g_return_if_fail (GST_IS_PAD (pad));
2470 g_return_if_fail (caps != NULL);
2471 g_return_if_fail (!gst_caps_is_empty (caps));
2472 /* FIXME-0.11: do not allow fixating any-caps
2473 * g_return_if_fail (!gst_caps_is_any (caps));
2476 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2479 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2481 fixatefunc (pad, caps);
2484 /* default fixation */
2485 gst_caps_truncate (caps);
2486 s = gst_caps_get_structure (caps, 0);
2487 gst_structure_foreach (s, gst_pad_default_fixate, s);
2490 /* Default accept caps implementation just checks against
2491 * against the allowed caps for the pad */
2493 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2495 /* get the caps and see if it intersects to something not empty */
2497 gboolean result = FALSE;
2499 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2501 allowed = gst_pad_get_caps_reffed (pad);
2503 goto nothing_allowed;
2505 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2507 result = gst_caps_can_intersect (allowed, caps);
2509 gst_caps_unref (allowed);
2516 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2522 * gst_pad_accept_caps:
2523 * @pad: a #GstPad to check
2524 * @caps: a #GstCaps to check on the pad
2526 * Check if the given pad accepts the caps.
2528 * Returns: TRUE if the pad can accept the caps.
2531 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2534 GstPadAcceptCapsFunction acceptfunc;
2535 GstCaps *existing = NULL;
2537 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2539 /* any pad can be unnegotiated */
2543 /* lock for checking the existing caps */
2544 GST_OBJECT_LOCK (pad);
2545 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2546 /* The current caps on a pad are trivially acceptable */
2547 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2548 if (caps == existing || gst_caps_is_equal (caps, existing))
2551 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2552 GST_OBJECT_UNLOCK (pad);
2554 if (G_LIKELY (acceptfunc)) {
2555 /* we can call the function */
2556 result = acceptfunc (pad, caps);
2557 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2559 /* Only null if the element explicitly unset it */
2560 result = gst_pad_acceptcaps_default (pad, caps);
2561 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2567 GST_DEBUG_OBJECT (pad, "pad had same caps");
2568 GST_OBJECT_UNLOCK (pad);
2574 * gst_pad_peer_accept_caps:
2575 * @pad: a #GstPad to check the peer of
2576 * @caps: a #GstCaps to check on the pad
2578 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2581 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2584 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2589 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2591 GST_OBJECT_LOCK (pad);
2593 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2595 peerpad = GST_PAD_PEER (pad);
2596 if (G_UNLIKELY (peerpad == NULL))
2599 gst_object_ref (peerpad);
2600 /* release lock before calling external methods but keep ref to pad */
2601 GST_OBJECT_UNLOCK (pad);
2603 result = gst_pad_accept_caps (peerpad, caps);
2605 gst_object_unref (peerpad);
2611 GST_OBJECT_UNLOCK (pad);
2618 * @pad: a #GstPad to set the capabilities of.
2619 * @caps: a #GstCaps to set.
2621 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2622 * caps on the pad will be unreffed. This function refs the caps so you should
2623 * unref if as soon as you don't need it anymore.
2624 * It is possible to set NULL caps, which will make the pad unnegotiated
2627 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2628 * or bad parameters were provided to this function.
2633 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2635 GstPadSetCapsFunction setcaps;
2638 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2639 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2641 GST_OBJECT_LOCK (pad);
2642 existing = GST_PAD_CAPS (pad);
2643 if (existing == caps)
2646 if (gst_caps_is_equal (caps, existing))
2647 goto setting_same_caps;
2649 setcaps = GST_PAD_SETCAPSFUNC (pad);
2651 /* call setcaps function to configure the pad only if the
2652 * caps is not NULL */
2653 if (setcaps != NULL && caps) {
2654 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2655 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2656 GST_OBJECT_UNLOCK (pad);
2657 if (!setcaps (pad, caps))
2659 GST_OBJECT_LOCK (pad);
2660 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2662 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2666 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2667 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2669 GST_OBJECT_UNLOCK (pad);
2671 g_object_notify (G_OBJECT (pad), "caps");
2677 GST_OBJECT_UNLOCK (pad);
2682 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2683 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2684 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2686 GST_OBJECT_UNLOCK (pad);
2693 GST_OBJECT_LOCK (pad);
2694 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2695 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2696 "caps %" GST_PTR_FORMAT " could not be set", caps);
2697 GST_OBJECT_UNLOCK (pad);
2704 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2708 /* See if pad accepts the caps */
2709 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2712 /* set caps on pad if call succeeds */
2713 res = gst_pad_set_caps (pad, caps);
2714 /* no need to unref the caps here, set_caps takes a ref and
2715 * our ref goes away when we leave this function. */
2721 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2722 "caps %" GST_PTR_FORMAT " not accepted", caps);
2727 /* returns TRUE if the src pad could be configured to accept the given caps */
2729 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2734 /* See if pad accepts the caps */
2735 if (!gst_pad_accept_caps (pad, caps))
2738 res = gst_pad_set_caps (pad, caps);
2746 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2747 "caps %" GST_PTR_FORMAT " not accepted", caps);
2753 * gst_pad_get_pad_template_caps:
2754 * @pad: a #GstPad to get the template capabilities from.
2756 * Gets the capabilities for @pad's template.
2758 * Returns: the #GstCaps of this pad template. If you intend to keep a
2759 * reference on the caps, make a copy (see gst_caps_copy ()).
2762 gst_pad_get_pad_template_caps (GstPad * pad)
2764 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2766 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2768 if (GST_PAD_PAD_TEMPLATE (pad))
2769 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2771 return gst_static_caps_get (&anycaps);
2776 * @pad: a #GstPad to get the peer of.
2778 * Gets the peer of @pad. This function refs the peer pad so
2779 * you need to unref it after use.
2781 * Returns: the peer #GstPad. Unref after usage.
2786 gst_pad_get_peer (GstPad * pad)
2790 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2792 GST_OBJECT_LOCK (pad);
2793 result = GST_PAD_PEER (pad);
2795 gst_object_ref (result);
2796 GST_OBJECT_UNLOCK (pad);
2802 * gst_pad_get_allowed_caps:
2805 * Gets the capabilities of the allowed media types that can flow through
2806 * @pad and its peer.
2808 * The allowed capabilities is calculated as the intersection of the results of
2809 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2810 * on the resulting caps.
2812 * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2813 * longer need it. This function returns NULL when @pad has no peer.
2818 gst_pad_get_allowed_caps (GstPad * pad)
2825 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2827 GST_OBJECT_LOCK (pad);
2829 peer = GST_PAD_PEER (pad);
2830 if (G_UNLIKELY (peer == NULL))
2833 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2835 gst_object_ref (peer);
2836 GST_OBJECT_UNLOCK (pad);
2837 mycaps = gst_pad_get_caps_reffed (pad);
2839 peercaps = gst_pad_get_caps_reffed (peer);
2840 gst_object_unref (peer);
2842 caps = gst_caps_intersect (mycaps, peercaps);
2843 gst_caps_unref (peercaps);
2844 gst_caps_unref (mycaps);
2846 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2853 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2854 GST_OBJECT_UNLOCK (pad);
2861 * gst_pad_get_negotiated_caps:
2864 * Gets the capabilities of the media type that currently flows through @pad
2867 * This function can be used on both src and sinkpads. Note that srcpads are
2868 * always negotiated before sinkpads so it is possible that the negotiated caps
2869 * on the srcpad do not match the negotiated caps of the peer.
2871 * Returns: the negotiated #GstCaps of the pad link. Unref the caps when
2872 * you no longer need it. This function returns NULL when the @pad has no
2873 * peer or is not negotiated yet.
2878 gst_pad_get_negotiated_caps (GstPad * pad)
2883 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2885 GST_OBJECT_LOCK (pad);
2887 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2890 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2892 caps = GST_PAD_CAPS (pad);
2894 gst_caps_ref (caps);
2895 GST_OBJECT_UNLOCK (pad);
2897 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2904 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2905 GST_OBJECT_UNLOCK (pad);
2911 /* calls the buffer_alloc function on the given pad */
2912 static GstFlowReturn
2913 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2914 GstCaps * caps, GstBuffer ** buf)
2917 GstPadBufferAllocFunction bufferallocfunc;
2919 GST_OBJECT_LOCK (pad);
2920 /* when the pad is flushing we cannot give a buffer */
2921 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2924 bufferallocfunc = pad->bufferallocfunc;
2926 if (offset == GST_BUFFER_OFFSET_NONE) {
2927 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2928 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2929 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2931 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2932 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2933 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2934 bufferallocfunc, size, offset);
2936 GST_OBJECT_UNLOCK (pad);
2938 /* G_LIKELY for now since most elements don't implement a buffer alloc
2939 * function and there is no default alloc proxy function as this is usually
2941 if (G_LIKELY (bufferallocfunc == NULL))
2944 ret = bufferallocfunc (pad, offset, size, caps, buf);
2946 if (G_UNLIKELY (ret != GST_FLOW_OK))
2949 /* no error, but NULL buffer means fallback to the default */
2950 if (G_UNLIKELY (*buf == NULL))
2953 /* If the buffer alloc function didn't set up the caps like it should,
2955 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2956 GST_WARNING_OBJECT (pad,
2957 "Buffer allocation function did not set caps. Setting");
2958 gst_buffer_set_caps (*buf, caps);
2964 /* pad was flushing */
2965 GST_OBJECT_UNLOCK (pad);
2966 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2967 return GST_FLOW_WRONG_STATE;
2971 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2972 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2977 /* fallback case, allocate a buffer of our own, add pad caps. */
2978 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2980 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2981 GST_BUFFER_OFFSET (*buf) = offset;
2982 gst_buffer_set_caps (*buf, caps);
2985 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2986 "out of memory allocating %d bytes", size);
2987 return GST_FLOW_ERROR;
2992 /* FIXME 0.11: size should be unsigned */
2993 static GstFlowReturn
2994 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2995 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
3000 gboolean caps_changed;
3002 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3003 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3004 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
3005 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
3007 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
3008 GST_PTR_FORMAT, offset, size, caps);
3010 GST_OBJECT_LOCK (pad);
3011 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3012 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3015 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3018 gst_object_ref (peer);
3019 GST_OBJECT_UNLOCK (pad);
3021 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
3022 gst_object_unref (peer);
3024 if (G_UNLIKELY (ret != GST_FLOW_OK))
3027 /* FIXME, move capnego this into a base class? */
3028 newcaps = GST_BUFFER_CAPS (*buf);
3030 /* Lock for checking caps, pretty pointless as the _pad_push() function might
3031 * change it concurrently, one of the problems with automatic caps setting in
3032 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
3033 * when there is heavy renegotiation going on in both directions. */
3034 GST_OBJECT_LOCK (pad);
3035 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
3036 GST_OBJECT_UNLOCK (pad);
3038 /* we got a new datatype on the pad, see if it can handle it */
3039 if (G_UNLIKELY (caps_changed)) {
3040 GST_DEBUG_OBJECT (pad,
3041 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3042 GST_PAD_CAPS (pad), newcaps, newcaps);
3043 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
3044 goto not_negotiated;
3047 /* sanity check (only if caps are the same) */
3048 if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
3049 goto wrong_size_fallback;
3055 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
3056 GST_OBJECT_UNLOCK (pad);
3061 /* pad has no peer */
3062 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3063 "called bufferallocfunc but had no peer");
3064 GST_OBJECT_UNLOCK (pad);
3065 return GST_FLOW_NOT_LINKED;
3069 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3070 "alloc function returned error %s", gst_flow_get_name (ret));
3075 gst_buffer_unref (*buf);
3077 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3078 "alloc function returned unacceptable buffer");
3079 return GST_FLOW_NOT_NEGOTIATED;
3081 wrong_size_fallback:
3083 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3084 "function is too small (%u < %d), doing fallback buffer alloc",
3085 GST_BUFFER_SIZE (*buf), size);
3087 gst_buffer_unref (*buf);
3089 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3090 GST_BUFFER_OFFSET (*buf) = offset;
3091 gst_buffer_set_caps (*buf, caps);
3094 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3095 "out of memory allocating %d bytes", size);
3096 return GST_FLOW_ERROR;
3102 * gst_pad_alloc_buffer:
3103 * @pad: a source #GstPad
3104 * @offset: the offset of the new buffer in the stream
3105 * @size: the size of the new buffer
3106 * @caps: the caps of the new buffer
3107 * @buf: a newly allocated buffer
3109 * Allocates a new, empty buffer optimized to push to pad @pad. This
3110 * function only works if @pad is a source pad and has a peer.
3112 * A new, empty #GstBuffer will be put in the @buf argument.
3113 * You need to check the caps of the buffer after performing this
3114 * function and renegotiate to the format if needed. If the caps changed, it is
3115 * possible that the buffer returned in @buf is not of the right size for the
3116 * new format, @buf needs to be unreffed and reallocated if this is the case.
3118 * Returns: a result code indicating success of the operation. Any
3119 * result code other than #GST_FLOW_OK is an error and @buf should
3121 * An error can occur if the pad is not connected or when the downstream
3122 * peer elements cannot provide an acceptable buffer.
3127 /* FIXME 0.11: size should be unsigned */
3129 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3132 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3136 * gst_pad_alloc_buffer_and_set_caps:
3137 * @pad: a source #GstPad
3138 * @offset: the offset of the new buffer in the stream
3139 * @size: the size of the new buffer
3140 * @caps: the caps of the new buffer
3141 * @buf: a newly allocated buffer
3143 * In addition to the function gst_pad_alloc_buffer(), this function
3144 * automatically calls gst_pad_set_caps() when the caps of the
3145 * newly allocated buffer are different from the @pad caps.
3147 * After a renegotiation, the size of the new buffer returned in @buf could
3148 * be of the wrong size for the new format and must be unreffed an reallocated
3151 * Returns: a result code indicating success of the operation. Any
3152 * result code other than #GST_FLOW_OK is an error and @buf should
3154 * An error can occur if the pad is not connected or when the downstream
3155 * peer elements cannot provide an acceptable buffer.
3160 /* FIXME 0.11: size should be unsigned */
3162 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3163 GstCaps * caps, GstBuffer ** buf)
3165 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3169 #ifndef GST_REMOVE_DEPRECATED
3177 int_link_iter_data_free (IntLinkIterData * data)
3179 g_list_free (data->list);
3180 g_slice_free (IntLinkIterData, data);
3184 static GstIteratorItem
3185 iterate_pad (GstIterator * it, GstPad * pad)
3187 gst_object_ref (pad);
3188 return GST_ITERATOR_ITEM_PASS;
3192 * gst_pad_iterate_internal_links_default:
3193 * @pad: the #GstPad to get the internal links of.
3195 * Iterate the list of pads to which the given pad is linked to inside of
3196 * the parent element.
3197 * This is the default handler, and thus returns an iterator of all of the
3198 * pads inside the parent element with opposite direction.
3200 * The caller must free this iterator after use with gst_iterator_free().
3202 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3203 * returned pad with gst_object_unref().
3208 gst_pad_iterate_internal_links_default (GstPad * pad)
3215 GstIteratorDisposeFunction dispose;
3217 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3219 #ifndef GST_REMOVE_DEPRECATED
3220 /* when we get here, the default handler for the iterate links is called,
3221 * which means that the user has not installed a custom one. We first check if
3222 * there is maybe a custom legacy function we can call. */
3223 if (GST_PAD_INTLINKFUNC (pad) &&
3224 GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3225 IntLinkIterData *data;
3227 /* make an iterator for the list. We can't protect the list with a
3228 * cookie. If we would take the cookie of the parent element, we need to
3229 * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3230 * cache the per-pad list and invalidate the list when a new call to
3231 * INTLINKFUNC() returned a different list but then this would only work if
3232 * two concurrent iterators were used and the last iterator would still be
3233 * thread-unsafe. Just don't use this method anymore. */
3234 data = g_slice_new (IntLinkIterData);
3235 data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3238 GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3240 cookie = &data->cookie;
3241 padlist = &data->list;
3243 dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3244 /* reuse the pad lock, it's all we have here */
3245 lock = GST_OBJECT_GET_LOCK (pad);
3251 GST_OBJECT_LOCK (pad);
3252 parent = GST_PAD_PARENT (pad);
3253 if (!parent || !GST_IS_ELEMENT (parent))
3256 gst_object_ref (parent);
3257 GST_OBJECT_UNLOCK (pad);
3259 if (pad->direction == GST_PAD_SRC)
3260 padlist = &parent->sinkpads;
3262 padlist = &parent->srcpads;
3264 GST_DEBUG_OBJECT (pad, "Making iterator");
3266 cookie = &parent->pads_cookie;
3268 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3269 lock = GST_OBJECT_GET_LOCK (parent);
3272 res = gst_iterator_new_list (GST_TYPE_PAD,
3273 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3281 GST_OBJECT_UNLOCK (pad);
3282 GST_DEBUG_OBJECT (pad, "no parent element");
3288 * gst_pad_iterate_internal_links:
3289 * @pad: the GstPad to get the internal links of.
3291 * Gets an iterator for the pads to which the given pad is linked to inside
3292 * of the parent element.
3294 * Each #GstPad element yielded by the iterator will have its refcount increased,
3295 * so unref after use.
3297 * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3298 * iterator function configured. Use gst_iterator_free() after usage.
3303 gst_pad_iterate_internal_links (GstPad * pad)
3305 GstIterator *res = NULL;
3307 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3309 if (GST_PAD_ITERINTLINKFUNC (pad))
3310 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3315 #ifndef GST_REMOVE_DEPRECATED
3317 add_unref_pad_to_list (GstPad * pad, GList ** list)
3319 *list = g_list_prepend (*list, pad);
3320 gst_object_unref (pad);
3325 * gst_pad_get_internal_links_default:
3326 * @pad: the #GstPad to get the internal links of.
3328 * Gets a list of pads to which the given pad is linked to
3329 * inside of the parent element.
3330 * This is the default handler, and thus returns a list of all of the
3331 * pads inside the parent element with opposite direction.
3333 * The caller must free this list after use with g_list_free().
3335 * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3339 * Deprecated: This function does not ref the pads in the list so that they
3340 * could become invalid by the time the application accesses them. It's also
3341 * possible that the list changes while handling the pads, which the caller of
3342 * this function is unable to know. Use the thread-safe
3343 * gst_pad_iterate_internal_links_default() instead.
3345 #ifndef GST_REMOVE_DEPRECATED
3347 gst_pad_get_internal_links_default (GstPad * pad)
3352 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3354 GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3356 /* when we get here, the default handler for get_internal_links is called,
3357 * which means that the user has not installed a custom one. We first check if
3358 * there is maybe a custom iterate function we can call. */
3359 if (GST_PAD_ITERINTLINKFUNC (pad) &&
3360 GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3362 GstIteratorResult ires;
3363 gboolean done = FALSE;
3365 it = gst_pad_iterate_internal_links (pad);
3366 /* loop over the iterator and put all elements into a list, we also
3367 * immediatly unref them, which is bad. */
3369 ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, &res);
3371 case GST_ITERATOR_OK:
3372 case GST_ITERATOR_DONE:
3373 case GST_ITERATOR_ERROR:
3376 case GST_ITERATOR_RESYNC:
3377 /* restart, discard previous list */
3378 gst_iterator_resync (it);
3385 gst_iterator_free (it);
3387 /* lock pad, check and ref parent */
3388 GST_OBJECT_LOCK (pad);
3389 parent = GST_PAD_PARENT (pad);
3390 if (!parent || !GST_IS_ELEMENT (parent))
3393 parent = gst_object_ref (parent);
3394 GST_OBJECT_UNLOCK (pad);
3396 /* now lock the parent while we copy the pads */
3397 GST_OBJECT_LOCK (parent);
3398 if (pad->direction == GST_PAD_SRC)
3399 res = g_list_copy (parent->sinkpads);
3401 res = g_list_copy (parent->srcpads);
3402 GST_OBJECT_UNLOCK (parent);
3404 gst_object_unref (parent);
3407 /* At this point pads can be changed and unreffed. Nothing we can do about it
3408 * because for compatibility reasons this function cannot ref the pads or
3409 * notify the app that the list changed. */
3415 GST_DEBUG_OBJECT (pad, "no parent");
3416 GST_OBJECT_UNLOCK (pad);
3420 #endif /* GST_REMOVE_DEPRECATED */
3423 * gst_pad_get_internal_links:
3424 * @pad: the #GstPad to get the internal links of.
3426 * Gets a list of pads to which the given pad is linked to
3427 * inside of the parent element.
3428 * The caller must free this list after use.
3432 * Returns: a newly allocated #GList of pads, free with g_list_free().
3434 * Deprecated: This function does not ref the pads in the list so that they
3435 * could become invalid by the time the application accesses them. It's also
3436 * possible that the list changes while handling the pads, which the caller of
3437 * this function is unable to know. Use the thread-safe
3438 * gst_pad_iterate_internal_links() instead.
3440 #ifndef GST_REMOVE_DEPRECATED
3441 #ifdef GST_DISABLE_DEPRECATED
3442 GList *gst_pad_get_internal_links (GstPad * pad);
3445 gst_pad_get_internal_links (GstPad * pad)
3449 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3451 GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3453 if (GST_PAD_INTLINKFUNC (pad))
3454 res = GST_PAD_INTLINKFUNC (pad) (pad);
3458 #endif /* GST_REMOVE_DEPRECATED */
3461 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3463 gboolean result = FALSE;
3465 gboolean done = FALSE;
3468 GList *pushed_pads = NULL;
3470 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3471 event, GST_EVENT_TYPE_NAME (event));
3473 iter = gst_pad_iterate_internal_links (pad);
3479 switch (gst_iterator_next (iter, &item)) {
3480 case GST_ITERATOR_OK:
3481 eventpad = GST_PAD_CAST (item);
3483 /* if already pushed, skip */
3484 if (g_list_find (pushed_pads, eventpad)) {
3485 gst_object_unref (item);
3489 if (GST_PAD_IS_SRC (eventpad)) {
3490 /* for each pad we send to, we should ref the event; it's up
3491 * to downstream to unref again when handled. */
3492 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3493 event, GST_EVENT_TYPE_NAME (event),
3494 GST_DEBUG_PAD_NAME (eventpad));
3495 gst_event_ref (event);
3496 result |= gst_pad_push_event (eventpad, event);
3498 /* we only send the event on one pad, multi-sinkpad elements
3499 * should implement a handler */
3500 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3501 event, GST_EVENT_TYPE_NAME (event),
3502 GST_DEBUG_PAD_NAME (eventpad));
3503 result = gst_pad_push_event (eventpad, event);
3508 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3510 gst_object_unref (item);
3512 case GST_ITERATOR_RESYNC:
3513 /* FIXME, if we want to reset the result value we need to remember which
3514 * pads pushed with which result */
3515 gst_iterator_resync (iter);
3517 case GST_ITERATOR_ERROR:
3518 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3521 case GST_ITERATOR_DONE:
3526 gst_iterator_free (iter);
3530 /* If this is a sinkpad and we don't have pads to send the event to, we
3531 * return TRUE. This is so that when using the default handler on a sink
3532 * element, we don't fail to push it. */
3534 result = GST_PAD_IS_SINK (pad);
3536 g_list_free (pushed_pads);
3538 /* we handled the incoming event so we unref once */
3540 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3541 gst_event_unref (event);
3548 * gst_pad_event_default:
3549 * @pad: a #GstPad to call the default event handler on.
3550 * @event: the #GstEvent to handle.
3552 * Invokes the default event handler for the given pad. End-of-stream and
3553 * discontinuity events are handled specially, and then the event is sent to all
3554 * pads internally linked to @pad. Note that if there are many possible sink
3555 * pads that are internally linked to @pad, only one will be sent an event.
3556 * Multi-sinkpad elements should implement custom event handlers.
3558 * Returns: TRUE if the event was sent succesfully.
3561 gst_pad_event_default (GstPad * pad, GstEvent * event)
3563 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3564 g_return_val_if_fail (event != NULL, FALSE);
3566 GST_LOG_OBJECT (pad, "default event handler");
3568 switch (GST_EVENT_TYPE (event)) {
3571 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3572 gst_pad_pause_task (pad);
3579 return gst_pad_event_default_dispatch (pad, event);
3583 * gst_pad_dispatcher:
3584 * @pad: a #GstPad to dispatch.
3585 * @dispatch: the #GstPadDispatcherFunction to call.
3586 * @data: gpointer user data passed to the dispatcher function.
3588 * Invokes the given dispatcher function on each respective peer of
3589 * all pads that are internally linked to the given pad.
3590 * The GstPadDispatcherFunction should return TRUE when no further pads
3591 * need to be processed.
3593 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3596 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3599 gboolean res = FALSE;
3600 GstIterator *iter = NULL;
3601 gboolean done = FALSE;
3604 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3605 g_return_val_if_fail (dispatch != NULL, FALSE);
3607 iter = gst_pad_iterate_internal_links (pad);
3613 switch (gst_iterator_next (iter, &item)) {
3614 case GST_ITERATOR_OK:
3616 GstPad *int_pad = GST_PAD_CAST (item);
3617 GstPad *int_peer = gst_pad_get_peer (int_pad);
3620 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3621 GST_DEBUG_PAD_NAME (int_peer));
3622 done = res = dispatch (int_peer, data);
3623 gst_object_unref (int_peer);
3625 GST_DEBUG_OBJECT (int_pad, "no peer");
3628 gst_object_unref (item);
3630 case GST_ITERATOR_RESYNC:
3631 gst_iterator_resync (iter);
3633 case GST_ITERATOR_ERROR:
3635 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3637 case GST_ITERATOR_DONE:
3642 gst_iterator_free (iter);
3644 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3653 * @pad: a #GstPad to invoke the default query on.
3654 * @query: the #GstQuery to perform.
3656 * Dispatches a query to a pad. The query should have been allocated by the
3657 * caller via one of the type-specific allocation functions in gstquery.h. The
3658 * element is responsible for filling the query with an appropriate response,
3659 * which should then be parsed with a type-specific query parsing function.
3661 * Again, the caller is responsible for both the allocation and deallocation of
3662 * the query structure.
3664 * Returns: TRUE if the query could be performed.
3667 gst_pad_query (GstPad * pad, GstQuery * query)
3669 GstPadQueryFunction func;
3671 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3672 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3674 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3676 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3679 return func (pad, query);
3683 GST_DEBUG_OBJECT (pad, "had no query function");
3689 * gst_pad_peer_query:
3690 * @pad: a #GstPad to invoke the peer query on.
3691 * @query: the #GstQuery to perform.
3693 * Performs gst_pad_query() on the peer of @pad.
3695 * The caller is responsible for both the allocation and deallocation of
3696 * the query structure.
3698 * Returns: TRUE if the query could be performed. This function returns %FALSE
3699 * if @pad has no peer.
3704 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3709 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3710 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3712 GST_OBJECT_LOCK (pad);
3714 GST_DEBUG_OBJECT (pad, "peer query");
3716 peerpad = GST_PAD_PEER (pad);
3717 if (G_UNLIKELY (peerpad == NULL))
3720 gst_object_ref (peerpad);
3721 GST_OBJECT_UNLOCK (pad);
3723 result = gst_pad_query (peerpad, query);
3725 gst_object_unref (peerpad);
3732 GST_WARNING_OBJECT (pad, "pad has no peer");
3733 GST_OBJECT_UNLOCK (pad);
3739 * gst_pad_query_default:
3740 * @pad: a #GstPad to call the default query handler on.
3741 * @query: the #GstQuery to handle.
3743 * Invokes the default query handler for the given pad.
3744 * The query is sent to all pads internally linked to @pad. Note that
3745 * if there are many possible sink pads that are internally linked to
3746 * @pad, only one will be sent the query.
3747 * Multi-sinkpad elements should implement custom query handlers.
3749 * Returns: TRUE if the query was performed succesfully.
3752 gst_pad_query_default (GstPad * pad, GstQuery * query)
3754 switch (GST_QUERY_TYPE (query)) {
3755 case GST_QUERY_POSITION:
3756 case GST_QUERY_SEEKING:
3757 case GST_QUERY_FORMATS:
3758 case GST_QUERY_LATENCY:
3759 case GST_QUERY_JITTER:
3760 case GST_QUERY_RATE:
3761 case GST_QUERY_CONVERT:
3763 return gst_pad_dispatcher
3764 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3768 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
3769 /* FIXME: why isn't this on a GstElement ? */
3771 * gst_pad_load_and_link:
3772 * @self: an #xmlNodePtr to read the description from.
3773 * @parent: the #GstObject element that owns the pad.
3775 * Reads the pad definition from the XML node and links the given pad
3776 * in the element to a pad of an element up in the hierarchy.
3779 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3781 xmlNodePtr field = self->xmlChildrenNode;
3782 GstPad *pad = NULL, *targetpad;
3783 GstPadTemplate *tmpl;
3787 GstObject *grandparent;
3791 if (!strcmp ((char *) field->name, "name")) {
3792 name = (gchar *) xmlNodeGetContent (field);
3793 pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3794 if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3795 && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3796 pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3798 } else if (!strcmp ((char *) field->name, "peer")) {
3799 peer = (gchar *) xmlNodeGetContent (field);
3801 field = field->next;
3803 g_return_if_fail (pad != NULL);
3808 split = g_strsplit (peer, ".", 2);
3810 if (split[0] == NULL || split[1] == NULL) {
3811 GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3812 "Could not parse peer '%s', leaving unlinked", peer);
3819 g_return_if_fail (split[0] != NULL);
3820 g_return_if_fail (split[1] != NULL);
3822 grandparent = gst_object_get_parent (parent);
3824 if (grandparent && GST_IS_BIN (grandparent)) {
3825 target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3832 targetpad = gst_element_get_static_pad (target, split[1]);
3834 targetpad = gst_element_get_request_pad (target, split[1]);
3836 if (targetpad == NULL)
3839 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3840 gst_pad_link (pad, targetpad);
3842 gst_pad_link (targetpad, pad);
3849 * gst_pad_save_thyself:
3850 * @pad: a #GstPad to save.
3851 * @parent: the parent #xmlNodePtr to save the description in.
3853 * Saves the pad into an xml representation.
3855 * Returns: the #xmlNodePtr representation of the pad.
3858 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3863 g_return_val_if_fail (GST_IS_PAD (object), NULL);
3865 pad = GST_PAD_CAST (object);
3867 xmlNewChild (parent, NULL, (xmlChar *) "name",
3868 (xmlChar *) GST_PAD_NAME (pad));
3870 if (GST_PAD_IS_SRC (pad)) {
3871 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3872 } else if (GST_PAD_IS_SINK (pad)) {
3873 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3875 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3878 if (GST_PAD_PEER (pad) != NULL) {
3881 peer = GST_PAD_PEER (pad);
3882 /* first check to see if the peer's parent's parent is the same */
3883 /* we just save it off */
3884 content = g_strdup_printf ("%s.%s",
3885 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3886 xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3889 xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3896 * gst_ghost_pad_save_thyself:
3897 * @pad: a ghost #GstPad to save.
3898 * @parent: the parent #xmlNodePtr to save the description in.
3900 * Saves the ghost pad into an xml representation.
3902 * Returns: the #xmlNodePtr representation of the pad.
3905 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3909 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3911 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3912 xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3913 xmlNewChild (self, NULL, (xmlChar *) "parent",
3914 (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3916 /* FIXME FIXME FIXME! */
3921 #endif /* GST_DISABLE_LOADSAVE */
3924 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3925 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3928 * This function performs the pad blocking when an event, buffer push
3929 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3930 * streaming thread after informing the pad has been blocked.
3932 * An application can with this method wait and block any streaming
3933 * thread and perform operations such as seeking or linking.
3935 * Two methods are available for notifying the application of the
3937 * - the callback method, which happens in the STREAMING thread with
3938 * the STREAM_LOCK held. With this method, the most useful way of
3939 * dealing with the callback is to post a message to the main thread
3940 * where the pad block can then be handled outside of the streaming
3941 * thread. With the last method one can perform all operations such
3942 * as doing a state change, linking, unblocking, seeking etc on the
3944 * - the GCond signal method, which makes any thread unblock when
3945 * the pad block happens.
3947 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3948 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3952 static GstFlowReturn
3953 handle_pad_block (GstPad * pad)
3955 GstPadBlockCallback callback;
3957 GstFlowReturn ret = GST_FLOW_OK;
3959 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3961 /* flushing, don't bother trying to block and return WRONG_STATE
3963 if (GST_PAD_IS_FLUSHING (pad))
3964 goto flushingnonref;
3966 /* we grab an extra ref for the callbacks, this is probably not
3967 * needed (callback code does not have a ref and cannot unref). I
3968 * think this was done to make it possible to unref the element in
3969 * the callback, which is in the end totally impossible as it
3970 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3971 * all taken when calling this function. */
3972 gst_object_ref (pad);
3974 while (GST_PAD_IS_BLOCKED (pad)) {
3976 /* we either have a callback installed to notify the block or
3977 * some other thread is doing a GCond wait. */
3978 callback = pad->block_callback;
3979 pad->abidata.ABI.block_callback_called = TRUE;
3981 /* there is a callback installed, call it. We release the
3982 * lock so that the callback can do something usefull with the
3984 user_data = pad->block_data;
3985 GST_OBJECT_UNLOCK (pad);
3986 callback (pad, TRUE, user_data);
3987 GST_OBJECT_LOCK (pad);
3989 /* we released the lock, recheck flushing */
3990 if (GST_PAD_IS_FLUSHING (pad))
3993 /* no callback, signal the thread that is doing a GCond wait
3995 GST_PAD_BLOCK_BROADCAST (pad);
3997 } while (pad->abidata.ABI.block_callback_called == FALSE
3998 && GST_PAD_IS_BLOCKED (pad));
4000 /* OBJECT_LOCK could have been released when we did the callback, which
4001 * then could have made the pad unblock so we need to check the blocking
4002 * condition again. */
4003 if (!GST_PAD_IS_BLOCKED (pad))
4006 /* now we block the streaming thread. It can be unlocked when we
4007 * deactivate the pad (which will also set the FLUSHING flag) or
4008 * when the pad is unblocked. A flushing event will also unblock
4009 * the pad after setting the FLUSHING flag. */
4010 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4011 "Waiting to be unblocked or set flushing");
4012 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
4013 GST_PAD_BLOCK_WAIT (pad);
4014 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
4016 /* see if we got unblocked by a flush or not */
4017 if (GST_PAD_IS_FLUSHING (pad))
4021 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
4023 /* when we get here, the pad is unblocked again and we perform
4024 * the needed unblock code. */
4025 callback = pad->block_callback;
4027 /* we need to call the callback */
4028 user_data = pad->block_data;
4029 GST_OBJECT_UNLOCK (pad);
4030 callback (pad, FALSE, user_data);
4031 GST_OBJECT_LOCK (pad);
4033 /* we need to signal the thread waiting on the GCond */
4034 GST_PAD_BLOCK_BROADCAST (pad);
4037 gst_object_unref (pad);
4043 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
4044 return GST_FLOW_WRONG_STATE;
4048 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
4049 gst_object_unref (pad);
4050 return GST_FLOW_WRONG_STATE;
4054 /**********************************************************************
4055 * Data passing functions
4059 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
4062 GValue args[2] = { {0}, {0} };
4067 g_value_init (&ret, G_TYPE_BOOLEAN);
4068 g_value_set_boolean (&ret, TRUE);
4069 g_value_init (&args[0], GST_TYPE_PAD);
4070 g_value_set_object (&args[0], pad);
4071 g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4072 gst_value_set_mini_object (&args[1], obj);
4074 if (GST_IS_EVENT (obj))
4075 detail = event_quark;
4077 detail = buffer_quark;
4080 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4081 res = g_value_get_boolean (&ret);
4084 g_value_unset (&ret);
4085 g_value_unset (&args[0]);
4086 g_value_unset (&args[1]);
4092 gst_pad_data_unref (gboolean is_buffer, void *data)
4094 if (G_LIKELY (is_buffer)) {
4095 gst_buffer_unref (data);
4097 gst_buffer_list_unref (data);
4102 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4106 if (G_LIKELY (is_buffer)) {
4107 caps = GST_BUFFER_CAPS (data);
4111 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4112 caps = GST_BUFFER_CAPS (buf);
4119 /* this is the chain function that does not perform the additional argument
4120 * checking for that little extra speed.
4122 static inline GstFlowReturn
4123 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
4126 gboolean caps_changed;
4128 gboolean emit_signal;
4130 GST_PAD_STREAM_LOCK (pad);
4132 GST_OBJECT_LOCK (pad);
4133 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4136 caps = gst_pad_data_get_caps (is_buffer, data);
4137 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4139 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4140 GST_OBJECT_UNLOCK (pad);
4142 /* see if the signal should be emited, we emit before caps nego as
4143 * we might drop the buffer and do capsnego for nothing. */
4144 if (G_UNLIKELY (emit_signal)) {
4145 if (G_LIKELY (is_buffer)) {
4146 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4149 /* chain all groups in the buffer list one by one to avoid problems with
4150 * buffer probes that push buffers or events */
4155 /* we got a new datatype on the pad, see if it can handle it */
4156 if (G_UNLIKELY (caps_changed)) {
4157 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4158 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4159 goto not_negotiated;
4162 /* NOTE: we read the chainfunc unlocked.
4163 * we cannot hold the lock for the pad so we might send
4164 * the data to the wrong function. This is not really a
4165 * problem since functions are assigned at creation time
4166 * and don't change that often... */
4167 if (G_LIKELY (is_buffer)) {
4168 GstPadChainFunction chainfunc;
4170 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4173 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4174 "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4176 ret = chainfunc (pad, GST_BUFFER_CAST (data));
4178 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4179 "called chainfunction &%s, returned %s",
4180 GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4182 GstPadChainListFunction chainlistfunc;
4184 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4187 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4188 "calling chainlistfunction &%s",
4189 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4191 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4193 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4194 "called chainlistfunction &%s, returned %s",
4195 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4198 GST_PAD_STREAM_UNLOCK (pad);
4204 GstBufferList *list;
4205 GstBufferListIterator *it;
4208 GST_PAD_STREAM_UNLOCK (pad);
4210 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4212 list = GST_BUFFER_LIST_CAST (data);
4213 it = gst_buffer_list_iterate (list);
4215 if (gst_buffer_list_iterator_next_group (it)) {
4217 group = gst_buffer_list_iterator_merge_group (it);
4218 if (group == NULL) {
4219 group = gst_buffer_new ();
4220 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4222 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4224 ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4225 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4227 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4228 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4231 gst_buffer_list_iterator_free (it);
4232 gst_buffer_list_unref (list);
4240 gst_pad_data_unref (is_buffer, data);
4241 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4242 "pushing, but pad was flushing");
4243 GST_OBJECT_UNLOCK (pad);
4244 GST_PAD_STREAM_UNLOCK (pad);
4245 return GST_FLOW_WRONG_STATE;
4249 gst_pad_data_unref (is_buffer, data);
4250 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4251 GST_PAD_STREAM_UNLOCK (pad);
4256 gst_pad_data_unref (is_buffer, data);
4257 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4258 "pushing data but pad did not accept");
4259 GST_PAD_STREAM_UNLOCK (pad);
4260 return GST_FLOW_NOT_NEGOTIATED;
4264 gst_pad_data_unref (is_buffer, data);
4265 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4266 "pushing, but not chainhandler");
4267 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4268 ("push on pad %s:%s but it has no chainfunction",
4269 GST_DEBUG_PAD_NAME (pad)));
4270 GST_PAD_STREAM_UNLOCK (pad);
4271 return GST_FLOW_NOT_SUPPORTED;
4277 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4278 * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4280 * Chain a buffer to @pad.
4282 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4284 * If the caps on @buffer are different from the current caps on @pad, this
4285 * function will call any setcaps function (see gst_pad_set_setcaps_function())
4286 * installed on @pad. If the new caps are not acceptable for @pad, this
4287 * function returns #GST_FLOW_NOT_NEGOTIATED.
4289 * The function proceeds calling the chain function installed on @pad (see
4290 * gst_pad_set_chain_function()) and the return value of that function is
4291 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4294 * In all cases, success or failure, the caller loses its reference to @buffer
4295 * after calling this function.
4297 * Returns: a #GstFlowReturn from the pad.
4302 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4304 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4305 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4306 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4308 return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4312 * gst_pad_chain_list:
4313 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4314 * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4316 * Chain a bufferlist to @pad.
4318 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4320 * If the caps on the first buffer of @list are different from the current
4321 * caps on @pad, this function will call any setcaps function
4322 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4323 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4325 * The function proceeds calling the chainlist function installed on @pad (see
4326 * gst_pad_set_chain_list_function()) and the return value of that function is
4327 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4328 * chainlist function.
4330 * In all cases, success or failure, the caller loses its reference to @list
4331 * after calling this function.
4335 * Returns: a #GstFlowReturn from the pad.
4340 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4342 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4343 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4344 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4346 return gst_pad_chain_data_unchecked (pad, FALSE, list);
4349 static GstFlowReturn
4350 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4355 gboolean caps_changed;
4357 GST_OBJECT_LOCK (pad);
4359 /* FIXME: this check can go away; pad_set_blocked could be implemented with
4360 * probes completely or probes with an extended pad block. */
4361 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4362 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4365 /* we emit signals on the pad arg, the peer will have a chance to
4366 * emit in the _chain() function */
4367 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4368 /* unlock before emitting */
4369 GST_OBJECT_UNLOCK (pad);
4371 if (G_LIKELY (is_buffer)) {
4372 /* if the signal handler returned FALSE, it means we should just drop the
4374 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4377 /* push all buffers in the list */
4380 GST_OBJECT_LOCK (pad);
4383 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4386 /* Before pushing the buffer to the peer pad, ensure that caps
4387 * are set on this pad */
4388 caps = gst_pad_data_get_caps (is_buffer, data);
4389 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4391 /* take ref to peer pad before releasing the lock */
4392 gst_object_ref (peer);
4394 GST_OBJECT_UNLOCK (pad);
4396 /* we got a new datatype from the pad, it had better handle it */
4397 if (G_UNLIKELY (caps_changed)) {
4398 GST_DEBUG_OBJECT (pad,
4399 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4400 GST_PAD_CAPS (pad), caps, caps);
4401 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
4402 goto not_negotiated;
4405 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4407 gst_object_unref (peer);
4413 GstBufferList *list;
4414 GstBufferListIterator *it;
4417 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4419 list = GST_BUFFER_LIST_CAST (data);
4420 it = gst_buffer_list_iterate (list);
4422 if (gst_buffer_list_iterator_next_group (it)) {
4424 group = gst_buffer_list_iterator_merge_group (it);
4425 if (group == NULL) {
4426 group = gst_buffer_new ();
4427 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4429 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4431 ret = gst_pad_push_data (pad, TRUE, group);
4432 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4434 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4435 ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4438 gst_buffer_list_iterator_free (it);
4439 gst_buffer_list_unref (list);
4444 /* ERROR recovery here */
4447 gst_pad_data_unref (is_buffer, data);
4448 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4449 GST_OBJECT_UNLOCK (pad);
4454 gst_pad_data_unref (is_buffer, data);
4455 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4460 gst_pad_data_unref (is_buffer, data);
4461 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4462 "pushing, but it was not linked");
4463 GST_OBJECT_UNLOCK (pad);
4464 return GST_FLOW_NOT_LINKED;
4468 gst_pad_data_unref (is_buffer, data);
4469 gst_object_unref (peer);
4470 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4471 "element pushed data then refused to accept the caps");
4472 return GST_FLOW_NOT_NEGOTIATED;
4478 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4479 * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4481 * Pushes a buffer to the peer of @pad.
4483 * This function will call an installed pad block before triggering any
4484 * installed pad probes.
4486 * If the caps on @buffer are different from the currently configured caps on
4487 * @pad, this function will call any installed setcaps function on @pad (see
4488 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4489 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4491 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4492 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4495 * In all cases, success or failure, the caller loses its reference to @buffer
4496 * after calling this function.
4498 * Returns: a #GstFlowReturn from the peer pad.
4503 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4505 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4506 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4507 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4509 return gst_pad_push_data (pad, TRUE, buffer);
4513 * gst_pad_push_list:
4514 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4515 * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4517 * Pushes a buffer list to the peer of @pad.
4519 * This function will call an installed pad block before triggering any
4520 * installed pad probes.
4522 * If the caps on the first buffer in the first group of @list are different
4523 * from the currently configured caps on @pad, this function will call any
4524 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4525 * case of failure to renegotiate the new format, this function returns
4526 * #GST_FLOW_NOT_NEGOTIATED.
4528 * If there are any probes installed on @pad every group of the buffer list
4529 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4530 * buffer list will be unreffed.
4532 * The function proceeds calling the chain function on the peer pad and returns
4533 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4534 * be returned. If the peer pad does not have any installed chainlist function
4535 * every group buffer of the list will be merged into a normal #GstBuffer and
4536 * chained via gst_pad_chain().
4538 * In all cases, success or failure, the caller loses its reference to @list
4539 * after calling this function.
4541 * Returns: a #GstFlowReturn from the peer pad.
4548 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4550 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4551 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4552 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4554 return gst_pad_push_data (pad, FALSE, list);
4558 * gst_pad_check_pull_range:
4559 * @pad: a sink #GstPad.
4561 * Checks if a gst_pad_pull_range() can be performed on the peer
4562 * source pad. This function is used by plugins that want to check
4563 * if they can use random access on the peer source pad.
4565 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4566 * if it needs to perform some logic to determine if pull_range is
4569 * Returns: a gboolean with the result.
4574 gst_pad_check_pull_range (GstPad * pad)
4578 GstPadCheckGetRangeFunction checkgetrangefunc;
4580 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4582 GST_OBJECT_LOCK (pad);
4583 if (!GST_PAD_IS_SINK (pad))
4584 goto wrong_direction;
4586 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4589 gst_object_ref (peer);
4590 GST_OBJECT_UNLOCK (pad);
4592 /* see note in above function */
4593 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4594 /* FIXME, kindoff ghetto */
4595 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4596 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4597 "no checkgetrangefunc, assuming %d", ret);
4599 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4600 "calling checkgetrangefunc %s of peer pad %s:%s",
4601 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4603 ret = checkgetrangefunc (peer);
4606 gst_object_unref (peer);
4610 /* ERROR recovery here */
4613 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4614 "checking pull range, but pad must be a sinkpad");
4615 GST_OBJECT_UNLOCK (pad);
4620 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4621 "checking pull range, but it was not linked");
4622 GST_OBJECT_UNLOCK (pad);
4628 * gst_pad_get_range:
4629 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4630 * @offset: The start offset of the buffer
4631 * @size: The length of the buffer
4632 * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4634 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4635 * immediatly and @buffer is %NULL.
4637 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4638 * description of a getrange function. If @pad has no getrange function
4639 * installed (see gst_pad_set_getrange_function()) this function returns
4640 * #GST_FLOW_NOT_SUPPORTED.
4642 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4644 * Returns: a #GstFlowReturn from the pad.
4649 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4650 GstBuffer ** buffer)
4653 GstPadGetRangeFunction getrangefunc;
4654 gboolean emit_signal;
4656 gboolean caps_changed;
4658 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4659 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4660 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4662 GST_PAD_STREAM_LOCK (pad);
4664 GST_OBJECT_LOCK (pad);
4665 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4668 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4669 GST_OBJECT_UNLOCK (pad);
4671 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4674 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4675 "calling getrangefunc %s, offset %"
4676 G_GUINT64_FORMAT ", size %u",
4677 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4679 ret = getrangefunc (pad, offset, size, buffer);
4681 /* can only fire the signal if we have a valid buffer */
4682 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4683 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4687 GST_PAD_STREAM_UNLOCK (pad);
4689 if (G_UNLIKELY (ret != GST_FLOW_OK))
4690 goto get_range_failed;
4692 GST_OBJECT_LOCK (pad);
4693 /* Before pushing the buffer to the peer pad, ensure that caps
4694 * are set on this pad */
4695 caps = GST_BUFFER_CAPS (*buffer);
4696 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4697 GST_OBJECT_UNLOCK (pad);
4699 if (G_UNLIKELY (caps_changed)) {
4700 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4701 /* this should usually work because the element produced the buffer */
4702 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4703 goto not_negotiated;
4710 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4711 "pulling range, but pad was flushing");
4712 GST_OBJECT_UNLOCK (pad);
4713 GST_PAD_STREAM_UNLOCK (pad);
4714 return GST_FLOW_WRONG_STATE;
4718 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4719 ("pullrange on pad %s:%s but it has no getrangefunction",
4720 GST_DEBUG_PAD_NAME (pad)));
4721 GST_PAD_STREAM_UNLOCK (pad);
4722 return GST_FLOW_NOT_SUPPORTED;
4726 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4727 "Dropping data after FALSE probe return");
4728 GST_PAD_STREAM_UNLOCK (pad);
4729 gst_buffer_unref (*buffer);
4731 return GST_FLOW_UNEXPECTED;
4736 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4737 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4738 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4743 gst_buffer_unref (*buffer);
4745 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4746 "getrange returned buffer of unaccaptable caps");
4747 return GST_FLOW_NOT_NEGOTIATED;
4753 * gst_pad_pull_range:
4754 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4755 * @offset: The start offset of the buffer
4756 * @size: The length of the buffer
4757 * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4759 * Pulls a @buffer from the peer pad.
4761 * This function will first trigger the pad block signal if it was
4764 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4765 * function returns the result of gst_pad_get_range() on the peer pad.
4766 * See gst_pad_get_range() for a list of return values and for the
4767 * semantics of the arguments of this function.
4769 * @buffer's caps must either be unset or the same as what is already
4770 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4773 * Returns: a #GstFlowReturn from the peer pad.
4774 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4775 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4776 * @buffer may not be used or freed when any other return value than
4777 * #GST_FLOW_OK is returned.
4782 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4783 GstBuffer ** buffer)
4787 gboolean emit_signal;
4789 gboolean caps_changed;
4791 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4792 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4793 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4795 GST_OBJECT_LOCK (pad);
4797 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4798 handle_pad_block (pad);
4800 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4803 /* signal emision for the pad, peer has chance to emit when
4804 * we call _get_range() */
4805 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4807 gst_object_ref (peer);
4808 GST_OBJECT_UNLOCK (pad);
4810 ret = gst_pad_get_range (peer, offset, size, buffer);
4812 gst_object_unref (peer);
4814 if (G_UNLIKELY (ret != GST_FLOW_OK))
4815 goto pull_range_failed;
4817 /* can only fire the signal if we have a valid buffer */
4818 if (G_UNLIKELY (emit_signal)) {
4819 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4823 GST_OBJECT_LOCK (pad);
4824 /* Before pushing the buffer to the peer pad, ensure that caps
4825 * are set on this pad */
4826 caps = GST_BUFFER_CAPS (*buffer);
4827 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4828 GST_OBJECT_UNLOCK (pad);
4830 /* we got a new datatype on the pad, see if it can handle it */
4831 if (G_UNLIKELY (caps_changed)) {
4832 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4833 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4834 goto not_negotiated;
4838 /* ERROR recovery here */
4841 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4842 "pulling range, but it was not linked");
4843 GST_OBJECT_UNLOCK (pad);
4844 return GST_FLOW_NOT_LINKED;
4849 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4850 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4851 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4856 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4857 "Dropping data after FALSE probe return");
4858 gst_buffer_unref (*buffer);
4860 return GST_FLOW_UNEXPECTED;
4864 gst_buffer_unref (*buffer);
4866 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4867 "pullrange returned buffer of different caps");
4868 return GST_FLOW_NOT_NEGOTIATED;
4873 * gst_pad_push_event:
4874 * @pad: a #GstPad to push the event to.
4875 * @event: the #GstEvent to send to the pad.
4877 * Sends the event to the peer of the given pad. This function is
4878 * mainly used by elements to send events to their peer
4881 * This function takes owership of the provided event so you should
4882 * gst_event_ref() it if you want to reuse the event after this call.
4884 * Returns: TRUE if the event was handled.
4889 gst_pad_push_event (GstPad * pad, GstEvent * event)
4894 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4895 g_return_val_if_fail (event != NULL, FALSE);
4896 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4898 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4900 GST_OBJECT_LOCK (pad);
4902 /* Two checks to be made:
4903 * . (un)set the FLUSHING flag for flushing events,
4904 * . handle pad blocking */
4905 switch (GST_EVENT_TYPE (event)) {
4906 case GST_EVENT_FLUSH_START:
4907 GST_PAD_SET_FLUSHING (pad);
4909 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4910 /* flush start will have set the FLUSHING flag and will then
4911 * unlock all threads doing a GCond wait on the blocking pad. This
4912 * will typically unblock the STREAMING thread blocked on a pad. */
4913 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4914 "doing block signal.");
4915 GST_PAD_BLOCK_BROADCAST (pad);
4919 case GST_EVENT_FLUSH_STOP:
4920 GST_PAD_UNSET_FLUSHING (pad);
4922 /* if we are blocked, flush away the FLUSH_STOP event */
4923 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4924 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4929 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4930 /* block the event as long as the pad is blocked */
4931 if (handle_pad_block (pad) != GST_FLOW_OK)
4937 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4938 GST_OBJECT_UNLOCK (pad);
4940 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4943 GST_OBJECT_LOCK (pad);
4945 peerpad = GST_PAD_PEER (pad);
4946 if (peerpad == NULL)
4949 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4950 GST_EVENT_TYPE_NAME (event), peerpad);
4951 gst_object_ref (peerpad);
4952 GST_OBJECT_UNLOCK (pad);
4954 result = gst_pad_send_event (peerpad, event);
4956 /* Note: we gave away ownership of the event at this point */
4957 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4959 gst_object_unref (peerpad);
4963 /* ERROR handling */
4966 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4967 gst_event_unref (event);
4972 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4973 gst_event_unref (event);
4974 GST_OBJECT_UNLOCK (pad);
4979 GST_DEBUG_OBJECT (pad,
4980 "Not forwarding event since we're flushing and blocking");
4981 gst_event_unref (event);
4982 GST_OBJECT_UNLOCK (pad);
4988 * gst_pad_send_event:
4989 * @pad: a #GstPad to send the event to.
4990 * @event: the #GstEvent to send to the pad.
4992 * Sends the event to the pad. This function can be used
4993 * by applications to send events in the pipeline.
4995 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4996 * sink pad, @event should be a downstream event. For example, you would not
4997 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4998 * Furthermore, some downstream events have to be serialized with data flow,
4999 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5000 * the event needs to be serialized with data flow, this function will take the
5001 * pad's stream lock while calling its event function.
5003 * To find out whether an event type is upstream, downstream, or downstream and
5004 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5005 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5006 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5007 * plugin doesn't need to bother itself with this information; the core handles
5008 * all necessary locks and checks.
5010 * This function takes owership of the provided event so you should
5011 * gst_event_ref() it if you want to reuse the event after this call.
5013 * Returns: TRUE if the event was handled.
5016 gst_pad_send_event (GstPad * pad, GstEvent * event)
5018 gboolean result = FALSE;
5019 GstPadEventFunction eventfunc;
5020 gboolean serialized, need_unlock = FALSE;
5022 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5023 g_return_val_if_fail (event != NULL, FALSE);
5025 GST_OBJECT_LOCK (pad);
5026 if (GST_PAD_IS_SINK (pad)) {
5027 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5028 goto wrong_direction;
5029 serialized = GST_EVENT_IS_SERIALIZED (event);
5030 } else if (GST_PAD_IS_SRC (pad)) {
5031 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5032 goto wrong_direction;
5033 /* events on srcpad never are serialized */
5036 goto unknown_direction;
5038 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5039 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5040 GST_EVENT_SRC (event) = gst_object_ref (pad);
5044 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5045 GST_OBJECT_UNLOCK (pad);
5047 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
5050 GST_OBJECT_LOCK (pad);
5053 switch (GST_EVENT_TYPE (event)) {
5054 case GST_EVENT_FLUSH_START:
5055 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5056 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5058 /* can't even accept a flush begin event when flushing */
5059 if (GST_PAD_IS_FLUSHING (pad))
5061 GST_PAD_SET_FLUSHING (pad);
5062 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5064 case GST_EVENT_FLUSH_STOP:
5065 GST_PAD_UNSET_FLUSHING (pad);
5066 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5067 GST_OBJECT_UNLOCK (pad);
5068 /* grab stream lock */
5069 GST_PAD_STREAM_LOCK (pad);
5071 GST_OBJECT_LOCK (pad);
5074 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5075 GST_EVENT_TYPE_NAME (event));
5077 /* make this a little faster, no point in grabbing the lock
5078 * if the pad is allready flushing. */
5079 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5083 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5084 GST_OBJECT_UNLOCK (pad);
5085 GST_PAD_STREAM_LOCK (pad);
5087 GST_OBJECT_LOCK (pad);
5088 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5093 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5096 GST_OBJECT_UNLOCK (pad);
5098 result = eventfunc (pad, event);
5101 GST_PAD_STREAM_UNLOCK (pad);
5103 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5107 /* ERROR handling */
5110 g_warning ("pad %s:%s sending %s event in wrong direction",
5111 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5112 GST_OBJECT_UNLOCK (pad);
5113 gst_event_unref (event);
5118 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5119 GST_OBJECT_UNLOCK (pad);
5120 gst_event_unref (event);
5125 g_warning ("pad %s:%s has no event handler, file a bug.",
5126 GST_DEBUG_PAD_NAME (pad));
5127 GST_OBJECT_UNLOCK (pad);
5129 GST_PAD_STREAM_UNLOCK (pad);
5130 gst_event_unref (event);
5135 GST_OBJECT_UNLOCK (pad);
5137 GST_PAD_STREAM_UNLOCK (pad);
5138 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5139 "Received event on flushing pad. Discarding");
5140 gst_event_unref (event);
5145 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5146 gst_event_unref (event);
5152 * gst_pad_set_element_private:
5153 * @pad: the #GstPad to set the private data of.
5154 * @priv: The private data to attach to the pad.
5156 * Set the given private data gpointer on the pad.
5157 * This function can only be used by the element that owns the pad.
5158 * No locking is performed in this function.
5161 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5163 pad->element_private = priv;
5167 * gst_pad_get_element_private:
5168 * @pad: the #GstPad to get the private data of.
5170 * Gets the private data of a pad.
5171 * No locking is performed in this function.
5173 * Returns: a #gpointer to the private data.
5176 gst_pad_get_element_private (GstPad * pad)
5178 return pad->element_private;
5182 do_stream_status (GstPad * pad, GstStreamStatusType type,
5183 GThread * thread, GstTask * task)
5187 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5189 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5190 if (GST_IS_ELEMENT (parent)) {
5191 GstMessage *message;
5192 GValue value = { 0 };
5194 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5195 gchar *tname, *ename, *pname;
5197 /* create a good task name */
5198 ename = gst_element_get_name (parent);
5199 pname = gst_pad_get_name (pad);
5200 tname = g_strdup_printf ("%s:%s", ename, pname);
5204 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5208 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5211 g_value_init (&value, GST_TYPE_TASK);
5212 g_value_set_object (&value, task);
5213 gst_message_set_stream_status_object (message, &value);
5214 g_value_unset (&value);
5216 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5217 gst_element_post_message (parent, message);
5219 gst_object_unref (parent);
5224 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5226 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5231 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5233 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5237 static GstTaskThreadCallbacks thr_callbacks = {
5243 * gst_pad_start_task:
5244 * @pad: the #GstPad to start the task of
5245 * @func: the task function to call
5246 * @data: data passed to the task function
5248 * Starts a task that repeatedly calls @func with @data. This function
5249 * is mostly used in pad activation functions to start the dataflow.
5250 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5251 * before @func is called.
5253 * Returns: a %TRUE if the task could be started.
5256 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5261 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5262 g_return_val_if_fail (func != NULL, FALSE);
5264 GST_DEBUG_OBJECT (pad, "start task");
5266 GST_OBJECT_LOCK (pad);
5267 task = GST_PAD_TASK (pad);
5269 task = gst_task_create (func, data);
5270 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5271 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5272 GST_DEBUG_OBJECT (pad, "created task");
5273 GST_PAD_TASK (pad) = task;
5274 gst_object_ref (task);
5275 /* release lock to post the message */
5276 GST_OBJECT_UNLOCK (pad);
5278 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5280 gst_object_unref (task);
5282 GST_OBJECT_LOCK (pad);
5283 /* nobody else is supposed to have changed the pad now */
5284 if (GST_PAD_TASK (pad) != task)
5285 goto concurrent_stop;
5287 res = gst_task_set_state (task, GST_TASK_STARTED);
5288 GST_OBJECT_UNLOCK (pad);
5295 GST_OBJECT_UNLOCK (pad);
5301 * gst_pad_pause_task:
5302 * @pad: the #GstPad to pause the task of
5304 * Pause the task of @pad. This function will also wait until the
5305 * function executed by the task is finished if this function is not
5306 * called from the task function.
5308 * Returns: a TRUE if the task could be paused or FALSE when the pad
5312 gst_pad_pause_task (GstPad * pad)
5317 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5319 GST_DEBUG_OBJECT (pad, "pause task");
5321 GST_OBJECT_LOCK (pad);
5322 task = GST_PAD_TASK (pad);
5325 res = gst_task_set_state (task, GST_TASK_PAUSED);
5326 GST_OBJECT_UNLOCK (pad);
5328 /* wait for task function to finish, this lock is recursive so it does nothing
5329 * when the pause is called from the task itself */
5330 GST_PAD_STREAM_LOCK (pad);
5331 GST_PAD_STREAM_UNLOCK (pad);
5337 GST_DEBUG_OBJECT (pad, "pad has no task");
5338 GST_OBJECT_UNLOCK (pad);
5344 * gst_pad_stop_task:
5345 * @pad: the #GstPad to stop the task of
5347 * Stop the task of @pad. This function will also make sure that the
5348 * function executed by the task will effectively stop if not called
5349 * from the GstTaskFunction.
5351 * This function will deadlock if called from the GstTaskFunction of
5352 * the task. Use gst_task_pause() instead.
5354 * Regardless of whether the pad has a task, the stream lock is acquired and
5355 * released so as to ensure that streaming through this pad has finished.
5357 * Returns: a TRUE if the task could be stopped or FALSE on error.
5360 gst_pad_stop_task (GstPad * pad)
5365 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5367 GST_DEBUG_OBJECT (pad, "stop task");
5369 GST_OBJECT_LOCK (pad);
5370 task = GST_PAD_TASK (pad);
5373 GST_PAD_TASK (pad) = NULL;
5374 res = gst_task_set_state (task, GST_TASK_STOPPED);
5375 GST_OBJECT_UNLOCK (pad);
5377 GST_PAD_STREAM_LOCK (pad);
5378 GST_PAD_STREAM_UNLOCK (pad);
5380 if (!gst_task_join (task))
5383 gst_object_unref (task);
5389 GST_DEBUG_OBJECT (pad, "no task");
5390 GST_OBJECT_UNLOCK (pad);
5392 GST_PAD_STREAM_LOCK (pad);
5393 GST_PAD_STREAM_UNLOCK (pad);
5395 /* this is not an error */
5400 /* this is bad, possibly the application tried to join the task from
5401 * the task's thread. We install the task again so that it will be stopped
5402 * again from the right thread next time hopefully. */
5403 GST_OBJECT_LOCK (pad);
5404 GST_DEBUG_OBJECT (pad, "join failed");
5405 /* we can only install this task if there was no other task */
5406 if (GST_PAD_TASK (pad) == NULL)
5407 GST_PAD_TASK (pad) = task;
5408 GST_OBJECT_UNLOCK (pad);