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 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
64 * with newer GLib versions (>= 2.31.0) */
65 #define GLIB_DISABLE_DEPRECATION_WARNINGS
66 #include "gst_private.h"
69 #include "gstpadtemplate.h"
70 #include "gstenumtypes.h"
71 #include "gstmarshal.h"
76 #include "glib-compat-private.h"
78 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
79 #define GST_CAT_DEFAULT GST_CAT_PADS
81 /* Pad signals and args */
101 typedef struct _GstPadPushCache GstPadPushCache;
103 struct _GstPadPushCache
105 GstPad *peer; /* reffed peer pad */
106 GstCaps *caps; /* caps for this link */
109 static GstPadPushCache _pad_cache_invalid = { NULL, };
111 #define PAD_CACHE_INVALID (&_pad_cache_invalid)
113 #define GST_PAD_GET_PRIVATE(obj) \
114 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
116 #define GST_PAD_CHAINLISTFUNC(pad) ((pad)->abidata.ABI.priv->chainlistfunc)
118 struct _GstPadPrivate
120 GstPadChainListFunction chainlistfunc;
122 GstPadPushCache *cache_ptr;
125 static void gst_pad_dispose (GObject * object);
126 static void gst_pad_finalize (GObject * object);
127 static void gst_pad_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_pad_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
132 static GstFlowReturn handle_pad_block (GstPad * pad);
133 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
134 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
135 static gboolean gst_pad_activate_default (GstPad * pad);
136 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
138 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
139 #ifdef GST_DISABLE_DEPRECATED
140 #include <libxml/parser.h>
142 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
143 void gst_pad_load_and_link (xmlNodePtr self, GstObject * parent);
146 /* Some deprecated stuff that we need inside here for
147 * backwards compatibility */
148 #ifdef GST_DISABLE_DEPRECATED
149 #ifndef GST_REMOVE_DEPRECATED
150 #define GST_PAD_INTLINKFUNC(pad) (GST_PAD_CAST(pad)->intlinkfunc)
151 GList *gst_pad_get_internal_links_default (GstPad * pad);
155 static GstObjectClass *parent_class = NULL;
156 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
158 static GParamSpec *pspec_caps = NULL;
160 /* quarks for probe signals */
161 static GQuark buffer_quark;
162 static GQuark event_quark;
171 static GstFlowQuarks flow_quarks[] = {
172 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
173 {GST_FLOW_RESEND, "resend", 0},
174 {GST_FLOW_OK, "ok", 0},
175 {GST_FLOW_NOT_LINKED, "not-linked", 0},
176 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
177 {GST_FLOW_UNEXPECTED, "unexpected", 0},
178 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
179 {GST_FLOW_ERROR, "error", 0},
180 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
181 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
186 * @ret: a #GstFlowReturn to get the name of.
188 * Gets a string representing the given flow return.
190 * Returns: a static string with the name of the flow return.
193 gst_flow_get_name (GstFlowReturn ret)
197 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
199 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
200 if (ret == flow_quarks[i].ret)
201 return flow_quarks[i].name;
208 * @ret: a #GstFlowReturn to get the quark of.
210 * Get the unique quark for the given GstFlowReturn.
212 * Returns: the quark associated with the flow return or 0 if an
213 * invalid return was specified.
216 gst_flow_to_quark (GstFlowReturn ret)
220 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
222 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
223 if (ret == flow_quarks[i].ret)
224 return flow_quarks[i].quark;
233 buffer_quark = g_quark_from_static_string ("buffer"); \
234 event_quark = g_quark_from_static_string ("event"); \
236 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
237 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
240 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
241 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
244 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
247 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
248 GValue * return_accu, const GValue * handler_return, gpointer dummy)
250 gboolean ret = g_value_get_boolean (handler_return);
252 GST_DEBUG ("accumulated %d", ret);
253 g_value_set_boolean (return_accu, ret);
259 default_have_data (GstPad * pad, GstMiniObject * o)
265 gst_pad_class_init (GstPadClass * klass)
267 GObjectClass *gobject_class;
268 GstObjectClass *gstobject_class;
270 gobject_class = G_OBJECT_CLASS (klass);
271 gstobject_class = GST_OBJECT_CLASS (klass);
273 g_type_class_add_private (klass, sizeof (GstPadPrivate));
275 parent_class = g_type_class_peek_parent (klass);
277 gobject_class->dispose = gst_pad_dispose;
278 gobject_class->finalize = gst_pad_finalize;
279 gobject_class->set_property = gst_pad_set_property;
280 gobject_class->get_property = gst_pad_get_property;
284 * @pad: the pad that emitted the signal
285 * @peer: the peer pad that has been connected
287 * Signals that a pad has been linked to the peer pad.
289 gst_pad_signals[PAD_LINKED] =
290 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
291 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
292 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
295 * @pad: the pad that emitted the signal
296 * @peer: the peer pad that has been disconnected
298 * Signals that a pad has been unlinked from the peer pad.
300 gst_pad_signals[PAD_UNLINKED] =
301 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
302 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
303 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
305 * GstPad::request-link:
306 * @pad: the pad that emitted the signal
307 * @peer: the peer pad for which a connection is requested
309 * Signals that a pad connection has been requested.
311 gst_pad_signals[PAD_REQUEST_LINK] =
312 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
313 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
314 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
318 * @pad: the pad that emitted the signal
319 * @mini_obj: new data
321 * Signals that new data is available on the pad. This signal is used
322 * internally for implementing pad probes.
323 * See gst_pad_add_*_probe functions.
325 * Returns: %TRUE to keep the data, %FALSE to drop it
327 gst_pad_signals[PAD_HAVE_DATA] =
328 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
329 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
330 G_STRUCT_OFFSET (GstPadClass, have_data),
331 _gst_do_pass_data_accumulator,
332 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
333 GST_TYPE_MINI_OBJECT);
335 pspec_caps = g_param_spec_boxed ("caps", "Caps",
336 "The capabilities of the pad", GST_TYPE_CAPS,
337 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
338 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
340 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
341 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
342 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
343 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
344 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
345 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
346 g_param_spec_object ("template", "Template",
347 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
351 gstobject_class->save_thyself =
352 ((gpointer (*)(GstObject * object,
353 gpointer self)) * GST_DEBUG_FUNCPTR (gst_pad_save_thyself));
355 gstobject_class->path_string_separator = ".";
357 /* Register common function pointer descriptions */
358 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
359 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
360 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
361 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
362 #ifndef GST_REMOVE_DEPRECATED
363 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_internal_links_default);
365 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
366 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
368 /* from gstutils.c */
369 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_fixed_caps_func);
371 klass->have_data = default_have_data;
375 gst_pad_init (GstPad * pad)
377 pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
379 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
380 GST_PAD_PEER (pad) = NULL;
382 GST_PAD_CHAINFUNC (pad) = NULL;
384 GST_PAD_LINKFUNC (pad) = NULL;
386 GST_PAD_CAPS (pad) = NULL;
387 GST_PAD_GETCAPSFUNC (pad) = NULL;
389 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
390 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
391 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
392 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
393 #ifndef GST_REMOVE_DEPRECATED
394 GST_PAD_INTLINKFUNC (pad) = gst_pad_get_internal_links_default;
396 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
398 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
400 pad->do_buffer_signals = 0;
401 pad->do_event_signals = 0;
403 GST_PAD_SET_FLUSHING (pad);
405 pad->preroll_lock = g_mutex_new ();
406 pad->preroll_cond = g_cond_new ();
408 /* FIXME 0.11: Store this directly in the instance struct */
409 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
410 g_static_rec_mutex_init (pad->stream_rec_lock);
412 pad->block_cond = g_cond_new ();
416 gst_pad_dispose (GObject * object)
418 GstPad *pad = GST_PAD_CAST (object);
421 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
423 /* unlink the peer pad */
424 if ((peer = gst_pad_get_peer (pad))) {
425 /* window for MT unsafeness, someone else could unlink here
426 * and then we call unlink with wrong pads. The unlink
427 * function would catch this and safely return failed. */
428 if (GST_PAD_IS_SRC (pad))
429 gst_pad_unlink (pad, peer);
431 gst_pad_unlink (peer, pad);
433 gst_object_unref (peer);
437 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
439 gst_pad_set_pad_template (pad, NULL);
441 if (pad->block_destroy_data && pad->block_data) {
442 pad->block_destroy_data (pad->block_data);
443 pad->block_data = NULL;
446 G_OBJECT_CLASS (parent_class)->dispose (object);
450 gst_pad_finalize (GObject * object)
452 GstPad *pad = GST_PAD_CAST (object);
455 /* in case the task is still around, clean it up */
456 if ((task = GST_PAD_TASK (pad))) {
457 gst_task_join (task);
458 GST_PAD_TASK (pad) = NULL;
459 gst_object_unref (task);
462 if (pad->stream_rec_lock) {
463 g_static_rec_mutex_free (pad->stream_rec_lock);
464 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
465 pad->stream_rec_lock = NULL;
467 if (pad->preroll_lock) {
468 g_mutex_free (pad->preroll_lock);
469 g_cond_free (pad->preroll_cond);
470 pad->preroll_lock = NULL;
471 pad->preroll_cond = NULL;
473 if (pad->block_cond) {
474 g_cond_free (pad->block_cond);
475 pad->block_cond = NULL;
478 G_OBJECT_CLASS (parent_class)->finalize (object);
482 gst_pad_set_property (GObject * object, guint prop_id,
483 const GValue * value, GParamSpec * pspec)
485 g_return_if_fail (GST_IS_PAD (object));
488 case PAD_PROP_DIRECTION:
489 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
491 case PAD_PROP_TEMPLATE:
492 gst_pad_set_pad_template (GST_PAD_CAST (object),
493 (GstPadTemplate *) g_value_get_object (value));
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502 gst_pad_get_property (GObject * object, guint prop_id,
503 GValue * value, GParamSpec * pspec)
505 g_return_if_fail (GST_IS_PAD (object));
509 GST_OBJECT_LOCK (object);
510 g_value_set_boxed (value, GST_PAD_CAPS (object));
511 GST_OBJECT_UNLOCK (object);
513 case PAD_PROP_DIRECTION:
514 g_value_set_enum (value, GST_PAD_DIRECTION (object));
516 case PAD_PROP_TEMPLATE:
517 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
527 * @name: the name of the new pad.
528 * @direction: the #GstPadDirection of the pad.
530 * Creates a new pad with the given name in the given direction.
531 * If name is NULL, a guaranteed unique name (across all pads)
533 * This function makes a copy of the name so you can safely free the name.
535 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
540 gst_pad_new (const gchar * name, GstPadDirection direction)
542 return g_object_new (GST_TYPE_PAD,
543 "name", name, "direction", direction, NULL);
547 * gst_pad_new_from_template:
548 * @templ: the pad template to use
549 * @name: the name of the element
551 * Creates a new pad with the given name from the given template.
552 * If name is NULL, a guaranteed unique name (across all pads)
554 * This function makes a copy of the name so you can safely free the name.
556 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
559 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
561 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
563 return g_object_new (GST_TYPE_PAD,
564 "name", name, "direction", templ->direction, "template", templ, NULL);
568 * gst_pad_new_from_static_template:
569 * @templ: the #GstStaticPadTemplate to use
570 * @name: the name of the element
572 * Creates a new pad with the given name from the given static template.
573 * If name is NULL, a guaranteed unique name (across all pads)
575 * This function makes a copy of the name so you can safely free the name.
577 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
580 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
584 GstPadTemplate *template;
586 template = gst_static_pad_template_get (templ);
587 pad = gst_pad_new_from_template (template, name);
588 gst_object_unref (template);
593 * gst_pad_get_direction:
594 * @pad: a #GstPad to get the direction of.
596 * Gets the direction of the pad. The direction of the pad is
597 * decided at construction time so this function does not take
600 * Returns: the #GstPadDirection of the pad.
605 gst_pad_get_direction (GstPad * pad)
607 GstPadDirection result;
609 /* PAD_UNKNOWN is a little silly but we need some sort of
610 * error return value */
611 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
613 result = GST_PAD_DIRECTION (pad);
619 gst_pad_activate_default (GstPad * pad)
621 return gst_pad_activate_push (pad, TRUE);
625 pre_activate (GstPad * pad, GstActivateMode new_mode)
628 case GST_ACTIVATE_PUSH:
629 case GST_ACTIVATE_PULL:
630 GST_OBJECT_LOCK (pad);
631 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
633 GST_PAD_UNSET_FLUSHING (pad);
634 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
635 GST_OBJECT_UNLOCK (pad);
637 case GST_ACTIVATE_NONE:
638 GST_OBJECT_LOCK (pad);
639 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
640 _priv_gst_pad_invalidate_cache (pad);
641 GST_PAD_SET_FLUSHING (pad);
642 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
643 /* unlock blocked pads so element can resume and stop */
644 GST_PAD_BLOCK_BROADCAST (pad);
645 GST_OBJECT_UNLOCK (pad);
651 post_activate (GstPad * pad, GstActivateMode new_mode)
654 case GST_ACTIVATE_PUSH:
655 case GST_ACTIVATE_PULL:
658 case GST_ACTIVATE_NONE:
659 /* ensures that streaming stops */
660 GST_PAD_STREAM_LOCK (pad);
661 GST_DEBUG_OBJECT (pad, "stopped streaming");
662 GST_PAD_STREAM_UNLOCK (pad);
668 * gst_pad_set_active:
669 * @pad: the #GstPad to activate or deactivate.
670 * @active: whether or not the pad should be active.
672 * Activates or deactivates the given pad.
673 * Normally called from within core state change functions.
675 * If @active, makes sure the pad is active. If it is already active, either in
676 * push or pull mode, just return. Otherwise dispatches to the pad's activate
677 * function to perform the actual activation.
679 * If not @active, checks the pad's current mode and calls
680 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
683 * Returns: #TRUE if the operation was successful.
688 gst_pad_set_active (GstPad * pad, gboolean active)
691 gboolean ret = FALSE;
693 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
695 GST_OBJECT_LOCK (pad);
696 old = GST_PAD_ACTIVATE_MODE (pad);
697 GST_OBJECT_UNLOCK (pad);
701 case GST_ACTIVATE_PUSH:
702 GST_DEBUG_OBJECT (pad, "activating pad from push");
705 case GST_ACTIVATE_PULL:
706 GST_DEBUG_OBJECT (pad, "activating pad from pull");
709 case GST_ACTIVATE_NONE:
710 GST_DEBUG_OBJECT (pad, "activating pad from none");
711 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
716 case GST_ACTIVATE_PUSH:
717 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
718 ret = gst_pad_activate_push (pad, FALSE);
720 case GST_ACTIVATE_PULL:
721 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
722 ret = gst_pad_activate_pull (pad, FALSE);
724 case GST_ACTIVATE_NONE:
725 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
732 GST_OBJECT_LOCK (pad);
734 g_critical ("Failed to deactivate pad %s:%s, very bad",
735 GST_DEBUG_PAD_NAME (pad));
737 GST_WARNING_OBJECT (pad, "Failed to activate pad");
739 GST_OBJECT_UNLOCK (pad);
746 * gst_pad_activate_pull:
747 * @pad: the #GstPad to activate or deactivate.
748 * @active: whether or not the pad should be active.
750 * Activates or deactivates the given pad in pull mode via dispatching to the
751 * pad's activatepullfunc. For use from within pad activation functions only.
752 * When called on sink pads, will first proxy the call to the peer pad, which
753 * is expected to activate its internally linked pads from within its
754 * activate_pull function.
756 * If you don't know what this is, you probably don't want to call it.
758 * Returns: TRUE if the operation was successful.
763 gst_pad_activate_pull (GstPad * pad, gboolean active)
765 GstActivateMode old, new;
768 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
770 GST_OBJECT_LOCK (pad);
771 old = GST_PAD_ACTIVATE_MODE (pad);
772 GST_OBJECT_UNLOCK (pad);
776 case GST_ACTIVATE_PULL:
777 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
779 case GST_ACTIVATE_PUSH:
780 GST_DEBUG_OBJECT (pad,
781 "activating pad from push, deactivate push first");
782 /* pad was activate in the wrong direction, deactivate it
783 * and reactivate it in pull mode */
784 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
785 goto deactivate_failed;
786 /* fallthrough, pad is deactivated now. */
787 case GST_ACTIVATE_NONE:
788 GST_DEBUG_OBJECT (pad, "activating pad from none");
793 case GST_ACTIVATE_NONE:
794 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
796 case GST_ACTIVATE_PUSH:
797 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
798 /* pad was activated in the other direction, deactivate it
799 * in push mode, this should not happen... */
800 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
801 goto deactivate_failed;
802 /* everything is fine now */
804 case GST_ACTIVATE_PULL:
805 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
810 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
811 if ((peer = gst_pad_get_peer (pad))) {
812 GST_DEBUG_OBJECT (pad, "calling peer");
813 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
815 gst_object_unref (peer);
817 /* there is no peer, this is only fatal when we activate. When we
818 * deactivate, we must assume the application has unlinked the peer and
819 * will deactivate it eventually. */
823 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
826 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
827 goto failure; /* Can't activate pull on a src without a
831 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
832 pre_activate (pad, new);
834 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
835 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
838 /* can happen for sinks of passthrough elements */
841 post_activate (pad, new);
843 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
844 active ? "activated" : "deactivated");
850 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
851 active ? "activated" : "deactivated");
856 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
857 "failed to %s in switch to pull from mode %d",
858 (active ? "activate" : "deactivate"), old);
863 GST_OBJECT_LOCK (peer);
864 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
865 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
866 GST_OBJECT_UNLOCK (peer);
867 gst_object_unref (peer);
872 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
878 GST_OBJECT_LOCK (pad);
879 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
880 active ? "activate" : "deactivate");
881 _priv_gst_pad_invalidate_cache (pad);
882 GST_PAD_SET_FLUSHING (pad);
883 GST_PAD_ACTIVATE_MODE (pad) = old;
884 GST_OBJECT_UNLOCK (pad);
890 * gst_pad_activate_push:
891 * @pad: the #GstPad to activate or deactivate.
892 * @active: whether the pad should be active or not.
894 * Activates or deactivates the given pad in push mode via dispatching to the
895 * pad's activatepushfunc. For use from within pad activation functions only.
897 * If you don't know what this is, you probably don't want to call it.
899 * Returns: %TRUE if the operation was successful.
904 gst_pad_activate_push (GstPad * pad, gboolean active)
906 GstActivateMode old, new;
908 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
909 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
910 active ? "activated" : "deactivated");
912 GST_OBJECT_LOCK (pad);
913 old = GST_PAD_ACTIVATE_MODE (pad);
914 GST_OBJECT_UNLOCK (pad);
918 case GST_ACTIVATE_PUSH:
919 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
921 case GST_ACTIVATE_PULL:
922 GST_DEBUG_OBJECT (pad,
923 "activating pad from push, deactivating pull first");
924 /* pad was activate in the wrong direction, deactivate it
925 * an reactivate it in push mode */
926 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
927 goto deactivate_failed;
928 /* fallthrough, pad is deactivated now. */
929 case GST_ACTIVATE_NONE:
930 GST_DEBUG_OBJECT (pad, "activating pad from none");
935 case GST_ACTIVATE_NONE:
936 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
938 case GST_ACTIVATE_PULL:
939 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
940 /* pad was activated in the other direction, deactivate it
941 * in pull mode, this should not happen... */
942 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
943 goto deactivate_failed;
944 /* everything is fine now */
946 case GST_ACTIVATE_PUSH:
947 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
952 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
953 pre_activate (pad, new);
955 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
956 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
960 /* quite ok, element relies on state change func to prepare itself */
963 post_activate (pad, new);
965 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
966 active ? "activated" : "deactivated");
971 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
972 active ? "activated" : "deactivated");
977 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
978 "failed to %s in switch to push from mode %d",
979 (active ? "activate" : "deactivate"), old);
984 GST_OBJECT_LOCK (pad);
985 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
986 active ? "activate" : "deactivate");
987 _priv_gst_pad_invalidate_cache (pad);
988 GST_PAD_SET_FLUSHING (pad);
989 GST_PAD_ACTIVATE_MODE (pad) = old;
990 GST_OBJECT_UNLOCK (pad);
997 * @pad: the #GstPad to query
999 * Query if a pad is active
1001 * Returns: TRUE if the pad is active.
1006 gst_pad_is_active (GstPad * pad)
1008 gboolean result = FALSE;
1010 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1012 GST_OBJECT_LOCK (pad);
1013 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1014 GST_OBJECT_UNLOCK (pad);
1020 * gst_pad_set_blocked_async_full:
1021 * @pad: the #GstPad to block or unblock
1022 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1023 * @callback: #GstPadBlockCallback that will be called when the
1024 * operation succeeds
1025 * @user_data: (closure): user data passed to the callback
1026 * @destroy_data: #GDestroyNotify for user_data
1028 * Blocks or unblocks the dataflow on a pad. The provided callback
1029 * is called when the operation succeeds; this happens right before the next
1030 * attempt at pushing a buffer on the pad.
1032 * This can take a while as the pad can only become blocked when real dataflow
1034 * When the pipeline is stalled, for example in PAUSED, this can
1035 * take an indeterminate amount of time.
1036 * You can pass NULL as the callback to make this call block. Be careful with
1037 * this blocking call as it might not return for reasons stated above.
1040 * Pad block handlers are only called for source pads in push mode
1041 * and sink pads in pull mode.
1044 * Returns: %TRUE if the pad could be blocked. This function can fail if the
1045 * wrong parameters were passed or the pad was already in the requested state.
1052 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1053 GstPadBlockCallback callback, gpointer user_data,
1054 GDestroyNotify destroy_data)
1056 gboolean was_blocked = FALSE;
1058 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1060 GST_OBJECT_LOCK (pad);
1062 was_blocked = GST_PAD_IS_BLOCKED (pad);
1064 if (G_UNLIKELY (was_blocked == blocked))
1065 goto had_right_state;
1068 (GST_PAD_ACTIVATE_MODE (pad) == GST_ACTIVATE_PUSH) &&
1069 (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)))
1070 goto wrong_direction;
1072 (GST_PAD_ACTIVATE_MODE (pad) == GST_ACTIVATE_PULL) &&
1073 (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)))
1074 goto wrong_direction;
1077 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1079 _priv_gst_pad_invalidate_cache (pad);
1080 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1082 if (pad->block_destroy_data && pad->block_data)
1083 pad->block_destroy_data (pad->block_data);
1085 pad->block_callback = callback;
1086 pad->block_data = user_data;
1087 pad->block_destroy_data = destroy_data;
1088 pad->abidata.ABI.block_callback_called = FALSE;
1090 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1091 GST_PAD_BLOCK_WAIT (pad);
1092 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1095 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1097 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1099 if (pad->block_destroy_data && pad->block_data)
1100 pad->block_destroy_data (pad->block_data);
1102 pad->block_callback = callback;
1103 pad->block_data = user_data;
1104 pad->block_destroy_data = destroy_data;
1105 pad->abidata.ABI.block_callback_called = FALSE;
1107 GST_PAD_BLOCK_BROADCAST (pad);
1109 /* no callback, wait for the unblock to happen */
1110 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1111 GST_PAD_BLOCK_WAIT (pad);
1112 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1115 GST_OBJECT_UNLOCK (pad);
1123 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1124 "pad was in right state (%d)", was_blocked);
1125 GST_OBJECT_UNLOCK (pad);
1131 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pad block on the wrong pad, "
1132 "block src pads in push mode and sink pads in pull mode.");
1133 GST_OBJECT_UNLOCK (pad);
1140 * gst_pad_set_blocked_async:
1141 * @pad: the #GstPad to block or unblock
1142 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1143 * @callback: #GstPadBlockCallback that will be called when the
1144 * operation succeeds
1145 * @user_data: (closure): user data passed to the callback
1147 * Blocks or unblocks the dataflow on a pad. The provided callback
1148 * is called when the operation succeeds; this happens right before the next
1149 * attempt at pushing a buffer on the pad.
1151 * This can take a while as the pad can only become blocked when real dataflow
1153 * When the pipeline is stalled, for example in PAUSED, this can
1154 * take an indeterminate amount of time.
1155 * You can pass NULL as the callback to make this call block. Be careful with
1156 * this blocking call as it might not return for reasons stated above.
1159 * Pad block handlers are only called for source pads in push mode
1160 * and sink pads in pull mode.
1163 * Returns: TRUE if the pad could be blocked. This function can fail if the
1164 * wrong parameters were passed or the pad was already in the requested state.
1169 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1170 GstPadBlockCallback callback, gpointer user_data)
1172 return gst_pad_set_blocked_async_full (pad, blocked,
1173 callback, user_data, NULL);
1177 * gst_pad_set_blocked:
1178 * @pad: the #GstPad to block or unblock
1179 * @blocked: boolean indicating we should block or unblock
1181 * Blocks or unblocks the dataflow on a pad. This function is
1182 * a shortcut for gst_pad_set_blocked_async() with a NULL
1186 * Pad blocks are only possible for source pads in push mode
1187 * and sink pads in pull mode.
1190 * Returns: TRUE if the pad could be blocked. This function can fail if the
1191 * wrong parameters were passed or the pad was already in the requested state.
1196 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1198 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1202 * gst_pad_is_blocked:
1203 * @pad: the #GstPad to query
1205 * Checks if the pad is blocked or not. This function returns the
1206 * last requested state of the pad. It is not certain that the pad
1207 * is actually blocking at this point (see gst_pad_is_blocking()).
1209 * Returns: TRUE if the pad is blocked.
1214 gst_pad_is_blocked (GstPad * pad)
1216 gboolean result = FALSE;
1218 g_return_val_if_fail (GST_IS_PAD (pad), result);
1220 GST_OBJECT_LOCK (pad);
1221 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1222 GST_OBJECT_UNLOCK (pad);
1228 * gst_pad_is_blocking:
1229 * @pad: the #GstPad to query
1231 * Checks if the pad is blocking or not. This is a guaranteed state
1232 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1234 * Returns: TRUE if the pad is blocking.
1241 gst_pad_is_blocking (GstPad * pad)
1243 gboolean result = FALSE;
1245 g_return_val_if_fail (GST_IS_PAD (pad), result);
1247 GST_OBJECT_LOCK (pad);
1248 /* the blocking flag is only valid if the pad is not flushing */
1249 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1250 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1251 GST_OBJECT_UNLOCK (pad);
1257 * gst_pad_set_activate_function:
1259 * @activate: the #GstPadActivateFunction to set.
1261 * Sets the given activate function for @pad. The activate function will
1262 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1263 * the actual activation. Only makes sense to set on sink pads.
1265 * Call this function if your sink pad can start a pull-based task.
1268 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1270 g_return_if_fail (GST_IS_PAD (pad));
1272 GST_PAD_ACTIVATEFUNC (pad) = activate;
1273 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1274 GST_DEBUG_FUNCPTR_NAME (activate));
1278 * gst_pad_set_activatepull_function:
1280 * @activatepull: the #GstPadActivateModeFunction to set.
1282 * Sets the given activate_pull function for the pad. An activate_pull function
1283 * prepares the element and any upstream connections for pulling. See XXX
1284 * part-activation.txt for details.
1287 gst_pad_set_activatepull_function (GstPad * pad,
1288 GstPadActivateModeFunction activatepull)
1290 g_return_if_fail (GST_IS_PAD (pad));
1292 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1293 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1294 GST_DEBUG_FUNCPTR_NAME (activatepull));
1298 * gst_pad_set_activatepush_function:
1300 * @activatepush: the #GstPadActivateModeFunction to set.
1302 * Sets the given activate_push function for the pad. An activate_push function
1303 * prepares the element for pushing. See XXX part-activation.txt for details.
1306 gst_pad_set_activatepush_function (GstPad * pad,
1307 GstPadActivateModeFunction activatepush)
1309 g_return_if_fail (GST_IS_PAD (pad));
1311 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1312 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1313 GST_DEBUG_FUNCPTR_NAME (activatepush));
1317 * gst_pad_set_chain_function:
1318 * @pad: a sink #GstPad.
1319 * @chain: the #GstPadChainFunction to set.
1321 * Sets the given chain function for the pad. The chain function is called to
1322 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1325 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1327 g_return_if_fail (GST_IS_PAD (pad));
1328 g_return_if_fail (GST_PAD_IS_SINK (pad));
1330 GST_PAD_CHAINFUNC (pad) = chain;
1331 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1332 GST_DEBUG_FUNCPTR_NAME (chain));
1336 * gst_pad_set_chain_list_function:
1337 * @pad: a sink #GstPad.
1338 * @chainlist: the #GstPadChainListFunction to set.
1340 * Sets the given chain list function for the pad. The chainlist function is
1341 * called to process a #GstBufferList input buffer list. See
1342 * #GstPadChainListFunction for more details.
1347 gst_pad_set_chain_list_function (GstPad * pad,
1348 GstPadChainListFunction chainlist)
1350 g_return_if_fail (GST_IS_PAD (pad));
1351 g_return_if_fail (GST_PAD_IS_SINK (pad));
1353 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1354 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1355 GST_DEBUG_FUNCPTR_NAME (chainlist));
1359 * gst_pad_set_getrange_function:
1360 * @pad: a source #GstPad.
1361 * @get: the #GstPadGetRangeFunction to set.
1363 * Sets the given getrange function for the pad. The getrange function is
1364 * called to produce a new #GstBuffer to start the processing pipeline. see
1365 * #GstPadGetRangeFunction for a description of the getrange function.
1368 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1370 g_return_if_fail (GST_IS_PAD (pad));
1371 g_return_if_fail (GST_PAD_IS_SRC (pad));
1373 GST_PAD_GETRANGEFUNC (pad) = get;
1375 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1376 GST_DEBUG_FUNCPTR_NAME (get));
1380 * gst_pad_set_checkgetrange_function:
1381 * @pad: a source #GstPad.
1382 * @check: the #GstPadCheckGetRangeFunction to set.
1384 * Sets the given checkgetrange function for the pad. Implement this function
1385 * on a pad if you dynamically support getrange based scheduling on the pad.
1388 gst_pad_set_checkgetrange_function (GstPad * pad,
1389 GstPadCheckGetRangeFunction check)
1391 g_return_if_fail (GST_IS_PAD (pad));
1392 g_return_if_fail (GST_PAD_IS_SRC (pad));
1394 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1396 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1397 GST_DEBUG_FUNCPTR_NAME (check));
1401 * gst_pad_set_event_function:
1402 * @pad: a #GstPad of either direction.
1403 * @event: the #GstPadEventFunction to set.
1405 * Sets the given event handler for the pad.
1408 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1410 g_return_if_fail (GST_IS_PAD (pad));
1412 GST_PAD_EVENTFUNC (pad) = event;
1414 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1415 GST_DEBUG_FUNCPTR_NAME (event));
1419 * gst_pad_set_query_function:
1420 * @pad: a #GstPad of either direction.
1421 * @query: the #GstPadQueryFunction to set.
1423 * Set the given query function for the pad.
1426 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1428 g_return_if_fail (GST_IS_PAD (pad));
1430 GST_PAD_QUERYFUNC (pad) = query;
1432 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1433 GST_DEBUG_FUNCPTR_NAME (query));
1437 * gst_pad_set_query_type_function:
1438 * @pad: a #GstPad of either direction.
1439 * @type_func: the #GstPadQueryTypeFunction to set.
1441 * Set the given query type function for the pad.
1444 gst_pad_set_query_type_function (GstPad * pad,
1445 GstPadQueryTypeFunction type_func)
1447 g_return_if_fail (GST_IS_PAD (pad));
1449 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1451 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1452 GST_DEBUG_FUNCPTR_NAME (type_func));
1456 * gst_pad_get_query_types:
1459 * Get an array of supported queries that can be performed
1462 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1465 const GstQueryType *
1466 gst_pad_get_query_types (GstPad * pad)
1468 GstPadQueryTypeFunction func;
1470 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1472 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1484 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1486 *data = gst_pad_get_query_types (pad);
1492 * gst_pad_get_query_types_default:
1495 * Invoke the default dispatcher for the query types on
1498 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1499 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1500 * query types function.
1502 const GstQueryType *
1503 gst_pad_get_query_types_default (GstPad * pad)
1505 GstQueryType *result = NULL;
1507 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1509 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1510 gst_pad_get_query_types_dispatcher, &result);
1516 * gst_pad_set_iterate_internal_links_function:
1517 * @pad: a #GstPad of either direction.
1518 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1520 * Sets the given internal link iterator function for the pad.
1525 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1526 GstPadIterIntLinkFunction iterintlink)
1528 g_return_if_fail (GST_IS_PAD (pad));
1530 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1531 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1532 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1536 * gst_pad_set_internal_link_function:
1537 * @pad: a #GstPad of either direction.
1538 * @intlink: the #GstPadIntLinkFunction to set.
1540 * Sets the given internal link function for the pad.
1542 * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1544 #ifndef GST_REMOVE_DEPRECATED
1545 #ifdef GST_DISABLE_DEPRECATED
1547 gst_pad_set_internal_link_function (GstPad * pad,
1548 GstPadIntLinkFunction intlink);
1551 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1553 g_return_if_fail (GST_IS_PAD (pad));
1555 GST_PAD_INTLINKFUNC (pad) = intlink;
1556 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1557 GST_DEBUG_FUNCPTR_NAME (intlink));
1559 #endif /* GST_REMOVE_DEPRECATED */
1562 * gst_pad_set_link_function:
1564 * @link: the #GstPadLinkFunction to set.
1566 * Sets the given link function for the pad. It will be called when
1567 * the pad is linked with another pad.
1569 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1572 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1573 * cannot be made for some reason.
1575 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1576 * of the peer sink pad, if present.
1579 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1581 g_return_if_fail (GST_IS_PAD (pad));
1583 GST_PAD_LINKFUNC (pad) = link;
1584 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1585 GST_DEBUG_FUNCPTR_NAME (link));
1589 * gst_pad_set_unlink_function:
1591 * @unlink: the #GstPadUnlinkFunction to set.
1593 * Sets the given unlink function for the pad. It will be called
1594 * when the pad is unlinked.
1597 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1599 g_return_if_fail (GST_IS_PAD (pad));
1601 GST_PAD_UNLINKFUNC (pad) = unlink;
1602 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1603 GST_DEBUG_FUNCPTR_NAME (unlink));
1607 * gst_pad_set_getcaps_function:
1609 * @getcaps: the #GstPadGetCapsFunction to set.
1611 * Sets the given getcaps function for the pad. @getcaps should return the
1612 * allowable caps for a pad in the context of the element's state, its link to
1613 * other elements, and the devices or files it has opened. These caps must be a
1614 * subset of the pad template caps. In the NULL state with no links, @getcaps
1615 * should ideally return the same caps as the pad template. In rare
1616 * circumstances, an object property can affect the caps returned by @getcaps,
1617 * but this is discouraged.
1619 * You do not need to call this function if @pad's allowed caps are always the
1620 * same as the pad template caps. This can only be true if the padtemplate
1621 * has fixed simple caps.
1623 * For most filters, the caps returned by @getcaps is directly affected by the
1624 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1625 * the srcpad's getcaps function is directly related to the stream data. Again,
1626 * @getcaps should return the most specific caps it reasonably can, since this
1627 * helps with autoplugging.
1629 * Note that the return value from @getcaps is owned by the caller, so the
1630 * caller should unref the caps after usage.
1633 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1635 g_return_if_fail (GST_IS_PAD (pad));
1637 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1638 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1639 GST_DEBUG_FUNCPTR_NAME (getcaps));
1643 * gst_pad_set_acceptcaps_function:
1645 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1647 * Sets the given acceptcaps function for the pad. The acceptcaps function
1648 * will be called to check if the pad can accept the given caps. Setting the
1649 * acceptcaps function to NULL restores the default behaviour of allowing
1650 * any caps that matches the caps from gst_pad_get_caps().
1653 gst_pad_set_acceptcaps_function (GstPad * pad,
1654 GstPadAcceptCapsFunction acceptcaps)
1656 g_return_if_fail (GST_IS_PAD (pad));
1658 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1659 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1660 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1664 * gst_pad_set_fixatecaps_function:
1666 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1668 * Sets the given fixatecaps function for the pad. The fixatecaps function
1669 * will be called whenever the default values for a GstCaps needs to be
1673 gst_pad_set_fixatecaps_function (GstPad * pad,
1674 GstPadFixateCapsFunction fixatecaps)
1676 g_return_if_fail (GST_IS_PAD (pad));
1678 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1679 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1680 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1684 * gst_pad_set_setcaps_function:
1686 * @setcaps: the #GstPadSetCapsFunction to set.
1688 * Sets the given setcaps function for the pad. The setcaps function
1689 * will be called whenever a buffer with a new media type is pushed or
1690 * pulled from the pad. The pad/element needs to update its internal
1691 * structures to process the new media type. If this new type is not
1692 * acceptable, the setcaps function should return FALSE.
1695 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1697 g_return_if_fail (GST_IS_PAD (pad));
1699 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1700 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1701 GST_DEBUG_FUNCPTR_NAME (setcaps));
1705 * gst_pad_set_bufferalloc_function:
1706 * @pad: a sink #GstPad.
1707 * @bufalloc: the #GstPadBufferAllocFunction to set.
1709 * Sets the given bufferalloc function for the pad. Note that the
1710 * bufferalloc function can only be set on sinkpads.
1713 gst_pad_set_bufferalloc_function (GstPad * pad,
1714 GstPadBufferAllocFunction bufalloc)
1716 g_return_if_fail (GST_IS_PAD (pad));
1717 g_return_if_fail (GST_PAD_IS_SINK (pad));
1719 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1720 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1721 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1726 * @srcpad: the source #GstPad to unlink.
1727 * @sinkpad: the sink #GstPad to unlink.
1729 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1730 * signal on both pads.
1732 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1733 * the pads were not linked together.
1738 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1740 gboolean result = FALSE;
1741 GstElement *parent = NULL;
1743 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1744 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1745 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1746 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1748 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1749 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1750 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1752 /* We need to notify the parent before taking any pad locks as the bin in
1753 * question might be waiting for a lock on the pad while holding its lock
1754 * that our message will try to take. */
1755 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1756 if (GST_IS_ELEMENT (parent)) {
1757 gst_element_post_message (parent,
1758 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1759 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1761 gst_object_unref (parent);
1766 GST_OBJECT_LOCK (srcpad);
1768 GST_OBJECT_LOCK (sinkpad);
1770 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1771 goto not_linked_together;
1773 if (GST_PAD_UNLINKFUNC (srcpad)) {
1774 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1776 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1777 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1780 _priv_gst_pad_invalidate_cache (srcpad);
1782 /* first clear peers */
1783 GST_PAD_PEER (srcpad) = NULL;
1784 GST_PAD_PEER (sinkpad) = NULL;
1786 GST_OBJECT_UNLOCK (sinkpad);
1787 GST_OBJECT_UNLOCK (srcpad);
1789 /* fire off a signal to each of the pads telling them
1790 * that they've been unlinked */
1791 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1792 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1794 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1795 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1800 if (parent != NULL) {
1801 gst_element_post_message (parent,
1802 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1803 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1804 gst_object_unref (parent);
1809 not_linked_together:
1811 /* we do not emit a warning in this case because unlinking cannot
1812 * be made MT safe.*/
1813 GST_OBJECT_UNLOCK (sinkpad);
1814 GST_OBJECT_UNLOCK (srcpad);
1820 * gst_pad_is_linked:
1821 * @pad: pad to check
1823 * Checks if a @pad is linked to another pad or not.
1825 * Returns: TRUE if the pad is linked, FALSE otherwise.
1830 gst_pad_is_linked (GstPad * pad)
1834 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1836 GST_OBJECT_LOCK (pad);
1837 result = (GST_PAD_PEER (pad) != NULL);
1838 GST_OBJECT_UNLOCK (pad);
1843 /* get the caps from both pads and see if the intersection
1846 * This function should be called with the pad LOCK on both
1850 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1851 GstPadLinkCheck flags)
1853 GstCaps *srccaps = NULL;
1854 GstCaps *sinkcaps = NULL;
1855 gboolean compatible = FALSE;
1857 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1860 /* Doing the expensive caps checking takes priority over only checking the template caps */
1861 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1862 srccaps = gst_pad_get_caps_unlocked (src);
1863 sinkcaps = gst_pad_get_caps_unlocked (sink);
1865 /* If one of the two pads doesn't have a template, consider the intersection
1867 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1868 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1872 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1874 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1877 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1879 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1882 /* if we have caps on both pads we can check the intersection. If one
1883 * of the caps is NULL, we return TRUE. */
1884 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1886 gst_caps_unref (srccaps);
1888 gst_caps_unref (sinkcaps);
1892 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1893 gst_caps_unref (srccaps);
1894 gst_caps_unref (sinkcaps);
1897 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1898 (compatible ? "" : "not"));
1903 /* check if the grandparents of both pads are the same.
1904 * This check is required so that we don't try to link
1905 * pads from elements in different bins without ghostpads.
1907 * The LOCK should be held on both pads
1910 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1912 GstObject *psrc, *psink;
1914 psrc = GST_OBJECT_PARENT (src);
1915 psink = GST_OBJECT_PARENT (sink);
1917 /* if one of the pads has no parent, we allow the link */
1918 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1921 /* only care about parents that are elements */
1922 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1923 goto no_element_parent;
1925 /* if the parents are the same, we have a loop */
1926 if (G_UNLIKELY (psrc == psink))
1929 /* if they both have a parent, we check the grandparents. We can not lock
1930 * the parent because we hold on the child (pad) and the locking order is
1931 * parent >> child. */
1932 psrc = GST_OBJECT_PARENT (psrc);
1933 psink = GST_OBJECT_PARENT (psink);
1935 /* if they have grandparents but they are not the same */
1936 if (G_UNLIKELY (psrc != psink))
1937 goto wrong_grandparents;
1944 GST_CAT_DEBUG (GST_CAT_CAPS,
1945 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1946 GST_PTR_FORMAT, psrc, psink);
1951 GST_CAT_DEBUG (GST_CAT_CAPS,
1952 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1953 GST_PTR_FORMAT, psrc, psink);
1958 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1964 GST_CAT_DEBUG (GST_CAT_CAPS,
1965 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1966 GST_PTR_FORMAT, psrc, psink);
1971 /* FIXME leftover from an attempt at refactoring... */
1972 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1973 * the two pads will be locked in the srcpad, sinkpad order. */
1974 static GstPadLinkReturn
1975 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1977 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1978 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1980 GST_OBJECT_LOCK (srcpad);
1982 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1983 goto src_was_linked;
1985 GST_OBJECT_LOCK (sinkpad);
1987 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1988 goto sink_was_linked;
1990 /* check hierarchy, pads can only be linked if the grandparents
1992 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1993 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1994 goto wrong_hierarchy;
1996 /* check pad caps for non-empty intersection */
1997 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2000 /* FIXME check pad scheduling for non-empty intersection */
2002 return GST_PAD_LINK_OK;
2006 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2007 GST_DEBUG_PAD_NAME (srcpad),
2008 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2009 /* we do not emit a warning in this case because unlinking cannot
2010 * be made MT safe.*/
2011 GST_OBJECT_UNLOCK (srcpad);
2012 return GST_PAD_LINK_WAS_LINKED;
2016 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2017 GST_DEBUG_PAD_NAME (sinkpad),
2018 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2019 /* we do not emit a warning in this case because unlinking cannot
2020 * be made MT safe.*/
2021 GST_OBJECT_UNLOCK (sinkpad);
2022 GST_OBJECT_UNLOCK (srcpad);
2023 return GST_PAD_LINK_WAS_LINKED;
2027 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2028 GST_OBJECT_UNLOCK (sinkpad);
2029 GST_OBJECT_UNLOCK (srcpad);
2030 return GST_PAD_LINK_WRONG_HIERARCHY;
2034 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2035 GST_OBJECT_UNLOCK (sinkpad);
2036 GST_OBJECT_UNLOCK (srcpad);
2037 return GST_PAD_LINK_NOFORMAT;
2043 * @srcpad: the source #GstPad.
2044 * @sinkpad: the sink #GstPad.
2046 * Checks if the source pad and the sink pad are compatible so they can be
2049 * Returns: TRUE if the pads can be linked.
2052 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2054 GstPadLinkReturn result;
2056 /* generic checks */
2057 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2058 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2060 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2061 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2063 /* gst_pad_link_prepare does everything for us, we only release the locks
2064 * on the pads that it gets us. If this function returns !OK the locks are not
2066 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2067 if (result != GST_PAD_LINK_OK)
2070 GST_OBJECT_UNLOCK (srcpad);
2071 GST_OBJECT_UNLOCK (sinkpad);
2074 return result == GST_PAD_LINK_OK;
2078 * gst_pad_link_full:
2079 * @srcpad: the source #GstPad to link.
2080 * @sinkpad: the sink #GstPad to link.
2081 * @flags: the checks to validate when linking
2083 * Links the source pad and the sink pad.
2085 * This variant of #gst_pad_link provides a more granular control on the
2086 * checks being done when linking. While providing some considerable speedups
2087 * the caller of this method must be aware that wrong usage of those flags
2088 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2089 * for more information.
2093 * Returns: A result code indicating if the connection worked or
2099 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2101 GstPadLinkReturn result;
2104 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2105 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2106 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2107 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2108 GST_PAD_LINK_WRONG_DIRECTION);
2110 /* Notify the parent early. See gst_pad_unlink for details. */
2111 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2112 if (GST_IS_ELEMENT (parent)) {
2113 gst_element_post_message (parent,
2114 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2115 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2117 gst_object_unref (parent);
2122 /* prepare will also lock the two pads */
2123 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2125 if (result != GST_PAD_LINK_OK)
2128 /* must set peers before calling the link function */
2129 GST_PAD_PEER (srcpad) = sinkpad;
2130 GST_PAD_PEER (sinkpad) = srcpad;
2132 GST_OBJECT_UNLOCK (sinkpad);
2133 GST_OBJECT_UNLOCK (srcpad);
2135 /* FIXME released the locks here, concurrent thread might link
2136 * something else. */
2137 if (GST_PAD_LINKFUNC (srcpad)) {
2138 /* this one will call the peer link function */
2139 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2140 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2141 /* if no source link function, we need to call the sink link
2142 * function ourselves. */
2143 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2145 result = GST_PAD_LINK_OK;
2148 GST_OBJECT_LOCK (srcpad);
2149 GST_OBJECT_LOCK (sinkpad);
2151 if (result == GST_PAD_LINK_OK) {
2152 GST_OBJECT_UNLOCK (sinkpad);
2153 GST_OBJECT_UNLOCK (srcpad);
2155 /* fire off a signal to each of the pads telling them
2156 * that they've been linked */
2157 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2158 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2160 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2161 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2163 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2164 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2166 GST_PAD_PEER (srcpad) = NULL;
2167 GST_PAD_PEER (sinkpad) = NULL;
2169 GST_OBJECT_UNLOCK (sinkpad);
2170 GST_OBJECT_UNLOCK (srcpad);
2175 gst_element_post_message (parent,
2176 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2177 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2178 gst_object_unref (parent);
2186 * @srcpad: the source #GstPad to link.
2187 * @sinkpad: the sink #GstPad to link.
2189 * Links the source pad and the sink pad.
2191 * Returns: A result code indicating if the connection worked or
2197 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2199 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2203 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2205 GstPadTemplate **template_p;
2207 /* this function would need checks if it weren't static */
2209 GST_OBJECT_LOCK (pad);
2210 template_p = &pad->padtemplate;
2211 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2212 GST_OBJECT_UNLOCK (pad);
2215 gst_pad_template_pad_created (templ, pad);
2219 * gst_pad_get_pad_template:
2222 * Gets the template for @pad.
2224 * Returns: (transfer none): the #GstPadTemplate from which this pad was
2225 * instantiated, or %NULL if this pad has no template.
2227 * FIXME: currently returns an unrefcounted padtemplate.
2230 gst_pad_get_pad_template (GstPad * pad)
2232 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2234 return GST_PAD_PAD_TEMPLATE (pad);
2238 /* should be called with the pad LOCK held */
2239 /* refs the caps, so caller is responsible for getting it unreffed */
2241 gst_pad_get_caps_unlocked (GstPad * pad)
2243 GstCaps *result = NULL;
2244 GstPadTemplate *templ;
2246 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2248 if (GST_PAD_GETCAPSFUNC (pad)) {
2249 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2250 "dispatching to pad getcaps function");
2252 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2253 GST_OBJECT_UNLOCK (pad);
2254 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2255 GST_OBJECT_LOCK (pad);
2256 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2258 if (result == NULL) {
2259 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2260 GST_DEBUG_PAD_NAME (pad));
2262 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2263 "pad getcaps returned %" GST_PTR_FORMAT, result);
2264 #ifndef G_DISABLE_ASSERT
2265 /* check that the returned caps are a real subset of the template caps */
2266 if (GST_PAD_PAD_TEMPLATE (pad)) {
2267 const GstCaps *templ_caps =
2268 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2269 if (!gst_caps_is_subset (result, templ_caps)) {
2272 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2273 "pad returned caps %" GST_PTR_FORMAT
2274 " which are not a real subset of its template caps %"
2275 GST_PTR_FORMAT, result, templ_caps);
2277 ("pad %s:%s returned caps which are not a real "
2278 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2279 temp = gst_caps_intersect (templ_caps, result);
2280 gst_caps_unref (result);
2288 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2289 result = GST_PAD_TEMPLATE_CAPS (templ);
2290 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2291 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2294 result = gst_caps_ref (result);
2297 if ((result = GST_PAD_CAPS (pad))) {
2298 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2299 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2301 result = gst_caps_ref (result);
2305 /* this almost never happens */
2306 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2307 result = gst_caps_new_empty ();
2313 /* FIXME-0.11: what about making this the default and using
2314 * gst_caps_make_writable() explicitly where needed
2317 * gst_pad_get_caps_reffed:
2318 * @pad: a #GstPad to get the capabilities of.
2320 * Gets the capabilities this pad can produce or consume. Preferred function if
2321 * one only wants to read or intersect the caps.
2323 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2328 gst_pad_get_caps_reffed (GstPad * pad)
2330 GstCaps *result = NULL;
2332 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2334 GST_OBJECT_LOCK (pad);
2336 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2338 result = gst_pad_get_caps_unlocked (pad);
2340 GST_OBJECT_UNLOCK (pad);
2347 * @pad: a #GstPad to get the capabilities of.
2349 * Gets the capabilities this pad can produce or consume.
2350 * Note that this method doesn't necessarily return the caps set by
2351 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2352 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2353 * the pad's get_caps function;
2354 * this returns the pad template caps if not explicitly set.
2356 * Returns: (transfer full): a newly allocated copy of the #GstCaps of this pad
2361 gst_pad_get_caps (GstPad * pad)
2363 GstCaps *result = gst_pad_get_caps_reffed (pad);
2365 /* be sure that we have a copy */
2366 if (G_LIKELY (result))
2367 result = gst_caps_make_writable (result);
2372 /* FIXME-0.11: what about making this the default and using
2373 * gst_caps_make_writable() explicitly where needed
2376 * gst_pad_peer_get_caps_reffed:
2377 * @pad: a #GstPad to get the capabilities of.
2379 * Gets the capabilities of the peer connected to this pad. Preferred function
2380 * if one only wants to read or intersect the caps.
2382 * Returns: (transfer full): the caps of the pad with incremented ref-count
2387 gst_pad_peer_get_caps_reffed (GstPad * pad)
2390 GstCaps *result = NULL;
2392 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2394 GST_OBJECT_LOCK (pad);
2396 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2398 peerpad = GST_PAD_PEER (pad);
2399 if (G_UNLIKELY (peerpad == NULL))
2402 gst_object_ref (peerpad);
2403 GST_OBJECT_UNLOCK (pad);
2405 result = gst_pad_get_caps_reffed (peerpad);
2407 gst_object_unref (peerpad);
2413 GST_OBJECT_UNLOCK (pad);
2419 * gst_pad_peer_get_caps:
2420 * @pad: a #GstPad to get the peer capabilities of.
2422 * Gets the capabilities of the peer connected to this pad. Similar to
2423 * gst_pad_get_caps().
2425 * Returns: (transfer full): a newly allocated copy of the #GstCaps of the
2426 * peer pad. Use gst_caps_unref() to get rid of it. This function
2427 * returns %NULL if there is no peer pad.
2430 gst_pad_peer_get_caps (GstPad * pad)
2433 GstCaps *result = NULL;
2435 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2437 GST_OBJECT_LOCK (pad);
2439 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2441 peerpad = GST_PAD_PEER (pad);
2442 if (G_UNLIKELY (peerpad == NULL))
2445 gst_object_ref (peerpad);
2446 GST_OBJECT_UNLOCK (pad);
2448 result = gst_pad_get_caps (peerpad);
2450 gst_object_unref (peerpad);
2456 GST_OBJECT_UNLOCK (pad);
2462 fixate_value (GValue * dest, const GValue * src)
2464 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2465 g_value_init (dest, G_TYPE_INT);
2466 g_value_set_int (dest, gst_value_get_int_range_min (src));
2467 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2468 g_value_init (dest, G_TYPE_DOUBLE);
2469 g_value_set_double (dest, gst_value_get_double_range_min (src));
2470 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2471 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2472 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2473 GValue temp = { 0 };
2475 /* list could be empty */
2476 if (gst_value_list_get_size (src) <= 0)
2479 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2481 if (!fixate_value (dest, &temp))
2482 gst_value_init_and_copy (dest, &temp);
2483 g_value_unset (&temp);
2484 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2485 gboolean res = FALSE;
2488 len = gst_value_array_get_size (src);
2489 g_value_init (dest, GST_TYPE_ARRAY);
2490 for (n = 0; n < len; n++) {
2492 const GValue *orig_kid = gst_value_array_get_value (src, n);
2494 if (!fixate_value (&kid, orig_kid))
2495 gst_value_init_and_copy (&kid, orig_kid);
2498 gst_value_array_append_value (dest, &kid);
2499 g_value_unset (&kid);
2503 g_value_unset (dest);
2514 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2516 GstStructure *s = data;
2519 if (fixate_value (&v, value)) {
2520 gst_structure_id_set_value (s, field_id, &v);
2528 * gst_pad_fixate_caps:
2529 * @pad: a #GstPad to fixate
2530 * @caps: the #GstCaps to fixate
2532 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2533 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2536 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2538 GstPadFixateCapsFunction fixatefunc;
2541 g_return_if_fail (GST_IS_PAD (pad));
2542 g_return_if_fail (caps != NULL);
2543 g_return_if_fail (!gst_caps_is_empty (caps));
2544 /* FIXME-0.11: do not allow fixating any-caps
2545 * g_return_if_fail (!gst_caps_is_any (caps));
2548 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2551 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2553 fixatefunc (pad, caps);
2556 /* default fixation */
2557 gst_caps_truncate (caps);
2558 s = gst_caps_get_structure (caps, 0);
2559 gst_structure_foreach (s, gst_pad_default_fixate, s);
2562 /* Default accept caps implementation just checks against
2563 * against the allowed caps for the pad */
2565 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2567 /* get the caps and see if it intersects to something not empty */
2569 gboolean result = FALSE;
2571 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2573 allowed = gst_pad_get_caps_reffed (pad);
2575 goto nothing_allowed;
2577 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2579 result = gst_caps_can_intersect (allowed, caps);
2581 gst_caps_unref (allowed);
2588 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2594 * gst_pad_accept_caps:
2595 * @pad: a #GstPad to check
2596 * @caps: a #GstCaps to check on the pad
2598 * Check if the given pad accepts the caps.
2600 * Returns: TRUE if the pad can accept the caps.
2603 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2606 GstPadAcceptCapsFunction acceptfunc;
2607 GstCaps *existing = NULL;
2609 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2611 /* any pad can be unnegotiated */
2615 /* lock for checking the existing caps */
2616 GST_OBJECT_LOCK (pad);
2617 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2618 /* The current caps on a pad are trivially acceptable */
2619 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2620 if (caps == existing || gst_caps_is_equal (caps, existing))
2623 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2624 GST_OBJECT_UNLOCK (pad);
2626 if (G_LIKELY (acceptfunc)) {
2627 /* we can call the function */
2628 result = acceptfunc (pad, caps);
2629 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2631 /* Only null if the element explicitly unset it */
2632 result = gst_pad_acceptcaps_default (pad, caps);
2633 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2640 GST_DEBUG_OBJECT (pad, "pad had same caps");
2641 GST_OBJECT_UNLOCK (pad);
2647 * gst_pad_peer_accept_caps:
2648 * @pad: a #GstPad to check the peer of
2649 * @caps: a #GstCaps to check on the pad
2651 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2654 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2657 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2662 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2664 GST_OBJECT_LOCK (pad);
2666 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2668 peerpad = GST_PAD_PEER (pad);
2669 if (G_UNLIKELY (peerpad == NULL))
2672 gst_object_ref (peerpad);
2673 /* release lock before calling external methods but keep ref to pad */
2674 GST_OBJECT_UNLOCK (pad);
2676 result = gst_pad_accept_caps (peerpad, caps);
2678 gst_object_unref (peerpad);
2684 GST_OBJECT_UNLOCK (pad);
2691 * @pad: a #GstPad to set the capabilities of.
2692 * @caps: (transfer none): a #GstCaps to set.
2694 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2695 * caps on the pad will be unreffed. This function refs the caps so you should
2696 * unref if as soon as you don't need it anymore.
2697 * It is possible to set NULL caps, which will make the pad unnegotiated
2700 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2701 * or bad parameters were provided to this function.
2706 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2708 GstPadSetCapsFunction setcaps;
2711 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2712 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2714 GST_OBJECT_LOCK (pad);
2715 existing = GST_PAD_CAPS (pad);
2716 if (existing == caps)
2719 if (gst_caps_is_equal (caps, existing))
2720 goto setting_same_caps;
2722 setcaps = GST_PAD_SETCAPSFUNC (pad);
2724 /* call setcaps function to configure the pad only if the
2725 * caps is not NULL */
2726 if (setcaps != NULL && caps) {
2727 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2728 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2729 GST_OBJECT_UNLOCK (pad);
2730 if (!setcaps (pad, caps))
2732 GST_OBJECT_LOCK (pad);
2733 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2735 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2739 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2740 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2742 GST_OBJECT_UNLOCK (pad);
2744 #if GLIB_CHECK_VERSION(2,26,0)
2745 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2747 g_object_notify ((GObject *) pad, "caps");
2754 GST_OBJECT_UNLOCK (pad);
2759 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2760 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2761 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2763 GST_OBJECT_UNLOCK (pad);
2770 GST_OBJECT_LOCK (pad);
2771 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2772 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2773 "caps %" GST_PTR_FORMAT " could not be set", caps);
2774 GST_OBJECT_UNLOCK (pad);
2781 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2785 /* See if pad accepts the caps */
2786 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2789 /* set caps on pad if call succeeds */
2790 res = gst_pad_set_caps (pad, caps);
2791 /* no need to unref the caps here, set_caps takes a ref and
2792 * our ref goes away when we leave this function. */
2798 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2799 "caps %" GST_PTR_FORMAT " not accepted", caps);
2804 /* returns TRUE if the src pad could be configured to accept the given caps */
2806 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2811 /* See if pad accepts the caps */
2812 if (!gst_pad_accept_caps (pad, caps))
2815 res = gst_pad_set_caps (pad, caps);
2823 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2824 "caps %" GST_PTR_FORMAT " not accepted", caps);
2830 * gst_pad_get_pad_template_caps:
2831 * @pad: a #GstPad to get the template capabilities from.
2833 * Gets the capabilities for @pad's template.
2835 * Returns: (transfer none): the #GstCaps of this pad template. If you intend
2836 * to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2839 gst_pad_get_pad_template_caps (GstPad * pad)
2841 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2843 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2845 if (GST_PAD_PAD_TEMPLATE (pad))
2846 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2848 return gst_static_caps_get (&anycaps);
2853 * @pad: a #GstPad to get the peer of.
2855 * Gets the peer of @pad. This function refs the peer pad so
2856 * you need to unref it after use.
2858 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2863 gst_pad_get_peer (GstPad * pad)
2867 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2869 GST_OBJECT_LOCK (pad);
2870 result = GST_PAD_PEER (pad);
2872 gst_object_ref (result);
2873 GST_OBJECT_UNLOCK (pad);
2879 * gst_pad_get_allowed_caps:
2882 * Gets the capabilities of the allowed media types that can flow through
2883 * @pad and its peer.
2885 * The allowed capabilities is calculated as the intersection of the results of
2886 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2887 * on the resulting caps.
2889 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2890 * caps when you no longer need it. This function returns NULL when @pad
2896 gst_pad_get_allowed_caps (GstPad * pad)
2903 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2905 GST_OBJECT_LOCK (pad);
2907 peer = GST_PAD_PEER (pad);
2908 if (G_UNLIKELY (peer == NULL))
2911 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2913 gst_object_ref (peer);
2914 GST_OBJECT_UNLOCK (pad);
2915 mycaps = gst_pad_get_caps_reffed (pad);
2917 peercaps = gst_pad_get_caps_reffed (peer);
2918 gst_object_unref (peer);
2920 caps = gst_caps_intersect (mycaps, peercaps);
2921 gst_caps_unref (peercaps);
2922 gst_caps_unref (mycaps);
2924 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2931 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2932 GST_OBJECT_UNLOCK (pad);
2939 * gst_pad_get_negotiated_caps:
2942 * Gets the capabilities of the media type that currently flows through @pad
2945 * This function can be used on both src and sinkpads. Note that srcpads are
2946 * always negotiated before sinkpads so it is possible that the negotiated caps
2947 * on the srcpad do not match the negotiated caps of the peer.
2949 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2950 * the caps when you no longer need it. This function returns NULL when
2951 * the @pad has no peer or is not negotiated yet.
2956 gst_pad_get_negotiated_caps (GstPad * pad)
2961 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2963 GST_OBJECT_LOCK (pad);
2965 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2968 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2970 caps = GST_PAD_CAPS (pad);
2972 gst_caps_ref (caps);
2973 GST_OBJECT_UNLOCK (pad);
2975 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2982 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2983 GST_OBJECT_UNLOCK (pad);
2989 /* calls the buffer_alloc function on the given pad */
2990 static GstFlowReturn
2991 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2992 GstCaps * caps, GstBuffer ** buf)
2995 GstPadBufferAllocFunction bufferallocfunc;
2997 GST_OBJECT_LOCK (pad);
2998 /* when the pad is flushing we cannot give a buffer */
2999 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3002 bufferallocfunc = pad->bufferallocfunc;
3004 if (offset == GST_BUFFER_OFFSET_NONE) {
3005 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3006 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
3007 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
3009 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3010 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
3011 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
3012 bufferallocfunc, size, offset);
3014 GST_OBJECT_UNLOCK (pad);
3016 /* G_LIKELY for now since most elements don't implement a buffer alloc
3017 * function and there is no default alloc proxy function as this is usually
3019 if (G_LIKELY (bufferallocfunc == NULL))
3022 ret = bufferallocfunc (pad, offset, size, caps, buf);
3024 if (G_UNLIKELY (ret != GST_FLOW_OK))
3027 /* no error, but NULL buffer means fallback to the default */
3028 if (G_UNLIKELY (*buf == NULL))
3031 /* If the buffer alloc function didn't set up the caps like it should,
3033 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
3034 GST_WARNING_OBJECT (pad,
3035 "Buffer allocation function did not set caps. Setting");
3036 gst_buffer_set_caps (*buf, caps);
3042 /* pad was flushing */
3043 GST_OBJECT_UNLOCK (pad);
3044 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
3045 return GST_FLOW_WRONG_STATE;
3049 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3050 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
3055 /* fallback case, allocate a buffer of our own, add pad caps. */
3056 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
3058 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3059 GST_BUFFER_OFFSET (*buf) = offset;
3060 gst_buffer_set_caps (*buf, caps);
3063 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3064 "out of memory allocating %d bytes", size);
3065 return GST_FLOW_ERROR;
3070 /* FIXME 0.11: size should be unsigned */
3071 static GstFlowReturn
3072 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
3073 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
3078 gboolean caps_changed;
3080 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3081 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3082 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
3083 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
3085 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
3086 GST_PTR_FORMAT, offset, size, caps);
3088 GST_OBJECT_LOCK (pad);
3089 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3090 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3093 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3096 gst_object_ref (peer);
3097 GST_OBJECT_UNLOCK (pad);
3099 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
3100 gst_object_unref (peer);
3102 if (G_UNLIKELY (ret != GST_FLOW_OK))
3105 /* FIXME, move capnego this into a base class? */
3106 newcaps = GST_BUFFER_CAPS (*buf);
3108 /* Lock for checking caps, pretty pointless as the _pad_push() function might
3109 * change it concurrently, one of the problems with automatic caps setting in
3110 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
3111 * when there is heavy renegotiation going on in both directions. */
3112 GST_OBJECT_LOCK (pad);
3113 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
3114 GST_OBJECT_UNLOCK (pad);
3116 /* we got a new datatype on the pad, see if it can handle it */
3117 if (G_UNLIKELY (caps_changed)) {
3118 GST_DEBUG_OBJECT (pad,
3119 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3120 GST_PAD_CAPS (pad), newcaps, newcaps);
3121 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
3122 goto not_negotiated;
3125 /* sanity check (only if caps are the same) */
3126 if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
3127 goto wrong_size_fallback;
3133 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
3134 GST_OBJECT_UNLOCK (pad);
3139 /* pad has no peer */
3140 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3141 "called bufferallocfunc but had no peer");
3142 GST_OBJECT_UNLOCK (pad);
3143 return GST_FLOW_NOT_LINKED;
3147 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3148 "alloc function returned error %s", gst_flow_get_name (ret));
3153 gst_buffer_unref (*buf);
3155 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3156 "alloc function returned unacceptable buffer");
3157 return GST_FLOW_NOT_NEGOTIATED;
3159 wrong_size_fallback:
3161 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3162 "function is too small (%u < %d), doing fallback buffer alloc",
3163 GST_BUFFER_SIZE (*buf), size);
3165 gst_buffer_unref (*buf);
3167 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3168 GST_BUFFER_OFFSET (*buf) = offset;
3169 gst_buffer_set_caps (*buf, caps);
3172 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3173 "out of memory allocating %d bytes", size);
3174 return GST_FLOW_ERROR;
3180 * gst_pad_alloc_buffer:
3181 * @pad: a source #GstPad
3182 * @offset: the offset of the new buffer in the stream
3183 * @size: the size of the new buffer
3184 * @caps: the caps of the new buffer
3185 * @buf: a newly allocated buffer
3187 * Allocates a new, empty buffer optimized to push to pad @pad. This
3188 * function only works if @pad is a source pad and has a peer.
3190 * A new, empty #GstBuffer will be put in the @buf argument.
3191 * You need to check the caps of the buffer after performing this
3192 * function and renegotiate to the format if needed. If the caps changed, it is
3193 * possible that the buffer returned in @buf is not of the right size for the
3194 * new format, @buf needs to be unreffed and reallocated if this is the case.
3196 * Returns: a result code indicating success of the operation. Any
3197 * result code other than #GST_FLOW_OK is an error and @buf should
3199 * An error can occur if the pad is not connected or when the downstream
3200 * peer elements cannot provide an acceptable buffer.
3205 /* FIXME 0.11: size should be unsigned */
3207 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3210 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3214 * gst_pad_alloc_buffer_and_set_caps:
3215 * @pad: a source #GstPad
3216 * @offset: the offset of the new buffer in the stream
3217 * @size: the size of the new buffer
3218 * @caps: (transfer none): the caps of the new buffer
3219 * @buf: (out callee-allocates): a newly allocated buffer
3221 * In addition to the function gst_pad_alloc_buffer(), this function
3222 * automatically calls gst_pad_set_caps() when the caps of the
3223 * newly allocated buffer are different from the @pad caps.
3225 * After a renegotiation, the size of the new buffer returned in @buf could
3226 * be of the wrong size for the new format and must be unreffed an reallocated
3229 * Returns: a result code indicating success of the operation. Any
3230 * result code other than #GST_FLOW_OK is an error and @buf should
3232 * An error can occur if the pad is not connected or when the downstream
3233 * peer elements cannot provide an acceptable buffer.
3238 /* FIXME 0.11: size should be unsigned */
3240 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3241 GstCaps * caps, GstBuffer ** buf)
3243 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3247 #ifndef GST_REMOVE_DEPRECATED
3255 int_link_iter_data_free (IntLinkIterData * data)
3257 g_list_free (data->list);
3258 g_slice_free (IntLinkIterData, data);
3262 static GstIteratorItem
3263 iterate_pad (GstIterator * it, GstPad * pad)
3265 gst_object_ref (pad);
3266 return GST_ITERATOR_ITEM_PASS;
3270 * gst_pad_iterate_internal_links_default:
3271 * @pad: the #GstPad to get the internal links of.
3273 * Iterate the list of pads to which the given pad is linked to inside of
3274 * the parent element.
3275 * This is the default handler, and thus returns an iterator of all of the
3276 * pads inside the parent element with opposite direction.
3278 * The caller must free this iterator after use with gst_iterator_free().
3280 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3281 * returned pad with gst_object_unref().
3286 gst_pad_iterate_internal_links_default (GstPad * pad)
3293 GstIteratorDisposeFunction dispose;
3295 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3297 #ifndef GST_REMOVE_DEPRECATED
3298 /* when we get here, the default handler for the iterate links is called,
3299 * which means that the user has not installed a custom one. We first check if
3300 * there is maybe a custom legacy function we can call. */
3301 if (GST_PAD_INTLINKFUNC (pad) &&
3302 GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3303 IntLinkIterData *data;
3305 /* make an iterator for the list. We can't protect the list with a
3306 * cookie. If we would take the cookie of the parent element, we need to
3307 * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3308 * cache the per-pad list and invalidate the list when a new call to
3309 * INTLINKFUNC() returned a different list but then this would only work if
3310 * two concurrent iterators were used and the last iterator would still be
3311 * thread-unsafe. Just don't use this method anymore. */
3312 data = g_slice_new (IntLinkIterData);
3313 data->list = ((GstPadIntLinkFunction) GST_PAD_INTLINKFUNC (pad)) (pad);
3316 GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3318 cookie = &data->cookie;
3319 padlist = &data->list;
3321 dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3322 /* reuse the pad lock, it's all we have here */
3323 lock = GST_OBJECT_GET_LOCK (pad);
3329 GST_OBJECT_LOCK (pad);
3330 parent = GST_PAD_PARENT (pad);
3331 if (!parent || !GST_IS_ELEMENT (parent))
3334 gst_object_ref (parent);
3335 GST_OBJECT_UNLOCK (pad);
3337 if (pad->direction == GST_PAD_SRC)
3338 padlist = &parent->sinkpads;
3340 padlist = &parent->srcpads;
3342 GST_DEBUG_OBJECT (pad, "Making iterator");
3344 cookie = &parent->pads_cookie;
3346 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3347 lock = GST_OBJECT_GET_LOCK (parent);
3350 res = gst_iterator_new_list (GST_TYPE_PAD,
3351 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3359 GST_OBJECT_UNLOCK (pad);
3360 GST_DEBUG_OBJECT (pad, "no parent element");
3366 * gst_pad_iterate_internal_links:
3367 * @pad: the GstPad to get the internal links of.
3369 * Gets an iterator for the pads to which the given pad is linked to inside
3370 * of the parent element.
3372 * Each #GstPad element yielded by the iterator will have its refcount increased,
3373 * so unref after use.
3375 * Free-function: gst_iterator_free
3377 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3378 * pad does not have an iterator function configured. Use
3379 * gst_iterator_free() after usage.
3384 gst_pad_iterate_internal_links (GstPad * pad)
3386 GstIterator *res = NULL;
3388 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3390 if (GST_PAD_ITERINTLINKFUNC (pad))
3391 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3396 #ifndef GST_REMOVE_DEPRECATED
3398 add_unref_pad_to_list (GstPad * pad, GList ** list)
3400 *list = g_list_prepend (*list, pad);
3401 gst_object_unref (pad);
3406 * gst_pad_get_internal_links_default:
3407 * @pad: the #GstPad to get the internal links of.
3409 * Gets a list of pads to which the given pad is linked to
3410 * inside of the parent element.
3411 * This is the default handler, and thus returns a list of all of the
3412 * pads inside the parent element with opposite direction.
3414 * The caller must free this list after use with g_list_free().
3416 * Returns: (transfer full) (element-type Gst.Pad): a newly allocated #GList
3417 * of pads, or NULL if the pad has no parent.
3421 * Deprecated: This function does not ref the pads in the list so that they
3422 * could become invalid by the time the application accesses them. It's also
3423 * possible that the list changes while handling the pads, which the caller of
3424 * this function is unable to know. Use the thread-safe
3425 * gst_pad_iterate_internal_links_default() instead.
3427 #ifndef GST_REMOVE_DEPRECATED
3429 gst_pad_get_internal_links_default (GstPad * pad)
3434 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3436 GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3438 /* when we get here, the default handler for get_internal_links is called,
3439 * which means that the user has not installed a custom one. We first check if
3440 * there is maybe a custom iterate function we can call. */
3441 if (GST_PAD_ITERINTLINKFUNC (pad) &&
3442 GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3444 GstIteratorResult ires;
3445 gboolean done = FALSE;
3447 it = gst_pad_iterate_internal_links (pad);
3448 /* loop over the iterator and put all elements into a list, we also
3449 * immediately unref them, which is bad. */
3451 ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, &res);
3453 case GST_ITERATOR_OK:
3454 case GST_ITERATOR_DONE:
3455 case GST_ITERATOR_ERROR:
3458 case GST_ITERATOR_RESYNC:
3459 /* restart, discard previous list */
3460 gst_iterator_resync (it);
3467 gst_iterator_free (it);
3469 /* lock pad, check and ref parent */
3470 GST_OBJECT_LOCK (pad);
3471 parent = GST_PAD_PARENT (pad);
3472 if (!parent || !GST_IS_ELEMENT (parent))
3475 parent = gst_object_ref (parent);
3476 GST_OBJECT_UNLOCK (pad);
3478 /* now lock the parent while we copy the pads */
3479 GST_OBJECT_LOCK (parent);
3480 if (pad->direction == GST_PAD_SRC)
3481 res = g_list_copy (parent->sinkpads);
3483 res = g_list_copy (parent->srcpads);
3484 GST_OBJECT_UNLOCK (parent);
3486 gst_object_unref (parent);
3489 /* At this point pads can be changed and unreffed. Nothing we can do about it
3490 * because for compatibility reasons this function cannot ref the pads or
3491 * notify the app that the list changed. */
3497 GST_DEBUG_OBJECT (pad, "no parent");
3498 GST_OBJECT_UNLOCK (pad);
3502 #endif /* GST_REMOVE_DEPRECATED */
3505 * gst_pad_get_internal_links:
3506 * @pad: the #GstPad to get the internal links of.
3508 * Gets a list of pads to which the given pad is linked to
3509 * inside of the parent element.
3510 * The caller must free this list after use.
3514 * Returns: (transfer full) (element-type Gst.Pad): a newly allocated #GList
3515 * of pads, free with g_list_free().
3517 * Deprecated: This function does not ref the pads in the list so that they
3518 * could become invalid by the time the application accesses them. It's also
3519 * possible that the list changes while handling the pads, which the caller of
3520 * this function is unable to know. Use the thread-safe
3521 * gst_pad_iterate_internal_links() instead.
3523 #ifndef GST_REMOVE_DEPRECATED
3524 #ifdef GST_DISABLE_DEPRECATED
3525 GList *gst_pad_get_internal_links (GstPad * pad);
3528 gst_pad_get_internal_links (GstPad * pad)
3532 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3534 GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3536 if (GST_PAD_INTLINKFUNC (pad))
3537 res = ((GstPadIntLinkFunction) GST_PAD_INTLINKFUNC (pad)) (pad);
3541 #endif /* GST_REMOVE_DEPRECATED */
3544 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3546 gboolean result = FALSE;
3548 gboolean done = FALSE;
3551 GList *pushed_pads = NULL;
3553 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3554 event, GST_EVENT_TYPE_NAME (event));
3556 iter = gst_pad_iterate_internal_links (pad);
3562 switch (gst_iterator_next (iter, &item)) {
3563 case GST_ITERATOR_OK:
3564 eventpad = GST_PAD_CAST (item);
3566 /* if already pushed, skip */
3567 if (g_list_find (pushed_pads, eventpad)) {
3568 gst_object_unref (item);
3572 if (GST_PAD_IS_SRC (eventpad)) {
3573 /* for each pad we send to, we should ref the event; it's up
3574 * to downstream to unref again when handled. */
3575 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3576 event, GST_EVENT_TYPE_NAME (event),
3577 GST_DEBUG_PAD_NAME (eventpad));
3578 gst_event_ref (event);
3579 result |= gst_pad_push_event (eventpad, event);
3581 /* we only send the event on one pad, multi-sinkpad elements
3582 * should implement a handler */
3583 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3584 event, GST_EVENT_TYPE_NAME (event),
3585 GST_DEBUG_PAD_NAME (eventpad));
3586 result = gst_pad_push_event (eventpad, event);
3591 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3593 gst_object_unref (item);
3595 case GST_ITERATOR_RESYNC:
3596 /* We don't reset the result here because we don't push the event
3597 * again on pads that got the event already and because we need
3598 * to consider the result of the previous pushes */
3599 gst_iterator_resync (iter);
3601 case GST_ITERATOR_ERROR:
3602 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3605 case GST_ITERATOR_DONE:
3610 gst_iterator_free (iter);
3614 /* If this is a sinkpad and we don't have pads to send the event to, we
3615 * return TRUE. This is so that when using the default handler on a sink
3616 * element, we don't fail to push it. */
3618 result = GST_PAD_IS_SINK (pad);
3620 g_list_free (pushed_pads);
3622 /* we handled the incoming event so we unref once */
3624 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3625 gst_event_unref (event);
3632 * gst_pad_event_default:
3633 * @pad: a #GstPad to call the default event handler on.
3634 * @event: (transfer full): the #GstEvent to handle.
3636 * Invokes the default event handler for the given pad. End-of-stream and
3637 * discontinuity events are handled specially, and then the event is sent to all
3638 * pads internally linked to @pad. Note that if there are many possible sink
3639 * pads that are internally linked to @pad, only one will be sent an event.
3640 * Multi-sinkpad elements should implement custom event handlers.
3642 * Returns: TRUE if the event was sent successfully.
3645 gst_pad_event_default (GstPad * pad, GstEvent * event)
3647 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3648 g_return_val_if_fail (event != NULL, FALSE);
3650 GST_LOG_OBJECT (pad, "default event handler");
3652 switch (GST_EVENT_TYPE (event)) {
3655 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3656 gst_pad_pause_task (pad);
3663 return gst_pad_event_default_dispatch (pad, event);
3667 * gst_pad_dispatcher:
3668 * @pad: a #GstPad to dispatch.
3669 * @dispatch: the #GstPadDispatcherFunction to call.
3670 * @data: (closure): gpointer user data passed to the dispatcher function.
3672 * Invokes the given dispatcher function on each respective peer of
3673 * all pads that are internally linked to the given pad.
3674 * The GstPadDispatcherFunction should return TRUE when no further pads
3675 * need to be processed.
3677 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3680 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3683 gboolean res = FALSE;
3684 GstIterator *iter = NULL;
3685 gboolean done = FALSE;
3688 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3689 g_return_val_if_fail (dispatch != NULL, FALSE);
3691 iter = gst_pad_iterate_internal_links (pad);
3697 switch (gst_iterator_next (iter, &item)) {
3698 case GST_ITERATOR_OK:
3700 GstPad *int_pad = GST_PAD_CAST (item);
3701 GstPad *int_peer = gst_pad_get_peer (int_pad);
3704 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3705 GST_DEBUG_PAD_NAME (int_peer));
3706 done = res = dispatch (int_peer, data);
3707 gst_object_unref (int_peer);
3709 GST_DEBUG_OBJECT (int_pad, "no peer");
3712 gst_object_unref (item);
3714 case GST_ITERATOR_RESYNC:
3715 gst_iterator_resync (iter);
3717 case GST_ITERATOR_ERROR:
3719 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3721 case GST_ITERATOR_DONE:
3726 gst_iterator_free (iter);
3728 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3737 * @pad: a #GstPad to invoke the default query on.
3738 * @query: (transfer none): the #GstQuery to perform.
3740 * Dispatches a query to a pad. The query should have been allocated by the
3741 * caller via one of the type-specific allocation functions. The element that
3742 * the pad belongs to is responsible for filling the query with an appropriate
3743 * response, which should then be parsed with a type-specific query parsing
3746 * Again, the caller is responsible for both the allocation and deallocation of
3747 * the query structure.
3749 * Please also note that some queries might need a running pipeline to work.
3751 * Returns: TRUE if the query could be performed.
3754 gst_pad_query (GstPad * pad, GstQuery * query)
3756 GstPadQueryFunction func;
3758 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3759 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3761 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3763 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3766 return func (pad, query);
3770 GST_DEBUG_OBJECT (pad, "had no query function");
3776 * gst_pad_peer_query:
3777 * @pad: a #GstPad to invoke the peer query on.
3778 * @query: (transfer none): the #GstQuery to perform.
3780 * Performs gst_pad_query() on the peer of @pad.
3782 * The caller is responsible for both the allocation and deallocation of
3783 * the query structure.
3785 * Returns: TRUE if the query could be performed. This function returns %FALSE
3786 * if @pad has no peer.
3791 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3796 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3797 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3799 GST_OBJECT_LOCK (pad);
3801 GST_DEBUG_OBJECT (pad, "peer query");
3803 peerpad = GST_PAD_PEER (pad);
3804 if (G_UNLIKELY (peerpad == NULL))
3807 gst_object_ref (peerpad);
3808 GST_OBJECT_UNLOCK (pad);
3810 result = gst_pad_query (peerpad, query);
3812 gst_object_unref (peerpad);
3819 GST_WARNING_OBJECT (pad, "pad has no peer");
3820 GST_OBJECT_UNLOCK (pad);
3826 * gst_pad_query_default:
3827 * @pad: a #GstPad to call the default query handler on.
3828 * @query: (transfer none): the #GstQuery to handle.
3830 * Invokes the default query handler for the given pad.
3831 * The query is sent to all pads internally linked to @pad. Note that
3832 * if there are many possible sink pads that are internally linked to
3833 * @pad, only one will be sent the query.
3834 * Multi-sinkpad elements should implement custom query handlers.
3836 * Returns: TRUE if the query was performed successfully.
3839 gst_pad_query_default (GstPad * pad, GstQuery * query)
3841 switch (GST_QUERY_TYPE (query)) {
3842 case GST_QUERY_POSITION:
3843 case GST_QUERY_SEEKING:
3844 case GST_QUERY_FORMATS:
3845 case GST_QUERY_LATENCY:
3846 case GST_QUERY_JITTER:
3847 case GST_QUERY_RATE:
3848 case GST_QUERY_CONVERT:
3850 return gst_pad_dispatcher
3851 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3855 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
3856 /* FIXME: why isn't this on a GstElement ? */
3858 * gst_pad_load_and_link:
3859 * @self: an #xmlNodePtr to read the description from.
3860 * @parent: the #GstObject element that owns the pad.
3862 * Reads the pad definition from the XML node and links the given pad
3863 * in the element to a pad of an element up in the hierarchy.
3866 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3868 xmlNodePtr field = self->xmlChildrenNode;
3869 GstPad *pad = NULL, *targetpad;
3870 GstPadTemplate *tmpl;
3874 GstObject *grandparent;
3878 if (!strcmp ((char *) field->name, "name")) {
3879 name = (gchar *) xmlNodeGetContent (field);
3880 pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3881 if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3882 && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3883 pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3885 } else if (!strcmp ((char *) field->name, "peer")) {
3886 peer = (gchar *) xmlNodeGetContent (field);
3888 field = field->next;
3890 g_return_if_fail (pad != NULL);
3895 split = g_strsplit (peer, ".", 2);
3897 if (split[0] == NULL || split[1] == NULL) {
3898 GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3899 "Could not parse peer '%s', leaving unlinked", peer);
3906 g_return_if_fail (split[0] != NULL);
3907 g_return_if_fail (split[1] != NULL);
3909 grandparent = gst_object_get_parent (parent);
3911 if (grandparent && GST_IS_BIN (grandparent)) {
3912 target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3919 targetpad = gst_element_get_static_pad (target, split[1]);
3921 targetpad = gst_element_get_request_pad (target, split[1]);
3923 if (targetpad == NULL)
3926 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3927 gst_pad_link (pad, targetpad);
3929 gst_pad_link (targetpad, pad);
3936 * gst_pad_save_thyself:
3937 * @pad: a #GstPad to save.
3938 * @parent: the parent #xmlNodePtr to save the description in.
3940 * Saves the pad into an xml representation.
3942 * Returns: the #xmlNodePtr representation of the pad.
3945 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3950 g_return_val_if_fail (GST_IS_PAD (object), NULL);
3952 pad = GST_PAD_CAST (object);
3954 xmlNewChild (parent, NULL, (xmlChar *) "name",
3955 (xmlChar *) GST_PAD_NAME (pad));
3957 if (GST_PAD_IS_SRC (pad)) {
3958 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3959 } else if (GST_PAD_IS_SINK (pad)) {
3960 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3962 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3965 if (GST_PAD_PEER (pad) != NULL) {
3968 peer = GST_PAD_PEER (pad);
3969 /* first check to see if the peer's parent's parent is the same */
3970 /* we just save it off */
3971 content = g_strdup_printf ("%s.%s",
3972 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3973 xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3976 xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3983 * gst_ghost_pad_save_thyself:
3984 * @pad: a ghost #GstPad to save.
3985 * @parent: the parent #xmlNodePtr to save the description in.
3987 * Saves the ghost pad into an xml representation.
3989 * Returns: the #xmlNodePtr representation of the pad.
3992 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3996 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3998 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3999 xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
4000 xmlNewChild (self, NULL, (xmlChar *) "parent",
4001 (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
4003 /* FIXME FIXME FIXME! */
4008 #endif /* GST_DISABLE_LOADSAVE */
4011 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
4012 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
4015 * This function performs the pad blocking when an event, buffer push
4016 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
4017 * streaming thread after informing the pad has been blocked.
4019 * An application can with this method wait and block any streaming
4020 * thread and perform operations such as seeking or linking.
4022 * Two methods are available for notifying the application of the
4024 * - the callback method, which happens in the STREAMING thread with
4025 * the STREAM_LOCK held. With this method, the most useful way of
4026 * dealing with the callback is to post a message to the main thread
4027 * where the pad block can then be handled outside of the streaming
4028 * thread. With the last method one can perform all operations such
4029 * as doing a state change, linking, unblocking, seeking etc on the
4031 * - the GCond signal method, which makes any thread unblock when
4032 * the pad block happens.
4034 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
4035 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
4039 static GstFlowReturn
4040 handle_pad_block (GstPad * pad)
4042 GstPadBlockCallback callback;
4044 GstFlowReturn ret = GST_FLOW_OK;
4046 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
4048 /* flushing, don't bother trying to block and return WRONG_STATE
4050 if (GST_PAD_IS_FLUSHING (pad))
4051 goto flushingnonref;
4053 /* we grab an extra ref for the callbacks, this is probably not
4054 * needed (callback code does not have a ref and cannot unref). I
4055 * think this was done to make it possible to unref the element in
4056 * the callback, which is in the end totally impossible as it
4057 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
4058 * all taken when calling this function. */
4059 gst_object_ref (pad);
4061 while (GST_PAD_IS_BLOCKED (pad)) {
4063 /* we either have a callback installed to notify the block or
4064 * some other thread is doing a GCond wait. */
4065 callback = pad->block_callback;
4066 pad->abidata.ABI.block_callback_called = TRUE;
4068 /* there is a callback installed, call it. We release the
4069 * lock so that the callback can do something useful with the
4071 user_data = pad->block_data;
4072 GST_OBJECT_UNLOCK (pad);
4073 callback (pad, TRUE, user_data);
4074 GST_OBJECT_LOCK (pad);
4076 /* we released the lock, recheck flushing */
4077 if (GST_PAD_IS_FLUSHING (pad))
4080 /* no callback, signal the thread that is doing a GCond wait
4082 GST_PAD_BLOCK_BROADCAST (pad);
4084 } while (pad->abidata.ABI.block_callback_called == FALSE
4085 && GST_PAD_IS_BLOCKED (pad));
4087 /* OBJECT_LOCK could have been released when we did the callback, which
4088 * then could have made the pad unblock so we need to check the blocking
4089 * condition again. */
4090 if (!GST_PAD_IS_BLOCKED (pad))
4093 /* now we block the streaming thread. It can be unlocked when we
4094 * deactivate the pad (which will also set the FLUSHING flag) or
4095 * when the pad is unblocked. A flushing event will also unblock
4096 * the pad after setting the FLUSHING flag. */
4097 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4098 "Waiting to be unblocked or set flushing");
4099 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
4100 GST_PAD_BLOCK_WAIT (pad);
4101 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
4103 /* see if we got unblocked by a flush or not */
4104 if (GST_PAD_IS_FLUSHING (pad))
4108 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
4110 /* when we get here, the pad is unblocked again and we perform
4111 * the needed unblock code. */
4112 callback = pad->block_callback;
4114 /* we need to call the callback */
4115 user_data = pad->block_data;
4116 GST_OBJECT_UNLOCK (pad);
4117 callback (pad, FALSE, user_data);
4118 GST_OBJECT_LOCK (pad);
4120 /* we need to signal the thread waiting on the GCond */
4121 GST_PAD_BLOCK_BROADCAST (pad);
4124 gst_object_unref (pad);
4130 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
4131 return GST_FLOW_WRONG_STATE;
4135 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
4136 gst_object_unref (pad);
4137 return GST_FLOW_WRONG_STATE;
4141 /**********************************************************************
4142 * Data passing functions
4146 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
4149 GValue args[2] = { {0}, {0} };
4154 g_value_init (&ret, G_TYPE_BOOLEAN);
4155 g_value_set_boolean (&ret, TRUE);
4156 g_value_init (&args[0], GST_TYPE_PAD);
4157 g_value_set_object (&args[0], pad);
4158 g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4159 gst_value_set_mini_object (&args[1], obj);
4161 if (GST_IS_EVENT (obj))
4162 detail = event_quark;
4164 detail = buffer_quark;
4167 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4168 res = g_value_get_boolean (&ret);
4171 g_value_unset (&ret);
4172 g_value_unset (&args[0]);
4173 g_value_unset (&args[1]);
4179 gst_pad_data_unref (gboolean is_buffer, void *data)
4181 if (G_LIKELY (is_buffer)) {
4182 gst_buffer_unref (data);
4184 gst_buffer_list_unref (data);
4189 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4193 if (G_LIKELY (is_buffer)) {
4194 caps = GST_BUFFER_CAPS (data);
4198 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4199 caps = GST_BUFFER_CAPS (buf);
4206 /* this is the chain function that does not perform the additional argument
4207 * checking for that little extra speed.
4209 static inline GstFlowReturn
4210 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
4211 GstPadPushCache * cache)
4214 gboolean caps_changed;
4216 gboolean emit_signal;
4218 GST_PAD_STREAM_LOCK (pad);
4220 GST_OBJECT_LOCK (pad);
4221 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4224 caps = gst_pad_data_get_caps (is_buffer, data);
4225 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4227 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4228 GST_OBJECT_UNLOCK (pad);
4230 /* see if the signal should be emited, we emit before caps nego as
4231 * we might drop the buffer and do capsnego for nothing. */
4232 if (G_UNLIKELY (emit_signal)) {
4234 if (G_LIKELY (is_buffer)) {
4235 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4238 /* chain all groups in the buffer list one by one to avoid problems with
4239 * buffer probes that push buffers or events */
4244 /* we got a new datatype on the pad, see if it can handle it */
4245 if (G_UNLIKELY (caps_changed)) {
4246 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4247 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4248 goto not_negotiated;
4251 /* NOTE: we read the chainfunc unlocked.
4252 * we cannot hold the lock for the pad so we might send
4253 * the data to the wrong function. This is not really a
4254 * problem since functions are assigned at creation time
4255 * and don't change that often... */
4256 if (G_LIKELY (is_buffer)) {
4257 GstPadChainFunction chainfunc;
4259 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4262 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4263 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4264 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4267 cache->peer = gst_object_ref (pad);
4268 cache->caps = caps ? gst_caps_ref (caps) : NULL;
4271 ret = chainfunc (pad, GST_BUFFER_CAST (data));
4273 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4274 "called chainfunction &%s with buffer %p, returned %s",
4275 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4277 GstPadChainListFunction chainlistfunc;
4279 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4282 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4283 "calling chainlistfunction &%s",
4284 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4286 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4288 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4289 "called chainlistfunction &%s, returned %s",
4290 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4293 GST_PAD_STREAM_UNLOCK (pad);
4299 GstBufferList *list;
4300 GstBufferListIterator *it;
4303 GST_PAD_STREAM_UNLOCK (pad);
4305 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4307 list = GST_BUFFER_LIST_CAST (data);
4308 it = gst_buffer_list_iterate (list);
4310 if (gst_buffer_list_iterator_next_group (it)) {
4312 group = gst_buffer_list_iterator_merge_group (it);
4313 if (group == NULL) {
4314 group = gst_buffer_new ();
4315 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4317 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4319 ret = gst_pad_chain_data_unchecked (pad, TRUE, group, NULL);
4320 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4322 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4323 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new (), NULL);
4326 gst_buffer_list_iterator_free (it);
4327 gst_buffer_list_unref (list);
4335 gst_pad_data_unref (is_buffer, data);
4336 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4337 "pushing, but pad was flushing");
4338 GST_OBJECT_UNLOCK (pad);
4339 GST_PAD_STREAM_UNLOCK (pad);
4340 return GST_FLOW_WRONG_STATE;
4344 gst_pad_data_unref (is_buffer, data);
4345 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4346 GST_PAD_STREAM_UNLOCK (pad);
4351 gst_pad_data_unref (is_buffer, data);
4352 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4353 "pushing data but pad did not accept");
4354 GST_PAD_STREAM_UNLOCK (pad);
4355 return GST_FLOW_NOT_NEGOTIATED;
4359 gst_pad_data_unref (is_buffer, data);
4360 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4361 "pushing, but not chainhandler");
4362 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4363 ("push on pad %s:%s but it has no chainfunction",
4364 GST_DEBUG_PAD_NAME (pad)));
4365 GST_PAD_STREAM_UNLOCK (pad);
4366 return GST_FLOW_NOT_SUPPORTED;
4372 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4373 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4376 * Chain a buffer to @pad.
4378 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4380 * If the caps on @buffer are different from the current caps on @pad, this
4381 * function will call any setcaps function (see gst_pad_set_setcaps_function())
4382 * installed on @pad. If the new caps are not acceptable for @pad, this
4383 * function returns #GST_FLOW_NOT_NEGOTIATED.
4385 * The function proceeds calling the chain function installed on @pad (see
4386 * gst_pad_set_chain_function()) and the return value of that function is
4387 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4390 * In all cases, success or failure, the caller loses its reference to @buffer
4391 * after calling this function.
4393 * Returns: a #GstFlowReturn from the pad.
4398 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4400 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4401 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4402 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4404 return gst_pad_chain_data_unchecked (pad, TRUE, buffer, NULL);
4408 * gst_pad_chain_list:
4409 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4410 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4413 * Chain a bufferlist to @pad.
4415 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4417 * If the caps on the first buffer of @list are different from the current
4418 * caps on @pad, this function will call any setcaps function
4419 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4420 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4422 * The function proceeds calling the chainlist function installed on @pad (see
4423 * gst_pad_set_chain_list_function()) and the return value of that function is
4424 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4425 * chainlist function.
4427 * In all cases, success or failure, the caller loses its reference to @list
4428 * after calling this function.
4432 * Returns: a #GstFlowReturn from the pad.
4437 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4439 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4440 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4441 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4443 return gst_pad_chain_data_unchecked (pad, FALSE, list, NULL);
4446 static GstFlowReturn
4447 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
4448 GstPadPushCache * cache)
4453 gboolean caps_changed;
4455 GST_OBJECT_LOCK (pad);
4457 /* FIXME: this check can go away; pad_set_blocked could be implemented with
4458 * probes completely or probes with an extended pad block. */
4459 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4460 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4463 /* we emit signals on the pad arg, the peer will have a chance to
4464 * emit in the _chain() function */
4465 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4467 /* unlock before emitting */
4468 GST_OBJECT_UNLOCK (pad);
4470 if (G_LIKELY (is_buffer)) {
4471 /* if the signal handler returned FALSE, it means we should just drop the
4473 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4476 /* push all buffers in the list */
4479 GST_OBJECT_LOCK (pad);
4482 /* Before pushing the buffer to the peer pad, ensure that caps
4483 * are set on this pad */
4484 caps = gst_pad_data_get_caps (is_buffer, data);
4485 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4487 /* we got a new datatype from the pad, it had better handle it */
4488 if (G_UNLIKELY (caps_changed)) {
4489 /* unlock before setting */
4490 GST_OBJECT_UNLOCK (pad);
4491 GST_DEBUG_OBJECT (pad,
4492 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4493 GST_PAD_CAPS (pad), caps, caps);
4494 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
4495 goto not_negotiated;
4496 GST_OBJECT_LOCK (pad);
4499 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4502 /* take ref to peer pad before releasing the lock */
4503 gst_object_ref (peer);
4504 GST_OBJECT_UNLOCK (pad);
4506 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, cache);
4508 gst_object_unref (peer);
4514 GstBufferList *list;
4515 GstBufferListIterator *it;
4518 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4520 list = GST_BUFFER_LIST_CAST (data);
4521 it = gst_buffer_list_iterate (list);
4523 if (gst_buffer_list_iterator_next_group (it)) {
4525 group = gst_buffer_list_iterator_merge_group (it);
4526 if (group == NULL) {
4527 group = gst_buffer_new ();
4528 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4530 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4532 ret = gst_pad_push_data (pad, TRUE, group, NULL);
4533 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4535 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4536 ret = gst_pad_push_data (pad, TRUE, gst_buffer_new (), NULL);
4539 gst_buffer_list_iterator_free (it);
4540 gst_buffer_list_unref (list);
4545 /* ERROR recovery here */
4548 gst_pad_data_unref (is_buffer, data);
4549 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4550 GST_OBJECT_UNLOCK (pad);
4555 gst_pad_data_unref (is_buffer, data);
4556 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4561 gst_pad_data_unref (is_buffer, data);
4562 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4563 "pushing, but it was not linked");
4564 GST_OBJECT_UNLOCK (pad);
4565 return GST_FLOW_NOT_LINKED;
4569 gst_pad_data_unref (is_buffer, data);
4570 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4571 "element pushed data then refused to accept the caps");
4572 return GST_FLOW_NOT_NEGOTIATED;
4576 static inline GstPadPushCache *
4577 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
4579 GstPadPushCache *cache;
4581 /* try to get the cached data */
4583 cache = g_atomic_pointer_get (cache_ptr);
4584 /* now try to replace the pointer with NULL to mark that we are busy
4586 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
4588 /* we could have a leftover invalid entry */
4589 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
4596 pad_free_cache (GstPadPushCache * cache)
4598 gst_object_unref (cache->peer);
4600 gst_caps_unref (cache->caps);
4601 g_slice_free (GstPadPushCache, cache);
4605 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
4608 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
4609 /* something changed, clean up our cache */
4610 pad_free_cache (cache);
4614 /* must be called with the pad lock */
4616 _priv_gst_pad_invalidate_cache (GstPad * pad)
4618 GstPadPushCache *cache;
4619 gpointer *cache_ptr;
4621 GST_LOG_OBJECT (pad, "Invalidating pad cache");
4623 /* we hold the pad lock here so we can get the peer and it stays
4624 * alive during this call */
4625 if (GST_PAD_IS_SINK (pad)) {
4626 if (!(pad = GST_PAD_PEER (pad)))
4630 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4632 /* try to get the cached data */
4634 cache = g_atomic_pointer_get (cache_ptr);
4635 /* now try to replace the pointer with INVALID. If nothing is busy with this
4636 * caps, we get the cache and clean it up. If something is busy, we replace
4637 * with INVALID so that when the function finishes and tries to put the
4638 * cache back, it'll fail and cleanup */
4639 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
4640 PAD_CACHE_INVALID));
4642 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
4643 pad_free_cache (cache);
4648 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4649 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4652 * Pushes a buffer to the peer of @pad.
4654 * This function will call an installed pad block before triggering any
4655 * installed pad probes.
4657 * If the caps on @buffer are different from the currently configured caps on
4658 * @pad, this function will call any installed setcaps function on @pad (see
4659 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4660 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4662 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4663 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4666 * In all cases, success or failure, the caller loses its reference to @buffer
4667 * after calling this function.
4669 * Returns: a #GstFlowReturn from the peer pad.
4674 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4676 GstPadPushCache *cache;
4678 gpointer *cache_ptr;
4682 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4683 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4684 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4686 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4688 cache = pad_take_cache (pad, cache_ptr);
4690 if (G_UNLIKELY (cache == NULL))
4694 caps = GST_BUFFER_CAPS (buffer);
4695 if (G_UNLIKELY (caps && caps != cache->caps)) {
4696 pad_free_cache (cache);
4702 GST_PAD_STREAM_LOCK (peer);
4703 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4706 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4707 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4708 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer);
4710 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
4712 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4713 "called chainfunction &%s with buffer %p, returned %s",
4714 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer,
4715 gst_flow_get_name (ret));
4717 GST_PAD_STREAM_UNLOCK (peer);
4719 pad_put_cache (pad, cache, cache_ptr);
4726 GstPadPushCache scache = { NULL, };
4728 GST_LOG_OBJECT (pad, "Taking slow path");
4730 ret = gst_pad_push_data (pad, TRUE, buffer, &scache);
4733 GstPadPushCache *ncache;
4735 GST_LOG_OBJECT (pad, "Caching push data");
4737 /* make cache structure */
4738 ncache = g_slice_new (GstPadPushCache);
4741 pad_put_cache (pad, ncache, cache_ptr);
4747 GST_PAD_STREAM_UNLOCK (peer);
4748 pad_free_cache (cache);
4754 * gst_pad_push_list:
4755 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4756 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4759 * Pushes a buffer list to the peer of @pad.
4761 * This function will call an installed pad block before triggering any
4762 * installed pad probes.
4764 * If the caps on the first buffer in the first group of @list are different
4765 * from the currently configured caps on @pad, this function will call any
4766 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4767 * case of failure to renegotiate the new format, this function returns
4768 * #GST_FLOW_NOT_NEGOTIATED.
4770 * If there are any probes installed on @pad every group of the buffer list
4771 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4772 * buffer list will be unreffed.
4774 * The function proceeds calling the chain function on the peer pad and returns
4775 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4776 * be returned. If the peer pad does not have any installed chainlist function
4777 * every group buffer of the list will be merged into a normal #GstBuffer and
4778 * chained via gst_pad_chain().
4780 * In all cases, success or failure, the caller loses its reference to @list
4781 * after calling this function.
4783 * Returns: a #GstFlowReturn from the peer pad.
4790 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4793 GstPadPushCache *cache;
4795 gpointer *cache_ptr;
4799 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4800 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4801 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4803 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4805 cache = pad_take_cache (pad, cache_ptr);
4807 if (G_UNLIKELY (cache == NULL))
4811 if ((buf = gst_buffer_list_get (list, 0, 0)))
4812 caps = GST_BUFFER_CAPS (buf);
4816 if (G_UNLIKELY (caps && caps != cache->caps)) {
4817 pad_free_cache (cache);
4823 GST_PAD_STREAM_LOCK (peer);
4824 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4827 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
4829 GST_PAD_STREAM_UNLOCK (peer);
4831 pad_put_cache (pad, cache, cache_ptr);
4838 GstPadPushCache scache = { NULL, };
4840 GST_LOG_OBJECT (pad, "Taking slow path");
4842 ret = gst_pad_push_data (pad, FALSE, list, &scache);
4845 GstPadPushCache *ncache;
4847 GST_LOG_OBJECT (pad, "Caching push data");
4849 /* make cache structure */
4850 ncache = g_slice_new (GstPadPushCache);
4853 pad_put_cache (pad, ncache, cache_ptr);
4859 GST_PAD_STREAM_UNLOCK (peer);
4860 pad_free_cache (cache);
4866 * gst_pad_check_pull_range:
4867 * @pad: a sink #GstPad.
4869 * Checks if a gst_pad_pull_range() can be performed on the peer
4870 * source pad. This function is used by plugins that want to check
4871 * if they can use random access on the peer source pad.
4873 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4874 * if it needs to perform some logic to determine if pull_range is
4877 * Returns: a gboolean with the result.
4882 gst_pad_check_pull_range (GstPad * pad)
4886 GstPadCheckGetRangeFunction checkgetrangefunc;
4888 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4890 GST_OBJECT_LOCK (pad);
4891 if (!GST_PAD_IS_SINK (pad))
4892 goto wrong_direction;
4894 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4897 gst_object_ref (peer);
4898 GST_OBJECT_UNLOCK (pad);
4900 /* see note in above function */
4901 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4902 /* FIXME, kindoff ghetto */
4903 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4904 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4905 "no checkgetrangefunc, assuming %d", ret);
4907 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4908 "calling checkgetrangefunc %s of peer pad %s:%s",
4909 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4911 ret = checkgetrangefunc (peer);
4914 gst_object_unref (peer);
4918 /* ERROR recovery here */
4921 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4922 "checking pull range, but pad must be a sinkpad");
4923 GST_OBJECT_UNLOCK (pad);
4928 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4929 "checking pull range, but it was not linked");
4930 GST_OBJECT_UNLOCK (pad);
4935 static GstFlowReturn
4936 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4937 GstBuffer ** buffer)
4940 GstPadGetRangeFunction getrangefunc;
4941 gboolean emit_signal;
4943 gboolean caps_changed;
4945 GST_PAD_STREAM_LOCK (pad);
4947 GST_OBJECT_LOCK (pad);
4948 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4951 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4952 GST_OBJECT_UNLOCK (pad);
4954 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4957 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4958 "calling getrangefunc %s, offset %"
4959 G_GUINT64_FORMAT ", size %u",
4960 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4962 ret = getrangefunc (pad, offset, size, buffer);
4964 /* can only fire the signal if we have a valid buffer */
4965 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4966 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4970 GST_PAD_STREAM_UNLOCK (pad);
4972 if (G_UNLIKELY (ret != GST_FLOW_OK))
4973 goto get_range_failed;
4975 GST_OBJECT_LOCK (pad);
4976 /* Before pushing the buffer to the peer pad, ensure that caps
4977 * are set on this pad */
4978 caps = GST_BUFFER_CAPS (*buffer);
4979 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4980 GST_OBJECT_UNLOCK (pad);
4982 if (G_UNLIKELY (caps_changed)) {
4983 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4984 /* this should usually work because the element produced the buffer */
4985 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4986 goto not_negotiated;
4993 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4994 "pulling range, but pad was flushing");
4995 GST_OBJECT_UNLOCK (pad);
4996 GST_PAD_STREAM_UNLOCK (pad);
4997 return GST_FLOW_WRONG_STATE;
5001 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
5002 ("pullrange on pad %s:%s but it has no getrangefunction",
5003 GST_DEBUG_PAD_NAME (pad)));
5004 GST_PAD_STREAM_UNLOCK (pad);
5005 return GST_FLOW_NOT_SUPPORTED;
5009 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5010 "Dropping data after FALSE probe return");
5011 GST_PAD_STREAM_UNLOCK (pad);
5012 gst_buffer_unref (*buffer);
5014 return GST_FLOW_UNEXPECTED;
5019 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5020 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5021 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
5026 gst_buffer_unref (*buffer);
5028 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
5029 "getrange returned buffer of unaccaptable caps");
5030 return GST_FLOW_NOT_NEGOTIATED;
5035 * gst_pad_get_range:
5036 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
5037 * @offset: The start offset of the buffer
5038 * @size: The length of the buffer
5039 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
5040 * returns #GST_FLOW_ERROR if %NULL.
5042 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
5043 * immediately and @buffer is %NULL.
5045 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
5046 * description of a getrange function. If @pad has no getrange function
5047 * installed (see gst_pad_set_getrange_function()) this function returns
5048 * #GST_FLOW_NOT_SUPPORTED.
5050 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
5052 * Returns: a #GstFlowReturn from the pad.
5057 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
5058 GstBuffer ** buffer)
5060 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5061 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
5062 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5064 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
5068 * gst_pad_pull_range:
5069 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
5070 * @offset: The start offset of the buffer
5071 * @size: The length of the buffer
5072 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
5073 * GST_FLOW_ERROR if %NULL.
5075 * Pulls a @buffer from the peer pad.
5077 * This function will first trigger the pad block signal if it was
5080 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
5081 * function returns the result of gst_pad_get_range() on the peer pad.
5082 * See gst_pad_get_range() for a list of return values and for the
5083 * semantics of the arguments of this function.
5085 * @buffer's caps must either be unset or the same as what is already
5086 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
5089 * Returns: a #GstFlowReturn from the peer pad.
5090 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
5091 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
5092 * @buffer may not be used or freed when any other return value than
5093 * #GST_FLOW_OK is returned.
5098 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
5099 GstBuffer ** buffer)
5103 gboolean emit_signal;
5105 gboolean caps_changed;
5107 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5108 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
5109 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5111 GST_OBJECT_LOCK (pad);
5113 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
5114 handle_pad_block (pad);
5116 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
5119 /* signal emision for the pad, peer has chance to emit when
5120 * we call _get_range() */
5121 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
5123 gst_object_ref (peer);
5124 GST_OBJECT_UNLOCK (pad);
5126 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
5128 gst_object_unref (peer);
5130 if (G_UNLIKELY (ret != GST_FLOW_OK))
5131 goto pull_range_failed;
5133 /* can only fire the signal if we have a valid buffer */
5134 if (G_UNLIKELY (emit_signal)) {
5135 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
5139 GST_OBJECT_LOCK (pad);
5140 /* Before pushing the buffer to the peer pad, ensure that caps
5141 * are set on this pad */
5142 caps = GST_BUFFER_CAPS (*buffer);
5143 caps_changed = caps && caps != GST_PAD_CAPS (pad);
5144 GST_OBJECT_UNLOCK (pad);
5146 /* we got a new datatype on the pad, see if it can handle it */
5147 if (G_UNLIKELY (caps_changed)) {
5148 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
5149 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
5150 goto not_negotiated;
5154 /* ERROR recovery here */
5157 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5158 "pulling range, but it was not linked");
5159 GST_OBJECT_UNLOCK (pad);
5160 return GST_FLOW_NOT_LINKED;
5165 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5166 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5167 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
5172 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5173 "Dropping data after FALSE probe return");
5174 gst_buffer_unref (*buffer);
5176 return GST_FLOW_UNEXPECTED;
5180 gst_buffer_unref (*buffer);
5182 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
5183 "pullrange returned buffer of different caps");
5184 return GST_FLOW_NOT_NEGOTIATED;
5189 * gst_pad_push_event:
5190 * @pad: a #GstPad to push the event to.
5191 * @event: (transfer full): the #GstEvent to send to the pad.
5193 * Sends the event to the peer of the given pad. This function is
5194 * mainly used by elements to send events to their peer
5197 * This function takes owership of the provided event so you should
5198 * gst_event_ref() it if you want to reuse the event after this call.
5200 * Returns: TRUE if the event was handled.
5205 gst_pad_push_event (GstPad * pad, GstEvent * event)
5210 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5211 g_return_val_if_fail (event != NULL, FALSE);
5212 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5214 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
5216 GST_OBJECT_LOCK (pad);
5218 /* Two checks to be made:
5219 * . (un)set the FLUSHING flag for flushing events,
5220 * . handle pad blocking */
5221 switch (GST_EVENT_TYPE (event)) {
5222 case GST_EVENT_FLUSH_START:
5223 _priv_gst_pad_invalidate_cache (pad);
5224 GST_PAD_SET_FLUSHING (pad);
5226 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5227 /* flush start will have set the FLUSHING flag and will then
5228 * unlock all threads doing a GCond wait on the blocking pad. This
5229 * will typically unblock the STREAMING thread blocked on a pad. */
5230 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
5231 "doing block signal.");
5232 GST_PAD_BLOCK_BROADCAST (pad);
5236 case GST_EVENT_FLUSH_STOP:
5237 GST_PAD_UNSET_FLUSHING (pad);
5239 /* if we are blocked, flush away the FLUSH_STOP event */
5240 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5241 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
5246 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5247 /* block the event as long as the pad is blocked */
5248 if (handle_pad_block (pad) != GST_FLOW_OK)
5254 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5255 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5256 GST_EVENT_SRC (event) = gst_object_ref (pad);
5259 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5260 GST_OBJECT_UNLOCK (pad);
5262 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
5265 GST_OBJECT_LOCK (pad);
5267 peerpad = GST_PAD_PEER (pad);
5268 if (peerpad == NULL)
5271 GST_LOG_OBJECT (pad,
5272 "sending event %s (%" GST_PTR_FORMAT ") to peerpad %" GST_PTR_FORMAT,
5273 GST_EVENT_TYPE_NAME (event), event, peerpad);
5274 gst_object_ref (peerpad);
5275 GST_OBJECT_UNLOCK (pad);
5277 result = gst_pad_send_event (peerpad, event);
5279 /* Note: we gave away ownership of the event at this point */
5280 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
5282 gst_object_unref (peerpad);
5286 /* ERROR handling */
5289 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5290 gst_event_unref (event);
5295 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
5296 gst_event_unref (event);
5297 GST_OBJECT_UNLOCK (pad);
5302 GST_DEBUG_OBJECT (pad,
5303 "Not forwarding event since we're flushing and blocking");
5304 gst_event_unref (event);
5305 GST_OBJECT_UNLOCK (pad);
5311 * gst_pad_send_event:
5312 * @pad: a #GstPad to send the event to.
5313 * @event: (transfer full): the #GstEvent to send to the pad.
5315 * Sends the event to the pad. This function can be used
5316 * by applications to send events in the pipeline.
5318 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5319 * sink pad, @event should be a downstream event. For example, you would not
5320 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5321 * Furthermore, some downstream events have to be serialized with data flow,
5322 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5323 * the event needs to be serialized with data flow, this function will take the
5324 * pad's stream lock while calling its event function.
5326 * To find out whether an event type is upstream, downstream, or downstream and
5327 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5328 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5329 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5330 * plugin doesn't need to bother itself with this information; the core handles
5331 * all necessary locks and checks.
5333 * This function takes owership of the provided event so you should
5334 * gst_event_ref() it if you want to reuse the event after this call.
5336 * Returns: TRUE if the event was handled.
5339 gst_pad_send_event (GstPad * pad, GstEvent * event)
5341 gboolean result = FALSE;
5342 GstPadEventFunction eventfunc;
5343 gboolean serialized, need_unlock = FALSE;
5345 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5346 g_return_val_if_fail (event != NULL, FALSE);
5348 GST_OBJECT_LOCK (pad);
5349 if (GST_PAD_IS_SINK (pad)) {
5350 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5351 goto wrong_direction;
5352 serialized = GST_EVENT_IS_SERIALIZED (event);
5353 } else if (GST_PAD_IS_SRC (pad)) {
5354 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5355 goto wrong_direction;
5356 /* events on srcpad never are serialized */
5359 goto unknown_direction;
5361 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5362 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5363 GST_EVENT_SRC (event) = gst_object_ref (pad);
5367 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5368 GST_OBJECT_UNLOCK (pad);
5370 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
5373 GST_OBJECT_LOCK (pad);
5376 switch (GST_EVENT_TYPE (event)) {
5377 case GST_EVENT_FLUSH_START:
5378 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5379 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5381 /* can't even accept a flush begin event when flushing */
5382 if (GST_PAD_IS_FLUSHING (pad))
5385 _priv_gst_pad_invalidate_cache (pad);
5386 GST_PAD_SET_FLUSHING (pad);
5387 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5389 case GST_EVENT_FLUSH_STOP:
5390 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
5391 GST_PAD_UNSET_FLUSHING (pad);
5392 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5394 GST_OBJECT_UNLOCK (pad);
5395 /* grab stream lock */
5396 GST_PAD_STREAM_LOCK (pad);
5398 GST_OBJECT_LOCK (pad);
5401 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5402 GST_EVENT_TYPE_NAME (event));
5404 /* make this a little faster, no point in grabbing the lock
5405 * if the pad is already flushing. */
5406 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5410 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5411 GST_OBJECT_UNLOCK (pad);
5412 GST_PAD_STREAM_LOCK (pad);
5414 GST_OBJECT_LOCK (pad);
5415 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5420 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5423 GST_OBJECT_UNLOCK (pad);
5425 result = eventfunc (pad, event);
5428 GST_PAD_STREAM_UNLOCK (pad);
5430 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5434 /* ERROR handling */
5437 g_warning ("pad %s:%s sending %s event in wrong direction",
5438 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5439 GST_OBJECT_UNLOCK (pad);
5440 gst_event_unref (event);
5445 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5446 GST_OBJECT_UNLOCK (pad);
5447 gst_event_unref (event);
5452 g_warning ("pad %s:%s has no event handler, file a bug.",
5453 GST_DEBUG_PAD_NAME (pad));
5454 GST_OBJECT_UNLOCK (pad);
5456 GST_PAD_STREAM_UNLOCK (pad);
5457 gst_event_unref (event);
5462 GST_OBJECT_UNLOCK (pad);
5464 GST_PAD_STREAM_UNLOCK (pad);
5465 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5466 "Received event on flushing pad. Discarding");
5467 gst_event_unref (event);
5472 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5473 gst_event_unref (event);
5479 * gst_pad_set_element_private:
5480 * @pad: the #GstPad to set the private data of.
5481 * @priv: The private data to attach to the pad.
5483 * Set the given private data gpointer on the pad.
5484 * This function can only be used by the element that owns the pad.
5485 * No locking is performed in this function.
5488 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5490 pad->element_private = priv;
5494 * gst_pad_get_element_private:
5495 * @pad: the #GstPad to get the private data of.
5497 * Gets the private data of a pad.
5498 * No locking is performed in this function.
5500 * Returns: (transfer none): a #gpointer to the private data.
5503 gst_pad_get_element_private (GstPad * pad)
5505 return pad->element_private;
5509 do_stream_status (GstPad * pad, GstStreamStatusType type,
5510 GThread * thread, GstTask * task)
5514 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5516 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5517 if (GST_IS_ELEMENT (parent)) {
5518 GstMessage *message;
5519 GValue value = { 0 };
5521 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5522 gchar *tname, *ename, *pname;
5524 /* create a good task name */
5525 ename = gst_element_get_name (parent);
5526 pname = gst_pad_get_name (pad);
5527 tname = g_strdup_printf ("%s:%s", ename, pname);
5531 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5535 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5538 g_value_init (&value, GST_TYPE_TASK);
5539 g_value_set_object (&value, task);
5540 gst_message_set_stream_status_object (message, &value);
5541 g_value_unset (&value);
5543 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5544 gst_element_post_message (parent, message);
5546 gst_object_unref (parent);
5551 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5553 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5558 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5560 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5564 static GstTaskThreadCallbacks thr_callbacks = {
5570 * gst_pad_start_task:
5571 * @pad: the #GstPad to start the task of
5572 * @func: the task function to call
5573 * @data: data passed to the task function
5575 * Starts a task that repeatedly calls @func with @data. This function
5576 * is mostly used in pad activation functions to start the dataflow.
5577 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5578 * before @func is called.
5580 * Returns: a %TRUE if the task could be started.
5583 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5588 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5589 g_return_val_if_fail (func != NULL, FALSE);
5591 GST_DEBUG_OBJECT (pad, "start task");
5593 GST_OBJECT_LOCK (pad);
5594 task = GST_PAD_TASK (pad);
5596 task = gst_task_create (func, data);
5597 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5598 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5599 GST_DEBUG_OBJECT (pad, "created task");
5600 GST_PAD_TASK (pad) = task;
5601 gst_object_ref (task);
5602 /* release lock to post the message */
5603 GST_OBJECT_UNLOCK (pad);
5605 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5607 gst_object_unref (task);
5609 GST_OBJECT_LOCK (pad);
5610 /* nobody else is supposed to have changed the pad now */
5611 if (GST_PAD_TASK (pad) != task)
5612 goto concurrent_stop;
5614 res = gst_task_set_state (task, GST_TASK_STARTED);
5615 GST_OBJECT_UNLOCK (pad);
5622 GST_OBJECT_UNLOCK (pad);
5628 * gst_pad_pause_task:
5629 * @pad: the #GstPad to pause the task of
5631 * Pause the task of @pad. This function will also wait until the
5632 * function executed by the task is finished if this function is not
5633 * called from the task function.
5635 * Returns: a TRUE if the task could be paused or FALSE when the pad
5639 gst_pad_pause_task (GstPad * pad)
5644 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5646 GST_DEBUG_OBJECT (pad, "pause task");
5648 GST_OBJECT_LOCK (pad);
5649 task = GST_PAD_TASK (pad);
5652 res = gst_task_set_state (task, GST_TASK_PAUSED);
5653 GST_OBJECT_UNLOCK (pad);
5655 /* wait for task function to finish, this lock is recursive so it does nothing
5656 * when the pause is called from the task itself */
5657 GST_PAD_STREAM_LOCK (pad);
5658 GST_PAD_STREAM_UNLOCK (pad);
5664 GST_DEBUG_OBJECT (pad, "pad has no task");
5665 GST_OBJECT_UNLOCK (pad);
5671 * gst_pad_stop_task:
5672 * @pad: the #GstPad to stop the task of
5674 * Stop the task of @pad. This function will also make sure that the
5675 * function executed by the task will effectively stop if not called
5676 * from the GstTaskFunction.
5678 * This function will deadlock if called from the GstTaskFunction of
5679 * the task. Use gst_task_pause() instead.
5681 * Regardless of whether the pad has a task, the stream lock is acquired and
5682 * released so as to ensure that streaming through this pad has finished.
5684 * Returns: a TRUE if the task could be stopped or FALSE on error.
5687 gst_pad_stop_task (GstPad * pad)
5692 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5694 GST_DEBUG_OBJECT (pad, "stop task");
5696 GST_OBJECT_LOCK (pad);
5697 task = GST_PAD_TASK (pad);
5700 GST_PAD_TASK (pad) = NULL;
5701 res = gst_task_set_state (task, GST_TASK_STOPPED);
5702 GST_OBJECT_UNLOCK (pad);
5704 GST_PAD_STREAM_LOCK (pad);
5705 GST_PAD_STREAM_UNLOCK (pad);
5707 if (!gst_task_join (task))
5710 gst_object_unref (task);
5716 GST_DEBUG_OBJECT (pad, "no task");
5717 GST_OBJECT_UNLOCK (pad);
5719 GST_PAD_STREAM_LOCK (pad);
5720 GST_PAD_STREAM_UNLOCK (pad);
5722 /* this is not an error */
5727 /* this is bad, possibly the application tried to join the task from
5728 * the task's thread. We install the task again so that it will be stopped
5729 * again from the right thread next time hopefully. */
5730 GST_OBJECT_LOCK (pad);
5731 GST_DEBUG_OBJECT (pad, "join failed");
5732 /* we can only install this task if there was no other task */
5733 if (GST_PAD_TASK (pad) == NULL)
5734 GST_PAD_TASK (pad) = task;
5735 GST_OBJECT_UNLOCK (pad);