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 struct _GstPadPrivate
115 GstPadPushCache *cache_ptr;
118 static void gst_pad_dispose (GObject * object);
119 static void gst_pad_finalize (GObject * object);
120 static void gst_pad_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_pad_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static GstFlowReturn handle_pad_block (GstPad * pad);
126 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
127 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
128 static gboolean gst_pad_activate_default (GstPad * pad);
129 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
131 static GstObjectClass *parent_class = NULL;
132 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
134 static GParamSpec *pspec_caps = NULL;
136 /* quarks for probe signals */
137 static GQuark buffer_quark;
138 static GQuark event_quark;
147 static GstFlowQuarks flow_quarks[] = {
148 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
149 {GST_FLOW_RESEND, "resend", 0},
150 {GST_FLOW_OK, "ok", 0},
151 {GST_FLOW_NOT_LINKED, "not-linked", 0},
152 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
153 {GST_FLOW_UNEXPECTED, "unexpected", 0},
154 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
155 {GST_FLOW_ERROR, "error", 0},
156 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
157 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
162 * @ret: a #GstFlowReturn to get the name of.
164 * Gets a string representing the given flow return.
166 * Returns: a static string with the name of the flow return.
168 G_CONST_RETURN gchar *
169 gst_flow_get_name (GstFlowReturn ret)
173 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
175 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
176 if (ret == flow_quarks[i].ret)
177 return flow_quarks[i].name;
184 * @ret: a #GstFlowReturn to get the quark of.
186 * Get the unique quark for the given GstFlowReturn.
188 * Returns: the quark associated with the flow return or 0 if an
189 * invalid return was specified.
192 gst_flow_to_quark (GstFlowReturn ret)
196 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
198 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
199 if (ret == flow_quarks[i].ret)
200 return flow_quarks[i].quark;
209 buffer_quark = g_quark_from_static_string ("buffer"); \
210 event_quark = g_quark_from_static_string ("event"); \
212 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
213 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
216 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
217 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
220 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
223 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
224 GValue * return_accu, const GValue * handler_return, gpointer dummy)
226 gboolean ret = g_value_get_boolean (handler_return);
228 GST_DEBUG ("accumulated %d", ret);
229 g_value_set_boolean (return_accu, ret);
235 default_have_data (GstPad * pad, GstMiniObject * o)
241 gst_pad_class_init (GstPadClass * klass)
243 GObjectClass *gobject_class;
244 GstObjectClass *gstobject_class;
246 gobject_class = G_OBJECT_CLASS (klass);
247 gstobject_class = GST_OBJECT_CLASS (klass);
249 g_type_class_add_private (klass, sizeof (GstPadPrivate));
251 parent_class = g_type_class_peek_parent (klass);
253 gobject_class->dispose = gst_pad_dispose;
254 gobject_class->finalize = gst_pad_finalize;
255 gobject_class->set_property = gst_pad_set_property;
256 gobject_class->get_property = gst_pad_get_property;
260 * @pad: the pad that emitted the signal
261 * @peer: the peer pad that has been connected
263 * Signals that a pad has been linked to the peer pad.
265 gst_pad_signals[PAD_LINKED] =
266 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
267 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
268 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
271 * @pad: the pad that emitted the signal
272 * @peer: the peer pad that has been disconnected
274 * Signals that a pad has been unlinked from the peer pad.
276 gst_pad_signals[PAD_UNLINKED] =
277 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
278 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
279 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
281 * GstPad::request-link:
282 * @pad: the pad that emitted the signal
283 * @peer: the peer pad for which a connection is requested
285 * Signals that a pad connection has been requested.
287 gst_pad_signals[PAD_REQUEST_LINK] =
288 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
289 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
290 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
294 * @pad: the pad that emitted the signal
295 * @mini_obj: new data
297 * Signals that new data is available on the pad. This signal is used
298 * internally for implementing pad probes.
299 * See gst_pad_add_*_probe functions.
301 * Returns: %TRUE to keep the data, %FALSE to drop it
303 gst_pad_signals[PAD_HAVE_DATA] =
304 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
305 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
306 G_STRUCT_OFFSET (GstPadClass, have_data),
307 _gst_do_pass_data_accumulator,
308 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
310 pspec_caps = g_param_spec_boxed ("caps", "Caps",
311 "The capabilities of the pad", GST_TYPE_CAPS,
312 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
313 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
315 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
316 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
317 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
318 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
319 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
320 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
321 g_param_spec_object ("template", "Template",
322 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
323 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 gstobject_class->path_string_separator = ".";
327 /* Register common function pointer descriptions */
328 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
329 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
330 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
331 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
332 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
333 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
335 /* from gstutils.c */
336 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_fixed_caps_func);
338 klass->have_data = default_have_data;
342 gst_pad_init (GstPad * pad)
344 pad->priv = GST_PAD_GET_PRIVATE (pad);
346 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
347 GST_PAD_PEER (pad) = NULL;
349 GST_PAD_CHAINFUNC (pad) = NULL;
351 GST_PAD_LINKFUNC (pad) = NULL;
353 GST_PAD_CAPS (pad) = NULL;
354 GST_PAD_GETCAPSFUNC (pad) = NULL;
356 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
357 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
358 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
359 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
360 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
362 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
364 pad->do_buffer_signals = 0;
365 pad->do_event_signals = 0;
367 GST_PAD_SET_FLUSHING (pad);
369 /* FIXME 0.11: Store this directly in the instance struct */
370 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
371 g_static_rec_mutex_init (pad->stream_rec_lock);
373 pad->block_cond = g_cond_new ();
377 gst_pad_dispose (GObject * object)
379 GstPad *pad = GST_PAD_CAST (object);
382 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
384 /* unlink the peer pad */
385 if ((peer = gst_pad_get_peer (pad))) {
386 /* window for MT unsafeness, someone else could unlink here
387 * and then we call unlink with wrong pads. The unlink
388 * function would catch this and safely return failed. */
389 if (GST_PAD_IS_SRC (pad))
390 gst_pad_unlink (pad, peer);
392 gst_pad_unlink (peer, pad);
394 gst_object_unref (peer);
398 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
400 gst_pad_set_pad_template (pad, NULL);
402 if (pad->block_destroy_data && pad->block_data) {
403 pad->block_destroy_data (pad->block_data);
404 pad->block_data = NULL;
407 G_OBJECT_CLASS (parent_class)->dispose (object);
411 gst_pad_finalize (GObject * object)
413 GstPad *pad = GST_PAD_CAST (object);
416 /* in case the task is still around, clean it up */
417 if ((task = GST_PAD_TASK (pad))) {
418 gst_task_join (task);
419 GST_PAD_TASK (pad) = NULL;
420 gst_object_unref (task);
423 if (pad->stream_rec_lock) {
424 g_static_rec_mutex_free (pad->stream_rec_lock);
425 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
426 pad->stream_rec_lock = NULL;
428 if (pad->block_cond) {
429 g_cond_free (pad->block_cond);
430 pad->block_cond = NULL;
433 G_OBJECT_CLASS (parent_class)->finalize (object);
437 gst_pad_set_property (GObject * object, guint prop_id,
438 const GValue * value, GParamSpec * pspec)
440 g_return_if_fail (GST_IS_PAD (object));
443 case PAD_PROP_DIRECTION:
444 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
446 case PAD_PROP_TEMPLATE:
447 gst_pad_set_pad_template (GST_PAD_CAST (object),
448 (GstPadTemplate *) g_value_get_object (value));
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gst_pad_get_property (GObject * object, guint prop_id,
458 GValue * value, GParamSpec * pspec)
460 g_return_if_fail (GST_IS_PAD (object));
464 GST_OBJECT_LOCK (object);
465 g_value_set_boxed (value, GST_PAD_CAPS (object));
466 GST_OBJECT_UNLOCK (object);
468 case PAD_PROP_DIRECTION:
469 g_value_set_enum (value, GST_PAD_DIRECTION (object));
471 case PAD_PROP_TEMPLATE:
472 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 * @name: the name of the new pad.
483 * @direction: the #GstPadDirection of the pad.
485 * Creates a new pad with the given name in the given direction.
486 * If name is NULL, a guaranteed unique name (across all pads)
488 * This function makes a copy of the name so you can safely free the name.
490 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
495 gst_pad_new (const gchar * name, GstPadDirection direction)
497 return g_object_new (GST_TYPE_PAD,
498 "name", name, "direction", direction, NULL);
502 * gst_pad_new_from_template:
503 * @templ: the pad template to use
504 * @name: the name of the element
506 * Creates a new pad with the given name from the given template.
507 * If name is NULL, a guaranteed unique name (across all pads)
509 * This function makes a copy of the name so you can safely free the name.
511 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
514 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
516 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
518 return g_object_new (GST_TYPE_PAD,
519 "name", name, "direction", templ->direction, "template", templ, NULL);
523 * gst_pad_new_from_static_template:
524 * @templ: the #GstStaticPadTemplate to use
525 * @name: the name of the element
527 * Creates a new pad with the given name from the given static template.
528 * If name is NULL, a guaranteed unique name (across all pads)
530 * This function makes a copy of the name so you can safely free the name.
532 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
535 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
539 GstPadTemplate *template;
541 template = gst_static_pad_template_get (templ);
542 pad = gst_pad_new_from_template (template, name);
543 gst_object_unref (template);
548 * gst_pad_get_direction:
549 * @pad: a #GstPad to get the direction of.
551 * Gets the direction of the pad. The direction of the pad is
552 * decided at construction time so this function does not take
555 * Returns: the #GstPadDirection of the pad.
560 gst_pad_get_direction (GstPad * pad)
562 GstPadDirection result;
564 /* PAD_UNKNOWN is a little silly but we need some sort of
565 * error return value */
566 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
568 result = GST_PAD_DIRECTION (pad);
574 gst_pad_activate_default (GstPad * pad)
576 return gst_pad_activate_push (pad, TRUE);
580 pre_activate (GstPad * pad, GstActivateMode new_mode)
583 case GST_ACTIVATE_PUSH:
584 case GST_ACTIVATE_PULL:
585 GST_OBJECT_LOCK (pad);
586 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
588 GST_PAD_UNSET_FLUSHING (pad);
589 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
590 GST_OBJECT_UNLOCK (pad);
592 case GST_ACTIVATE_NONE:
593 GST_OBJECT_LOCK (pad);
594 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
595 _priv_gst_pad_invalidate_cache (pad);
596 GST_PAD_SET_FLUSHING (pad);
597 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
598 /* unlock blocked pads so element can resume and stop */
599 GST_PAD_BLOCK_BROADCAST (pad);
600 GST_OBJECT_UNLOCK (pad);
606 post_activate (GstPad * pad, GstActivateMode new_mode)
609 case GST_ACTIVATE_PUSH:
610 case GST_ACTIVATE_PULL:
613 case GST_ACTIVATE_NONE:
614 /* ensures that streaming stops */
615 GST_PAD_STREAM_LOCK (pad);
616 GST_DEBUG_OBJECT (pad, "stopped streaming");
617 GST_PAD_STREAM_UNLOCK (pad);
623 * gst_pad_set_active:
624 * @pad: the #GstPad to activate or deactivate.
625 * @active: whether or not the pad should be active.
627 * Activates or deactivates the given pad.
628 * Normally called from within core state change functions.
630 * If @active, makes sure the pad is active. If it is already active, either in
631 * push or pull mode, just return. Otherwise dispatches to the pad's activate
632 * function to perform the actual activation.
634 * If not @active, checks the pad's current mode and calls
635 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
638 * Returns: #TRUE if the operation was successful.
643 gst_pad_set_active (GstPad * pad, gboolean active)
646 gboolean ret = FALSE;
648 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
650 GST_OBJECT_LOCK (pad);
651 old = GST_PAD_ACTIVATE_MODE (pad);
652 GST_OBJECT_UNLOCK (pad);
656 case GST_ACTIVATE_PUSH:
657 GST_DEBUG_OBJECT (pad, "activating pad from push");
660 case GST_ACTIVATE_PULL:
661 GST_DEBUG_OBJECT (pad, "activating pad from pull");
664 case GST_ACTIVATE_NONE:
665 GST_DEBUG_OBJECT (pad, "activating pad from none");
666 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
671 case GST_ACTIVATE_PUSH:
672 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
673 ret = gst_pad_activate_push (pad, FALSE);
675 case GST_ACTIVATE_PULL:
676 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
677 ret = gst_pad_activate_pull (pad, FALSE);
679 case GST_ACTIVATE_NONE:
680 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
687 GST_OBJECT_LOCK (pad);
689 g_critical ("Failed to deactivate pad %s:%s, very bad",
690 GST_DEBUG_PAD_NAME (pad));
692 GST_WARNING_OBJECT (pad, "Failed to activate pad");
694 GST_OBJECT_UNLOCK (pad);
701 * gst_pad_activate_pull:
702 * @pad: the #GstPad to activate or deactivate.
703 * @active: whether or not the pad should be active.
705 * Activates or deactivates the given pad in pull mode via dispatching to the
706 * pad's activatepullfunc. For use from within pad activation functions only.
707 * When called on sink pads, will first proxy the call to the peer pad, which
708 * is expected to activate its internally linked pads from within its
709 * activate_pull function.
711 * If you don't know what this is, you probably don't want to call it.
713 * Returns: TRUE if the operation was successful.
718 gst_pad_activate_pull (GstPad * pad, gboolean active)
720 GstActivateMode old, new;
723 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
725 GST_OBJECT_LOCK (pad);
726 old = GST_PAD_ACTIVATE_MODE (pad);
727 GST_OBJECT_UNLOCK (pad);
731 case GST_ACTIVATE_PULL:
732 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
734 case GST_ACTIVATE_PUSH:
735 GST_DEBUG_OBJECT (pad,
736 "activating pad from push, deactivate push first");
737 /* pad was activate in the wrong direction, deactivate it
738 * and reactivate it in pull mode */
739 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
740 goto deactivate_failed;
741 /* fallthrough, pad is deactivated now. */
742 case GST_ACTIVATE_NONE:
743 GST_DEBUG_OBJECT (pad, "activating pad from none");
748 case GST_ACTIVATE_NONE:
749 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
751 case GST_ACTIVATE_PUSH:
752 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
753 /* pad was activated in the other direction, deactivate it
754 * in push mode, this should not happen... */
755 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
756 goto deactivate_failed;
757 /* everything is fine now */
759 case GST_ACTIVATE_PULL:
760 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
765 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
766 if ((peer = gst_pad_get_peer (pad))) {
767 GST_DEBUG_OBJECT (pad, "calling peer");
768 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
770 gst_object_unref (peer);
772 /* there is no peer, this is only fatal when we activate. When we
773 * deactivate, we must assume the application has unlinked the peer and
774 * will deactivate it eventually. */
778 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
781 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
782 goto failure; /* Can't activate pull on a src without a
786 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
787 pre_activate (pad, new);
789 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
790 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
793 /* can happen for sinks of passthrough elements */
796 post_activate (pad, new);
798 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
799 active ? "activated" : "deactivated");
805 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
806 active ? "activated" : "deactivated");
811 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
812 "failed to %s in switch to pull from mode %d",
813 (active ? "activate" : "deactivate"), old);
818 GST_OBJECT_LOCK (peer);
819 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
820 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
821 GST_OBJECT_UNLOCK (peer);
822 gst_object_unref (peer);
827 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
833 GST_OBJECT_LOCK (pad);
834 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
835 active ? "activate" : "deactivate");
836 _priv_gst_pad_invalidate_cache (pad);
837 GST_PAD_SET_FLUSHING (pad);
838 GST_PAD_ACTIVATE_MODE (pad) = old;
839 GST_OBJECT_UNLOCK (pad);
845 * gst_pad_activate_push:
846 * @pad: the #GstPad to activate or deactivate.
847 * @active: whether the pad should be active or not.
849 * Activates or deactivates the given pad in push mode via dispatching to the
850 * pad's activatepushfunc. For use from within pad activation functions only.
852 * If you don't know what this is, you probably don't want to call it.
854 * Returns: %TRUE if the operation was successful.
859 gst_pad_activate_push (GstPad * pad, gboolean active)
861 GstActivateMode old, new;
863 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
864 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
865 active ? "activated" : "deactivated");
867 GST_OBJECT_LOCK (pad);
868 old = GST_PAD_ACTIVATE_MODE (pad);
869 GST_OBJECT_UNLOCK (pad);
873 case GST_ACTIVATE_PUSH:
874 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
876 case GST_ACTIVATE_PULL:
877 GST_DEBUG_OBJECT (pad,
878 "activating pad from push, deactivating pull first");
879 /* pad was activate in the wrong direction, deactivate it
880 * an reactivate it in push mode */
881 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
882 goto deactivate_failed;
883 /* fallthrough, pad is deactivated now. */
884 case GST_ACTIVATE_NONE:
885 GST_DEBUG_OBJECT (pad, "activating pad from none");
890 case GST_ACTIVATE_NONE:
891 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
893 case GST_ACTIVATE_PULL:
894 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
895 /* pad was activated in the other direction, deactivate it
896 * in pull mode, this should not happen... */
897 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
898 goto deactivate_failed;
899 /* everything is fine now */
901 case GST_ACTIVATE_PUSH:
902 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
907 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
908 pre_activate (pad, new);
910 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
911 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
915 /* quite ok, element relies on state change func to prepare itself */
918 post_activate (pad, new);
920 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
921 active ? "activated" : "deactivated");
926 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
927 active ? "activated" : "deactivated");
932 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
933 "failed to %s in switch to push from mode %d",
934 (active ? "activate" : "deactivate"), old);
939 GST_OBJECT_LOCK (pad);
940 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
941 active ? "activate" : "deactivate");
942 _priv_gst_pad_invalidate_cache (pad);
943 GST_PAD_SET_FLUSHING (pad);
944 GST_PAD_ACTIVATE_MODE (pad) = old;
945 GST_OBJECT_UNLOCK (pad);
952 * @pad: the #GstPad to query
954 * Query if a pad is active
956 * Returns: TRUE if the pad is active.
961 gst_pad_is_active (GstPad * pad)
963 gboolean result = FALSE;
965 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
967 GST_OBJECT_LOCK (pad);
968 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
969 GST_OBJECT_UNLOCK (pad);
975 * gst_pad_set_blocked_async_full:
976 * @pad: the #GstPad to block or unblock
977 * @blocked: boolean indicating whether the pad should be blocked or unblocked
978 * @callback: #GstPadBlockCallback that will be called when the
980 * @user_data: (closure): user data passed to the callback
981 * @destroy_data: #GDestroyNotify for user_data
983 * Blocks or unblocks the dataflow on a pad. The provided callback
984 * is called when the operation succeeds; this happens right before the next
985 * attempt at pushing a buffer on the pad.
987 * This can take a while as the pad can only become blocked when real dataflow
989 * When the pipeline is stalled, for example in PAUSED, this can
990 * take an indeterminate amount of time.
991 * You can pass NULL as the callback to make this call block. Be careful with
992 * this blocking call as it might not return for reasons stated above.
995 * Pad block handlers are only called for source pads in push mode
996 * and sink pads in pull mode.
999 * Returns: TRUE if the pad could be blocked. This function can fail if the
1000 * wrong parameters were passed or the pad was already in the requested state.
1007 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1008 GstPadBlockCallback callback, gpointer user_data,
1009 GDestroyNotify destroy_data)
1011 gboolean was_blocked = FALSE;
1013 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1015 GST_OBJECT_LOCK (pad);
1017 was_blocked = GST_PAD_IS_BLOCKED (pad);
1019 if (G_UNLIKELY (was_blocked == blocked))
1020 goto had_right_state;
1023 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1025 _priv_gst_pad_invalidate_cache (pad);
1026 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1028 if (pad->block_destroy_data && pad->block_data)
1029 pad->block_destroy_data (pad->block_data);
1031 pad->block_callback = callback;
1032 pad->block_data = user_data;
1033 pad->block_destroy_data = destroy_data;
1034 pad->block_callback_called = FALSE;
1036 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1037 GST_PAD_BLOCK_WAIT (pad);
1038 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1041 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1043 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1045 if (pad->block_destroy_data && pad->block_data)
1046 pad->block_destroy_data (pad->block_data);
1048 pad->block_callback = callback;
1049 pad->block_data = user_data;
1050 pad->block_destroy_data = destroy_data;
1051 pad->block_callback_called = FALSE;
1053 GST_PAD_BLOCK_BROADCAST (pad);
1055 /* no callback, wait for the unblock to happen */
1056 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1057 GST_PAD_BLOCK_WAIT (pad);
1058 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1061 GST_OBJECT_UNLOCK (pad);
1067 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1068 "pad was in right state (%d)", was_blocked);
1069 GST_OBJECT_UNLOCK (pad);
1076 * gst_pad_set_blocked_async:
1077 * @pad: the #GstPad to block or unblock
1078 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1079 * @callback: #GstPadBlockCallback that will be called when the
1080 * operation succeeds
1081 * @user_data: (closure): user data passed to the callback
1083 * Blocks or unblocks the dataflow on a pad. The provided callback
1084 * is called when the operation succeeds; this happens right before the next
1085 * attempt at pushing a buffer on the pad.
1087 * This can take a while as the pad can only become blocked when real dataflow
1089 * When the pipeline is stalled, for example in PAUSED, this can
1090 * take an indeterminate amount of time.
1091 * You can pass NULL as the callback to make this call block. Be careful with
1092 * this blocking call as it might not return for reasons stated above.
1095 * Pad block handlers are only called for source pads in push mode
1096 * and sink pads in pull mode.
1099 * Returns: TRUE if the pad could be blocked. This function can fail if the
1100 * wrong parameters were passed or the pad was already in the requested state.
1105 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1106 GstPadBlockCallback callback, gpointer user_data)
1108 return gst_pad_set_blocked_async_full (pad, blocked,
1109 callback, user_data, NULL);
1113 * gst_pad_set_blocked:
1114 * @pad: the #GstPad to block or unblock
1115 * @blocked: boolean indicating we should block or unblock
1117 * Blocks or unblocks the dataflow on a pad. This function is
1118 * a shortcut for gst_pad_set_blocked_async() with a NULL
1122 * Pad blocks are only possible for source pads in push mode
1123 * and sink pads in pull mode.
1126 * Returns: TRUE if the pad could be blocked. This function can fail if the
1127 * wrong parameters were passed or the pad was already in the requested state.
1132 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1134 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1138 * gst_pad_is_blocked:
1139 * @pad: the #GstPad to query
1141 * Checks if the pad is blocked or not. This function returns the
1142 * last requested state of the pad. It is not certain that the pad
1143 * is actually blocking at this point (see gst_pad_is_blocking()).
1145 * Returns: TRUE if the pad is blocked.
1150 gst_pad_is_blocked (GstPad * pad)
1152 gboolean result = FALSE;
1154 g_return_val_if_fail (GST_IS_PAD (pad), result);
1156 GST_OBJECT_LOCK (pad);
1157 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1158 GST_OBJECT_UNLOCK (pad);
1164 * gst_pad_is_blocking:
1165 * @pad: the #GstPad to query
1167 * Checks if the pad is blocking or not. This is a guaranteed state
1168 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1170 * Returns: TRUE if the pad is blocking.
1177 gst_pad_is_blocking (GstPad * pad)
1179 gboolean result = FALSE;
1181 g_return_val_if_fail (GST_IS_PAD (pad), result);
1183 GST_OBJECT_LOCK (pad);
1184 /* the blocking flag is only valid if the pad is not flushing */
1185 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1186 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1187 GST_OBJECT_UNLOCK (pad);
1193 * gst_pad_set_activate_function:
1195 * @activate: the #GstPadActivateFunction to set.
1197 * Sets the given activate function for @pad. The activate function will
1198 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1199 * the actual activation. Only makes sense to set on sink pads.
1201 * Call this function if your sink pad can start a pull-based task.
1204 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1206 g_return_if_fail (GST_IS_PAD (pad));
1208 GST_PAD_ACTIVATEFUNC (pad) = activate;
1209 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1210 GST_DEBUG_FUNCPTR_NAME (activate));
1214 * gst_pad_set_activatepull_function:
1216 * @activatepull: the #GstPadActivateModeFunction to set.
1218 * Sets the given activate_pull function for the pad. An activate_pull function
1219 * prepares the element and any upstream connections for pulling. See XXX
1220 * part-activation.txt for details.
1223 gst_pad_set_activatepull_function (GstPad * pad,
1224 GstPadActivateModeFunction activatepull)
1226 g_return_if_fail (GST_IS_PAD (pad));
1228 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1229 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1230 GST_DEBUG_FUNCPTR_NAME (activatepull));
1234 * gst_pad_set_activatepush_function:
1236 * @activatepush: the #GstPadActivateModeFunction to set.
1238 * Sets the given activate_push function for the pad. An activate_push function
1239 * prepares the element for pushing. See XXX part-activation.txt for details.
1242 gst_pad_set_activatepush_function (GstPad * pad,
1243 GstPadActivateModeFunction activatepush)
1245 g_return_if_fail (GST_IS_PAD (pad));
1247 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1248 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1249 GST_DEBUG_FUNCPTR_NAME (activatepush));
1253 * gst_pad_set_chain_function:
1254 * @pad: a sink #GstPad.
1255 * @chain: the #GstPadChainFunction to set.
1257 * Sets the given chain function for the pad. The chain function is called to
1258 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1261 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1263 g_return_if_fail (GST_IS_PAD (pad));
1264 g_return_if_fail (GST_PAD_IS_SINK (pad));
1266 GST_PAD_CHAINFUNC (pad) = chain;
1267 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1268 GST_DEBUG_FUNCPTR_NAME (chain));
1272 * gst_pad_set_chain_list_function:
1273 * @pad: a sink #GstPad.
1274 * @chainlist: the #GstPadChainListFunction to set.
1276 * Sets the given chain list function for the pad. The chainlist function is
1277 * called to process a #GstBufferList input buffer list. See
1278 * #GstPadChainListFunction for more details.
1283 gst_pad_set_chain_list_function (GstPad * pad,
1284 GstPadChainListFunction chainlist)
1286 g_return_if_fail (GST_IS_PAD (pad));
1287 g_return_if_fail (GST_PAD_IS_SINK (pad));
1289 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1290 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1291 GST_DEBUG_FUNCPTR_NAME (chainlist));
1295 * gst_pad_set_getrange_function:
1296 * @pad: a source #GstPad.
1297 * @get: the #GstPadGetRangeFunction to set.
1299 * Sets the given getrange function for the pad. The getrange function is
1300 * called to produce a new #GstBuffer to start the processing pipeline. see
1301 * #GstPadGetRangeFunction for a description of the getrange function.
1304 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1306 g_return_if_fail (GST_IS_PAD (pad));
1307 g_return_if_fail (GST_PAD_IS_SRC (pad));
1309 GST_PAD_GETRANGEFUNC (pad) = get;
1311 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1312 GST_DEBUG_FUNCPTR_NAME (get));
1316 * gst_pad_set_checkgetrange_function:
1317 * @pad: a source #GstPad.
1318 * @check: the #GstPadCheckGetRangeFunction to set.
1320 * Sets the given checkgetrange function for the pad. Implement this function
1321 * on a pad if you dynamically support getrange based scheduling on the pad.
1324 gst_pad_set_checkgetrange_function (GstPad * pad,
1325 GstPadCheckGetRangeFunction check)
1327 g_return_if_fail (GST_IS_PAD (pad));
1328 g_return_if_fail (GST_PAD_IS_SRC (pad));
1330 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1332 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1333 GST_DEBUG_FUNCPTR_NAME (check));
1337 * gst_pad_set_event_function:
1338 * @pad: a #GstPad of either direction.
1339 * @event: the #GstPadEventFunction to set.
1341 * Sets the given event handler for the pad.
1344 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1346 g_return_if_fail (GST_IS_PAD (pad));
1348 GST_PAD_EVENTFUNC (pad) = event;
1350 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1351 GST_DEBUG_FUNCPTR_NAME (event));
1355 * gst_pad_set_query_function:
1356 * @pad: a #GstPad of either direction.
1357 * @query: the #GstPadQueryFunction to set.
1359 * Set the given query function for the pad.
1362 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1364 g_return_if_fail (GST_IS_PAD (pad));
1366 GST_PAD_QUERYFUNC (pad) = query;
1368 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1369 GST_DEBUG_FUNCPTR_NAME (query));
1373 * gst_pad_set_query_type_function:
1374 * @pad: a #GstPad of either direction.
1375 * @type_func: the #GstPadQueryTypeFunction to set.
1377 * Set the given query type function for the pad.
1380 gst_pad_set_query_type_function (GstPad * pad,
1381 GstPadQueryTypeFunction type_func)
1383 g_return_if_fail (GST_IS_PAD (pad));
1385 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1387 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1388 GST_DEBUG_FUNCPTR_NAME (type_func));
1392 * gst_pad_get_query_types:
1395 * Get an array of supported queries that can be performed
1398 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1401 const GstQueryType *
1402 gst_pad_get_query_types (GstPad * pad)
1404 GstPadQueryTypeFunction func;
1406 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1408 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1420 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1422 *data = gst_pad_get_query_types (pad);
1428 * gst_pad_get_query_types_default:
1431 * Invoke the default dispatcher for the query types on
1434 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1435 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1436 * query types function.
1438 const GstQueryType *
1439 gst_pad_get_query_types_default (GstPad * pad)
1441 GstQueryType *result = NULL;
1443 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1445 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1446 gst_pad_get_query_types_dispatcher, &result);
1452 * gst_pad_set_iterate_internal_links_function:
1453 * @pad: a #GstPad of either direction.
1454 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1456 * Sets the given internal link iterator function for the pad.
1461 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1462 GstPadIterIntLinkFunction iterintlink)
1464 g_return_if_fail (GST_IS_PAD (pad));
1466 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1467 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1468 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1472 * gst_pad_set_link_function:
1474 * @link: the #GstPadLinkFunction to set.
1476 * Sets the given link function for the pad. It will be called when
1477 * the pad is linked with another pad.
1479 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1482 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1483 * cannot be made for some reason.
1485 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1486 * of the peer sink pad, if present.
1489 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1491 g_return_if_fail (GST_IS_PAD (pad));
1493 GST_PAD_LINKFUNC (pad) = link;
1494 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1495 GST_DEBUG_FUNCPTR_NAME (link));
1499 * gst_pad_set_unlink_function:
1501 * @unlink: the #GstPadUnlinkFunction to set.
1503 * Sets the given unlink function for the pad. It will be called
1504 * when the pad is unlinked.
1507 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1509 g_return_if_fail (GST_IS_PAD (pad));
1511 GST_PAD_UNLINKFUNC (pad) = unlink;
1512 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1513 GST_DEBUG_FUNCPTR_NAME (unlink));
1517 * gst_pad_set_getcaps_function:
1519 * @getcaps: the #GstPadGetCapsFunction to set.
1521 * Sets the given getcaps function for the pad. @getcaps should return the
1522 * allowable caps for a pad in the context of the element's state, its link to
1523 * other elements, and the devices or files it has opened. These caps must be a
1524 * subset of the pad template caps. In the NULL state with no links, @getcaps
1525 * should ideally return the same caps as the pad template. In rare
1526 * circumstances, an object property can affect the caps returned by @getcaps,
1527 * but this is discouraged.
1529 * You do not need to call this function if @pad's allowed caps are always the
1530 * same as the pad template caps. This can only be true if the padtemplate
1531 * has fixed simple caps.
1533 * For most filters, the caps returned by @getcaps is directly affected by the
1534 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1535 * the srcpad's getcaps function is directly related to the stream data. Again,
1536 * @getcaps should return the most specific caps it reasonably can, since this
1537 * helps with autoplugging.
1539 * Note that the return value from @getcaps is owned by the caller, so the
1540 * caller should unref the caps after usage.
1543 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1545 g_return_if_fail (GST_IS_PAD (pad));
1547 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1548 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1549 GST_DEBUG_FUNCPTR_NAME (getcaps));
1553 * gst_pad_set_acceptcaps_function:
1555 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1557 * Sets the given acceptcaps function for the pad. The acceptcaps function
1558 * will be called to check if the pad can accept the given caps. Setting the
1559 * acceptcaps function to NULL restores the default behaviour of allowing
1560 * any caps that matches the caps from gst_pad_get_caps().
1563 gst_pad_set_acceptcaps_function (GstPad * pad,
1564 GstPadAcceptCapsFunction acceptcaps)
1566 g_return_if_fail (GST_IS_PAD (pad));
1568 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1569 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1570 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1574 * gst_pad_set_fixatecaps_function:
1576 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1578 * Sets the given fixatecaps function for the pad. The fixatecaps function
1579 * will be called whenever the default values for a GstCaps needs to be
1583 gst_pad_set_fixatecaps_function (GstPad * pad,
1584 GstPadFixateCapsFunction fixatecaps)
1586 g_return_if_fail (GST_IS_PAD (pad));
1588 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1589 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1590 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1594 * gst_pad_set_setcaps_function:
1596 * @setcaps: the #GstPadSetCapsFunction to set.
1598 * Sets the given setcaps function for the pad. The setcaps function
1599 * will be called whenever a buffer with a new media type is pushed or
1600 * pulled from the pad. The pad/element needs to update its internal
1601 * structures to process the new media type. If this new type is not
1602 * acceptable, the setcaps function should return FALSE.
1605 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1607 g_return_if_fail (GST_IS_PAD (pad));
1609 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1610 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1611 GST_DEBUG_FUNCPTR_NAME (setcaps));
1615 * gst_pad_set_bufferalloc_function:
1616 * @pad: a sink #GstPad.
1617 * @bufalloc: the #GstPadBufferAllocFunction to set.
1619 * Sets the given bufferalloc function for the pad. Note that the
1620 * bufferalloc function can only be set on sinkpads.
1623 gst_pad_set_bufferalloc_function (GstPad * pad,
1624 GstPadBufferAllocFunction bufalloc)
1626 g_return_if_fail (GST_IS_PAD (pad));
1627 g_return_if_fail (GST_PAD_IS_SINK (pad));
1629 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1630 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1631 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1636 * @srcpad: the source #GstPad to unlink.
1637 * @sinkpad: the sink #GstPad to unlink.
1639 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1640 * signal on both pads.
1642 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1643 * the pads were not linked together.
1648 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1650 gboolean result = FALSE;
1651 GstElement *parent = NULL;
1653 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1654 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1655 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1656 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1658 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1659 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1660 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1662 /* We need to notify the parent before taking any pad locks as the bin in
1663 * question might be waiting for a lock on the pad while holding its lock
1664 * that our message will try to take. */
1665 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1666 if (GST_IS_ELEMENT (parent)) {
1667 gst_element_post_message (parent,
1668 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1669 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1671 gst_object_unref (parent);
1676 GST_OBJECT_LOCK (srcpad);
1678 GST_OBJECT_LOCK (sinkpad);
1680 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1681 goto not_linked_together;
1683 if (GST_PAD_UNLINKFUNC (srcpad)) {
1684 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1686 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1687 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1690 _priv_gst_pad_invalidate_cache (srcpad);
1692 /* first clear peers */
1693 GST_PAD_PEER (srcpad) = NULL;
1694 GST_PAD_PEER (sinkpad) = NULL;
1696 GST_OBJECT_UNLOCK (sinkpad);
1697 GST_OBJECT_UNLOCK (srcpad);
1699 /* fire off a signal to each of the pads telling them
1700 * that they've been unlinked */
1701 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1702 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1704 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1705 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1710 if (parent != NULL) {
1711 gst_element_post_message (parent,
1712 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1713 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1714 gst_object_unref (parent);
1719 not_linked_together:
1721 /* we do not emit a warning in this case because unlinking cannot
1722 * be made MT safe.*/
1723 GST_OBJECT_UNLOCK (sinkpad);
1724 GST_OBJECT_UNLOCK (srcpad);
1730 * gst_pad_is_linked:
1731 * @pad: pad to check
1733 * Checks if a @pad is linked to another pad or not.
1735 * Returns: TRUE if the pad is linked, FALSE otherwise.
1740 gst_pad_is_linked (GstPad * pad)
1744 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1746 GST_OBJECT_LOCK (pad);
1747 result = (GST_PAD_PEER (pad) != NULL);
1748 GST_OBJECT_UNLOCK (pad);
1753 /* get the caps from both pads and see if the intersection
1756 * This function should be called with the pad LOCK on both
1760 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1761 GstPadLinkCheck flags)
1763 GstCaps *srccaps = NULL;
1764 GstCaps *sinkcaps = NULL;
1765 gboolean compatible = FALSE;
1767 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1770 /* Doing the expensive caps checking takes priority over only checking the template caps */
1771 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1772 srccaps = gst_pad_get_caps_unlocked (src);
1773 sinkcaps = gst_pad_get_caps_unlocked (sink);
1775 /* If one of the two pads doesn't have a template, consider the intersection
1777 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1778 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1782 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1784 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1787 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1789 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1792 /* if we have caps on both pads we can check the intersection. If one
1793 * of the caps is NULL, we return TRUE. */
1794 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1796 gst_caps_unref (srccaps);
1798 gst_caps_unref (sinkcaps);
1802 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1803 gst_caps_unref (srccaps);
1804 gst_caps_unref (sinkcaps);
1807 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1808 (compatible ? "" : "not"));
1813 /* check if the grandparents of both pads are the same.
1814 * This check is required so that we don't try to link
1815 * pads from elements in different bins without ghostpads.
1817 * The LOCK should be held on both pads
1820 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1822 GstObject *psrc, *psink;
1824 psrc = GST_OBJECT_PARENT (src);
1825 psink = GST_OBJECT_PARENT (sink);
1827 /* if one of the pads has no parent, we allow the link */
1828 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1831 /* only care about parents that are elements */
1832 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1833 goto no_element_parent;
1835 /* if the parents are the same, we have a loop */
1836 if (G_UNLIKELY (psrc == psink))
1839 /* if they both have a parent, we check the grandparents. We can not lock
1840 * the parent because we hold on the child (pad) and the locking order is
1841 * parent >> child. */
1842 psrc = GST_OBJECT_PARENT (psrc);
1843 psink = GST_OBJECT_PARENT (psink);
1845 /* if they have grandparents but they are not the same */
1846 if (G_UNLIKELY (psrc != psink))
1847 goto wrong_grandparents;
1854 GST_CAT_DEBUG (GST_CAT_CAPS,
1855 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1856 GST_PTR_FORMAT, psrc, psink);
1861 GST_CAT_DEBUG (GST_CAT_CAPS,
1862 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1863 GST_PTR_FORMAT, psrc, psink);
1868 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1874 GST_CAT_DEBUG (GST_CAT_CAPS,
1875 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1876 GST_PTR_FORMAT, psrc, psink);
1881 /* FIXME leftover from an attempt at refactoring... */
1882 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1883 * the two pads will be locked in the srcpad, sinkpad order. */
1884 static GstPadLinkReturn
1885 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1887 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1888 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1890 GST_OBJECT_LOCK (srcpad);
1892 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1893 goto src_was_linked;
1895 GST_OBJECT_LOCK (sinkpad);
1897 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1898 goto sink_was_linked;
1900 /* check hierarchy, pads can only be linked if the grandparents
1902 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1903 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1904 goto wrong_hierarchy;
1906 /* check pad caps for non-empty intersection */
1907 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1910 /* FIXME check pad scheduling for non-empty intersection */
1912 return GST_PAD_LINK_OK;
1916 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1917 GST_DEBUG_PAD_NAME (srcpad),
1918 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1919 /* we do not emit a warning in this case because unlinking cannot
1920 * be made MT safe.*/
1921 GST_OBJECT_UNLOCK (srcpad);
1922 return GST_PAD_LINK_WAS_LINKED;
1926 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1927 GST_DEBUG_PAD_NAME (sinkpad),
1928 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1929 /* we do not emit a warning in this case because unlinking cannot
1930 * be made MT safe.*/
1931 GST_OBJECT_UNLOCK (sinkpad);
1932 GST_OBJECT_UNLOCK (srcpad);
1933 return GST_PAD_LINK_WAS_LINKED;
1937 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1938 GST_OBJECT_UNLOCK (sinkpad);
1939 GST_OBJECT_UNLOCK (srcpad);
1940 return GST_PAD_LINK_WRONG_HIERARCHY;
1944 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1945 GST_OBJECT_UNLOCK (sinkpad);
1946 GST_OBJECT_UNLOCK (srcpad);
1947 return GST_PAD_LINK_NOFORMAT;
1953 * @srcpad: the source #GstPad.
1954 * @sinkpad: the sink #GstPad.
1956 * Checks if the source pad and the sink pad are compatible so they can be
1959 * Returns: TRUE if the pads can be linked.
1962 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1964 GstPadLinkReturn result;
1966 /* generic checks */
1967 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1968 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1970 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1971 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1973 /* gst_pad_link_prepare does everything for us, we only release the locks
1974 * on the pads that it gets us. If this function returns !OK the locks are not
1976 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1977 if (result != GST_PAD_LINK_OK)
1980 GST_OBJECT_UNLOCK (srcpad);
1981 GST_OBJECT_UNLOCK (sinkpad);
1984 return result == GST_PAD_LINK_OK;
1988 * gst_pad_link_full:
1989 * @srcpad: the source #GstPad to link.
1990 * @sinkpad: the sink #GstPad to link.
1991 * @flags: the checks to validate when linking
1993 * Links the source pad and the sink pad.
1995 * This variant of #gst_pad_link provides a more granular control on the
1996 * checks being done when linking. While providing some considerable speedups
1997 * the caller of this method must be aware that wrong usage of those flags
1998 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1999 * for more information.
2003 * Returns: A result code indicating if the connection worked or
2009 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2011 GstPadLinkReturn result;
2014 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2015 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2016 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2017 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2018 GST_PAD_LINK_WRONG_DIRECTION);
2020 /* Notify the parent early. See gst_pad_unlink for details. */
2021 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2022 if (GST_IS_ELEMENT (parent)) {
2023 gst_element_post_message (parent,
2024 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2025 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2027 gst_object_unref (parent);
2032 /* prepare will also lock the two pads */
2033 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2035 if (result != GST_PAD_LINK_OK)
2038 /* must set peers before calling the link function */
2039 GST_PAD_PEER (srcpad) = sinkpad;
2040 GST_PAD_PEER (sinkpad) = srcpad;
2042 GST_OBJECT_UNLOCK (sinkpad);
2043 GST_OBJECT_UNLOCK (srcpad);
2045 /* FIXME released the locks here, concurrent thread might link
2046 * something else. */
2047 if (GST_PAD_LINKFUNC (srcpad)) {
2048 /* this one will call the peer link function */
2049 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2050 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2051 /* if no source link function, we need to call the sink link
2052 * function ourselves. */
2053 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2055 result = GST_PAD_LINK_OK;
2058 GST_OBJECT_LOCK (srcpad);
2059 GST_OBJECT_LOCK (sinkpad);
2061 if (result == GST_PAD_LINK_OK) {
2062 GST_OBJECT_UNLOCK (sinkpad);
2063 GST_OBJECT_UNLOCK (srcpad);
2065 /* fire off a signal to each of the pads telling them
2066 * that they've been linked */
2067 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2068 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2070 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2071 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2073 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2074 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2076 GST_PAD_PEER (srcpad) = NULL;
2077 GST_PAD_PEER (sinkpad) = NULL;
2079 GST_OBJECT_UNLOCK (sinkpad);
2080 GST_OBJECT_UNLOCK (srcpad);
2085 gst_element_post_message (parent,
2086 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2087 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2088 gst_object_unref (parent);
2096 * @srcpad: the source #GstPad to link.
2097 * @sinkpad: the sink #GstPad to link.
2099 * Links the source pad and the sink pad.
2101 * Returns: A result code indicating if the connection worked or
2107 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2109 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2113 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2115 GstPadTemplate **template_p;
2117 /* this function would need checks if it weren't static */
2119 GST_OBJECT_LOCK (pad);
2120 template_p = &pad->padtemplate;
2121 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2122 GST_OBJECT_UNLOCK (pad);
2125 gst_pad_template_pad_created (templ, pad);
2129 * gst_pad_get_pad_template:
2132 * Gets the template for @pad.
2134 * Returns: (transfer none): the #GstPadTemplate from which this pad was
2135 * instantiated, or %NULL if this pad has no template.
2137 * FIXME: currently returns an unrefcounted padtemplate.
2140 gst_pad_get_pad_template (GstPad * pad)
2142 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2144 return GST_PAD_PAD_TEMPLATE (pad);
2148 /* should be called with the pad LOCK held */
2149 /* refs the caps, so caller is responsible for getting it unreffed */
2151 gst_pad_get_caps_unlocked (GstPad * pad)
2153 GstCaps *result = NULL;
2154 GstPadTemplate *templ;
2156 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2158 if (GST_PAD_GETCAPSFUNC (pad)) {
2159 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2160 "dispatching to pad getcaps function");
2162 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2163 GST_OBJECT_UNLOCK (pad);
2164 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2165 GST_OBJECT_LOCK (pad);
2166 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2168 if (result == NULL) {
2169 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2170 GST_DEBUG_PAD_NAME (pad));
2172 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2173 "pad getcaps returned %" GST_PTR_FORMAT, result);
2174 #ifndef G_DISABLE_ASSERT
2175 /* check that the returned caps are a real subset of the template caps */
2176 if (GST_PAD_PAD_TEMPLATE (pad)) {
2177 const GstCaps *templ_caps =
2178 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2179 if (!gst_caps_is_subset (result, templ_caps)) {
2182 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2183 "pad returned caps %" GST_PTR_FORMAT
2184 " which are not a real subset of its template caps %"
2185 GST_PTR_FORMAT, result, templ_caps);
2187 ("pad %s:%s returned caps which are not a real "
2188 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2189 temp = gst_caps_intersect (templ_caps, result);
2190 gst_caps_unref (result);
2198 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2199 result = GST_PAD_TEMPLATE_CAPS (templ);
2200 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2201 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2204 result = gst_caps_ref (result);
2207 if ((result = GST_PAD_CAPS (pad))) {
2208 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2209 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2211 result = gst_caps_ref (result);
2215 /* this almost never happens */
2216 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2217 result = gst_caps_new_empty ();
2225 * @pad: a #GstPad to get the capabilities of.
2227 * Gets the capabilities this pad can produce or consume.
2228 * Note that this method doesn't necessarily return the caps set by
2229 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2230 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2231 * the pad's get_caps function;
2232 * this returns the pad template caps if not explicitly set.
2234 * Note that this function does not return writable #GstCaps, use
2235 * gst_caps_make_writable() before modifying the caps.
2237 * Returns: the caps of the pad with incremented ref-count.
2240 gst_pad_get_caps (GstPad * pad)
2242 GstCaps *result = NULL;
2244 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2246 GST_OBJECT_LOCK (pad);
2248 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2250 result = gst_pad_get_caps_unlocked (pad);
2252 GST_OBJECT_UNLOCK (pad);
2258 * gst_pad_peer_get_caps:
2259 * @pad: a #GstPad to get the capabilities of.
2261 * Gets the capabilities of the peer connected to this pad. Similar to
2262 * gst_pad_get_caps().
2264 * Returns: the caps of the peer pad with incremented ref-count. This function
2265 * returns %NULL when there is no peer pad.
2268 gst_pad_peer_get_caps (GstPad * pad)
2271 GstCaps *result = NULL;
2273 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2275 GST_OBJECT_LOCK (pad);
2277 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2279 peerpad = GST_PAD_PEER (pad);
2280 if (G_UNLIKELY (peerpad == NULL))
2283 gst_object_ref (peerpad);
2284 GST_OBJECT_UNLOCK (pad);
2286 result = gst_pad_get_caps (peerpad);
2288 gst_object_unref (peerpad);
2294 GST_OBJECT_UNLOCK (pad);
2300 fixate_value (GValue * dest, const GValue * src)
2302 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2303 g_value_init (dest, G_TYPE_INT);
2304 g_value_set_int (dest, gst_value_get_int_range_min (src));
2305 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2306 g_value_init (dest, G_TYPE_DOUBLE);
2307 g_value_set_double (dest, gst_value_get_double_range_min (src));
2308 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2309 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2310 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2311 GValue temp = { 0 };
2313 /* list could be empty */
2314 if (gst_value_list_get_size (src) <= 0)
2317 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2319 if (!fixate_value (dest, &temp))
2320 gst_value_init_and_copy (dest, &temp);
2321 g_value_unset (&temp);
2322 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2323 gboolean res = FALSE;
2326 len = gst_value_array_get_size (src);
2327 g_value_init (dest, GST_TYPE_ARRAY);
2328 for (n = 0; n < len; n++) {
2330 const GValue *orig_kid = gst_value_array_get_value (src, n);
2332 if (!fixate_value (&kid, orig_kid))
2333 gst_value_init_and_copy (&kid, orig_kid);
2336 gst_value_array_append_value (dest, &kid);
2337 g_value_unset (&kid);
2341 g_value_unset (dest);
2352 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2354 GstStructure *s = data;
2357 if (fixate_value (&v, value)) {
2358 gst_structure_id_set_value (s, field_id, &v);
2366 * gst_pad_fixate_caps:
2367 * @pad: a #GstPad to fixate
2368 * @caps: the #GstCaps to fixate
2370 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2371 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2374 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2376 GstPadFixateCapsFunction fixatefunc;
2379 g_return_if_fail (GST_IS_PAD (pad));
2380 g_return_if_fail (caps != NULL);
2381 g_return_if_fail (!gst_caps_is_empty (caps));
2382 /* FIXME-0.11: do not allow fixating any-caps
2383 * g_return_if_fail (!gst_caps_is_any (caps));
2386 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2389 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2391 fixatefunc (pad, caps);
2394 /* default fixation */
2395 gst_caps_truncate (caps);
2396 s = gst_caps_get_structure (caps, 0);
2397 gst_structure_foreach (s, gst_pad_default_fixate, s);
2400 /* Default accept caps implementation just checks against
2401 * against the allowed caps for the pad */
2403 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2405 /* get the caps and see if it intersects to something not empty */
2407 gboolean result = FALSE;
2409 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2411 allowed = gst_pad_get_caps (pad);
2413 goto nothing_allowed;
2415 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2417 result = gst_caps_can_intersect (allowed, caps);
2419 gst_caps_unref (allowed);
2426 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2432 * gst_pad_accept_caps:
2433 * @pad: a #GstPad to check
2434 * @caps: a #GstCaps to check on the pad
2436 * Check if the given pad accepts the caps.
2438 * Returns: TRUE if the pad can accept the caps.
2441 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2444 GstPadAcceptCapsFunction acceptfunc;
2445 GstCaps *existing = NULL;
2447 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2449 /* any pad can be unnegotiated */
2453 /* lock for checking the existing caps */
2454 GST_OBJECT_LOCK (pad);
2455 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2456 /* The current caps on a pad are trivially acceptable */
2457 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2458 if (caps == existing || gst_caps_is_equal (caps, existing))
2461 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2462 GST_OBJECT_UNLOCK (pad);
2464 if (G_LIKELY (acceptfunc)) {
2465 /* we can call the function */
2466 result = acceptfunc (pad, caps);
2467 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2469 /* Only null if the element explicitly unset it */
2470 result = gst_pad_acceptcaps_default (pad, caps);
2471 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2477 GST_DEBUG_OBJECT (pad, "pad had same caps");
2478 GST_OBJECT_UNLOCK (pad);
2484 * gst_pad_peer_accept_caps:
2485 * @pad: a #GstPad to check the peer of
2486 * @caps: a #GstCaps to check on the pad
2488 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2491 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2494 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2499 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2501 GST_OBJECT_LOCK (pad);
2503 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2505 peerpad = GST_PAD_PEER (pad);
2506 if (G_UNLIKELY (peerpad == NULL))
2509 gst_object_ref (peerpad);
2510 /* release lock before calling external methods but keep ref to pad */
2511 GST_OBJECT_UNLOCK (pad);
2513 result = gst_pad_accept_caps (peerpad, caps);
2515 gst_object_unref (peerpad);
2521 GST_OBJECT_UNLOCK (pad);
2528 * @pad: a #GstPad to set the capabilities of.
2529 * @caps: (transfer none): a #GstCaps to set.
2531 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2532 * caps on the pad will be unreffed. This function refs the caps so you should
2533 * unref if as soon as you don't need it anymore.
2534 * It is possible to set NULL caps, which will make the pad unnegotiated
2537 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2538 * or bad parameters were provided to this function.
2543 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2545 GstPadSetCapsFunction setcaps;
2548 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2549 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2551 GST_OBJECT_LOCK (pad);
2552 existing = GST_PAD_CAPS (pad);
2553 if (existing == caps)
2556 if (gst_caps_is_equal (caps, existing))
2557 goto setting_same_caps;
2559 setcaps = GST_PAD_SETCAPSFUNC (pad);
2561 /* call setcaps function to configure the pad only if the
2562 * caps is not NULL */
2563 if (setcaps != NULL && caps) {
2564 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2565 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2566 GST_OBJECT_UNLOCK (pad);
2567 if (!setcaps (pad, caps))
2569 GST_OBJECT_LOCK (pad);
2570 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2572 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2576 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2577 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2579 GST_OBJECT_UNLOCK (pad);
2581 #if GLIB_CHECK_VERSION(2,26,0)
2582 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2584 g_object_notify ((GObject *) pad, "caps");
2591 GST_OBJECT_UNLOCK (pad);
2596 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2597 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2598 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2600 GST_OBJECT_UNLOCK (pad);
2607 GST_OBJECT_LOCK (pad);
2608 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2609 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2610 "caps %" GST_PTR_FORMAT " could not be set", caps);
2611 GST_OBJECT_UNLOCK (pad);
2618 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2622 /* See if pad accepts the caps */
2623 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2626 /* set caps on pad if call succeeds */
2627 res = gst_pad_set_caps (pad, caps);
2628 /* no need to unref the caps here, set_caps takes a ref and
2629 * our ref goes away when we leave this function. */
2635 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2636 "caps %" GST_PTR_FORMAT " not accepted", caps);
2641 /* returns TRUE if the src pad could be configured to accept the given caps */
2643 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2648 /* See if pad accepts the caps */
2649 if (!gst_pad_accept_caps (pad, caps))
2652 res = gst_pad_set_caps (pad, caps);
2660 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2661 "caps %" GST_PTR_FORMAT " not accepted", caps);
2667 * gst_pad_get_pad_template_caps:
2668 * @pad: a #GstPad to get the template capabilities from.
2670 * Gets the capabilities for @pad's template.
2672 * Returns: (transfer none): the #GstCaps of this pad template. If you intend
2673 * to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2676 gst_pad_get_pad_template_caps (GstPad * pad)
2678 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2680 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2682 if (GST_PAD_PAD_TEMPLATE (pad))
2683 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2685 return gst_static_caps_get (&anycaps);
2690 * @pad: a #GstPad to get the peer of.
2692 * Gets the peer of @pad. This function refs the peer pad so
2693 * you need to unref it after use.
2695 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2700 gst_pad_get_peer (GstPad * pad)
2704 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2706 GST_OBJECT_LOCK (pad);
2707 result = GST_PAD_PEER (pad);
2709 gst_object_ref (result);
2710 GST_OBJECT_UNLOCK (pad);
2716 * gst_pad_get_allowed_caps:
2719 * Gets the capabilities of the allowed media types that can flow through
2720 * @pad and its peer.
2722 * The allowed capabilities is calculated as the intersection of the results of
2723 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2724 * on the resulting caps.
2726 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2727 * caps when you no longer need it. This function returns NULL when @pad
2733 gst_pad_get_allowed_caps (GstPad * pad)
2740 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2742 GST_OBJECT_LOCK (pad);
2744 peer = GST_PAD_PEER (pad);
2745 if (G_UNLIKELY (peer == NULL))
2748 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2750 gst_object_ref (peer);
2751 GST_OBJECT_UNLOCK (pad);
2752 mycaps = gst_pad_get_caps (pad);
2754 peercaps = gst_pad_get_caps (peer);
2755 gst_object_unref (peer);
2757 caps = gst_caps_intersect (mycaps, peercaps);
2758 gst_caps_unref (peercaps);
2759 gst_caps_unref (mycaps);
2761 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2768 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2769 GST_OBJECT_UNLOCK (pad);
2776 * gst_pad_get_negotiated_caps:
2779 * Gets the capabilities of the media type that currently flows through @pad
2782 * This function can be used on both src and sinkpads. Note that srcpads are
2783 * always negotiated before sinkpads so it is possible that the negotiated caps
2784 * on the srcpad do not match the negotiated caps of the peer.
2786 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2787 * the caps when you no longer need it. This function returns NULL when
2788 * the @pad has no peer or is not negotiated yet.
2793 gst_pad_get_negotiated_caps (GstPad * pad)
2798 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2800 GST_OBJECT_LOCK (pad);
2802 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2805 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2807 caps = GST_PAD_CAPS (pad);
2809 gst_caps_ref (caps);
2810 GST_OBJECT_UNLOCK (pad);
2812 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2819 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2820 GST_OBJECT_UNLOCK (pad);
2826 /* calls the buffer_alloc function on the given pad */
2827 static GstFlowReturn
2828 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2829 GstCaps * caps, GstBuffer ** buf)
2832 GstPadBufferAllocFunction bufferallocfunc;
2834 GST_OBJECT_LOCK (pad);
2835 /* when the pad is flushing we cannot give a buffer */
2836 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2839 bufferallocfunc = pad->bufferallocfunc;
2841 if (offset == GST_BUFFER_OFFSET_NONE) {
2842 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2843 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2844 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2846 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2847 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2848 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2849 bufferallocfunc, size, offset);
2851 GST_OBJECT_UNLOCK (pad);
2853 /* G_LIKELY for now since most elements don't implement a buffer alloc
2854 * function and there is no default alloc proxy function as this is usually
2856 if (G_LIKELY (bufferallocfunc == NULL))
2859 ret = bufferallocfunc (pad, offset, size, caps, buf);
2861 if (G_UNLIKELY (ret != GST_FLOW_OK))
2864 /* no error, but NULL buffer means fallback to the default */
2865 if (G_UNLIKELY (*buf == NULL))
2868 /* If the buffer alloc function didn't set up the caps like it should,
2870 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2871 GST_WARNING_OBJECT (pad,
2872 "Buffer allocation function did not set caps. Setting");
2873 gst_buffer_set_caps (*buf, caps);
2879 /* pad was flushing */
2880 GST_OBJECT_UNLOCK (pad);
2881 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2882 return GST_FLOW_WRONG_STATE;
2886 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2887 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2892 /* fallback case, allocate a buffer of our own, add pad caps. */
2893 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2895 if ((*buf = gst_buffer_new_and_alloc (size))) {
2896 GST_BUFFER_OFFSET (*buf) = offset;
2897 gst_buffer_set_caps (*buf, caps);
2900 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2901 "out of memory allocating %d bytes", size);
2902 return GST_FLOW_ERROR;
2907 /* FIXME 0.11: size should be unsigned */
2908 static GstFlowReturn
2909 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2910 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
2915 gboolean caps_changed;
2917 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2918 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2919 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2920 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
2922 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
2923 GST_PTR_FORMAT, offset, size, caps);
2925 GST_OBJECT_LOCK (pad);
2926 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2927 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
2930 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2933 gst_object_ref (peer);
2934 GST_OBJECT_UNLOCK (pad);
2936 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
2937 gst_object_unref (peer);
2939 if (G_UNLIKELY (ret != GST_FLOW_OK))
2942 /* FIXME, move capnego this into a base class? */
2943 newcaps = GST_BUFFER_CAPS (*buf);
2945 /* Lock for checking caps, pretty pointless as the _pad_push() function might
2946 * change it concurrently, one of the problems with automatic caps setting in
2947 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
2948 * when there is heavy renegotiation going on in both directions. */
2949 GST_OBJECT_LOCK (pad);
2950 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
2951 GST_OBJECT_UNLOCK (pad);
2953 /* we got a new datatype on the pad, see if it can handle it */
2954 if (G_UNLIKELY (caps_changed)) {
2955 GST_DEBUG_OBJECT (pad,
2956 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
2957 GST_PAD_CAPS (pad), newcaps, newcaps);
2958 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
2959 goto not_negotiated;
2962 /* sanity check (only if caps are the same) */
2963 if (G_LIKELY (newcaps == caps)
2964 && G_UNLIKELY (gst_buffer_get_size (*buf) < size))
2965 goto wrong_size_fallback;
2971 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
2972 GST_OBJECT_UNLOCK (pad);
2977 /* pad has no peer */
2978 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2979 "called bufferallocfunc but had no peer");
2980 GST_OBJECT_UNLOCK (pad);
2981 return GST_FLOW_NOT_LINKED;
2985 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2986 "alloc function returned error %s", gst_flow_get_name (ret));
2991 gst_buffer_unref (*buf);
2993 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2994 "alloc function returned unacceptable buffer");
2995 return GST_FLOW_NOT_NEGOTIATED;
2997 wrong_size_fallback:
2999 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3000 "function is too small (%u < %d), doing fallback buffer alloc",
3001 gst_buffer_get_size (*buf), size);
3003 gst_buffer_unref (*buf);
3005 if ((*buf = gst_buffer_new_and_alloc (size))) {
3006 GST_BUFFER_OFFSET (*buf) = offset;
3007 gst_buffer_set_caps (*buf, caps);
3010 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3011 "out of memory allocating %d bytes", size);
3012 return GST_FLOW_ERROR;
3018 * gst_pad_alloc_buffer:
3019 * @pad: a source #GstPad
3020 * @offset: the offset of the new buffer in the stream
3021 * @size: the size of the new buffer
3022 * @caps: the caps of the new buffer
3023 * @buf: a newly allocated buffer
3025 * Allocates a new, empty buffer optimized to push to pad @pad. This
3026 * function only works if @pad is a source pad and has a peer.
3028 * A new, empty #GstBuffer will be put in the @buf argument.
3029 * You need to check the caps of the buffer after performing this
3030 * function and renegotiate to the format if needed. If the caps changed, it is
3031 * possible that the buffer returned in @buf is not of the right size for the
3032 * new format, @buf needs to be unreffed and reallocated if this is the case.
3034 * Returns: a result code indicating success of the operation. Any
3035 * result code other than #GST_FLOW_OK is an error and @buf should
3037 * An error can occur if the pad is not connected or when the downstream
3038 * peer elements cannot provide an acceptable buffer.
3043 /* FIXME 0.11: size should be unsigned */
3045 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3048 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3052 * gst_pad_alloc_buffer_and_set_caps:
3053 * @pad: a source #GstPad
3054 * @offset: the offset of the new buffer in the stream
3055 * @size: the size of the new buffer
3056 * @caps: (transfer none): the caps of the new buffer
3057 * @buf: (out callee-allocates): a newly allocated buffer
3059 * In addition to the function gst_pad_alloc_buffer(), this function
3060 * automatically calls gst_pad_set_caps() when the caps of the
3061 * newly allocated buffer are different from the @pad caps.
3063 * After a renegotiation, the size of the new buffer returned in @buf could
3064 * be of the wrong size for the new format and must be unreffed an reallocated
3067 * Returns: a result code indicating success of the operation. Any
3068 * result code other than #GST_FLOW_OK is an error and @buf should
3070 * An error can occur if the pad is not connected or when the downstream
3071 * peer elements cannot provide an acceptable buffer.
3076 /* FIXME 0.11: size should be unsigned */
3078 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3079 GstCaps * caps, GstBuffer ** buf)
3081 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3085 static GstIteratorItem
3086 iterate_pad (GstIterator * it, GstPad * pad)
3088 gst_object_ref (pad);
3089 return GST_ITERATOR_ITEM_PASS;
3093 * gst_pad_iterate_internal_links_default:
3094 * @pad: the #GstPad to get the internal links of.
3096 * Iterate the list of pads to which the given pad is linked to inside of
3097 * the parent element.
3098 * This is the default handler, and thus returns an iterator of all of the
3099 * pads inside the parent element with opposite direction.
3101 * The caller must free this iterator after use with gst_iterator_free().
3103 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3104 * returned pad with gst_object_unref().
3109 gst_pad_iterate_internal_links_default (GstPad * pad)
3116 GstIteratorDisposeFunction dispose;
3118 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3123 GST_OBJECT_LOCK (pad);
3124 parent = GST_PAD_PARENT (pad);
3125 if (!parent || !GST_IS_ELEMENT (parent))
3128 gst_object_ref (parent);
3129 GST_OBJECT_UNLOCK (pad);
3131 if (pad->direction == GST_PAD_SRC)
3132 padlist = &parent->sinkpads;
3134 padlist = &parent->srcpads;
3136 GST_DEBUG_OBJECT (pad, "Making iterator");
3138 cookie = &parent->pads_cookie;
3140 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3141 lock = GST_OBJECT_GET_LOCK (parent);
3144 res = gst_iterator_new_list (GST_TYPE_PAD,
3145 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3153 GST_OBJECT_UNLOCK (pad);
3154 GST_DEBUG_OBJECT (pad, "no parent element");
3160 * gst_pad_iterate_internal_links:
3161 * @pad: the GstPad to get the internal links of.
3163 * Gets an iterator for the pads to which the given pad is linked to inside
3164 * of the parent element.
3166 * Each #GstPad element yielded by the iterator will have its refcount increased,
3167 * so unref after use.
3169 * Free-function: gst_iterator_free
3171 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3172 * pad does not have an iterator function configured. Use
3173 * gst_iterator_free() after usage.
3178 gst_pad_iterate_internal_links (GstPad * pad)
3180 GstIterator *res = NULL;
3182 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3184 if (GST_PAD_ITERINTLINKFUNC (pad))
3185 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3191 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3193 gboolean result = FALSE;
3195 gboolean done = FALSE;
3198 GList *pushed_pads = NULL;
3200 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3201 event, GST_EVENT_TYPE_NAME (event));
3203 iter = gst_pad_iterate_internal_links (pad);
3209 switch (gst_iterator_next (iter, &item)) {
3210 case GST_ITERATOR_OK:
3211 eventpad = GST_PAD_CAST (item);
3213 /* if already pushed, skip */
3214 if (g_list_find (pushed_pads, eventpad)) {
3215 gst_object_unref (item);
3219 if (GST_PAD_IS_SRC (eventpad)) {
3220 /* for each pad we send to, we should ref the event; it's up
3221 * to downstream to unref again when handled. */
3222 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3223 event, GST_EVENT_TYPE_NAME (event),
3224 GST_DEBUG_PAD_NAME (eventpad));
3225 gst_event_ref (event);
3226 result |= gst_pad_push_event (eventpad, event);
3228 /* we only send the event on one pad, multi-sinkpad elements
3229 * should implement a handler */
3230 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3231 event, GST_EVENT_TYPE_NAME (event),
3232 GST_DEBUG_PAD_NAME (eventpad));
3233 result = gst_pad_push_event (eventpad, event);
3238 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3240 gst_object_unref (item);
3242 case GST_ITERATOR_RESYNC:
3243 /* FIXME, if we want to reset the result value we need to remember which
3244 * pads pushed with which result */
3245 gst_iterator_resync (iter);
3247 case GST_ITERATOR_ERROR:
3248 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3251 case GST_ITERATOR_DONE:
3256 gst_iterator_free (iter);
3260 /* If this is a sinkpad and we don't have pads to send the event to, we
3261 * return TRUE. This is so that when using the default handler on a sink
3262 * element, we don't fail to push it. */
3264 result = GST_PAD_IS_SINK (pad);
3266 g_list_free (pushed_pads);
3268 /* we handled the incoming event so we unref once */
3270 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3271 gst_event_unref (event);
3278 * gst_pad_event_default:
3279 * @pad: a #GstPad to call the default event handler on.
3280 * @event: (transfer full): the #GstEvent to handle.
3282 * Invokes the default event handler for the given pad. End-of-stream and
3283 * discontinuity events are handled specially, and then the event is sent to all
3284 * pads internally linked to @pad. Note that if there are many possible sink
3285 * pads that are internally linked to @pad, only one will be sent an event.
3286 * Multi-sinkpad elements should implement custom event handlers.
3288 * Returns: TRUE if the event was sent succesfully.
3291 gst_pad_event_default (GstPad * pad, GstEvent * event)
3293 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3294 g_return_val_if_fail (event != NULL, FALSE);
3296 GST_LOG_OBJECT (pad, "default event handler");
3298 switch (GST_EVENT_TYPE (event)) {
3301 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3302 gst_pad_pause_task (pad);
3309 return gst_pad_event_default_dispatch (pad, event);
3313 * gst_pad_dispatcher:
3314 * @pad: a #GstPad to dispatch.
3315 * @dispatch: the #GstPadDispatcherFunction to call.
3316 * @data: (closure): gpointer user data passed to the dispatcher function.
3318 * Invokes the given dispatcher function on each respective peer of
3319 * all pads that are internally linked to the given pad.
3320 * The GstPadDispatcherFunction should return TRUE when no further pads
3321 * need to be processed.
3323 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3326 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3329 gboolean res = FALSE;
3330 GstIterator *iter = NULL;
3331 gboolean done = FALSE;
3334 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3335 g_return_val_if_fail (dispatch != NULL, FALSE);
3337 iter = gst_pad_iterate_internal_links (pad);
3343 switch (gst_iterator_next (iter, &item)) {
3344 case GST_ITERATOR_OK:
3346 GstPad *int_pad = GST_PAD_CAST (item);
3347 GstPad *int_peer = gst_pad_get_peer (int_pad);
3350 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3351 GST_DEBUG_PAD_NAME (int_peer));
3352 done = res = dispatch (int_peer, data);
3353 gst_object_unref (int_peer);
3355 GST_DEBUG_OBJECT (int_pad, "no peer");
3358 gst_object_unref (item);
3360 case GST_ITERATOR_RESYNC:
3361 gst_iterator_resync (iter);
3363 case GST_ITERATOR_ERROR:
3365 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3367 case GST_ITERATOR_DONE:
3372 gst_iterator_free (iter);
3374 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3383 * @pad: a #GstPad to invoke the default query on.
3384 * @query: (transfer none): the #GstQuery to perform.
3386 * Dispatches a query to a pad. The query should have been allocated by the
3387 * caller via one of the type-specific allocation functions. The element that
3388 * the pad belongs to is responsible for filling the query with an appropriate
3389 * response, which should then be parsed with a type-specific query parsing
3392 * Again, the caller is responsible for both the allocation and deallocation of
3393 * the query structure.
3395 * Please also note that some queries might need a running pipeline to work.
3397 * Returns: TRUE if the query could be performed.
3400 gst_pad_query (GstPad * pad, GstQuery * query)
3402 GstPadQueryFunction func;
3404 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3405 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3407 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3409 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3412 return func (pad, query);
3416 GST_DEBUG_OBJECT (pad, "had no query function");
3422 * gst_pad_peer_query:
3423 * @pad: a #GstPad to invoke the peer query on.
3424 * @query: (transfer none): the #GstQuery to perform.
3426 * Performs gst_pad_query() on the peer of @pad.
3428 * The caller is responsible for both the allocation and deallocation of
3429 * the query structure.
3431 * Returns: TRUE if the query could be performed. This function returns %FALSE
3432 * if @pad has no peer.
3437 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3442 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3443 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3445 GST_OBJECT_LOCK (pad);
3447 GST_DEBUG_OBJECT (pad, "peer query");
3449 peerpad = GST_PAD_PEER (pad);
3450 if (G_UNLIKELY (peerpad == NULL))
3453 gst_object_ref (peerpad);
3454 GST_OBJECT_UNLOCK (pad);
3456 result = gst_pad_query (peerpad, query);
3458 gst_object_unref (peerpad);
3465 GST_WARNING_OBJECT (pad, "pad has no peer");
3466 GST_OBJECT_UNLOCK (pad);
3472 * gst_pad_query_default:
3473 * @pad: a #GstPad to call the default query handler on.
3474 * @query: (transfer none): the #GstQuery to handle.
3476 * Invokes the default query handler for the given pad.
3477 * The query is sent to all pads internally linked to @pad. Note that
3478 * if there are many possible sink pads that are internally linked to
3479 * @pad, only one will be sent the query.
3480 * Multi-sinkpad elements should implement custom query handlers.
3482 * Returns: TRUE if the query was performed succesfully.
3485 gst_pad_query_default (GstPad * pad, GstQuery * query)
3487 switch (GST_QUERY_TYPE (query)) {
3488 case GST_QUERY_POSITION:
3489 case GST_QUERY_SEEKING:
3490 case GST_QUERY_FORMATS:
3491 case GST_QUERY_LATENCY:
3492 case GST_QUERY_JITTER:
3493 case GST_QUERY_RATE:
3494 case GST_QUERY_CONVERT:
3496 return gst_pad_dispatcher
3497 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3502 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3503 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3506 * This function performs the pad blocking when an event, buffer push
3507 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3508 * streaming thread after informing the pad has been blocked.
3510 * An application can with this method wait and block any streaming
3511 * thread and perform operations such as seeking or linking.
3513 * Two methods are available for notifying the application of the
3515 * - the callback method, which happens in the STREAMING thread with
3516 * the STREAM_LOCK held. With this method, the most useful way of
3517 * dealing with the callback is to post a message to the main thread
3518 * where the pad block can then be handled outside of the streaming
3519 * thread. With the last method one can perform all operations such
3520 * as doing a state change, linking, unblocking, seeking etc on the
3522 * - the GCond signal method, which makes any thread unblock when
3523 * the pad block happens.
3525 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3526 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3530 static GstFlowReturn
3531 handle_pad_block (GstPad * pad)
3533 GstPadBlockCallback callback;
3535 GstFlowReturn ret = GST_FLOW_OK;
3537 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3539 /* flushing, don't bother trying to block and return WRONG_STATE
3541 if (GST_PAD_IS_FLUSHING (pad))
3542 goto flushingnonref;
3544 /* we grab an extra ref for the callbacks, this is probably not
3545 * needed (callback code does not have a ref and cannot unref). I
3546 * think this was done to make it possible to unref the element in
3547 * the callback, which is in the end totally impossible as it
3548 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3549 * all taken when calling this function. */
3550 gst_object_ref (pad);
3552 while (GST_PAD_IS_BLOCKED (pad)) {
3554 /* we either have a callback installed to notify the block or
3555 * some other thread is doing a GCond wait. */
3556 callback = pad->block_callback;
3557 pad->block_callback_called = TRUE;
3559 /* there is a callback installed, call it. We release the
3560 * lock so that the callback can do something usefull with the
3562 user_data = pad->block_data;
3563 GST_OBJECT_UNLOCK (pad);
3564 callback (pad, TRUE, user_data);
3565 GST_OBJECT_LOCK (pad);
3567 /* we released the lock, recheck flushing */
3568 if (GST_PAD_IS_FLUSHING (pad))
3571 /* no callback, signal the thread that is doing a GCond wait
3573 GST_PAD_BLOCK_BROADCAST (pad);
3575 } while (pad->block_callback_called == FALSE && GST_PAD_IS_BLOCKED (pad));
3577 /* OBJECT_LOCK could have been released when we did the callback, which
3578 * then could have made the pad unblock so we need to check the blocking
3579 * condition again. */
3580 if (!GST_PAD_IS_BLOCKED (pad))
3583 /* now we block the streaming thread. It can be unlocked when we
3584 * deactivate the pad (which will also set the FLUSHING flag) or
3585 * when the pad is unblocked. A flushing event will also unblock
3586 * the pad after setting the FLUSHING flag. */
3587 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3588 "Waiting to be unblocked or set flushing");
3589 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3590 GST_PAD_BLOCK_WAIT (pad);
3591 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3593 /* see if we got unblocked by a flush or not */
3594 if (GST_PAD_IS_FLUSHING (pad))
3598 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3600 /* when we get here, the pad is unblocked again and we perform
3601 * the needed unblock code. */
3602 callback = pad->block_callback;
3604 /* we need to call the callback */
3605 user_data = pad->block_data;
3606 GST_OBJECT_UNLOCK (pad);
3607 callback (pad, FALSE, user_data);
3608 GST_OBJECT_LOCK (pad);
3610 /* we need to signal the thread waiting on the GCond */
3611 GST_PAD_BLOCK_BROADCAST (pad);
3614 gst_object_unref (pad);
3620 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3621 return GST_FLOW_WRONG_STATE;
3625 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3626 gst_object_unref (pad);
3627 return GST_FLOW_WRONG_STATE;
3631 /**********************************************************************
3632 * Data passing functions
3636 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3639 GValue args[2] = { {0}, {0} };
3644 g_value_init (&ret, G_TYPE_BOOLEAN);
3645 g_value_set_boolean (&ret, TRUE);
3646 g_value_init (&args[0], GST_TYPE_PAD);
3647 g_value_set_object (&args[0], pad);
3648 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3649 g_value_set_boxed (&args[1], obj);
3651 if (GST_IS_EVENT (obj))
3652 detail = event_quark;
3654 detail = buffer_quark;
3657 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3658 res = g_value_get_boolean (&ret);
3661 g_value_unset (&ret);
3662 g_value_unset (&args[0]);
3663 g_value_unset (&args[1]);
3669 gst_pad_data_unref (gboolean is_buffer, void *data)
3671 if (G_LIKELY (is_buffer)) {
3672 gst_buffer_unref (data);
3674 gst_buffer_list_unref (data);
3679 gst_pad_data_get_caps (gboolean is_buffer, void *data)
3683 if (G_LIKELY (is_buffer)) {
3684 caps = GST_BUFFER_CAPS (data);
3688 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0)))
3689 caps = GST_BUFFER_CAPS (buf);
3696 /* this is the chain function that does not perform the additional argument
3697 * checking for that little extra speed.
3699 static inline GstFlowReturn
3700 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
3701 GstPadPushCache * cache)
3704 gboolean caps_changed;
3706 gboolean emit_signal;
3708 GST_PAD_STREAM_LOCK (pad);
3710 GST_OBJECT_LOCK (pad);
3711 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3714 caps = gst_pad_data_get_caps (is_buffer, data);
3715 caps_changed = caps && caps != GST_PAD_CAPS (pad);
3717 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3718 GST_OBJECT_UNLOCK (pad);
3720 /* see if the signal should be emited, we emit before caps nego as
3721 * we might drop the buffer and do capsnego for nothing. */
3722 if (G_UNLIKELY (emit_signal)) {
3724 if (G_LIKELY (is_buffer)) {
3725 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3728 /* chain all groups in the buffer list one by one to avoid problems with
3729 * buffer probes that push buffers or events */
3734 /* we got a new datatype on the pad, see if it can handle it */
3735 if (G_UNLIKELY (caps_changed)) {
3736 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
3737 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
3738 goto not_negotiated;
3741 /* NOTE: we read the chainfunc unlocked.
3742 * we cannot hold the lock for the pad so we might send
3743 * the data to the wrong function. This is not really a
3744 * problem since functions are assigned at creation time
3745 * and don't change that often... */
3746 if (G_LIKELY (is_buffer)) {
3747 GstPadChainFunction chainfunc;
3749 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3752 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3753 "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
3756 cache->peer = gst_object_ref (pad);
3757 cache->caps = caps ? gst_caps_ref (caps) : NULL;
3760 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3762 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3763 "called chainfunction &%s, returned %s",
3764 GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
3766 GstPadChainListFunction chainlistfunc;
3768 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3771 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3772 "calling chainlistfunction &%s",
3773 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3775 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3777 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3778 "called chainlistfunction &%s, returned %s",
3779 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3782 GST_PAD_STREAM_UNLOCK (pad);
3788 GstBufferList *list;
3792 GST_PAD_STREAM_UNLOCK (pad);
3794 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3796 list = GST_BUFFER_LIST_CAST (data);
3797 len = gst_buffer_list_len (list);
3799 for (i = 0; i < len; i++) {
3800 buffer = gst_buffer_list_get (list, i);
3802 gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_ref (buffer),
3804 if (ret != GST_FLOW_OK)
3807 gst_buffer_list_unref (list);
3815 gst_pad_data_unref (is_buffer, data);
3816 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3817 "pushing, but pad was flushing");
3818 GST_OBJECT_UNLOCK (pad);
3819 GST_PAD_STREAM_UNLOCK (pad);
3820 return GST_FLOW_WRONG_STATE;
3824 gst_pad_data_unref (is_buffer, data);
3825 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3826 GST_PAD_STREAM_UNLOCK (pad);
3831 gst_pad_data_unref (is_buffer, data);
3832 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3833 "pushing data but pad did not accept");
3834 GST_PAD_STREAM_UNLOCK (pad);
3835 return GST_FLOW_NOT_NEGOTIATED;
3839 gst_pad_data_unref (is_buffer, data);
3840 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3841 "pushing, but not chainhandler");
3842 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3843 ("push on pad %s:%s but it has no chainfunction",
3844 GST_DEBUG_PAD_NAME (pad)));
3845 GST_PAD_STREAM_UNLOCK (pad);
3846 return GST_FLOW_NOT_SUPPORTED;
3852 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3853 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3856 * Chain a buffer to @pad.
3858 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3860 * If the caps on @buffer are different from the current caps on @pad, this
3861 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3862 * installed on @pad. If the new caps are not acceptable for @pad, this
3863 * function returns #GST_FLOW_NOT_NEGOTIATED.
3865 * The function proceeds calling the chain function installed on @pad (see
3866 * gst_pad_set_chain_function()) and the return value of that function is
3867 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3870 * In all cases, success or failure, the caller loses its reference to @buffer
3871 * after calling this function.
3873 * Returns: a #GstFlowReturn from the pad.
3878 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3880 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3881 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3882 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3884 return gst_pad_chain_data_unchecked (pad, TRUE, buffer, NULL);
3888 * gst_pad_chain_list:
3889 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3890 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3893 * Chain a bufferlist to @pad.
3895 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3897 * If the caps on the first buffer of @list are different from the current
3898 * caps on @pad, this function will call any setcaps function
3899 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3900 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3902 * The function proceeds calling the chainlist function installed on @pad (see
3903 * gst_pad_set_chain_list_function()) and the return value of that function is
3904 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3905 * chainlist function.
3907 * In all cases, success or failure, the caller loses its reference to @list
3908 * after calling this function.
3912 * Returns: a #GstFlowReturn from the pad.
3917 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3919 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3920 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3921 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3923 return gst_pad_chain_data_unchecked (pad, FALSE, list, NULL);
3926 static GstFlowReturn
3927 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
3928 GstPadPushCache * cache)
3933 gboolean caps_changed;
3935 GST_OBJECT_LOCK (pad);
3937 /* FIXME: this check can go away; pad_set_blocked could be implemented with
3938 * probes completely or probes with an extended pad block. */
3939 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3940 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3943 /* we emit signals on the pad arg, the peer will have a chance to
3944 * emit in the _chain() function */
3945 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
3947 /* unlock before emitting */
3948 GST_OBJECT_UNLOCK (pad);
3950 if (G_LIKELY (is_buffer)) {
3951 /* if the signal handler returned FALSE, it means we should just drop the
3953 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3956 /* push all buffers in the list */
3959 GST_OBJECT_LOCK (pad);
3962 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3965 /* Before pushing the buffer to the peer pad, ensure that caps
3966 * are set on this pad */
3967 caps = gst_pad_data_get_caps (is_buffer, data);
3968 caps_changed = caps && caps != GST_PAD_CAPS (pad);
3970 /* take ref to peer pad before releasing the lock */
3971 gst_object_ref (peer);
3972 GST_OBJECT_UNLOCK (pad);
3974 /* we got a new datatype from the pad, it had better handle it */
3975 if (G_UNLIKELY (caps_changed)) {
3976 GST_DEBUG_OBJECT (pad,
3977 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3978 GST_PAD_CAPS (pad), caps, caps);
3979 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
3980 goto not_negotiated;
3983 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, cache);
3985 gst_object_unref (peer);
3991 GstBufferList *list;
3995 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
3997 list = GST_BUFFER_LIST_CAST (data);
3998 len = gst_buffer_list_len (list);
4000 for (i = 0; i < len; i++) {
4001 buffer = gst_buffer_list_get (list, i);
4002 ret = gst_pad_push_data (pad, TRUE, gst_buffer_ref (buffer), NULL);
4003 if (ret != GST_FLOW_OK)
4006 gst_buffer_list_unref (list);
4011 /* ERROR recovery here */
4014 gst_pad_data_unref (is_buffer, data);
4015 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4016 GST_OBJECT_UNLOCK (pad);
4021 gst_pad_data_unref (is_buffer, data);
4022 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4027 gst_pad_data_unref (is_buffer, data);
4028 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4029 "pushing, but it was not linked");
4030 GST_OBJECT_UNLOCK (pad);
4031 return GST_FLOW_NOT_LINKED;
4035 gst_pad_data_unref (is_buffer, data);
4036 gst_object_unref (peer);
4037 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4038 "element pushed data then refused to accept the caps");
4039 return GST_FLOW_NOT_NEGOTIATED;
4043 static inline GstPadPushCache *
4044 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
4046 GstPadPushCache *cache;
4048 /* try to get the cached data */
4050 cache = g_atomic_pointer_get (cache_ptr);
4051 /* now try to replace the pointer with NULL to mark that we are busy
4053 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
4055 /* we could have a leftover invalid entry */
4056 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
4063 pad_free_cache (GstPadPushCache * cache)
4065 gst_object_unref (cache->peer);
4067 gst_caps_unref (cache->caps);
4068 g_slice_free (GstPadPushCache, cache);
4072 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
4075 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
4076 /* something changed, clean up our cache */
4077 pad_free_cache (cache);
4081 /* must be called with the pad lock */
4083 _priv_gst_pad_invalidate_cache (GstPad * pad)
4085 GstPadPushCache *cache;
4086 gpointer *cache_ptr;
4088 GST_LOG_OBJECT (pad, "Invalidating pad cache");
4090 /* we hold the pad lock here so we can get the peer and it stays
4091 * alive during this call */
4092 if (GST_PAD_IS_SINK (pad)) {
4093 if (!(pad = GST_PAD_PEER (pad)))
4097 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4099 /* try to get the cached data */
4101 cache = g_atomic_pointer_get (cache_ptr);
4102 /* now try to replace the pointer with INVALID. If nothing is busy with this
4103 * caps, we get the cache and clean it up. If something is busy, we replace
4104 * with INVALID so that when the function finishes and tries to put the
4105 * cache back, it'll fail and cleanup */
4106 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
4107 PAD_CACHE_INVALID));
4109 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
4110 pad_free_cache (cache);
4115 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4116 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4119 * Pushes a buffer to the peer of @pad.
4121 * This function will call an installed pad block before triggering any
4122 * installed pad probes.
4124 * If the caps on @buffer are different from the currently configured caps on
4125 * @pad, this function will call any installed setcaps function on @pad (see
4126 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4127 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4129 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4130 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4133 * In all cases, success or failure, the caller loses its reference to @buffer
4134 * after calling this function.
4136 * Returns: a #GstFlowReturn from the peer pad.
4141 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4143 GstPadPushCache *cache;
4145 gpointer *cache_ptr;
4149 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4150 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4151 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4153 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4155 cache = pad_take_cache (pad, cache_ptr);
4157 if (G_UNLIKELY (cache == NULL))
4161 caps = GST_BUFFER_CAPS (buffer);
4162 if (G_UNLIKELY (caps && caps != cache->caps)) {
4163 pad_free_cache (cache);
4169 GST_PAD_STREAM_LOCK (peer);
4170 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4173 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "calling chainfunction &%s",
4174 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)));
4176 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
4178 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4179 "called chainfunction &%s, returned %s",
4180 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)),
4181 gst_flow_get_name (ret));
4183 GST_PAD_STREAM_UNLOCK (peer);
4185 pad_put_cache (pad, cache, cache_ptr);
4192 GstPadPushCache scache = { NULL, };
4194 GST_LOG_OBJECT (pad, "Taking slow path");
4196 ret = gst_pad_push_data (pad, TRUE, buffer, &scache);
4199 GstPadPushCache *ncache;
4201 GST_LOG_OBJECT (pad, "Caching push data");
4203 /* make cache structure */
4204 ncache = g_slice_new (GstPadPushCache);
4207 pad_put_cache (pad, ncache, cache_ptr);
4213 pad_free_cache (cache);
4214 GST_PAD_STREAM_UNLOCK (peer);
4220 * gst_pad_push_list:
4221 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4222 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4225 * Pushes a buffer list to the peer of @pad.
4227 * This function will call an installed pad block before triggering any
4228 * installed pad probes.
4230 * If the caps on the first buffer in the first group of @list are different
4231 * from the currently configured caps on @pad, this function will call any
4232 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4233 * case of failure to renegotiate the new format, this function returns
4234 * #GST_FLOW_NOT_NEGOTIATED.
4236 * If there are any probes installed on @pad every group of the buffer list
4237 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4238 * buffer list will be unreffed.
4240 * The function proceeds calling the chain function on the peer pad and returns
4241 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4242 * be returned. If the peer pad does not have any installed chainlist function
4243 * every group buffer of the list will be merged into a normal #GstBuffer and
4244 * chained via gst_pad_chain().
4246 * In all cases, success or failure, the caller loses its reference to @list
4247 * after calling this function.
4249 * Returns: a #GstFlowReturn from the peer pad.
4256 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4259 GstPadPushCache *cache;
4261 gpointer *cache_ptr;
4265 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4266 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4267 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4269 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4271 cache = pad_take_cache (pad, cache_ptr);
4273 if (G_UNLIKELY (cache == NULL))
4277 if ((buf = gst_buffer_list_get (list, 0)))
4278 caps = GST_BUFFER_CAPS (buf);
4282 if (G_UNLIKELY (caps && caps != cache->caps)) {
4283 pad_free_cache (cache);
4289 GST_PAD_STREAM_LOCK (peer);
4290 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4293 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
4295 GST_PAD_STREAM_UNLOCK (peer);
4297 pad_put_cache (pad, cache, cache_ptr);
4304 GstPadPushCache scache = { NULL, };
4306 GST_LOG_OBJECT (pad, "Taking slow path");
4308 ret = gst_pad_push_data (pad, FALSE, list, &scache);
4311 GstPadPushCache *ncache;
4313 GST_LOG_OBJECT (pad, "Caching push data");
4315 /* make cache structure */
4316 ncache = g_slice_new (GstPadPushCache);
4319 pad_put_cache (pad, ncache, cache_ptr);
4325 pad_free_cache (cache);
4326 GST_PAD_STREAM_UNLOCK (peer);
4332 * gst_pad_check_pull_range:
4333 * @pad: a sink #GstPad.
4335 * Checks if a gst_pad_pull_range() can be performed on the peer
4336 * source pad. This function is used by plugins that want to check
4337 * if they can use random access on the peer source pad.
4339 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4340 * if it needs to perform some logic to determine if pull_range is
4343 * Returns: a gboolean with the result.
4348 gst_pad_check_pull_range (GstPad * pad)
4352 GstPadCheckGetRangeFunction checkgetrangefunc;
4354 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4356 GST_OBJECT_LOCK (pad);
4357 if (!GST_PAD_IS_SINK (pad))
4358 goto wrong_direction;
4360 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4363 gst_object_ref (peer);
4364 GST_OBJECT_UNLOCK (pad);
4366 /* see note in above function */
4367 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4368 /* FIXME, kindoff ghetto */
4369 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4370 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4371 "no checkgetrangefunc, assuming %d", ret);
4373 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4374 "calling checkgetrangefunc %s of peer pad %s:%s",
4375 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4377 ret = checkgetrangefunc (peer);
4380 gst_object_unref (peer);
4384 /* ERROR recovery here */
4387 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4388 "checking pull range, but pad must be a sinkpad");
4389 GST_OBJECT_UNLOCK (pad);
4394 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4395 "checking pull range, but it was not linked");
4396 GST_OBJECT_UNLOCK (pad);
4401 static GstFlowReturn
4402 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4403 GstBuffer ** buffer)
4406 GstPadGetRangeFunction getrangefunc;
4407 gboolean emit_signal;
4409 gboolean caps_changed;
4411 GST_PAD_STREAM_LOCK (pad);
4413 GST_OBJECT_LOCK (pad);
4414 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4417 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4418 GST_OBJECT_UNLOCK (pad);
4420 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4423 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4424 "calling getrangefunc %s, offset %"
4425 G_GUINT64_FORMAT ", size %u",
4426 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4428 ret = getrangefunc (pad, offset, size, buffer);
4430 /* can only fire the signal if we have a valid buffer */
4431 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4432 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4436 GST_PAD_STREAM_UNLOCK (pad);
4438 if (G_UNLIKELY (ret != GST_FLOW_OK))
4439 goto get_range_failed;
4441 GST_OBJECT_LOCK (pad);
4442 /* Before pushing the buffer to the peer pad, ensure that caps
4443 * are set on this pad */
4444 caps = GST_BUFFER_CAPS (*buffer);
4445 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4446 GST_OBJECT_UNLOCK (pad);
4448 if (G_UNLIKELY (caps_changed)) {
4449 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4450 /* this should usually work because the element produced the buffer */
4451 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4452 goto not_negotiated;
4459 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4460 "pulling range, but pad was flushing");
4461 GST_OBJECT_UNLOCK (pad);
4462 GST_PAD_STREAM_UNLOCK (pad);
4463 return GST_FLOW_WRONG_STATE;
4467 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4468 ("pullrange on pad %s:%s but it has no getrangefunction",
4469 GST_DEBUG_PAD_NAME (pad)));
4470 GST_PAD_STREAM_UNLOCK (pad);
4471 return GST_FLOW_NOT_SUPPORTED;
4475 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4476 "Dropping data after FALSE probe return");
4477 GST_PAD_STREAM_UNLOCK (pad);
4478 gst_buffer_unref (*buffer);
4480 return GST_FLOW_UNEXPECTED;
4485 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4486 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4487 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4492 gst_buffer_unref (*buffer);
4494 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4495 "getrange returned buffer of unaccaptable caps");
4496 return GST_FLOW_NOT_NEGOTIATED;
4501 * gst_pad_get_range:
4502 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4503 * @offset: The start offset of the buffer
4504 * @size: The length of the buffer
4505 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4506 * returns #GST_FLOW_ERROR if %NULL.
4508 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4509 * immediatly and @buffer is %NULL.
4511 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4512 * description of a getrange function. If @pad has no getrange function
4513 * installed (see gst_pad_set_getrange_function()) this function returns
4514 * #GST_FLOW_NOT_SUPPORTED.
4516 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4518 * Returns: a #GstFlowReturn from the pad.
4523 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4524 GstBuffer ** buffer)
4526 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4527 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4528 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4530 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4534 * gst_pad_pull_range:
4535 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4536 * @offset: The start offset of the buffer
4537 * @size: The length of the buffer
4538 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4539 * GST_FLOW_ERROR if %NULL.
4541 * Pulls a @buffer from the peer pad.
4543 * This function will first trigger the pad block signal if it was
4546 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4547 * function returns the result of gst_pad_get_range() on the peer pad.
4548 * See gst_pad_get_range() for a list of return values and for the
4549 * semantics of the arguments of this function.
4551 * @buffer's caps must either be unset or the same as what is already
4552 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4555 * Returns: a #GstFlowReturn from the peer pad.
4556 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4557 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4558 * @buffer may not be used or freed when any other return value than
4559 * #GST_FLOW_OK is returned.
4564 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4565 GstBuffer ** buffer)
4569 gboolean emit_signal;
4571 gboolean caps_changed;
4573 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4574 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4575 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4577 GST_OBJECT_LOCK (pad);
4579 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4580 handle_pad_block (pad);
4582 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4585 /* signal emision for the pad, peer has chance to emit when
4586 * we call _get_range() */
4587 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4589 gst_object_ref (peer);
4590 GST_OBJECT_UNLOCK (pad);
4592 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4594 gst_object_unref (peer);
4596 if (G_UNLIKELY (ret != GST_FLOW_OK))
4597 goto pull_range_failed;
4599 /* can only fire the signal if we have a valid buffer */
4600 if (G_UNLIKELY (emit_signal)) {
4601 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4605 GST_OBJECT_LOCK (pad);
4606 /* Before pushing the buffer to the peer pad, ensure that caps
4607 * are set on this pad */
4608 caps = GST_BUFFER_CAPS (*buffer);
4609 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4610 GST_OBJECT_UNLOCK (pad);
4612 /* we got a new datatype on the pad, see if it can handle it */
4613 if (G_UNLIKELY (caps_changed)) {
4614 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4615 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4616 goto not_negotiated;
4620 /* ERROR recovery here */
4623 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4624 "pulling range, but it was not linked");
4625 GST_OBJECT_UNLOCK (pad);
4626 return GST_FLOW_NOT_LINKED;
4631 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4632 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4633 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4638 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4639 "Dropping data after FALSE probe return");
4640 gst_buffer_unref (*buffer);
4642 return GST_FLOW_UNEXPECTED;
4646 gst_buffer_unref (*buffer);
4648 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4649 "pullrange returned buffer of different caps");
4650 return GST_FLOW_NOT_NEGOTIATED;
4655 * gst_pad_push_event:
4656 * @pad: a #GstPad to push the event to.
4657 * @event: (transfer full): the #GstEvent to send to the pad.
4659 * Sends the event to the peer of the given pad. This function is
4660 * mainly used by elements to send events to their peer
4663 * This function takes owership of the provided event so you should
4664 * gst_event_ref() it if you want to reuse the event after this call.
4666 * Returns: TRUE if the event was handled.
4671 gst_pad_push_event (GstPad * pad, GstEvent * event)
4676 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4677 g_return_val_if_fail (event != NULL, FALSE);
4678 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4680 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4682 GST_OBJECT_LOCK (pad);
4684 /* Two checks to be made:
4685 * . (un)set the FLUSHING flag for flushing events,
4686 * . handle pad blocking */
4687 switch (GST_EVENT_TYPE (event)) {
4688 case GST_EVENT_FLUSH_START:
4689 _priv_gst_pad_invalidate_cache (pad);
4690 GST_PAD_SET_FLUSHING (pad);
4692 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4693 /* flush start will have set the FLUSHING flag and will then
4694 * unlock all threads doing a GCond wait on the blocking pad. This
4695 * will typically unblock the STREAMING thread blocked on a pad. */
4696 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4697 "doing block signal.");
4698 GST_PAD_BLOCK_BROADCAST (pad);
4702 case GST_EVENT_FLUSH_STOP:
4703 GST_PAD_UNSET_FLUSHING (pad);
4705 /* if we are blocked, flush away the FLUSH_STOP event */
4706 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4707 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4712 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4713 /* block the event as long as the pad is blocked */
4714 if (handle_pad_block (pad) != GST_FLOW_OK)
4720 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4721 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4722 GST_EVENT_SRC (event) = gst_object_ref (pad);
4725 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4726 GST_OBJECT_UNLOCK (pad);
4728 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4731 GST_OBJECT_LOCK (pad);
4733 peerpad = GST_PAD_PEER (pad);
4734 if (peerpad == NULL)
4737 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4738 GST_EVENT_TYPE_NAME (event), peerpad);
4739 gst_object_ref (peerpad);
4740 GST_OBJECT_UNLOCK (pad);
4742 result = gst_pad_send_event (peerpad, event);
4744 /* Note: we gave away ownership of the event at this point */
4745 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4747 gst_object_unref (peerpad);
4751 /* ERROR handling */
4754 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4755 gst_event_unref (event);
4760 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4761 gst_event_unref (event);
4762 GST_OBJECT_UNLOCK (pad);
4767 GST_DEBUG_OBJECT (pad,
4768 "Not forwarding event since we're flushing and blocking");
4769 gst_event_unref (event);
4770 GST_OBJECT_UNLOCK (pad);
4776 * gst_pad_send_event:
4777 * @pad: a #GstPad to send the event to.
4778 * @event: (transfer full): the #GstEvent to send to the pad.
4780 * Sends the event to the pad. This function can be used
4781 * by applications to send events in the pipeline.
4783 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4784 * sink pad, @event should be a downstream event. For example, you would not
4785 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4786 * Furthermore, some downstream events have to be serialized with data flow,
4787 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4788 * the event needs to be serialized with data flow, this function will take the
4789 * pad's stream lock while calling its event function.
4791 * To find out whether an event type is upstream, downstream, or downstream and
4792 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4793 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4794 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4795 * plugin doesn't need to bother itself with this information; the core handles
4796 * all necessary locks and checks.
4798 * This function takes owership of the provided event so you should
4799 * gst_event_ref() it if you want to reuse the event after this call.
4801 * Returns: TRUE if the event was handled.
4804 gst_pad_send_event (GstPad * pad, GstEvent * event)
4806 gboolean result = FALSE;
4807 GstPadEventFunction eventfunc;
4808 gboolean serialized, need_unlock = FALSE;
4810 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4811 g_return_val_if_fail (event != NULL, FALSE);
4813 GST_OBJECT_LOCK (pad);
4814 if (GST_PAD_IS_SINK (pad)) {
4815 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4816 goto wrong_direction;
4817 serialized = GST_EVENT_IS_SERIALIZED (event);
4818 } else if (GST_PAD_IS_SRC (pad)) {
4819 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4820 goto wrong_direction;
4821 /* events on srcpad never are serialized */
4824 goto unknown_direction;
4826 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4827 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4828 GST_EVENT_SRC (event) = gst_object_ref (pad);
4832 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4833 GST_OBJECT_UNLOCK (pad);
4835 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4838 GST_OBJECT_LOCK (pad);
4841 switch (GST_EVENT_TYPE (event)) {
4842 case GST_EVENT_FLUSH_START:
4843 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4844 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4846 /* can't even accept a flush begin event when flushing */
4847 if (GST_PAD_IS_FLUSHING (pad))
4850 _priv_gst_pad_invalidate_cache (pad);
4851 GST_PAD_SET_FLUSHING (pad);
4852 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4854 case GST_EVENT_FLUSH_STOP:
4855 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4856 GST_PAD_UNSET_FLUSHING (pad);
4857 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4859 GST_OBJECT_UNLOCK (pad);
4860 /* grab stream lock */
4861 GST_PAD_STREAM_LOCK (pad);
4863 GST_OBJECT_LOCK (pad);
4866 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4867 GST_EVENT_TYPE_NAME (event));
4869 /* make this a little faster, no point in grabbing the lock
4870 * if the pad is allready flushing. */
4871 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4875 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4876 GST_OBJECT_UNLOCK (pad);
4877 GST_PAD_STREAM_LOCK (pad);
4879 GST_OBJECT_LOCK (pad);
4880 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4885 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4888 GST_OBJECT_UNLOCK (pad);
4890 result = eventfunc (pad, event);
4893 GST_PAD_STREAM_UNLOCK (pad);
4895 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4899 /* ERROR handling */
4902 g_warning ("pad %s:%s sending %s event in wrong direction",
4903 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4904 GST_OBJECT_UNLOCK (pad);
4905 gst_event_unref (event);
4910 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4911 GST_OBJECT_UNLOCK (pad);
4912 gst_event_unref (event);
4917 g_warning ("pad %s:%s has no event handler, file a bug.",
4918 GST_DEBUG_PAD_NAME (pad));
4919 GST_OBJECT_UNLOCK (pad);
4921 GST_PAD_STREAM_UNLOCK (pad);
4922 gst_event_unref (event);
4927 GST_OBJECT_UNLOCK (pad);
4929 GST_PAD_STREAM_UNLOCK (pad);
4930 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4931 "Received event on flushing pad. Discarding");
4932 gst_event_unref (event);
4937 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4938 gst_event_unref (event);
4944 * gst_pad_set_element_private:
4945 * @pad: the #GstPad to set the private data of.
4946 * @priv: The private data to attach to the pad.
4948 * Set the given private data gpointer on the pad.
4949 * This function can only be used by the element that owns the pad.
4950 * No locking is performed in this function.
4953 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4955 pad->element_private = priv;
4959 * gst_pad_get_element_private:
4960 * @pad: the #GstPad to get the private data of.
4962 * Gets the private data of a pad.
4963 * No locking is performed in this function.
4965 * Returns: (transfer none): a #gpointer to the private data.
4968 gst_pad_get_element_private (GstPad * pad)
4970 return pad->element_private;
4974 do_stream_status (GstPad * pad, GstStreamStatusType type,
4975 GThread * thread, GstTask * task)
4979 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4981 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4982 if (GST_IS_ELEMENT (parent)) {
4983 GstMessage *message;
4984 GValue value = { 0 };
4986 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4987 gchar *tname, *ename, *pname;
4989 /* create a good task name */
4990 ename = gst_element_get_name (parent);
4991 pname = gst_pad_get_name (pad);
4992 tname = g_strdup_printf ("%s:%s", ename, pname);
4996 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5000 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5003 g_value_init (&value, GST_TYPE_TASK);
5004 g_value_set_object (&value, task);
5005 gst_message_set_stream_status_object (message, &value);
5006 g_value_unset (&value);
5008 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5009 gst_element_post_message (parent, message);
5011 gst_object_unref (parent);
5016 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5018 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5023 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5025 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5029 static GstTaskThreadCallbacks thr_callbacks = {
5035 * gst_pad_start_task:
5036 * @pad: the #GstPad to start the task of
5037 * @func: the task function to call
5038 * @data: data passed to the task function
5040 * Starts a task that repeatedly calls @func with @data. This function
5041 * is mostly used in pad activation functions to start the dataflow.
5042 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5043 * before @func is called.
5045 * Returns: a %TRUE if the task could be started.
5048 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5053 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5054 g_return_val_if_fail (func != NULL, FALSE);
5056 GST_DEBUG_OBJECT (pad, "start task");
5058 GST_OBJECT_LOCK (pad);
5059 task = GST_PAD_TASK (pad);
5061 task = gst_task_create (func, data);
5062 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5063 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5064 GST_DEBUG_OBJECT (pad, "created task");
5065 GST_PAD_TASK (pad) = task;
5066 gst_object_ref (task);
5067 /* release lock to post the message */
5068 GST_OBJECT_UNLOCK (pad);
5070 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5072 gst_object_unref (task);
5074 GST_OBJECT_LOCK (pad);
5075 /* nobody else is supposed to have changed the pad now */
5076 if (GST_PAD_TASK (pad) != task)
5077 goto concurrent_stop;
5079 res = gst_task_set_state (task, GST_TASK_STARTED);
5080 GST_OBJECT_UNLOCK (pad);
5087 GST_OBJECT_UNLOCK (pad);
5093 * gst_pad_pause_task:
5094 * @pad: the #GstPad to pause the task of
5096 * Pause the task of @pad. This function will also wait until the
5097 * function executed by the task is finished if this function is not
5098 * called from the task function.
5100 * Returns: a TRUE if the task could be paused or FALSE when the pad
5104 gst_pad_pause_task (GstPad * pad)
5109 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5111 GST_DEBUG_OBJECT (pad, "pause task");
5113 GST_OBJECT_LOCK (pad);
5114 task = GST_PAD_TASK (pad);
5117 res = gst_task_set_state (task, GST_TASK_PAUSED);
5118 GST_OBJECT_UNLOCK (pad);
5120 /* wait for task function to finish, this lock is recursive so it does nothing
5121 * when the pause is called from the task itself */
5122 GST_PAD_STREAM_LOCK (pad);
5123 GST_PAD_STREAM_UNLOCK (pad);
5129 GST_DEBUG_OBJECT (pad, "pad has no task");
5130 GST_OBJECT_UNLOCK (pad);
5136 * gst_pad_stop_task:
5137 * @pad: the #GstPad to stop the task of
5139 * Stop the task of @pad. This function will also make sure that the
5140 * function executed by the task will effectively stop if not called
5141 * from the GstTaskFunction.
5143 * This function will deadlock if called from the GstTaskFunction of
5144 * the task. Use gst_task_pause() instead.
5146 * Regardless of whether the pad has a task, the stream lock is acquired and
5147 * released so as to ensure that streaming through this pad has finished.
5149 * Returns: a TRUE if the task could be stopped or FALSE on error.
5152 gst_pad_stop_task (GstPad * pad)
5157 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5159 GST_DEBUG_OBJECT (pad, "stop task");
5161 GST_OBJECT_LOCK (pad);
5162 task = GST_PAD_TASK (pad);
5165 GST_PAD_TASK (pad) = NULL;
5166 res = gst_task_set_state (task, GST_TASK_STOPPED);
5167 GST_OBJECT_UNLOCK (pad);
5169 GST_PAD_STREAM_LOCK (pad);
5170 GST_PAD_STREAM_UNLOCK (pad);
5172 if (!gst_task_join (task))
5175 gst_object_unref (task);
5181 GST_DEBUG_OBJECT (pad, "no task");
5182 GST_OBJECT_UNLOCK (pad);
5184 GST_PAD_STREAM_LOCK (pad);
5185 GST_PAD_STREAM_UNLOCK (pad);
5187 /* this is not an error */
5192 /* this is bad, possibly the application tried to join the task from
5193 * the task's thread. We install the task again so that it will be stopped
5194 * again from the right thread next time hopefully. */
5195 GST_OBJECT_LOCK (pad);
5196 GST_DEBUG_OBJECT (pad, "join failed");
5197 /* we can only install this task if there was no other task */
5198 if (GST_PAD_TASK (pad) == NULL)
5199 GST_PAD_TASK (pad) = task;
5200 GST_OBJECT_UNLOCK (pad);