2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
30 * After two pads are retrieved from an element with gst_element_get_pad(),
31 * the pads can be link with gst_pad_link(). (For quick links,
32 * you can also use gst_element_link(), which will make the obvious
33 * link for you if it's straightforward.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
38 * Pads have #GstCaps attached to it to describe the media type they are
39 * capable of dealing with. gst_pad_get_caps() and gst_pad_set_caps() are
40 * used to manipulate the caps of the pads.
41 * Pads created from a pad template cannot set capabilities that are
42 * incompatible with the pad template capabilities.
44 * Pads without pad templates can be created with gst_pad_new(),
45 * which takes a direction and a name as an argument. If the name is NULL,
46 * then a guaranteed unique name will be assigned to it.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
73 #include "glib-compat-private.h"
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
78 /* Pad signals and args */
98 typedef struct _GstPadPushCache GstPadPushCache;
100 struct _GstPadPushCache
102 GstPad *peer; /* reffed peer pad */
103 GstCaps *caps; /* caps for this link */
106 static GstPadPushCache _pad_cache_invalid = { NULL, };
108 #define PAD_CACHE_INVALID (&_pad_cache_invalid)
110 #define GST_PAD_GET_PRIVATE(obj) \
111 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
113 struct _GstPadPrivate
115 GstPadPushCache *cache_ptr;
118 static void gst_pad_dispose (GObject * object);
119 static void gst_pad_finalize (GObject * object);
120 static void gst_pad_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_pad_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static GstFlowReturn handle_pad_block (GstPad * pad);
126 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
127 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
128 static gboolean gst_pad_activate_default (GstPad * pad);
129 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
131 static GstObjectClass *parent_class = NULL;
132 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
134 static GParamSpec *pspec_caps = NULL;
136 /* quarks for probe signals */
137 static GQuark buffer_quark;
138 static GQuark event_quark;
147 static GstFlowQuarks flow_quarks[] = {
148 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
149 {GST_FLOW_RESEND, "resend", 0},
150 {GST_FLOW_OK, "ok", 0},
151 {GST_FLOW_NOT_LINKED, "not-linked", 0},
152 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
153 {GST_FLOW_UNEXPECTED, "unexpected", 0},
154 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
155 {GST_FLOW_ERROR, "error", 0},
156 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
157 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
162 * @ret: a #GstFlowReturn to get the name of.
164 * Gets a string representing the given flow return.
166 * Returns: a static string with the name of the flow return.
168 G_CONST_RETURN gchar *
169 gst_flow_get_name (GstFlowReturn ret)
173 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
175 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
176 if (ret == flow_quarks[i].ret)
177 return flow_quarks[i].name;
184 * @ret: a #GstFlowReturn to get the quark of.
186 * Get the unique quark for the given GstFlowReturn.
188 * Returns: the quark associated with the flow return or 0 if an
189 * invalid return was specified.
192 gst_flow_to_quark (GstFlowReturn ret)
196 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
198 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
199 if (ret == flow_quarks[i].ret)
200 return flow_quarks[i].quark;
209 buffer_quark = g_quark_from_static_string ("buffer"); \
210 event_quark = g_quark_from_static_string ("event"); \
212 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
213 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
216 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
217 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
220 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
223 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
224 GValue * return_accu, const GValue * handler_return, gpointer dummy)
226 gboolean ret = g_value_get_boolean (handler_return);
228 GST_DEBUG ("accumulated %d", ret);
229 g_value_set_boolean (return_accu, ret);
235 default_have_data (GstPad * pad, GstMiniObject * o)
241 gst_pad_class_init (GstPadClass * klass)
243 GObjectClass *gobject_class;
244 GstObjectClass *gstobject_class;
246 gobject_class = G_OBJECT_CLASS (klass);
247 gstobject_class = GST_OBJECT_CLASS (klass);
249 g_type_class_add_private (klass, sizeof (GstPadPrivate));
251 parent_class = g_type_class_peek_parent (klass);
253 gobject_class->dispose = gst_pad_dispose;
254 gobject_class->finalize = gst_pad_finalize;
255 gobject_class->set_property = gst_pad_set_property;
256 gobject_class->get_property = gst_pad_get_property;
260 * @pad: the pad that emitted the signal
261 * @peer: the peer pad that has been connected
263 * Signals that a pad has been linked to the peer pad.
265 gst_pad_signals[PAD_LINKED] =
266 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
267 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
268 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
271 * @pad: the pad that emitted the signal
272 * @peer: the peer pad that has been disconnected
274 * Signals that a pad has been unlinked from the peer pad.
276 gst_pad_signals[PAD_UNLINKED] =
277 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
278 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
279 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
281 * GstPad::request-link:
282 * @pad: the pad that emitted the signal
283 * @peer: the peer pad for which a connection is requested
285 * Signals that a pad connection has been requested.
287 gst_pad_signals[PAD_REQUEST_LINK] =
288 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
289 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
290 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
294 * @pad: the pad that emitted the signal
295 * @mini_obj: new data
297 * Signals that new data is available on the pad. This signal is used
298 * internally for implementing pad probes.
299 * See gst_pad_add_*_probe functions.
301 * Returns: %TRUE to keep the data, %FALSE to drop it
303 gst_pad_signals[PAD_HAVE_DATA] =
304 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
305 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
306 G_STRUCT_OFFSET (GstPadClass, have_data),
307 _gst_do_pass_data_accumulator,
308 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
310 pspec_caps = g_param_spec_boxed ("caps", "Caps",
311 "The capabilities of the pad", GST_TYPE_CAPS,
312 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
313 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
315 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
316 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
317 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
318 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
319 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
320 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
321 g_param_spec_object ("template", "Template",
322 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
323 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 gstobject_class->path_string_separator = ".";
327 /* Register common function pointer descriptions */
328 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
329 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
330 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
331 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
332 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
333 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
335 /* from gstutils.c */
336 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_fixed_caps_func);
338 klass->have_data = default_have_data;
342 gst_pad_init (GstPad * pad)
344 pad->priv = GST_PAD_GET_PRIVATE (pad);
346 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
347 GST_PAD_PEER (pad) = NULL;
349 GST_PAD_CHAINFUNC (pad) = NULL;
351 GST_PAD_LINKFUNC (pad) = NULL;
353 GST_PAD_CAPS (pad) = NULL;
354 GST_PAD_GETCAPSFUNC (pad) = NULL;
356 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
357 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
358 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
359 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
360 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
362 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
364 pad->do_buffer_signals = 0;
365 pad->do_event_signals = 0;
367 GST_PAD_SET_FLUSHING (pad);
369 /* FIXME 0.11: Store this directly in the instance struct */
370 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
371 g_static_rec_mutex_init (pad->stream_rec_lock);
373 pad->block_cond = g_cond_new ();
377 gst_pad_dispose (GObject * object)
379 GstPad *pad = GST_PAD_CAST (object);
382 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
384 /* unlink the peer pad */
385 if ((peer = gst_pad_get_peer (pad))) {
386 /* window for MT unsafeness, someone else could unlink here
387 * and then we call unlink with wrong pads. The unlink
388 * function would catch this and safely return failed. */
389 if (GST_PAD_IS_SRC (pad))
390 gst_pad_unlink (pad, peer);
392 gst_pad_unlink (peer, pad);
394 gst_object_unref (peer);
398 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
400 gst_pad_set_pad_template (pad, NULL);
402 if (pad->block_destroy_data && pad->block_data) {
403 pad->block_destroy_data (pad->block_data);
404 pad->block_data = NULL;
407 if (GST_PAD_CONTEXT (pad))
408 gst_context_replace (&GST_PAD_CONTEXT (pad), NULL);
410 G_OBJECT_CLASS (parent_class)->dispose (object);
414 gst_pad_finalize (GObject * object)
416 GstPad *pad = GST_PAD_CAST (object);
419 /* in case the task is still around, clean it up */
420 if ((task = GST_PAD_TASK (pad))) {
421 gst_task_join (task);
422 GST_PAD_TASK (pad) = NULL;
423 gst_object_unref (task);
426 if (pad->stream_rec_lock) {
427 g_static_rec_mutex_free (pad->stream_rec_lock);
428 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
429 pad->stream_rec_lock = NULL;
431 if (pad->block_cond) {
432 g_cond_free (pad->block_cond);
433 pad->block_cond = NULL;
436 G_OBJECT_CLASS (parent_class)->finalize (object);
440 gst_pad_set_property (GObject * object, guint prop_id,
441 const GValue * value, GParamSpec * pspec)
443 g_return_if_fail (GST_IS_PAD (object));
446 case PAD_PROP_DIRECTION:
447 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
449 case PAD_PROP_TEMPLATE:
450 gst_pad_set_pad_template (GST_PAD_CAST (object),
451 (GstPadTemplate *) g_value_get_object (value));
454 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
460 gst_pad_get_property (GObject * object, guint prop_id,
461 GValue * value, GParamSpec * pspec)
463 g_return_if_fail (GST_IS_PAD (object));
467 GST_OBJECT_LOCK (object);
468 g_value_set_boxed (value, GST_PAD_CAPS (object));
469 GST_OBJECT_UNLOCK (object);
471 case PAD_PROP_DIRECTION:
472 g_value_set_enum (value, GST_PAD_DIRECTION (object));
474 case PAD_PROP_TEMPLATE:
475 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
478 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 * @name: the name of the new pad.
486 * @direction: the #GstPadDirection of the pad.
488 * Creates a new pad with the given name in the given direction.
489 * If name is NULL, a guaranteed unique name (across all pads)
491 * This function makes a copy of the name so you can safely free the name.
493 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
498 gst_pad_new (const gchar * name, GstPadDirection direction)
500 return g_object_new (GST_TYPE_PAD,
501 "name", name, "direction", direction, NULL);
505 * gst_pad_new_from_template:
506 * @templ: the pad template to use
507 * @name: the name of the element
509 * Creates a new pad with the given name from the given template.
510 * If name is NULL, a guaranteed unique name (across all pads)
512 * This function makes a copy of the name so you can safely free the name.
514 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
517 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
519 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
521 return g_object_new (GST_TYPE_PAD,
522 "name", name, "direction", templ->direction, "template", templ, NULL);
526 * gst_pad_new_from_static_template:
527 * @templ: the #GstStaticPadTemplate to use
528 * @name: the name of the element
530 * Creates a new pad with the given name from the given static template.
531 * If name is NULL, a guaranteed unique name (across all pads)
533 * This function makes a copy of the name so you can safely free the name.
535 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
538 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
542 GstPadTemplate *template;
544 template = gst_static_pad_template_get (templ);
545 pad = gst_pad_new_from_template (template, name);
546 gst_object_unref (template);
551 * gst_pad_get_direction:
552 * @pad: a #GstPad to get the direction of.
554 * Gets the direction of the pad. The direction of the pad is
555 * decided at construction time so this function does not take
558 * Returns: the #GstPadDirection of the pad.
563 gst_pad_get_direction (GstPad * pad)
565 GstPadDirection result;
567 /* PAD_UNKNOWN is a little silly but we need some sort of
568 * error return value */
569 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
571 result = GST_PAD_DIRECTION (pad);
577 gst_pad_activate_default (GstPad * pad)
579 return gst_pad_activate_push (pad, TRUE);
583 pre_activate (GstPad * pad, GstActivateMode new_mode)
586 case GST_ACTIVATE_PUSH:
587 case GST_ACTIVATE_PULL:
588 GST_OBJECT_LOCK (pad);
589 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
591 GST_PAD_UNSET_FLUSHING (pad);
592 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
593 GST_OBJECT_UNLOCK (pad);
595 case GST_ACTIVATE_NONE:
596 GST_OBJECT_LOCK (pad);
597 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
598 _priv_gst_pad_invalidate_cache (pad);
599 GST_PAD_SET_FLUSHING (pad);
600 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
601 /* unlock blocked pads so element can resume and stop */
602 GST_PAD_BLOCK_BROADCAST (pad);
603 GST_OBJECT_UNLOCK (pad);
609 post_activate (GstPad * pad, GstActivateMode new_mode)
612 case GST_ACTIVATE_PUSH:
613 case GST_ACTIVATE_PULL:
616 case GST_ACTIVATE_NONE:
617 /* ensures that streaming stops */
618 GST_PAD_STREAM_LOCK (pad);
619 GST_DEBUG_OBJECT (pad, "stopped streaming");
620 GST_OBJECT_LOCK (pad);
621 if (GST_PAD_CONTEXT (pad))
622 gst_context_clear (GST_PAD_CONTEXT (pad));
623 GST_OBJECT_UNLOCK (pad);
624 GST_PAD_STREAM_UNLOCK (pad);
630 * gst_pad_set_active:
631 * @pad: the #GstPad to activate or deactivate.
632 * @active: whether or not the pad should be active.
634 * Activates or deactivates the given pad.
635 * Normally called from within core state change functions.
637 * If @active, makes sure the pad is active. If it is already active, either in
638 * push or pull mode, just return. Otherwise dispatches to the pad's activate
639 * function to perform the actual activation.
641 * If not @active, checks the pad's current mode and calls
642 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
645 * Returns: #TRUE if the operation was successful.
650 gst_pad_set_active (GstPad * pad, gboolean active)
653 gboolean ret = FALSE;
655 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
657 GST_OBJECT_LOCK (pad);
658 old = GST_PAD_ACTIVATE_MODE (pad);
659 GST_OBJECT_UNLOCK (pad);
663 case GST_ACTIVATE_PUSH:
664 GST_DEBUG_OBJECT (pad, "activating pad from push");
667 case GST_ACTIVATE_PULL:
668 GST_DEBUG_OBJECT (pad, "activating pad from pull");
671 case GST_ACTIVATE_NONE:
672 GST_DEBUG_OBJECT (pad, "activating pad from none");
673 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
676 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
681 case GST_ACTIVATE_PUSH:
682 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
683 ret = gst_pad_activate_push (pad, FALSE);
685 case GST_ACTIVATE_PULL:
686 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
687 ret = gst_pad_activate_pull (pad, FALSE);
689 case GST_ACTIVATE_NONE:
690 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
694 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
700 GST_OBJECT_LOCK (pad);
702 g_critical ("Failed to deactivate pad %s:%s, very bad",
703 GST_DEBUG_PAD_NAME (pad));
705 GST_WARNING_OBJECT (pad, "Failed to activate pad");
707 GST_OBJECT_UNLOCK (pad);
714 * gst_pad_activate_pull:
715 * @pad: the #GstPad to activate or deactivate.
716 * @active: whether or not the pad should be active.
718 * Activates or deactivates the given pad in pull mode via dispatching to the
719 * pad's activatepullfunc. For use from within pad activation functions only.
720 * When called on sink pads, will first proxy the call to the peer pad, which
721 * is expected to activate its internally linked pads from within its
722 * activate_pull function.
724 * If you don't know what this is, you probably don't want to call it.
726 * Returns: TRUE if the operation was successful.
731 gst_pad_activate_pull (GstPad * pad, gboolean active)
733 GstActivateMode old, new;
736 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
738 GST_OBJECT_LOCK (pad);
739 old = GST_PAD_ACTIVATE_MODE (pad);
740 GST_OBJECT_UNLOCK (pad);
744 case GST_ACTIVATE_PULL:
745 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
747 case GST_ACTIVATE_PUSH:
748 GST_DEBUG_OBJECT (pad,
749 "activating pad from push, deactivate push first");
750 /* pad was activate in the wrong direction, deactivate it
751 * and reactivate it in pull mode */
752 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
753 goto deactivate_failed;
754 /* fallthrough, pad is deactivated now. */
755 case GST_ACTIVATE_NONE:
756 GST_DEBUG_OBJECT (pad, "activating pad from none");
761 case GST_ACTIVATE_NONE:
762 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
764 case GST_ACTIVATE_PUSH:
765 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
766 /* pad was activated in the other direction, deactivate it
767 * in push mode, this should not happen... */
768 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
769 goto deactivate_failed;
770 /* everything is fine now */
772 case GST_ACTIVATE_PULL:
773 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
778 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
779 if ((peer = gst_pad_get_peer (pad))) {
780 GST_DEBUG_OBJECT (pad, "calling peer");
781 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
783 gst_object_unref (peer);
785 /* there is no peer, this is only fatal when we activate. When we
786 * deactivate, we must assume the application has unlinked the peer and
787 * will deactivate it eventually. */
791 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
794 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
795 goto failure; /* Can't activate pull on a src without a
799 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
800 pre_activate (pad, new);
802 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
803 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
806 /* can happen for sinks of passthrough elements */
809 post_activate (pad, new);
811 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
812 active ? "activated" : "deactivated");
818 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
819 active ? "activated" : "deactivated");
824 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
825 "failed to %s in switch to pull from mode %d",
826 (active ? "activate" : "deactivate"), old);
831 GST_OBJECT_LOCK (peer);
832 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
833 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
834 GST_OBJECT_UNLOCK (peer);
835 gst_object_unref (peer);
840 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
846 GST_OBJECT_LOCK (pad);
847 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
848 active ? "activate" : "deactivate");
849 _priv_gst_pad_invalidate_cache (pad);
850 GST_PAD_SET_FLUSHING (pad);
851 GST_PAD_ACTIVATE_MODE (pad) = old;
852 GST_OBJECT_UNLOCK (pad);
858 * gst_pad_activate_push:
859 * @pad: the #GstPad to activate or deactivate.
860 * @active: whether the pad should be active or not.
862 * Activates or deactivates the given pad in push mode via dispatching to the
863 * pad's activatepushfunc. For use from within pad activation functions only.
865 * If you don't know what this is, you probably don't want to call it.
867 * Returns: %TRUE if the operation was successful.
872 gst_pad_activate_push (GstPad * pad, gboolean active)
874 GstActivateMode old, new;
876 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
877 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
878 active ? "activated" : "deactivated");
880 GST_OBJECT_LOCK (pad);
881 old = GST_PAD_ACTIVATE_MODE (pad);
882 GST_OBJECT_UNLOCK (pad);
886 case GST_ACTIVATE_PUSH:
887 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
889 case GST_ACTIVATE_PULL:
890 GST_DEBUG_OBJECT (pad,
891 "activating pad from push, deactivating pull first");
892 /* pad was activate in the wrong direction, deactivate it
893 * an reactivate it in push mode */
894 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
895 goto deactivate_failed;
896 /* fallthrough, pad is deactivated now. */
897 case GST_ACTIVATE_NONE:
898 GST_DEBUG_OBJECT (pad, "activating pad from none");
903 case GST_ACTIVATE_NONE:
904 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
906 case GST_ACTIVATE_PULL:
907 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
908 /* pad was activated in the other direction, deactivate it
909 * in pull mode, this should not happen... */
910 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
911 goto deactivate_failed;
912 /* everything is fine now */
914 case GST_ACTIVATE_PUSH:
915 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
920 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
921 pre_activate (pad, new);
923 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
924 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
928 /* quite ok, element relies on state change func to prepare itself */
931 post_activate (pad, new);
933 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
934 active ? "activated" : "deactivated");
939 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
940 active ? "activated" : "deactivated");
945 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
946 "failed to %s in switch to push from mode %d",
947 (active ? "activate" : "deactivate"), old);
952 GST_OBJECT_LOCK (pad);
953 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
954 active ? "activate" : "deactivate");
955 _priv_gst_pad_invalidate_cache (pad);
956 GST_PAD_SET_FLUSHING (pad);
957 GST_PAD_ACTIVATE_MODE (pad) = old;
958 GST_OBJECT_UNLOCK (pad);
965 * @pad: the #GstPad to query
967 * Query if a pad is active
969 * Returns: TRUE if the pad is active.
974 gst_pad_is_active (GstPad * pad)
976 gboolean result = FALSE;
978 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
980 GST_OBJECT_LOCK (pad);
981 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
982 GST_OBJECT_UNLOCK (pad);
988 * gst_pad_set_blocked_async_full:
989 * @pad: the #GstPad to block or unblock
990 * @blocked: boolean indicating whether the pad should be blocked or unblocked
991 * @callback: #GstPadBlockCallback that will be called when the
993 * @user_data: (closure): user data passed to the callback
994 * @destroy_data: #GDestroyNotify for user_data
996 * Blocks or unblocks the dataflow on a pad. The provided callback
997 * is called when the operation succeeds; this happens right before the next
998 * attempt at pushing a buffer on the pad.
1000 * This can take a while as the pad can only become blocked when real dataflow
1002 * When the pipeline is stalled, for example in PAUSED, this can
1003 * take an indeterminate amount of time.
1004 * You can pass NULL as the callback to make this call block. Be careful with
1005 * this blocking call as it might not return for reasons stated above.
1008 * Pad block handlers are only called for source pads in push mode
1009 * and sink pads in pull mode.
1012 * Returns: TRUE if the pad could be blocked. This function can fail if the
1013 * wrong parameters were passed or the pad was already in the requested state.
1020 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1021 GstPadBlockCallback callback, gpointer user_data,
1022 GDestroyNotify destroy_data)
1024 gboolean was_blocked = FALSE;
1026 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1028 GST_OBJECT_LOCK (pad);
1030 was_blocked = GST_PAD_IS_BLOCKED (pad);
1032 if (G_UNLIKELY (was_blocked == blocked))
1033 goto had_right_state;
1036 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1038 _priv_gst_pad_invalidate_cache (pad);
1039 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1041 if (pad->block_destroy_data && pad->block_data)
1042 pad->block_destroy_data (pad->block_data);
1044 pad->block_callback = callback;
1045 pad->block_data = user_data;
1046 pad->block_destroy_data = destroy_data;
1047 pad->block_callback_called = FALSE;
1049 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1050 GST_PAD_BLOCK_WAIT (pad);
1051 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1054 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1056 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1058 if (pad->block_destroy_data && pad->block_data)
1059 pad->block_destroy_data (pad->block_data);
1061 pad->block_callback = callback;
1062 pad->block_data = user_data;
1063 pad->block_destroy_data = destroy_data;
1064 pad->block_callback_called = FALSE;
1066 GST_PAD_BLOCK_BROADCAST (pad);
1068 /* no callback, wait for the unblock to happen */
1069 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1070 GST_PAD_BLOCK_WAIT (pad);
1071 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1074 GST_OBJECT_UNLOCK (pad);
1080 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1081 "pad was in right state (%d)", was_blocked);
1082 GST_OBJECT_UNLOCK (pad);
1089 * gst_pad_set_blocked_async:
1090 * @pad: the #GstPad to block or unblock
1091 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1092 * @callback: #GstPadBlockCallback that will be called when the
1093 * operation succeeds
1094 * @user_data: (closure): user data passed to the callback
1096 * Blocks or unblocks the dataflow on a pad. The provided callback
1097 * is called when the operation succeeds; this happens right before the next
1098 * attempt at pushing a buffer on the pad.
1100 * This can take a while as the pad can only become blocked when real dataflow
1102 * When the pipeline is stalled, for example in PAUSED, this can
1103 * take an indeterminate amount of time.
1104 * You can pass NULL as the callback to make this call block. Be careful with
1105 * this blocking call as it might not return for reasons stated above.
1108 * Pad block handlers are only called for source pads in push mode
1109 * and sink pads in pull mode.
1112 * Returns: TRUE if the pad could be blocked. This function can fail if the
1113 * wrong parameters were passed or the pad was already in the requested state.
1118 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1119 GstPadBlockCallback callback, gpointer user_data)
1121 return gst_pad_set_blocked_async_full (pad, blocked,
1122 callback, user_data, NULL);
1126 * gst_pad_set_blocked:
1127 * @pad: the #GstPad to block or unblock
1128 * @blocked: boolean indicating we should block or unblock
1130 * Blocks or unblocks the dataflow on a pad. This function is
1131 * a shortcut for gst_pad_set_blocked_async() with a NULL
1135 * Pad blocks are only possible for source pads in push mode
1136 * and sink pads in pull mode.
1139 * Returns: TRUE if the pad could be blocked. This function can fail if the
1140 * wrong parameters were passed or the pad was already in the requested state.
1145 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1147 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1151 * gst_pad_is_blocked:
1152 * @pad: the #GstPad to query
1154 * Checks if the pad is blocked or not. This function returns the
1155 * last requested state of the pad. It is not certain that the pad
1156 * is actually blocking at this point (see gst_pad_is_blocking()).
1158 * Returns: TRUE if the pad is blocked.
1163 gst_pad_is_blocked (GstPad * pad)
1165 gboolean result = FALSE;
1167 g_return_val_if_fail (GST_IS_PAD (pad), result);
1169 GST_OBJECT_LOCK (pad);
1170 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1171 GST_OBJECT_UNLOCK (pad);
1177 * gst_pad_is_blocking:
1178 * @pad: the #GstPad to query
1180 * Checks if the pad is blocking or not. This is a guaranteed state
1181 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1183 * Returns: TRUE if the pad is blocking.
1190 gst_pad_is_blocking (GstPad * pad)
1192 gboolean result = FALSE;
1194 g_return_val_if_fail (GST_IS_PAD (pad), result);
1196 GST_OBJECT_LOCK (pad);
1197 /* the blocking flag is only valid if the pad is not flushing */
1198 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1199 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1200 GST_OBJECT_UNLOCK (pad);
1206 * gst_pad_set_activate_function:
1208 * @activate: the #GstPadActivateFunction to set.
1210 * Sets the given activate function for @pad. The activate function will
1211 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1212 * the actual activation. Only makes sense to set on sink pads.
1214 * Call this function if your sink pad can start a pull-based task.
1217 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1219 g_return_if_fail (GST_IS_PAD (pad));
1221 GST_PAD_ACTIVATEFUNC (pad) = activate;
1222 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1223 GST_DEBUG_FUNCPTR_NAME (activate));
1227 * gst_pad_set_activatepull_function:
1229 * @activatepull: the #GstPadActivateModeFunction to set.
1231 * Sets the given activate_pull function for the pad. An activate_pull function
1232 * prepares the element and any upstream connections for pulling. See XXX
1233 * part-activation.txt for details.
1236 gst_pad_set_activatepull_function (GstPad * pad,
1237 GstPadActivateModeFunction activatepull)
1239 g_return_if_fail (GST_IS_PAD (pad));
1241 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1242 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1243 GST_DEBUG_FUNCPTR_NAME (activatepull));
1247 * gst_pad_set_activatepush_function:
1249 * @activatepush: the #GstPadActivateModeFunction to set.
1251 * Sets the given activate_push function for the pad. An activate_push function
1252 * prepares the element for pushing. See XXX part-activation.txt for details.
1255 gst_pad_set_activatepush_function (GstPad * pad,
1256 GstPadActivateModeFunction activatepush)
1258 g_return_if_fail (GST_IS_PAD (pad));
1260 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1261 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1262 GST_DEBUG_FUNCPTR_NAME (activatepush));
1266 * gst_pad_set_chain_function:
1267 * @pad: a sink #GstPad.
1268 * @chain: the #GstPadChainFunction to set.
1270 * Sets the given chain function for the pad. The chain function is called to
1271 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1274 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1276 g_return_if_fail (GST_IS_PAD (pad));
1277 g_return_if_fail (GST_PAD_IS_SINK (pad));
1279 GST_PAD_CHAINFUNC (pad) = chain;
1280 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1281 GST_DEBUG_FUNCPTR_NAME (chain));
1285 * gst_pad_set_chain_list_function:
1286 * @pad: a sink #GstPad.
1287 * @chainlist: the #GstPadChainListFunction to set.
1289 * Sets the given chain list function for the pad. The chainlist function is
1290 * called to process a #GstBufferList input buffer list. See
1291 * #GstPadChainListFunction for more details.
1296 gst_pad_set_chain_list_function (GstPad * pad,
1297 GstPadChainListFunction chainlist)
1299 g_return_if_fail (GST_IS_PAD (pad));
1300 g_return_if_fail (GST_PAD_IS_SINK (pad));
1302 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1303 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1304 GST_DEBUG_FUNCPTR_NAME (chainlist));
1308 * gst_pad_set_getrange_function:
1309 * @pad: a source #GstPad.
1310 * @get: the #GstPadGetRangeFunction to set.
1312 * Sets the given getrange function for the pad. The getrange function is
1313 * called to produce a new #GstBuffer to start the processing pipeline. see
1314 * #GstPadGetRangeFunction for a description of the getrange function.
1317 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1319 g_return_if_fail (GST_IS_PAD (pad));
1320 g_return_if_fail (GST_PAD_IS_SRC (pad));
1322 GST_PAD_GETRANGEFUNC (pad) = get;
1324 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1325 GST_DEBUG_FUNCPTR_NAME (get));
1329 * gst_pad_set_checkgetrange_function:
1330 * @pad: a source #GstPad.
1331 * @check: the #GstPadCheckGetRangeFunction to set.
1333 * Sets the given checkgetrange function for the pad. Implement this function
1334 * on a pad if you dynamically support getrange based scheduling on the pad.
1337 gst_pad_set_checkgetrange_function (GstPad * pad,
1338 GstPadCheckGetRangeFunction check)
1340 g_return_if_fail (GST_IS_PAD (pad));
1341 g_return_if_fail (GST_PAD_IS_SRC (pad));
1343 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1345 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1346 GST_DEBUG_FUNCPTR_NAME (check));
1350 * gst_pad_set_event_function:
1351 * @pad: a #GstPad of either direction.
1352 * @event: the #GstPadEventFunction to set.
1354 * Sets the given event handler for the pad.
1357 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1359 g_return_if_fail (GST_IS_PAD (pad));
1361 GST_PAD_EVENTFUNC (pad) = event;
1363 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1364 GST_DEBUG_FUNCPTR_NAME (event));
1368 * gst_pad_set_query_function:
1369 * @pad: a #GstPad of either direction.
1370 * @query: the #GstPadQueryFunction to set.
1372 * Set the given query function for the pad.
1375 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1377 g_return_if_fail (GST_IS_PAD (pad));
1379 GST_PAD_QUERYFUNC (pad) = query;
1381 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1382 GST_DEBUG_FUNCPTR_NAME (query));
1386 * gst_pad_set_query_type_function:
1387 * @pad: a #GstPad of either direction.
1388 * @type_func: the #GstPadQueryTypeFunction to set.
1390 * Set the given query type function for the pad.
1393 gst_pad_set_query_type_function (GstPad * pad,
1394 GstPadQueryTypeFunction type_func)
1396 g_return_if_fail (GST_IS_PAD (pad));
1398 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1400 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1401 GST_DEBUG_FUNCPTR_NAME (type_func));
1405 * gst_pad_get_query_types:
1408 * Get an array of supported queries that can be performed
1411 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1414 const GstQueryType *
1415 gst_pad_get_query_types (GstPad * pad)
1417 GstPadQueryTypeFunction func;
1419 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1421 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1433 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1435 *data = gst_pad_get_query_types (pad);
1441 * gst_pad_get_query_types_default:
1444 * Invoke the default dispatcher for the query types on
1447 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1448 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1449 * query types function.
1451 const GstQueryType *
1452 gst_pad_get_query_types_default (GstPad * pad)
1454 GstQueryType *result = NULL;
1456 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1458 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1459 gst_pad_get_query_types_dispatcher, &result);
1465 * gst_pad_set_iterate_internal_links_function:
1466 * @pad: a #GstPad of either direction.
1467 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1469 * Sets the given internal link iterator function for the pad.
1474 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1475 GstPadIterIntLinkFunction iterintlink)
1477 g_return_if_fail (GST_IS_PAD (pad));
1479 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1480 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1481 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1485 * gst_pad_set_link_function:
1487 * @link: the #GstPadLinkFunction to set.
1489 * Sets the given link function for the pad. It will be called when
1490 * the pad is linked with another pad.
1492 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1495 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1496 * cannot be made for some reason.
1498 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1499 * of the peer sink pad, if present.
1502 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1504 g_return_if_fail (GST_IS_PAD (pad));
1506 GST_PAD_LINKFUNC (pad) = link;
1507 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1508 GST_DEBUG_FUNCPTR_NAME (link));
1512 * gst_pad_set_unlink_function:
1514 * @unlink: the #GstPadUnlinkFunction to set.
1516 * Sets the given unlink function for the pad. It will be called
1517 * when the pad is unlinked.
1520 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1522 g_return_if_fail (GST_IS_PAD (pad));
1524 GST_PAD_UNLINKFUNC (pad) = unlink;
1525 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1526 GST_DEBUG_FUNCPTR_NAME (unlink));
1530 * gst_pad_set_getcaps_function:
1532 * @getcaps: the #GstPadGetCapsFunction to set.
1534 * Sets the given getcaps function for the pad. @getcaps should return the
1535 * allowable caps for a pad in the context of the element's state, its link to
1536 * other elements, and the devices or files it has opened. These caps must be a
1537 * subset of the pad template caps. In the NULL state with no links, @getcaps
1538 * should ideally return the same caps as the pad template. In rare
1539 * circumstances, an object property can affect the caps returned by @getcaps,
1540 * but this is discouraged.
1542 * You do not need to call this function if @pad's allowed caps are always the
1543 * same as the pad template caps. This can only be true if the padtemplate
1544 * has fixed simple caps.
1546 * For most filters, the caps returned by @getcaps is directly affected by the
1547 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1548 * the srcpad's getcaps function is directly related to the stream data. Again,
1549 * @getcaps should return the most specific caps it reasonably can, since this
1550 * helps with autoplugging.
1552 * Note that the return value from @getcaps is owned by the caller, so the
1553 * caller should unref the caps after usage.
1556 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1558 g_return_if_fail (GST_IS_PAD (pad));
1560 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1561 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1562 GST_DEBUG_FUNCPTR_NAME (getcaps));
1566 * gst_pad_set_acceptcaps_function:
1568 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1570 * Sets the given acceptcaps function for the pad. The acceptcaps function
1571 * will be called to check if the pad can accept the given caps. Setting the
1572 * acceptcaps function to NULL restores the default behaviour of allowing
1573 * any caps that matches the caps from gst_pad_get_caps().
1576 gst_pad_set_acceptcaps_function (GstPad * pad,
1577 GstPadAcceptCapsFunction acceptcaps)
1579 g_return_if_fail (GST_IS_PAD (pad));
1581 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1582 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1583 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1587 * gst_pad_set_fixatecaps_function:
1589 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1591 * Sets the given fixatecaps function for the pad. The fixatecaps function
1592 * will be called whenever the default values for a GstCaps needs to be
1596 gst_pad_set_fixatecaps_function (GstPad * pad,
1597 GstPadFixateCapsFunction fixatecaps)
1599 g_return_if_fail (GST_IS_PAD (pad));
1601 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1602 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1603 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1607 * gst_pad_set_setcaps_function:
1609 * @setcaps: the #GstPadSetCapsFunction to set.
1611 * Sets the given setcaps function for the pad. The setcaps function
1612 * will be called whenever a buffer with a new media type is pushed or
1613 * pulled from the pad. The pad/element needs to update its internal
1614 * structures to process the new media type. If this new type is not
1615 * acceptable, the setcaps function should return FALSE.
1618 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1620 g_return_if_fail (GST_IS_PAD (pad));
1622 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1623 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1624 GST_DEBUG_FUNCPTR_NAME (setcaps));
1629 * @srcpad: the source #GstPad to unlink.
1630 * @sinkpad: the sink #GstPad to unlink.
1632 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1633 * signal on both pads.
1635 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1636 * the pads were not linked together.
1641 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1643 gboolean result = FALSE;
1644 GstElement *parent = NULL;
1646 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1647 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1648 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1649 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1651 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1652 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1653 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1655 /* We need to notify the parent before taking any pad locks as the bin in
1656 * question might be waiting for a lock on the pad while holding its lock
1657 * that our message will try to take. */
1658 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1659 if (GST_IS_ELEMENT (parent)) {
1660 gst_element_post_message (parent,
1661 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1662 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1664 gst_object_unref (parent);
1669 GST_OBJECT_LOCK (srcpad);
1671 GST_OBJECT_LOCK (sinkpad);
1673 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1674 goto not_linked_together;
1676 if (GST_PAD_UNLINKFUNC (srcpad)) {
1677 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1679 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1680 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1683 _priv_gst_pad_invalidate_cache (srcpad);
1685 /* first clear peers */
1686 GST_PAD_PEER (srcpad) = NULL;
1687 GST_PAD_PEER (sinkpad) = NULL;
1689 GST_OBJECT_UNLOCK (sinkpad);
1690 GST_OBJECT_UNLOCK (srcpad);
1692 /* fire off a signal to each of the pads telling them
1693 * that they've been unlinked */
1694 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1695 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1697 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1698 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1703 if (parent != NULL) {
1704 gst_element_post_message (parent,
1705 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1706 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1707 gst_object_unref (parent);
1712 not_linked_together:
1714 /* we do not emit a warning in this case because unlinking cannot
1715 * be made MT safe.*/
1716 GST_OBJECT_UNLOCK (sinkpad);
1717 GST_OBJECT_UNLOCK (srcpad);
1723 * gst_pad_is_linked:
1724 * @pad: pad to check
1726 * Checks if a @pad is linked to another pad or not.
1728 * Returns: TRUE if the pad is linked, FALSE otherwise.
1733 gst_pad_is_linked (GstPad * pad)
1737 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1739 GST_OBJECT_LOCK (pad);
1740 result = (GST_PAD_PEER (pad) != NULL);
1741 GST_OBJECT_UNLOCK (pad);
1746 /* get the caps from both pads and see if the intersection
1749 * This function should be called with the pad LOCK on both
1753 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1754 GstPadLinkCheck flags)
1756 GstCaps *srccaps = NULL;
1757 GstCaps *sinkcaps = NULL;
1758 gboolean compatible = FALSE;
1760 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1763 /* Doing the expensive caps checking takes priority over only checking the template caps */
1764 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1765 srccaps = gst_pad_get_caps_unlocked (src);
1766 sinkcaps = gst_pad_get_caps_unlocked (sink);
1768 /* If one of the two pads doesn't have a template, consider the intersection
1770 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1771 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1775 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1777 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1780 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1782 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1785 /* if we have caps on both pads we can check the intersection. If one
1786 * of the caps is NULL, we return TRUE. */
1787 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1789 gst_caps_unref (srccaps);
1791 gst_caps_unref (sinkcaps);
1795 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1796 gst_caps_unref (srccaps);
1797 gst_caps_unref (sinkcaps);
1800 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1801 (compatible ? "" : "not"));
1806 /* check if the grandparents of both pads are the same.
1807 * This check is required so that we don't try to link
1808 * pads from elements in different bins without ghostpads.
1810 * The LOCK should be held on both pads
1813 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1815 GstObject *psrc, *psink;
1817 psrc = GST_OBJECT_PARENT (src);
1818 psink = GST_OBJECT_PARENT (sink);
1820 /* if one of the pads has no parent, we allow the link */
1821 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1824 /* only care about parents that are elements */
1825 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1826 goto no_element_parent;
1828 /* if the parents are the same, we have a loop */
1829 if (G_UNLIKELY (psrc == psink))
1832 /* if they both have a parent, we check the grandparents. We can not lock
1833 * the parent because we hold on the child (pad) and the locking order is
1834 * parent >> child. */
1835 psrc = GST_OBJECT_PARENT (psrc);
1836 psink = GST_OBJECT_PARENT (psink);
1838 /* if they have grandparents but they are not the same */
1839 if (G_UNLIKELY (psrc != psink))
1840 goto wrong_grandparents;
1847 GST_CAT_DEBUG (GST_CAT_CAPS,
1848 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1849 GST_PTR_FORMAT, psrc, psink);
1854 GST_CAT_DEBUG (GST_CAT_CAPS,
1855 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1856 GST_PTR_FORMAT, psrc, psink);
1861 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1867 GST_CAT_DEBUG (GST_CAT_CAPS,
1868 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1869 GST_PTR_FORMAT, psrc, psink);
1874 /* FIXME leftover from an attempt at refactoring... */
1875 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1876 * the two pads will be locked in the srcpad, sinkpad order. */
1877 static GstPadLinkReturn
1878 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1880 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1881 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1883 GST_OBJECT_LOCK (srcpad);
1885 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1886 goto src_was_linked;
1888 GST_OBJECT_LOCK (sinkpad);
1890 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1891 goto sink_was_linked;
1893 /* check hierarchy, pads can only be linked if the grandparents
1895 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1896 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1897 goto wrong_hierarchy;
1899 /* check pad caps for non-empty intersection */
1900 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1903 /* FIXME check pad scheduling for non-empty intersection */
1905 return GST_PAD_LINK_OK;
1909 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1910 GST_DEBUG_PAD_NAME (srcpad),
1911 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1912 /* we do not emit a warning in this case because unlinking cannot
1913 * be made MT safe.*/
1914 GST_OBJECT_UNLOCK (srcpad);
1915 return GST_PAD_LINK_WAS_LINKED;
1919 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1920 GST_DEBUG_PAD_NAME (sinkpad),
1921 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1922 /* we do not emit a warning in this case because unlinking cannot
1923 * be made MT safe.*/
1924 GST_OBJECT_UNLOCK (sinkpad);
1925 GST_OBJECT_UNLOCK (srcpad);
1926 return GST_PAD_LINK_WAS_LINKED;
1930 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1931 GST_OBJECT_UNLOCK (sinkpad);
1932 GST_OBJECT_UNLOCK (srcpad);
1933 return GST_PAD_LINK_WRONG_HIERARCHY;
1937 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1938 GST_OBJECT_UNLOCK (sinkpad);
1939 GST_OBJECT_UNLOCK (srcpad);
1940 return GST_PAD_LINK_NOFORMAT;
1946 * @srcpad: the source #GstPad.
1947 * @sinkpad: the sink #GstPad.
1949 * Checks if the source pad and the sink pad are compatible so they can be
1952 * Returns: TRUE if the pads can be linked.
1955 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1957 GstPadLinkReturn result;
1959 /* generic checks */
1960 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1961 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1963 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1964 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1966 /* gst_pad_link_prepare does everything for us, we only release the locks
1967 * on the pads that it gets us. If this function returns !OK the locks are not
1969 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1970 if (result != GST_PAD_LINK_OK)
1973 GST_OBJECT_UNLOCK (srcpad);
1974 GST_OBJECT_UNLOCK (sinkpad);
1977 return result == GST_PAD_LINK_OK;
1981 * gst_pad_link_full:
1982 * @srcpad: the source #GstPad to link.
1983 * @sinkpad: the sink #GstPad to link.
1984 * @flags: the checks to validate when linking
1986 * Links the source pad and the sink pad.
1988 * This variant of #gst_pad_link provides a more granular control on the
1989 * checks being done when linking. While providing some considerable speedups
1990 * the caller of this method must be aware that wrong usage of those flags
1991 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1992 * for more information.
1996 * Returns: A result code indicating if the connection worked or
2002 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2004 GstPadLinkReturn result;
2007 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2008 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2009 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2010 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2011 GST_PAD_LINK_WRONG_DIRECTION);
2013 /* Notify the parent early. See gst_pad_unlink for details. */
2014 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2015 if (GST_IS_ELEMENT (parent)) {
2016 gst_element_post_message (parent,
2017 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2018 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2020 gst_object_unref (parent);
2025 /* prepare will also lock the two pads */
2026 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2028 if (result != GST_PAD_LINK_OK)
2031 /* must set peers before calling the link function */
2032 GST_PAD_PEER (srcpad) = sinkpad;
2033 GST_PAD_PEER (sinkpad) = srcpad;
2035 GST_OBJECT_UNLOCK (sinkpad);
2036 GST_OBJECT_UNLOCK (srcpad);
2038 /* FIXME released the locks here, concurrent thread might link
2039 * something else. */
2040 if (GST_PAD_LINKFUNC (srcpad)) {
2041 /* this one will call the peer link function */
2042 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2043 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2044 /* if no source link function, we need to call the sink link
2045 * function ourselves. */
2046 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2048 result = GST_PAD_LINK_OK;
2051 GST_OBJECT_LOCK (srcpad);
2052 GST_OBJECT_LOCK (sinkpad);
2054 if (result == GST_PAD_LINK_OK) {
2055 GST_OBJECT_UNLOCK (sinkpad);
2056 GST_OBJECT_UNLOCK (srcpad);
2058 /* fire off a signal to each of the pads telling them
2059 * that they've been linked */
2060 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2061 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2063 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2064 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2066 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2067 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2069 GST_PAD_PEER (srcpad) = NULL;
2070 GST_PAD_PEER (sinkpad) = NULL;
2072 GST_OBJECT_UNLOCK (sinkpad);
2073 GST_OBJECT_UNLOCK (srcpad);
2078 gst_element_post_message (parent,
2079 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2080 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2081 gst_object_unref (parent);
2089 * @srcpad: the source #GstPad to link.
2090 * @sinkpad: the sink #GstPad to link.
2092 * Links the source pad and the sink pad.
2094 * Returns: A result code indicating if the connection worked or
2100 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2102 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2106 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2108 GstPadTemplate **template_p;
2110 /* this function would need checks if it weren't static */
2112 GST_OBJECT_LOCK (pad);
2113 template_p = &pad->padtemplate;
2114 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2115 GST_OBJECT_UNLOCK (pad);
2118 gst_pad_template_pad_created (templ, pad);
2122 * gst_pad_get_pad_template:
2125 * Gets the template for @pad.
2127 * Returns: (transfer none): the #GstPadTemplate from which this pad was
2128 * instantiated, or %NULL if this pad has no template.
2130 * FIXME: currently returns an unrefcounted padtemplate.
2133 gst_pad_get_pad_template (GstPad * pad)
2135 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2137 return GST_PAD_PAD_TEMPLATE (pad);
2141 /* should be called with the pad LOCK held */
2142 /* refs the caps, so caller is responsible for getting it unreffed */
2144 gst_pad_get_caps_unlocked (GstPad * pad)
2146 GstCaps *result = NULL;
2147 GstPadTemplate *templ;
2149 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2151 if (GST_PAD_GETCAPSFUNC (pad)) {
2152 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2153 "dispatching to pad getcaps function");
2155 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2156 GST_OBJECT_UNLOCK (pad);
2157 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2158 GST_OBJECT_LOCK (pad);
2159 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2161 if (result == NULL) {
2162 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2163 GST_DEBUG_PAD_NAME (pad));
2165 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2166 "pad getcaps returned %" GST_PTR_FORMAT, result);
2167 #ifndef G_DISABLE_ASSERT
2168 /* check that the returned caps are a real subset of the template caps */
2169 if (GST_PAD_PAD_TEMPLATE (pad)) {
2170 const GstCaps *templ_caps =
2171 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2172 if (!gst_caps_is_subset (result, templ_caps)) {
2175 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2176 "pad returned caps %" GST_PTR_FORMAT
2177 " which are not a real subset of its template caps %"
2178 GST_PTR_FORMAT, result, templ_caps);
2180 ("pad %s:%s returned caps which are not a real "
2181 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2182 temp = gst_caps_intersect (templ_caps, result);
2183 gst_caps_unref (result);
2191 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2192 result = GST_PAD_TEMPLATE_CAPS (templ);
2193 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2194 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2197 result = gst_caps_ref (result);
2200 if ((result = GST_PAD_CAPS (pad))) {
2201 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2202 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2204 result = gst_caps_ref (result);
2208 /* this almost never happens */
2209 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2210 result = gst_caps_new_empty ();
2218 * @pad: a #GstPad to get the capabilities of.
2220 * Gets the capabilities this pad can produce or consume.
2221 * Note that this method doesn't necessarily return the caps set by
2222 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2223 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2224 * the pad's get_caps function;
2225 * this returns the pad template caps if not explicitly set.
2227 * Note that this function does not return writable #GstCaps, use
2228 * gst_caps_make_writable() before modifying the caps.
2230 * Returns: the caps of the pad with incremented ref-count.
2233 gst_pad_get_caps (GstPad * pad)
2235 GstCaps *result = NULL;
2237 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2239 GST_OBJECT_LOCK (pad);
2241 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2243 result = gst_pad_get_caps_unlocked (pad);
2245 GST_OBJECT_UNLOCK (pad);
2251 * gst_pad_peer_get_caps:
2252 * @pad: a #GstPad to get the capabilities of.
2254 * Gets the capabilities of the peer connected to this pad. Similar to
2255 * gst_pad_get_caps().
2257 * Returns: the caps of the peer pad with incremented ref-count. This function
2258 * returns %NULL when there is no peer pad.
2261 gst_pad_peer_get_caps (GstPad * pad)
2264 GstCaps *result = NULL;
2266 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2268 GST_OBJECT_LOCK (pad);
2270 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2272 peerpad = GST_PAD_PEER (pad);
2273 if (G_UNLIKELY (peerpad == NULL))
2276 gst_object_ref (peerpad);
2277 GST_OBJECT_UNLOCK (pad);
2279 result = gst_pad_get_caps (peerpad);
2281 gst_object_unref (peerpad);
2287 GST_OBJECT_UNLOCK (pad);
2293 fixate_value (GValue * dest, const GValue * src)
2295 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2296 g_value_init (dest, G_TYPE_INT);
2297 g_value_set_int (dest, gst_value_get_int_range_min (src));
2298 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2299 g_value_init (dest, G_TYPE_DOUBLE);
2300 g_value_set_double (dest, gst_value_get_double_range_min (src));
2301 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2302 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2303 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2304 GValue temp = { 0 };
2306 /* list could be empty */
2307 if (gst_value_list_get_size (src) <= 0)
2310 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2312 if (!fixate_value (dest, &temp))
2313 gst_value_init_and_copy (dest, &temp);
2314 g_value_unset (&temp);
2315 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2316 gboolean res = FALSE;
2319 len = gst_value_array_get_size (src);
2320 g_value_init (dest, GST_TYPE_ARRAY);
2321 for (n = 0; n < len; n++) {
2323 const GValue *orig_kid = gst_value_array_get_value (src, n);
2325 if (!fixate_value (&kid, orig_kid))
2326 gst_value_init_and_copy (&kid, orig_kid);
2329 gst_value_array_append_value (dest, &kid);
2330 g_value_unset (&kid);
2334 g_value_unset (dest);
2345 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2347 GstStructure *s = data;
2350 if (fixate_value (&v, value)) {
2351 gst_structure_id_set_value (s, field_id, &v);
2359 * gst_pad_fixate_caps:
2360 * @pad: a #GstPad to fixate
2361 * @caps: the #GstCaps to fixate
2363 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2364 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2367 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2369 GstPadFixateCapsFunction fixatefunc;
2372 g_return_if_fail (GST_IS_PAD (pad));
2373 g_return_if_fail (caps != NULL);
2374 g_return_if_fail (!gst_caps_is_empty (caps));
2375 g_return_if_fail (!gst_caps_is_any (caps));
2377 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2380 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2382 fixatefunc (pad, caps);
2385 /* default fixation */
2386 gst_caps_truncate (caps);
2387 s = gst_caps_get_structure (caps, 0);
2388 gst_structure_foreach (s, gst_pad_default_fixate, s);
2391 /* Default accept caps implementation just checks against
2392 * against the allowed caps for the pad */
2394 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2396 /* get the caps and see if it intersects to something not empty */
2398 gboolean result = FALSE;
2400 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2402 allowed = gst_pad_get_caps (pad);
2404 goto nothing_allowed;
2406 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2408 result = gst_caps_can_intersect (allowed, caps);
2410 gst_caps_unref (allowed);
2417 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2423 * gst_pad_accept_caps:
2424 * @pad: a #GstPad to check
2425 * @caps: a #GstCaps to check on the pad
2427 * Check if the given pad accepts the caps.
2429 * Returns: TRUE if the pad can accept the caps.
2432 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2435 GstPadAcceptCapsFunction acceptfunc;
2436 GstCaps *existing = NULL;
2438 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2440 /* any pad can be unnegotiated */
2444 /* lock for checking the existing caps */
2445 GST_OBJECT_LOCK (pad);
2446 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2447 /* The current caps on a pad are trivially acceptable */
2448 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2449 if (caps == existing || gst_caps_is_equal (caps, existing))
2452 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2453 GST_OBJECT_UNLOCK (pad);
2455 if (G_LIKELY (acceptfunc)) {
2456 /* we can call the function */
2457 result = acceptfunc (pad, caps);
2458 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2460 /* Only null if the element explicitly unset it */
2461 result = gst_pad_acceptcaps_default (pad, caps);
2462 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2468 GST_DEBUG_OBJECT (pad, "pad had same caps");
2469 GST_OBJECT_UNLOCK (pad);
2475 * gst_pad_peer_accept_caps:
2476 * @pad: a #GstPad to check the peer of
2477 * @caps: a #GstCaps to check on the pad
2479 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2482 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2485 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2490 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2492 GST_OBJECT_LOCK (pad);
2494 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2496 peerpad = GST_PAD_PEER (pad);
2497 if (G_UNLIKELY (peerpad == NULL))
2500 gst_object_ref (peerpad);
2501 /* release lock before calling external methods but keep ref to pad */
2502 GST_OBJECT_UNLOCK (pad);
2504 result = gst_pad_accept_caps (peerpad, caps);
2506 gst_object_unref (peerpad);
2512 GST_OBJECT_UNLOCK (pad);
2519 * @pad: a #GstPad to set the capabilities of.
2520 * @caps: (transfer none): a #GstCaps to set.
2522 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2523 * caps on the pad will be unreffed. This function refs the caps so you should
2524 * unref if as soon as you don't need it anymore.
2525 * It is possible to set NULL caps, which will make the pad unnegotiated
2528 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2529 * or bad parameters were provided to this function.
2534 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2536 GstPadSetCapsFunction setcaps;
2539 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2540 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2542 GST_OBJECT_LOCK (pad);
2543 existing = GST_PAD_CAPS (pad);
2544 if (existing == caps)
2547 if (gst_caps_is_equal (caps, existing))
2548 goto setting_same_caps;
2550 setcaps = GST_PAD_SETCAPSFUNC (pad);
2552 /* call setcaps function to configure the pad only if the
2553 * caps is not NULL */
2554 if (setcaps != NULL && caps) {
2555 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2556 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2557 GST_OBJECT_UNLOCK (pad);
2558 if (!setcaps (pad, caps))
2560 GST_OBJECT_LOCK (pad);
2561 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2563 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2567 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2568 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2570 GST_OBJECT_UNLOCK (pad);
2572 #if GLIB_CHECK_VERSION(2,26,0)
2573 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2575 g_object_notify ((GObject *) pad, "caps");
2582 GST_OBJECT_UNLOCK (pad);
2587 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2588 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2589 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2591 GST_OBJECT_UNLOCK (pad);
2598 GST_OBJECT_LOCK (pad);
2599 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2600 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2601 "caps %" GST_PTR_FORMAT " could not be set", caps);
2602 GST_OBJECT_UNLOCK (pad);
2610 GstPadEventFunction eventfunc;
2616 context_func (GstEvent * event, ContextData * data)
2618 data->eventfunc (data->pad, gst_event_ref (event));
2619 /* FIXME, update return value when we can */
2622 static GstFlowReturn
2623 gst_pad_update_context (GstPad * pad, GstContext * context)
2627 if (G_UNLIKELY ((data.eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2630 data.ret = GST_FLOW_OK;
2632 gst_context_foreach (context, (GFunc) context_func, &data);
2634 /* set context on pad if all succeeds */
2635 gst_context_replace (&GST_PAD_CONTEXT (pad), context);
2642 g_warning ("pad %s:%s has no event handler, file a bug.",
2643 GST_DEBUG_PAD_NAME (pad));
2644 return GST_FLOW_ERROR;
2649 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2653 /* See if pad accepts the caps */
2654 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2657 /* set caps on pad if call succeeds */
2658 res = gst_pad_set_caps (pad, caps);
2659 /* no need to unref the caps here, set_caps takes a ref and
2660 * our ref goes away when we leave this function. */
2666 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2667 "caps %" GST_PTR_FORMAT " not accepted", caps);
2672 /* returns TRUE if the src pad could be configured to accept the given caps */
2674 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2679 /* See if pad accepts the caps */
2680 if (!gst_pad_accept_caps (pad, caps))
2683 res = gst_pad_set_caps (pad, caps);
2691 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2692 "caps %" GST_PTR_FORMAT " not accepted", caps);
2698 * gst_pad_get_pad_template_caps:
2699 * @pad: a #GstPad to get the template capabilities from.
2701 * Gets the capabilities for @pad's template.
2703 * Returns: (transfer none): the #GstCaps of this pad template. If you intend
2704 * to keep a reference on the caps, make a copy (see gst_caps_copy ()).
2707 gst_pad_get_pad_template_caps (GstPad * pad)
2709 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2711 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2713 if (GST_PAD_PAD_TEMPLATE (pad))
2714 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2716 return gst_static_caps_get (&anycaps);
2721 * @pad: a #GstPad to get the peer of.
2723 * Gets the peer of @pad. This function refs the peer pad so
2724 * you need to unref it after use.
2726 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2731 gst_pad_get_peer (GstPad * pad)
2735 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2737 GST_OBJECT_LOCK (pad);
2738 result = GST_PAD_PEER (pad);
2740 gst_object_ref (result);
2741 GST_OBJECT_UNLOCK (pad);
2747 * gst_pad_get_allowed_caps:
2750 * Gets the capabilities of the allowed media types that can flow through
2751 * @pad and its peer.
2753 * The allowed capabilities is calculated as the intersection of the results of
2754 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2755 * on the resulting caps.
2757 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2758 * caps when you no longer need it. This function returns NULL when @pad
2764 gst_pad_get_allowed_caps (GstPad * pad)
2771 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2773 GST_OBJECT_LOCK (pad);
2775 peer = GST_PAD_PEER (pad);
2776 if (G_UNLIKELY (peer == NULL))
2779 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2781 gst_object_ref (peer);
2782 GST_OBJECT_UNLOCK (pad);
2783 mycaps = gst_pad_get_caps (pad);
2785 peercaps = gst_pad_get_caps (peer);
2786 gst_object_unref (peer);
2788 caps = gst_caps_intersect (mycaps, peercaps);
2789 gst_caps_unref (peercaps);
2790 gst_caps_unref (mycaps);
2792 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2799 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2800 GST_OBJECT_UNLOCK (pad);
2807 * gst_pad_get_negotiated_caps:
2810 * Gets the capabilities of the media type that currently flows through @pad
2813 * This function can be used on both src and sinkpads. Note that srcpads are
2814 * always negotiated before sinkpads so it is possible that the negotiated caps
2815 * on the srcpad do not match the negotiated caps of the peer.
2817 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2818 * the caps when you no longer need it. This function returns NULL when
2819 * the @pad has no peer or is not negotiated yet.
2824 gst_pad_get_negotiated_caps (GstPad * pad)
2829 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2831 GST_OBJECT_LOCK (pad);
2833 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2836 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2838 caps = GST_PAD_CAPS (pad);
2840 gst_caps_ref (caps);
2841 GST_OBJECT_UNLOCK (pad);
2843 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2850 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2851 GST_OBJECT_UNLOCK (pad);
2857 static GstIteratorItem
2858 iterate_pad (GstIterator * it, GstPad * pad)
2860 gst_object_ref (pad);
2861 return GST_ITERATOR_ITEM_PASS;
2865 * gst_pad_iterate_internal_links_default:
2866 * @pad: the #GstPad to get the internal links of.
2868 * Iterate the list of pads to which the given pad is linked to inside of
2869 * the parent element.
2870 * This is the default handler, and thus returns an iterator of all of the
2871 * pads inside the parent element with opposite direction.
2873 * The caller must free this iterator after use with gst_iterator_free().
2875 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2876 * returned pad with gst_object_unref().
2881 gst_pad_iterate_internal_links_default (GstPad * pad)
2888 GstIteratorDisposeFunction dispose;
2890 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2895 GST_OBJECT_LOCK (pad);
2896 parent = GST_PAD_PARENT (pad);
2897 if (!parent || !GST_IS_ELEMENT (parent))
2900 gst_object_ref (parent);
2901 GST_OBJECT_UNLOCK (pad);
2903 if (pad->direction == GST_PAD_SRC)
2904 padlist = &parent->sinkpads;
2906 padlist = &parent->srcpads;
2908 GST_DEBUG_OBJECT (pad, "Making iterator");
2910 cookie = &parent->pads_cookie;
2912 dispose = (GstIteratorDisposeFunction) gst_object_unref;
2913 lock = GST_OBJECT_GET_LOCK (parent);
2916 res = gst_iterator_new_list (GST_TYPE_PAD,
2917 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
2925 GST_OBJECT_UNLOCK (pad);
2926 GST_DEBUG_OBJECT (pad, "no parent element");
2932 * gst_pad_iterate_internal_links:
2933 * @pad: the GstPad to get the internal links of.
2935 * Gets an iterator for the pads to which the given pad is linked to inside
2936 * of the parent element.
2938 * Each #GstPad element yielded by the iterator will have its refcount increased,
2939 * so unref after use.
2941 * Free-function: gst_iterator_free
2943 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2944 * pad does not have an iterator function configured. Use
2945 * gst_iterator_free() after usage.
2950 gst_pad_iterate_internal_links (GstPad * pad)
2952 GstIterator *res = NULL;
2954 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2956 if (GST_PAD_ITERINTLINKFUNC (pad))
2957 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
2963 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
2965 gboolean result = FALSE;
2967 gboolean done = FALSE;
2970 GList *pushed_pads = NULL;
2972 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
2973 event, GST_EVENT_TYPE_NAME (event));
2975 iter = gst_pad_iterate_internal_links (pad);
2981 switch (gst_iterator_next (iter, &item)) {
2982 case GST_ITERATOR_OK:
2983 eventpad = GST_PAD_CAST (item);
2985 /* if already pushed, skip */
2986 if (g_list_find (pushed_pads, eventpad)) {
2987 gst_object_unref (item);
2991 if (GST_PAD_IS_SRC (eventpad)) {
2992 /* for each pad we send to, we should ref the event; it's up
2993 * to downstream to unref again when handled. */
2994 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
2995 event, GST_EVENT_TYPE_NAME (event),
2996 GST_DEBUG_PAD_NAME (eventpad));
2997 gst_event_ref (event);
2998 result |= gst_pad_push_event (eventpad, event);
3000 /* we only send the event on one pad, multi-sinkpad elements
3001 * should implement a handler */
3002 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3003 event, GST_EVENT_TYPE_NAME (event),
3004 GST_DEBUG_PAD_NAME (eventpad));
3005 result = gst_pad_push_event (eventpad, event);
3010 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3012 gst_object_unref (item);
3014 case GST_ITERATOR_RESYNC:
3015 /* We don't reset the result here because we don't push the event
3016 * again on pads that got the event already and because we need
3017 * to consider the result of the previous pushes */
3018 gst_iterator_resync (iter);
3020 case GST_ITERATOR_ERROR:
3021 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3024 case GST_ITERATOR_DONE:
3029 gst_iterator_free (iter);
3033 /* If this is a sinkpad and we don't have pads to send the event to, we
3034 * return TRUE. This is so that when using the default handler on a sink
3035 * element, we don't fail to push it. */
3037 result = GST_PAD_IS_SINK (pad);
3039 g_list_free (pushed_pads);
3041 /* we handled the incoming event so we unref once */
3043 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3044 gst_event_unref (event);
3051 * gst_pad_event_default:
3052 * @pad: a #GstPad to call the default event handler on.
3053 * @event: (transfer full): the #GstEvent to handle.
3055 * Invokes the default event handler for the given pad. End-of-stream and
3056 * discontinuity events are handled specially, and then the event is sent to all
3057 * pads internally linked to @pad. Note that if there are many possible sink
3058 * pads that are internally linked to @pad, only one will be sent an event.
3059 * Multi-sinkpad elements should implement custom event handlers.
3061 * Returns: TRUE if the event was sent succesfully.
3064 gst_pad_event_default (GstPad * pad, GstEvent * event)
3066 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3067 g_return_val_if_fail (event != NULL, FALSE);
3069 GST_LOG_OBJECT (pad, "default event handler");
3071 switch (GST_EVENT_TYPE (event)) {
3074 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3075 gst_pad_pause_task (pad);
3082 return gst_pad_event_default_dispatch (pad, event);
3086 * gst_pad_dispatcher:
3087 * @pad: a #GstPad to dispatch.
3088 * @dispatch: the #GstPadDispatcherFunction to call.
3089 * @data: (closure): gpointer user data passed to the dispatcher function.
3091 * Invokes the given dispatcher function on each respective peer of
3092 * all pads that are internally linked to the given pad.
3093 * The GstPadDispatcherFunction should return TRUE when no further pads
3094 * need to be processed.
3096 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3099 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3102 gboolean res = FALSE;
3103 GstIterator *iter = NULL;
3104 gboolean done = FALSE;
3107 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3108 g_return_val_if_fail (dispatch != NULL, FALSE);
3110 iter = gst_pad_iterate_internal_links (pad);
3116 switch (gst_iterator_next (iter, &item)) {
3117 case GST_ITERATOR_OK:
3119 GstPad *int_pad = GST_PAD_CAST (item);
3120 GstPad *int_peer = gst_pad_get_peer (int_pad);
3123 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3124 GST_DEBUG_PAD_NAME (int_peer));
3125 done = res = dispatch (int_peer, data);
3126 gst_object_unref (int_peer);
3128 GST_DEBUG_OBJECT (int_pad, "no peer");
3131 gst_object_unref (item);
3133 case GST_ITERATOR_RESYNC:
3134 gst_iterator_resync (iter);
3136 case GST_ITERATOR_ERROR:
3138 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3140 case GST_ITERATOR_DONE:
3145 gst_iterator_free (iter);
3147 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3156 * @pad: a #GstPad to invoke the default query on.
3157 * @query: (transfer none): the #GstQuery to perform.
3159 * Dispatches a query to a pad. The query should have been allocated by the
3160 * caller via one of the type-specific allocation functions. The element that
3161 * the pad belongs to is responsible for filling the query with an appropriate
3162 * response, which should then be parsed with a type-specific query parsing
3165 * Again, the caller is responsible for both the allocation and deallocation of
3166 * the query structure.
3168 * Please also note that some queries might need a running pipeline to work.
3170 * Returns: TRUE if the query could be performed.
3173 gst_pad_query (GstPad * pad, GstQuery * query)
3175 GstPadQueryFunction func;
3177 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3178 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3180 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3182 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3185 return func (pad, query);
3189 GST_DEBUG_OBJECT (pad, "had no query function");
3195 * gst_pad_peer_query:
3196 * @pad: a #GstPad to invoke the peer query on.
3197 * @query: (transfer none): the #GstQuery to perform.
3199 * Performs gst_pad_query() on the peer of @pad.
3201 * The caller is responsible for both the allocation and deallocation of
3202 * the query structure.
3204 * Returns: TRUE if the query could be performed. This function returns %FALSE
3205 * if @pad has no peer.
3210 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3215 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3216 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3218 GST_OBJECT_LOCK (pad);
3220 GST_DEBUG_OBJECT (pad, "peer query");
3222 peerpad = GST_PAD_PEER (pad);
3223 if (G_UNLIKELY (peerpad == NULL))
3226 gst_object_ref (peerpad);
3227 GST_OBJECT_UNLOCK (pad);
3229 result = gst_pad_query (peerpad, query);
3231 gst_object_unref (peerpad);
3238 GST_WARNING_OBJECT (pad, "pad has no peer");
3239 GST_OBJECT_UNLOCK (pad);
3245 * gst_pad_query_default:
3246 * @pad: a #GstPad to call the default query handler on.
3247 * @query: (transfer none): the #GstQuery to handle.
3249 * Invokes the default query handler for the given pad.
3250 * The query is sent to all pads internally linked to @pad. Note that
3251 * if there are many possible sink pads that are internally linked to
3252 * @pad, only one will be sent the query.
3253 * Multi-sinkpad elements should implement custom query handlers.
3255 * Returns: TRUE if the query was performed succesfully.
3258 gst_pad_query_default (GstPad * pad, GstQuery * query)
3260 switch (GST_QUERY_TYPE (query)) {
3261 case GST_QUERY_POSITION:
3262 case GST_QUERY_SEEKING:
3263 case GST_QUERY_FORMATS:
3264 case GST_QUERY_LATENCY:
3265 case GST_QUERY_JITTER:
3266 case GST_QUERY_RATE:
3267 case GST_QUERY_CONVERT:
3269 return gst_pad_dispatcher
3270 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3275 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3276 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3279 * This function performs the pad blocking when an event, buffer push
3280 * is performed on a _SRC_ pad. It blocks the streaming thread after
3281 * informing the pad has been blocked.
3283 * An application can with this method wait and block any streaming
3284 * thread and perform operations such as seeking or linking.
3286 * Two methods are available for notifying the application of the
3288 * - the callback method, which happens in the STREAMING thread with
3289 * the STREAM_LOCK held. With this method, the most useful way of
3290 * dealing with the callback is to post a message to the main thread
3291 * where the pad block can then be handled outside of the streaming
3292 * thread. With the last method one can perform all operations such
3293 * as doing a state change, linking, unblocking, seeking etc on the
3295 * - the GCond signal method, which makes any thread unblock when
3296 * the pad block happens.
3298 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3299 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3303 static GstFlowReturn
3304 handle_pad_block (GstPad * pad)
3306 GstPadBlockCallback callback;
3308 GstFlowReturn ret = GST_FLOW_OK;
3310 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3312 /* flushing, don't bother trying to block and return WRONG_STATE
3314 if (GST_PAD_IS_FLUSHING (pad))
3315 goto flushingnonref;
3317 /* we grab an extra ref for the callbacks, this is probably not
3318 * needed (callback code does not have a ref and cannot unref). I
3319 * think this was done to make it possible to unref the element in
3320 * the callback, which is in the end totally impossible as it
3321 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3322 * all taken when calling this function. */
3323 gst_object_ref (pad);
3325 while (GST_PAD_IS_BLOCKED (pad)) {
3327 /* we either have a callback installed to notify the block or
3328 * some other thread is doing a GCond wait. */
3329 callback = pad->block_callback;
3330 pad->block_callback_called = TRUE;
3332 /* there is a callback installed, call it. We release the
3333 * lock so that the callback can do something usefull with the
3335 user_data = pad->block_data;
3336 GST_OBJECT_UNLOCK (pad);
3337 callback (pad, TRUE, user_data);
3338 GST_OBJECT_LOCK (pad);
3340 /* we released the lock, recheck flushing */
3341 if (GST_PAD_IS_FLUSHING (pad))
3344 /* no callback, signal the thread that is doing a GCond wait
3346 GST_PAD_BLOCK_BROADCAST (pad);
3348 } while (pad->block_callback_called == FALSE && GST_PAD_IS_BLOCKED (pad));
3350 /* OBJECT_LOCK could have been released when we did the callback, which
3351 * then could have made the pad unblock so we need to check the blocking
3352 * condition again. */
3353 if (!GST_PAD_IS_BLOCKED (pad))
3356 /* now we block the streaming thread. It can be unlocked when we
3357 * deactivate the pad (which will also set the FLUSHING flag) or
3358 * when the pad is unblocked. A flushing event will also unblock
3359 * the pad after setting the FLUSHING flag. */
3360 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3361 "Waiting to be unblocked or set flushing");
3362 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3363 GST_PAD_BLOCK_WAIT (pad);
3364 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3366 /* see if we got unblocked by a flush or not */
3367 if (GST_PAD_IS_FLUSHING (pad))
3371 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3373 /* when we get here, the pad is unblocked again and we perform
3374 * the needed unblock code. */
3375 callback = pad->block_callback;
3377 /* we need to call the callback */
3378 user_data = pad->block_data;
3379 GST_OBJECT_UNLOCK (pad);
3380 callback (pad, FALSE, user_data);
3381 GST_OBJECT_LOCK (pad);
3383 /* we need to signal the thread waiting on the GCond */
3384 GST_PAD_BLOCK_BROADCAST (pad);
3387 gst_object_unref (pad);
3393 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3394 return GST_FLOW_WRONG_STATE;
3398 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3399 gst_object_unref (pad);
3400 return GST_FLOW_WRONG_STATE;
3404 /**********************************************************************
3405 * Data passing functions
3409 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3412 GValue args[2] = { {0}, {0} };
3417 g_value_init (&ret, G_TYPE_BOOLEAN);
3418 g_value_set_boolean (&ret, TRUE);
3419 g_value_init (&args[0], GST_TYPE_PAD);
3420 g_value_set_object (&args[0], pad);
3421 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3422 g_value_set_boxed (&args[1], obj);
3424 if (GST_IS_EVENT (obj))
3425 detail = event_quark;
3427 detail = buffer_quark;
3430 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3431 res = g_value_get_boolean (&ret);
3434 g_value_unset (&ret);
3435 g_value_unset (&args[0]);
3436 g_value_unset (&args[1]);
3442 gst_pad_data_unref (gboolean is_buffer, void *data)
3444 if (G_LIKELY (is_buffer)) {
3445 gst_buffer_unref (data);
3447 gst_buffer_list_unref (data);
3452 gst_pad_data_get_caps (gboolean is_buffer, void *data)
3456 if (G_LIKELY (is_buffer)) {
3457 caps = GST_BUFFER_CAPS (data);
3461 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0)))
3462 caps = GST_BUFFER_CAPS (buf);
3469 /* this is the chain function that does not perform the additional argument
3470 * checking for that little extra speed.
3472 static inline GstFlowReturn
3473 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data,
3474 GstContext * context, GstPadPushCache * cache)
3477 gboolean caps_changed, context_changed;
3479 gboolean emit_signal;
3481 GST_PAD_STREAM_LOCK (pad);
3483 GST_OBJECT_LOCK (pad);
3484 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3487 caps = gst_pad_data_get_caps (is_buffer, data);
3488 caps_changed = caps && caps != GST_PAD_CAPS (pad);
3490 context_changed = context && context != GST_PAD_CONTEXT (pad);
3492 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3494 GST_OBJECT_UNLOCK (pad);
3496 /* see if the signal should be emited, we emit before caps nego as
3497 * we might drop the buffer and do capsnego for nothing. */
3498 if (G_UNLIKELY (emit_signal)) {
3500 if (G_LIKELY (is_buffer)) {
3501 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3504 /* chain all groups in the buffer list one by one to avoid problems with
3505 * buffer probes that push buffers or events */
3510 if (G_UNLIKELY (context_changed)) {
3511 GST_DEBUG_OBJECT (pad, "context changed to %p", context);
3512 ret = gst_pad_update_context (pad, context);
3513 if (G_UNLIKELY (ret != GST_FLOW_OK))
3518 gst_context_unref (context);
3520 if (G_UNLIKELY (caps_changed)) {
3521 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
3522 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
3523 goto not_negotiated;
3526 /* NOTE: we read the chainfunc unlocked.
3527 * we cannot hold the lock for the pad so we might send
3528 * the data to the wrong function. This is not really a
3529 * problem since functions are assigned at creation time
3530 * and don't change that often... */
3531 if (G_LIKELY (is_buffer)) {
3532 GstPadChainFunction chainfunc;
3534 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3537 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3538 "calling chainfunction &%s with buffer %p",
3539 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3542 cache->peer = gst_object_ref (pad);
3543 cache->caps = caps ? gst_caps_ref (caps) : NULL;
3546 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3548 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3549 "called chainfunction &%s with buffer %p, returned %s",
3550 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3552 GstPadChainListFunction chainlistfunc;
3554 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3557 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3558 "calling chainlistfunction &%s",
3559 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3561 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3563 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3564 "called chainlistfunction &%s, returned %s",
3565 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3568 GST_PAD_STREAM_UNLOCK (pad);
3574 GstBufferList *list;
3578 GST_PAD_STREAM_UNLOCK (pad);
3580 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3582 list = GST_BUFFER_LIST_CAST (data);
3583 len = gst_buffer_list_len (list);
3586 for (i = 0; i < len; i++) {
3587 buffer = gst_buffer_list_get (list, i);
3589 gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_ref (buffer),
3591 if (ret != GST_FLOW_OK)
3594 gst_buffer_list_unref (list);
3602 gst_pad_data_unref (is_buffer, data);
3603 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3604 "pushing, but pad was flushing");
3605 GST_OBJECT_UNLOCK (pad);
3606 GST_PAD_STREAM_UNLOCK (pad);
3607 return GST_FLOW_WRONG_STATE;
3611 gst_pad_data_unref (is_buffer, data);
3612 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3613 GST_PAD_STREAM_UNLOCK (pad);
3618 gst_pad_data_unref (is_buffer, data);
3619 gst_context_unref (context);
3620 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "context was not accepted");
3621 GST_PAD_STREAM_UNLOCK (pad);
3626 gst_pad_data_unref (is_buffer, data);
3627 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3628 "pushing data but pad did not accept");
3629 GST_PAD_STREAM_UNLOCK (pad);
3630 return GST_FLOW_NOT_NEGOTIATED;
3634 gst_pad_data_unref (is_buffer, data);
3635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3636 "pushing, but not chainhandler");
3637 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3638 ("push on pad %s:%s but it has no chainfunction",
3639 GST_DEBUG_PAD_NAME (pad)));
3640 GST_PAD_STREAM_UNLOCK (pad);
3641 return GST_FLOW_NOT_SUPPORTED;
3647 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3648 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3651 * Chain a buffer to @pad.
3653 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3655 * If the caps on @buffer are different from the current caps on @pad, this
3656 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3657 * installed on @pad. If the new caps are not acceptable for @pad, this
3658 * function returns #GST_FLOW_NOT_NEGOTIATED.
3660 * The function proceeds calling the chain function installed on @pad (see
3661 * gst_pad_set_chain_function()) and the return value of that function is
3662 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3665 * In all cases, success or failure, the caller loses its reference to @buffer
3666 * after calling this function.
3668 * Returns: a #GstFlowReturn from the pad.
3673 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3675 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3676 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3677 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3679 return gst_pad_chain_data_unchecked (pad, TRUE, buffer, NULL, NULL);
3683 * gst_pad_chain_list:
3684 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3685 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3688 * Chain a bufferlist to @pad.
3690 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3692 * If the caps on the first buffer of @list are different from the current
3693 * caps on @pad, this function will call any setcaps function
3694 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3695 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3697 * The function proceeds calling the chainlist function installed on @pad (see
3698 * gst_pad_set_chain_list_function()) and the return value of that function is
3699 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3700 * chainlist function.
3702 * In all cases, success or failure, the caller loses its reference to @list
3703 * after calling this function.
3707 * Returns: a #GstFlowReturn from the pad.
3712 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3714 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3715 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3716 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3718 return gst_pad_chain_data_unchecked (pad, FALSE, list, NULL, NULL);
3721 static GstFlowReturn
3722 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data,
3723 GstPadPushCache * cache)
3728 GstContext *context;
3729 gboolean caps_changed;
3731 GST_OBJECT_LOCK (pad);
3733 /* FIXME: this check can go away; pad_set_blocked could be implemented with
3734 * probes completely or probes with an extended pad block. */
3735 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3736 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3739 /* we emit signals on the pad arg, the peer will have a chance to
3740 * emit in the _chain() function */
3741 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
3743 /* unlock before emitting */
3744 GST_OBJECT_UNLOCK (pad);
3746 if (G_LIKELY (is_buffer)) {
3747 /* if the signal handler returned FALSE, it means we should just drop the
3749 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3752 /* push all buffers in the list */
3755 GST_OBJECT_LOCK (pad);
3758 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3761 /* Before pushing the buffer to the peer pad, ensure that caps
3762 * are set on this pad */
3763 caps = gst_pad_data_get_caps (is_buffer, data);
3764 caps_changed = caps && caps != GST_PAD_CAPS (pad);
3766 context = GST_PAD_CONTEXT (pad);
3767 if (G_LIKELY (context))
3768 gst_context_ref (context);
3770 /* take ref to peer pad before releasing the lock */
3771 gst_object_ref (peer);
3772 GST_OBJECT_UNLOCK (pad);
3774 /* we got a new datatype from the pad, it had better handle it */
3775 if (G_UNLIKELY (caps_changed)) {
3776 GST_DEBUG_OBJECT (pad,
3777 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3778 GST_PAD_CAPS (pad), caps, caps);
3779 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
3780 goto not_negotiated;
3783 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data, context, cache);
3785 gst_object_unref (peer);
3791 GstBufferList *list;
3795 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
3797 list = GST_BUFFER_LIST_CAST (data);
3798 len = gst_buffer_list_len (list);
3800 for (i = 0; i < len; i++) {
3801 buffer = gst_buffer_list_get (list, i);
3802 ret = gst_pad_push_data (pad, TRUE, gst_buffer_ref (buffer), NULL);
3803 if (ret != GST_FLOW_OK)
3806 gst_buffer_list_unref (list);
3811 /* ERROR recovery here */
3814 gst_pad_data_unref (is_buffer, data);
3815 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
3816 GST_OBJECT_UNLOCK (pad);
3821 gst_pad_data_unref (is_buffer, data);
3822 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3827 gst_pad_data_unref (is_buffer, data);
3828 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3829 "pushing, but it was not linked");
3830 GST_OBJECT_UNLOCK (pad);
3831 return GST_FLOW_NOT_LINKED;
3835 gst_pad_data_unref (is_buffer, data);
3836 gst_object_unref (peer);
3837 if (G_LIKELY (context))
3838 gst_context_unref (context);
3839 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
3840 "element pushed data then refused to accept the caps");
3841 return GST_FLOW_NOT_NEGOTIATED;
3845 static inline GstPadPushCache *
3846 pad_take_cache (GstPad * pad, gpointer * cache_ptr)
3848 GstPadPushCache *cache;
3850 /* try to get the cached data */
3852 cache = g_atomic_pointer_get (cache_ptr);
3853 /* now try to replace the pointer with NULL to mark that we are busy
3855 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache, NULL));
3857 /* we could have a leftover invalid entry */
3858 if (G_UNLIKELY (cache == PAD_CACHE_INVALID))
3865 pad_free_cache (GstPadPushCache * cache)
3867 gst_object_unref (cache->peer);
3869 gst_caps_unref (cache->caps);
3870 g_slice_free (GstPadPushCache, cache);
3874 pad_put_cache (GstPad * pad, GstPadPushCache * cache, gpointer * cache_ptr)
3877 if (!g_atomic_pointer_compare_and_exchange (cache_ptr, NULL, cache)) {
3878 /* something changed, clean up our cache */
3879 pad_free_cache (cache);
3883 /* must be called with the pad lock */
3885 _priv_gst_pad_invalidate_cache (GstPad * pad)
3887 GstPadPushCache *cache;
3888 gpointer *cache_ptr;
3890 GST_LOG_OBJECT (pad, "Invalidating pad cache");
3892 /* we hold the pad lock here so we can get the peer and it stays
3893 * alive during this call */
3894 if (GST_PAD_IS_SINK (pad)) {
3895 if (!(pad = GST_PAD_PEER (pad)))
3899 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
3901 /* try to get the cached data */
3903 cache = g_atomic_pointer_get (cache_ptr);
3904 /* now try to replace the pointer with INVALID. If nothing is busy with this
3905 * caps, we get the cache and clean it up. If something is busy, we replace
3906 * with INVALID so that when the function finishes and tries to put the
3907 * cache back, it'll fail and cleanup */
3908 } while (!g_atomic_pointer_compare_and_exchange (cache_ptr, cache,
3909 PAD_CACHE_INVALID));
3911 if (G_LIKELY (cache && cache != PAD_CACHE_INVALID))
3912 pad_free_cache (cache);
3917 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3918 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3921 * Pushes a buffer to the peer of @pad.
3923 * This function will call an installed pad block before triggering any
3924 * installed pad probes.
3926 * If the caps on @buffer are different from the currently configured caps on
3927 * @pad, this function will call any installed setcaps function on @pad (see
3928 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
3929 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
3931 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3932 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3935 * In all cases, success or failure, the caller loses its reference to @buffer
3936 * after calling this function.
3938 * Returns: a #GstFlowReturn from the peer pad.
3943 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3945 GstPadPushCache *cache;
3947 gpointer *cache_ptr;
3951 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3952 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3953 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3955 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
3957 cache = pad_take_cache (pad, cache_ptr);
3959 if (G_UNLIKELY (cache == NULL))
3963 caps = GST_BUFFER_CAPS (buffer);
3964 if (G_UNLIKELY (caps && caps != cache->caps)) {
3965 pad_free_cache (cache);
3971 GST_PAD_STREAM_LOCK (peer);
3972 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
3975 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3976 "calling chainfunction &%s with buffer %p",
3977 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer);
3979 ret = GST_PAD_CHAINFUNC (peer) (peer, buffer);
3981 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3982 "called chainfunction &%s with buffer %p, returned %s",
3983 GST_DEBUG_FUNCPTR_NAME (GST_PAD_CHAINFUNC (peer)), buffer,
3984 gst_flow_get_name (ret));
3986 GST_PAD_STREAM_UNLOCK (peer);
3988 pad_put_cache (pad, cache, cache_ptr);
3995 GstPadPushCache scache = { NULL, };
3997 GST_LOG_OBJECT (pad, "Taking slow path");
3999 ret = gst_pad_push_data (pad, TRUE, buffer, &scache);
4002 GstPadPushCache *ncache;
4004 GST_LOG_OBJECT (pad, "Caching push data");
4006 /* make cache structure */
4007 ncache = g_slice_new (GstPadPushCache);
4010 pad_put_cache (pad, ncache, cache_ptr);
4016 GST_PAD_STREAM_UNLOCK (peer);
4017 pad_free_cache (cache);
4023 * gst_pad_push_list:
4024 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4025 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4028 * Pushes a buffer list to the peer of @pad.
4030 * This function will call an installed pad block before triggering any
4031 * installed pad probes.
4033 * If the caps on the first buffer in the first group of @list are different
4034 * from the currently configured caps on @pad, this function will call any
4035 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4036 * case of failure to renegotiate the new format, this function returns
4037 * #GST_FLOW_NOT_NEGOTIATED.
4039 * If there are any probes installed on @pad every group of the buffer list
4040 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4041 * buffer list will be unreffed.
4043 * The function proceeds calling the chain function on the peer pad and returns
4044 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4045 * be returned. If the peer pad does not have any installed chainlist function
4046 * every group buffer of the list will be merged into a normal #GstBuffer and
4047 * chained via gst_pad_chain().
4049 * In all cases, success or failure, the caller loses its reference to @list
4050 * after calling this function.
4052 * Returns: a #GstFlowReturn from the peer pad.
4059 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4062 GstPadPushCache *cache;
4064 gpointer *cache_ptr;
4068 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4069 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4070 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4072 cache_ptr = (gpointer *) & pad->priv->cache_ptr;
4074 cache = pad_take_cache (pad, cache_ptr);
4076 if (G_UNLIKELY (cache == NULL))
4080 if ((buf = gst_buffer_list_get (list, 0)))
4081 caps = GST_BUFFER_CAPS (buf);
4085 if (G_UNLIKELY (caps && caps != cache->caps)) {
4086 pad_free_cache (cache);
4092 GST_PAD_STREAM_LOCK (peer);
4093 if (G_UNLIKELY (g_atomic_pointer_get (cache_ptr) == PAD_CACHE_INVALID))
4096 ret = GST_PAD_CHAINLISTFUNC (peer) (peer, list);
4098 GST_PAD_STREAM_UNLOCK (peer);
4100 pad_put_cache (pad, cache, cache_ptr);
4107 GstPadPushCache scache = { NULL, };
4109 GST_LOG_OBJECT (pad, "Taking slow path");
4111 ret = gst_pad_push_data (pad, FALSE, list, &scache);
4114 GstPadPushCache *ncache;
4116 GST_LOG_OBJECT (pad, "Caching push data");
4118 /* make cache structure */
4119 ncache = g_slice_new (GstPadPushCache);
4122 pad_put_cache (pad, ncache, cache_ptr);
4128 GST_PAD_STREAM_UNLOCK (peer);
4129 pad_free_cache (cache);
4135 * gst_pad_check_pull_range:
4136 * @pad: a sink #GstPad.
4138 * Checks if a gst_pad_pull_range() can be performed on the peer
4139 * source pad. This function is used by plugins that want to check
4140 * if they can use random access on the peer source pad.
4142 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4143 * if it needs to perform some logic to determine if pull_range is
4146 * Returns: a gboolean with the result.
4151 gst_pad_check_pull_range (GstPad * pad)
4155 GstPadCheckGetRangeFunction checkgetrangefunc;
4157 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4159 GST_OBJECT_LOCK (pad);
4160 if (!GST_PAD_IS_SINK (pad))
4161 goto wrong_direction;
4163 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4166 gst_object_ref (peer);
4167 GST_OBJECT_UNLOCK (pad);
4169 /* see note in above function */
4170 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4171 /* FIXME, kindoff ghetto */
4172 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4173 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4174 "no checkgetrangefunc, assuming %d", ret);
4176 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4177 "calling checkgetrangefunc %s of peer pad %s:%s",
4178 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4180 ret = checkgetrangefunc (peer);
4183 gst_object_unref (peer);
4187 /* ERROR recovery here */
4190 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4191 "checking pull range, but pad must be a sinkpad");
4192 GST_OBJECT_UNLOCK (pad);
4197 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4198 "checking pull range, but it was not linked");
4199 GST_OBJECT_UNLOCK (pad);
4204 static GstFlowReturn
4205 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4206 GstBuffer ** buffer, GstContext ** context)
4209 GstPadGetRangeFunction getrangefunc;
4210 gboolean emit_signal;
4212 gboolean caps_changed;
4214 GST_PAD_STREAM_LOCK (pad);
4216 GST_OBJECT_LOCK (pad);
4217 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4220 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4221 GST_OBJECT_UNLOCK (pad);
4223 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4226 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4227 "calling getrangefunc %s, offset %"
4228 G_GUINT64_FORMAT ", size %u",
4229 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4231 ret = getrangefunc (pad, offset, size, buffer);
4233 /* can only fire the signal if we have a valid buffer */
4234 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4235 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4240 gst_context_replace (context, GST_PAD_CONTEXT (pad));
4241 GST_PAD_STREAM_UNLOCK (pad);
4243 if (G_UNLIKELY (ret != GST_FLOW_OK))
4244 goto get_range_failed;
4246 GST_OBJECT_LOCK (pad);
4247 /* Before pushing the buffer to the peer pad, ensure that caps
4248 * are set on this pad */
4249 caps = GST_BUFFER_CAPS (*buffer);
4250 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4251 GST_OBJECT_UNLOCK (pad);
4253 if (G_UNLIKELY (caps_changed)) {
4254 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4255 /* this should usually work because the element produced the buffer */
4256 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4257 goto not_negotiated;
4264 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4265 "pulling range, but pad was flushing");
4266 GST_OBJECT_UNLOCK (pad);
4267 GST_PAD_STREAM_UNLOCK (pad);
4268 return GST_FLOW_WRONG_STATE;
4272 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4273 ("pullrange on pad %s:%s but it has no getrangefunction",
4274 GST_DEBUG_PAD_NAME (pad)));
4275 GST_PAD_STREAM_UNLOCK (pad);
4276 return GST_FLOW_NOT_SUPPORTED;
4280 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4281 "Dropping data after FALSE probe return");
4282 GST_PAD_STREAM_UNLOCK (pad);
4283 gst_buffer_unref (*buffer);
4285 return GST_FLOW_UNEXPECTED;
4290 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4291 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4292 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4297 gst_buffer_unref (*buffer);
4299 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4300 "getrange returned buffer of unaccaptable caps");
4301 return GST_FLOW_NOT_NEGOTIATED;
4306 * gst_pad_get_range:
4307 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4308 * @offset: The start offset of the buffer
4309 * @size: The length of the buffer
4310 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4311 * returns #GST_FLOW_ERROR if %NULL.
4313 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4314 * immediatly and @buffer is %NULL.
4316 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4317 * description of a getrange function. If @pad has no getrange function
4318 * installed (see gst_pad_set_getrange_function()) this function returns
4319 * #GST_FLOW_NOT_SUPPORTED.
4321 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4323 * Returns: a #GstFlowReturn from the pad.
4328 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4329 GstBuffer ** buffer)
4331 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4332 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4333 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4335 return gst_pad_get_range_unchecked (pad, offset, size, buffer, NULL);
4339 * gst_pad_pull_range:
4340 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4341 * @offset: The start offset of the buffer
4342 * @size: The length of the buffer
4343 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4344 * GST_FLOW_ERROR if %NULL.
4346 * Pulls a @buffer from the peer pad.
4348 * This function will first trigger the pad block signal if it was
4351 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4352 * function returns the result of gst_pad_get_range() on the peer pad.
4353 * See gst_pad_get_range() for a list of return values and for the
4354 * semantics of the arguments of this function.
4356 * @buffer's caps must either be unset or the same as what is already
4357 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4360 * Returns: a #GstFlowReturn from the peer pad.
4361 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4362 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4363 * @buffer may not be used or freed when any other return value than
4364 * #GST_FLOW_OK is returned.
4369 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4370 GstBuffer ** buffer)
4374 gboolean emit_signal;
4376 GstContext *context = NULL;
4377 gboolean caps_changed, context_changed;
4379 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4380 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4381 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4383 GST_OBJECT_LOCK (pad);
4385 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4386 handle_pad_block (pad);
4388 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4391 /* signal emision for the pad, peer has chance to emit when
4392 * we call _get_range() */
4393 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4395 gst_object_ref (peer);
4396 GST_OBJECT_UNLOCK (pad);
4398 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer, &context);
4400 gst_object_unref (peer);
4402 if (G_UNLIKELY (ret != GST_FLOW_OK))
4403 goto pull_range_failed;
4405 /* can only fire the signal if we have a valid buffer */
4406 if (G_UNLIKELY (emit_signal)) {
4407 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4411 GST_OBJECT_LOCK (pad);
4412 /* Before pushing the buffer to the peer pad, ensure that caps
4413 * are set on this pad */
4414 caps = GST_BUFFER_CAPS (*buffer);
4415 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4417 context_changed = context && context != GST_PAD_CONTEXT (pad);
4418 GST_OBJECT_UNLOCK (pad);
4421 gst_context_unref (context);
4423 if (G_UNLIKELY (context_changed)) {
4424 GST_DEBUG_OBJECT (pad, "context changed to %p", context);
4425 ret = gst_pad_update_context (pad, context);
4426 if (G_UNLIKELY (ret != GST_FLOW_OK))
4430 /* we got a new datatype on the pad, see if it can handle it */
4431 /* we got a new datatype on the pad, see if it can handle it */
4432 if (G_UNLIKELY (caps_changed)) {
4433 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4434 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4435 goto not_negotiated;
4439 /* ERROR recovery here */
4442 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4443 "pulling range, but it was not linked");
4444 GST_OBJECT_UNLOCK (pad);
4445 return GST_FLOW_NOT_LINKED;
4450 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4451 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4452 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4454 gst_context_unref (context);
4459 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4460 "Dropping data after FALSE probe return");
4461 gst_buffer_unref (*buffer);
4464 gst_context_unref (context);
4465 return GST_FLOW_UNEXPECTED;
4469 gst_buffer_unref (*buffer);
4471 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4472 "pullrange returned context that was not accepted");
4477 gst_buffer_unref (*buffer);
4479 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4480 "pullrange returned buffer of different caps");
4481 return GST_FLOW_NOT_NEGOTIATED;
4486 * gst_pad_push_event:
4487 * @pad: a #GstPad to push the event to.
4488 * @event: (transfer full): the #GstEvent to send to the pad.
4490 * Sends the event to the peer of the given pad. This function is
4491 * mainly used by elements to send events to their peer
4494 * This function takes owership of the provided event so you should
4495 * gst_event_ref() it if you want to reuse the event after this call.
4497 * Returns: TRUE if the event was handled.
4502 gst_pad_push_event (GstPad * pad, GstEvent * event)
4507 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4508 g_return_val_if_fail (event != NULL, FALSE);
4509 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4511 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4513 GST_OBJECT_LOCK (pad);
4515 /* Two checks to be made:
4516 * . (un)set the FLUSHING flag for flushing events,
4517 * . handle pad blocking */
4518 switch (GST_EVENT_TYPE (event)) {
4519 case GST_EVENT_FLUSH_START:
4520 _priv_gst_pad_invalidate_cache (pad);
4521 GST_PAD_SET_FLUSHING (pad);
4523 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4524 /* flush start will have set the FLUSHING flag and will then
4525 * unlock all threads doing a GCond wait on the blocking pad. This
4526 * will typically unblock the STREAMING thread blocked on a pad. */
4527 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4528 "doing block signal.");
4529 GST_PAD_BLOCK_BROADCAST (pad);
4533 case GST_EVENT_FLUSH_STOP:
4534 GST_PAD_UNSET_FLUSHING (pad);
4536 /* if we are blocked, flush away the FLUSH_STOP event */
4537 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4538 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4542 case GST_EVENT_RENEGOTIATE:
4543 if (GST_PAD_IS_SINK (pad) && GST_PAD_GETCAPSFUNC (pad) == NULL)
4544 goto drop_renegotiate;
4546 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4547 /* block the event as long as the pad is blocked */
4548 if (handle_pad_block (pad) != GST_FLOW_OK)
4554 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4555 GST_OBJECT_UNLOCK (pad);
4557 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4560 GST_OBJECT_LOCK (pad);
4563 /* store the event on the pad, but only on srcpads */
4564 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4565 if (GST_PAD_CONTEXT (pad))
4566 GST_PAD_CONTEXT (pad) = gst_context_make_writable (GST_PAD_CONTEXT (pad));
4568 GST_PAD_CONTEXT (pad) = gst_context_new ();
4570 gst_context_update (GST_PAD_CONTEXT (pad), event);
4571 _priv_gst_pad_invalidate_cache (pad);
4574 peerpad = GST_PAD_PEER (pad);
4575 if (peerpad == NULL)
4578 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4579 GST_EVENT_TYPE_NAME (event), peerpad);
4580 gst_object_ref (peerpad);
4581 GST_OBJECT_UNLOCK (pad);
4583 result = gst_pad_send_event (peerpad, event);
4585 /* Note: we gave away ownership of the event at this point */
4586 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4588 gst_object_unref (peerpad);
4592 /* ERROR handling */
4595 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4596 gst_event_unref (event);
4601 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4602 gst_event_unref (event);
4603 GST_OBJECT_UNLOCK (pad);
4608 GST_DEBUG_OBJECT (pad,
4609 "Not forwarding event since we're flushing and blocking");
4610 gst_event_unref (event);
4611 GST_OBJECT_UNLOCK (pad);
4617 * gst_pad_send_event:
4618 * @pad: a #GstPad to send the event to.
4619 * @event: (transfer full): the #GstEvent to send to the pad.
4621 * Sends the event to the pad. This function can be used
4622 * by applications to send events in the pipeline.
4624 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4625 * sink pad, @event should be a downstream event. For example, you would not
4626 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4627 * Furthermore, some downstream events have to be serialized with data flow,
4628 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4629 * the event needs to be serialized with data flow, this function will take the
4630 * pad's stream lock while calling its event function.
4632 * To find out whether an event type is upstream, downstream, or downstream and
4633 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4634 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4635 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4636 * plugin doesn't need to bother itself with this information; the core handles
4637 * all necessary locks and checks.
4639 * This function takes owership of the provided event so you should
4640 * gst_event_ref() it if you want to reuse the event after this call.
4642 * Returns: TRUE if the event was handled.
4645 gst_pad_send_event (GstPad * pad, GstEvent * event)
4647 gboolean result = FALSE;
4648 GstPadEventFunction eventfunc;
4649 gboolean serialized, need_unlock = FALSE;
4651 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4652 g_return_val_if_fail (event != NULL, FALSE);
4654 GST_OBJECT_LOCK (pad);
4655 if (GST_PAD_IS_SINK (pad)) {
4656 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4657 goto wrong_direction;
4658 serialized = GST_EVENT_IS_SERIALIZED (event);
4659 } else if (GST_PAD_IS_SRC (pad)) {
4660 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4661 goto wrong_direction;
4662 /* events on srcpad never are serialized */
4665 goto unknown_direction;
4668 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4669 GST_OBJECT_UNLOCK (pad);
4671 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4674 GST_OBJECT_LOCK (pad);
4677 switch (GST_EVENT_TYPE (event)) {
4678 case GST_EVENT_FLUSH_START:
4679 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4680 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4682 /* can't even accept a flush begin event when flushing */
4683 if (GST_PAD_IS_FLUSHING (pad))
4686 _priv_gst_pad_invalidate_cache (pad);
4687 GST_PAD_SET_FLUSHING (pad);
4688 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4690 case GST_EVENT_FLUSH_STOP:
4691 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4692 GST_PAD_UNSET_FLUSHING (pad);
4693 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4695 GST_OBJECT_UNLOCK (pad);
4696 /* grab stream lock */
4697 GST_PAD_STREAM_LOCK (pad);
4699 GST_OBJECT_LOCK (pad);
4702 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4703 GST_EVENT_TYPE_NAME (event));
4705 /* make this a little faster, no point in grabbing the lock
4706 * if the pad is allready flushing. */
4707 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4711 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4712 GST_OBJECT_UNLOCK (pad);
4713 GST_PAD_STREAM_LOCK (pad);
4715 GST_OBJECT_LOCK (pad);
4716 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4721 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4724 GST_OBJECT_UNLOCK (pad);
4726 result = eventfunc (pad, event);
4729 GST_PAD_STREAM_UNLOCK (pad);
4731 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4735 /* ERROR handling */
4738 g_warning ("pad %s:%s sending %s event in wrong direction",
4739 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4740 GST_OBJECT_UNLOCK (pad);
4741 gst_event_unref (event);
4746 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4747 GST_OBJECT_UNLOCK (pad);
4748 gst_event_unref (event);
4753 g_warning ("pad %s:%s has no event handler, file a bug.",
4754 GST_DEBUG_PAD_NAME (pad));
4755 GST_OBJECT_UNLOCK (pad);
4757 GST_PAD_STREAM_UNLOCK (pad);
4758 gst_event_unref (event);
4763 GST_OBJECT_UNLOCK (pad);
4765 GST_PAD_STREAM_UNLOCK (pad);
4766 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4767 "Received event on flushing pad. Discarding");
4768 gst_event_unref (event);
4773 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4774 gst_event_unref (event);
4780 * gst_pad_set_element_private:
4781 * @pad: the #GstPad to set the private data of.
4782 * @priv: The private data to attach to the pad.
4784 * Set the given private data gpointer on the pad.
4785 * This function can only be used by the element that owns the pad.
4786 * No locking is performed in this function.
4789 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4791 pad->element_private = priv;
4795 * gst_pad_get_element_private:
4796 * @pad: the #GstPad to get the private data of.
4798 * Gets the private data of a pad.
4799 * No locking is performed in this function.
4801 * Returns: (transfer none): a #gpointer to the private data.
4804 gst_pad_get_element_private (GstPad * pad)
4806 return pad->element_private;
4810 do_stream_status (GstPad * pad, GstStreamStatusType type,
4811 GThread * thread, GstTask * task)
4815 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4817 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4818 if (GST_IS_ELEMENT (parent)) {
4819 GstMessage *message;
4820 GValue value = { 0 };
4822 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4823 gchar *tname, *ename, *pname;
4825 /* create a good task name */
4826 ename = gst_element_get_name (parent);
4827 pname = gst_pad_get_name (pad);
4828 tname = g_strdup_printf ("%s:%s", ename, pname);
4832 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4836 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4839 g_value_init (&value, GST_TYPE_TASK);
4840 g_value_set_object (&value, task);
4841 gst_message_set_stream_status_object (message, &value);
4842 g_value_unset (&value);
4844 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4845 gst_element_post_message (parent, message);
4847 gst_object_unref (parent);
4852 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4854 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4859 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4861 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4865 static GstTaskThreadCallbacks thr_callbacks = {
4871 * gst_pad_start_task:
4872 * @pad: the #GstPad to start the task of
4873 * @func: the task function to call
4874 * @data: data passed to the task function
4876 * Starts a task that repeatedly calls @func with @data. This function
4877 * is mostly used in pad activation functions to start the dataflow.
4878 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4879 * before @func is called.
4881 * Returns: a %TRUE if the task could be started.
4884 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4889 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4890 g_return_val_if_fail (func != NULL, FALSE);
4892 GST_DEBUG_OBJECT (pad, "start task");
4894 GST_OBJECT_LOCK (pad);
4895 task = GST_PAD_TASK (pad);
4897 task = gst_task_create (func, data);
4898 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4899 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4900 GST_DEBUG_OBJECT (pad, "created task");
4901 GST_PAD_TASK (pad) = task;
4902 gst_object_ref (task);
4903 /* release lock to post the message */
4904 GST_OBJECT_UNLOCK (pad);
4906 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4908 gst_object_unref (task);
4910 GST_OBJECT_LOCK (pad);
4911 /* nobody else is supposed to have changed the pad now */
4912 if (GST_PAD_TASK (pad) != task)
4913 goto concurrent_stop;
4915 res = gst_task_set_state (task, GST_TASK_STARTED);
4916 GST_OBJECT_UNLOCK (pad);
4923 GST_OBJECT_UNLOCK (pad);
4928 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4929 "No getcaps function on sink pad, dropping renegotiate event");
4930 gst_event_unref (event);
4931 GST_OBJECT_UNLOCK (pad);
4937 * gst_pad_pause_task:
4938 * @pad: the #GstPad to pause the task of
4940 * Pause the task of @pad. This function will also wait until the
4941 * function executed by the task is finished if this function is not
4942 * called from the task function.
4944 * Returns: a TRUE if the task could be paused or FALSE when the pad
4948 gst_pad_pause_task (GstPad * pad)
4953 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4955 GST_DEBUG_OBJECT (pad, "pause task");
4957 GST_OBJECT_LOCK (pad);
4958 task = GST_PAD_TASK (pad);
4961 res = gst_task_set_state (task, GST_TASK_PAUSED);
4962 GST_OBJECT_UNLOCK (pad);
4964 /* wait for task function to finish, this lock is recursive so it does nothing
4965 * when the pause is called from the task itself */
4966 GST_PAD_STREAM_LOCK (pad);
4967 GST_PAD_STREAM_UNLOCK (pad);
4973 GST_DEBUG_OBJECT (pad, "pad has no task");
4974 GST_OBJECT_UNLOCK (pad);
4980 * gst_pad_stop_task:
4981 * @pad: the #GstPad to stop the task of
4983 * Stop the task of @pad. This function will also make sure that the
4984 * function executed by the task will effectively stop if not called
4985 * from the GstTaskFunction.
4987 * This function will deadlock if called from the GstTaskFunction of
4988 * the task. Use gst_task_pause() instead.
4990 * Regardless of whether the pad has a task, the stream lock is acquired and
4991 * released so as to ensure that streaming through this pad has finished.
4993 * Returns: a TRUE if the task could be stopped or FALSE on error.
4996 gst_pad_stop_task (GstPad * pad)
5001 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5003 GST_DEBUG_OBJECT (pad, "stop task");
5005 GST_OBJECT_LOCK (pad);
5006 task = GST_PAD_TASK (pad);
5009 GST_PAD_TASK (pad) = NULL;
5010 res = gst_task_set_state (task, GST_TASK_STOPPED);
5011 GST_OBJECT_UNLOCK (pad);
5013 GST_PAD_STREAM_LOCK (pad);
5014 GST_PAD_STREAM_UNLOCK (pad);
5016 if (!gst_task_join (task))
5019 gst_object_unref (task);
5025 GST_DEBUG_OBJECT (pad, "no task");
5026 GST_OBJECT_UNLOCK (pad);
5028 GST_PAD_STREAM_LOCK (pad);
5029 GST_PAD_STREAM_UNLOCK (pad);
5031 /* this is not an error */
5036 /* this is bad, possibly the application tried to join the task from
5037 * the task's thread. We install the task again so that it will be stopped
5038 * again from the right thread next time hopefully. */
5039 GST_OBJECT_LOCK (pad);
5040 GST_DEBUG_OBJECT (pad, "join failed");
5041 /* we can only install this task if there was no other task */
5042 if (GST_PAD_TASK (pad) == NULL)
5043 GST_PAD_TASK (pad) = task;
5044 GST_OBJECT_UNLOCK (pad);