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 */
105 static GstPadPushCache _pad_cache_invalid = { NULL, };
107 #define PAD_CACHE_INVALID (&_pad_cache_invalid)
109 #define GST_PAD_GET_PRIVATE(obj) \
110 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
112 struct _GstPadPrivate
114 GstPadPushCache *cache_ptr;
117 static void gst_pad_dispose (GObject * object);
118 static void gst_pad_finalize (GObject * object);
119 static void gst_pad_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_pad_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static GstFlowReturn handle_pad_block (GstPad * pad);
125 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
126 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
127 static gboolean gst_pad_activate_default (GstPad * pad);
128 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
130 static GstObjectClass *parent_class = NULL;
131 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
133 static GParamSpec *pspec_caps = NULL;
135 /* quarks for probe signals */
136 static GQuark buffer_quark;
137 static GQuark event_quark;
146 static GstFlowQuarks flow_quarks[] = {
147 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
148 {GST_FLOW_RESEND, "resend", 0},
149 {GST_FLOW_OK, "ok", 0},
150 {GST_FLOW_NOT_LINKED, "not-linked", 0},
151 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
152 {GST_FLOW_UNEXPECTED, "unexpected", 0},
153 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
154 {GST_FLOW_ERROR, "error", 0},
155 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
156 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
161 * @ret: a #GstFlowReturn to get the name of.
163 * Gets a string representing the given flow return.
165 * Returns: a static string with the name of the flow return.
167 G_CONST_RETURN gchar *
168 gst_flow_get_name (GstFlowReturn ret)
172 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
174 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
175 if (ret == flow_quarks[i].ret)
176 return flow_quarks[i].name;
183 * @ret: a #GstFlowReturn to get the quark of.
185 * Get the unique quark for the given GstFlowReturn.
187 * Returns: the quark associated with the flow return or 0 if an
188 * invalid return was specified.
191 gst_flow_to_quark (GstFlowReturn ret)
195 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
197 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
198 if (ret == flow_quarks[i].ret)
199 return flow_quarks[i].quark;
208 buffer_quark = g_quark_from_static_string ("buffer"); \
209 event_quark = g_quark_from_static_string ("event"); \
211 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
212 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
215 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
216 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
219 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
222 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
223 GValue * return_accu, const GValue * handler_return, gpointer dummy)
225 gboolean ret = g_value_get_boolean (handler_return);
227 GST_DEBUG ("accumulated %d", ret);
228 g_value_set_boolean (return_accu, ret);
234 default_have_data (GstPad * pad, GstMiniObject * o)
240 gst_pad_class_init (GstPadClass * klass)
242 GObjectClass *gobject_class;
243 GstObjectClass *gstobject_class;
245 gobject_class = G_OBJECT_CLASS (klass);
246 gstobject_class = GST_OBJECT_CLASS (klass);
248 g_type_class_add_private (klass, sizeof (GstPadPrivate));
250 parent_class = g_type_class_peek_parent (klass);
252 gobject_class->dispose = gst_pad_dispose;
253 gobject_class->finalize = gst_pad_finalize;
254 gobject_class->set_property = gst_pad_set_property;
255 gobject_class->get_property = gst_pad_get_property;
259 * @pad: the pad that emitted the signal
260 * @peer: the peer pad that has been connected
262 * Signals that a pad has been linked to the peer pad.
264 gst_pad_signals[PAD_LINKED] =
265 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
266 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
267 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
270 * @pad: the pad that emitted the signal
271 * @peer: the peer pad that has been disconnected
273 * Signals that a pad has been unlinked from the peer pad.
275 gst_pad_signals[PAD_UNLINKED] =
276 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
277 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
278 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
280 * GstPad::request-link:
281 * @pad: the pad that emitted the signal
282 * @peer: the peer pad for which a connection is requested
284 * Signals that a pad connection has been requested.
286 gst_pad_signals[PAD_REQUEST_LINK] =
287 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
288 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
289 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
293 * @pad: the pad that emitted the signal
294 * @mini_obj: new data
296 * Signals that new data is available on the pad. This signal is used
297 * internally for implementing pad probes.
298 * See gst_pad_add_*_probe functions.
300 * Returns: %TRUE to keep the data, %FALSE to drop it
302 gst_pad_signals[PAD_HAVE_DATA] =
303 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
304 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
305 G_STRUCT_OFFSET (GstPadClass, have_data),
306 _gst_do_pass_data_accumulator,
307 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
309 pspec_caps = g_param_spec_boxed ("caps", "Caps",
310 "The capabilities of the pad", GST_TYPE_CAPS,
311 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
312 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
314 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
315 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
316 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
317 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
318 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
319 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
320 g_param_spec_object ("template", "Template",
321 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324 gstobject_class->path_string_separator = ".";
326 /* Register common function pointer descriptions */
327 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
328 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
329 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
330 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
331 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
332 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
334 /* from gstutils.c */
335 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_fixed_caps_func);
337 klass->have_data = default_have_data;
341 gst_pad_init (GstPad * pad)
343 pad->priv = GST_PAD_GET_PRIVATE (pad);
345 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
346 GST_PAD_PEER (pad) = NULL;
348 GST_PAD_CHAINFUNC (pad) = NULL;
350 GST_PAD_LINKFUNC (pad) = NULL;
352 GST_PAD_CAPS (pad) = NULL;
353 GST_PAD_GETCAPSFUNC (pad) = NULL;
355 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
356 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
357 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
358 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
359 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
361 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
363 pad->do_buffer_signals = 0;
364 pad->do_event_signals = 0;
366 GST_PAD_SET_FLUSHING (pad);
368 /* FIXME 0.11: Store this directly in the instance struct */
369 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
370 g_static_rec_mutex_init (pad->stream_rec_lock);
372 pad->block_cond = g_cond_new ();
376 gst_pad_dispose (GObject * object)
378 GstPad *pad = GST_PAD_CAST (object);
381 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
383 /* unlink the peer pad */
384 if ((peer = gst_pad_get_peer (pad))) {
385 /* window for MT unsafeness, someone else could unlink here
386 * and then we call unlink with wrong pads. The unlink
387 * function would catch this and safely return failed. */
388 if (GST_PAD_IS_SRC (pad))
389 gst_pad_unlink (pad, peer);
391 gst_pad_unlink (peer, pad);
393 gst_object_unref (peer);
397 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
399 gst_pad_set_pad_template (pad, NULL);
401 if (pad->block_destroy_data && pad->block_data) {
402 pad->block_destroy_data (pad->block_data);
403 pad->block_data = NULL;
407 if (GST_PAD_CONTEXT (pad))
408 gst_context_replace (&GST_PAD_CONTEXT (pad), NULL);
411 G_OBJECT_CLASS (parent_class)->dispose (object);
415 gst_pad_finalize (GObject * object)
417 GstPad *pad = GST_PAD_CAST (object);
420 /* in case the task is still around, clean it up */
421 if ((task = GST_PAD_TASK (pad))) {
422 gst_task_join (task);
423 GST_PAD_TASK (pad) = NULL;
424 gst_object_unref (task);
427 if (pad->stream_rec_lock) {
428 g_static_rec_mutex_free (pad->stream_rec_lock);
429 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
430 pad->stream_rec_lock = NULL;
432 if (pad->block_cond) {
433 g_cond_free (pad->block_cond);
434 pad->block_cond = NULL;
437 G_OBJECT_CLASS (parent_class)->finalize (object);
441 gst_pad_set_property (GObject * object, guint prop_id,
442 const GValue * value, GParamSpec * pspec)
444 g_return_if_fail (GST_IS_PAD (object));
447 case PAD_PROP_DIRECTION:
448 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
450 case PAD_PROP_TEMPLATE:
451 gst_pad_set_pad_template (GST_PAD_CAST (object),
452 (GstPadTemplate *) g_value_get_object (value));
455 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
461 gst_pad_get_property (GObject * object, guint prop_id,
462 GValue * value, GParamSpec * pspec)
464 g_return_if_fail (GST_IS_PAD (object));
468 GST_OBJECT_LOCK (object);
469 g_value_set_boxed (value, GST_PAD_CAPS (object));
470 GST_OBJECT_UNLOCK (object);
472 case PAD_PROP_DIRECTION:
473 g_value_set_enum (value, GST_PAD_DIRECTION (object));
475 case PAD_PROP_TEMPLATE:
476 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 * @name: the name of the new pad.
487 * @direction: the #GstPadDirection of the pad.
489 * Creates a new pad with the given name in the given direction.
490 * If name is NULL, a guaranteed unique name (across all pads)
492 * This function makes a copy of the name so you can safely free the name.
494 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
499 gst_pad_new (const gchar * name, GstPadDirection direction)
501 return g_object_new (GST_TYPE_PAD,
502 "name", name, "direction", direction, NULL);
506 * gst_pad_new_from_template:
507 * @templ: the pad template to use
508 * @name: the name of the element
510 * Creates a new pad with the given name from the given template.
511 * If name is NULL, a guaranteed unique name (across all pads)
513 * This function makes a copy of the name so you can safely free the name.
515 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
518 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
520 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
522 return g_object_new (GST_TYPE_PAD,
523 "name", name, "direction", templ->direction, "template", templ, NULL);
527 * gst_pad_new_from_static_template:
528 * @templ: the #GstStaticPadTemplate to use
529 * @name: the name of the element
531 * Creates a new pad with the given name from the given static template.
532 * If name is NULL, a guaranteed unique name (across all pads)
534 * This function makes a copy of the name so you can safely free the name.
536 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
539 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
543 GstPadTemplate *template;
545 template = gst_static_pad_template_get (templ);
546 pad = gst_pad_new_from_template (template, name);
547 gst_object_unref (template);
552 * gst_pad_get_direction:
553 * @pad: a #GstPad to get the direction of.
555 * Gets the direction of the pad. The direction of the pad is
556 * decided at construction time so this function does not take
559 * Returns: the #GstPadDirection of the pad.
564 gst_pad_get_direction (GstPad * pad)
566 GstPadDirection result;
568 /* PAD_UNKNOWN is a little silly but we need some sort of
569 * error return value */
570 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
572 result = GST_PAD_DIRECTION (pad);
578 gst_pad_activate_default (GstPad * pad)
580 return gst_pad_activate_push (pad, TRUE);
584 pre_activate (GstPad * pad, GstActivateMode new_mode)
587 case GST_ACTIVATE_PUSH:
588 case GST_ACTIVATE_PULL:
589 GST_OBJECT_LOCK (pad);
590 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
592 GST_PAD_UNSET_FLUSHING (pad);
593 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
594 GST_OBJECT_UNLOCK (pad);
596 case GST_ACTIVATE_NONE:
597 GST_OBJECT_LOCK (pad);
598 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
599 _priv_gst_pad_invalidate_cache (pad);
600 GST_PAD_SET_FLUSHING (pad);
601 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
602 /* unlock blocked pads so element can resume and stop */
603 GST_PAD_BLOCK_BROADCAST (pad);
604 GST_OBJECT_UNLOCK (pad);
610 post_activate (GstPad * pad, GstActivateMode new_mode)
613 case GST_ACTIVATE_PUSH:
614 case GST_ACTIVATE_PULL:
617 case GST_ACTIVATE_NONE:
618 /* ensures that streaming stops */
619 GST_PAD_STREAM_LOCK (pad);
620 GST_DEBUG_OBJECT (pad, "stopped streaming");
622 gst_context_clear (pad->context);
623 GST_PAD_STREAM_UNLOCK (pad);
629 * gst_pad_set_active:
630 * @pad: the #GstPad to activate or deactivate.
631 * @active: whether or not the pad should be active.
633 * Activates or deactivates the given pad.
634 * Normally called from within core state change functions.
636 * If @active, makes sure the pad is active. If it is already active, either in
637 * push or pull mode, just return. Otherwise dispatches to the pad's activate
638 * function to perform the actual activation.
640 * If not @active, checks the pad's current mode and calls
641 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
644 * Returns: #TRUE if the operation was successful.
649 gst_pad_set_active (GstPad * pad, gboolean active)
652 gboolean ret = FALSE;
654 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
656 GST_OBJECT_LOCK (pad);
657 old = GST_PAD_ACTIVATE_MODE (pad);
658 GST_OBJECT_UNLOCK (pad);
662 case GST_ACTIVATE_PUSH:
663 GST_DEBUG_OBJECT (pad, "activating pad from push");
666 case GST_ACTIVATE_PULL:
667 GST_DEBUG_OBJECT (pad, "activating pad from pull");
670 case GST_ACTIVATE_NONE:
671 GST_DEBUG_OBJECT (pad, "activating pad from none");
672 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
675 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
680 case GST_ACTIVATE_PUSH:
681 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
682 ret = gst_pad_activate_push (pad, FALSE);
684 case GST_ACTIVATE_PULL:
685 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
686 ret = gst_pad_activate_pull (pad, FALSE);
688 case GST_ACTIVATE_NONE:
689 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
693 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
699 GST_OBJECT_LOCK (pad);
701 g_critical ("Failed to deactivate pad %s:%s, very bad",
702 GST_DEBUG_PAD_NAME (pad));
704 GST_WARNING_OBJECT (pad, "Failed to activate pad");
706 GST_OBJECT_UNLOCK (pad);
713 * gst_pad_activate_pull:
714 * @pad: the #GstPad to activate or deactivate.
715 * @active: whether or not the pad should be active.
717 * Activates or deactivates the given pad in pull mode via dispatching to the
718 * pad's activatepullfunc. For use from within pad activation functions only.
719 * When called on sink pads, will first proxy the call to the peer pad, which
720 * is expected to activate its internally linked pads from within its
721 * activate_pull function.
723 * If you don't know what this is, you probably don't want to call it.
725 * Returns: TRUE if the operation was successful.
730 gst_pad_activate_pull (GstPad * pad, gboolean active)
732 GstActivateMode old, new;
735 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
737 GST_OBJECT_LOCK (pad);
738 old = GST_PAD_ACTIVATE_MODE (pad);
739 GST_OBJECT_UNLOCK (pad);
743 case GST_ACTIVATE_PULL:
744 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
746 case GST_ACTIVATE_PUSH:
747 GST_DEBUG_OBJECT (pad,
748 "activating pad from push, deactivate push first");
749 /* pad was activate in the wrong direction, deactivate it
750 * and reactivate it in pull mode */
751 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
752 goto deactivate_failed;
753 /* fallthrough, pad is deactivated now. */
754 case GST_ACTIVATE_NONE:
755 GST_DEBUG_OBJECT (pad, "activating pad from none");
760 case GST_ACTIVATE_NONE:
761 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
763 case GST_ACTIVATE_PUSH:
764 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
765 /* pad was activated in the other direction, deactivate it
766 * in push mode, this should not happen... */
767 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
768 goto deactivate_failed;
769 /* everything is fine now */
771 case GST_ACTIVATE_PULL:
772 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
777 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
778 if ((peer = gst_pad_get_peer (pad))) {
779 GST_DEBUG_OBJECT (pad, "calling peer");
780 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
782 gst_object_unref (peer);
784 /* there is no peer, this is only fatal when we activate. When we
785 * deactivate, we must assume the application has unlinked the peer and
786 * will deactivate it eventually. */
790 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
793 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
794 goto failure; /* Can't activate pull on a src without a
798 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
799 pre_activate (pad, new);
801 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
802 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
805 /* can happen for sinks of passthrough elements */
808 post_activate (pad, new);
810 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
811 active ? "activated" : "deactivated");
817 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
818 active ? "activated" : "deactivated");
823 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
824 "failed to %s in switch to pull from mode %d",
825 (active ? "activate" : "deactivate"), old);
830 GST_OBJECT_LOCK (peer);
831 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
832 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
833 GST_OBJECT_UNLOCK (peer);
834 gst_object_unref (peer);
839 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
845 GST_OBJECT_LOCK (pad);
846 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
847 active ? "activate" : "deactivate");
848 _priv_gst_pad_invalidate_cache (pad);
849 GST_PAD_SET_FLUSHING (pad);
850 GST_PAD_ACTIVATE_MODE (pad) = old;
851 GST_OBJECT_UNLOCK (pad);
857 * gst_pad_activate_push:
858 * @pad: the #GstPad to activate or deactivate.
859 * @active: whether the pad should be active or not.
861 * Activates or deactivates the given pad in push mode via dispatching to the
862 * pad's activatepushfunc. For use from within pad activation functions only.
864 * If you don't know what this is, you probably don't want to call it.
866 * Returns: %TRUE if the operation was successful.
871 gst_pad_activate_push (GstPad * pad, gboolean active)
873 GstActivateMode old, new;
875 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
876 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
877 active ? "activated" : "deactivated");
879 GST_OBJECT_LOCK (pad);
880 old = GST_PAD_ACTIVATE_MODE (pad);
881 GST_OBJECT_UNLOCK (pad);
885 case GST_ACTIVATE_PUSH:
886 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
888 case GST_ACTIVATE_PULL:
889 GST_DEBUG_OBJECT (pad,
890 "activating pad from push, deactivating pull first");
891 /* pad was activate in the wrong direction, deactivate it
892 * an reactivate it in push mode */
893 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
894 goto deactivate_failed;
895 /* fallthrough, pad is deactivated now. */
896 case GST_ACTIVATE_NONE:
897 GST_DEBUG_OBJECT (pad, "activating pad from none");
902 case GST_ACTIVATE_NONE:
903 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
905 case GST_ACTIVATE_PULL:
906 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
907 /* pad was activated in the other direction, deactivate it
908 * in pull mode, this should not happen... */
909 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
910 goto deactivate_failed;
911 /* everything is fine now */
913 case GST_ACTIVATE_PUSH:
914 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
919 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
920 pre_activate (pad, new);
922 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
923 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
927 /* quite ok, element relies on state change func to prepare itself */
930 post_activate (pad, new);
932 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
933 active ? "activated" : "deactivated");
938 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
939 active ? "activated" : "deactivated");
944 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
945 "failed to %s in switch to push from mode %d",
946 (active ? "activate" : "deactivate"), old);
951 GST_OBJECT_LOCK (pad);
952 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
953 active ? "activate" : "deactivate");
954 _priv_gst_pad_invalidate_cache (pad);
955 GST_PAD_SET_FLUSHING (pad);
956 GST_PAD_ACTIVATE_MODE (pad) = old;
957 GST_OBJECT_UNLOCK (pad);
964 * @pad: the #GstPad to query
966 * Query if a pad is active
968 * Returns: TRUE if the pad is active.
973 gst_pad_is_active (GstPad * pad)
975 gboolean result = FALSE;
977 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
979 GST_OBJECT_LOCK (pad);
980 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
981 GST_OBJECT_UNLOCK (pad);
987 * gst_pad_set_blocked_async_full:
988 * @pad: the #GstPad to block or unblock
989 * @blocked: boolean indicating whether the pad should be blocked or unblocked
990 * @callback: #GstPadBlockCallback that will be called when the
992 * @user_data: (closure): user data passed to the callback
993 * @destroy_data: #GDestroyNotify for user_data
995 * Blocks or unblocks the dataflow on a pad. The provided callback
996 * is called when the operation succeeds; this happens right before the next
997 * attempt at pushing a buffer on the pad.
999 * This can take a while as the pad can only become blocked when real dataflow
1001 * When the pipeline is stalled, for example in PAUSED, this can
1002 * take an indeterminate amount of time.
1003 * You can pass NULL as the callback to make this call block. Be careful with
1004 * this blocking call as it might not return for reasons stated above.
1007 * Pad block handlers are only called for source pads in push mode
1008 * and sink pads in pull mode.
1011 * Returns: TRUE if the pad could be blocked. This function can fail if the
1012 * wrong parameters were passed or the pad was already in the requested state.
1019 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1020 GstPadBlockCallback callback, gpointer user_data,
1021 GDestroyNotify destroy_data)
1023 gboolean was_blocked = FALSE;
1025 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1027 GST_OBJECT_LOCK (pad);
1029 was_blocked = GST_PAD_IS_BLOCKED (pad);
1031 if (G_UNLIKELY (was_blocked == blocked))
1032 goto had_right_state;
1035 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1037 _priv_gst_pad_invalidate_cache (pad);
1038 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1040 if (pad->block_destroy_data && pad->block_data)
1041 pad->block_destroy_data (pad->block_data);
1043 pad->block_callback = callback;
1044 pad->block_data = user_data;
1045 pad->block_destroy_data = destroy_data;
1046 pad->block_callback_called = FALSE;
1048 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1049 GST_PAD_BLOCK_WAIT (pad);
1050 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1053 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1055 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1057 if (pad->block_destroy_data && pad->block_data)
1058 pad->block_destroy_data (pad->block_data);
1060 pad->block_callback = callback;
1061 pad->block_data = user_data;
1062 pad->block_destroy_data = destroy_data;
1063 pad->block_callback_called = FALSE;
1065 GST_PAD_BLOCK_BROADCAST (pad);
1067 /* no callback, wait for the unblock to happen */
1068 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1069 GST_PAD_BLOCK_WAIT (pad);
1070 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1073 GST_OBJECT_UNLOCK (pad);
1079 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1080 "pad was in right state (%d)", was_blocked);
1081 GST_OBJECT_UNLOCK (pad);
1088 * gst_pad_set_blocked_async:
1089 * @pad: the #GstPad to block or unblock
1090 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1091 * @callback: #GstPadBlockCallback that will be called when the
1092 * operation succeeds
1093 * @user_data: (closure): user data passed to the callback
1095 * Blocks or unblocks the dataflow on a pad. The provided callback
1096 * is called when the operation succeeds; this happens right before the next
1097 * attempt at pushing a buffer on the pad.
1099 * This can take a while as the pad can only become blocked when real dataflow
1101 * When the pipeline is stalled, for example in PAUSED, this can
1102 * take an indeterminate amount of time.
1103 * You can pass NULL as the callback to make this call block. Be careful with
1104 * this blocking call as it might not return for reasons stated above.
1107 * Pad block handlers are only called for source pads in push mode
1108 * and sink pads in pull mode.
1111 * Returns: TRUE if the pad could be blocked. This function can fail if the
1112 * wrong parameters were passed or the pad was already in the requested state.
1117 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1118 GstPadBlockCallback callback, gpointer user_data)
1120 return gst_pad_set_blocked_async_full (pad, blocked,
1121 callback, user_data, NULL);
1125 * gst_pad_set_blocked:
1126 * @pad: the #GstPad to block or unblock
1127 * @blocked: boolean indicating we should block or unblock
1129 * Blocks or unblocks the dataflow on a pad. This function is
1130 * a shortcut for gst_pad_set_blocked_async() with a NULL
1134 * Pad blocks are only possible for source pads in push mode
1135 * and sink pads in pull mode.
1138 * Returns: TRUE if the pad could be blocked. This function can fail if the
1139 * wrong parameters were passed or the pad was already in the requested state.
1144 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1146 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1150 * gst_pad_is_blocked:
1151 * @pad: the #GstPad to query
1153 * Checks if the pad is blocked or not. This function returns the
1154 * last requested state of the pad. It is not certain that the pad
1155 * is actually blocking at this point (see gst_pad_is_blocking()).
1157 * Returns: TRUE if the pad is blocked.
1162 gst_pad_is_blocked (GstPad * pad)
1164 gboolean result = FALSE;
1166 g_return_val_if_fail (GST_IS_PAD (pad), result);
1168 GST_OBJECT_LOCK (pad);
1169 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1170 GST_OBJECT_UNLOCK (pad);
1176 * gst_pad_is_blocking:
1177 * @pad: the #GstPad to query
1179 * Checks if the pad is blocking or not. This is a guaranteed state
1180 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1182 * Returns: TRUE if the pad is blocking.
1189 gst_pad_is_blocking (GstPad * pad)
1191 gboolean result = FALSE;
1193 g_return_val_if_fail (GST_IS_PAD (pad), result);
1195 GST_OBJECT_LOCK (pad);
1196 /* the blocking flag is only valid if the pad is not flushing */
1197 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1198 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1199 GST_OBJECT_UNLOCK (pad);
1205 * gst_pad_set_activate_function:
1207 * @activate: the #GstPadActivateFunction to set.
1209 * Sets the given activate function for @pad. The activate function will
1210 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1211 * the actual activation. Only makes sense to set on sink pads.
1213 * Call this function if your sink pad can start a pull-based task.
1216 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1218 g_return_if_fail (GST_IS_PAD (pad));
1220 GST_PAD_ACTIVATEFUNC (pad) = activate;
1221 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1222 GST_DEBUG_FUNCPTR_NAME (activate));
1226 * gst_pad_set_activatepull_function:
1228 * @activatepull: the #GstPadActivateModeFunction to set.
1230 * Sets the given activate_pull function for the pad. An activate_pull function
1231 * prepares the element and any upstream connections for pulling. See XXX
1232 * part-activation.txt for details.
1235 gst_pad_set_activatepull_function (GstPad * pad,
1236 GstPadActivateModeFunction activatepull)
1238 g_return_if_fail (GST_IS_PAD (pad));
1240 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1241 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1242 GST_DEBUG_FUNCPTR_NAME (activatepull));
1246 * gst_pad_set_activatepush_function:
1248 * @activatepush: the #GstPadActivateModeFunction to set.
1250 * Sets the given activate_push function for the pad. An activate_push function
1251 * prepares the element for pushing. See XXX part-activation.txt for details.
1254 gst_pad_set_activatepush_function (GstPad * pad,
1255 GstPadActivateModeFunction activatepush)
1257 g_return_if_fail (GST_IS_PAD (pad));
1259 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1260 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1261 GST_DEBUG_FUNCPTR_NAME (activatepush));
1265 * gst_pad_set_chain_function:
1266 * @pad: a sink #GstPad.
1267 * @chain: the #GstPadChainFunction to set.
1269 * Sets the given chain function for the pad. The chain function is called to
1270 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1273 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1275 g_return_if_fail (GST_IS_PAD (pad));
1276 g_return_if_fail (GST_PAD_IS_SINK (pad));
1278 GST_PAD_CHAINFUNC (pad) = chain;
1279 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1280 GST_DEBUG_FUNCPTR_NAME (chain));
1284 * gst_pad_set_chain_list_function:
1285 * @pad: a sink #GstPad.
1286 * @chainlist: the #GstPadChainListFunction to set.
1288 * Sets the given chain list function for the pad. The chainlist function is
1289 * called to process a #GstBufferList input buffer list. See
1290 * #GstPadChainListFunction for more details.
1295 gst_pad_set_chain_list_function (GstPad * pad,
1296 GstPadChainListFunction chainlist)
1298 g_return_if_fail (GST_IS_PAD (pad));
1299 g_return_if_fail (GST_PAD_IS_SINK (pad));
1301 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1302 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1303 GST_DEBUG_FUNCPTR_NAME (chainlist));
1307 * gst_pad_set_getrange_function:
1308 * @pad: a source #GstPad.
1309 * @get: the #GstPadGetRangeFunction to set.
1311 * Sets the given getrange function for the pad. The getrange function is
1312 * called to produce a new #GstBuffer to start the processing pipeline. see
1313 * #GstPadGetRangeFunction for a description of the getrange function.
1316 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1318 g_return_if_fail (GST_IS_PAD (pad));
1319 g_return_if_fail (GST_PAD_IS_SRC (pad));
1321 GST_PAD_GETRANGEFUNC (pad) = get;
1323 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1324 GST_DEBUG_FUNCPTR_NAME (get));
1328 * gst_pad_set_checkgetrange_function:
1329 * @pad: a source #GstPad.
1330 * @check: the #GstPadCheckGetRangeFunction to set.
1332 * Sets the given checkgetrange function for the pad. Implement this function
1333 * on a pad if you dynamically support getrange based scheduling on the pad.
1336 gst_pad_set_checkgetrange_function (GstPad * pad,
1337 GstPadCheckGetRangeFunction check)
1339 g_return_if_fail (GST_IS_PAD (pad));
1340 g_return_if_fail (GST_PAD_IS_SRC (pad));
1342 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1344 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1345 GST_DEBUG_FUNCPTR_NAME (check));
1349 * gst_pad_set_event_function:
1350 * @pad: a #GstPad of either direction.
1351 * @event: the #GstPadEventFunction to set.
1353 * Sets the given event handler for the pad.
1356 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1358 g_return_if_fail (GST_IS_PAD (pad));
1360 GST_PAD_EVENTFUNC (pad) = event;
1362 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1363 GST_DEBUG_FUNCPTR_NAME (event));
1367 * gst_pad_set_query_function:
1368 * @pad: a #GstPad of either direction.
1369 * @query: the #GstPadQueryFunction to set.
1371 * Set the given query function for the pad.
1374 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1376 g_return_if_fail (GST_IS_PAD (pad));
1378 GST_PAD_QUERYFUNC (pad) = query;
1380 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1381 GST_DEBUG_FUNCPTR_NAME (query));
1385 * gst_pad_set_query_type_function:
1386 * @pad: a #GstPad of either direction.
1387 * @type_func: the #GstPadQueryTypeFunction to set.
1389 * Set the given query type function for the pad.
1392 gst_pad_set_query_type_function (GstPad * pad,
1393 GstPadQueryTypeFunction type_func)
1395 g_return_if_fail (GST_IS_PAD (pad));
1397 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1399 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1400 GST_DEBUG_FUNCPTR_NAME (type_func));
1404 * gst_pad_get_query_types:
1407 * Get an array of supported queries that can be performed
1410 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1413 const GstQueryType *
1414 gst_pad_get_query_types (GstPad * pad)
1416 GstPadQueryTypeFunction func;
1418 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1420 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1432 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1434 *data = gst_pad_get_query_types (pad);
1440 * gst_pad_get_query_types_default:
1443 * Invoke the default dispatcher for the query types on
1446 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1447 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1448 * query types function.
1450 const GstQueryType *
1451 gst_pad_get_query_types_default (GstPad * pad)
1453 GstQueryType *result = NULL;
1455 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1457 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1458 gst_pad_get_query_types_dispatcher, &result);
1464 * gst_pad_set_iterate_internal_links_function:
1465 * @pad: a #GstPad of either direction.
1466 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1468 * Sets the given internal link iterator function for the pad.
1473 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1474 GstPadIterIntLinkFunction iterintlink)
1476 g_return_if_fail (GST_IS_PAD (pad));
1478 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1479 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1480 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1484 * gst_pad_set_link_function:
1486 * @link: the #GstPadLinkFunction to set.
1488 * Sets the given link function for the pad. It will be called when
1489 * the pad is linked with another pad.
1491 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1494 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1495 * cannot be made for some reason.
1497 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1498 * of the peer sink pad, if present.
1501 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1503 g_return_if_fail (GST_IS_PAD (pad));
1505 GST_PAD_LINKFUNC (pad) = link;
1506 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1507 GST_DEBUG_FUNCPTR_NAME (link));
1511 * gst_pad_set_unlink_function:
1513 * @unlink: the #GstPadUnlinkFunction to set.
1515 * Sets the given unlink function for the pad. It will be called
1516 * when the pad is unlinked.
1519 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1521 g_return_if_fail (GST_IS_PAD (pad));
1523 GST_PAD_UNLINKFUNC (pad) = unlink;
1524 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1525 GST_DEBUG_FUNCPTR_NAME (unlink));
1529 * gst_pad_set_getcaps_function:
1531 * @getcaps: the #GstPadGetCapsFunction to set.
1533 * Sets the given getcaps function for the pad. @getcaps should return the
1534 * allowable caps for a pad in the context of the element's state, its link to
1535 * other elements, and the devices or files it has opened. These caps must be a
1536 * subset of the pad template caps. In the NULL state with no links, @getcaps
1537 * should ideally return the same caps as the pad template. In rare
1538 * circumstances, an object property can affect the caps returned by @getcaps,
1539 * but this is discouraged.
1541 * You do not need to call this function if @pad's allowed caps are always the
1542 * same as the pad template caps. This can only be true if the padtemplate
1543 * has fixed simple caps.
1545 * For most filters, the caps returned by @getcaps is directly affected by the
1546 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1547 * the srcpad's getcaps function is directly related to the stream data. Again,
1548 * @getcaps should return the most specific caps it reasonably can, since this
1549 * helps with autoplugging.
1551 * Note that the return value from @getcaps is owned by the caller, so the
1552 * caller should unref the caps after usage.
1555 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1557 g_return_if_fail (GST_IS_PAD (pad));
1559 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1560 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1561 GST_DEBUG_FUNCPTR_NAME (getcaps));
1565 * gst_pad_set_acceptcaps_function:
1567 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1569 * Sets the given acceptcaps function for the pad. The acceptcaps function
1570 * will be called to check if the pad can accept the given caps. Setting the
1571 * acceptcaps function to NULL restores the default behaviour of allowing
1572 * any caps that matches the caps from gst_pad_get_caps().
1575 gst_pad_set_acceptcaps_function (GstPad * pad,
1576 GstPadAcceptCapsFunction acceptcaps)
1578 g_return_if_fail (GST_IS_PAD (pad));
1580 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1581 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1582 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1586 * gst_pad_set_fixatecaps_function:
1588 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1590 * Sets the given fixatecaps function for the pad. The fixatecaps function
1591 * will be called whenever the default values for a GstCaps needs to be
1595 gst_pad_set_fixatecaps_function (GstPad * pad,
1596 GstPadFixateCapsFunction fixatecaps)
1598 g_return_if_fail (GST_IS_PAD (pad));
1600 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1601 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1602 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1606 * gst_pad_set_setcaps_function:
1608 * @setcaps: the #GstPadSetCapsFunction to set.
1610 * Sets the given setcaps function for the pad. The setcaps function
1611 * will be called whenever a buffer with a new media type is pushed or
1612 * pulled from the pad. The pad/element needs to update its internal
1613 * structures to process the new media type. If this new type is not
1614 * acceptable, the setcaps function should return FALSE.
1617 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1619 g_return_if_fail (GST_IS_PAD (pad));
1621 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1622 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1623 GST_DEBUG_FUNCPTR_NAME (setcaps));
1628 * @srcpad: the source #GstPad to unlink.
1629 * @sinkpad: the sink #GstPad to unlink.
1631 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1632 * signal on both pads.
1634 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1635 * the pads were not linked together.
1640 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1642 gboolean result = FALSE;
1643 GstElement *parent = NULL;
1645 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1646 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1647 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1648 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1650 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1651 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1652 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1654 /* We need to notify the parent before taking any pad locks as the bin in
1655 * question might be waiting for a lock on the pad while holding its lock
1656 * that our message will try to take. */
1657 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1658 if (GST_IS_ELEMENT (parent)) {
1659 gst_element_post_message (parent,
1660 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1661 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1663 gst_object_unref (parent);
1668 GST_OBJECT_LOCK (srcpad);
1670 GST_OBJECT_LOCK (sinkpad);
1672 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1673 goto not_linked_together;
1675 if (GST_PAD_UNLINKFUNC (srcpad)) {
1676 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1678 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1679 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1682 _priv_gst_pad_invalidate_cache (srcpad);
1684 /* first clear peers */
1685 GST_PAD_PEER (srcpad) = NULL;
1686 GST_PAD_PEER (sinkpad) = NULL;
1688 GST_OBJECT_UNLOCK (sinkpad);
1689 GST_OBJECT_UNLOCK (srcpad);
1691 /* fire off a signal to each of the pads telling them
1692 * that they've been unlinked */
1693 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1694 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1696 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1697 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1702 if (parent != NULL) {
1703 gst_element_post_message (parent,
1704 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1705 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1706 gst_object_unref (parent);
1711 not_linked_together:
1713 /* we do not emit a warning in this case because unlinking cannot
1714 * be made MT safe.*/
1715 GST_OBJECT_UNLOCK (sinkpad);
1716 GST_OBJECT_UNLOCK (srcpad);
1722 * gst_pad_is_linked:
1723 * @pad: pad to check
1725 * Checks if a @pad is linked to another pad or not.
1727 * Returns: TRUE if the pad is linked, FALSE otherwise.
1732 gst_pad_is_linked (GstPad * pad)
1736 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1738 GST_OBJECT_LOCK (pad);
1739 result = (GST_PAD_PEER (pad) != NULL);
1740 GST_OBJECT_UNLOCK (pad);
1745 /* get the caps from both pads and see if the intersection
1748 * This function should be called with the pad LOCK on both
1752 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1753 GstPadLinkCheck flags)
1755 GstCaps *srccaps = NULL;
1756 GstCaps *sinkcaps = NULL;
1757 gboolean compatible = FALSE;
1759 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1762 /* Doing the expensive caps checking takes priority over only checking the template caps */
1763 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1764 srccaps = gst_pad_get_caps_unlocked (src);
1765 sinkcaps = gst_pad_get_caps_unlocked (sink);
1767 /* If one of the two pads doesn't have a template, consider the intersection
1769 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1770 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1774 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1776 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1779 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1781 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1784 /* if we have caps on both pads we can check the intersection. If one
1785 * of the caps is NULL, we return TRUE. */
1786 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1788 gst_caps_unref (srccaps);
1790 gst_caps_unref (sinkcaps);
1794 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1795 gst_caps_unref (srccaps);
1796 gst_caps_unref (sinkcaps);
1799 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1800 (compatible ? "" : "not"));
1805 /* check if the grandparents of both pads are the same.
1806 * This check is required so that we don't try to link
1807 * pads from elements in different bins without ghostpads.
1809 * The LOCK should be held on both pads
1812 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1814 GstObject *psrc, *psink;
1816 psrc = GST_OBJECT_PARENT (src);
1817 psink = GST_OBJECT_PARENT (sink);
1819 /* if one of the pads has no parent, we allow the link */
1820 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1823 /* only care about parents that are elements */
1824 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1825 goto no_element_parent;
1827 /* if the parents are the same, we have a loop */
1828 if (G_UNLIKELY (psrc == psink))
1831 /* if they both have a parent, we check the grandparents. We can not lock
1832 * the parent because we hold on the child (pad) and the locking order is
1833 * parent >> child. */
1834 psrc = GST_OBJECT_PARENT (psrc);
1835 psink = GST_OBJECT_PARENT (psink);
1837 /* if they have grandparents but they are not the same */
1838 if (G_UNLIKELY (psrc != psink))
1839 goto wrong_grandparents;
1846 GST_CAT_DEBUG (GST_CAT_CAPS,
1847 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1848 GST_PTR_FORMAT, psrc, psink);
1853 GST_CAT_DEBUG (GST_CAT_CAPS,
1854 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1855 GST_PTR_FORMAT, psrc, psink);
1860 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1866 GST_CAT_DEBUG (GST_CAT_CAPS,
1867 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1868 GST_PTR_FORMAT, psrc, psink);
1873 /* FIXME leftover from an attempt at refactoring... */
1874 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1875 * the two pads will be locked in the srcpad, sinkpad order. */
1876 static GstPadLinkReturn
1877 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1879 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1880 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1882 GST_OBJECT_LOCK (srcpad);
1884 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1885 goto src_was_linked;
1887 GST_OBJECT_LOCK (sinkpad);
1889 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1890 goto sink_was_linked;
1892 /* check hierarchy, pads can only be linked if the grandparents
1894 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1895 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1896 goto wrong_hierarchy;
1898 /* check pad caps for non-empty intersection */
1899 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1902 /* FIXME check pad scheduling for non-empty intersection */
1904 return GST_PAD_LINK_OK;
1908 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1909 GST_DEBUG_PAD_NAME (srcpad),
1910 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1911 /* we do not emit a warning in this case because unlinking cannot
1912 * be made MT safe.*/
1913 GST_OBJECT_UNLOCK (srcpad);
1914 return GST_PAD_LINK_WAS_LINKED;
1918 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1919 GST_DEBUG_PAD_NAME (sinkpad),
1920 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1921 /* we do not emit a warning in this case because unlinking cannot
1922 * be made MT safe.*/
1923 GST_OBJECT_UNLOCK (sinkpad);
1924 GST_OBJECT_UNLOCK (srcpad);
1925 return GST_PAD_LINK_WAS_LINKED;
1929 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1930 GST_OBJECT_UNLOCK (sinkpad);
1931 GST_OBJECT_UNLOCK (srcpad);
1932 return GST_PAD_LINK_WRONG_HIERARCHY;
1936 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1937 GST_OBJECT_UNLOCK (sinkpad);
1938 GST_OBJECT_UNLOCK (srcpad);
1939 return GST_PAD_LINK_NOFORMAT;
1945 * @srcpad: the source #GstPad.
1946 * @sinkpad: the sink #GstPad.
1948 * Checks if the source pad and the sink pad are compatible so they can be
1951 * Returns: TRUE if the pads can be linked.
1954 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1956 GstPadLinkReturn result;
1958 /* generic checks */
1959 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1960 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1962 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1963 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1965 /* gst_pad_link_prepare does everything for us, we only release the locks
1966 * on the pads that it gets us. If this function returns !OK the locks are not
1968 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1969 if (result != GST_PAD_LINK_OK)
1972 GST_OBJECT_UNLOCK (srcpad);
1973 GST_OBJECT_UNLOCK (sinkpad);
1976 return result == GST_PAD_LINK_OK;
1980 * gst_pad_link_full:
1981 * @srcpad: the source #GstPad to link.
1982 * @sinkpad: the sink #GstPad to link.
1983 * @flags: the checks to validate when linking
1985 * Links the source pad and the sink pad.
1987 * This variant of #gst_pad_link provides a more granular control on the
1988 * checks being done when linking. While providing some considerable speedups
1989 * the caller of this method must be aware that wrong usage of those flags
1990 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1991 * for more information.
1995 * Returns: A result code indicating if the connection worked or
2001 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2003 GstPadLinkReturn result;
2006 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2007 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2008 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2009 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2010 GST_PAD_LINK_WRONG_DIRECTION);
2012 /* Notify the parent early. See gst_pad_unlink for details. */
2013 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2014 if (GST_IS_ELEMENT (parent)) {
2015 gst_element_post_message (parent,
2016 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2017 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2019 gst_object_unref (parent);
2024 /* prepare will also lock the two pads */
2025 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2027 if (result != GST_PAD_LINK_OK)
2030 /* must set peers before calling the link function */
2031 GST_PAD_PEER (srcpad) = sinkpad;
2032 GST_PAD_PEER (sinkpad) = srcpad;
2034 GST_OBJECT_UNLOCK (sinkpad);
2035 GST_OBJECT_UNLOCK (srcpad);
2037 /* FIXME released the locks here, concurrent thread might link
2038 * something else. */
2039 if (GST_PAD_LINKFUNC (srcpad)) {
2040 /* this one will call the peer link function */
2041 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2042 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2043 /* if no source link function, we need to call the sink link
2044 * function ourselves. */
2045 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2047 result = GST_PAD_LINK_OK;
2050 GST_OBJECT_LOCK (srcpad);
2051 GST_OBJECT_LOCK (sinkpad);
2053 if (result == GST_PAD_LINK_OK) {
2054 GST_OBJECT_UNLOCK (sinkpad);
2055 GST_OBJECT_UNLOCK (srcpad);
2057 /* fire off a signal to each of the pads telling them
2058 * that they've been linked */
2059 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2060 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2062 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2063 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2065 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2066 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2068 GST_PAD_PEER (srcpad) = NULL;
2069 GST_PAD_PEER (sinkpad) = NULL;
2071 GST_OBJECT_UNLOCK (sinkpad);
2072 GST_OBJECT_UNLOCK (srcpad);
2077 gst_element_post_message (parent,
2078 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2079 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2080 gst_object_unref (parent);
2088 * @srcpad: the source #GstPad to link.
2089 * @sinkpad: the sink #GstPad to link.
2091 * Links the source pad and the sink pad.
2093 * Returns: A result code indicating if the connection worked or
2099 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2101 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2105 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2107 GstPadTemplate **template_p;
2109 /* this function would need checks if it weren't static */
2111 GST_OBJECT_LOCK (pad);
2112 template_p = &pad->padtemplate;
2113 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2114 GST_OBJECT_UNLOCK (pad);
2117 gst_pad_template_pad_created (templ, pad);
2121 * gst_pad_get_pad_template:
2124 * Gets the template for @pad.
2126 * Returns: (transfer none): the #GstPadTemplate from which this pad was
2127 * instantiated, or %NULL if this pad has no template.
2129 * FIXME: currently returns an unrefcounted padtemplate.
2132 gst_pad_get_pad_template (GstPad * pad)
2134 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2136 return GST_PAD_PAD_TEMPLATE (pad);
2140 /* should be called with the pad LOCK held */
2141 /* refs the caps, so caller is responsible for getting it unreffed */
2143 gst_pad_get_caps_unlocked (GstPad * pad)
2145 GstCaps *result = NULL;
2146 GstPadTemplate *templ;
2148 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2150 if (GST_PAD_GETCAPSFUNC (pad)) {
2151 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2152 "dispatching to pad getcaps function");
2154 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2155 GST_OBJECT_UNLOCK (pad);
2156 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2157 GST_OBJECT_LOCK (pad);
2158 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2160 if (result == NULL) {
2161 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2162 GST_DEBUG_PAD_NAME (pad));
2164 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2165 "pad getcaps returned %" GST_PTR_FORMAT, result);
2166 #ifndef G_DISABLE_ASSERT
2167 /* check that the returned caps are a real subset of the template caps */
2168 if (GST_PAD_PAD_TEMPLATE (pad)) {
2169 const GstCaps *templ_caps =
2170 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2171 if (!gst_caps_is_subset (result, templ_caps)) {
2174 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2175 "pad returned caps %" GST_PTR_FORMAT
2176 " which are not a real subset of its template caps %"
2177 GST_PTR_FORMAT, result, templ_caps);
2179 ("pad %s:%s returned caps which are not a real "
2180 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2181 temp = gst_caps_intersect (templ_caps, result);
2182 gst_caps_unref (result);
2190 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2191 result = GST_PAD_TEMPLATE_CAPS (templ);
2192 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2193 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2196 result = gst_caps_ref (result);
2199 if ((result = GST_PAD_CAPS (pad))) {
2200 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2201 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2203 result = gst_caps_ref (result);
2207 /* this almost never happens */
2208 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2209 result = gst_caps_new_empty ();
2217 * @pad: a #GstPad to get the capabilities of.
2219 * Gets the capabilities this pad can produce or consume.
2220 * Note that this method doesn't necessarily return the caps set by
2221 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2222 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2223 * the pad's get_caps function;
2224 * this returns the pad template caps if not explicitly set.
2226 * Note that this function does not return writable #GstCaps, use
2227 * gst_caps_make_writable() before modifying the caps.
2229 * Returns: the caps of the pad with incremented ref-count.
2232 gst_pad_get_caps (GstPad * pad)
2234 GstCaps *result = NULL;
2236 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2238 GST_OBJECT_LOCK (pad);
2240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2242 result = gst_pad_get_caps_unlocked (pad);
2244 GST_OBJECT_UNLOCK (pad);
2250 * gst_pad_peer_get_caps:
2251 * @pad: a #GstPad to get the capabilities of.
2253 * Gets the capabilities of the peer connected to this pad. Similar to
2254 * gst_pad_get_caps().
2256 * Returns: the caps of the peer pad with incremented ref-count. This function
2257 * returns %NULL when there is no peer pad.
2260 gst_pad_peer_get_caps (GstPad * pad)
2263 GstCaps *result = NULL;
2265 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2267 GST_OBJECT_LOCK (pad);
2269 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2271 peerpad = GST_PAD_PEER (pad);
2272 if (G_UNLIKELY (peerpad == NULL))
2275 gst_object_ref (peerpad);
2276 GST_OBJECT_UNLOCK (pad);
2278 result = gst_pad_get_caps (peerpad);
2280 gst_object_unref (peerpad);
2286 GST_OBJECT_UNLOCK (pad);
2292 fixate_value (GValue * dest, const GValue * src)
2294 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2295 g_value_init (dest, G_TYPE_INT);
2296 g_value_set_int (dest, gst_value_get_int_range_min (src));
2297 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2298 g_value_init (dest, G_TYPE_DOUBLE);
2299 g_value_set_double (dest, gst_value_get_double_range_min (src));
2300 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2301 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2302 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2303 GValue temp = { 0 };
2305 /* list could be empty */
2306 if (gst_value_list_get_size (src) <= 0)
2309 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2311 if (!fixate_value (dest, &temp))
2312 gst_value_init_and_copy (dest, &temp);
2313 g_value_unset (&temp);
2314 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2315 gboolean res = FALSE;
2318 len = gst_value_array_get_size (src);
2319 g_value_init (dest, GST_TYPE_ARRAY);
2320 for (n = 0; n < len; n++) {
2322 const GValue *orig_kid = gst_value_array_get_value (src, n);
2324 if (!fixate_value (&kid, orig_kid))
2325 gst_value_init_and_copy (&kid, orig_kid);
2328 gst_value_array_append_value (dest, &kid);
2329 g_value_unset (&kid);
2333 g_value_unset (dest);
2344 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2346 GstStructure *s = data;
2349 if (fixate_value (&v, value)) {
2350 gst_structure_id_set_value (s, field_id, &v);
2358 * gst_pad_fixate_caps:
2359 * @pad: a #GstPad to fixate
2360 * @caps: the #GstCaps to fixate
2362 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2363 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2366 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2368 GstPadFixateCapsFunction fixatefunc;
2371 g_return_if_fail (GST_IS_PAD (pad));
2372 g_return_if_fail (caps != NULL);
2373 g_return_if_fail (!gst_caps_is_empty (caps));
2374 g_return_if_fail (!gst_caps_is_any (caps));
2376 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2379 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2381 fixatefunc (pad, caps);
2384 /* default fixation */
2385 gst_caps_truncate (caps);
2386 s = gst_caps_get_structure (caps, 0);
2387 gst_structure_foreach (s, gst_pad_default_fixate, s);
2390 /* Default accept caps implementation just checks against
2391 * against the allowed caps for the pad */
2393 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2395 /* get the caps and see if it intersects to something not empty */
2397 gboolean result = FALSE;
2399 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2401 allowed = gst_pad_get_caps (pad);
2403 goto nothing_allowed;
2405 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2407 result = gst_caps_can_intersect (allowed, caps);
2409 gst_caps_unref (allowed);
2416 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2422 * gst_pad_accept_caps:
2423 * @pad: a #GstPad to check
2424 * @caps: a #GstCaps to check on the pad
2426 * Check if the given pad accepts the caps.
2428 * Returns: TRUE if the pad can accept the caps.
2431 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2434 GstPadAcceptCapsFunction acceptfunc;
2435 GstCaps *existing = NULL;
2437 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2439 /* any pad can be unnegotiated */
2443 /* lock for checking the existing caps */
2444 GST_OBJECT_LOCK (pad);
2445 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2446 /* The current caps on a pad are trivially acceptable */
2447 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2448 if (caps == existing || gst_caps_is_equal (caps, existing))
2451 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2452 GST_OBJECT_UNLOCK (pad);
2454 if (G_LIKELY (acceptfunc)) {
2455 /* we can call the function */
2456 result = acceptfunc (pad, caps);
2457 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2459 /* Only null if the element explicitly unset it */
2460 result = gst_pad_acceptcaps_default (pad, caps);
2461 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2467 GST_DEBUG_OBJECT (pad, "pad had same caps");
2468 GST_OBJECT_UNLOCK (pad);
2474 * gst_pad_peer_accept_caps:
2475 * @pad: a #GstPad to check the peer of
2476 * @caps: a #GstCaps to check on the pad
2478 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2481 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2484 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2489 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2491 GST_OBJECT_LOCK (pad);
2493 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2495 peerpad = GST_PAD_PEER (pad);
2496 if (G_UNLIKELY (peerpad == NULL))
2499 gst_object_ref (peerpad);
2500 /* release lock before calling external methods but keep ref to pad */
2501 GST_OBJECT_UNLOCK (pad);
2503 result = gst_pad_accept_caps (peerpad, caps);
2505 gst_object_unref (peerpad);
2511 GST_OBJECT_UNLOCK (pad);
2518 * @pad: a #GstPad to set the capabilities of.
2519 * @caps: (transfer none): a #GstCaps to set.
2521 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2522 * caps on the pad will be unreffed. This function refs the caps so you should
2523 * unref if as soon as you don't need it anymore.
2524 * It is possible to set NULL caps, which will make the pad unnegotiated
2527 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2528 * or bad parameters were provided to this function.
2533 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2535 GstPadSetCapsFunction setcaps;
2538 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2539 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2541 GST_OBJECT_LOCK (pad);
2542 existing = GST_PAD_CAPS (pad);
2543 if (existing == caps)
2546 if (gst_caps_is_equal (caps, existing))
2547 goto setting_same_caps;
2549 setcaps = GST_PAD_SETCAPSFUNC (pad);
2551 /* call setcaps function to configure the pad only if the
2552 * caps is not NULL */
2553 if (setcaps != NULL && caps) {
2554 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2555 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2556 GST_OBJECT_UNLOCK (pad);
2557 if (!setcaps (pad, caps))
2559 GST_OBJECT_LOCK (pad);
2560 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2562 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2566 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2567 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2569 GST_OBJECT_UNLOCK (pad);
2571 #if GLIB_CHECK_VERSION(2,26,0)
2572 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2574 g_object_notify ((GObject *) pad, "caps");
2581 GST_OBJECT_UNLOCK (pad);
2586 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2587 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2588 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2590 GST_OBJECT_UNLOCK (pad);
2597 GST_OBJECT_LOCK (pad);
2598 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2599 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2600 "caps %" GST_PTR_FORMAT " could not be set", caps);
2601 GST_OBJECT_UNLOCK (pad);
2609 GstPadEventFunction eventfunc;
2615 context_func (GstEvent * event, ContextData * data)
2617 data->eventfunc (data->pad, gst_event_ref (event));
2618 /* FIXME, update return value when we can */
2621 static GstFlowReturn
2622 gst_pad_configure_sink (GstPad * pad, GstContext * context)
2626 if (G_UNLIKELY ((data.eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2629 data.ret = GST_FLOW_OK;
2631 gst_context_foreach (context, (GFunc) context_func, &data);
2633 /* set context on pad if all succeeds */
2634 gst_context_replace (&GST_PAD_CONTEXT (pad), context);
2640 g_warning ("pad %s:%s has no event handler, file a bug.",
2641 GST_DEBUG_PAD_NAME (pad));
2642 return GST_FLOW_ERROR;
2647 * gst_pad_get_pad_template_caps:
2648 * @pad: a #GstPad to get the template capabilities from.
2650 * Gets the capabilities for @pad's template.
2652 * Returns: (transfer none): the #GstCaps of this pad template. If you intend
2653 * to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2656 gst_pad_get_pad_template_caps (GstPad * pad)
2658 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2660 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2662 if (GST_PAD_PAD_TEMPLATE (pad))
2663 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2665 return gst_static_caps_get (&anycaps);
2670 * @pad: a #GstPad to get the peer of.
2672 * Gets the peer of @pad. This function refs the peer pad so
2673 * you need to unref it after use.
2675 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2680 gst_pad_get_peer (GstPad * pad)
2684 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2686 GST_OBJECT_LOCK (pad);
2687 result = GST_PAD_PEER (pad);
2689 gst_object_ref (result);
2690 GST_OBJECT_UNLOCK (pad);
2696 * gst_pad_get_allowed_caps:
2699 * Gets the capabilities of the allowed media types that can flow through
2700 * @pad and its peer.
2702 * The allowed capabilities is calculated as the intersection of the results of
2703 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2704 * on the resulting caps.
2706 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2707 * caps when you no longer need it. This function returns NULL when @pad
2713 gst_pad_get_allowed_caps (GstPad * pad)
2720 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2722 GST_OBJECT_LOCK (pad);
2724 peer = GST_PAD_PEER (pad);
2725 if (G_UNLIKELY (peer == NULL))
2728 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2730 gst_object_ref (peer);
2731 GST_OBJECT_UNLOCK (pad);
2732 mycaps = gst_pad_get_caps (pad);
2734 peercaps = gst_pad_get_caps (peer);
2735 gst_object_unref (peer);
2737 caps = gst_caps_intersect (mycaps, peercaps);
2738 gst_caps_unref (peercaps);
2739 gst_caps_unref (mycaps);
2741 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2748 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2749 GST_OBJECT_UNLOCK (pad);
2756 * gst_pad_get_negotiated_caps:
2759 * Gets the capabilities of the media type that currently flows through @pad
2762 * This function can be used on both src and sinkpads. Note that srcpads are
2763 * always negotiated before sinkpads so it is possible that the negotiated caps
2764 * on the srcpad do not match the negotiated caps of the peer.
2766 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2767 * the caps when you no longer need it. This function returns NULL when
2768 * the @pad has no peer or is not negotiated yet.
2773 gst_pad_get_negotiated_caps (GstPad * pad)
2778 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2780 GST_OBJECT_LOCK (pad);
2782 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2785 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2787 caps = GST_PAD_CAPS (pad);
2789 gst_caps_ref (caps);
2790 GST_OBJECT_UNLOCK (pad);
2792 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2799 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2800 GST_OBJECT_UNLOCK (pad);
2806 static GstIteratorItem
2807 iterate_pad (GstIterator * it, GstPad * pad)
2809 gst_object_ref (pad);
2810 return GST_ITERATOR_ITEM_PASS;
2814 * gst_pad_iterate_internal_links_default:
2815 * @pad: the #GstPad to get the internal links of.
2817 * Iterate the list of pads to which the given pad is linked to inside of
2818 * the parent element.
2819 * This is the default handler, and thus returns an iterator of all of the
2820 * pads inside the parent element with opposite direction.
2822 * The caller must free this iterator after use with gst_iterator_free().
2824 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2825 * returned pad with gst_object_unref().
2830 gst_pad_iterate_internal_links_default (GstPad * pad)
2837 GstIteratorDisposeFunction dispose;
2839 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2844 GST_OBJECT_LOCK (pad);
2845 parent = GST_PAD_PARENT (pad);
2846 if (!parent || !GST_IS_ELEMENT (parent))
2849 gst_object_ref (parent);
2850 GST_OBJECT_UNLOCK (pad);
2852 if (pad->direction == GST_PAD_SRC)
2853 padlist = &parent->sinkpads;
2855 padlist = &parent->srcpads;
2857 GST_DEBUG_OBJECT (pad, "Making iterator");
2859 cookie = &parent->pads_cookie;
2861 dispose = (GstIteratorDisposeFunction) gst_object_unref;
2862 lock = GST_OBJECT_GET_LOCK (parent);
2865 res = gst_iterator_new_list (GST_TYPE_PAD,
2866 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
2874 GST_OBJECT_UNLOCK (pad);
2875 GST_DEBUG_OBJECT (pad, "no parent element");
2881 * gst_pad_iterate_internal_links:
2882 * @pad: the GstPad to get the internal links of.
2884 * Gets an iterator for the pads to which the given pad is linked to inside
2885 * of the parent element.
2887 * Each #GstPad element yielded by the iterator will have its refcount increased,
2888 * so unref after use.
2890 * Free-function: gst_iterator_free
2892 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2893 * pad does not have an iterator function configured. Use
2894 * gst_iterator_free() after usage.
2899 gst_pad_iterate_internal_links (GstPad * pad)
2901 GstIterator *res = NULL;
2903 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2905 if (GST_PAD_ITERINTLINKFUNC (pad))
2906 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
2912 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2914 gboolean result = FALSE;
2916 gboolean done = FALSE;
2919 GList *pushed_pads = NULL;
2921 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
2922 event, GST_EVENT_TYPE_NAME (event));
2924 iter = gst_pad_iterate_internal_links (pad);
2930 switch (gst_iterator_next (iter, &item)) {
2931 case GST_ITERATOR_OK:
2932 eventpad = GST_PAD_CAST (item);
2934 /* if already pushed, skip */
2935 if (g_list_find (pushed_pads, eventpad)) {
2936 gst_object_unref (item);
2940 if (GST_PAD_IS_SRC (eventpad)) {
2941 /* for each pad we send to, we should ref the event; it's up
2942 * to downstream to unref again when handled. */
2943 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
2944 event, GST_EVENT_TYPE_NAME (event),
2945 GST_DEBUG_PAD_NAME (eventpad));
2946 gst_event_ref (event);
2947 result |= gst_pad_push_event (eventpad, event);
2949 /* we only send the event on one pad, multi-sinkpad elements
2950 * should implement a handler */
2951 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
2952 event, GST_EVENT_TYPE_NAME (event),
2953 GST_DEBUG_PAD_NAME (eventpad));
2954 result = gst_pad_push_event (eventpad, event);
2959 pushed_pads = g_list_prepend (pushed_pads, eventpad);
2961 gst_object_unref (item);
2963 case GST_ITERATOR_RESYNC:
2964 /* We don't reset the result here because we don't push the event
2965 * again on pads that got the event already and because we need
2966 * to consider the result of the previous pushes */
2967 gst_iterator_resync (iter);
2969 case GST_ITERATOR_ERROR:
2970 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2973 case GST_ITERATOR_DONE:
2978 gst_iterator_free (iter);
2982 /* If this is a sinkpad and we don't have pads to send the event to, we
2983 * return TRUE. This is so that when using the default handler on a sink
2984 * element, we don't fail to push it. */
2986 result = GST_PAD_IS_SINK (pad);
2988 g_list_free (pushed_pads);
2990 /* we handled the incoming event so we unref once */
2992 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
2993 gst_event_unref (event);
3000 * gst_pad_event_default:
3001 * @pad: a #GstPad to call the default event handler on.
3002 * @event: (transfer full): the #GstEvent to handle.
3004 * Invokes the default event handler for the given pad. End-of-stream and
3005 * discontinuity events are handled specially, and then the event is sent to all
3006 * pads internally linked to @pad. Note that if there are many possible sink
3007 * pads that are internally linked to @pad, only one will be sent an event.
3008 * Multi-sinkpad elements should implement custom event handlers.
3010 * Returns: TRUE if the event was sent succesfully.
3013 gst_pad_event_default (GstPad * pad, GstEvent * event)
3015 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3016 g_return_val_if_fail (event != NULL, FALSE);
3018 GST_LOG_OBJECT (pad, "default event handler");
3020 switch (GST_EVENT_TYPE (event)) {
3023 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3024 gst_pad_pause_task (pad);
3031 return gst_pad_event_default_dispatch (pad, event);
3035 * gst_pad_dispatcher:
3036 * @pad: a #GstPad to dispatch.
3037 * @dispatch: the #GstPadDispatcherFunction to call.
3038 * @data: (closure): gpointer user data passed to the dispatcher function.
3040 * Invokes the given dispatcher function on each respective peer of
3041 * all pads that are internally linked to the given pad.
3042 * The GstPadDispatcherFunction should return TRUE when no further pads
3043 * need to be processed.
3045 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3048 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3051 gboolean res = FALSE;
3052 GstIterator *iter = NULL;
3053 gboolean done = FALSE;
3056 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3057 g_return_val_if_fail (dispatch != NULL, FALSE);
3059 iter = gst_pad_iterate_internal_links (pad);
3065 switch (gst_iterator_next (iter, &item)) {
3066 case GST_ITERATOR_OK:
3068 GstPad *int_pad = GST_PAD_CAST (item);
3069 GstPad *int_peer = gst_pad_get_peer (int_pad);
3072 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3073 GST_DEBUG_PAD_NAME (int_peer));
3074 done = res = dispatch (int_peer, data);
3075 gst_object_unref (int_peer);
3077 GST_DEBUG_OBJECT (int_pad, "no peer");
3080 gst_object_unref (item);
3082 case GST_ITERATOR_RESYNC:
3083 gst_iterator_resync (iter);
3085 case GST_ITERATOR_ERROR:
3087 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3089 case GST_ITERATOR_DONE:
3094 gst_iterator_free (iter);
3096 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3105 * @pad: a #GstPad to invoke the default query on.
3106 * @query: (transfer none): the #GstQuery to perform.
3108 * Dispatches a query to a pad. The query should have been allocated by the
3109 * caller via one of the type-specific allocation functions. The element that
3110 * the pad belongs to is responsible for filling the query with an appropriate
3111 * response, which should then be parsed with a type-specific query parsing
3114 * Again, the caller is responsible for both the allocation and deallocation of
3115 * the query structure.
3117 * Please also note that some queries might need a running pipeline to work.
3119 * Returns: TRUE if the query could be performed.
3122 gst_pad_query (GstPad * pad, GstQuery * query)
3124 GstPadQueryFunction func;
3126 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3127 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3129 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3131 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3134 return func (pad, query);
3138 GST_DEBUG_OBJECT (pad, "had no query function");
3144 * gst_pad_peer_query:
3145 * @pad: a #GstPad to invoke the peer query on.
3146 * @query: (transfer none): the #GstQuery to perform.
3148 * Performs gst_pad_query() on the peer of @pad.
3150 * The caller is responsible for both the allocation and deallocation of
3151 * the query structure.
3153 * Returns: TRUE if the query could be performed. This function returns %FALSE
3154 * if @pad has no peer.
3159 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3164 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3165 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3167 GST_OBJECT_LOCK (pad);
3169 GST_DEBUG_OBJECT (pad, "peer query");
3171 peerpad = GST_PAD_PEER (pad);
3172 if (G_UNLIKELY (peerpad == NULL))
3175 gst_object_ref (peerpad);
3176 GST_OBJECT_UNLOCK (pad);
3178 result = gst_pad_query (peerpad, query);
3180 gst_object_unref (peerpad);
3187 GST_WARNING_OBJECT (pad, "pad has no peer");
3188 GST_OBJECT_UNLOCK (pad);
3194 * gst_pad_query_default:
3195 * @pad: a #GstPad to call the default query handler on.
3196 * @query: (transfer none): the #GstQuery to handle.
3198 * Invokes the default query handler for the given pad.
3199 * The query is sent to all pads internally linked to @pad. Note that
3200 * if there are many possible sink pads that are internally linked to
3201 * @pad, only one will be sent the query.
3202 * Multi-sinkpad elements should implement custom query handlers.
3204 * Returns: TRUE if the query was performed succesfully.
3207 gst_pad_query_default (GstPad * pad, GstQuery * query)
3209 switch (GST_QUERY_TYPE (query)) {
3210 case GST_QUERY_POSITION:
3211 case GST_QUERY_SEEKING:
3212 case GST_QUERY_FORMATS:
3213 case GST_QUERY_LATENCY:
3214 case GST_QUERY_JITTER:
3215 case GST_QUERY_RATE:
3216 case GST_QUERY_CONVERT:
3218 return gst_pad_dispatcher
3219 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3224 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3225 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3228 * This function performs the pad blocking when an event, buffer push
3229 * is performed on a _SRC_ pad. It blocks the streaming thread after
3230 * informing the pad has been blocked.
3232 * An application can with this method wait and block any streaming
3233 * thread and perform operations such as seeking or linking.
3235 * Two methods are available for notifying the application of the
3237 * - the callback method, which happens in the STREAMING thread with
3238 * the STREAM_LOCK held. With this method, the most useful way of
3239 * dealing with the callback is to post a message to the main thread
3240 * where the pad block can then be handled outside of the streaming
3241 * thread. With the last method one can perform all operations such
3242 * as doing a state change, linking, unblocking, seeking etc on the
3244 * - the GCond signal method, which makes any thread unblock when
3245 * the pad block happens.
3247 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3248 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3252 static GstFlowReturn
3253 handle_pad_block (GstPad * pad)
3255 GstPadBlockCallback callback;
3257 GstFlowReturn ret = GST_FLOW_OK;
3259 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3261 /* flushing, don't bother trying to block and return WRONG_STATE
3263 if (GST_PAD_IS_FLUSHING (pad))
3264 goto flushingnonref;
3266 /* we grab an extra ref for the callbacks, this is probably not
3267 * needed (callback code does not have a ref and cannot unref). I
3268 * think this was done to make it possible to unref the element in
3269 * the callback, which is in the end totally impossible as it
3270 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3271 * all taken when calling this function. */
3272 gst_object_ref (pad);
3274 while (GST_PAD_IS_BLOCKED (pad)) {
3276 /* we either have a callback installed to notify the block or
3277 * some other thread is doing a GCond wait. */
3278 callback = pad->block_callback;
3279 pad->block_callback_called = TRUE;
3281 /* there is a callback installed, call it. We release the
3282 * lock so that the callback can do something usefull with the
3284 user_data = pad->block_data;
3285 GST_OBJECT_UNLOCK (pad);
3286 callback (pad, TRUE, user_data);
3287 GST_OBJECT_LOCK (pad);
3289 /* we released the lock, recheck flushing */
3290 if (GST_PAD_IS_FLUSHING (pad))
3293 /* no callback, signal the thread that is doing a GCond wait
3295 GST_PAD_BLOCK_BROADCAST (pad);
3297 } while (pad->block_callback_called == FALSE && GST_PAD_IS_BLOCKED (pad));
3299 /* OBJECT_LOCK could have been released when we did the callback, which
3300 * then could have made the pad unblock so we need to check the blocking
3301 * condition again. */
3302 if (!GST_PAD_IS_BLOCKED (pad))
3305 /* now we block the streaming thread. It can be unlocked when we
3306 * deactivate the pad (which will also set the FLUSHING flag) or
3307 * when the pad is unblocked. A flushing event will also unblock
3308 * the pad after setting the FLUSHING flag. */
3309 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3310 "Waiting to be unblocked or set flushing");
3311 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3312 GST_PAD_BLOCK_WAIT (pad);
3313 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3315 /* see if we got unblocked by a flush or not */
3316 if (GST_PAD_IS_FLUSHING (pad))
3320 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3322 /* when we get here, the pad is unblocked again and we perform
3323 * the needed unblock code. */
3324 callback = pad->block_callback;
3326 /* we need to call the callback */
3327 user_data = pad->block_data;
3328 GST_OBJECT_UNLOCK (pad);
3329 callback (pad, FALSE, user_data);
3330 GST_OBJECT_LOCK (pad);
3332 /* we need to signal the thread waiting on the GCond */
3333 GST_PAD_BLOCK_BROADCAST (pad);
3336 gst_object_unref (pad);
3342 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3343 return GST_FLOW_WRONG_STATE;
3347 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3348 gst_object_unref (pad);
3349 return GST_FLOW_WRONG_STATE;
3353 /**********************************************************************
3354 * Data passing functions
3358 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3361 GValue args[2] = { {0}, {0} };
3366 g_value_init (&ret, G_TYPE_BOOLEAN);
3367 g_value_set_boolean (&ret, TRUE);
3368 g_value_init (&args[0], GST_TYPE_PAD);
3369 g_value_set_object (&args[0], pad);
3370 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3371 g_value_set_boxed (&args[1], obj);
3373 if (GST_IS_EVENT (obj))
3374 detail = event_quark;
3376 detail = buffer_quark;
3379 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3380 res = g_value_get_boolean (&ret);
3383 g_value_unset (&ret);
3384 g_value_unset (&args[0]);
3385 g_value_unset (&args[1]);
3391 gst_pad_data_unref (gboolean is_buffer, void *data)
3393 if (G_LIKELY (is_buffer)) {
3394 gst_buffer_unref (data);
3396 gst_buffer_list_unref (data);
3401 gst_pad_data_get_context (gboolean is_buffer, void *data)
3403 GstContext *context;
3405 if (G_LIKELY (is_buffer)) {
3406 context = GST_BUFFER_CONTEXT (data);
3410 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0)))
3411 context = GST_BUFFER_CONTEXT (buf);
3418 /* this is the chain function that does not perform the additional argument
3419 * checking for that little extra speed.
3421 static inline GstFlowReturn
3422 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
3423 GstContext * context, GstPadPushCache * cache)
3425 gboolean context_changed;
3427 gboolean emit_signal;
3429 GST_PAD_STREAM_LOCK (pad);
3431 GST_OBJECT_LOCK (pad);
3432 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3435 context_changed = context && context != GST_PAD_CONTEXT (pad);
3436 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3437 GST_OBJECT_UNLOCK (pad);
3439 /* see if the signal should be emited, we emit before caps nego as
3440 * we might drop the buffer and do capsnego for nothing. */
3441 if (G_UNLIKELY (emit_signal)) {
3443 if (G_LIKELY (is_buffer)) {
3444 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3447 /* chain all groups in the buffer list one by one to avoid problems with
3448 * buffer probes that push buffers or events */
3453 /* we got a new datatype on the pad, see if it can handle it */
3454 if (G_UNLIKELY (context_changed)) {
3455 GST_DEBUG_OBJECT (pad, "context changed to %p", context);
3456 if (G_UNLIKELY (gst_pad_configure_sink (pad, context) != GST_FLOW_OK))
3457 goto not_negotiated;
3460 /* NOTE: we read the chainfunc unlocked.
3461 * we cannot hold the lock for the pad so we might send
3462 * the data to the wrong function. This is not really a
3463 * problem since functions are assigned at creation time
3464 * and don't change that often... */
3465 if (G_LIKELY (is_buffer)) {
3466 GstPadChainFunction chainfunc;
3468 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3471 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3472 "calling chainfunction &%s with buffer %p",
3473 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3476 cache->peer = gst_object_ref (pad);
3479 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3481 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3482 "called chainfunction &%s with buffer %p, returned %s",
3483 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3485 GstPadChainListFunction chainlistfunc;
3487 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3490 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3491 "calling chainlistfunction &%s",
3492 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3494 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3496 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3497 "called chainlistfunction &%s, returned %s",
3498 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3501 GST_PAD_STREAM_UNLOCK (pad);
3507 GstBufferList *list;
3511 GST_PAD_STREAM_UNLOCK (pad);
3513 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3515 list = GST_BUFFER_LIST_CAST (data);
3516 len = gst_buffer_list_len (list);
3518 for (i = 0; i < len; i++) {
3519 buffer = gst_buffer_list_get (list, i);
3520 context = GST_BUFFER_CONTEXT (buffer);
3522 gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_ref (buffer),
3524 if (ret != GST_FLOW_OK)
3527 gst_buffer_list_unref (list);
3535 gst_pad_data_unref (is_buffer, data);
3536 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3537 "pushing, but pad was flushing");
3538 GST_OBJECT_UNLOCK (pad);
3539 GST_PAD_STREAM_UNLOCK (pad);
3540 return GST_FLOW_WRONG_STATE;
3544 gst_pad_data_unref (is_buffer, data);
3545 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3546 GST_PAD_STREAM_UNLOCK (pad);
3551 gst_pad_data_unref (is_buffer, data);
3552 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3553 "pushing data but pad did not accept");
3554 GST_PAD_STREAM_UNLOCK (pad);
3555 return GST_FLOW_NOT_NEGOTIATED;
3559 gst_pad_data_unref (is_buffer, data);
3560 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3561 "pushing, but not chainhandler");
3562 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3563 ("push on pad %s:%s but it has no chainfunction",
3564 GST_DEBUG_PAD_NAME (pad)));
3565 GST_PAD_STREAM_UNLOCK (pad);
3566 return GST_FLOW_NOT_SUPPORTED;
3572 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3573 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3576 * Chain a buffer to @pad.
3578 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3580 * If the caps on @buffer are different from the current caps on @pad, this
3581 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3582 * installed on @pad. If the new caps are not acceptable for @pad, this
3583 * function returns #GST_FLOW_NOT_NEGOTIATED.
3585 * The function proceeds calling the chain function installed on @pad (see
3586 * gst_pad_set_chain_function()) and the return value of that function is
3587 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3590 * In all cases, success or failure, the caller loses its reference to @buffer
3591 * after calling this function.
3593 * Returns: a #GstFlowReturn from the pad.
3598 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3600 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3601 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3602 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3604 return gst_pad_chain_data_unchecked (pad, TRUE, buffer,
3605 GST_BUFFER_CONTEXT (buffer), NULL);
3609 * gst_pad_chain_list:
3610 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3611 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3614 * Chain a bufferlist to @pad.
3616 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3618 * If the caps on the first buffer of @list are different from the current
3619 * caps on @pad, this function will call any setcaps function
3620 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3621 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3623 * The function proceeds calling the chainlist function installed on @pad (see
3624 * gst_pad_set_chain_list_function()) and the return value of that function is
3625 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3626 * chainlist function.
3628 * In all cases, success or failure, the caller loses its reference to @list
3629 * after calling this function.
3633 * Returns: a #GstFlowReturn from the pad.
3638 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3640 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3641 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3642 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3644 return gst_pad_chain_data_unchecked (pad, FALSE, list,
3645 gst_pad_data_get_context (FALSE, list), NULL);
3648 static GstFlowReturn
3649 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
3650 GstContext * context, GstPadPushCache * cache)
3654 gboolean context_changed;
3656 GST_OBJECT_LOCK (pad);
3658 /* FIXME: this check can go away; pad_set_blocked could be implemented with
3659 * probes completely or probes with an extended pad block. */
3660 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3661 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3664 /* we emit signals on the pad arg, the peer will have a chance to
3665 * emit in the _chain() function */
3666 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
3668 /* unlock before emitting */
3669 GST_OBJECT_UNLOCK (pad);
3671 if (G_LIKELY (is_buffer)) {
3672 /* if the signal handler returned FALSE, it means we should just drop the
3674 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3677 /* push all buffers in the list */
3680 GST_OBJECT_LOCK (pad);
3683 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3686 /* Before pushing the buffer to the peer pad, ensure that context
3687 * is set on the buffer */
3688 context_changed = context != GST_PAD_CONTEXT (pad);
3690 /* take ref to peer pad before releasing the lock */
3691 gst_object_ref (peer);
3692 GST_OBJECT_UNLOCK (pad);
3694 /* we got a new datatype from the pad, it had better handle it */
3695 if (G_UNLIKELY (context_changed)) {
3696 GST_DEBUG_OBJECT (pad, "setting context %p on buffer", context);
3698 data = gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (data));
3701 gst_buffer_set_context (GST_BUFFER_CAST (data), context);
3704 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, context, cache);
3706 gst_object_unref (peer);
3712 GstBufferList *list;
3716 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
3718 list = GST_BUFFER_LIST_CAST (data);
3719 len = gst_buffer_list_len (list);
3721 for (i = 0; i < len; i++) {
3722 buffer = gst_buffer_list_get (list, i);
3723 ret = gst_pad_push_data (pad, TRUE, gst_buffer_ref (buffer),
3724 GST_BUFFER_CONTEXT (buffer), NULL);
3725 if (ret != GST_FLOW_OK)
3728 gst_buffer_list_unref (list);
3733 /* ERROR recovery here */
3736 gst_pad_data_unref (is_buffer, data);
3737 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
3738 GST_OBJECT_UNLOCK (pad);
3743 gst_pad_data_unref (is_buffer, data);
3744 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3749 gst_pad_data_unref (is_buffer, data);
3750 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3751 "pushing, but it was not linked");
3752 GST_OBJECT_UNLOCK (pad);
3753 return GST_FLOW_NOT_LINKED;
3757 static inline GstPadPushCache *
3758 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
3760 GstPadPushCache *cache;
3762 /* try to get the cached data */
3764 cache = g_atomic_pointer_get (cache_ptr);
3765 /* now try to replace the pointer with NULL to mark that we are busy
3767 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
3769 /* we could have a leftover invalid entry */
3770 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
3777 pad_free_cache (GstPadPushCache * cache)
3779 gst_object_unref (cache->peer);
3780 g_slice_free (GstPadPushCache, cache);
3784 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
3787 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
3788 /* something changed, clean up our cache */
3789 pad_free_cache (cache);
3793 /* must be called with the pad lock */
3795 _priv_gst_pad_invalidate_cache (GstPad * pad)
3797 GstPadPushCache *cache;
3798 gpointer *cache_ptr;
3800 GST_LOG_OBJECT (pad, "Invalidating pad cache");
3802 /* we hold the pad lock here so we can get the peer and it stays
3803 * alive during this call */
3804 if (GST_PAD_IS_SINK (pad)) {
3805 if (!(pad = GST_PAD_PEER (pad)))
3809 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
3811 /* try to get the cached data */
3813 cache = g_atomic_pointer_get (cache_ptr);
3814 /* now try to replace the pointer with INVALID. If nothing is busy with this
3815 * caps, we get the cache and clean it up. If something is busy, we replace
3816 * with INVALID so that when the function finishes and tries to put the
3817 * cache back, it'll fail and cleanup */
3818 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
3819 PAD_CACHE_INVALID));
3821 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
3822 pad_free_cache (cache);
3827 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3828 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3831 * Pushes a buffer to the peer of @pad.
3833 * This function will call an installed pad block before triggering any
3834 * installed pad probes.
3836 * If the caps on @buffer are different from the currently configured caps on
3837 * @pad, this function will call any installed setcaps function on @pad (see
3838 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
3839 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
3841 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3842 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3845 * In all cases, success or failure, the caller loses its reference to @buffer
3846 * after calling this function.
3848 * Returns: a #GstFlowReturn from the peer pad.
3853 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3855 GstPadPushCache *cache;
3857 gpointer *cache_ptr;
3860 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3861 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3862 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3864 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
3866 cache = pad_take_cache (pad, cache_ptr);
3868 if (G_UNLIKELY (cache == NULL))
3873 GST_PAD_STREAM_LOCK (peer);
3874 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
3877 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3878 "calling chainfunction &%s with buffer %p",
3879 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer);
3881 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
3883 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3884 "called chainfunction &%s with buffer %p, returned %s",
3885 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer,
3886 gst_flow_get_name (ret));
3888 GST_PAD_STREAM_UNLOCK (peer);
3890 pad_put_cache (pad, cache, cache_ptr);
3897 GstPadPushCache scache = { NULL, };
3899 GST_LOG_OBJECT (pad, "Taking slow path");
3902 gst_pad_push_data (pad, TRUE, buffer, GST_BUFFER_CONTEXT (buffer),
3906 GstPadPushCache *ncache;
3908 GST_LOG_OBJECT (pad, "Caching push data");
3910 /* make cache structure */
3911 ncache = g_slice_new (GstPadPushCache);
3914 pad_put_cache (pad, ncache, cache_ptr);
3920 GST_PAD_STREAM_UNLOCK (peer);
3921 pad_free_cache (cache);
3927 * gst_pad_push_list:
3928 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3929 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3932 * Pushes a buffer list to the peer of @pad.
3934 * This function will call an installed pad block before triggering any
3935 * installed pad probes.
3937 * If the caps on the first buffer in the first group of @list are different
3938 * from the currently configured caps on @pad, this function will call any
3939 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
3940 * case of failure to renegotiate the new format, this function returns
3941 * #GST_FLOW_NOT_NEGOTIATED.
3943 * If there are any probes installed on @pad every group of the buffer list
3944 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
3945 * buffer list will be unreffed.
3947 * The function proceeds calling the chain function on the peer pad and returns
3948 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3949 * be returned. If the peer pad does not have any installed chainlist function
3950 * every group buffer of the list will be merged into a normal #GstBuffer and
3951 * chained via gst_pad_chain().
3953 * In all cases, success or failure, the caller loses its reference to @list
3954 * after calling this function.
3956 * Returns: a #GstFlowReturn from the peer pad.
3963 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3965 GstPadPushCache *cache;
3967 gpointer *cache_ptr;
3970 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3971 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3972 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3974 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
3976 cache = pad_take_cache (pad, cache_ptr);
3978 if (G_UNLIKELY (cache == NULL))
3983 GST_PAD_STREAM_LOCK (peer);
3984 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
3987 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
3989 GST_PAD_STREAM_UNLOCK (peer);
3991 pad_put_cache (pad, cache, cache_ptr);
3998 GstPadPushCache scache = { NULL, };
4000 GST_LOG_OBJECT (pad, "Taking slow path");
4002 ret = gst_pad_push_data (pad, FALSE, list,
4003 gst_pad_data_get_context (FALSE, list), &scache);
4006 GstPadPushCache *ncache;
4008 GST_LOG_OBJECT (pad, "Caching push data");
4010 /* make cache structure */
4011 ncache = g_slice_new (GstPadPushCache);
4014 pad_put_cache (pad, ncache, cache_ptr);
4020 GST_PAD_STREAM_UNLOCK (peer);
4021 pad_free_cache (cache);
4027 * gst_pad_check_pull_range:
4028 * @pad: a sink #GstPad.
4030 * Checks if a gst_pad_pull_range() can be performed on the peer
4031 * source pad. This function is used by plugins that want to check
4032 * if they can use random access on the peer source pad.
4034 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4035 * if it needs to perform some logic to determine if pull_range is
4038 * Returns: a gboolean with the result.
4043 gst_pad_check_pull_range (GstPad * pad)
4047 GstPadCheckGetRangeFunction checkgetrangefunc;
4049 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4051 GST_OBJECT_LOCK (pad);
4052 if (!GST_PAD_IS_SINK (pad))
4053 goto wrong_direction;
4055 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4058 gst_object_ref (peer);
4059 GST_OBJECT_UNLOCK (pad);
4061 /* see note in above function */
4062 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4063 /* FIXME, kindoff ghetto */
4064 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4065 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4066 "no checkgetrangefunc, assuming %d", ret);
4068 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4069 "calling checkgetrangefunc %s of peer pad %s:%s",
4070 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4072 ret = checkgetrangefunc (peer);
4075 gst_object_unref (peer);
4079 /* ERROR recovery here */
4082 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4083 "checking pull range, but pad must be a sinkpad");
4084 GST_OBJECT_UNLOCK (pad);
4089 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4090 "checking pull range, but it was not linked");
4091 GST_OBJECT_UNLOCK (pad);
4096 static GstFlowReturn
4097 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4098 GstBuffer ** buffer)
4101 GstPadGetRangeFunction getrangefunc;
4102 gboolean emit_signal;
4103 GstContext *context;
4104 gboolean context_changed;
4106 GST_PAD_STREAM_LOCK (pad);
4108 GST_OBJECT_LOCK (pad);
4109 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4112 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4113 GST_OBJECT_UNLOCK (pad);
4115 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4118 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4119 "calling getrangefunc %s, offset %"
4120 G_GUINT64_FORMAT ", size %u",
4121 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4123 ret = getrangefunc (pad, offset, size, buffer);
4125 /* can only fire the signal if we have a valid buffer */
4126 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4127 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4131 GST_PAD_STREAM_UNLOCK (pad);
4133 if (G_UNLIKELY (ret != GST_FLOW_OK))
4134 goto get_range_failed;
4136 GST_OBJECT_LOCK (pad);
4137 /* Before pushing the buffer to the peer pad, ensure that context
4138 * is set on this buffer */
4139 context = GST_PAD_CONTEXT (pad);
4140 context_changed = context != GST_BUFFER_CONTEXT (*buffer);
4141 GST_OBJECT_UNLOCK (pad);
4143 if (G_UNLIKELY (context_changed)) {
4144 GST_DEBUG_OBJECT (pad, "set context on buffer %p", *buffer);
4146 *buffer = gst_buffer_make_writable (*buffer);
4147 gst_buffer_set_context (*buffer, context);
4154 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4155 "pulling range, but pad was flushing");
4156 GST_OBJECT_UNLOCK (pad);
4157 GST_PAD_STREAM_UNLOCK (pad);
4158 return GST_FLOW_WRONG_STATE;
4162 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4163 ("pullrange on pad %s:%s but it has no getrangefunction",
4164 GST_DEBUG_PAD_NAME (pad)));
4165 GST_PAD_STREAM_UNLOCK (pad);
4166 return GST_FLOW_NOT_SUPPORTED;
4170 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4171 "Dropping data after FALSE probe return");
4172 GST_PAD_STREAM_UNLOCK (pad);
4173 gst_buffer_unref (*buffer);
4175 return GST_FLOW_UNEXPECTED;
4180 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4181 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4182 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4188 * gst_pad_get_range:
4189 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4190 * @offset: The start offset of the buffer
4191 * @size: The length of the buffer
4192 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4193 * returns #GST_FLOW_ERROR if %NULL.
4195 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4196 * immediatly and @buffer is %NULL.
4198 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4199 * description of a getrange function. If @pad has no getrange function
4200 * installed (see gst_pad_set_getrange_function()) this function returns
4201 * #GST_FLOW_NOT_SUPPORTED.
4203 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4205 * Returns: a #GstFlowReturn from the pad.
4210 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4211 GstBuffer ** buffer)
4213 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4214 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4215 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4217 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4221 * gst_pad_pull_range:
4222 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4223 * @offset: The start offset of the buffer
4224 * @size: The length of the buffer
4225 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4226 * GST_FLOW_ERROR if %NULL.
4228 * Pulls a @buffer from the peer pad.
4230 * This function will first trigger the pad block signal if it was
4233 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4234 * function returns the result of gst_pad_get_range() on the peer pad.
4235 * See gst_pad_get_range() for a list of return values and for the
4236 * semantics of the arguments of this function.
4238 * @buffer's caps must either be unset or the same as what is already
4239 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4242 * Returns: a #GstFlowReturn from the peer pad.
4243 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4244 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4245 * @buffer may not be used or freed when any other return value than
4246 * #GST_FLOW_OK is returned.
4251 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4252 GstBuffer ** buffer)
4256 gboolean emit_signal;
4257 GstContext *context;
4258 gboolean context_changed;
4260 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4261 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4262 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4264 GST_OBJECT_LOCK (pad);
4266 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4267 handle_pad_block (pad);
4269 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4272 /* signal emision for the pad, peer has chance to emit when
4273 * we call _get_range() */
4274 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4276 gst_object_ref (peer);
4277 GST_OBJECT_UNLOCK (pad);
4279 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4281 gst_object_unref (peer);
4283 if (G_UNLIKELY (ret != GST_FLOW_OK))
4284 goto pull_range_failed;
4286 /* can only fire the signal if we have a valid buffer */
4287 if (G_UNLIKELY (emit_signal)) {
4288 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4292 GST_OBJECT_LOCK (pad);
4293 /* Before pushing the buffer to the peer pad, ensure that caps
4294 * are set on this pad */
4295 context = GST_BUFFER_CONTEXT (*buffer);
4296 context_changed = context && context != GST_PAD_CONTEXT (pad);
4297 GST_OBJECT_UNLOCK (pad);
4299 /* we got a new datatype on the pad, see if it can handle it */
4300 if (G_UNLIKELY (context_changed)) {
4301 GST_DEBUG_OBJECT (pad, "context changed to %p", context);
4302 if (G_UNLIKELY (gst_pad_configure_sink (pad, context) != GST_FLOW_OK))
4303 goto not_negotiated;
4307 /* ERROR recovery here */
4310 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4311 "pulling range, but it was not linked");
4312 GST_OBJECT_UNLOCK (pad);
4313 return GST_FLOW_NOT_LINKED;
4318 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4319 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4320 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4325 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4326 "Dropping data after FALSE probe return");
4327 gst_buffer_unref (*buffer);
4329 return GST_FLOW_UNEXPECTED;
4333 gst_buffer_unref (*buffer);
4335 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4336 "pullrange returned buffer of different caps");
4337 return GST_FLOW_NOT_NEGOTIATED;
4342 * gst_pad_push_event:
4343 * @pad: a #GstPad to push the event to.
4344 * @event: (transfer full): the #GstEvent to send to the pad.
4346 * Sends the event to the peer of the given pad. This function is
4347 * mainly used by elements to send events to their peer
4350 * This function takes owership of the provided event so you should
4351 * gst_event_ref() it if you want to reuse the event after this call.
4353 * Returns: TRUE if the event was handled.
4358 gst_pad_push_event (GstPad * pad, GstEvent * event)
4363 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4364 g_return_val_if_fail (event != NULL, FALSE);
4365 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4367 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4369 GST_OBJECT_LOCK (pad);
4371 /* Two checks to be made:
4372 * . (un)set the FLUSHING flag for flushing events,
4373 * . handle pad blocking */
4374 switch (GST_EVENT_TYPE (event)) {
4375 case GST_EVENT_FLUSH_START:
4376 _priv_gst_pad_invalidate_cache (pad);
4377 GST_PAD_SET_FLUSHING (pad);
4379 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4380 /* flush start will have set the FLUSHING flag and will then
4381 * unlock all threads doing a GCond wait on the blocking pad. This
4382 * will typically unblock the STREAMING thread blocked on a pad. */
4383 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4384 "doing block signal.");
4385 GST_PAD_BLOCK_BROADCAST (pad);
4389 case GST_EVENT_FLUSH_STOP:
4390 GST_PAD_UNSET_FLUSHING (pad);
4392 /* if we are blocked, flush away the FLUSH_STOP event */
4393 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4394 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4399 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4400 /* block the event as long as the pad is blocked */
4401 if (handle_pad_block (pad) != GST_FLOW_OK)
4407 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4408 GST_OBJECT_UNLOCK (pad);
4410 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4413 GST_OBJECT_LOCK (pad);
4416 /* store the event on the pad, but only on srcpads */
4417 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4419 pad->context = gst_context_make_writable (pad->context);
4421 pad->context = gst_context_new ();
4423 g_assert (GST_IS_CONTEXT (GST_PAD_CONTEXT (pad)));
4425 gst_context_update (pad->context, event);
4428 peerpad = GST_PAD_PEER (pad);
4429 if (peerpad == NULL)
4432 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4433 GST_EVENT_TYPE_NAME (event), peerpad);
4434 gst_object_ref (peerpad);
4435 GST_OBJECT_UNLOCK (pad);
4437 result = gst_pad_send_event (peerpad, event);
4439 /* Note: we gave away ownership of the event at this point */
4440 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4442 gst_object_unref (peerpad);
4446 /* ERROR handling */
4449 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4450 gst_event_unref (event);
4455 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4456 gst_event_unref (event);
4457 GST_OBJECT_UNLOCK (pad);
4462 GST_DEBUG_OBJECT (pad,
4463 "Not forwarding event since we're flushing and blocking");
4464 gst_event_unref (event);
4465 GST_OBJECT_UNLOCK (pad);
4471 * gst_pad_send_event:
4472 * @pad: a #GstPad to send the event to.
4473 * @event: (transfer full): the #GstEvent to send to the pad.
4475 * Sends the event to the pad. This function can be used
4476 * by applications to send events in the pipeline.
4478 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4479 * sink pad, @event should be a downstream event. For example, you would not
4480 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4481 * Furthermore, some downstream events have to be serialized with data flow,
4482 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4483 * the event needs to be serialized with data flow, this function will take the
4484 * pad's stream lock while calling its event function.
4486 * To find out whether an event type is upstream, downstream, or downstream and
4487 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4488 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4489 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4490 * plugin doesn't need to bother itself with this information; the core handles
4491 * all necessary locks and checks.
4493 * This function takes owership of the provided event so you should
4494 * gst_event_ref() it if you want to reuse the event after this call.
4496 * Returns: TRUE if the event was handled.
4499 gst_pad_send_event (GstPad * pad, GstEvent * event)
4501 gboolean result = FALSE;
4502 GstPadEventFunction eventfunc;
4503 gboolean serialized, need_unlock = FALSE;
4505 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4506 g_return_val_if_fail (event != NULL, FALSE);
4508 GST_OBJECT_LOCK (pad);
4509 if (GST_PAD_IS_SINK (pad)) {
4510 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4511 goto wrong_direction;
4512 serialized = GST_EVENT_IS_SERIALIZED (event);
4513 } else if (GST_PAD_IS_SRC (pad)) {
4514 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4515 goto wrong_direction;
4516 /* events on srcpad never are serialized */
4519 goto unknown_direction;
4522 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4523 GST_OBJECT_UNLOCK (pad);
4525 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4528 GST_OBJECT_LOCK (pad);
4531 switch (GST_EVENT_TYPE (event)) {
4532 case GST_EVENT_FLUSH_START:
4533 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4534 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4536 /* can't even accept a flush begin event when flushing */
4537 if (GST_PAD_IS_FLUSHING (pad))
4540 _priv_gst_pad_invalidate_cache (pad);
4541 GST_PAD_SET_FLUSHING (pad);
4542 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4544 case GST_EVENT_FLUSH_STOP:
4545 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4546 GST_PAD_UNSET_FLUSHING (pad);
4547 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4549 GST_OBJECT_UNLOCK (pad);
4550 /* grab stream lock */
4551 GST_PAD_STREAM_LOCK (pad);
4553 GST_OBJECT_LOCK (pad);
4556 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4557 GST_EVENT_TYPE_NAME (event));
4559 /* make this a little faster, no point in grabbing the lock
4560 * if the pad is allready flushing. */
4561 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4565 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4566 GST_OBJECT_UNLOCK (pad);
4567 GST_PAD_STREAM_LOCK (pad);
4569 GST_OBJECT_LOCK (pad);
4570 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4575 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4578 GST_OBJECT_UNLOCK (pad);
4580 result = eventfunc (pad, event);
4583 GST_PAD_STREAM_UNLOCK (pad);
4585 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4589 /* ERROR handling */
4592 g_warning ("pad %s:%s sending %s event in wrong direction",
4593 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4594 GST_OBJECT_UNLOCK (pad);
4595 gst_event_unref (event);
4600 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4601 GST_OBJECT_UNLOCK (pad);
4602 gst_event_unref (event);
4607 g_warning ("pad %s:%s has no event handler, file a bug.",
4608 GST_DEBUG_PAD_NAME (pad));
4609 GST_OBJECT_UNLOCK (pad);
4611 GST_PAD_STREAM_UNLOCK (pad);
4612 gst_event_unref (event);
4617 GST_OBJECT_UNLOCK (pad);
4619 GST_PAD_STREAM_UNLOCK (pad);
4620 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4621 "Received event on flushing pad. Discarding");
4622 gst_event_unref (event);
4627 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4628 gst_event_unref (event);
4634 * gst_pad_set_element_private:
4635 * @pad: the #GstPad to set the private data of.
4636 * @priv: The private data to attach to the pad.
4638 * Set the given private data gpointer on the pad.
4639 * This function can only be used by the element that owns the pad.
4640 * No locking is performed in this function.
4643 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4645 pad->element_private = priv;
4649 * gst_pad_get_element_private:
4650 * @pad: the #GstPad to get the private data of.
4652 * Gets the private data of a pad.
4653 * No locking is performed in this function.
4655 * Returns: (transfer none): a #gpointer to the private data.
4658 gst_pad_get_element_private (GstPad * pad)
4660 return pad->element_private;
4664 do_stream_status (GstPad * pad, GstStreamStatusType type,
4665 GThread * thread, GstTask * task)
4669 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4671 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4672 if (GST_IS_ELEMENT (parent)) {
4673 GstMessage *message;
4674 GValue value = { 0 };
4676 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4677 gchar *tname, *ename, *pname;
4679 /* create a good task name */
4680 ename = gst_element_get_name (parent);
4681 pname = gst_pad_get_name (pad);
4682 tname = g_strdup_printf ("%s:%s", ename, pname);
4686 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4690 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4693 g_value_init (&value, GST_TYPE_TASK);
4694 g_value_set_object (&value, task);
4695 gst_message_set_stream_status_object (message, &value);
4696 g_value_unset (&value);
4698 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4699 gst_element_post_message (parent, message);
4701 gst_object_unref (parent);
4706 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4708 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4713 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4715 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4719 static GstTaskThreadCallbacks thr_callbacks = {
4725 * gst_pad_start_task:
4726 * @pad: the #GstPad to start the task of
4727 * @func: the task function to call
4728 * @data: data passed to the task function
4730 * Starts a task that repeatedly calls @func with @data. This function
4731 * is mostly used in pad activation functions to start the dataflow.
4732 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4733 * before @func is called.
4735 * Returns: a %TRUE if the task could be started.
4738 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4743 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4744 g_return_val_if_fail (func != NULL, FALSE);
4746 GST_DEBUG_OBJECT (pad, "start task");
4748 GST_OBJECT_LOCK (pad);
4749 task = GST_PAD_TASK (pad);
4751 task = gst_task_create (func, data);
4752 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4753 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4754 GST_DEBUG_OBJECT (pad, "created task");
4755 GST_PAD_TASK (pad) = task;
4756 gst_object_ref (task);
4757 /* release lock to post the message */
4758 GST_OBJECT_UNLOCK (pad);
4760 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4762 gst_object_unref (task);
4764 GST_OBJECT_LOCK (pad);
4765 /* nobody else is supposed to have changed the pad now */
4766 if (GST_PAD_TASK (pad) != task)
4767 goto concurrent_stop;
4769 res = gst_task_set_state (task, GST_TASK_STARTED);
4770 GST_OBJECT_UNLOCK (pad);
4777 GST_OBJECT_UNLOCK (pad);
4783 * gst_pad_pause_task:
4784 * @pad: the #GstPad to pause the task of
4786 * Pause the task of @pad. This function will also wait until the
4787 * function executed by the task is finished if this function is not
4788 * called from the task function.
4790 * Returns: a TRUE if the task could be paused or FALSE when the pad
4794 gst_pad_pause_task (GstPad * pad)
4799 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4801 GST_DEBUG_OBJECT (pad, "pause task");
4803 GST_OBJECT_LOCK (pad);
4804 task = GST_PAD_TASK (pad);
4807 res = gst_task_set_state (task, GST_TASK_PAUSED);
4808 GST_OBJECT_UNLOCK (pad);
4810 /* wait for task function to finish, this lock is recursive so it does nothing
4811 * when the pause is called from the task itself */
4812 GST_PAD_STREAM_LOCK (pad);
4813 GST_PAD_STREAM_UNLOCK (pad);
4819 GST_DEBUG_OBJECT (pad, "pad has no task");
4820 GST_OBJECT_UNLOCK (pad);
4826 * gst_pad_stop_task:
4827 * @pad: the #GstPad to stop the task of
4829 * Stop the task of @pad. This function will also make sure that the
4830 * function executed by the task will effectively stop if not called
4831 * from the GstTaskFunction.
4833 * This function will deadlock if called from the GstTaskFunction of
4834 * the task. Use gst_task_pause() instead.
4836 * Regardless of whether the pad has a task, the stream lock is acquired and
4837 * released so as to ensure that streaming through this pad has finished.
4839 * Returns: a TRUE if the task could be stopped or FALSE on error.
4842 gst_pad_stop_task (GstPad * pad)
4847 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4849 GST_DEBUG_OBJECT (pad, "stop task");
4851 GST_OBJECT_LOCK (pad);
4852 task = GST_PAD_TASK (pad);
4855 GST_PAD_TASK (pad) = NULL;
4856 res = gst_task_set_state (task, GST_TASK_STOPPED);
4857 GST_OBJECT_UNLOCK (pad);
4859 GST_PAD_STREAM_LOCK (pad);
4860 GST_PAD_STREAM_UNLOCK (pad);
4862 if (!gst_task_join (task))
4865 gst_object_unref (task);
4871 GST_DEBUG_OBJECT (pad, "no task");
4872 GST_OBJECT_UNLOCK (pad);
4874 GST_PAD_STREAM_LOCK (pad);
4875 GST_PAD_STREAM_UNLOCK (pad);
4877 /* this is not an error */
4882 /* this is bad, possibly the application tried to join the task from
4883 * the task's thread. We install the task again so that it will be stopped
4884 * again from the right thread next time hopefully. */
4885 GST_OBJECT_LOCK (pad);
4886 GST_DEBUG_OBJECT (pad, "join failed");
4887 /* we can only install this task if there was no other task */
4888 if (GST_PAD_TASK (pad) == NULL)
4889 GST_PAD_TASK (pad) = task;
4890 GST_OBJECT_UNLOCK (pad);