2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
30 * After two pads are retrieved from an element with gst_element_get_pad(),
31 * the pads can be link with gst_pad_link(). (For quick links,
32 * you can also use gst_element_link(), which will make the obvious
33 * link for you if it's straightforward.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
38 * Pads have #GstCaps attached to it to describe the media type they are
39 * capable of dealing with. gst_pad_get_caps() and gst_pad_set_caps() are
40 * used to manipulate the caps of the pads.
41 * Pads created from a pad template cannot set capabilities that are
42 * incompatible with the pad template capabilities.
44 * Pads without pad templates can be created with gst_pad_new(),
45 * which takes a direction and a name as an argument. If the name is NULL,
46 * then a guaranteed unique name will be assigned to it.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
73 #include "glib-compat-private.h"
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
78 /* Pad signals and args */
98 typedef struct _GstPadPushCache GstPadPushCache;
100 struct _GstPadPushCache
102 GstPad *peer; /* reffed peer pad */
103 GstCaps *caps; /* caps for this link */
106 static GstPadPushCache _pad_cache_invalid = { NULL, };
108 #define PAD_CACHE_INVALID (&_pad_cache_invalid)
110 #define GST_PAD_GET_PRIVATE(obj) \
111 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
113 #define GST_PAD_CHAINLISTFUNC(pad) ((pad)->abidata.ABI.priv->chainlistfunc)
115 struct _GstPadPrivate
117 GstPadChainListFunction chainlistfunc;
119 GstPadPushCache *cache_ptr;
122 static void gst_pad_dispose (GObject * object);
123 static void gst_pad_finalize (GObject * object);
124 static void gst_pad_set_property (GObject * object, guint prop_id,
125 const GValue * value, GParamSpec * pspec);
126 static void gst_pad_get_property (GObject * object, guint prop_id,
127 GValue * value, GParamSpec * pspec);
129 static GstFlowReturn handle_pad_block (GstPad * pad);
130 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
131 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
132 static gboolean gst_pad_activate_default (GstPad * pad);
133 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
135 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
136 #ifdef GST_DISABLE_DEPRECATED
137 #include <libxml/parser.h>
139 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
140 void gst_pad_load_and_link (xmlNodePtr self, GstObject * parent);
143 /* Some deprecated stuff that we need inside here for
144 * backwards compatibility */
145 #ifdef GST_DISABLE_DEPRECATED
146 #ifndef GST_REMOVE_DEPRECATED
147 #define GST_PAD_INTLINKFUNC(pad) (GST_PAD_CAST(pad)->intlinkfunc)
148 GList *gst_pad_get_internal_links_default (GstPad * pad);
152 static GstObjectClass *parent_class = NULL;
153 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
155 static GParamSpec *pspec_caps = NULL;
157 /* quarks for probe signals */
158 static GQuark buffer_quark;
159 static GQuark event_quark;
168 static GstFlowQuarks flow_quarks[] = {
169 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
170 {GST_FLOW_RESEND, "resend", 0},
171 {GST_FLOW_OK, "ok", 0},
172 {GST_FLOW_NOT_LINKED, "not-linked", 0},
173 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
174 {GST_FLOW_UNEXPECTED, "unexpected", 0},
175 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
176 {GST_FLOW_ERROR, "error", 0},
177 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
178 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
183 * @ret: a #GstFlowReturn to get the name of.
185 * Gets a string representing the given flow return.
187 * Returns: a static string with the name of the flow return.
190 gst_flow_get_name (GstFlowReturn ret)
194 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
196 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
197 if (ret == flow_quarks[i].ret)
198 return flow_quarks[i].name;
205 * @ret: a #GstFlowReturn to get the quark of.
207 * Get the unique quark for the given GstFlowReturn.
209 * Returns: the quark associated with the flow return or 0 if an
210 * invalid return was specified.
213 gst_flow_to_quark (GstFlowReturn ret)
217 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
219 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
220 if (ret == flow_quarks[i].ret)
221 return flow_quarks[i].quark;
230 buffer_quark = g_quark_from_static_string ("buffer"); \
231 event_quark = g_quark_from_static_string ("event"); \
233 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
234 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
237 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
241 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
244 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
245 GValue * return_accu, const GValue * handler_return, gpointer dummy)
247 gboolean ret = g_value_get_boolean (handler_return);
249 GST_DEBUG ("accumulated %d", ret);
250 g_value_set_boolean (return_accu, ret);
256 default_have_data (GstPad * pad, GstMiniObject * o)
262 gst_pad_class_init (GstPadClass * klass)
264 GObjectClass *gobject_class;
265 GstObjectClass *gstobject_class;
267 gobject_class = G_OBJECT_CLASS (klass);
268 gstobject_class = GST_OBJECT_CLASS (klass);
270 g_type_class_add_private (klass, sizeof (GstPadPrivate));
272 parent_class = g_type_class_peek_parent (klass);
274 gobject_class->dispose = gst_pad_dispose;
275 gobject_class->finalize = gst_pad_finalize;
276 gobject_class->set_property = gst_pad_set_property;
277 gobject_class->get_property = gst_pad_get_property;
281 * @pad: the pad that emitted the signal
282 * @peer: the peer pad that has been connected
284 * Signals that a pad has been linked to the peer pad.
286 gst_pad_signals[PAD_LINKED] =
287 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
288 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
289 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
292 * @pad: the pad that emitted the signal
293 * @peer: the peer pad that has been disconnected
295 * Signals that a pad has been unlinked from the peer pad.
297 gst_pad_signals[PAD_UNLINKED] =
298 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
299 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
300 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
302 * GstPad::request-link:
303 * @pad: the pad that emitted the signal
304 * @peer: the peer pad for which a connection is requested
306 * Signals that a pad connection has been requested.
308 gst_pad_signals[PAD_REQUEST_LINK] =
309 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
310 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
311 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
315 * @pad: the pad that emitted the signal
316 * @mini_obj: new data
318 * Signals that new data is available on the pad. This signal is used
319 * internally for implementing pad probes.
320 * See gst_pad_add_*_probe functions.
322 * Returns: %TRUE to keep the data, %FALSE to drop it
324 gst_pad_signals[PAD_HAVE_DATA] =
325 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
326 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
327 G_STRUCT_OFFSET (GstPadClass, have_data),
328 _gst_do_pass_data_accumulator,
329 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
330 GST_TYPE_MINI_OBJECT);
332 pspec_caps = g_param_spec_boxed ("caps", "Caps",
333 "The capabilities of the pad", GST_TYPE_CAPS,
334 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
335 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
337 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
338 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
339 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
340 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
341 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
342 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
343 g_param_spec_object ("template", "Template",
344 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
348 gstobject_class->save_thyself =
349 ((gpointer (*)(GstObject * object,
350 gpointer self)) * GST_DEBUG_FUNCPTR (gst_pad_save_thyself));
352 gstobject_class->path_string_separator = ".";
354 /* Register common function pointer descriptions */
355 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
356 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
357 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
358 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
359 #ifndef GST_REMOVE_DEPRECATED
360 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_internal_links_default);
362 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
363 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
365 /* from gstutils.c */
366 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_fixed_caps_func);
368 klass->have_data = default_have_data;
372 gst_pad_init (GstPad * pad)
374 pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
376 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
377 GST_PAD_PEER (pad) = NULL;
379 GST_PAD_CHAINFUNC (pad) = NULL;
381 GST_PAD_LINKFUNC (pad) = NULL;
383 GST_PAD_CAPS (pad) = NULL;
384 GST_PAD_GETCAPSFUNC (pad) = NULL;
386 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
387 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
388 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
389 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
390 #ifndef GST_REMOVE_DEPRECATED
391 GST_PAD_INTLINKFUNC (pad) = gst_pad_get_internal_links_default;
393 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
395 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
397 pad->do_buffer_signals = 0;
398 pad->do_event_signals = 0;
400 GST_PAD_SET_FLUSHING (pad);
402 pad->preroll_lock = g_mutex_new ();
403 pad->preroll_cond = g_cond_new ();
405 /* FIXME 0.11: Store this directly in the instance struct */
406 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
407 g_static_rec_mutex_init (pad->stream_rec_lock);
409 pad->block_cond = g_cond_new ();
413 gst_pad_dispose (GObject * object)
415 GstPad *pad = GST_PAD_CAST (object);
418 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
420 /* unlink the peer pad */
421 if ((peer = gst_pad_get_peer (pad))) {
422 /* window for MT unsafeness, someone else could unlink here
423 * and then we call unlink with wrong pads. The unlink
424 * function would catch this and safely return failed. */
425 if (GST_PAD_IS_SRC (pad))
426 gst_pad_unlink (pad, peer);
428 gst_pad_unlink (peer, pad);
430 gst_object_unref (peer);
434 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
436 gst_pad_set_pad_template (pad, NULL);
438 if (pad->block_destroy_data && pad->block_data) {
439 pad->block_destroy_data (pad->block_data);
440 pad->block_data = NULL;
443 G_OBJECT_CLASS (parent_class)->dispose (object);
447 gst_pad_finalize (GObject * object)
449 GstPad *pad = GST_PAD_CAST (object);
452 /* in case the task is still around, clean it up */
453 if ((task = GST_PAD_TASK (pad))) {
454 gst_task_join (task);
455 GST_PAD_TASK (pad) = NULL;
456 gst_object_unref (task);
459 if (pad->stream_rec_lock) {
460 g_static_rec_mutex_free (pad->stream_rec_lock);
461 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
462 pad->stream_rec_lock = NULL;
464 if (pad->preroll_lock) {
465 g_mutex_free (pad->preroll_lock);
466 g_cond_free (pad->preroll_cond);
467 pad->preroll_lock = NULL;
468 pad->preroll_cond = NULL;
470 if (pad->block_cond) {
471 g_cond_free (pad->block_cond);
472 pad->block_cond = NULL;
475 G_OBJECT_CLASS (parent_class)->finalize (object);
479 gst_pad_set_property (GObject * object, guint prop_id,
480 const GValue * value, GParamSpec * pspec)
482 g_return_if_fail (GST_IS_PAD (object));
485 case PAD_PROP_DIRECTION:
486 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
488 case PAD_PROP_TEMPLATE:
489 gst_pad_set_pad_template (GST_PAD_CAST (object),
490 (GstPadTemplate *) g_value_get_object (value));
493 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
499 gst_pad_get_property (GObject * object, guint prop_id,
500 GValue * value, GParamSpec * pspec)
502 g_return_if_fail (GST_IS_PAD (object));
506 GST_OBJECT_LOCK (object);
507 g_value_set_boxed (value, GST_PAD_CAPS (object));
508 GST_OBJECT_UNLOCK (object);
510 case PAD_PROP_DIRECTION:
511 g_value_set_enum (value, GST_PAD_DIRECTION (object));
513 case PAD_PROP_TEMPLATE:
514 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524 * @name: the name of the new pad.
525 * @direction: the #GstPadDirection of the pad.
527 * Creates a new pad with the given name in the given direction.
528 * If name is NULL, a guaranteed unique name (across all pads)
530 * This function makes a copy of the name so you can safely free the name.
532 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
537 gst_pad_new (const gchar * name, GstPadDirection direction)
539 return g_object_new (GST_TYPE_PAD,
540 "name", name, "direction", direction, NULL);
544 * gst_pad_new_from_template:
545 * @templ: the pad template to use
546 * @name: the name of the element
548 * Creates a new pad with the given name from the given template.
549 * If name is NULL, a guaranteed unique name (across all pads)
551 * This function makes a copy of the name so you can safely free the name.
553 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
556 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
558 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
560 return g_object_new (GST_TYPE_PAD,
561 "name", name, "direction", templ->direction, "template", templ, NULL);
565 * gst_pad_new_from_static_template:
566 * @templ: the #GstStaticPadTemplate to use
567 * @name: the name of the element
569 * Creates a new pad with the given name from the given static template.
570 * If name is NULL, a guaranteed unique name (across all pads)
572 * This function makes a copy of the name so you can safely free the name.
574 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
577 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
581 GstPadTemplate *template;
583 template = gst_static_pad_template_get (templ);
584 pad = gst_pad_new_from_template (template, name);
585 gst_object_unref (template);
590 * gst_pad_get_direction:
591 * @pad: a #GstPad to get the direction of.
593 * Gets the direction of the pad. The direction of the pad is
594 * decided at construction time so this function does not take
597 * Returns: the #GstPadDirection of the pad.
602 gst_pad_get_direction (GstPad * pad)
604 GstPadDirection result;
606 /* PAD_UNKNOWN is a little silly but we need some sort of
607 * error return value */
608 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
610 result = GST_PAD_DIRECTION (pad);
616 gst_pad_activate_default (GstPad * pad)
618 return gst_pad_activate_push (pad, TRUE);
622 pre_activate (GstPad * pad, GstActivateMode new_mode)
625 case GST_ACTIVATE_PUSH:
626 case GST_ACTIVATE_PULL:
627 GST_OBJECT_LOCK (pad);
628 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
630 GST_PAD_UNSET_FLUSHING (pad);
631 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
632 GST_OBJECT_UNLOCK (pad);
634 case GST_ACTIVATE_NONE:
635 GST_OBJECT_LOCK (pad);
636 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
637 _priv_gst_pad_invalidate_cache (pad);
638 GST_PAD_SET_FLUSHING (pad);
639 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
640 /* unlock blocked pads so element can resume and stop */
641 GST_PAD_BLOCK_BROADCAST (pad);
642 GST_OBJECT_UNLOCK (pad);
648 post_activate (GstPad * pad, GstActivateMode new_mode)
651 case GST_ACTIVATE_PUSH:
652 case GST_ACTIVATE_PULL:
655 case GST_ACTIVATE_NONE:
656 /* ensures that streaming stops */
657 GST_PAD_STREAM_LOCK (pad);
658 GST_DEBUG_OBJECT (pad, "stopped streaming");
659 GST_PAD_STREAM_UNLOCK (pad);
665 * gst_pad_set_active:
666 * @pad: the #GstPad to activate or deactivate.
667 * @active: whether or not the pad should be active.
669 * Activates or deactivates the given pad.
670 * Normally called from within core state change functions.
672 * If @active, makes sure the pad is active. If it is already active, either in
673 * push or pull mode, just return. Otherwise dispatches to the pad's activate
674 * function to perform the actual activation.
676 * If not @active, checks the pad's current mode and calls
677 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
680 * Returns: #TRUE if the operation was successful.
685 gst_pad_set_active (GstPad * pad, gboolean active)
688 gboolean ret = FALSE;
690 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
692 GST_OBJECT_LOCK (pad);
693 old = GST_PAD_ACTIVATE_MODE (pad);
694 GST_OBJECT_UNLOCK (pad);
698 case GST_ACTIVATE_PUSH:
699 GST_DEBUG_OBJECT (pad, "activating pad from push");
702 case GST_ACTIVATE_PULL:
703 GST_DEBUG_OBJECT (pad, "activating pad from pull");
706 case GST_ACTIVATE_NONE:
707 GST_DEBUG_OBJECT (pad, "activating pad from none");
708 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
713 case GST_ACTIVATE_PUSH:
714 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
715 ret = gst_pad_activate_push (pad, FALSE);
717 case GST_ACTIVATE_PULL:
718 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
719 ret = gst_pad_activate_pull (pad, FALSE);
721 case GST_ACTIVATE_NONE:
722 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
729 GST_OBJECT_LOCK (pad);
731 g_critical ("Failed to deactivate pad %s:%s, very bad",
732 GST_DEBUG_PAD_NAME (pad));
734 GST_WARNING_OBJECT (pad, "Failed to activate pad");
736 GST_OBJECT_UNLOCK (pad);
743 * gst_pad_activate_pull:
744 * @pad: the #GstPad to activate or deactivate.
745 * @active: whether or not the pad should be active.
747 * Activates or deactivates the given pad in pull mode via dispatching to the
748 * pad's activatepullfunc. For use from within pad activation functions only.
749 * When called on sink pads, will first proxy the call to the peer pad, which
750 * is expected to activate its internally linked pads from within its
751 * activate_pull function.
753 * If you don't know what this is, you probably don't want to call it.
755 * Returns: TRUE if the operation was successful.
760 gst_pad_activate_pull (GstPad * pad, gboolean active)
762 GstActivateMode old, new;
765 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
767 GST_OBJECT_LOCK (pad);
768 old = GST_PAD_ACTIVATE_MODE (pad);
769 GST_OBJECT_UNLOCK (pad);
773 case GST_ACTIVATE_PULL:
774 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
776 case GST_ACTIVATE_PUSH:
777 GST_DEBUG_OBJECT (pad,
778 "activating pad from push, deactivate push first");
779 /* pad was activate in the wrong direction, deactivate it
780 * and reactivate it in pull mode */
781 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
782 goto deactivate_failed;
783 /* fallthrough, pad is deactivated now. */
784 case GST_ACTIVATE_NONE:
785 GST_DEBUG_OBJECT (pad, "activating pad from none");
790 case GST_ACTIVATE_NONE:
791 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
793 case GST_ACTIVATE_PUSH:
794 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
795 /* pad was activated in the other direction, deactivate it
796 * in push mode, this should not happen... */
797 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
798 goto deactivate_failed;
799 /* everything is fine now */
801 case GST_ACTIVATE_PULL:
802 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
807 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
808 if ((peer = gst_pad_get_peer (pad))) {
809 GST_DEBUG_OBJECT (pad, "calling peer");
810 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
812 gst_object_unref (peer);
814 /* there is no peer, this is only fatal when we activate. When we
815 * deactivate, we must assume the application has unlinked the peer and
816 * will deactivate it eventually. */
820 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
823 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
824 goto failure; /* Can't activate pull on a src without a
828 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
829 pre_activate (pad, new);
831 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
832 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
835 /* can happen for sinks of passthrough elements */
838 post_activate (pad, new);
840 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
841 active ? "activated" : "deactivated");
847 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
848 active ? "activated" : "deactivated");
853 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
854 "failed to %s in switch to pull from mode %d",
855 (active ? "activate" : "deactivate"), old);
860 GST_OBJECT_LOCK (peer);
861 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
862 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
863 GST_OBJECT_UNLOCK (peer);
864 gst_object_unref (peer);
869 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
875 GST_OBJECT_LOCK (pad);
876 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
877 active ? "activate" : "deactivate");
878 _priv_gst_pad_invalidate_cache (pad);
879 GST_PAD_SET_FLUSHING (pad);
880 GST_PAD_ACTIVATE_MODE (pad) = old;
881 GST_OBJECT_UNLOCK (pad);
887 * gst_pad_activate_push:
888 * @pad: the #GstPad to activate or deactivate.
889 * @active: whether the pad should be active or not.
891 * Activates or deactivates the given pad in push mode via dispatching to the
892 * pad's activatepushfunc. For use from within pad activation functions only.
894 * If you don't know what this is, you probably don't want to call it.
896 * Returns: %TRUE if the operation was successful.
901 gst_pad_activate_push (GstPad * pad, gboolean active)
903 GstActivateMode old, new;
905 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
906 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
907 active ? "activated" : "deactivated");
909 GST_OBJECT_LOCK (pad);
910 old = GST_PAD_ACTIVATE_MODE (pad);
911 GST_OBJECT_UNLOCK (pad);
915 case GST_ACTIVATE_PUSH:
916 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
918 case GST_ACTIVATE_PULL:
919 GST_DEBUG_OBJECT (pad,
920 "activating pad from push, deactivating pull first");
921 /* pad was activate in the wrong direction, deactivate it
922 * an reactivate it in push mode */
923 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
924 goto deactivate_failed;
925 /* fallthrough, pad is deactivated now. */
926 case GST_ACTIVATE_NONE:
927 GST_DEBUG_OBJECT (pad, "activating pad from none");
932 case GST_ACTIVATE_NONE:
933 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
935 case GST_ACTIVATE_PULL:
936 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
937 /* pad was activated in the other direction, deactivate it
938 * in pull mode, this should not happen... */
939 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
940 goto deactivate_failed;
941 /* everything is fine now */
943 case GST_ACTIVATE_PUSH:
944 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
949 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
950 pre_activate (pad, new);
952 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
953 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
957 /* quite ok, element relies on state change func to prepare itself */
960 post_activate (pad, new);
962 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
963 active ? "activated" : "deactivated");
968 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
969 active ? "activated" : "deactivated");
974 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
975 "failed to %s in switch to push from mode %d",
976 (active ? "activate" : "deactivate"), old);
981 GST_OBJECT_LOCK (pad);
982 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
983 active ? "activate" : "deactivate");
984 _priv_gst_pad_invalidate_cache (pad);
985 GST_PAD_SET_FLUSHING (pad);
986 GST_PAD_ACTIVATE_MODE (pad) = old;
987 GST_OBJECT_UNLOCK (pad);
994 * @pad: the #GstPad to query
996 * Query if a pad is active
998 * Returns: TRUE if the pad is active.
1003 gst_pad_is_active (GstPad * pad)
1005 gboolean result = FALSE;
1007 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1009 GST_OBJECT_LOCK (pad);
1010 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1011 GST_OBJECT_UNLOCK (pad);
1017 * gst_pad_set_blocked_async_full:
1018 * @pad: the #GstPad to block or unblock
1019 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1020 * @callback: #GstPadBlockCallback that will be called when the
1021 * operation succeeds
1022 * @user_data: (closure): user data passed to the callback
1023 * @destroy_data: #GDestroyNotify for user_data
1025 * Blocks or unblocks the dataflow on a pad. The provided callback
1026 * is called when the operation succeeds; this happens right before the next
1027 * attempt at pushing a buffer on the pad.
1029 * This can take a while as the pad can only become blocked when real dataflow
1031 * When the pipeline is stalled, for example in PAUSED, this can
1032 * take an indeterminate amount of time.
1033 * You can pass NULL as the callback to make this call block. Be careful with
1034 * this blocking call as it might not return for reasons stated above.
1037 * Pad block handlers are only called for source pads in push mode
1038 * and sink pads in pull mode.
1041 * Returns: TRUE if the pad could be blocked. This function can fail if the
1042 * wrong parameters were passed or the pad was already in the requested state.
1049 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1050 GstPadBlockCallback callback, gpointer user_data,
1051 GDestroyNotify destroy_data)
1053 gboolean was_blocked = FALSE;
1055 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1057 GST_OBJECT_LOCK (pad);
1059 was_blocked = GST_PAD_IS_BLOCKED (pad);
1061 if (G_UNLIKELY (was_blocked == blocked))
1062 goto had_right_state;
1065 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1067 _priv_gst_pad_invalidate_cache (pad);
1068 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1070 if (pad->block_destroy_data && pad->block_data)
1071 pad->block_destroy_data (pad->block_data);
1073 pad->block_callback = callback;
1074 pad->block_data = user_data;
1075 pad->block_destroy_data = destroy_data;
1076 pad->abidata.ABI.block_callback_called = FALSE;
1078 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1079 GST_PAD_BLOCK_WAIT (pad);
1080 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1083 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1085 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1087 if (pad->block_destroy_data && pad->block_data)
1088 pad->block_destroy_data (pad->block_data);
1090 pad->block_callback = callback;
1091 pad->block_data = user_data;
1092 pad->block_destroy_data = destroy_data;
1093 pad->abidata.ABI.block_callback_called = FALSE;
1095 GST_PAD_BLOCK_BROADCAST (pad);
1097 /* no callback, wait for the unblock to happen */
1098 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1099 GST_PAD_BLOCK_WAIT (pad);
1100 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1103 GST_OBJECT_UNLOCK (pad);
1109 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1110 "pad was in right state (%d)", was_blocked);
1111 GST_OBJECT_UNLOCK (pad);
1118 * gst_pad_set_blocked_async:
1119 * @pad: the #GstPad to block or unblock
1120 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1121 * @callback: #GstPadBlockCallback that will be called when the
1122 * operation succeeds
1123 * @user_data: (closure): user data passed to the callback
1125 * Blocks or unblocks the dataflow on a pad. The provided callback
1126 * is called when the operation succeeds; this happens right before the next
1127 * attempt at pushing a buffer on the pad.
1129 * This can take a while as the pad can only become blocked when real dataflow
1131 * When the pipeline is stalled, for example in PAUSED, this can
1132 * take an indeterminate amount of time.
1133 * You can pass NULL as the callback to make this call block. Be careful with
1134 * this blocking call as it might not return for reasons stated above.
1137 * Pad block handlers are only called for source pads in push mode
1138 * and sink pads in pull mode.
1141 * Returns: TRUE if the pad could be blocked. This function can fail if the
1142 * wrong parameters were passed or the pad was already in the requested state.
1147 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1148 GstPadBlockCallback callback, gpointer user_data)
1150 return gst_pad_set_blocked_async_full (pad, blocked,
1151 callback, user_data, NULL);
1155 * gst_pad_set_blocked:
1156 * @pad: the #GstPad to block or unblock
1157 * @blocked: boolean indicating we should block or unblock
1159 * Blocks or unblocks the dataflow on a pad. This function is
1160 * a shortcut for gst_pad_set_blocked_async() with a NULL
1164 * Pad blocks are only possible for source pads in push mode
1165 * and sink pads in pull mode.
1168 * Returns: TRUE if the pad could be blocked. This function can fail if the
1169 * wrong parameters were passed or the pad was already in the requested state.
1174 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1176 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1180 * gst_pad_is_blocked:
1181 * @pad: the #GstPad to query
1183 * Checks if the pad is blocked or not. This function returns the
1184 * last requested state of the pad. It is not certain that the pad
1185 * is actually blocking at this point (see gst_pad_is_blocking()).
1187 * Returns: TRUE if the pad is blocked.
1192 gst_pad_is_blocked (GstPad * pad)
1194 gboolean result = FALSE;
1196 g_return_val_if_fail (GST_IS_PAD (pad), result);
1198 GST_OBJECT_LOCK (pad);
1199 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1200 GST_OBJECT_UNLOCK (pad);
1206 * gst_pad_is_blocking:
1207 * @pad: the #GstPad to query
1209 * Checks if the pad is blocking or not. This is a guaranteed state
1210 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1212 * Returns: TRUE if the pad is blocking.
1219 gst_pad_is_blocking (GstPad * pad)
1221 gboolean result = FALSE;
1223 g_return_val_if_fail (GST_IS_PAD (pad), result);
1225 GST_OBJECT_LOCK (pad);
1226 /* the blocking flag is only valid if the pad is not flushing */
1227 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1228 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1229 GST_OBJECT_UNLOCK (pad);
1235 * gst_pad_set_activate_function:
1237 * @activate: the #GstPadActivateFunction to set.
1239 * Sets the given activate function for @pad. The activate function will
1240 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1241 * the actual activation. Only makes sense to set on sink pads.
1243 * Call this function if your sink pad can start a pull-based task.
1246 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1248 g_return_if_fail (GST_IS_PAD (pad));
1250 GST_PAD_ACTIVATEFUNC (pad) = activate;
1251 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1252 GST_DEBUG_FUNCPTR_NAME (activate));
1256 * gst_pad_set_activatepull_function:
1258 * @activatepull: the #GstPadActivateModeFunction to set.
1260 * Sets the given activate_pull function for the pad. An activate_pull function
1261 * prepares the element and any upstream connections for pulling. See XXX
1262 * part-activation.txt for details.
1265 gst_pad_set_activatepull_function (GstPad * pad,
1266 GstPadActivateModeFunction activatepull)
1268 g_return_if_fail (GST_IS_PAD (pad));
1270 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1271 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1272 GST_DEBUG_FUNCPTR_NAME (activatepull));
1276 * gst_pad_set_activatepush_function:
1278 * @activatepush: the #GstPadActivateModeFunction to set.
1280 * Sets the given activate_push function for the pad. An activate_push function
1281 * prepares the element for pushing. See XXX part-activation.txt for details.
1284 gst_pad_set_activatepush_function (GstPad * pad,
1285 GstPadActivateModeFunction activatepush)
1287 g_return_if_fail (GST_IS_PAD (pad));
1289 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1290 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1291 GST_DEBUG_FUNCPTR_NAME (activatepush));
1295 * gst_pad_set_chain_function:
1296 * @pad: a sink #GstPad.
1297 * @chain: the #GstPadChainFunction to set.
1299 * Sets the given chain function for the pad. The chain function is called to
1300 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1303 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1305 g_return_if_fail (GST_IS_PAD (pad));
1306 g_return_if_fail (GST_PAD_IS_SINK (pad));
1308 GST_PAD_CHAINFUNC (pad) = chain;
1309 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1310 GST_DEBUG_FUNCPTR_NAME (chain));
1314 * gst_pad_set_chain_list_function:
1315 * @pad: a sink #GstPad.
1316 * @chainlist: the #GstPadChainListFunction to set.
1318 * Sets the given chain list function for the pad. The chainlist function is
1319 * called to process a #GstBufferList input buffer list. See
1320 * #GstPadChainListFunction for more details.
1325 gst_pad_set_chain_list_function (GstPad * pad,
1326 GstPadChainListFunction chainlist)
1328 g_return_if_fail (GST_IS_PAD (pad));
1329 g_return_if_fail (GST_PAD_IS_SINK (pad));
1331 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1332 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1333 GST_DEBUG_FUNCPTR_NAME (chainlist));
1337 * gst_pad_set_getrange_function:
1338 * @pad: a source #GstPad.
1339 * @get: the #GstPadGetRangeFunction to set.
1341 * Sets the given getrange function for the pad. The getrange function is
1342 * called to produce a new #GstBuffer to start the processing pipeline. see
1343 * #GstPadGetRangeFunction for a description of the getrange function.
1346 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1348 g_return_if_fail (GST_IS_PAD (pad));
1349 g_return_if_fail (GST_PAD_IS_SRC (pad));
1351 GST_PAD_GETRANGEFUNC (pad) = get;
1353 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1354 GST_DEBUG_FUNCPTR_NAME (get));
1358 * gst_pad_set_checkgetrange_function:
1359 * @pad: a source #GstPad.
1360 * @check: the #GstPadCheckGetRangeFunction to set.
1362 * Sets the given checkgetrange function for the pad. Implement this function
1363 * on a pad if you dynamically support getrange based scheduling on the pad.
1366 gst_pad_set_checkgetrange_function (GstPad * pad,
1367 GstPadCheckGetRangeFunction check)
1369 g_return_if_fail (GST_IS_PAD (pad));
1370 g_return_if_fail (GST_PAD_IS_SRC (pad));
1372 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1374 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1375 GST_DEBUG_FUNCPTR_NAME (check));
1379 * gst_pad_set_event_function:
1380 * @pad: a #GstPad of either direction.
1381 * @event: the #GstPadEventFunction to set.
1383 * Sets the given event handler for the pad.
1386 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1388 g_return_if_fail (GST_IS_PAD (pad));
1390 GST_PAD_EVENTFUNC (pad) = event;
1392 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1393 GST_DEBUG_FUNCPTR_NAME (event));
1397 * gst_pad_set_query_function:
1398 * @pad: a #GstPad of either direction.
1399 * @query: the #GstPadQueryFunction to set.
1401 * Set the given query function for the pad.
1404 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1406 g_return_if_fail (GST_IS_PAD (pad));
1408 GST_PAD_QUERYFUNC (pad) = query;
1410 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1411 GST_DEBUG_FUNCPTR_NAME (query));
1415 * gst_pad_set_query_type_function:
1416 * @pad: a #GstPad of either direction.
1417 * @type_func: the #GstPadQueryTypeFunction to set.
1419 * Set the given query type function for the pad.
1422 gst_pad_set_query_type_function (GstPad * pad,
1423 GstPadQueryTypeFunction type_func)
1425 g_return_if_fail (GST_IS_PAD (pad));
1427 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1429 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1430 GST_DEBUG_FUNCPTR_NAME (type_func));
1434 * gst_pad_get_query_types:
1437 * Get an array of supported queries that can be performed
1440 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1443 const GstQueryType *
1444 gst_pad_get_query_types (GstPad * pad)
1446 GstPadQueryTypeFunction func;
1448 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1450 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1462 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1464 *data = gst_pad_get_query_types (pad);
1470 * gst_pad_get_query_types_default:
1473 * Invoke the default dispatcher for the query types on
1476 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1477 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1478 * query types function.
1480 const GstQueryType *
1481 gst_pad_get_query_types_default (GstPad * pad)
1483 GstQueryType *result = NULL;
1485 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1487 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1488 gst_pad_get_query_types_dispatcher, &result);
1494 * gst_pad_set_iterate_internal_links_function:
1495 * @pad: a #GstPad of either direction.
1496 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1498 * Sets the given internal link iterator function for the pad.
1503 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1504 GstPadIterIntLinkFunction iterintlink)
1506 g_return_if_fail (GST_IS_PAD (pad));
1508 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1509 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1510 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1514 * gst_pad_set_internal_link_function:
1515 * @pad: a #GstPad of either direction.
1516 * @intlink: the #GstPadIntLinkFunction to set.
1518 * Sets the given internal link function for the pad.
1520 * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1522 #ifndef GST_REMOVE_DEPRECATED
1523 #ifdef GST_DISABLE_DEPRECATED
1525 gst_pad_set_internal_link_function (GstPad * pad,
1526 GstPadIntLinkFunction intlink);
1529 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1531 g_return_if_fail (GST_IS_PAD (pad));
1533 GST_PAD_INTLINKFUNC (pad) = intlink;
1534 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1535 GST_DEBUG_FUNCPTR_NAME (intlink));
1537 #endif /* GST_REMOVE_DEPRECATED */
1540 * gst_pad_set_link_function:
1542 * @link: the #GstPadLinkFunction to set.
1544 * Sets the given link function for the pad. It will be called when
1545 * the pad is linked with another pad.
1547 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1550 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1551 * cannot be made for some reason.
1553 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1554 * of the peer sink pad, if present.
1557 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1559 g_return_if_fail (GST_IS_PAD (pad));
1561 GST_PAD_LINKFUNC (pad) = link;
1562 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1563 GST_DEBUG_FUNCPTR_NAME (link));
1567 * gst_pad_set_unlink_function:
1569 * @unlink: the #GstPadUnlinkFunction to set.
1571 * Sets the given unlink function for the pad. It will be called
1572 * when the pad is unlinked.
1575 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1577 g_return_if_fail (GST_IS_PAD (pad));
1579 GST_PAD_UNLINKFUNC (pad) = unlink;
1580 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1581 GST_DEBUG_FUNCPTR_NAME (unlink));
1585 * gst_pad_set_getcaps_function:
1587 * @getcaps: the #GstPadGetCapsFunction to set.
1589 * Sets the given getcaps function for the pad. @getcaps should return the
1590 * allowable caps for a pad in the context of the element's state, its link to
1591 * other elements, and the devices or files it has opened. These caps must be a
1592 * subset of the pad template caps. In the NULL state with no links, @getcaps
1593 * should ideally return the same caps as the pad template. In rare
1594 * circumstances, an object property can affect the caps returned by @getcaps,
1595 * but this is discouraged.
1597 * You do not need to call this function if @pad's allowed caps are always the
1598 * same as the pad template caps. This can only be true if the padtemplate
1599 * has fixed simple caps.
1601 * For most filters, the caps returned by @getcaps is directly affected by the
1602 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1603 * the srcpad's getcaps function is directly related to the stream data. Again,
1604 * @getcaps should return the most specific caps it reasonably can, since this
1605 * helps with autoplugging.
1607 * Note that the return value from @getcaps is owned by the caller, so the
1608 * caller should unref the caps after usage.
1611 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1613 g_return_if_fail (GST_IS_PAD (pad));
1615 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1616 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1617 GST_DEBUG_FUNCPTR_NAME (getcaps));
1621 * gst_pad_set_acceptcaps_function:
1623 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1625 * Sets the given acceptcaps function for the pad. The acceptcaps function
1626 * will be called to check if the pad can accept the given caps. Setting the
1627 * acceptcaps function to NULL restores the default behaviour of allowing
1628 * any caps that matches the caps from gst_pad_get_caps().
1631 gst_pad_set_acceptcaps_function (GstPad * pad,
1632 GstPadAcceptCapsFunction acceptcaps)
1634 g_return_if_fail (GST_IS_PAD (pad));
1636 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1637 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1638 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1642 * gst_pad_set_fixatecaps_function:
1644 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1646 * Sets the given fixatecaps function for the pad. The fixatecaps function
1647 * will be called whenever the default values for a GstCaps needs to be
1651 gst_pad_set_fixatecaps_function (GstPad * pad,
1652 GstPadFixateCapsFunction fixatecaps)
1654 g_return_if_fail (GST_IS_PAD (pad));
1656 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1657 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1658 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1662 * gst_pad_set_setcaps_function:
1664 * @setcaps: the #GstPadSetCapsFunction to set.
1666 * Sets the given setcaps function for the pad. The setcaps function
1667 * will be called whenever a buffer with a new media type is pushed or
1668 * pulled from the pad. The pad/element needs to update its internal
1669 * structures to process the new media type. If this new type is not
1670 * acceptable, the setcaps function should return FALSE.
1673 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1675 g_return_if_fail (GST_IS_PAD (pad));
1677 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1678 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1679 GST_DEBUG_FUNCPTR_NAME (setcaps));
1683 * gst_pad_set_bufferalloc_function:
1684 * @pad: a sink #GstPad.
1685 * @bufalloc: the #GstPadBufferAllocFunction to set.
1687 * Sets the given bufferalloc function for the pad. Note that the
1688 * bufferalloc function can only be set on sinkpads.
1691 gst_pad_set_bufferalloc_function (GstPad * pad,
1692 GstPadBufferAllocFunction bufalloc)
1694 g_return_if_fail (GST_IS_PAD (pad));
1695 g_return_if_fail (GST_PAD_IS_SINK (pad));
1697 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1698 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1699 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1704 * @srcpad: the source #GstPad to unlink.
1705 * @sinkpad: the sink #GstPad to unlink.
1707 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1708 * signal on both pads.
1710 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1711 * the pads were not linked together.
1716 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1718 gboolean result = FALSE;
1719 GstElement *parent = NULL;
1721 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1722 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1723 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1724 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1726 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1727 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1728 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1730 /* We need to notify the parent before taking any pad locks as the bin in
1731 * question might be waiting for a lock on the pad while holding its lock
1732 * that our message will try to take. */
1733 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1734 if (GST_IS_ELEMENT (parent)) {
1735 gst_element_post_message (parent,
1736 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1737 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1739 gst_object_unref (parent);
1744 GST_OBJECT_LOCK (srcpad);
1746 GST_OBJECT_LOCK (sinkpad);
1748 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1749 goto not_linked_together;
1751 if (GST_PAD_UNLINKFUNC (srcpad)) {
1752 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1754 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1755 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1758 _priv_gst_pad_invalidate_cache (srcpad);
1760 /* first clear peers */
1761 GST_PAD_PEER (srcpad) = NULL;
1762 GST_PAD_PEER (sinkpad) = NULL;
1764 GST_OBJECT_UNLOCK (sinkpad);
1765 GST_OBJECT_UNLOCK (srcpad);
1767 /* fire off a signal to each of the pads telling them
1768 * that they've been unlinked */
1769 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1770 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1772 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1773 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1778 if (parent != NULL) {
1779 gst_element_post_message (parent,
1780 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1781 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1782 gst_object_unref (parent);
1787 not_linked_together:
1789 /* we do not emit a warning in this case because unlinking cannot
1790 * be made MT safe.*/
1791 GST_OBJECT_UNLOCK (sinkpad);
1792 GST_OBJECT_UNLOCK (srcpad);
1798 * gst_pad_is_linked:
1799 * @pad: pad to check
1801 * Checks if a @pad is linked to another pad or not.
1803 * Returns: TRUE if the pad is linked, FALSE otherwise.
1808 gst_pad_is_linked (GstPad * pad)
1812 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1814 GST_OBJECT_LOCK (pad);
1815 result = (GST_PAD_PEER (pad) != NULL);
1816 GST_OBJECT_UNLOCK (pad);
1821 /* get the caps from both pads and see if the intersection
1824 * This function should be called with the pad LOCK on both
1828 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1829 GstPadLinkCheck flags)
1831 GstCaps *srccaps = NULL;
1832 GstCaps *sinkcaps = NULL;
1833 gboolean compatible = FALSE;
1835 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1838 /* Doing the expensive caps checking takes priority over only checking the template caps */
1839 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1840 srccaps = gst_pad_get_caps_unlocked (src);
1841 sinkcaps = gst_pad_get_caps_unlocked (sink);
1843 /* If one of the two pads doesn't have a template, consider the intersection
1845 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1846 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1850 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1852 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1855 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1857 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1860 /* if we have caps on both pads we can check the intersection. If one
1861 * of the caps is NULL, we return TRUE. */
1862 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1864 gst_caps_unref (srccaps);
1866 gst_caps_unref (sinkcaps);
1870 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1871 gst_caps_unref (srccaps);
1872 gst_caps_unref (sinkcaps);
1875 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1876 (compatible ? "" : "not"));
1881 /* check if the grandparents of both pads are the same.
1882 * This check is required so that we don't try to link
1883 * pads from elements in different bins without ghostpads.
1885 * The LOCK should be held on both pads
1888 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1890 GstObject *psrc, *psink;
1892 psrc = GST_OBJECT_PARENT (src);
1893 psink = GST_OBJECT_PARENT (sink);
1895 /* if one of the pads has no parent, we allow the link */
1896 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1899 /* only care about parents that are elements */
1900 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1901 goto no_element_parent;
1903 /* if the parents are the same, we have a loop */
1904 if (G_UNLIKELY (psrc == psink))
1907 /* if they both have a parent, we check the grandparents. We can not lock
1908 * the parent because we hold on the child (pad) and the locking order is
1909 * parent >> child. */
1910 psrc = GST_OBJECT_PARENT (psrc);
1911 psink = GST_OBJECT_PARENT (psink);
1913 /* if they have grandparents but they are not the same */
1914 if (G_UNLIKELY (psrc != psink))
1915 goto wrong_grandparents;
1922 GST_CAT_DEBUG (GST_CAT_CAPS,
1923 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1924 GST_PTR_FORMAT, psrc, psink);
1929 GST_CAT_DEBUG (GST_CAT_CAPS,
1930 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1931 GST_PTR_FORMAT, psrc, psink);
1936 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1942 GST_CAT_DEBUG (GST_CAT_CAPS,
1943 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1944 GST_PTR_FORMAT, psrc, psink);
1949 /* FIXME leftover from an attempt at refactoring... */
1950 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1951 * the two pads will be locked in the srcpad, sinkpad order. */
1952 static GstPadLinkReturn
1953 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1955 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1956 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1958 GST_OBJECT_LOCK (srcpad);
1960 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1961 goto src_was_linked;
1963 GST_OBJECT_LOCK (sinkpad);
1965 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1966 goto sink_was_linked;
1968 /* check hierarchy, pads can only be linked if the grandparents
1970 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1971 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1972 goto wrong_hierarchy;
1974 /* check pad caps for non-empty intersection */
1975 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1978 /* FIXME check pad scheduling for non-empty intersection */
1980 return GST_PAD_LINK_OK;
1984 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1985 GST_DEBUG_PAD_NAME (srcpad),
1986 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1987 /* we do not emit a warning in this case because unlinking cannot
1988 * be made MT safe.*/
1989 GST_OBJECT_UNLOCK (srcpad);
1990 return GST_PAD_LINK_WAS_LINKED;
1994 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1995 GST_DEBUG_PAD_NAME (sinkpad),
1996 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1997 /* we do not emit a warning in this case because unlinking cannot
1998 * be made MT safe.*/
1999 GST_OBJECT_UNLOCK (sinkpad);
2000 GST_OBJECT_UNLOCK (srcpad);
2001 return GST_PAD_LINK_WAS_LINKED;
2005 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2006 GST_OBJECT_UNLOCK (sinkpad);
2007 GST_OBJECT_UNLOCK (srcpad);
2008 return GST_PAD_LINK_WRONG_HIERARCHY;
2012 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2013 GST_OBJECT_UNLOCK (sinkpad);
2014 GST_OBJECT_UNLOCK (srcpad);
2015 return GST_PAD_LINK_NOFORMAT;
2021 * @srcpad: the source #GstPad.
2022 * @sinkpad: the sink #GstPad.
2024 * Checks if the source pad and the sink pad are compatible so they can be
2027 * Returns: TRUE if the pads can be linked.
2030 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2032 GstPadLinkReturn result;
2034 /* generic checks */
2035 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2036 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2038 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2039 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2041 /* gst_pad_link_prepare does everything for us, we only release the locks
2042 * on the pads that it gets us. If this function returns !OK the locks are not
2044 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2045 if (result != GST_PAD_LINK_OK)
2048 GST_OBJECT_UNLOCK (srcpad);
2049 GST_OBJECT_UNLOCK (sinkpad);
2052 return result == GST_PAD_LINK_OK;
2056 * gst_pad_link_full:
2057 * @srcpad: the source #GstPad to link.
2058 * @sinkpad: the sink #GstPad to link.
2059 * @flags: the checks to validate when linking
2061 * Links the source pad and the sink pad.
2063 * This variant of #gst_pad_link provides a more granular control on the
2064 * checks being done when linking. While providing some considerable speedups
2065 * the caller of this method must be aware that wrong usage of those flags
2066 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2067 * for more information.
2071 * Returns: A result code indicating if the connection worked or
2077 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2079 GstPadLinkReturn result;
2082 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2083 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2084 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2085 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2086 GST_PAD_LINK_WRONG_DIRECTION);
2088 /* Notify the parent early. See gst_pad_unlink for details. */
2089 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2090 if (GST_IS_ELEMENT (parent)) {
2091 gst_element_post_message (parent,
2092 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2093 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2095 gst_object_unref (parent);
2100 /* prepare will also lock the two pads */
2101 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2103 if (result != GST_PAD_LINK_OK)
2106 /* must set peers before calling the link function */
2107 GST_PAD_PEER (srcpad) = sinkpad;
2108 GST_PAD_PEER (sinkpad) = srcpad;
2110 GST_OBJECT_UNLOCK (sinkpad);
2111 GST_OBJECT_UNLOCK (srcpad);
2113 /* FIXME released the locks here, concurrent thread might link
2114 * something else. */
2115 if (GST_PAD_LINKFUNC (srcpad)) {
2116 /* this one will call the peer link function */
2117 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2118 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2119 /* if no source link function, we need to call the sink link
2120 * function ourselves. */
2121 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2123 result = GST_PAD_LINK_OK;
2126 GST_OBJECT_LOCK (srcpad);
2127 GST_OBJECT_LOCK (sinkpad);
2129 if (result == GST_PAD_LINK_OK) {
2130 GST_OBJECT_UNLOCK (sinkpad);
2131 GST_OBJECT_UNLOCK (srcpad);
2133 /* fire off a signal to each of the pads telling them
2134 * that they've been linked */
2135 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2136 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2138 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2139 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2141 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2142 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2144 GST_PAD_PEER (srcpad) = NULL;
2145 GST_PAD_PEER (sinkpad) = NULL;
2147 GST_OBJECT_UNLOCK (sinkpad);
2148 GST_OBJECT_UNLOCK (srcpad);
2153 gst_element_post_message (parent,
2154 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2155 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2156 gst_object_unref (parent);
2164 * @srcpad: the source #GstPad to link.
2165 * @sinkpad: the sink #GstPad to link.
2167 * Links the source pad and the sink pad.
2169 * Returns: A result code indicating if the connection worked or
2175 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2177 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2181 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2183 GstPadTemplate **template_p;
2185 /* this function would need checks if it weren't static */
2187 GST_OBJECT_LOCK (pad);
2188 template_p = &pad->padtemplate;
2189 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2190 GST_OBJECT_UNLOCK (pad);
2193 gst_pad_template_pad_created (templ, pad);
2197 * gst_pad_get_pad_template:
2200 * Gets the template for @pad.
2202 * Returns: (transfer none): the #GstPadTemplate from which this pad was
2203 * instantiated, or %NULL if this pad has no template.
2205 * FIXME: currently returns an unrefcounted padtemplate.
2208 gst_pad_get_pad_template (GstPad * pad)
2210 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2212 return GST_PAD_PAD_TEMPLATE (pad);
2216 /* should be called with the pad LOCK held */
2217 /* refs the caps, so caller is responsible for getting it unreffed */
2219 gst_pad_get_caps_unlocked (GstPad * pad)
2221 GstCaps *result = NULL;
2222 GstPadTemplate *templ;
2224 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2226 if (GST_PAD_GETCAPSFUNC (pad)) {
2227 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2228 "dispatching to pad getcaps function");
2230 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2231 GST_OBJECT_UNLOCK (pad);
2232 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2233 GST_OBJECT_LOCK (pad);
2234 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2236 if (result == NULL) {
2237 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2238 GST_DEBUG_PAD_NAME (pad));
2240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2241 "pad getcaps returned %" GST_PTR_FORMAT, result);
2242 #ifndef G_DISABLE_ASSERT
2243 /* check that the returned caps are a real subset of the template caps */
2244 if (GST_PAD_PAD_TEMPLATE (pad)) {
2245 const GstCaps *templ_caps =
2246 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2247 if (!gst_caps_is_subset (result, templ_caps)) {
2250 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2251 "pad returned caps %" GST_PTR_FORMAT
2252 " which are not a real subset of its template caps %"
2253 GST_PTR_FORMAT, result, templ_caps);
2255 ("pad %s:%s returned caps which are not a real "
2256 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2257 temp = gst_caps_intersect (templ_caps, result);
2258 gst_caps_unref (result);
2266 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2267 result = GST_PAD_TEMPLATE_CAPS (templ);
2268 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2269 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2272 result = gst_caps_ref (result);
2275 if ((result = GST_PAD_CAPS (pad))) {
2276 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2277 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2279 result = gst_caps_ref (result);
2283 /* this almost never happens */
2284 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2285 result = gst_caps_new_empty ();
2291 /* FIXME-0.11: what about making this the default and using
2292 * gst_caps_make_writable() explicitely where needed
2295 * gst_pad_get_caps_reffed:
2296 * @pad: a #GstPad to get the capabilities of.
2298 * Gets the capabilities this pad can produce or consume. Preferred function if
2299 * one only wants to read or intersect the caps.
2301 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2306 gst_pad_get_caps_reffed (GstPad * pad)
2308 GstCaps *result = NULL;
2310 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2312 GST_OBJECT_LOCK (pad);
2314 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2316 result = gst_pad_get_caps_unlocked (pad);
2318 GST_OBJECT_UNLOCK (pad);
2325 * @pad: a #GstPad to get the capabilities of.
2327 * Gets the capabilities this pad can produce or consume.
2328 * Note that this method doesn't necessarily return the caps set by
2329 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2330 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2331 * the pad's get_caps function;
2332 * this returns the pad template caps if not explicitly set.
2334 * Returns: (transfer full): a newly allocated copy of the #GstCaps of this pad
2339 gst_pad_get_caps (GstPad * pad)
2341 GstCaps *result = gst_pad_get_caps_reffed (pad);
2343 /* be sure that we have a copy */
2344 if (G_LIKELY (result))
2345 result = gst_caps_make_writable (result);
2350 /* FIXME-0.11: what about making this the default and using
2351 * gst_caps_make_writable() explicitely where needed
2354 * gst_pad_peer_get_caps_reffed:
2355 * @pad: a #GstPad to get the capabilities of.
2357 * Gets the capabilities of the peer connected to this pad. Preferred function
2358 * if one only wants to read or intersect the caps.
2360 * Returns: (transfer full): the caps of the pad with incremented ref-count
2365 gst_pad_peer_get_caps_reffed (GstPad * pad)
2368 GstCaps *result = NULL;
2370 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2372 GST_OBJECT_LOCK (pad);
2374 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2376 peerpad = GST_PAD_PEER (pad);
2377 if (G_UNLIKELY (peerpad == NULL))
2380 gst_object_ref (peerpad);
2381 GST_OBJECT_UNLOCK (pad);
2383 result = gst_pad_get_caps_reffed (peerpad);
2385 gst_object_unref (peerpad);
2391 GST_OBJECT_UNLOCK (pad);
2397 * gst_pad_peer_get_caps:
2398 * @pad: a #GstPad to get the peer capabilities of.
2400 * Gets the capabilities of the peer connected to this pad. Similar to
2401 * gst_pad_get_caps().
2403 * Returns: (transfer full): a newly allocated copy of the #GstCaps of the
2404 * peer pad. Use gst_caps_unref() to get rid of it. This function
2405 * returns %NULL if there is no peer pad.
2408 gst_pad_peer_get_caps (GstPad * pad)
2411 GstCaps *result = NULL;
2413 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2415 GST_OBJECT_LOCK (pad);
2417 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2419 peerpad = GST_PAD_PEER (pad);
2420 if (G_UNLIKELY (peerpad == NULL))
2423 gst_object_ref (peerpad);
2424 GST_OBJECT_UNLOCK (pad);
2426 result = gst_pad_get_caps (peerpad);
2428 gst_object_unref (peerpad);
2434 GST_OBJECT_UNLOCK (pad);
2440 fixate_value (GValue * dest, const GValue * src)
2442 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2443 g_value_init (dest, G_TYPE_INT);
2444 g_value_set_int (dest, gst_value_get_int_range_min (src));
2445 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2446 g_value_init (dest, G_TYPE_DOUBLE);
2447 g_value_set_double (dest, gst_value_get_double_range_min (src));
2448 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2449 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2450 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2451 GValue temp = { 0 };
2453 /* list could be empty */
2454 if (gst_value_list_get_size (src) <= 0)
2457 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2459 if (!fixate_value (dest, &temp))
2460 gst_value_init_and_copy (dest, &temp);
2461 g_value_unset (&temp);
2462 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2463 gboolean res = FALSE;
2466 len = gst_value_array_get_size (src);
2467 g_value_init (dest, GST_TYPE_ARRAY);
2468 for (n = 0; n < len; n++) {
2470 const GValue *orig_kid = gst_value_array_get_value (src, n);
2472 if (!fixate_value (&kid, orig_kid))
2473 gst_value_init_and_copy (&kid, orig_kid);
2476 gst_value_array_append_value (dest, &kid);
2477 g_value_unset (&kid);
2481 g_value_unset (dest);
2492 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2494 GstStructure *s = data;
2497 if (fixate_value (&v, value)) {
2498 gst_structure_id_set_value (s, field_id, &v);
2506 * gst_pad_fixate_caps:
2507 * @pad: a #GstPad to fixate
2508 * @caps: the #GstCaps to fixate
2510 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2511 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2514 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2516 GstPadFixateCapsFunction fixatefunc;
2519 g_return_if_fail (GST_IS_PAD (pad));
2520 g_return_if_fail (caps != NULL);
2521 g_return_if_fail (!gst_caps_is_empty (caps));
2522 /* FIXME-0.11: do not allow fixating any-caps
2523 * g_return_if_fail (!gst_caps_is_any (caps));
2526 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2529 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2531 fixatefunc (pad, caps);
2534 /* default fixation */
2535 gst_caps_truncate (caps);
2536 s = gst_caps_get_structure (caps, 0);
2537 gst_structure_foreach (s, gst_pad_default_fixate, s);
2540 /* Default accept caps implementation just checks against
2541 * against the allowed caps for the pad */
2543 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2545 /* get the caps and see if it intersects to something not empty */
2547 gboolean result = FALSE;
2549 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2551 allowed = gst_pad_get_caps_reffed (pad);
2553 goto nothing_allowed;
2555 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2557 result = gst_caps_can_intersect (allowed, caps);
2559 gst_caps_unref (allowed);
2566 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2572 * gst_pad_accept_caps:
2573 * @pad: a #GstPad to check
2574 * @caps: a #GstCaps to check on the pad
2576 * Check if the given pad accepts the caps.
2578 * Returns: TRUE if the pad can accept the caps.
2581 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2584 GstPadAcceptCapsFunction acceptfunc;
2585 GstCaps *existing = NULL;
2587 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2589 /* any pad can be unnegotiated */
2593 /* lock for checking the existing caps */
2594 GST_OBJECT_LOCK (pad);
2595 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2596 /* The current caps on a pad are trivially acceptable */
2597 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2598 if (caps == existing || gst_caps_is_equal (caps, existing))
2601 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2602 GST_OBJECT_UNLOCK (pad);
2604 if (G_LIKELY (acceptfunc)) {
2605 /* we can call the function */
2606 result = acceptfunc (pad, caps);
2607 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2609 /* Only null if the element explicitly unset it */
2610 result = gst_pad_acceptcaps_default (pad, caps);
2611 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2617 GST_DEBUG_OBJECT (pad, "pad had same caps");
2618 GST_OBJECT_UNLOCK (pad);
2624 * gst_pad_peer_accept_caps:
2625 * @pad: a #GstPad to check the peer of
2626 * @caps: a #GstCaps to check on the pad
2628 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2631 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2634 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2639 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2641 GST_OBJECT_LOCK (pad);
2643 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2645 peerpad = GST_PAD_PEER (pad);
2646 if (G_UNLIKELY (peerpad == NULL))
2649 gst_object_ref (peerpad);
2650 /* release lock before calling external methods but keep ref to pad */
2651 GST_OBJECT_UNLOCK (pad);
2653 result = gst_pad_accept_caps (peerpad, caps);
2655 gst_object_unref (peerpad);
2661 GST_OBJECT_UNLOCK (pad);
2668 * @pad: a #GstPad to set the capabilities of.
2669 * @caps: (transfer none): a #GstCaps to set.
2671 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2672 * caps on the pad will be unreffed. This function refs the caps so you should
2673 * unref if as soon as you don't need it anymore.
2674 * It is possible to set NULL caps, which will make the pad unnegotiated
2677 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2678 * or bad parameters were provided to this function.
2683 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2685 GstPadSetCapsFunction setcaps;
2688 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2689 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2691 GST_OBJECT_LOCK (pad);
2692 existing = GST_PAD_CAPS (pad);
2693 if (existing == caps)
2696 if (gst_caps_is_equal (caps, existing))
2697 goto setting_same_caps;
2699 setcaps = GST_PAD_SETCAPSFUNC (pad);
2701 /* call setcaps function to configure the pad only if the
2702 * caps is not NULL */
2703 if (setcaps != NULL && caps) {
2704 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2705 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2706 GST_OBJECT_UNLOCK (pad);
2707 if (!setcaps (pad, caps))
2709 GST_OBJECT_LOCK (pad);
2710 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2712 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2716 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2717 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2719 GST_OBJECT_UNLOCK (pad);
2721 #if GLIB_CHECK_VERSION(2,26,0)
2722 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2724 g_object_notify ((GObject *) pad, "caps");
2731 GST_OBJECT_UNLOCK (pad);
2736 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2737 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2738 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2740 GST_OBJECT_UNLOCK (pad);
2747 GST_OBJECT_LOCK (pad);
2748 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2749 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2750 "caps %" GST_PTR_FORMAT " could not be set", caps);
2751 GST_OBJECT_UNLOCK (pad);
2758 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2762 /* See if pad accepts the caps */
2763 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2766 /* set caps on pad if call succeeds */
2767 res = gst_pad_set_caps (pad, caps);
2768 /* no need to unref the caps here, set_caps takes a ref and
2769 * our ref goes away when we leave this function. */
2775 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2776 "caps %" GST_PTR_FORMAT " not accepted", caps);
2781 /* returns TRUE if the src pad could be configured to accept the given caps */
2783 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2788 /* See if pad accepts the caps */
2789 if (!gst_pad_accept_caps (pad, caps))
2792 res = gst_pad_set_caps (pad, caps);
2800 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2801 "caps %" GST_PTR_FORMAT " not accepted", caps);
2807 * gst_pad_get_pad_template_caps:
2808 * @pad: a #GstPad to get the template capabilities from.
2810 * Gets the capabilities for @pad's template.
2812 * Returns: (transfer none): the #GstCaps of this pad template. If you intend
2813 * to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2816 gst_pad_get_pad_template_caps (GstPad * pad)
2818 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2820 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2822 if (GST_PAD_PAD_TEMPLATE (pad))
2823 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2825 return gst_static_caps_get (&anycaps);
2830 * @pad: a #GstPad to get the peer of.
2832 * Gets the peer of @pad. This function refs the peer pad so
2833 * you need to unref it after use.
2835 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2840 gst_pad_get_peer (GstPad * pad)
2844 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2846 GST_OBJECT_LOCK (pad);
2847 result = GST_PAD_PEER (pad);
2849 gst_object_ref (result);
2850 GST_OBJECT_UNLOCK (pad);
2856 * gst_pad_get_allowed_caps:
2859 * Gets the capabilities of the allowed media types that can flow through
2860 * @pad and its peer.
2862 * The allowed capabilities is calculated as the intersection of the results of
2863 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2864 * on the resulting caps.
2866 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2867 * caps when you no longer need it. This function returns NULL when @pad
2873 gst_pad_get_allowed_caps (GstPad * pad)
2880 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2882 GST_OBJECT_LOCK (pad);
2884 peer = GST_PAD_PEER (pad);
2885 if (G_UNLIKELY (peer == NULL))
2888 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2890 gst_object_ref (peer);
2891 GST_OBJECT_UNLOCK (pad);
2892 mycaps = gst_pad_get_caps_reffed (pad);
2894 peercaps = gst_pad_get_caps_reffed (peer);
2895 gst_object_unref (peer);
2897 caps = gst_caps_intersect (mycaps, peercaps);
2898 gst_caps_unref (peercaps);
2899 gst_caps_unref (mycaps);
2901 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2908 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2909 GST_OBJECT_UNLOCK (pad);
2916 * gst_pad_get_negotiated_caps:
2919 * Gets the capabilities of the media type that currently flows through @pad
2922 * This function can be used on both src and sinkpads. Note that srcpads are
2923 * always negotiated before sinkpads so it is possible that the negotiated caps
2924 * on the srcpad do not match the negotiated caps of the peer.
2926 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2927 * the caps when you no longer need it. This function returns NULL when
2928 * the @pad has no peer or is not negotiated yet.
2933 gst_pad_get_negotiated_caps (GstPad * pad)
2938 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2940 GST_OBJECT_LOCK (pad);
2942 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2945 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2947 caps = GST_PAD_CAPS (pad);
2949 gst_caps_ref (caps);
2950 GST_OBJECT_UNLOCK (pad);
2952 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2959 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2960 GST_OBJECT_UNLOCK (pad);
2966 /* calls the buffer_alloc function on the given pad */
2967 static GstFlowReturn
2968 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2969 GstCaps * caps, GstBuffer ** buf)
2972 GstPadBufferAllocFunction bufferallocfunc;
2974 GST_OBJECT_LOCK (pad);
2975 /* when the pad is flushing we cannot give a buffer */
2976 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2979 bufferallocfunc = pad->bufferallocfunc;
2981 if (offset == GST_BUFFER_OFFSET_NONE) {
2982 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2983 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2984 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2986 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2987 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2988 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2989 bufferallocfunc, size, offset);
2991 GST_OBJECT_UNLOCK (pad);
2993 /* G_LIKELY for now since most elements don't implement a buffer alloc
2994 * function and there is no default alloc proxy function as this is usually
2996 if (G_LIKELY (bufferallocfunc == NULL))
2999 ret = bufferallocfunc (pad, offset, size, caps, buf);
3001 if (G_UNLIKELY (ret != GST_FLOW_OK))
3004 /* no error, but NULL buffer means fallback to the default */
3005 if (G_UNLIKELY (*buf == NULL))
3008 /* If the buffer alloc function didn't set up the caps like it should,
3010 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
3011 GST_WARNING_OBJECT (pad,
3012 "Buffer allocation function did not set caps. Setting");
3013 gst_buffer_set_caps (*buf, caps);
3019 /* pad was flushing */
3020 GST_OBJECT_UNLOCK (pad);
3021 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
3022 return GST_FLOW_WRONG_STATE;
3026 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3027 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
3032 /* fallback case, allocate a buffer of our own, add pad caps. */
3033 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
3035 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3036 GST_BUFFER_OFFSET (*buf) = offset;
3037 gst_buffer_set_caps (*buf, caps);
3040 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3041 "out of memory allocating %d bytes", size);
3042 return GST_FLOW_ERROR;
3047 /* FIXME 0.11: size should be unsigned */
3048 static GstFlowReturn
3049 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
3050 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
3055 gboolean caps_changed;
3057 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3058 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3059 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
3060 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
3062 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
3063 GST_PTR_FORMAT, offset, size, caps);
3065 GST_OBJECT_LOCK (pad);
3066 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3067 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3070 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3073 gst_object_ref (peer);
3074 GST_OBJECT_UNLOCK (pad);
3076 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
3077 gst_object_unref (peer);
3079 if (G_UNLIKELY (ret != GST_FLOW_OK))
3082 /* FIXME, move capnego this into a base class? */
3083 newcaps = GST_BUFFER_CAPS (*buf);
3085 /* Lock for checking caps, pretty pointless as the _pad_push() function might
3086 * change it concurrently, one of the problems with automatic caps setting in
3087 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
3088 * when there is heavy renegotiation going on in both directions. */
3089 GST_OBJECT_LOCK (pad);
3090 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
3091 GST_OBJECT_UNLOCK (pad);
3093 /* we got a new datatype on the pad, see if it can handle it */
3094 if (G_UNLIKELY (caps_changed)) {
3095 GST_DEBUG_OBJECT (pad,
3096 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3097 GST_PAD_CAPS (pad), newcaps, newcaps);
3098 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
3099 goto not_negotiated;
3102 /* sanity check (only if caps are the same) */
3103 if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
3104 goto wrong_size_fallback;
3110 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
3111 GST_OBJECT_UNLOCK (pad);
3116 /* pad has no peer */
3117 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3118 "called bufferallocfunc but had no peer");
3119 GST_OBJECT_UNLOCK (pad);
3120 return GST_FLOW_NOT_LINKED;
3124 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3125 "alloc function returned error %s", gst_flow_get_name (ret));
3130 gst_buffer_unref (*buf);
3132 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3133 "alloc function returned unacceptable buffer");
3134 return GST_FLOW_NOT_NEGOTIATED;
3136 wrong_size_fallback:
3138 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3139 "function is too small (%u < %d), doing fallback buffer alloc",
3140 GST_BUFFER_SIZE (*buf), size);
3142 gst_buffer_unref (*buf);
3144 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3145 GST_BUFFER_OFFSET (*buf) = offset;
3146 gst_buffer_set_caps (*buf, caps);
3149 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3150 "out of memory allocating %d bytes", size);
3151 return GST_FLOW_ERROR;
3157 * gst_pad_alloc_buffer:
3158 * @pad: a source #GstPad
3159 * @offset: the offset of the new buffer in the stream
3160 * @size: the size of the new buffer
3161 * @caps: the caps of the new buffer
3162 * @buf: a newly allocated buffer
3164 * Allocates a new, empty buffer optimized to push to pad @pad. This
3165 * function only works if @pad is a source pad and has a peer.
3167 * A new, empty #GstBuffer will be put in the @buf argument.
3168 * You need to check the caps of the buffer after performing this
3169 * function and renegotiate to the format if needed. If the caps changed, it is
3170 * possible that the buffer returned in @buf is not of the right size for the
3171 * new format, @buf needs to be unreffed and reallocated if this is the case.
3173 * Returns: a result code indicating success of the operation. Any
3174 * result code other than #GST_FLOW_OK is an error and @buf should
3176 * An error can occur if the pad is not connected or when the downstream
3177 * peer elements cannot provide an acceptable buffer.
3182 /* FIXME 0.11: size should be unsigned */
3184 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3187 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3191 * gst_pad_alloc_buffer_and_set_caps:
3192 * @pad: a source #GstPad
3193 * @offset: the offset of the new buffer in the stream
3194 * @size: the size of the new buffer
3195 * @caps: (transfer none): the caps of the new buffer
3196 * @buf: (out callee-allocates): a newly allocated buffer
3198 * In addition to the function gst_pad_alloc_buffer(), this function
3199 * automatically calls gst_pad_set_caps() when the caps of the
3200 * newly allocated buffer are different from the @pad caps.
3202 * After a renegotiation, the size of the new buffer returned in @buf could
3203 * be of the wrong size for the new format and must be unreffed an reallocated
3206 * Returns: a result code indicating success of the operation. Any
3207 * result code other than #GST_FLOW_OK is an error and @buf should
3209 * An error can occur if the pad is not connected or when the downstream
3210 * peer elements cannot provide an acceptable buffer.
3215 /* FIXME 0.11: size should be unsigned */
3217 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3218 GstCaps * caps, GstBuffer ** buf)
3220 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3224 #ifndef GST_REMOVE_DEPRECATED
3232 int_link_iter_data_free (IntLinkIterData * data)
3234 g_list_free (data->list);
3235 g_slice_free (IntLinkIterData, data);
3239 static GstIteratorItem
3240 iterate_pad (GstIterator * it, GstPad * pad)
3242 gst_object_ref (pad);
3243 return GST_ITERATOR_ITEM_PASS;
3247 * gst_pad_iterate_internal_links_default:
3248 * @pad: the #GstPad to get the internal links of.
3250 * Iterate the list of pads to which the given pad is linked to inside of
3251 * the parent element.
3252 * This is the default handler, and thus returns an iterator of all of the
3253 * pads inside the parent element with opposite direction.
3255 * The caller must free this iterator after use with gst_iterator_free().
3257 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3258 * returned pad with gst_object_unref().
3263 gst_pad_iterate_internal_links_default (GstPad * pad)
3270 GstIteratorDisposeFunction dispose;
3272 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3274 #ifndef GST_REMOVE_DEPRECATED
3275 /* when we get here, the default handler for the iterate links is called,
3276 * which means that the user has not installed a custom one. We first check if
3277 * there is maybe a custom legacy function we can call. */
3278 if (GST_PAD_INTLINKFUNC (pad) &&
3279 GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3280 IntLinkIterData *data;
3282 /* make an iterator for the list. We can't protect the list with a
3283 * cookie. If we would take the cookie of the parent element, we need to
3284 * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3285 * cache the per-pad list and invalidate the list when a new call to
3286 * INTLINKFUNC() returned a different list but then this would only work if
3287 * two concurrent iterators were used and the last iterator would still be
3288 * thread-unsafe. Just don't use this method anymore. */
3289 data = g_slice_new (IntLinkIterData);
3290 data->list = ((GstPadIntLinkFunction) GST_PAD_INTLINKFUNC (pad)) (pad);
3293 GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3295 cookie = &data->cookie;
3296 padlist = &data->list;
3298 dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3299 /* reuse the pad lock, it's all we have here */
3300 lock = GST_OBJECT_GET_LOCK (pad);
3306 GST_OBJECT_LOCK (pad);
3307 parent = GST_PAD_PARENT (pad);
3308 if (!parent || !GST_IS_ELEMENT (parent))
3311 gst_object_ref (parent);
3312 GST_OBJECT_UNLOCK (pad);
3314 if (pad->direction == GST_PAD_SRC)
3315 padlist = &parent->sinkpads;
3317 padlist = &parent->srcpads;
3319 GST_DEBUG_OBJECT (pad, "Making iterator");
3321 cookie = &parent->pads_cookie;
3323 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3324 lock = GST_OBJECT_GET_LOCK (parent);
3327 res = gst_iterator_new_list (GST_TYPE_PAD,
3328 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3336 GST_OBJECT_UNLOCK (pad);
3337 GST_DEBUG_OBJECT (pad, "no parent element");
3343 * gst_pad_iterate_internal_links:
3344 * @pad: the GstPad to get the internal links of.
3346 * Gets an iterator for the pads to which the given pad is linked to inside
3347 * of the parent element.
3349 * Each #GstPad element yielded by the iterator will have its refcount increased,
3350 * so unref after use.
3352 * Free-function: gst_iterator_free
3354 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3355 * pad does not have an iterator function configured. Use
3356 * gst_iterator_free() after usage.
3361 gst_pad_iterate_internal_links (GstPad * pad)
3363 GstIterator *res = NULL;
3365 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3367 if (GST_PAD_ITERINTLINKFUNC (pad))
3368 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3373 #ifndef GST_REMOVE_DEPRECATED
3375 add_unref_pad_to_list (GstPad * pad, GList ** list)
3377 *list = g_list_prepend (*list, pad);
3378 gst_object_unref (pad);
3383 * gst_pad_get_internal_links_default:
3384 * @pad: the #GstPad to get the internal links of.
3386 * Gets a list of pads to which the given pad is linked to
3387 * inside of the parent element.
3388 * This is the default handler, and thus returns a list of all of the
3389 * pads inside the parent element with opposite direction.
3391 * The caller must free this list after use with g_list_free().
3393 * Returns: (transfer full) (element-type Gst.Pad): a newly allocated #GList
3394 * of pads, or NULL if the pad has no parent.
3398 * Deprecated: This function does not ref the pads in the list so that they
3399 * could become invalid by the time the application accesses them. It's also
3400 * possible that the list changes while handling the pads, which the caller of
3401 * this function is unable to know. Use the thread-safe
3402 * gst_pad_iterate_internal_links_default() instead.
3404 #ifndef GST_REMOVE_DEPRECATED
3406 gst_pad_get_internal_links_default (GstPad * pad)
3411 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3413 GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3415 /* when we get here, the default handler for get_internal_links is called,
3416 * which means that the user has not installed a custom one. We first check if
3417 * there is maybe a custom iterate function we can call. */
3418 if (GST_PAD_ITERINTLINKFUNC (pad) &&
3419 GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3421 GstIteratorResult ires;
3422 gboolean done = FALSE;
3424 it = gst_pad_iterate_internal_links (pad);
3425 /* loop over the iterator and put all elements into a list, we also
3426 * immediatly unref them, which is bad. */
3428 ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, &res);
3430 case GST_ITERATOR_OK:
3431 case GST_ITERATOR_DONE:
3432 case GST_ITERATOR_ERROR:
3435 case GST_ITERATOR_RESYNC:
3436 /* restart, discard previous list */
3437 gst_iterator_resync (it);
3444 gst_iterator_free (it);
3446 /* lock pad, check and ref parent */
3447 GST_OBJECT_LOCK (pad);
3448 parent = GST_PAD_PARENT (pad);
3449 if (!parent || !GST_IS_ELEMENT (parent))
3452 parent = gst_object_ref (parent);
3453 GST_OBJECT_UNLOCK (pad);
3455 /* now lock the parent while we copy the pads */
3456 GST_OBJECT_LOCK (parent);
3457 if (pad->direction == GST_PAD_SRC)
3458 res = g_list_copy (parent->sinkpads);
3460 res = g_list_copy (parent->srcpads);
3461 GST_OBJECT_UNLOCK (parent);
3463 gst_object_unref (parent);
3466 /* At this point pads can be changed and unreffed. Nothing we can do about it
3467 * because for compatibility reasons this function cannot ref the pads or
3468 * notify the app that the list changed. */
3474 GST_DEBUG_OBJECT (pad, "no parent");
3475 GST_OBJECT_UNLOCK (pad);
3479 #endif /* GST_REMOVE_DEPRECATED */
3482 * gst_pad_get_internal_links:
3483 * @pad: the #GstPad to get the internal links of.
3485 * Gets a list of pads to which the given pad is linked to
3486 * inside of the parent element.
3487 * The caller must free this list after use.
3491 * Returns: (transfer full) (element-type Gst.Pad): a newly allocated #GList
3492 * of pads, free with g_list_free().
3494 * Deprecated: This function does not ref the pads in the list so that they
3495 * could become invalid by the time the application accesses them. It's also
3496 * possible that the list changes while handling the pads, which the caller of
3497 * this function is unable to know. Use the thread-safe
3498 * gst_pad_iterate_internal_links() instead.
3500 #ifndef GST_REMOVE_DEPRECATED
3501 #ifdef GST_DISABLE_DEPRECATED
3502 GList *gst_pad_get_internal_links (GstPad * pad);
3505 gst_pad_get_internal_links (GstPad * pad)
3509 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3511 GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3513 if (GST_PAD_INTLINKFUNC (pad))
3514 res = ((GstPadIntLinkFunction) GST_PAD_INTLINKFUNC (pad)) (pad);
3518 #endif /* GST_REMOVE_DEPRECATED */
3521 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3523 gboolean result = FALSE;
3525 gboolean done = FALSE;
3528 GList *pushed_pads = NULL;
3530 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3531 event, GST_EVENT_TYPE_NAME (event));
3533 iter = gst_pad_iterate_internal_links (pad);
3539 switch (gst_iterator_next (iter, &item)) {
3540 case GST_ITERATOR_OK:
3541 eventpad = GST_PAD_CAST (item);
3543 /* if already pushed, skip */
3544 if (g_list_find (pushed_pads, eventpad)) {
3545 gst_object_unref (item);
3549 if (GST_PAD_IS_SRC (eventpad)) {
3550 /* for each pad we send to, we should ref the event; it's up
3551 * to downstream to unref again when handled. */
3552 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3553 event, GST_EVENT_TYPE_NAME (event),
3554 GST_DEBUG_PAD_NAME (eventpad));
3555 gst_event_ref (event);
3556 result |= gst_pad_push_event (eventpad, event);
3558 /* we only send the event on one pad, multi-sinkpad elements
3559 * should implement a handler */
3560 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3561 event, GST_EVENT_TYPE_NAME (event),
3562 GST_DEBUG_PAD_NAME (eventpad));
3563 result = gst_pad_push_event (eventpad, event);
3568 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3570 gst_object_unref (item);
3572 case GST_ITERATOR_RESYNC:
3573 /* We don't reset the result here because we don't push the event
3574 * again on pads that got the event already and because we need
3575 * to consider the result of the previous pushes */
3576 gst_iterator_resync (iter);
3578 case GST_ITERATOR_ERROR:
3579 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3582 case GST_ITERATOR_DONE:
3587 gst_iterator_free (iter);
3591 /* If this is a sinkpad and we don't have pads to send the event to, we
3592 * return TRUE. This is so that when using the default handler on a sink
3593 * element, we don't fail to push it. */
3595 result = GST_PAD_IS_SINK (pad);
3597 g_list_free (pushed_pads);
3599 /* we handled the incoming event so we unref once */
3601 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3602 gst_event_unref (event);
3609 * gst_pad_event_default:
3610 * @pad: a #GstPad to call the default event handler on.
3611 * @event: (transfer full): the #GstEvent to handle.
3613 * Invokes the default event handler for the given pad. End-of-stream and
3614 * discontinuity events are handled specially, and then the event is sent to all
3615 * pads internally linked to @pad. Note that if there are many possible sink
3616 * pads that are internally linked to @pad, only one will be sent an event.
3617 * Multi-sinkpad elements should implement custom event handlers.
3619 * Returns: TRUE if the event was sent succesfully.
3622 gst_pad_event_default (GstPad * pad, GstEvent * event)
3624 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3625 g_return_val_if_fail (event != NULL, FALSE);
3627 GST_LOG_OBJECT (pad, "default event handler");
3629 switch (GST_EVENT_TYPE (event)) {
3632 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3633 gst_pad_pause_task (pad);
3640 return gst_pad_event_default_dispatch (pad, event);
3644 * gst_pad_dispatcher:
3645 * @pad: a #GstPad to dispatch.
3646 * @dispatch: the #GstPadDispatcherFunction to call.
3647 * @data: (closure): gpointer user data passed to the dispatcher function.
3649 * Invokes the given dispatcher function on each respective peer of
3650 * all pads that are internally linked to the given pad.
3651 * The GstPadDispatcherFunction should return TRUE when no further pads
3652 * need to be processed.
3654 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3657 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3660 gboolean res = FALSE;
3661 GstIterator *iter = NULL;
3662 gboolean done = FALSE;
3665 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3666 g_return_val_if_fail (dispatch != NULL, FALSE);
3668 iter = gst_pad_iterate_internal_links (pad);
3674 switch (gst_iterator_next (iter, &item)) {
3675 case GST_ITERATOR_OK:
3677 GstPad *int_pad = GST_PAD_CAST (item);
3678 GstPad *int_peer = gst_pad_get_peer (int_pad);
3681 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3682 GST_DEBUG_PAD_NAME (int_peer));
3683 done = res = dispatch (int_peer, data);
3684 gst_object_unref (int_peer);
3686 GST_DEBUG_OBJECT (int_pad, "no peer");
3689 gst_object_unref (item);
3691 case GST_ITERATOR_RESYNC:
3692 gst_iterator_resync (iter);
3694 case GST_ITERATOR_ERROR:
3696 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3698 case GST_ITERATOR_DONE:
3703 gst_iterator_free (iter);
3705 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3714 * @pad: a #GstPad to invoke the default query on.
3715 * @query: (transfer none): the #GstQuery to perform.
3717 * Dispatches a query to a pad. The query should have been allocated by the
3718 * caller via one of the type-specific allocation functions. The element that
3719 * the pad belongs to is responsible for filling the query with an appropriate
3720 * response, which should then be parsed with a type-specific query parsing
3723 * Again, the caller is responsible for both the allocation and deallocation of
3724 * the query structure.
3726 * Please also note that some queries might need a running pipeline to work.
3728 * Returns: TRUE if the query could be performed.
3731 gst_pad_query (GstPad * pad, GstQuery * query)
3733 GstPadQueryFunction func;
3735 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3736 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3738 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3740 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3743 return func (pad, query);
3747 GST_DEBUG_OBJECT (pad, "had no query function");
3753 * gst_pad_peer_query:
3754 * @pad: a #GstPad to invoke the peer query on.
3755 * @query: (transfer none): the #GstQuery to perform.
3757 * Performs gst_pad_query() on the peer of @pad.
3759 * The caller is responsible for both the allocation and deallocation of
3760 * the query structure.
3762 * Returns: TRUE if the query could be performed. This function returns %FALSE
3763 * if @pad has no peer.
3768 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3773 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3774 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3776 GST_OBJECT_LOCK (pad);
3778 GST_DEBUG_OBJECT (pad, "peer query");
3780 peerpad = GST_PAD_PEER (pad);
3781 if (G_UNLIKELY (peerpad == NULL))
3784 gst_object_ref (peerpad);
3785 GST_OBJECT_UNLOCK (pad);
3787 result = gst_pad_query (peerpad, query);
3789 gst_object_unref (peerpad);
3796 GST_WARNING_OBJECT (pad, "pad has no peer");
3797 GST_OBJECT_UNLOCK (pad);
3803 * gst_pad_query_default:
3804 * @pad: a #GstPad to call the default query handler on.
3805 * @query: (transfer none): the #GstQuery to handle.
3807 * Invokes the default query handler for the given pad.
3808 * The query is sent to all pads internally linked to @pad. Note that
3809 * if there are many possible sink pads that are internally linked to
3810 * @pad, only one will be sent the query.
3811 * Multi-sinkpad elements should implement custom query handlers.
3813 * Returns: TRUE if the query was performed succesfully.
3816 gst_pad_query_default (GstPad * pad, GstQuery * query)
3818 switch (GST_QUERY_TYPE (query)) {
3819 case GST_QUERY_POSITION:
3820 case GST_QUERY_SEEKING:
3821 case GST_QUERY_FORMATS:
3822 case GST_QUERY_LATENCY:
3823 case GST_QUERY_JITTER:
3824 case GST_QUERY_RATE:
3825 case GST_QUERY_CONVERT:
3827 return gst_pad_dispatcher
3828 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3832 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
3833 /* FIXME: why isn't this on a GstElement ? */
3835 * gst_pad_load_and_link:
3836 * @self: an #xmlNodePtr to read the description from.
3837 * @parent: the #GstObject element that owns the pad.
3839 * Reads the pad definition from the XML node and links the given pad
3840 * in the element to a pad of an element up in the hierarchy.
3843 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3845 xmlNodePtr field = self->xmlChildrenNode;
3846 GstPad *pad = NULL, *targetpad;
3847 GstPadTemplate *tmpl;
3851 GstObject *grandparent;
3855 if (!strcmp ((char *) field->name, "name")) {
3856 name = (gchar *) xmlNodeGetContent (field);
3857 pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3858 if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3859 && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3860 pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3862 } else if (!strcmp ((char *) field->name, "peer")) {
3863 peer = (gchar *) xmlNodeGetContent (field);
3865 field = field->next;
3867 g_return_if_fail (pad != NULL);
3872 split = g_strsplit (peer, ".", 2);
3874 if (split[0] == NULL || split[1] == NULL) {
3875 GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3876 "Could not parse peer '%s', leaving unlinked", peer);
3883 g_return_if_fail (split[0] != NULL);
3884 g_return_if_fail (split[1] != NULL);
3886 grandparent = gst_object_get_parent (parent);
3888 if (grandparent && GST_IS_BIN (grandparent)) {
3889 target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3896 targetpad = gst_element_get_static_pad (target, split[1]);
3898 targetpad = gst_element_get_request_pad (target, split[1]);
3900 if (targetpad == NULL)
3903 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3904 gst_pad_link (pad, targetpad);
3906 gst_pad_link (targetpad, pad);
3913 * gst_pad_save_thyself:
3914 * @pad: a #GstPad to save.
3915 * @parent: the parent #xmlNodePtr to save the description in.
3917 * Saves the pad into an xml representation.
3919 * Returns: the #xmlNodePtr representation of the pad.
3922 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3927 g_return_val_if_fail (GST_IS_PAD (object), NULL);
3929 pad = GST_PAD_CAST (object);
3931 xmlNewChild (parent, NULL, (xmlChar *) "name",
3932 (xmlChar *) GST_PAD_NAME (pad));
3934 if (GST_PAD_IS_SRC (pad)) {
3935 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3936 } else if (GST_PAD_IS_SINK (pad)) {
3937 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3939 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3942 if (GST_PAD_PEER (pad) != NULL) {
3945 peer = GST_PAD_PEER (pad);
3946 /* first check to see if the peer's parent's parent is the same */
3947 /* we just save it off */
3948 content = g_strdup_printf ("%s.%s",
3949 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3950 xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3953 xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3960 * gst_ghost_pad_save_thyself:
3961 * @pad: a ghost #GstPad to save.
3962 * @parent: the parent #xmlNodePtr to save the description in.
3964 * Saves the ghost pad into an xml representation.
3966 * Returns: the #xmlNodePtr representation of the pad.
3969 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3973 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3975 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3976 xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3977 xmlNewChild (self, NULL, (xmlChar *) "parent",
3978 (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3980 /* FIXME FIXME FIXME! */
3985 #endif /* GST_DISABLE_LOADSAVE */
3988 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3989 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3992 * This function performs the pad blocking when an event, buffer push
3993 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3994 * streaming thread after informing the pad has been blocked.
3996 * An application can with this method wait and block any streaming
3997 * thread and perform operations such as seeking or linking.
3999 * Two methods are available for notifying the application of the
4001 * - the callback method, which happens in the STREAMING thread with
4002 * the STREAM_LOCK held. With this method, the most useful way of
4003 * dealing with the callback is to post a message to the main thread
4004 * where the pad block can then be handled outside of the streaming
4005 * thread. With the last method one can perform all operations such
4006 * as doing a state change, linking, unblocking, seeking etc on the
4008 * - the GCond signal method, which makes any thread unblock when
4009 * the pad block happens.
4011 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
4012 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
4016 static GstFlowReturn
4017 handle_pad_block (GstPad * pad)
4019 GstPadBlockCallback callback;
4021 GstFlowReturn ret = GST_FLOW_OK;
4023 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
4025 /* flushing, don't bother trying to block and return WRONG_STATE
4027 if (GST_PAD_IS_FLUSHING (pad))
4028 goto flushingnonref;
4030 /* we grab an extra ref for the callbacks, this is probably not
4031 * needed (callback code does not have a ref and cannot unref). I
4032 * think this was done to make it possible to unref the element in
4033 * the callback, which is in the end totally impossible as it
4034 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
4035 * all taken when calling this function. */
4036 gst_object_ref (pad);
4038 while (GST_PAD_IS_BLOCKED (pad)) {
4040 /* we either have a callback installed to notify the block or
4041 * some other thread is doing a GCond wait. */
4042 callback = pad->block_callback;
4043 pad->abidata.ABI.block_callback_called = TRUE;
4045 /* there is a callback installed, call it. We release the
4046 * lock so that the callback can do something usefull with the
4048 user_data = pad->block_data;
4049 GST_OBJECT_UNLOCK (pad);
4050 callback (pad, TRUE, user_data);
4051 GST_OBJECT_LOCK (pad);
4053 /* we released the lock, recheck flushing */
4054 if (GST_PAD_IS_FLUSHING (pad))
4057 /* no callback, signal the thread that is doing a GCond wait
4059 GST_PAD_BLOCK_BROADCAST (pad);
4061 } while (pad->abidata.ABI.block_callback_called == FALSE
4062 && GST_PAD_IS_BLOCKED (pad));
4064 /* OBJECT_LOCK could have been released when we did the callback, which
4065 * then could have made the pad unblock so we need to check the blocking
4066 * condition again. */
4067 if (!GST_PAD_IS_BLOCKED (pad))
4070 /* now we block the streaming thread. It can be unlocked when we
4071 * deactivate the pad (which will also set the FLUSHING flag) or
4072 * when the pad is unblocked. A flushing event will also unblock
4073 * the pad after setting the FLUSHING flag. */
4074 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4075 "Waiting to be unblocked or set flushing");
4076 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
4077 GST_PAD_BLOCK_WAIT (pad);
4078 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
4080 /* see if we got unblocked by a flush or not */
4081 if (GST_PAD_IS_FLUSHING (pad))
4085 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
4087 /* when we get here, the pad is unblocked again and we perform
4088 * the needed unblock code. */
4089 callback = pad->block_callback;
4091 /* we need to call the callback */
4092 user_data = pad->block_data;
4093 GST_OBJECT_UNLOCK (pad);
4094 callback (pad, FALSE, user_data);
4095 GST_OBJECT_LOCK (pad);
4097 /* we need to signal the thread waiting on the GCond */
4098 GST_PAD_BLOCK_BROADCAST (pad);
4101 gst_object_unref (pad);
4107 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
4108 return GST_FLOW_WRONG_STATE;
4112 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
4113 gst_object_unref (pad);
4114 return GST_FLOW_WRONG_STATE;
4118 /**********************************************************************
4119 * Data passing functions
4123 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
4126 GValue args[2] = { {0}, {0} };
4131 g_value_init (&ret, G_TYPE_BOOLEAN);
4132 g_value_set_boolean (&ret, TRUE);
4133 g_value_init (&args[0], GST_TYPE_PAD);
4134 g_value_set_object (&args[0], pad);
4135 g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4136 gst_value_set_mini_object (&args[1], obj);
4138 if (GST_IS_EVENT (obj))
4139 detail = event_quark;
4141 detail = buffer_quark;
4144 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4145 res = g_value_get_boolean (&ret);
4148 g_value_unset (&ret);
4149 g_value_unset (&args[0]);
4150 g_value_unset (&args[1]);
4156 gst_pad_data_unref (gboolean is_buffer, void *data)
4158 if (G_LIKELY (is_buffer)) {
4159 gst_buffer_unref (data);
4161 gst_buffer_list_unref (data);
4166 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4170 if (G_LIKELY (is_buffer)) {
4171 caps = GST_BUFFER_CAPS (data);
4175 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4176 caps = GST_BUFFER_CAPS (buf);
4183 /* this is the chain function that does not perform the additional argument
4184 * checking for that little extra speed.
4186 static inline GstFlowReturn
4187 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
4188 GstPadPushCache * cache)
4191 gboolean caps_changed;
4193 gboolean emit_signal;
4195 GST_PAD_STREAM_LOCK (pad);
4197 GST_OBJECT_LOCK (pad);
4198 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4201 caps = gst_pad_data_get_caps (is_buffer, data);
4202 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4204 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4205 GST_OBJECT_UNLOCK (pad);
4207 /* see if the signal should be emited, we emit before caps nego as
4208 * we might drop the buffer and do capsnego for nothing. */
4209 if (G_UNLIKELY (emit_signal)) {
4211 if (G_LIKELY (is_buffer)) {
4212 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4215 /* chain all groups in the buffer list one by one to avoid problems with
4216 * buffer probes that push buffers or events */
4221 /* we got a new datatype on the pad, see if it can handle it */
4222 if (G_UNLIKELY (caps_changed)) {
4223 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4224 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4225 goto not_negotiated;
4228 /* NOTE: we read the chainfunc unlocked.
4229 * we cannot hold the lock for the pad so we might send
4230 * the data to the wrong function. This is not really a
4231 * problem since functions are assigned at creation time
4232 * and don't change that often... */
4233 if (G_LIKELY (is_buffer)) {
4234 GstPadChainFunction chainfunc;
4236 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4239 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4240 "calling chainfunction &%s with buffer %p",
4241 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
4244 cache->peer = gst_object_ref (pad);
4245 cache->caps = caps ? gst_caps_ref (caps) : NULL;
4248 ret = chainfunc (pad, GST_BUFFER_CAST (data));
4250 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4251 "called chainfunction &%s with buffer %p, returned %s",
4252 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4254 GstPadChainListFunction chainlistfunc;
4256 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4259 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4260 "calling chainlistfunction &%s",
4261 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4263 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4265 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4266 "called chainlistfunction &%s, returned %s",
4267 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4270 GST_PAD_STREAM_UNLOCK (pad);
4276 GstBufferList *list;
4277 GstBufferListIterator *it;
4280 GST_PAD_STREAM_UNLOCK (pad);
4282 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4284 list = GST_BUFFER_LIST_CAST (data);
4285 it = gst_buffer_list_iterate (list);
4287 if (gst_buffer_list_iterator_next_group (it)) {
4289 group = gst_buffer_list_iterator_merge_group (it);
4290 if (group == NULL) {
4291 group = gst_buffer_new ();
4292 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4294 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4296 ret = gst_pad_chain_data_unchecked (pad, TRUE, group, NULL);
4297 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4299 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4300 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new (), NULL);
4303 gst_buffer_list_iterator_free (it);
4304 gst_buffer_list_unref (list);
4312 gst_pad_data_unref (is_buffer, data);
4313 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4314 "pushing, but pad was flushing");
4315 GST_OBJECT_UNLOCK (pad);
4316 GST_PAD_STREAM_UNLOCK (pad);
4317 return GST_FLOW_WRONG_STATE;
4321 gst_pad_data_unref (is_buffer, data);
4322 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4323 GST_PAD_STREAM_UNLOCK (pad);
4328 gst_pad_data_unref (is_buffer, data);
4329 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4330 "pushing data but pad did not accept");
4331 GST_PAD_STREAM_UNLOCK (pad);
4332 return GST_FLOW_NOT_NEGOTIATED;
4336 gst_pad_data_unref (is_buffer, data);
4337 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4338 "pushing, but not chainhandler");
4339 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4340 ("push on pad %s:%s but it has no chainfunction",
4341 GST_DEBUG_PAD_NAME (pad)));
4342 GST_PAD_STREAM_UNLOCK (pad);
4343 return GST_FLOW_NOT_SUPPORTED;
4349 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4350 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4353 * Chain a buffer to @pad.
4355 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4357 * If the caps on @buffer are different from the current caps on @pad, this
4358 * function will call any setcaps function (see gst_pad_set_setcaps_function())
4359 * installed on @pad. If the new caps are not acceptable for @pad, this
4360 * function returns #GST_FLOW_NOT_NEGOTIATED.
4362 * The function proceeds calling the chain function installed on @pad (see
4363 * gst_pad_set_chain_function()) and the return value of that function is
4364 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4367 * In all cases, success or failure, the caller loses its reference to @buffer
4368 * after calling this function.
4370 * Returns: a #GstFlowReturn from the pad.
4375 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4377 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4378 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4379 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4381 return gst_pad_chain_data_unchecked (pad, TRUE, buffer, NULL);
4385 * gst_pad_chain_list:
4386 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4387 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4390 * Chain a bufferlist to @pad.
4392 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4394 * If the caps on the first buffer of @list are different from the current
4395 * caps on @pad, this function will call any setcaps function
4396 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4397 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4399 * The function proceeds calling the chainlist function installed on @pad (see
4400 * gst_pad_set_chain_list_function()) and the return value of that function is
4401 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4402 * chainlist function.
4404 * In all cases, success or failure, the caller loses its reference to @list
4405 * after calling this function.
4409 * Returns: a #GstFlowReturn from the pad.
4414 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4416 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4417 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4418 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4420 return gst_pad_chain_data_unchecked (pad, FALSE, list, NULL);
4423 static GstFlowReturn
4424 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
4425 GstPadPushCache * cache)
4430 gboolean caps_changed;
4432 GST_OBJECT_LOCK (pad);
4434 /* FIXME: this check can go away; pad_set_blocked could be implemented with
4435 * probes completely or probes with an extended pad block. */
4436 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4437 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4440 /* we emit signals on the pad arg, the peer will have a chance to
4441 * emit in the _chain() function */
4442 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4444 /* unlock before emitting */
4445 GST_OBJECT_UNLOCK (pad);
4447 if (G_LIKELY (is_buffer)) {
4448 /* if the signal handler returned FALSE, it means we should just drop the
4450 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4453 /* push all buffers in the list */
4456 GST_OBJECT_LOCK (pad);
4459 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4462 /* Before pushing the buffer to the peer pad, ensure that caps
4463 * are set on this pad */
4464 caps = gst_pad_data_get_caps (is_buffer, data);
4465 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4467 /* take ref to peer pad before releasing the lock */
4468 gst_object_ref (peer);
4469 GST_OBJECT_UNLOCK (pad);
4471 /* we got a new datatype from the pad, it had better handle it */
4472 if (G_UNLIKELY (caps_changed)) {
4473 GST_DEBUG_OBJECT (pad,
4474 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4475 GST_PAD_CAPS (pad), caps, caps);
4476 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
4477 goto not_negotiated;
4480 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, cache);
4482 gst_object_unref (peer);
4488 GstBufferList *list;
4489 GstBufferListIterator *it;
4492 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4494 list = GST_BUFFER_LIST_CAST (data);
4495 it = gst_buffer_list_iterate (list);
4497 if (gst_buffer_list_iterator_next_group (it)) {
4499 group = gst_buffer_list_iterator_merge_group (it);
4500 if (group == NULL) {
4501 group = gst_buffer_new ();
4502 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4504 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4506 ret = gst_pad_push_data (pad, TRUE, group, NULL);
4507 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4509 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4510 ret = gst_pad_push_data (pad, TRUE, gst_buffer_new (), NULL);
4513 gst_buffer_list_iterator_free (it);
4514 gst_buffer_list_unref (list);
4519 /* ERROR recovery here */
4522 gst_pad_data_unref (is_buffer, data);
4523 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4524 GST_OBJECT_UNLOCK (pad);
4529 gst_pad_data_unref (is_buffer, data);
4530 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4535 gst_pad_data_unref (is_buffer, data);
4536 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4537 "pushing, but it was not linked");
4538 GST_OBJECT_UNLOCK (pad);
4539 return GST_FLOW_NOT_LINKED;
4543 gst_pad_data_unref (is_buffer, data);
4544 gst_object_unref (peer);
4545 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4546 "element pushed data then refused to accept the caps");
4547 return GST_FLOW_NOT_NEGOTIATED;
4551 static inline GstPadPushCache *
4552 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
4554 GstPadPushCache *cache;
4556 /* try to get the cached data */
4558 cache = g_atomic_pointer_get (cache_ptr);
4559 /* now try to replace the pointer with NULL to mark that we are busy
4561 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
4563 /* we could have a leftover invalid entry */
4564 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
4571 pad_free_cache (GstPadPushCache * cache)
4573 gst_object_unref (cache->peer);
4575 gst_caps_unref (cache->caps);
4576 g_slice_free (GstPadPushCache, cache);
4580 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
4583 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
4584 /* something changed, clean up our cache */
4585 pad_free_cache (cache);
4589 /* must be called with the pad lock */
4591 _priv_gst_pad_invalidate_cache (GstPad * pad)
4593 GstPadPushCache *cache;
4594 gpointer *cache_ptr;
4596 GST_LOG_OBJECT (pad, "Invalidating pad cache");
4598 /* we hold the pad lock here so we can get the peer and it stays
4599 * alive during this call */
4600 if (GST_PAD_IS_SINK (pad)) {
4601 if (!(pad = GST_PAD_PEER (pad)))
4605 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4607 /* try to get the cached data */
4609 cache = g_atomic_pointer_get (cache_ptr);
4610 /* now try to replace the pointer with INVALID. If nothing is busy with this
4611 * caps, we get the cache and clean it up. If something is busy, we replace
4612 * with INVALID so that when the function finishes and tries to put the
4613 * cache back, it'll fail and cleanup */
4614 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
4615 PAD_CACHE_INVALID));
4617 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
4618 pad_free_cache (cache);
4623 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4624 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4627 * Pushes a buffer to the peer of @pad.
4629 * This function will call an installed pad block before triggering any
4630 * installed pad probes.
4632 * If the caps on @buffer are different from the currently configured caps on
4633 * @pad, this function will call any installed setcaps function on @pad (see
4634 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4635 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4637 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4638 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4641 * In all cases, success or failure, the caller loses its reference to @buffer
4642 * after calling this function.
4644 * Returns: a #GstFlowReturn from the peer pad.
4649 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4651 GstPadPushCache *cache;
4653 gpointer *cache_ptr;
4657 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4658 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4659 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4661 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4663 cache = pad_take_cache (pad, cache_ptr);
4665 if (G_UNLIKELY (cache == NULL))
4669 caps = GST_BUFFER_CAPS (buffer);
4670 if (G_UNLIKELY (caps && caps != cache->caps)) {
4671 pad_free_cache (cache);
4677 GST_PAD_STREAM_LOCK (peer);
4678 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4681 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4682 "calling chainfunction &%s with buffer %p",
4683 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer);
4685 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
4687 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4688 "called chainfunction &%s with buffer %p, returned %s",
4689 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer,
4690 gst_flow_get_name (ret));
4692 GST_PAD_STREAM_UNLOCK (peer);
4694 pad_put_cache (pad, cache, cache_ptr);
4701 GstPadPushCache scache = { NULL, };
4703 GST_LOG_OBJECT (pad, "Taking slow path");
4705 ret = gst_pad_push_data (pad, TRUE, buffer, &scache);
4708 GstPadPushCache *ncache;
4710 GST_LOG_OBJECT (pad, "Caching push data");
4712 /* make cache structure */
4713 ncache = g_slice_new (GstPadPushCache);
4716 pad_put_cache (pad, ncache, cache_ptr);
4722 GST_PAD_STREAM_UNLOCK (peer);
4723 pad_free_cache (cache);
4729 * gst_pad_push_list:
4730 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4731 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4734 * Pushes a buffer list to the peer of @pad.
4736 * This function will call an installed pad block before triggering any
4737 * installed pad probes.
4739 * If the caps on the first buffer in the first group of @list are different
4740 * from the currently configured caps on @pad, this function will call any
4741 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4742 * case of failure to renegotiate the new format, this function returns
4743 * #GST_FLOW_NOT_NEGOTIATED.
4745 * If there are any probes installed on @pad every group of the buffer list
4746 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4747 * buffer list will be unreffed.
4749 * The function proceeds calling the chain function on the peer pad and returns
4750 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4751 * be returned. If the peer pad does not have any installed chainlist function
4752 * every group buffer of the list will be merged into a normal #GstBuffer and
4753 * chained via gst_pad_chain().
4755 * In all cases, success or failure, the caller loses its reference to @list
4756 * after calling this function.
4758 * Returns: a #GstFlowReturn from the peer pad.
4765 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4768 GstPadPushCache *cache;
4770 gpointer *cache_ptr;
4774 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4775 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4776 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4778 cache_ptr = (gpointer *) & pad->abidata.ABI.priv->cache_ptr;
4780 cache = pad_take_cache (pad, cache_ptr);
4782 if (G_UNLIKELY (cache == NULL))
4786 if ((buf = gst_buffer_list_get (list, 0, 0)))
4787 caps = GST_BUFFER_CAPS (buf);
4791 if (G_UNLIKELY (caps && caps != cache->caps)) {
4792 pad_free_cache (cache);
4798 GST_PAD_STREAM_LOCK (peer);
4799 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4802 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
4804 GST_PAD_STREAM_UNLOCK (peer);
4806 pad_put_cache (pad, cache, cache_ptr);
4813 GstPadPushCache scache = { NULL, };
4815 GST_LOG_OBJECT (pad, "Taking slow path");
4817 ret = gst_pad_push_data (pad, FALSE, list, &scache);
4820 GstPadPushCache *ncache;
4822 GST_LOG_OBJECT (pad, "Caching push data");
4824 /* make cache structure */
4825 ncache = g_slice_new (GstPadPushCache);
4828 pad_put_cache (pad, ncache, cache_ptr);
4834 GST_PAD_STREAM_UNLOCK (peer);
4835 pad_free_cache (cache);
4841 * gst_pad_check_pull_range:
4842 * @pad: a sink #GstPad.
4844 * Checks if a gst_pad_pull_range() can be performed on the peer
4845 * source pad. This function is used by plugins that want to check
4846 * if they can use random access on the peer source pad.
4848 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4849 * if it needs to perform some logic to determine if pull_range is
4852 * Returns: a gboolean with the result.
4857 gst_pad_check_pull_range (GstPad * pad)
4861 GstPadCheckGetRangeFunction checkgetrangefunc;
4863 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4865 GST_OBJECT_LOCK (pad);
4866 if (!GST_PAD_IS_SINK (pad))
4867 goto wrong_direction;
4869 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4872 gst_object_ref (peer);
4873 GST_OBJECT_UNLOCK (pad);
4875 /* see note in above function */
4876 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4877 /* FIXME, kindoff ghetto */
4878 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4879 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4880 "no checkgetrangefunc, assuming %d", ret);
4882 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4883 "calling checkgetrangefunc %s of peer pad %s:%s",
4884 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4886 ret = checkgetrangefunc (peer);
4889 gst_object_unref (peer);
4893 /* ERROR recovery here */
4896 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4897 "checking pull range, but pad must be a sinkpad");
4898 GST_OBJECT_UNLOCK (pad);
4903 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4904 "checking pull range, but it was not linked");
4905 GST_OBJECT_UNLOCK (pad);
4910 static GstFlowReturn
4911 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4912 GstBuffer ** buffer)
4915 GstPadGetRangeFunction getrangefunc;
4916 gboolean emit_signal;
4918 gboolean caps_changed;
4920 GST_PAD_STREAM_LOCK (pad);
4922 GST_OBJECT_LOCK (pad);
4923 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4926 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4927 GST_OBJECT_UNLOCK (pad);
4929 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4932 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4933 "calling getrangefunc %s, offset %"
4934 G_GUINT64_FORMAT ", size %u",
4935 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4937 ret = getrangefunc (pad, offset, size, buffer);
4939 /* can only fire the signal if we have a valid buffer */
4940 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4941 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4945 GST_PAD_STREAM_UNLOCK (pad);
4947 if (G_UNLIKELY (ret != GST_FLOW_OK))
4948 goto get_range_failed;
4950 GST_OBJECT_LOCK (pad);
4951 /* Before pushing the buffer to the peer pad, ensure that caps
4952 * are set on this pad */
4953 caps = GST_BUFFER_CAPS (*buffer);
4954 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4955 GST_OBJECT_UNLOCK (pad);
4957 if (G_UNLIKELY (caps_changed)) {
4958 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4959 /* this should usually work because the element produced the buffer */
4960 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4961 goto not_negotiated;
4968 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4969 "pulling range, but pad was flushing");
4970 GST_OBJECT_UNLOCK (pad);
4971 GST_PAD_STREAM_UNLOCK (pad);
4972 return GST_FLOW_WRONG_STATE;
4976 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4977 ("pullrange on pad %s:%s but it has no getrangefunction",
4978 GST_DEBUG_PAD_NAME (pad)));
4979 GST_PAD_STREAM_UNLOCK (pad);
4980 return GST_FLOW_NOT_SUPPORTED;
4984 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4985 "Dropping data after FALSE probe return");
4986 GST_PAD_STREAM_UNLOCK (pad);
4987 gst_buffer_unref (*buffer);
4989 return GST_FLOW_UNEXPECTED;
4994 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4995 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4996 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
5001 gst_buffer_unref (*buffer);
5003 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
5004 "getrange returned buffer of unaccaptable caps");
5005 return GST_FLOW_NOT_NEGOTIATED;
5010 * gst_pad_get_range:
5011 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
5012 * @offset: The start offset of the buffer
5013 * @size: The length of the buffer
5014 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
5015 * returns #GST_FLOW_ERROR if %NULL.
5017 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
5018 * immediatly and @buffer is %NULL.
5020 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
5021 * description of a getrange function. If @pad has no getrange function
5022 * installed (see gst_pad_set_getrange_function()) this function returns
5023 * #GST_FLOW_NOT_SUPPORTED.
5025 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
5027 * Returns: a #GstFlowReturn from the pad.
5032 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
5033 GstBuffer ** buffer)
5035 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5036 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
5037 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5039 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
5043 * gst_pad_pull_range:
5044 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
5045 * @offset: The start offset of the buffer
5046 * @size: The length of the buffer
5047 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
5048 * GST_FLOW_ERROR if %NULL.
5050 * Pulls a @buffer from the peer pad.
5052 * This function will first trigger the pad block signal if it was
5055 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
5056 * function returns the result of gst_pad_get_range() on the peer pad.
5057 * See gst_pad_get_range() for a list of return values and for the
5058 * semantics of the arguments of this function.
5060 * @buffer's caps must either be unset or the same as what is already
5061 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
5064 * Returns: a #GstFlowReturn from the peer pad.
5065 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
5066 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
5067 * @buffer may not be used or freed when any other return value than
5068 * #GST_FLOW_OK is returned.
5073 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
5074 GstBuffer ** buffer)
5078 gboolean emit_signal;
5080 gboolean caps_changed;
5082 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5083 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
5084 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5086 GST_OBJECT_LOCK (pad);
5088 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
5089 handle_pad_block (pad);
5091 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
5094 /* signal emision for the pad, peer has chance to emit when
5095 * we call _get_range() */
5096 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
5098 gst_object_ref (peer);
5099 GST_OBJECT_UNLOCK (pad);
5101 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
5103 gst_object_unref (peer);
5105 if (G_UNLIKELY (ret != GST_FLOW_OK))
5106 goto pull_range_failed;
5108 /* can only fire the signal if we have a valid buffer */
5109 if (G_UNLIKELY (emit_signal)) {
5110 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
5114 GST_OBJECT_LOCK (pad);
5115 /* Before pushing the buffer to the peer pad, ensure that caps
5116 * are set on this pad */
5117 caps = GST_BUFFER_CAPS (*buffer);
5118 caps_changed = caps && caps != GST_PAD_CAPS (pad);
5119 GST_OBJECT_UNLOCK (pad);
5121 /* we got a new datatype on the pad, see if it can handle it */
5122 if (G_UNLIKELY (caps_changed)) {
5123 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
5124 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
5125 goto not_negotiated;
5129 /* ERROR recovery here */
5132 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5133 "pulling range, but it was not linked");
5134 GST_OBJECT_UNLOCK (pad);
5135 return GST_FLOW_NOT_LINKED;
5140 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5141 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5142 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
5147 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5148 "Dropping data after FALSE probe return");
5149 gst_buffer_unref (*buffer);
5151 return GST_FLOW_UNEXPECTED;
5155 gst_buffer_unref (*buffer);
5157 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
5158 "pullrange returned buffer of different caps");
5159 return GST_FLOW_NOT_NEGOTIATED;
5164 * gst_pad_push_event:
5165 * @pad: a #GstPad to push the event to.
5166 * @event: (transfer full): the #GstEvent to send to the pad.
5168 * Sends the event to the peer of the given pad. This function is
5169 * mainly used by elements to send events to their peer
5172 * This function takes owership of the provided event so you should
5173 * gst_event_ref() it if you want to reuse the event after this call.
5175 * Returns: TRUE if the event was handled.
5180 gst_pad_push_event (GstPad * pad, GstEvent * event)
5185 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5186 g_return_val_if_fail (event != NULL, FALSE);
5187 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5189 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
5191 GST_OBJECT_LOCK (pad);
5193 /* Two checks to be made:
5194 * . (un)set the FLUSHING flag for flushing events,
5195 * . handle pad blocking */
5196 switch (GST_EVENT_TYPE (event)) {
5197 case GST_EVENT_FLUSH_START:
5198 _priv_gst_pad_invalidate_cache (pad);
5199 GST_PAD_SET_FLUSHING (pad);
5201 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5202 /* flush start will have set the FLUSHING flag and will then
5203 * unlock all threads doing a GCond wait on the blocking pad. This
5204 * will typically unblock the STREAMING thread blocked on a pad. */
5205 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
5206 "doing block signal.");
5207 GST_PAD_BLOCK_BROADCAST (pad);
5211 case GST_EVENT_FLUSH_STOP:
5212 GST_PAD_UNSET_FLUSHING (pad);
5214 /* if we are blocked, flush away the FLUSH_STOP event */
5215 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5216 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
5221 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
5222 /* block the event as long as the pad is blocked */
5223 if (handle_pad_block (pad) != GST_FLOW_OK)
5229 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5230 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5231 GST_EVENT_SRC (event) = gst_object_ref (pad);
5234 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5235 GST_OBJECT_UNLOCK (pad);
5237 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
5240 GST_OBJECT_LOCK (pad);
5242 peerpad = GST_PAD_PEER (pad);
5243 if (peerpad == NULL)
5246 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
5247 GST_EVENT_TYPE_NAME (event), peerpad);
5248 gst_object_ref (peerpad);
5249 GST_OBJECT_UNLOCK (pad);
5251 result = gst_pad_send_event (peerpad, event);
5253 /* Note: we gave away ownership of the event at this point */
5254 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
5256 gst_object_unref (peerpad);
5260 /* ERROR handling */
5263 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5264 gst_event_unref (event);
5269 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
5270 gst_event_unref (event);
5271 GST_OBJECT_UNLOCK (pad);
5276 GST_DEBUG_OBJECT (pad,
5277 "Not forwarding event since we're flushing and blocking");
5278 gst_event_unref (event);
5279 GST_OBJECT_UNLOCK (pad);
5285 * gst_pad_send_event:
5286 * @pad: a #GstPad to send the event to.
5287 * @event: (transfer full): the #GstEvent to send to the pad.
5289 * Sends the event to the pad. This function can be used
5290 * by applications to send events in the pipeline.
5292 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5293 * sink pad, @event should be a downstream event. For example, you would not
5294 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5295 * Furthermore, some downstream events have to be serialized with data flow,
5296 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5297 * the event needs to be serialized with data flow, this function will take the
5298 * pad's stream lock while calling its event function.
5300 * To find out whether an event type is upstream, downstream, or downstream and
5301 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5302 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5303 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5304 * plugin doesn't need to bother itself with this information; the core handles
5305 * all necessary locks and checks.
5307 * This function takes owership of the provided event so you should
5308 * gst_event_ref() it if you want to reuse the event after this call.
5310 * Returns: TRUE if the event was handled.
5313 gst_pad_send_event (GstPad * pad, GstEvent * event)
5315 gboolean result = FALSE;
5316 GstPadEventFunction eventfunc;
5317 gboolean serialized, need_unlock = FALSE;
5319 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5320 g_return_val_if_fail (event != NULL, FALSE);
5322 GST_OBJECT_LOCK (pad);
5323 if (GST_PAD_IS_SINK (pad)) {
5324 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5325 goto wrong_direction;
5326 serialized = GST_EVENT_IS_SERIALIZED (event);
5327 } else if (GST_PAD_IS_SRC (pad)) {
5328 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5329 goto wrong_direction;
5330 /* events on srcpad never are serialized */
5333 goto unknown_direction;
5335 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5336 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5337 GST_EVENT_SRC (event) = gst_object_ref (pad);
5341 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5342 GST_OBJECT_UNLOCK (pad);
5344 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
5347 GST_OBJECT_LOCK (pad);
5350 switch (GST_EVENT_TYPE (event)) {
5351 case GST_EVENT_FLUSH_START:
5352 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5353 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5355 /* can't even accept a flush begin event when flushing */
5356 if (GST_PAD_IS_FLUSHING (pad))
5359 _priv_gst_pad_invalidate_cache (pad);
5360 GST_PAD_SET_FLUSHING (pad);
5361 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5363 case GST_EVENT_FLUSH_STOP:
5364 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
5365 GST_PAD_UNSET_FLUSHING (pad);
5366 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5368 GST_OBJECT_UNLOCK (pad);
5369 /* grab stream lock */
5370 GST_PAD_STREAM_LOCK (pad);
5372 GST_OBJECT_LOCK (pad);
5375 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5376 GST_EVENT_TYPE_NAME (event));
5378 /* make this a little faster, no point in grabbing the lock
5379 * if the pad is allready flushing. */
5380 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5384 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5385 GST_OBJECT_UNLOCK (pad);
5386 GST_PAD_STREAM_LOCK (pad);
5388 GST_OBJECT_LOCK (pad);
5389 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5394 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5397 GST_OBJECT_UNLOCK (pad);
5399 result = eventfunc (pad, event);
5402 GST_PAD_STREAM_UNLOCK (pad);
5404 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5408 /* ERROR handling */
5411 g_warning ("pad %s:%s sending %s event in wrong direction",
5412 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5413 GST_OBJECT_UNLOCK (pad);
5414 gst_event_unref (event);
5419 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5420 GST_OBJECT_UNLOCK (pad);
5421 gst_event_unref (event);
5426 g_warning ("pad %s:%s has no event handler, file a bug.",
5427 GST_DEBUG_PAD_NAME (pad));
5428 GST_OBJECT_UNLOCK (pad);
5430 GST_PAD_STREAM_UNLOCK (pad);
5431 gst_event_unref (event);
5436 GST_OBJECT_UNLOCK (pad);
5438 GST_PAD_STREAM_UNLOCK (pad);
5439 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5440 "Received event on flushing pad. Discarding");
5441 gst_event_unref (event);
5446 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5447 gst_event_unref (event);
5453 * gst_pad_set_element_private:
5454 * @pad: the #GstPad to set the private data of.
5455 * @priv: The private data to attach to the pad.
5457 * Set the given private data gpointer on the pad.
5458 * This function can only be used by the element that owns the pad.
5459 * No locking is performed in this function.
5462 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5464 pad->element_private = priv;
5468 * gst_pad_get_element_private:
5469 * @pad: the #GstPad to get the private data of.
5471 * Gets the private data of a pad.
5472 * No locking is performed in this function.
5474 * Returns: (transfer none): a #gpointer to the private data.
5477 gst_pad_get_element_private (GstPad * pad)
5479 return pad->element_private;
5483 do_stream_status (GstPad * pad, GstStreamStatusType type,
5484 GThread * thread, GstTask * task)
5488 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5490 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5491 if (GST_IS_ELEMENT (parent)) {
5492 GstMessage *message;
5493 GValue value = { 0 };
5495 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5496 gchar *tname, *ename, *pname;
5498 /* create a good task name */
5499 ename = gst_element_get_name (parent);
5500 pname = gst_pad_get_name (pad);
5501 tname = g_strdup_printf ("%s:%s", ename, pname);
5505 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5509 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5512 g_value_init (&value, GST_TYPE_TASK);
5513 g_value_set_object (&value, task);
5514 gst_message_set_stream_status_object (message, &value);
5515 g_value_unset (&value);
5517 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5518 gst_element_post_message (parent, message);
5520 gst_object_unref (parent);
5525 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5527 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5532 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5534 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5538 static GstTaskThreadCallbacks thr_callbacks = {
5544 * gst_pad_start_task:
5545 * @pad: the #GstPad to start the task of
5546 * @func: the task function to call
5547 * @data: data passed to the task function
5549 * Starts a task that repeatedly calls @func with @data. This function
5550 * is mostly used in pad activation functions to start the dataflow.
5551 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5552 * before @func is called.
5554 * Returns: a %TRUE if the task could be started.
5557 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5562 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5563 g_return_val_if_fail (func != NULL, FALSE);
5565 GST_DEBUG_OBJECT (pad, "start task");
5567 GST_OBJECT_LOCK (pad);
5568 task = GST_PAD_TASK (pad);
5570 task = gst_task_create (func, data);
5571 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5572 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5573 GST_DEBUG_OBJECT (pad, "created task");
5574 GST_PAD_TASK (pad) = task;
5575 gst_object_ref (task);
5576 /* release lock to post the message */
5577 GST_OBJECT_UNLOCK (pad);
5579 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5581 gst_object_unref (task);
5583 GST_OBJECT_LOCK (pad);
5584 /* nobody else is supposed to have changed the pad now */
5585 if (GST_PAD_TASK (pad) != task)
5586 goto concurrent_stop;
5588 res = gst_task_set_state (task, GST_TASK_STARTED);
5589 GST_OBJECT_UNLOCK (pad);
5596 GST_OBJECT_UNLOCK (pad);
5602 * gst_pad_pause_task:
5603 * @pad: the #GstPad to pause the task of
5605 * Pause the task of @pad. This function will also wait until the
5606 * function executed by the task is finished if this function is not
5607 * called from the task function.
5609 * Returns: a TRUE if the task could be paused or FALSE when the pad
5613 gst_pad_pause_task (GstPad * pad)
5618 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5620 GST_DEBUG_OBJECT (pad, "pause task");
5622 GST_OBJECT_LOCK (pad);
5623 task = GST_PAD_TASK (pad);
5626 res = gst_task_set_state (task, GST_TASK_PAUSED);
5627 GST_OBJECT_UNLOCK (pad);
5629 /* wait for task function to finish, this lock is recursive so it does nothing
5630 * when the pause is called from the task itself */
5631 GST_PAD_STREAM_LOCK (pad);
5632 GST_PAD_STREAM_UNLOCK (pad);
5638 GST_DEBUG_OBJECT (pad, "pad has no task");
5639 GST_OBJECT_UNLOCK (pad);
5645 * gst_pad_stop_task:
5646 * @pad: the #GstPad to stop the task of
5648 * Stop the task of @pad. This function will also make sure that the
5649 * function executed by the task will effectively stop if not called
5650 * from the GstTaskFunction.
5652 * This function will deadlock if called from the GstTaskFunction of
5653 * the task. Use gst_task_pause() instead.
5655 * Regardless of whether the pad has a task, the stream lock is acquired and
5656 * released so as to ensure that streaming through this pad has finished.
5658 * Returns: a TRUE if the task could be stopped or FALSE on error.
5661 gst_pad_stop_task (GstPad * pad)
5666 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5668 GST_DEBUG_OBJECT (pad, "stop task");
5670 GST_OBJECT_LOCK (pad);
5671 task = GST_PAD_TASK (pad);
5674 GST_PAD_TASK (pad) = NULL;
5675 res = gst_task_set_state (task, GST_TASK_STOPPED);
5676 GST_OBJECT_UNLOCK (pad);
5678 GST_PAD_STREAM_LOCK (pad);
5679 GST_PAD_STREAM_UNLOCK (pad);
5681 if (!gst_task_join (task))
5684 gst_object_unref (task);
5690 GST_DEBUG_OBJECT (pad, "no task");
5691 GST_OBJECT_UNLOCK (pad);
5693 GST_PAD_STREAM_LOCK (pad);
5694 GST_PAD_STREAM_UNLOCK (pad);
5696 /* this is not an error */
5701 /* this is bad, possibly the application tried to join the task from
5702 * the task's thread. We install the task again so that it will be stopped
5703 * again from the right thread next time hopefully. */
5704 GST_OBJECT_LOCK (pad);
5705 GST_DEBUG_OBJECT (pad, "join failed");
5706 /* we can only install this task if there was no other task */
5707 if (GST_PAD_TASK (pad) == NULL)
5708 GST_PAD_TASK (pad) = task;
5709 GST_OBJECT_UNLOCK (pad);