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 static void gst_pad_dispose (GObject * object);
99 static void gst_pad_finalize (GObject * object);
100 static void gst_pad_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_pad_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
105 static GstFlowReturn handle_pad_block (GstPad * pad);
106 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
107 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
108 static gboolean gst_pad_activate_default (GstPad * pad);
109 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
111 #ifndef GST_DISABLE_LOADSAVE
112 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
115 static GstObjectClass *parent_class = NULL;
116 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
118 /* quarks for probe signals */
119 static GQuark buffer_quark;
120 static GQuark event_quark;
129 static GstFlowQuarks flow_quarks[] = {
130 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
131 {GST_FLOW_RESEND, "resend", 0},
132 {GST_FLOW_OK, "ok", 0},
133 {GST_FLOW_NOT_LINKED, "not-linked", 0},
134 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
135 {GST_FLOW_UNEXPECTED, "unexpected", 0},
136 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
137 {GST_FLOW_ERROR, "error", 0},
138 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
139 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0},
146 * @ret: a #GstFlowReturn to get the name of.
148 * Gets a string representing the given flow return.
150 * Returns: a static string with the name of the flow return.
152 G_CONST_RETURN gchar *
153 gst_flow_get_name (GstFlowReturn ret)
157 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
159 for (i = 0; flow_quarks[i].name; i++) {
160 if (ret == flow_quarks[i].ret)
161 return flow_quarks[i].name;
168 * @ret: a #GstFlowReturn to get the quark of.
170 * Get the unique quark for the given GstFlowReturn.
172 * Returns: the quark associated with the flow return or 0 if an
173 * invalid return was specified.
176 gst_flow_to_quark (GstFlowReturn ret)
180 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
182 for (i = 0; flow_quarks[i].name; i++) {
183 if (ret == flow_quarks[i].ret)
184 return flow_quarks[i].quark;
193 buffer_quark = g_quark_from_static_string ("buffer"); \
194 event_quark = g_quark_from_static_string ("event"); \
196 for (i = 0; flow_quarks[i].name; i++) { \
197 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
200 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
201 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
204 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
207 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
208 GValue * return_accu, const GValue * handler_return, gpointer dummy)
210 gboolean ret = g_value_get_boolean (handler_return);
212 GST_DEBUG ("accumulated %d", ret);
213 g_value_set_boolean (return_accu, ret);
219 default_have_data (GstPad * pad, GstMiniObject * o)
225 gst_pad_class_init (GstPadClass * klass)
227 GObjectClass *gobject_class;
228 GstObjectClass *gstobject_class;
230 gobject_class = G_OBJECT_CLASS (klass);
231 gstobject_class = GST_OBJECT_CLASS (klass);
233 parent_class = g_type_class_peek_parent (klass);
235 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
236 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pad_finalize);
237 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pad_set_property);
238 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pad_get_property);
242 * @pad: the pad that emitted the signal
243 * @peer: the peer pad that has been connected
245 * Signals that a pad has been linked to the peer pad.
247 gst_pad_signals[PAD_LINKED] =
248 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
249 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
250 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
253 * @pad: the pad that emitted the signal
254 * @peer: the peer pad that has been disconnected
256 * Signals that a pad has been unlinked from the peer pad.
258 gst_pad_signals[PAD_UNLINKED] =
259 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
260 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
261 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
263 * GstPad::request-link:
264 * @pad: the pad that emitted the signal
265 * @peer: the peer pad for which a connection is requested
267 * Signals that a pad connection has been requested.
269 gst_pad_signals[PAD_REQUEST_LINK] =
270 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
271 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
272 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
276 * @pad: the pad that emitted the signal
277 * @mini_obj: new data
279 * Signals that new data is available on the pad. This signal is used
280 * internally for implementing pad probes.
281 * See gst_pad_add_*_probe functions.
283 * Returns: %TRUE to keep the data, %FALSE to drop it
285 gst_pad_signals[PAD_HAVE_DATA] =
286 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
287 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
288 G_STRUCT_OFFSET (GstPadClass, have_data),
289 _gst_do_pass_data_accumulator,
290 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
291 GST_TYPE_MINI_OBJECT);
293 g_object_class_install_property (gobject_class, PAD_PROP_CAPS,
294 g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
295 GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
297 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
298 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
299 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
300 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
301 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
302 g_param_spec_object ("template", "Template",
303 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 #ifndef GST_DISABLE_LOADSAVE
307 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
309 gstobject_class->path_string_separator = ".";
311 klass->have_data = default_have_data;
315 gst_pad_init (GstPad * pad)
317 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
318 GST_PAD_PEER (pad) = NULL;
320 GST_PAD_CHAINFUNC (pad) = NULL;
322 GST_PAD_LINKFUNC (pad) = NULL;
324 GST_PAD_CAPS (pad) = NULL;
325 GST_PAD_GETCAPSFUNC (pad) = NULL;
327 GST_PAD_ACTIVATEFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_activate_default);
328 GST_PAD_EVENTFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_event_default);
329 GST_PAD_QUERYTYPEFUNC (pad) =
330 GST_DEBUG_FUNCPTR (gst_pad_get_query_types_default);
331 GST_PAD_QUERYFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_query_default);
332 GST_PAD_INTLINKFUNC (pad) =
333 GST_DEBUG_FUNCPTR (gst_pad_get_internal_links_default);
334 GST_PAD_ITERINTLINKFUNC (pad) =
335 GST_DEBUG_FUNCPTR (gst_pad_iterate_internal_links_default);
337 GST_PAD_ACCEPTCAPSFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_acceptcaps_default);
339 pad->do_buffer_signals = 0;
340 pad->do_event_signals = 0;
342 GST_PAD_SET_FLUSHING (pad);
344 pad->preroll_lock = g_mutex_new ();
345 pad->preroll_cond = g_cond_new ();
347 pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
348 g_static_rec_mutex_init (pad->stream_rec_lock);
350 pad->block_cond = g_cond_new ();
354 gst_pad_dispose (GObject * object)
356 GstPad *pad = GST_PAD (object);
359 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
361 /* unlink the peer pad */
362 if ((peer = gst_pad_get_peer (pad))) {
363 /* window for MT unsafeness, someone else could unlink here
364 * and then we call unlink with wrong pads. The unlink
365 * function would catch this and safely return failed. */
366 if (GST_PAD_IS_SRC (pad))
367 gst_pad_unlink (pad, peer);
369 gst_pad_unlink (peer, pad);
371 gst_object_unref (peer);
375 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
377 gst_pad_set_pad_template (pad, NULL);
379 if (pad->block_destroy_data && pad->block_data) {
380 pad->block_destroy_data (pad->block_data);
381 pad->block_data = NULL;
384 G_OBJECT_CLASS (parent_class)->dispose (object);
388 gst_pad_finalize (GObject * object)
390 GstPad *pad = GST_PAD (object);
393 /* in case the task is still around, clean it up */
394 if ((task = GST_PAD_TASK (pad))) {
395 gst_task_join (task);
396 GST_PAD_TASK (pad) = NULL;
397 gst_object_unref (task);
400 if (pad->stream_rec_lock) {
401 g_static_rec_mutex_free (pad->stream_rec_lock);
402 g_free (pad->stream_rec_lock);
403 pad->stream_rec_lock = NULL;
405 if (pad->preroll_lock) {
406 g_mutex_free (pad->preroll_lock);
407 g_cond_free (pad->preroll_cond);
408 pad->preroll_lock = NULL;
409 pad->preroll_cond = NULL;
411 if (pad->block_cond) {
412 g_cond_free (pad->block_cond);
413 pad->block_cond = NULL;
416 G_OBJECT_CLASS (parent_class)->finalize (object);
420 gst_pad_set_property (GObject * object, guint prop_id,
421 const GValue * value, GParamSpec * pspec)
423 g_return_if_fail (GST_IS_PAD (object));
426 case PAD_PROP_DIRECTION:
427 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
429 case PAD_PROP_TEMPLATE:
430 gst_pad_set_pad_template (GST_PAD_CAST (object),
431 (GstPadTemplate *) g_value_get_object (value));
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 gst_pad_get_property (GObject * object, guint prop_id,
441 GValue * value, GParamSpec * pspec)
443 g_return_if_fail (GST_IS_PAD (object));
447 GST_OBJECT_LOCK (object);
448 g_value_set_boxed (value, GST_PAD_CAPS (object));
449 GST_OBJECT_UNLOCK (object);
451 case PAD_PROP_DIRECTION:
452 g_value_set_enum (value, GST_PAD_DIRECTION (object));
454 case PAD_PROP_TEMPLATE:
455 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
458 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
465 * @name: the name of the new pad.
466 * @direction: the #GstPadDirection of the pad.
468 * Creates a new pad with the given name in the given direction.
469 * If name is NULL, a guaranteed unique name (across all pads)
471 * This function makes a copy of the name so you can safely free the name.
473 * Returns: a new #GstPad, or NULL in case of an error.
478 gst_pad_new (const gchar * name, GstPadDirection direction)
480 return g_object_new (GST_TYPE_PAD,
481 "name", name, "direction", direction, NULL);
485 * gst_pad_new_from_template:
486 * @templ: the pad template to use
487 * @name: the name of the element
489 * Creates a new pad with the given name from the given template.
490 * If name is NULL, a guaranteed unique name (across all pads)
492 * This function makes a copy of the name so you can safely free the name.
494 * Returns: a new #GstPad, or NULL in case of an error.
497 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
499 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
501 return g_object_new (GST_TYPE_PAD,
502 "name", name, "direction", templ->direction, "template", templ, NULL);
506 * gst_pad_new_from_static_template:
507 * @templ: the #GstStaticPadTemplate to use
508 * @name: the name of the element
510 * Creates a new pad with the given name from the given static template.
511 * If name is NULL, a guaranteed unique name (across all pads)
513 * This function makes a copy of the name so you can safely free the name.
515 * Returns: a new #GstPad, or NULL in case of an error.
518 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
522 GstPadTemplate *template;
524 template = gst_static_pad_template_get (templ);
525 pad = gst_pad_new_from_template (template, name);
526 gst_object_unref (template);
531 * gst_pad_get_direction:
532 * @pad: a #GstPad to get the direction of.
534 * Gets the direction of the pad. The direction of the pad is
535 * decided at construction time so this function does not take
538 * Returns: the #GstPadDirection of the pad.
543 gst_pad_get_direction (GstPad * pad)
545 GstPadDirection result;
547 /* PAD_UNKNOWN is a little silly but we need some sort of
548 * error return value */
549 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
551 result = GST_PAD_DIRECTION (pad);
557 gst_pad_activate_default (GstPad * pad)
559 return gst_pad_activate_push (pad, TRUE);
563 pre_activate (GstPad * pad, GstActivateMode new_mode)
566 case GST_ACTIVATE_PUSH:
567 case GST_ACTIVATE_PULL:
568 GST_OBJECT_LOCK (pad);
569 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
571 GST_PAD_UNSET_FLUSHING (pad);
572 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
573 GST_OBJECT_UNLOCK (pad);
575 case GST_ACTIVATE_NONE:
576 GST_OBJECT_LOCK (pad);
577 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
578 GST_PAD_SET_FLUSHING (pad);
579 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
580 /* unlock blocked pads so element can resume and stop */
581 GST_PAD_BLOCK_BROADCAST (pad);
582 GST_OBJECT_UNLOCK (pad);
588 post_activate (GstPad * pad, GstActivateMode new_mode)
591 case GST_ACTIVATE_PUSH:
592 case GST_ACTIVATE_PULL:
595 case GST_ACTIVATE_NONE:
596 /* ensures that streaming stops */
597 GST_PAD_STREAM_LOCK (pad);
598 GST_DEBUG_OBJECT (pad, "stopped streaming");
599 GST_PAD_STREAM_UNLOCK (pad);
605 * gst_pad_set_active:
606 * @pad: the #GstPad to activate or deactivate.
607 * @active: whether or not the pad should be active.
609 * Activates or deactivates the given pad.
610 * Normally called from within core state change functions.
612 * If @active, makes sure the pad is active. If it is already active, either in
613 * push or pull mode, just return. Otherwise dispatches to the pad's activate
614 * function to perform the actual activation.
616 * If not @active, checks the pad's current mode and calls
617 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
620 * Returns: #TRUE if the operation was successful.
625 gst_pad_set_active (GstPad * pad, gboolean active)
628 gboolean ret = FALSE;
630 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
632 GST_OBJECT_LOCK (pad);
633 old = GST_PAD_ACTIVATE_MODE (pad);
634 GST_OBJECT_UNLOCK (pad);
638 case GST_ACTIVATE_PUSH:
639 GST_DEBUG_OBJECT (pad, "activating pad from push");
642 case GST_ACTIVATE_PULL:
643 GST_DEBUG_OBJECT (pad, "activating pad from pull");
646 case GST_ACTIVATE_NONE:
647 GST_DEBUG_OBJECT (pad, "activating pad from none");
648 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
653 case GST_ACTIVATE_PUSH:
654 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
655 ret = gst_pad_activate_push (pad, FALSE);
657 case GST_ACTIVATE_PULL:
658 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
659 ret = gst_pad_activate_pull (pad, FALSE);
661 case GST_ACTIVATE_NONE:
662 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
669 GST_OBJECT_LOCK (pad);
671 g_critical ("Failed to deactivate pad %s:%s, very bad",
672 GST_DEBUG_PAD_NAME (pad));
674 GST_WARNING_OBJECT (pad, "Failed to activate pad");
676 GST_OBJECT_UNLOCK (pad);
683 * gst_pad_activate_pull:
684 * @pad: the #GstPad to activate or deactivate.
685 * @active: whether or not the pad should be active.
687 * Activates or deactivates the given pad in pull mode via dispatching to the
688 * pad's activatepullfunc. For use from within pad activation functions only.
689 * When called on sink pads, will first proxy the call to the peer pad, which
690 * is expected to activate its internally linked pads from within its
691 * activate_pull function.
693 * If you don't know what this is, you probably don't want to call it.
695 * Returns: TRUE if the operation was successful.
700 gst_pad_activate_pull (GstPad * pad, gboolean active)
702 GstActivateMode old, new;
705 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
707 GST_OBJECT_LOCK (pad);
708 old = GST_PAD_ACTIVATE_MODE (pad);
709 GST_OBJECT_UNLOCK (pad);
713 case GST_ACTIVATE_PULL:
714 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
716 case GST_ACTIVATE_PUSH:
717 GST_DEBUG_OBJECT (pad,
718 "activating pad from push, deactivate push first");
719 /* pad was activate in the wrong direction, deactivate it
720 * and reactivate it in pull mode */
721 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
722 goto deactivate_failed;
723 /* fallthrough, pad is deactivated now. */
724 case GST_ACTIVATE_NONE:
725 GST_DEBUG_OBJECT (pad, "activating pad from none");
730 case GST_ACTIVATE_NONE:
731 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
733 case GST_ACTIVATE_PUSH:
734 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
735 /* pad was activated in the other direction, deactivate it
736 * in push mode, this should not happen... */
737 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
738 goto deactivate_failed;
739 /* everything is fine now */
741 case GST_ACTIVATE_PULL:
742 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
747 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
748 if ((peer = gst_pad_get_peer (pad))) {
749 GST_DEBUG_OBJECT (pad, "calling peer");
750 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
752 gst_object_unref (peer);
754 /* there is no peer, this is only fatal when we activate. When we
755 * deactivate, we must assume the application has unlinked the peer and
756 * will deactivate it eventually. */
760 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
763 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
764 goto failure; /* Can't activate pull on a src without a
768 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
769 pre_activate (pad, new);
771 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
772 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
775 /* can happen for sinks of passthrough elements */
778 post_activate (pad, new);
780 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
781 active ? "activated" : "deactivated");
787 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
788 active ? "activated" : "deactivated");
793 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
794 "failed to %s in switch to pull from mode %d",
795 (active ? "activate" : "deactivate"), old);
800 GST_OBJECT_LOCK (peer);
801 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
802 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
803 GST_OBJECT_UNLOCK (peer);
804 gst_object_unref (peer);
809 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
815 GST_OBJECT_LOCK (pad);
816 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
817 active ? "activate" : "deactivate");
818 GST_PAD_SET_FLUSHING (pad);
819 GST_PAD_ACTIVATE_MODE (pad) = old;
820 GST_OBJECT_UNLOCK (pad);
826 * gst_pad_activate_push:
827 * @pad: the #GstPad to activate or deactivate.
828 * @active: whether the pad should be active or not.
830 * Activates or deactivates the given pad in push mode via dispatching to the
831 * pad's activatepushfunc. For use from within pad activation functions only.
833 * If you don't know what this is, you probably don't want to call it.
835 * Returns: %TRUE if the operation was successful.
840 gst_pad_activate_push (GstPad * pad, gboolean active)
842 GstActivateMode old, new;
844 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
845 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
846 active ? "activated" : "deactivated");
848 GST_OBJECT_LOCK (pad);
849 old = GST_PAD_ACTIVATE_MODE (pad);
850 GST_OBJECT_UNLOCK (pad);
854 case GST_ACTIVATE_PUSH:
855 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
857 case GST_ACTIVATE_PULL:
858 GST_DEBUG_OBJECT (pad,
859 "activating pad from push, deactivating pull first");
860 /* pad was activate in the wrong direction, deactivate it
861 * an reactivate it in push mode */
862 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
863 goto deactivate_failed;
864 /* fallthrough, pad is deactivated now. */
865 case GST_ACTIVATE_NONE:
866 GST_DEBUG_OBJECT (pad, "activating pad from none");
871 case GST_ACTIVATE_NONE:
872 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
874 case GST_ACTIVATE_PULL:
875 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
876 /* pad was activated in the other direction, deactivate it
877 * in pull mode, this should not happen... */
878 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
879 goto deactivate_failed;
880 /* everything is fine now */
882 case GST_ACTIVATE_PUSH:
883 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
888 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
889 pre_activate (pad, new);
891 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
892 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
896 /* quite ok, element relies on state change func to prepare itself */
899 post_activate (pad, new);
901 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
902 active ? "activated" : "deactivated");
907 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
908 active ? "activated" : "deactivated");
913 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
914 "failed to %s in switch to push from mode %d",
915 (active ? "activate" : "deactivate"), old);
920 GST_OBJECT_LOCK (pad);
921 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
922 active ? "activate" : "deactivate");
923 GST_PAD_SET_FLUSHING (pad);
924 GST_PAD_ACTIVATE_MODE (pad) = old;
925 GST_OBJECT_UNLOCK (pad);
932 * @pad: the #GstPad to query
934 * Query if a pad is active
936 * Returns: TRUE if the pad is active.
941 gst_pad_is_active (GstPad * pad)
943 gboolean result = FALSE;
945 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
947 GST_OBJECT_LOCK (pad);
948 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
949 GST_OBJECT_UNLOCK (pad);
955 * gst_pad_set_blocked_async_full:
956 * @pad: the #GstPad to block or unblock
957 * @blocked: boolean indicating whether the pad should be blocked or unblocked
958 * @callback: #GstPadBlockCallback that will be called when the
960 * @user_data: user data passed to the callback
961 * @destroy_data: #GDestroyNotify for user_data
963 * Blocks or unblocks the dataflow on a pad. The provided callback
964 * is called when the operation succeeds; this happens right before the next
965 * attempt at pushing a buffer on the pad.
967 * This can take a while as the pad can only become blocked when real dataflow
969 * When the pipeline is stalled, for example in PAUSED, this can
970 * take an indeterminate amount of time.
971 * You can pass NULL as the callback to make this call block. Be careful with
972 * this blocking call as it might not return for reasons stated above.
974 * Returns: TRUE if the pad could be blocked. This function can fail if the
975 * wrong parameters were passed or the pad was already in the requested state.
982 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
983 GstPadBlockCallback callback, gpointer user_data,
984 GDestroyNotify destroy_data)
986 gboolean was_blocked = FALSE;
988 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
990 GST_OBJECT_LOCK (pad);
992 was_blocked = GST_PAD_IS_BLOCKED (pad);
994 if (G_UNLIKELY (was_blocked == blocked))
995 goto had_right_state;
998 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1000 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1002 if (pad->block_destroy_data && pad->block_data &&
1003 pad->block_data != user_data)
1004 pad->block_destroy_data (pad->block_data);
1006 pad->block_callback = callback;
1007 pad->block_data = user_data;
1008 pad->block_destroy_data = destroy_data;
1009 pad->abidata.ABI.block_callback_called = FALSE;
1011 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1012 GST_PAD_BLOCK_WAIT (pad);
1013 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1016 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1018 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1020 if (pad->block_destroy_data && pad->block_data &&
1021 pad->block_data != user_data)
1022 pad->block_destroy_data (pad->block_data);
1024 pad->block_callback = callback;
1025 pad->block_data = user_data;
1026 pad->block_destroy_data = destroy_data;
1027 pad->abidata.ABI.block_callback_called = FALSE;
1029 GST_PAD_BLOCK_BROADCAST (pad);
1031 /* no callback, wait for the unblock to happen */
1032 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1033 GST_PAD_BLOCK_WAIT (pad);
1034 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1037 GST_OBJECT_UNLOCK (pad);
1043 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1044 "pad was in right state (%d)", was_blocked);
1045 GST_OBJECT_UNLOCK (pad);
1052 * gst_pad_set_blocked_async:
1053 * @pad: the #GstPad to block or unblock
1054 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1055 * @callback: #GstPadBlockCallback that will be called when the
1056 * operation succeeds
1057 * @user_data: user data passed to the callback
1059 * Blocks or unblocks the dataflow on a pad. The provided callback
1060 * is called when the operation succeeds; this happens right before the next
1061 * attempt at pushing a buffer on the pad.
1063 * This can take a while as the pad can only become blocked when real dataflow
1065 * When the pipeline is stalled, for example in PAUSED, this can
1066 * take an indeterminate amount of time.
1067 * You can pass NULL as the callback to make this call block. Be careful with
1068 * this blocking call as it might not return for reasons stated above.
1070 * Returns: TRUE if the pad could be blocked. This function can fail if the
1071 * wrong parameters were passed or the pad was already in the requested state.
1076 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1077 GstPadBlockCallback callback, gpointer user_data)
1079 return gst_pad_set_blocked_async_full (pad, blocked,
1080 callback, user_data, NULL);
1084 * gst_pad_set_blocked:
1085 * @pad: the #GstPad to block or unblock
1086 * @blocked: boolean indicating we should block or unblock
1088 * Blocks or unblocks the dataflow on a pad. This function is
1089 * a shortcut for gst_pad_set_blocked_async() with a NULL
1092 * Returns: TRUE if the pad could be blocked. This function can fail if the
1093 * wrong parameters were passed or the pad was already in the requested state.
1098 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1100 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1104 * gst_pad_is_blocked:
1105 * @pad: the #GstPad to query
1107 * Checks if the pad is blocked or not. This function returns the
1108 * last requested state of the pad. It is not certain that the pad
1109 * is actually blocking at this point (see gst_pad_is_blocking()).
1111 * Returns: TRUE if the pad is blocked.
1116 gst_pad_is_blocked (GstPad * pad)
1118 gboolean result = FALSE;
1120 g_return_val_if_fail (GST_IS_PAD (pad), result);
1122 GST_OBJECT_LOCK (pad);
1123 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1124 GST_OBJECT_UNLOCK (pad);
1130 * gst_pad_is_blocking:
1131 * @pad: the #GstPad to query
1133 * Checks if the pad is blocking or not. This is a guaranteed state
1134 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1136 * Returns: TRUE if the pad is blocking.
1143 gst_pad_is_blocking (GstPad * pad)
1145 gboolean result = FALSE;
1147 g_return_val_if_fail (GST_IS_PAD (pad), result);
1149 GST_OBJECT_LOCK (pad);
1150 /* the blocking flag is only valid if the pad is not flushing */
1151 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1152 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1153 GST_OBJECT_UNLOCK (pad);
1159 * gst_pad_set_activate_function:
1161 * @activate: the #GstPadActivateFunction to set.
1163 * Sets the given activate function for @pad. The activate function will
1164 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1165 * the actual activation. Only makes sense to set on sink pads.
1167 * Call this function if your sink pad can start a pull-based task.
1170 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1172 g_return_if_fail (GST_IS_PAD (pad));
1174 GST_PAD_ACTIVATEFUNC (pad) = activate;
1175 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1176 GST_DEBUG_FUNCPTR_NAME (activate));
1180 * gst_pad_set_activatepull_function:
1182 * @activatepull: the #GstPadActivateModeFunction to set.
1184 * Sets the given activate_pull function for the pad. An activate_pull function
1185 * prepares the element and any upstream connections for pulling. See XXX
1186 * part-activation.txt for details.
1189 gst_pad_set_activatepull_function (GstPad * pad,
1190 GstPadActivateModeFunction activatepull)
1192 g_return_if_fail (GST_IS_PAD (pad));
1194 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1195 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1196 GST_DEBUG_FUNCPTR_NAME (activatepull));
1200 * gst_pad_set_activatepush_function:
1202 * @activatepush: the #GstPadActivateModeFunction to set.
1204 * Sets the given activate_push function for the pad. An activate_push function
1205 * prepares the element for pushing. See XXX part-activation.txt for details.
1208 gst_pad_set_activatepush_function (GstPad * pad,
1209 GstPadActivateModeFunction activatepush)
1211 g_return_if_fail (GST_IS_PAD (pad));
1213 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1214 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1215 GST_DEBUG_FUNCPTR_NAME (activatepush));
1219 * gst_pad_set_chain_function:
1220 * @pad: a sink #GstPad.
1221 * @chain: the #GstPadChainFunction to set.
1223 * Sets the given chain function for the pad. The chain function is called to
1224 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1227 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1229 g_return_if_fail (GST_IS_PAD (pad));
1230 g_return_if_fail (GST_PAD_IS_SINK (pad));
1232 GST_PAD_CHAINFUNC (pad) = chain;
1233 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1234 GST_DEBUG_FUNCPTR_NAME (chain));
1238 * gst_pad_set_chain_list_function:
1239 * @pad: a sink #GstPad.
1240 * @chainlist: the #GstPadChainListFunction to set.
1242 * Sets the given chain list function for the pad. The chainlist function is
1243 * called to process a #GstBufferList input buffer list. See
1244 * #GstPadChainListFunction for more details.
1249 gst_pad_set_chain_list_function (GstPad * pad,
1250 GstPadChainListFunction chainlist)
1252 g_return_if_fail (GST_IS_PAD (pad));
1253 g_return_if_fail (GST_PAD_IS_SINK (pad));
1255 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1256 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1257 GST_DEBUG_FUNCPTR_NAME (chainlist));
1261 * gst_pad_set_getrange_function:
1262 * @pad: a source #GstPad.
1263 * @get: the #GstPadGetRangeFunction to set.
1265 * Sets the given getrange function for the pad. The getrange function is
1266 * called to produce a new #GstBuffer to start the processing pipeline. see
1267 * #GstPadGetRangeFunction for a description of the getrange function.
1270 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1272 g_return_if_fail (GST_IS_PAD (pad));
1273 g_return_if_fail (GST_PAD_IS_SRC (pad));
1275 GST_PAD_GETRANGEFUNC (pad) = get;
1277 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1278 GST_DEBUG_FUNCPTR_NAME (get));
1282 * gst_pad_set_checkgetrange_function:
1283 * @pad: a source #GstPad.
1284 * @check: the #GstPadCheckGetRangeFunction to set.
1286 * Sets the given checkgetrange function for the pad. Implement this function
1287 * on a pad if you dynamically support getrange based scheduling on the pad.
1290 gst_pad_set_checkgetrange_function (GstPad * pad,
1291 GstPadCheckGetRangeFunction check)
1293 g_return_if_fail (GST_IS_PAD (pad));
1294 g_return_if_fail (GST_PAD_IS_SRC (pad));
1296 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1298 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1299 GST_DEBUG_FUNCPTR_NAME (check));
1303 * gst_pad_set_event_function:
1304 * @pad: a #GstPad of either direction.
1305 * @event: the #GstPadEventFunction to set.
1307 * Sets the given event handler for the pad.
1310 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1312 g_return_if_fail (GST_IS_PAD (pad));
1314 GST_PAD_EVENTFUNC (pad) = event;
1316 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1317 GST_DEBUG_FUNCPTR_NAME (event));
1321 * gst_pad_set_query_function:
1322 * @pad: a #GstPad of either direction.
1323 * @query: the #GstPadQueryFunction to set.
1325 * Set the given query function for the pad.
1328 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1330 g_return_if_fail (GST_IS_PAD (pad));
1332 GST_PAD_QUERYFUNC (pad) = query;
1334 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1335 GST_DEBUG_FUNCPTR_NAME (query));
1339 * gst_pad_set_query_type_function:
1340 * @pad: a #GstPad of either direction.
1341 * @type_func: the #GstPadQueryTypeFunction to set.
1343 * Set the given query type function for the pad.
1346 gst_pad_set_query_type_function (GstPad * pad,
1347 GstPadQueryTypeFunction type_func)
1349 g_return_if_fail (GST_IS_PAD (pad));
1351 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1353 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1354 GST_DEBUG_FUNCPTR_NAME (type_func));
1358 * gst_pad_get_query_types:
1361 * Get an array of supported queries that can be performed
1364 * Returns: a zero-terminated array of #GstQueryType.
1366 const GstQueryType *
1367 gst_pad_get_query_types (GstPad * pad)
1369 GstPadQueryTypeFunction func;
1371 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1373 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1385 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1387 *data = gst_pad_get_query_types (pad);
1393 * gst_pad_get_query_types_default:
1396 * Invoke the default dispatcher for the query types on
1399 * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1400 * internally-linked pads has a query types function.
1402 const GstQueryType *
1403 gst_pad_get_query_types_default (GstPad * pad)
1405 GstQueryType *result = NULL;
1407 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1409 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1410 gst_pad_get_query_types_dispatcher, &result);
1416 * gst_pad_set_iterate_internal_links_function:
1417 * @pad: a #GstPad of either direction.
1418 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1420 * Sets the given internal link iterator function for the pad.
1425 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1426 GstPadIterIntLinkFunction iterintlink)
1428 g_return_if_fail (GST_IS_PAD (pad));
1430 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1431 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1432 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1436 * gst_pad_set_internal_link_function:
1437 * @pad: a #GstPad of either direction.
1438 * @intlink: the #GstPadIntLinkFunction to set.
1440 * Sets the given internal link function for the pad.
1442 * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1444 #ifndef GST_REMOVE_DEPRECATED
1446 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1448 g_return_if_fail (GST_IS_PAD (pad));
1450 GST_PAD_INTLINKFUNC (pad) = intlink;
1451 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1452 GST_DEBUG_FUNCPTR_NAME (intlink));
1454 #endif /* GST_REMOVE_DEPRECATED */
1457 * gst_pad_set_link_function:
1459 * @link: the #GstPadLinkFunction to set.
1461 * Sets the given link function for the pad. It will be called when
1462 * the pad is linked with another pad.
1464 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1467 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1468 * cannot be made for some reason.
1470 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1471 * of the peer sink pad, if present.
1474 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1476 g_return_if_fail (GST_IS_PAD (pad));
1478 GST_PAD_LINKFUNC (pad) = link;
1479 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1480 GST_DEBUG_FUNCPTR_NAME (link));
1484 * gst_pad_set_unlink_function:
1486 * @unlink: the #GstPadUnlinkFunction to set.
1488 * Sets the given unlink function for the pad. It will be called
1489 * when the pad is unlinked.
1492 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1494 g_return_if_fail (GST_IS_PAD (pad));
1496 GST_PAD_UNLINKFUNC (pad) = unlink;
1497 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1498 GST_DEBUG_FUNCPTR_NAME (unlink));
1502 * gst_pad_set_getcaps_function:
1504 * @getcaps: the #GstPadGetCapsFunction to set.
1506 * Sets the given getcaps function for the pad. @getcaps should return the
1507 * allowable caps for a pad in the context of the element's state, its link to
1508 * other elements, and the devices or files it has opened. These caps must be a
1509 * subset of the pad template caps. In the NULL state with no links, @getcaps
1510 * should ideally return the same caps as the pad template. In rare
1511 * circumstances, an object property can affect the caps returned by @getcaps,
1512 * but this is discouraged.
1514 * You do not need to call this function if @pad's allowed caps are always the
1515 * same as the pad template caps. This can only be true if the padtemplate
1516 * has fixed simple caps.
1518 * For most filters, the caps returned by @getcaps is directly affected by the
1519 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1520 * the srcpad's getcaps function is directly related to the stream data. Again,
1521 * @getcaps should return the most specific caps it reasonably can, since this
1522 * helps with autoplugging.
1524 * Note that the return value from @getcaps is owned by the caller, so the
1525 * caller should unref the caps after usage.
1528 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1530 g_return_if_fail (GST_IS_PAD (pad));
1532 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1533 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1534 GST_DEBUG_FUNCPTR_NAME (getcaps));
1538 * gst_pad_set_acceptcaps_function:
1540 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1542 * Sets the given acceptcaps function for the pad. The acceptcaps function
1543 * will be called to check if the pad can accept the given caps. Setting the
1544 * acceptcaps function to NULL restores the default behaviour of allowing
1545 * any caps that matches the caps from gst_pad_get_caps.
1548 gst_pad_set_acceptcaps_function (GstPad * pad,
1549 GstPadAcceptCapsFunction acceptcaps)
1551 g_return_if_fail (GST_IS_PAD (pad));
1553 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1554 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1555 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1559 * gst_pad_set_fixatecaps_function:
1561 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1563 * Sets the given fixatecaps function for the pad. The fixatecaps function
1564 * will be called whenever the default values for a GstCaps needs to be
1568 gst_pad_set_fixatecaps_function (GstPad * pad,
1569 GstPadFixateCapsFunction fixatecaps)
1571 g_return_if_fail (GST_IS_PAD (pad));
1573 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1574 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1575 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1579 * gst_pad_set_setcaps_function:
1581 * @setcaps: the #GstPadSetCapsFunction to set.
1583 * Sets the given setcaps function for the pad. The setcaps function
1584 * will be called whenever a buffer with a new media type is pushed or
1585 * pulled from the pad. The pad/element needs to update its internal
1586 * structures to process the new media type. If this new type is not
1587 * acceptable, the setcaps function should return FALSE.
1590 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1592 g_return_if_fail (GST_IS_PAD (pad));
1594 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1595 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1596 GST_DEBUG_FUNCPTR_NAME (setcaps));
1600 * gst_pad_set_bufferalloc_function:
1601 * @pad: a sink #GstPad.
1602 * @bufalloc: the #GstPadBufferAllocFunction to set.
1604 * Sets the given bufferalloc function for the pad. Note that the
1605 * bufferalloc function can only be set on sinkpads.
1608 gst_pad_set_bufferalloc_function (GstPad * pad,
1609 GstPadBufferAllocFunction bufalloc)
1611 g_return_if_fail (GST_IS_PAD (pad));
1612 g_return_if_fail (GST_PAD_IS_SINK (pad));
1614 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1615 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1616 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1621 * @srcpad: the source #GstPad to unlink.
1622 * @sinkpad: the sink #GstPad to unlink.
1624 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1625 * signal on both pads.
1627 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1628 * the pads were not linked together.
1633 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1635 gboolean result = FALSE;
1636 GstElement *parent = NULL;
1638 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1639 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1640 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1641 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1643 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1644 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1645 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1647 /* We need to notify the parent before taking any pad locks as the bin in
1648 * question might be waiting for a lock on the pad while holding its lock
1649 * that our message will try to take. */
1650 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1651 if (GST_IS_ELEMENT (parent)) {
1652 gst_element_post_message (parent,
1653 gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1654 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1656 gst_object_unref (parent);
1661 GST_OBJECT_LOCK (srcpad);
1663 GST_OBJECT_LOCK (sinkpad);
1665 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1666 goto not_linked_together;
1668 if (GST_PAD_UNLINKFUNC (srcpad)) {
1669 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1671 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1672 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1675 /* first clear peers */
1676 GST_PAD_PEER (srcpad) = NULL;
1677 GST_PAD_PEER (sinkpad) = NULL;
1679 GST_OBJECT_UNLOCK (sinkpad);
1680 GST_OBJECT_UNLOCK (srcpad);
1682 /* fire off a signal to each of the pads telling them
1683 * that they've been unlinked */
1684 g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1685 g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1687 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1688 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1693 if (parent != NULL) {
1694 gst_element_post_message (parent,
1695 gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1696 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1697 gst_object_unref (parent);
1702 not_linked_together:
1704 /* we do not emit a warning in this case because unlinking cannot
1705 * be made MT safe.*/
1706 GST_OBJECT_UNLOCK (sinkpad);
1707 GST_OBJECT_UNLOCK (srcpad);
1713 * gst_pad_is_linked:
1714 * @pad: pad to check
1716 * Checks if a @pad is linked to another pad or not.
1718 * Returns: TRUE if the pad is linked, FALSE otherwise.
1723 gst_pad_is_linked (GstPad * pad)
1727 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1729 GST_OBJECT_LOCK (pad);
1730 result = (GST_PAD_PEER (pad) != NULL);
1731 GST_OBJECT_UNLOCK (pad);
1736 /* get the caps from both pads and see if the intersection
1739 * This function should be called with the pad LOCK on both
1743 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1749 srccaps = gst_pad_get_caps_unlocked (src);
1750 sinkcaps = gst_pad_get_caps_unlocked (sink);
1752 GST_CAT_DEBUG (GST_CAT_CAPS, "src caps %" GST_PTR_FORMAT, srccaps);
1753 GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1755 /* if we have caps on both pads we can check the intersection. If one
1756 * of the caps is NULL, we return TRUE. */
1757 if (srccaps == NULL || sinkcaps == NULL) {
1759 gst_caps_unref (srccaps);
1761 gst_caps_unref (sinkcaps);
1765 icaps = gst_caps_intersect (srccaps, sinkcaps);
1766 gst_caps_unref (srccaps);
1767 gst_caps_unref (sinkcaps);
1772 GST_CAT_DEBUG (GST_CAT_CAPS,
1773 "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1775 if (gst_caps_is_empty (icaps))
1778 gst_caps_unref (icaps);
1783 /* incompatible cases */
1786 GST_CAT_DEBUG (GST_CAT_CAPS, "intersection gave NULL");
1791 GST_CAT_DEBUG (GST_CAT_CAPS, "intersection is EMPTY");
1792 gst_caps_unref (icaps);
1797 /* check if the grandparents of both pads are the same.
1798 * This check is required so that we don't try to link
1799 * pads from elements in different bins without ghostpads.
1801 * The LOCK should be held on both pads
1804 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1806 GstObject *psrc, *psink;
1808 psrc = GST_OBJECT_PARENT (src);
1809 psink = GST_OBJECT_PARENT (sink);
1811 /* if one of the pads has no parent, we allow the link */
1812 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1815 /* only care about parents that are elements */
1816 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1817 goto no_element_parent;
1819 /* if the parents are the same, we have a loop */
1820 if (G_UNLIKELY (psrc == psink))
1823 /* if they both have a parent, we check the grandparents. We can not lock
1824 * the parent because we hold on the child (pad) and the locking order is
1825 * parent >> child. */
1826 psrc = GST_OBJECT_PARENT (psrc);
1827 psink = GST_OBJECT_PARENT (psink);
1829 /* if they have grandparents but they are not the same */
1830 if (G_UNLIKELY (psrc != psink))
1831 goto wrong_grandparents;
1838 GST_CAT_DEBUG (GST_CAT_CAPS,
1839 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1840 GST_PTR_FORMAT, psrc, psink);
1845 GST_CAT_DEBUG (GST_CAT_CAPS,
1846 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1847 GST_PTR_FORMAT, psrc, psink);
1852 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1858 GST_CAT_DEBUG (GST_CAT_CAPS,
1859 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1860 GST_PTR_FORMAT, psrc, psink);
1865 /* FIXME leftover from an attempt at refactoring... */
1866 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1867 * the two pads will be locked in the srcpad, sinkpad order. */
1868 static GstPadLinkReturn
1869 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1871 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1872 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1874 GST_OBJECT_LOCK (srcpad);
1876 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1877 goto src_was_linked;
1879 GST_OBJECT_LOCK (sinkpad);
1881 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1882 goto sink_was_linked;
1884 /* check hierarchy, pads can only be linked if the grandparents
1886 if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1887 goto wrong_hierarchy;
1889 /* check pad caps for non-empty intersection */
1890 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1893 /* FIXME check pad scheduling for non-empty intersection */
1895 return GST_PAD_LINK_OK;
1899 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1900 GST_DEBUG_PAD_NAME (srcpad),
1901 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1902 /* we do not emit a warning in this case because unlinking cannot
1903 * be made MT safe.*/
1904 GST_OBJECT_UNLOCK (srcpad);
1905 return GST_PAD_LINK_WAS_LINKED;
1909 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1910 GST_DEBUG_PAD_NAME (sinkpad),
1911 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1912 /* we do not emit a warning in this case because unlinking cannot
1913 * be made MT safe.*/
1914 GST_OBJECT_UNLOCK (sinkpad);
1915 GST_OBJECT_UNLOCK (srcpad);
1916 return GST_PAD_LINK_WAS_LINKED;
1920 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1921 GST_OBJECT_UNLOCK (sinkpad);
1922 GST_OBJECT_UNLOCK (srcpad);
1923 return GST_PAD_LINK_WRONG_HIERARCHY;
1927 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1928 GST_OBJECT_UNLOCK (sinkpad);
1929 GST_OBJECT_UNLOCK (srcpad);
1930 return GST_PAD_LINK_NOFORMAT;
1936 * @srcpad: the source #GstPad.
1937 * @sinkpad: the sink #GstPad.
1939 * Checks if the source pad and the sink pad are compatible so they can be
1942 * Returns: TRUE if the pads can be linked.
1945 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1947 GstPadLinkReturn result;
1949 /* generic checks */
1950 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1951 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1953 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1954 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1956 /* gst_pad_link_prepare does everything for us, we only release the locks
1957 * on the pads that it gets us. If this function returns !OK the locks are not
1959 result = gst_pad_link_prepare (srcpad, sinkpad);
1960 if (result != GST_PAD_LINK_OK)
1963 GST_OBJECT_UNLOCK (srcpad);
1964 GST_OBJECT_UNLOCK (sinkpad);
1967 return result == GST_PAD_LINK_OK;
1972 * @srcpad: the source #GstPad to link.
1973 * @sinkpad: the sink #GstPad to link.
1975 * Links the source pad and the sink pad.
1977 * Returns: A result code indicating if the connection worked or
1983 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1985 GstPadLinkReturn result;
1988 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1989 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1990 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1991 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1992 GST_PAD_LINK_WRONG_DIRECTION);
1994 /* Notify the parent early. See gst_pad_unlink for details. */
1995 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1996 if (GST_IS_ELEMENT (parent)) {
1997 gst_element_post_message (parent,
1998 gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1999 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2001 gst_object_unref (parent);
2006 /* prepare will also lock the two pads */
2007 result = gst_pad_link_prepare (srcpad, sinkpad);
2009 if (result != GST_PAD_LINK_OK)
2012 /* must set peers before calling the link function */
2013 GST_PAD_PEER (srcpad) = sinkpad;
2014 GST_PAD_PEER (sinkpad) = srcpad;
2016 GST_OBJECT_UNLOCK (sinkpad);
2017 GST_OBJECT_UNLOCK (srcpad);
2019 /* FIXME released the locks here, concurrent thread might link
2020 * something else. */
2021 if (GST_PAD_LINKFUNC (srcpad)) {
2022 /* this one will call the peer link function */
2023 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2024 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2025 /* if no source link function, we need to call the sink link
2026 * function ourselves. */
2027 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2029 result = GST_PAD_LINK_OK;
2032 GST_OBJECT_LOCK (srcpad);
2033 GST_OBJECT_LOCK (sinkpad);
2035 if (result == GST_PAD_LINK_OK) {
2036 GST_OBJECT_UNLOCK (sinkpad);
2037 GST_OBJECT_UNLOCK (srcpad);
2039 /* fire off a signal to each of the pads telling them
2040 * that they've been linked */
2041 g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
2042 g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
2044 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2045 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2047 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2048 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2050 GST_PAD_PEER (srcpad) = NULL;
2051 GST_PAD_PEER (sinkpad) = NULL;
2053 GST_OBJECT_UNLOCK (sinkpad);
2054 GST_OBJECT_UNLOCK (srcpad);
2059 gst_element_post_message (parent,
2060 gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
2061 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2062 gst_object_unref (parent);
2069 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2071 GstPadTemplate **template_p;
2073 /* this function would need checks if it weren't static */
2075 GST_OBJECT_LOCK (pad);
2076 template_p = &pad->padtemplate;
2077 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2078 GST_OBJECT_UNLOCK (pad);
2081 gst_pad_template_pad_created (templ, pad);
2085 * gst_pad_get_pad_template:
2088 * Gets the template for @pad.
2090 * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2091 * if this pad has no template.
2093 * FIXME: currently returns an unrefcounted padtemplate.
2096 gst_pad_get_pad_template (GstPad * pad)
2098 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2100 return GST_PAD_PAD_TEMPLATE (pad);
2104 /* should be called with the pad LOCK held */
2105 /* refs the caps, so caller is responsible for getting it unreffed */
2107 gst_pad_get_caps_unlocked (GstPad * pad)
2109 GstCaps *result = NULL;
2110 GstPadTemplate *templ;
2112 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2114 if (GST_PAD_GETCAPSFUNC (pad)) {
2115 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2116 "dispatching to pad getcaps function");
2118 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2119 GST_OBJECT_UNLOCK (pad);
2120 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2121 GST_OBJECT_LOCK (pad);
2122 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2124 if (result == NULL) {
2125 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2126 GST_DEBUG_PAD_NAME (pad));
2128 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2129 "pad getcaps returned %" GST_PTR_FORMAT, result);
2130 #ifndef G_DISABLE_ASSERT
2131 /* check that the returned caps are a real subset of the template caps */
2132 if (GST_PAD_PAD_TEMPLATE (pad)) {
2133 const GstCaps *templ_caps =
2134 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2135 if (!gst_caps_is_subset (result, templ_caps)) {
2138 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2139 "pad returned caps %" GST_PTR_FORMAT
2140 " which are not a real subset of its template caps %"
2141 GST_PTR_FORMAT, result, templ_caps);
2143 ("pad %s:%s returned caps which are not a real "
2144 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2145 temp = gst_caps_intersect (templ_caps, result);
2146 gst_caps_unref (result);
2154 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2155 result = GST_PAD_TEMPLATE_CAPS (templ);
2156 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2157 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2160 result = gst_caps_ref (result);
2163 if ((result = GST_PAD_CAPS (pad))) {
2164 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2165 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2167 result = gst_caps_ref (result);
2171 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2172 result = gst_caps_new_empty ();
2180 * @pad: a #GstPad to get the capabilities of.
2182 * Gets the capabilities this pad can produce or consume.
2183 * Note that this method doesn't necessarily return the caps set by
2184 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2185 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2186 * the pad's get_caps function;
2187 * this returns the pad template caps if not explicitly set.
2189 * Returns: a newly allocated copy of the #GstCaps of this pad.
2194 gst_pad_get_caps (GstPad * pad)
2196 GstCaps *result = NULL;
2198 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2200 GST_OBJECT_LOCK (pad);
2202 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2204 result = gst_pad_get_caps_unlocked (pad);
2206 /* be sure that we have a copy */
2208 result = gst_caps_make_writable (result);
2210 GST_OBJECT_UNLOCK (pad);
2216 * gst_pad_peer_get_caps:
2217 * @pad: a #GstPad to get the peer capabilities of.
2219 * Gets the capabilities of the peer connected to this pad.
2221 * Returns: the #GstCaps of the peer pad. This function returns a new caps, so
2222 * use gst_caps_unref to get rid of it. this function returns NULL if there is
2226 gst_pad_peer_get_caps (GstPad * pad)
2229 GstCaps *result = NULL;
2231 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2233 GST_OBJECT_LOCK (pad);
2235 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2237 peerpad = GST_PAD_PEER (pad);
2238 if (G_UNLIKELY (peerpad == NULL))
2241 gst_object_ref (peerpad);
2242 GST_OBJECT_UNLOCK (pad);
2244 result = gst_pad_get_caps (peerpad);
2246 gst_object_unref (peerpad);
2252 GST_OBJECT_UNLOCK (pad);
2258 fixate_value (GValue * dest, const GValue * src)
2260 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2261 g_value_init (dest, G_TYPE_INT);
2262 g_value_set_int (dest, gst_value_get_int_range_min (src));
2263 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2264 g_value_init (dest, G_TYPE_DOUBLE);
2265 g_value_set_double (dest, gst_value_get_double_range_min (src));
2266 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2267 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2268 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2269 GValue temp = { 0 };
2271 /* list could be empty */
2272 if (gst_value_list_get_size (src) <= 0)
2275 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2277 if (!fixate_value (dest, &temp))
2278 gst_value_init_and_copy (dest, &temp);
2279 g_value_unset (&temp);
2280 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2281 gboolean res = FALSE;
2284 g_value_init (dest, GST_TYPE_ARRAY);
2285 for (n = 0; n < gst_value_array_get_size (src); n++) {
2287 const GValue *orig_kid = gst_value_array_get_value (src, n);
2289 if (!fixate_value (&kid, orig_kid))
2290 gst_value_init_and_copy (&kid, orig_kid);
2293 gst_value_array_append_value (dest, &kid);
2294 g_value_unset (&kid);
2298 g_value_unset (dest);
2309 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2311 GstStructure *s = data;
2314 if (fixate_value (&v, value)) {
2315 gst_structure_id_set_value (s, field_id, &v);
2323 * gst_pad_fixate_caps:
2324 * @pad: a #GstPad to fixate
2325 * @caps: the #GstCaps to fixate
2327 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2328 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2331 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2333 GstPadFixateCapsFunction fixatefunc;
2336 g_return_if_fail (GST_IS_PAD (pad));
2337 g_return_if_fail (caps != NULL);
2339 if (gst_caps_is_fixed (caps))
2342 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2344 fixatefunc (pad, caps);
2347 /* default fixation */
2348 for (n = 0; n < gst_caps_get_size (caps); n++) {
2349 GstStructure *s = gst_caps_get_structure (caps, n);
2351 gst_structure_foreach (s, gst_pad_default_fixate, s);
2355 /* Default accept caps implementation just checks against
2356 * against the allowed caps for the pad */
2358 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2360 /* get the caps and see if it intersects to something
2364 gboolean result = FALSE;
2366 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2368 allowed = gst_pad_get_caps (pad);
2370 goto nothing_allowed;
2372 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2374 intersect = gst_caps_intersect (allowed, caps);
2376 GST_DEBUG_OBJECT (pad, "intersection %" GST_PTR_FORMAT, intersect);
2378 result = !gst_caps_is_empty (intersect);
2380 gst_caps_unref (allowed);
2381 gst_caps_unref (intersect);
2388 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2394 * gst_pad_accept_caps:
2395 * @pad: a #GstPad to check
2396 * @caps: a #GstCaps to check on the pad
2398 * Check if the given pad accepts the caps.
2400 * Returns: TRUE if the pad can accept the caps.
2403 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2406 GstPadAcceptCapsFunction acceptfunc;
2407 GstCaps *existing = NULL;
2409 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2411 /* any pad can be unnegotiated */
2415 /* lock for checking the existing caps */
2416 GST_OBJECT_LOCK (pad);
2417 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2418 /* The current caps on a pad are trivially acceptable */
2419 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2420 if (caps == existing || gst_caps_is_equal (caps, existing))
2423 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2424 GST_OBJECT_UNLOCK (pad);
2426 if (G_LIKELY (acceptfunc)) {
2427 /* we can call the function */
2428 result = acceptfunc (pad, caps);
2429 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2431 /* Only null if the element explicitly unset it */
2432 result = gst_pad_acceptcaps_default (pad, caps);
2433 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2439 GST_DEBUG_OBJECT (pad, "pad had same caps");
2440 GST_OBJECT_UNLOCK (pad);
2446 * gst_pad_peer_accept_caps:
2447 * @pad: a #GstPad to check the peer of
2448 * @caps: a #GstCaps to check on the pad
2450 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2453 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2456 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2461 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2463 GST_OBJECT_LOCK (pad);
2465 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2467 peerpad = GST_PAD_PEER (pad);
2468 if (G_UNLIKELY (peerpad == NULL))
2471 gst_object_ref (peerpad);
2472 /* release lock before calling external methods but keep ref to pad */
2473 GST_OBJECT_UNLOCK (pad);
2475 result = gst_pad_accept_caps (peerpad, caps);
2477 gst_object_unref (peerpad);
2483 GST_OBJECT_UNLOCK (pad);
2490 * @pad: a #GstPad to set the capabilities of.
2491 * @caps: a #GstCaps to set.
2493 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2494 * caps on the pad will be unreffed. This function refs the caps so you should
2495 * unref if as soon as you don't need it anymore.
2496 * It is possible to set NULL caps, which will make the pad unnegotiated
2499 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2500 * or bad parameters were provided to this function.
2505 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2507 GstPadSetCapsFunction setcaps;
2510 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2511 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2513 GST_OBJECT_LOCK (pad);
2514 existing = GST_PAD_CAPS (pad);
2515 if (existing == caps)
2518 if (gst_caps_is_equal (caps, existing))
2519 goto setting_same_caps;
2521 setcaps = GST_PAD_SETCAPSFUNC (pad);
2523 /* call setcaps function to configure the pad only if the
2524 * caps is not NULL */
2525 if (setcaps != NULL && caps) {
2526 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2527 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2528 GST_OBJECT_UNLOCK (pad);
2529 if (!setcaps (pad, caps))
2531 GST_OBJECT_LOCK (pad);
2532 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2534 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2538 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2539 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %" GST_PTR_FORMAT, caps);
2540 GST_OBJECT_UNLOCK (pad);
2542 g_object_notify (G_OBJECT (pad), "caps");
2548 GST_OBJECT_UNLOCK (pad);
2553 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2554 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2555 "caps %" GST_PTR_FORMAT " same as existing, updating ptr only", caps);
2556 GST_OBJECT_UNLOCK (pad);
2563 GST_OBJECT_LOCK (pad);
2564 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2565 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2566 "caps %" GST_PTR_FORMAT " could not be set", caps);
2567 GST_OBJECT_UNLOCK (pad);
2574 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2578 /* See if pad accepts the caps */
2579 if (!gst_pad_accept_caps (pad, caps))
2582 /* set caps on pad if call succeeds */
2583 res = gst_pad_set_caps (pad, caps);
2584 /* no need to unref the caps here, set_caps takes a ref and
2585 * our ref goes away when we leave this function. */
2591 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2592 "caps %" GST_PTR_FORMAT " not accepted", caps);
2597 /* returns TRUE if the src pad could be configured to accept the given caps */
2599 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2604 /* See if pad accepts the caps */
2605 if (!gst_pad_accept_caps (pad, caps))
2608 res = gst_pad_set_caps (pad, caps);
2616 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2617 "caps %" GST_PTR_FORMAT " not accepted", caps);
2623 * gst_pad_get_pad_template_caps:
2624 * @pad: a #GstPad to get the template capabilities from.
2626 * Gets the capabilities for @pad's template.
2628 * Returns: the #GstCaps of this pad template. If you intend to keep a
2629 * reference on the caps, make a copy (see gst_caps_copy ()).
2632 gst_pad_get_pad_template_caps (GstPad * pad)
2634 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2636 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2638 if (GST_PAD_PAD_TEMPLATE (pad))
2639 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2641 return gst_static_caps_get (&anycaps);
2646 * @pad: a #GstPad to get the peer of.
2648 * Gets the peer of @pad. This function refs the peer pad so
2649 * you need to unref it after use.
2651 * Returns: the peer #GstPad. Unref after usage.
2656 gst_pad_get_peer (GstPad * pad)
2660 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2662 GST_OBJECT_LOCK (pad);
2663 result = GST_PAD_PEER (pad);
2665 gst_object_ref (result);
2666 GST_OBJECT_UNLOCK (pad);
2672 * gst_pad_get_allowed_caps:
2675 * Gets the capabilities of the allowed media types that can flow through
2676 * @pad and its peer.
2678 * The allowed capabilities is calculated as the intersection of the results of
2679 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2680 * on the resulting caps.
2682 * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2683 * longer need it. This function returns NULL when @pad has no peer.
2688 gst_pad_get_allowed_caps (GstPad * pad)
2695 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2697 GST_OBJECT_LOCK (pad);
2699 peer = GST_PAD_PEER (pad);
2700 if (G_UNLIKELY (peer == NULL))
2703 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2705 gst_object_ref (peer);
2706 GST_OBJECT_UNLOCK (pad);
2707 mycaps = gst_pad_get_caps (pad);
2709 peercaps = gst_pad_get_caps (peer);
2710 gst_object_unref (peer);
2712 caps = gst_caps_intersect (mycaps, peercaps);
2713 gst_caps_unref (peercaps);
2714 gst_caps_unref (mycaps);
2716 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2723 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2724 GST_OBJECT_UNLOCK (pad);
2731 * gst_pad_get_negotiated_caps:
2734 * Gets the capabilities of the media type that currently flows through @pad
2737 * This function can be used on both src and sinkpads. Note that srcpads are
2738 * always negotiated before sinkpads so it is possible that the negotiated caps
2739 * on the srcpad do not match the negotiated caps of the peer.
2741 * Returns: the negotiated #GstCaps of the pad link. Unref the caps when
2742 * you no longer need it. This function returns NULL when the @pad has no
2743 * peer or is not negotiated yet.
2748 gst_pad_get_negotiated_caps (GstPad * pad)
2753 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2755 GST_OBJECT_LOCK (pad);
2757 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2760 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2762 caps = GST_PAD_CAPS (pad);
2764 gst_caps_ref (caps);
2765 GST_OBJECT_UNLOCK (pad);
2767 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2774 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2775 GST_OBJECT_UNLOCK (pad);
2781 /* calls the buffer_alloc function on the given pad */
2782 static GstFlowReturn
2783 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2784 GstCaps * caps, GstBuffer ** buf)
2787 GstPadBufferAllocFunction bufferallocfunc;
2789 GST_OBJECT_LOCK (pad);
2790 /* when the pad is flushing we cannot give a buffer */
2791 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2794 bufferallocfunc = pad->bufferallocfunc;
2796 if (offset == GST_BUFFER_OFFSET_NONE) {
2797 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2798 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2799 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2801 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2802 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2803 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2804 bufferallocfunc, size, offset);
2806 GST_OBJECT_UNLOCK (pad);
2808 /* G_LIKELY for now since most elements don't implement a buffer alloc
2809 * function and there is no default alloc proxy function as this is usually
2811 if (G_LIKELY (bufferallocfunc == NULL))
2814 ret = bufferallocfunc (pad, offset, size, caps, buf);
2816 if (G_UNLIKELY (ret != GST_FLOW_OK))
2819 /* no error, but NULL buffer means fallback to the default */
2820 if (G_UNLIKELY (*buf == NULL))
2823 /* If the buffer alloc function didn't set up the caps like it should,
2825 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2826 GST_WARNING_OBJECT (pad,
2827 "Buffer allocation function did not set caps. Setting");
2828 gst_buffer_set_caps (*buf, caps);
2834 /* pad was flushing */
2835 GST_OBJECT_UNLOCK (pad);
2836 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2837 return GST_FLOW_WRONG_STATE;
2841 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2842 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2847 /* fallback case, allocate a buffer of our own, add pad caps. */
2848 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2850 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2851 GST_BUFFER_OFFSET (*buf) = offset;
2852 gst_buffer_set_caps (*buf, caps);
2855 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2856 "out of memory allocating %d bytes", size);
2857 return GST_FLOW_ERROR;
2862 /* FIXME 0.11: size should be unsigned */
2863 static GstFlowReturn
2864 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2865 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
2870 gboolean caps_changed;
2872 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2873 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2874 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2875 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
2877 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
2878 GST_PTR_FORMAT, offset, size, caps);
2880 GST_OBJECT_LOCK (pad);
2881 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2882 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
2885 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2888 gst_object_ref (peer);
2889 GST_OBJECT_UNLOCK (pad);
2891 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
2892 gst_object_unref (peer);
2894 if (G_UNLIKELY (ret != GST_FLOW_OK))
2897 /* FIXME, move capnego this into a base class? */
2898 newcaps = GST_BUFFER_CAPS (*buf);
2900 /* Lock for checking caps, pretty pointless as the _pad_push() function might
2901 * change it concurrently, one of the problems with automatic caps setting in
2902 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
2903 * when there is heavy renegotiation going on in both directions. */
2904 GST_OBJECT_LOCK (pad);
2905 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
2906 GST_OBJECT_UNLOCK (pad);
2908 /* we got a new datatype on the pad, see if it can handle it */
2909 if (G_UNLIKELY (caps_changed)) {
2910 GST_DEBUG_OBJECT (pad,
2911 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
2912 GST_PAD_CAPS (pad), newcaps, newcaps);
2913 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
2914 goto not_negotiated;
2917 /* sanity check (only if caps are the same) */
2918 if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
2919 goto wrong_size_fallback;
2925 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
2926 GST_OBJECT_UNLOCK (pad);
2931 /* pad has no peer */
2932 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2933 "called bufferallocfunc but had no peer");
2934 GST_OBJECT_UNLOCK (pad);
2935 return GST_FLOW_NOT_LINKED;
2939 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2940 "alloc function returned error %s", gst_flow_get_name (ret));
2945 gst_buffer_unref (*buf);
2947 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2948 "alloc function returned unacceptable buffer");
2949 return GST_FLOW_NOT_NEGOTIATED;
2951 wrong_size_fallback:
2953 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
2954 "function is too small (%u < %d), doing fallback buffer alloc",
2955 GST_BUFFER_SIZE (*buf), size);
2957 gst_buffer_unref (*buf);
2959 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2960 GST_BUFFER_OFFSET (*buf) = offset;
2961 gst_buffer_set_caps (*buf, caps);
2964 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2965 "out of memory allocating %d bytes", size);
2966 return GST_FLOW_ERROR;
2972 * gst_pad_alloc_buffer:
2973 * @pad: a source #GstPad
2974 * @offset: the offset of the new buffer in the stream
2975 * @size: the size of the new buffer
2976 * @caps: the caps of the new buffer
2977 * @buf: a newly allocated buffer
2979 * Allocates a new, empty buffer optimized to push to pad @pad. This
2980 * function only works if @pad is a source pad and has a peer.
2982 * A new, empty #GstBuffer will be put in the @buf argument.
2983 * You need to check the caps of the buffer after performing this
2984 * function and renegotiate to the format if needed. If the caps changed, it is
2985 * possible that the buffer returned in @buf is not of the right size for the
2986 * new format, @buf needs to be unreffed and reallocated if this is the case.
2988 * Returns: a result code indicating success of the operation. Any
2989 * result code other than #GST_FLOW_OK is an error and @buf should
2991 * An error can occur if the pad is not connected or when the downstream
2992 * peer elements cannot provide an acceptable buffer.
2997 /* FIXME 0.11: size should be unsigned */
2999 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3002 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3006 * gst_pad_alloc_buffer_and_set_caps:
3007 * @pad: a source #GstPad
3008 * @offset: the offset of the new buffer in the stream
3009 * @size: the size of the new buffer
3010 * @caps: the caps of the new buffer
3011 * @buf: a newly allocated buffer
3013 * In addition to the function gst_pad_alloc_buffer(), this function
3014 * automatically calls gst_pad_set_caps() when the caps of the
3015 * newly allocated buffer are different from the @pad caps.
3017 * After a renegotiation, the size of the new buffer returned in @buf could
3018 * be of the wrong size for the new format and must be unreffed an reallocated
3021 * Returns: a result code indicating success of the operation. Any
3022 * result code other than #GST_FLOW_OK is an error and @buf should
3024 * An error can occur if the pad is not connected or when the downstream
3025 * peer elements cannot provide an acceptable buffer.
3030 /* FIXME 0.11: size should be unsigned */
3032 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3033 GstCaps * caps, GstBuffer ** buf)
3035 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3039 #ifndef GST_REMOVE_DEPRECATED
3047 int_link_iter_data_free (IntLinkIterData * data)
3049 g_list_free (data->list);
3054 static GstIteratorItem
3055 iterate_pad (GstIterator * it, GstPad * pad)
3057 gst_object_ref (pad);
3058 return GST_ITERATOR_ITEM_PASS;
3062 * gst_pad_iterate_internal_links_default:
3063 * @pad: the #GstPad to get the internal links of.
3065 * Iterate the list of pads to which the given pad is linked to inside of
3066 * the parent element.
3067 * This is the default handler, and thus returns an iterator of all of the
3068 * pads inside the parent element with opposite direction.
3070 * The caller must free this iterator after use with gst_iterator_free().
3072 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3073 * returned pad with gst_object_unref().
3078 gst_pad_iterate_internal_links_default (GstPad * pad)
3085 GstIteratorDisposeFunction dispose;
3087 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3089 #ifndef GST_REMOVE_DEPRECATED
3090 /* when we get here, the default handler for the iterate links is called,
3091 * which means that the user has not installed a custom one. We first check if
3092 * there is maybe a custom legacy function we can call. */
3093 if (GST_PAD_INTLINKFUNC (pad) &&
3094 GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3095 IntLinkIterData *data;
3097 /* make an iterator for the list. We can't protect the list with a
3098 * cookie. If we would take the cookie of the parent element, we need to
3099 * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3100 * cache the per-pad list and invalidate the list when a new call to
3101 * INTLINKFUNC() returned a different list but then this would only work if
3102 * two concurrent iterators were used and the last iterator would still be
3103 * thread-unsafe. Just don't use this method anymore. */
3104 data = g_new0 (IntLinkIterData, 1);
3105 data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3108 GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3110 cookie = &data->cookie;
3111 padlist = &data->list;
3113 dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3114 /* reuse the pad lock, it's all we have here */
3115 lock = GST_OBJECT_GET_LOCK (pad);
3121 GST_OBJECT_LOCK (pad);
3122 parent = GST_PAD_PARENT (pad);
3123 if (!parent || !GST_IS_ELEMENT (parent))
3126 gst_object_ref (parent);
3127 GST_OBJECT_UNLOCK (pad);
3129 if (pad->direction == GST_PAD_SRC)
3130 padlist = &parent->sinkpads;
3132 padlist = &parent->srcpads;
3134 GST_DEBUG_OBJECT (pad, "Making iterator");
3136 cookie = &parent->pads_cookie;
3138 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3139 lock = GST_OBJECT_GET_LOCK (parent);
3142 res = gst_iterator_new_list (GST_TYPE_PAD,
3143 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3151 GST_OBJECT_UNLOCK (pad);
3152 GST_DEBUG_OBJECT (pad, "no parent element");
3158 * gst_pad_iterate_internal_links:
3159 * @pad: the GstPad to get the internal links of.
3161 * Gets an iterator for the pads to which the given pad is linked to inside
3162 * of the parent element.
3164 * Each #GstPad element yielded by the iterator will have its refcount increased,
3165 * so unref after use.
3167 * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3168 * iterator function configured. Use gst_iterator_free() after usage.
3173 gst_pad_iterate_internal_links (GstPad * pad)
3175 GstIterator *res = NULL;
3177 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3179 if (GST_PAD_ITERINTLINKFUNC (pad))
3180 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3185 #ifndef GST_REMOVE_DEPRECATED
3187 add_unref_pad_to_list (GstPad * pad, GList * list)
3189 list = g_list_prepend (list, pad);
3190 gst_object_unref (pad);
3195 * gst_pad_get_internal_links_default:
3196 * @pad: the #GstPad to get the internal links of.
3198 * Gets a list of pads to which the given pad is linked to
3199 * inside of the parent element.
3200 * This is the default handler, and thus returns a list of all of the
3201 * pads inside the parent element with opposite direction.
3203 * The caller must free this list after use with g_list_free().
3205 * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3209 * Deprecated: This function does not ref the pads in the list so that they
3210 * could become invalid by the time the application accesses them. It's also
3211 * possible that the list changes while handling the pads, which the caller of
3212 * this function is unable to know. Use the thread-safe
3213 * gst_pad_iterate_internal_links_default() instead.
3215 #ifndef GST_REMOVE_DEPRECATED
3217 gst_pad_get_internal_links_default (GstPad * pad)
3222 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3224 GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3226 /* when we get here, the default handler for get_internal_links is called,
3227 * which means that the user has not installed a custom one. We first check if
3228 * there is maybe a custom iterate function we can call. */
3229 if (GST_PAD_ITERINTLINKFUNC (pad) &&
3230 GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3232 GstIteratorResult ires;
3233 gboolean done = FALSE;
3235 it = gst_pad_iterate_internal_links (pad);
3236 /* loop over the iterator and put all elements into a list, we also
3237 * immediatly unref them, which is bad. */
3239 ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, res);
3241 case GST_ITERATOR_OK:
3242 case GST_ITERATOR_DONE:
3243 case GST_ITERATOR_ERROR:
3246 case GST_ITERATOR_RESYNC:
3247 /* restart, discard previous list */
3248 gst_iterator_resync (it);
3255 gst_iterator_free (it);
3257 /* lock pad, check and ref parent */
3258 GST_OBJECT_LOCK (pad);
3259 parent = GST_PAD_PARENT (pad);
3260 if (!parent || !GST_IS_ELEMENT (parent))
3263 parent = gst_object_ref (parent);
3264 GST_OBJECT_UNLOCK (pad);
3266 /* now lock the parent while we copy the pads */
3267 GST_OBJECT_LOCK (parent);
3268 if (pad->direction == GST_PAD_SRC)
3269 res = g_list_copy (parent->sinkpads);
3271 res = g_list_copy (parent->srcpads);
3272 GST_OBJECT_UNLOCK (parent);
3274 gst_object_unref (parent);
3277 /* At this point pads can be changed and unreffed. Nothing we can do about it
3278 * because for compatibility reasons this function cannot ref the pads or
3279 * notify the app that the list changed. */
3285 GST_DEBUG_OBJECT (pad, "no parent");
3286 GST_OBJECT_UNLOCK (pad);
3290 #endif /* GST_REMOVE_DEPRECATED */
3293 * gst_pad_get_internal_links:
3294 * @pad: the #GstPad to get the internal links of.
3296 * Gets a list of pads to which the given pad is linked to
3297 * inside of the parent element.
3298 * The caller must free this list after use.
3300 * Returns: a newly allocated #GList of pads, free with g_list_free().
3304 * Deprecated: This function does not ref the pads in the list so that they
3305 * could become invalid by the time the application accesses them. It's also
3306 * possible that the list changes while handling the pads, which the caller of
3307 * this function is unable to know. Use the thread-safe
3308 * gst_pad_iterate_internal_links() instead.
3310 #ifndef GST_REMOVE_DEPRECATED
3312 gst_pad_get_internal_links (GstPad * pad)
3316 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3318 GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3320 if (GST_PAD_INTLINKFUNC (pad))
3321 res = GST_PAD_INTLINKFUNC (pad) (pad);
3325 #endif /* GST_REMOVE_DEPRECATED */
3328 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3330 gboolean result = FALSE;
3332 gboolean done = FALSE;
3335 GList *pushed_pads = NULL;
3337 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3338 event, GST_EVENT_TYPE_NAME (event));
3340 iter = gst_pad_iterate_internal_links (pad);
3346 switch (gst_iterator_next (iter, &item)) {
3347 case GST_ITERATOR_OK:
3348 eventpad = GST_PAD (item);
3350 /* if already pushed, skip */
3351 if (g_list_find (pushed_pads, eventpad)) {
3352 gst_object_unref (item);
3356 if (GST_PAD_IS_SRC (eventpad)) {
3357 /* for each pad we send to, we should ref the event; it's up
3358 * to downstream to unref again when handled. */
3359 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3360 event, GST_EVENT_TYPE_NAME (event),
3361 GST_DEBUG_PAD_NAME (eventpad));
3362 gst_event_ref (event);
3363 result |= gst_pad_push_event (eventpad, event);
3365 /* we only send the event on one pad, multi-sinkpad elements
3366 * should implement a handler */
3367 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3368 event, GST_EVENT_TYPE_NAME (event),
3369 GST_DEBUG_PAD_NAME (eventpad));
3370 result = gst_pad_push_event (eventpad, event);
3375 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3377 gst_object_unref (item);
3379 case GST_ITERATOR_RESYNC:
3380 /* FIXME, if we want to reset the result value we need to remember which
3381 * pads pushed with which result */
3382 gst_iterator_resync (iter);
3384 case GST_ITERATOR_ERROR:
3385 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3388 case GST_ITERATOR_DONE:
3393 gst_iterator_free (iter);
3397 /* If this is a sinkpad and we don't have pads to send the event to, we
3398 * return TRUE. This is so that when using the default handler on a sink
3399 * element, we don't fail to push it. */
3401 result = GST_PAD_IS_SINK (pad);
3403 g_list_free (pushed_pads);
3405 /* we handled the incoming event so we unref once */
3407 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3408 gst_event_unref (event);
3415 * gst_pad_event_default:
3416 * @pad: a #GstPad to call the default event handler on.
3417 * @event: the #GstEvent to handle.
3419 * Invokes the default event handler for the given pad. End-of-stream and
3420 * discontinuity events are handled specially, and then the event is sent to all
3421 * pads internally linked to @pad. Note that if there are many possible sink
3422 * pads that are internally linked to @pad, only one will be sent an event.
3423 * Multi-sinkpad elements should implement custom event handlers.
3425 * Returns: TRUE if the event was sent succesfully.
3428 gst_pad_event_default (GstPad * pad, GstEvent * event)
3430 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3431 g_return_val_if_fail (event != NULL, FALSE);
3433 switch (GST_EVENT_TYPE (event)) {
3436 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3437 gst_pad_pause_task (pad);
3444 return gst_pad_event_default_dispatch (pad, event);
3448 * gst_pad_dispatcher:
3449 * @pad: a #GstPad to dispatch.
3450 * @dispatch: the #GstDispatcherFunction to call.
3451 * @data: gpointer user data passed to the dispatcher function.
3453 * Invokes the given dispatcher function on each respective peer of
3454 * all pads that are internally linked to the given pad.
3455 * The GstPadDispatcherFunction should return TRUE when no further pads
3456 * need to be processed.
3458 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3461 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3464 gboolean res = FALSE;
3465 GstIterator *iter = NULL;
3466 gboolean done = FALSE;
3469 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3470 g_return_val_if_fail (dispatch != NULL, FALSE);
3472 iter = gst_pad_iterate_internal_links (pad);
3478 switch (gst_iterator_next (iter, &item)) {
3479 case GST_ITERATOR_OK:
3481 GstPad *int_pad = GST_PAD_CAST (item);
3482 GstPad *int_peer = gst_pad_get_peer (int_pad);
3485 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3486 GST_DEBUG_PAD_NAME (int_peer));
3487 done = res = dispatch (int_peer, data);
3488 gst_object_unref (int_peer);
3490 GST_DEBUG_OBJECT (int_pad, "no peer");
3493 gst_object_unref (item);
3495 case GST_ITERATOR_RESYNC:
3496 gst_iterator_resync (iter);
3498 case GST_ITERATOR_ERROR:
3500 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3502 case GST_ITERATOR_DONE:
3507 gst_iterator_free (iter);
3509 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3518 * @pad: a #GstPad to invoke the default query on.
3519 * @query: the #GstQuery to perform.
3521 * Dispatches a query to a pad. The query should have been allocated by the
3522 * caller via one of the type-specific allocation functions in gstquery.h. The
3523 * element is responsible for filling the query with an appropriate response,
3524 * which should then be parsed with a type-specific query parsing function.
3526 * Again, the caller is responsible for both the allocation and deallocation of
3527 * the query structure.
3529 * Returns: TRUE if the query could be performed.
3532 gst_pad_query (GstPad * pad, GstQuery * query)
3534 GstPadQueryFunction func;
3536 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3537 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3539 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3541 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3544 return func (pad, query);
3548 GST_DEBUG_OBJECT (pad, "had no query function");
3554 * gst_pad_peer_query:
3555 * @pad: a #GstPad to invoke the peer query on.
3556 * @query: the #GstQuery to perform.
3558 * Performs gst_pad_query() on the peer of @pad.
3560 * The caller is responsible for both the allocation and deallocation of
3561 * the query structure.
3563 * Returns: TRUE if the query could be performed. This function returns %FALSE
3564 * if @pad has no peer.
3569 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3574 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3575 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3577 GST_OBJECT_LOCK (pad);
3579 GST_DEBUG_OBJECT (pad, "peer query");
3581 peerpad = GST_PAD_PEER (pad);
3582 if (G_UNLIKELY (peerpad == NULL))
3585 gst_object_ref (peerpad);
3586 GST_OBJECT_UNLOCK (pad);
3588 result = gst_pad_query (peerpad, query);
3590 gst_object_unref (peerpad);
3597 GST_WARNING_OBJECT (pad, "pad has no peer");
3598 GST_OBJECT_UNLOCK (pad);
3604 * gst_pad_query_default:
3605 * @pad: a #GstPad to call the default query handler on.
3606 * @query: the #GstQuery to handle.
3608 * Invokes the default query handler for the given pad.
3609 * The query is sent to all pads internally linked to @pad. Note that
3610 * if there are many possible sink pads that are internally linked to
3611 * @pad, only one will be sent the query.
3612 * Multi-sinkpad elements should implement custom query handlers.
3614 * Returns: TRUE if the query was performed succesfully.
3617 gst_pad_query_default (GstPad * pad, GstQuery * query)
3619 switch (GST_QUERY_TYPE (query)) {
3620 case GST_QUERY_POSITION:
3621 case GST_QUERY_SEEKING:
3622 case GST_QUERY_FORMATS:
3623 case GST_QUERY_LATENCY:
3624 case GST_QUERY_JITTER:
3625 case GST_QUERY_RATE:
3626 case GST_QUERY_CONVERT:
3628 return gst_pad_dispatcher
3629 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3633 #ifndef GST_DISABLE_LOADSAVE
3634 /* FIXME: why isn't this on a GstElement ? */
3636 * gst_pad_load_and_link:
3637 * @self: an #xmlNodePtr to read the description from.
3638 * @parent: the #GstObject element that owns the pad.
3640 * Reads the pad definition from the XML node and links the given pad
3641 * in the element to a pad of an element up in the hierarchy.
3644 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3646 xmlNodePtr field = self->xmlChildrenNode;
3647 GstPad *pad = NULL, *targetpad;
3651 GstObject *grandparent;
3655 if (!strcmp ((char *) field->name, "name")) {
3656 name = (gchar *) xmlNodeGetContent (field);
3657 pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3659 pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3661 } else if (!strcmp ((char *) field->name, "peer")) {
3662 peer = (gchar *) xmlNodeGetContent (field);
3664 field = field->next;
3666 g_return_if_fail (pad != NULL);
3671 split = g_strsplit (peer, ".", 2);
3673 if (split[0] == NULL || split[1] == NULL) {
3674 GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3675 "Could not parse peer '%s', leaving unlinked", peer);
3682 g_return_if_fail (split[0] != NULL);
3683 g_return_if_fail (split[1] != NULL);
3685 grandparent = gst_object_get_parent (parent);
3687 if (grandparent && GST_IS_BIN (grandparent)) {
3688 target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3695 targetpad = gst_element_get_static_pad (target, split[1]);
3697 targetpad = gst_element_get_request_pad (target, split[1]);
3699 if (targetpad == NULL)
3702 gst_pad_link (pad, targetpad);
3709 * gst_pad_save_thyself:
3710 * @pad: a #GstPad to save.
3711 * @parent: the parent #xmlNodePtr to save the description in.
3713 * Saves the pad into an xml representation.
3715 * Returns: the #xmlNodePtr representation of the pad.
3718 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3723 g_return_val_if_fail (GST_IS_PAD (object), NULL);
3725 pad = GST_PAD (object);
3727 xmlNewChild (parent, NULL, (xmlChar *) "name",
3728 (xmlChar *) GST_PAD_NAME (pad));
3730 if (GST_PAD_IS_SRC (pad)) {
3731 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3732 } else if (GST_PAD_IS_SINK (pad)) {
3733 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3735 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3738 if (GST_PAD_PEER (pad) != NULL) {
3741 peer = GST_PAD_PEER (pad);
3742 /* first check to see if the peer's parent's parent is the same */
3743 /* we just save it off */
3744 content = g_strdup_printf ("%s.%s",
3745 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3746 xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3749 xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3756 * gst_ghost_pad_save_thyself:
3757 * @pad: a ghost #GstPad to save.
3758 * @parent: the parent #xmlNodePtr to save the description in.
3760 * Saves the ghost pad into an xml representation.
3762 * Returns: the #xmlNodePtr representation of the pad.
3765 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3769 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3771 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3772 xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3773 xmlNewChild (self, NULL, (xmlChar *) "parent",
3774 (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3776 /* FIXME FIXME FIXME! */
3781 #endif /* GST_DISABLE_LOADSAVE */
3784 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3785 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3788 * This function performs the pad blocking when an event, buffer push
3789 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3790 * streaming thread after informing the pad has been blocked.
3792 * An application can with this method wait and block any streaming
3793 * thread and perform operations such as seeking or linking.
3795 * Two methods are available for notifying the application of the
3797 * - the callback method, which happens in the STREAMING thread with
3798 * the STREAM_LOCK held. With this method, the most useful way of
3799 * dealing with the callback is to post a message to the main thread
3800 * where the pad block can then be handled outside of the streaming
3801 * thread. With the last method one can perform all operations such
3802 * as doing a state change, linking, unblocking, seeking etc on the
3804 * - the GCond signal method, which makes any thread unblock when
3805 * the pad block happens.
3807 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3808 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3812 static GstFlowReturn
3813 handle_pad_block (GstPad * pad)
3815 GstPadBlockCallback callback;
3817 GstFlowReturn ret = GST_FLOW_OK;
3819 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3821 /* flushing, don't bother trying to block and return WRONG_STATE
3823 if (GST_PAD_IS_FLUSHING (pad))
3824 goto flushingnonref;
3826 /* we grab an extra ref for the callbacks, this is probably not
3827 * needed (callback code does not have a ref and cannot unref). I
3828 * think this was done to make it possible to unref the element in
3829 * the callback, which is in the end totally impossible as it
3830 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3831 * all taken when calling this function. */
3832 gst_object_ref (pad);
3834 while (GST_PAD_IS_BLOCKED (pad)) {
3836 /* we either have a callback installed to notify the block or
3837 * some other thread is doing a GCond wait. */
3838 callback = pad->block_callback;
3839 pad->abidata.ABI.block_callback_called = TRUE;
3841 /* there is a callback installed, call it. We release the
3842 * lock so that the callback can do something usefull with the
3844 user_data = pad->block_data;
3845 GST_OBJECT_UNLOCK (pad);
3846 callback (pad, TRUE, user_data);
3847 GST_OBJECT_LOCK (pad);
3849 /* we released the lock, recheck flushing */
3850 if (GST_PAD_IS_FLUSHING (pad))
3853 /* no callback, signal the thread that is doing a GCond wait
3855 GST_PAD_BLOCK_BROADCAST (pad);
3857 } while (pad->abidata.ABI.block_callback_called == FALSE
3858 && GST_PAD_IS_BLOCKED (pad));
3860 /* OBJECT_LOCK could have been released when we did the callback, which
3861 * then could have made the pad unblock so we need to check the blocking
3862 * condition again. */
3863 if (!GST_PAD_IS_BLOCKED (pad))
3866 /* now we block the streaming thread. It can be unlocked when we
3867 * deactivate the pad (which will also set the FLUSHING flag) or
3868 * when the pad is unblocked. A flushing event will also unblock
3869 * the pad after setting the FLUSHING flag. */
3870 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3871 "Waiting to be unblocked or set flushing");
3872 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3873 GST_PAD_BLOCK_WAIT (pad);
3874 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3876 /* see if we got unblocked by a flush or not */
3877 if (GST_PAD_IS_FLUSHING (pad))
3881 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3883 /* when we get here, the pad is unblocked again and we perform
3884 * the needed unblock code. */
3885 callback = pad->block_callback;
3887 /* we need to call the callback */
3888 user_data = pad->block_data;
3889 GST_OBJECT_UNLOCK (pad);
3890 callback (pad, FALSE, user_data);
3891 GST_OBJECT_LOCK (pad);
3893 /* we need to signal the thread waiting on the GCond */
3894 GST_PAD_BLOCK_BROADCAST (pad);
3897 gst_object_unref (pad);
3903 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3904 return GST_FLOW_WRONG_STATE;
3908 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3909 gst_object_unref (pad);
3910 return GST_FLOW_WRONG_STATE;
3914 /**********************************************************************
3915 * Data passing functions
3919 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3922 GValue args[2] = { {0}, {0} };
3927 g_value_init (&ret, G_TYPE_BOOLEAN);
3928 g_value_set_boolean (&ret, TRUE);
3929 g_value_init (&args[0], GST_TYPE_PAD);
3930 g_value_set_object (&args[0], pad);
3931 g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
3932 gst_value_set_mini_object (&args[1], obj);
3934 if (GST_IS_EVENT (obj))
3935 detail = event_quark;
3937 detail = buffer_quark;
3940 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3941 res = g_value_get_boolean (&ret);
3944 g_value_unset (&ret);
3945 g_value_unset (&args[0]);
3946 g_value_unset (&args[1]);
3952 gst_pad_data_unref (gboolean is_buffer, void *data)
3954 if (G_LIKELY (is_buffer)) {
3955 gst_buffer_unref (data);
3957 gst_buffer_list_unref (data);
3962 gst_pad_data_get_caps (gboolean is_buffer, void *data)
3966 if (G_LIKELY (is_buffer)) {
3967 caps = GST_BUFFER_CAPS (data);
3969 GstBufferListIterator *it;
3973 it = gst_buffer_list_iterate (GST_BUFFER_LIST_CAST (data));
3974 if (gst_buffer_list_iterator_next_group (it)) {
3975 buf = gst_buffer_list_iterator_next (it);
3977 caps = GST_BUFFER_CAPS (buf);
3980 gst_buffer_list_iterator_free (it);
3985 /* this is the chain function that does not perform the additional argument
3986 * checking for that little extra speed.
3988 static inline GstFlowReturn
3989 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
3992 gboolean caps_changed;
3994 gboolean emit_signal;
3996 GST_PAD_STREAM_LOCK (pad);
3998 GST_OBJECT_LOCK (pad);
3999 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4002 caps = gst_pad_data_get_caps (is_buffer, data);
4003 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4005 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4006 GST_OBJECT_UNLOCK (pad);
4008 /* see if the signal should be emited, we emit before caps nego as
4009 * we might drop the buffer and do capsnego for nothing. */
4010 if (G_UNLIKELY (emit_signal)) {
4011 if (G_LIKELY (is_buffer)) {
4012 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4015 /* chain all groups in the buffer list one by one to avoid problems with
4016 * buffer probes that push buffers or events */
4021 /* we got a new datatype on the pad, see if it can handle it */
4022 if (G_UNLIKELY (caps_changed)) {
4023 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4024 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4025 goto not_negotiated;
4028 /* NOTE: we read the chainfunc unlocked.
4029 * we cannot hold the lock for the pad so we might send
4030 * the data to the wrong function. This is not really a
4031 * problem since functions are assigned at creation time
4032 * and don't change that often... */
4033 if (G_LIKELY (is_buffer)) {
4034 GstPadChainFunction chainfunc;
4036 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4039 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4040 "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4042 ret = chainfunc (pad, GST_BUFFER_CAST (data));
4044 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4045 "called chainfunction &%s, returned %s",
4046 GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4048 GstPadChainListFunction chainlistfunc;
4050 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4053 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4054 "calling chainlistfunction &%s",
4055 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4057 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4059 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4060 "called chainlistfunction &%s, returned %s",
4061 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4064 GST_PAD_STREAM_UNLOCK (pad);
4070 GstBufferList *list;
4071 GstBufferListIterator *it;
4074 GST_PAD_STREAM_UNLOCK (pad);
4076 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4078 list = GST_BUFFER_LIST_CAST (data);
4079 it = gst_buffer_list_iterate (list);
4082 if (gst_buffer_list_iterator_next_group (it)) {
4084 group = gst_buffer_list_iterator_merge_group (it);
4085 if (group == NULL) {
4086 group = gst_buffer_new ();
4087 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4089 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4091 ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4092 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4094 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4095 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4098 gst_buffer_list_iterator_free (it);
4099 gst_buffer_list_unref (list);
4107 gst_pad_data_unref (is_buffer, data);
4108 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4109 "pushing, but pad was flushing");
4110 GST_OBJECT_UNLOCK (pad);
4111 GST_PAD_STREAM_UNLOCK (pad);
4112 return GST_FLOW_WRONG_STATE;
4116 gst_pad_data_unref (is_buffer, data);
4117 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4118 GST_PAD_STREAM_UNLOCK (pad);
4123 gst_pad_data_unref (is_buffer, data);
4124 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4125 "pushing data but pad did not accept");
4126 GST_PAD_STREAM_UNLOCK (pad);
4127 return GST_FLOW_NOT_NEGOTIATED;
4131 gst_pad_data_unref (is_buffer, data);
4132 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4133 "pushing, but not chainhandler");
4134 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4135 ("push on pad %s:%s but it has no chainfunction",
4136 GST_DEBUG_PAD_NAME (pad)));
4137 GST_PAD_STREAM_UNLOCK (pad);
4138 return GST_FLOW_NOT_SUPPORTED;
4144 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4145 * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4147 * Chain a buffer to @pad.
4149 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4151 * If the caps on @buffer are different from the current caps on @pad, this
4152 * function will call any setcaps function (see gst_pad_set_setcaps_function())
4153 * installed on @pad. If the new caps are not acceptable for @pad, this
4154 * function returns #GST_FLOW_NOT_NEGOTIATED.
4156 * The function proceeds calling the chain function installed on @pad (see
4157 * gst_pad_set_chain_function()) and the return value of that function is
4158 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4161 * In all cases, success or failure, the caller loses its reference to @buffer
4162 * after calling this function.
4164 * Returns: a #GstFlowReturn from the pad.
4169 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4171 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4172 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4173 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4175 return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4179 * gst_pad_chain_list:
4180 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4181 * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4183 * Chain a bufferlist to @pad.
4185 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4187 * If the caps on the first buffer of @list are different from the current
4188 * caps on @pad, this function will call any setcaps function
4189 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4190 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4192 * The function proceeds calling the chainlist function installed on @pad (see
4193 * gst_pad_set_chain_list_function()) and the return value of that function is
4194 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4195 * chainlist function.
4197 * In all cases, success or failure, the caller loses its reference to @list
4198 * after calling this function.
4200 * Returns: a #GstFlowReturn from the pad.
4207 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4209 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4210 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4211 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4213 return gst_pad_chain_data_unchecked (pad, FALSE, list);
4216 static GstFlowReturn
4217 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4222 gboolean caps_changed;
4224 GST_OBJECT_LOCK (pad);
4226 /* FIXME: this check can go away; pad_set_blocked could be implemented with
4227 * probes completely or probes with an extended pad block. */
4228 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4229 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4232 /* we emit signals on the pad arg, the peer will have a chance to
4233 * emit in the _chain() function */
4234 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4235 /* unlock before emitting */
4236 GST_OBJECT_UNLOCK (pad);
4238 if (G_LIKELY (is_buffer)) {
4239 /* if the signal handler returned FALSE, it means we should just drop the
4241 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4244 /* push all buffers in the list */
4247 GST_OBJECT_LOCK (pad);
4250 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4253 /* Before pushing the buffer to the peer pad, ensure that caps
4254 * are set on this pad */
4255 caps = gst_pad_data_get_caps (is_buffer, data);
4256 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4258 /* take ref to peer pad before releasing the lock */
4259 gst_object_ref (peer);
4261 GST_OBJECT_UNLOCK (pad);
4263 /* we got a new datatype from the pad, it had better handle it */
4264 if (G_UNLIKELY (caps_changed)) {
4265 GST_DEBUG_OBJECT (pad,
4266 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4267 GST_PAD_CAPS (pad), caps, caps);
4268 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4269 goto not_negotiated;
4272 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4274 gst_object_unref (peer);
4280 GstBufferList *list;
4281 GstBufferListIterator *it;
4284 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4286 list = GST_BUFFER_LIST_CAST (data);
4287 it = gst_buffer_list_iterate (list);
4290 if (gst_buffer_list_iterator_next_group (it)) {
4292 group = gst_buffer_list_iterator_merge_group (it);
4293 if (group == NULL) {
4294 group = gst_buffer_new ();
4295 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4297 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4299 ret = gst_pad_push_data (pad, TRUE, group);
4300 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4302 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4303 ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4306 gst_buffer_list_iterator_free (it);
4307 gst_buffer_list_unref (list);
4312 /* ERROR recovery here */
4315 gst_pad_data_unref (is_buffer, data);
4316 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4317 GST_OBJECT_UNLOCK (pad);
4322 gst_pad_data_unref (is_buffer, data);
4323 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4328 gst_pad_data_unref (is_buffer, data);
4329 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4330 "pushing, but it was not linked");
4331 GST_OBJECT_UNLOCK (pad);
4332 return GST_FLOW_NOT_LINKED;
4336 gst_pad_data_unref (is_buffer, data);
4337 gst_object_unref (peer);
4338 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4339 "element pushed data then refused to accept the caps");
4340 return GST_FLOW_NOT_NEGOTIATED;
4346 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4347 * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4349 * Pushes a buffer to the peer of @pad.
4351 * This function will call an installed pad block before triggering any
4352 * installed pad probes.
4354 * If the caps on @buffer are different from the currently configured caps on
4355 * @pad, this function will call any installed setcaps function on @pad (see
4356 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4357 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4359 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4360 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4363 * In all cases, success or failure, the caller loses its reference to @buffer
4364 * after calling this function.
4366 * Returns: a #GstFlowReturn from the peer pad.
4371 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4373 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4374 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4375 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4377 return gst_pad_push_data (pad, TRUE, buffer);
4381 * gst_pad_push_list:
4382 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4383 * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4385 * Pushes a buffer list to the peer of @pad.
4387 * This function will call an installed pad block before triggering any
4388 * installed pad probes.
4390 * If the caps on the first buffer in the first group of @list are different
4391 * from the currently configured caps on @pad, this function will call any
4392 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4393 * case of failure to renegotiate the new format, this function returns
4394 * #GST_FLOW_NOT_NEGOTIATED.
4396 * If there are any probes installed on @pad every group of the buffer list
4397 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4398 * buffer list will be unreffed.
4400 * The function proceeds calling the chain function on the peer pad and returns
4401 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4402 * be returned. If the peer pad does not have any installed chainlist function
4403 * every group buffer of the list will be merged into a normal #GstBuffer and
4404 * chained via gst_pad_chain().
4406 * In all cases, success or failure, the caller loses its reference to @list
4407 * after calling this function.
4409 * Returns: a #GstFlowReturn from the peer pad.
4416 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4418 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4419 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4420 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4422 return gst_pad_push_data (pad, FALSE, list);
4426 * gst_pad_check_pull_range:
4427 * @pad: a sink #GstPad.
4429 * Checks if a gst_pad_pull_range() can be performed on the peer
4430 * source pad. This function is used by plugins that want to check
4431 * if they can use random access on the peer source pad.
4433 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4434 * if it needs to perform some logic to determine if pull_range is
4437 * Returns: a gboolean with the result.
4442 gst_pad_check_pull_range (GstPad * pad)
4446 GstPadCheckGetRangeFunction checkgetrangefunc;
4448 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4450 GST_OBJECT_LOCK (pad);
4451 if (!GST_PAD_IS_SINK (pad))
4452 goto wrong_direction;
4454 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4457 gst_object_ref (peer);
4458 GST_OBJECT_UNLOCK (pad);
4460 /* see note in above function */
4461 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4462 /* FIXME, kindoff ghetto */
4463 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4464 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4465 "no checkgetrangefunc, assuming %d", ret);
4467 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4468 "calling checkgetrangefunc %s of peer pad %s:%s",
4469 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4471 ret = checkgetrangefunc (peer);
4474 gst_object_unref (peer);
4478 /* ERROR recovery here */
4481 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4482 "checking pull range, but pad must be a sinkpad");
4483 GST_OBJECT_UNLOCK (pad);
4488 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4489 "checking pull range, but it was not linked");
4490 GST_OBJECT_UNLOCK (pad);
4496 * gst_pad_get_range:
4497 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4498 * @offset: The start offset of the buffer
4499 * @size: The length of the buffer
4500 * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4502 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4505 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4506 * description of a getrange function. If @pad has no getrange function
4507 * installed (see gst_pad_set_getrange_function()) this function returns
4508 * #GST_FLOW_NOT_SUPPORTED.
4510 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4512 * Returns: a #GstFlowReturn from the pad.
4517 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4518 GstBuffer ** buffer)
4521 GstPadGetRangeFunction getrangefunc;
4522 gboolean emit_signal;
4524 gboolean caps_changed;
4526 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4527 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4528 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4530 GST_PAD_STREAM_LOCK (pad);
4532 GST_OBJECT_LOCK (pad);
4533 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4536 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4537 GST_OBJECT_UNLOCK (pad);
4539 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4542 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4543 "calling getrangefunc %s, offset %"
4544 G_GUINT64_FORMAT ", size %u",
4545 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4547 ret = getrangefunc (pad, offset, size, buffer);
4549 /* can only fire the signal if we have a valid buffer */
4550 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4551 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4555 GST_PAD_STREAM_UNLOCK (pad);
4557 if (G_UNLIKELY (ret != GST_FLOW_OK))
4558 goto get_range_failed;
4560 GST_OBJECT_LOCK (pad);
4561 /* Before pushing the buffer to the peer pad, ensure that caps
4562 * are set on this pad */
4563 caps = GST_BUFFER_CAPS (*buffer);
4564 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4565 GST_OBJECT_UNLOCK (pad);
4567 if (G_UNLIKELY (caps_changed)) {
4568 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4569 /* this should usually work because the element produced the buffer */
4570 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4571 goto not_negotiated;
4578 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4579 "pulling range, but pad was flushing");
4580 GST_OBJECT_UNLOCK (pad);
4581 GST_PAD_STREAM_UNLOCK (pad);
4582 return GST_FLOW_WRONG_STATE;
4586 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4587 ("pullrange on pad %s:%s but it has no getrangefunction",
4588 GST_DEBUG_PAD_NAME (pad)));
4589 GST_PAD_STREAM_UNLOCK (pad);
4590 return GST_FLOW_NOT_SUPPORTED;
4594 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4595 "Dropping data after FALSE probe return");
4596 GST_PAD_STREAM_UNLOCK (pad);
4597 gst_buffer_unref (*buffer);
4599 return GST_FLOW_UNEXPECTED;
4604 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4605 "getrange failed %s", gst_flow_get_name (ret));
4610 gst_buffer_unref (*buffer);
4612 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4613 "getrange returned buffer of unaccaptable caps");
4614 return GST_FLOW_NOT_NEGOTIATED;
4620 * gst_pad_pull_range:
4621 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4622 * @offset: The start offset of the buffer
4623 * @size: The length of the buffer
4624 * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4626 * Pulls a @buffer from the peer pad.
4628 * This function will first trigger the pad block signal if it was
4631 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4632 * function returns the result of gst_pad_get_range() on the peer pad.
4633 * See gst_pad_get_range() for a list of return values and for the
4634 * semantics of the arguments of this function.
4636 * @buffer's caps must either be unset or the same as what is already
4637 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4640 * Returns: a #GstFlowReturn from the peer pad.
4641 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4642 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4643 * @buffer may not be used or freed when any other return value than
4644 * #GST_FLOW_OK is returned.
4649 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4650 GstBuffer ** buffer)
4654 gboolean emit_signal;
4656 gboolean caps_changed;
4658 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4659 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4660 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4662 GST_OBJECT_LOCK (pad);
4664 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4665 handle_pad_block (pad);
4667 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4670 /* signal emision for the pad, peer has chance to emit when
4671 * we call _get_range() */
4672 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4674 gst_object_ref (peer);
4675 GST_OBJECT_UNLOCK (pad);
4677 ret = gst_pad_get_range (peer, offset, size, buffer);
4679 gst_object_unref (peer);
4681 if (G_UNLIKELY (ret != GST_FLOW_OK))
4682 goto pull_range_failed;
4684 /* can only fire the signal if we have a valid buffer */
4685 if (G_UNLIKELY (emit_signal)) {
4686 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4690 GST_OBJECT_LOCK (pad);
4691 /* Before pushing the buffer to the peer pad, ensure that caps
4692 * are set on this pad */
4693 caps = GST_BUFFER_CAPS (*buffer);
4694 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4695 GST_OBJECT_UNLOCK (pad);
4697 /* we got a new datatype on the pad, see if it can handle it */
4698 if (G_UNLIKELY (caps_changed)) {
4699 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4700 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4701 goto not_negotiated;
4705 /* ERROR recovery here */
4708 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4709 "pulling range, but it was not linked");
4710 GST_OBJECT_UNLOCK (pad);
4711 return GST_FLOW_NOT_LINKED;
4716 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4717 "pullrange failed %s", gst_flow_get_name (ret));
4722 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4723 "Dropping data after FALSE probe return");
4724 gst_buffer_unref (*buffer);
4726 return GST_FLOW_UNEXPECTED;
4730 gst_buffer_unref (*buffer);
4732 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4733 "pullrange returned buffer of different caps");
4734 return GST_FLOW_NOT_NEGOTIATED;
4739 * gst_pad_push_event:
4740 * @pad: a #GstPad to push the event to.
4741 * @event: the #GstEvent to send to the pad.
4743 * Sends the event to the peer of the given pad. This function is
4744 * mainly used by elements to send events to their peer
4747 * This function takes owership of the provided event so you should
4748 * gst_event_ref() it if you want to reuse the event after this call.
4750 * Returns: TRUE if the event was handled.
4755 gst_pad_push_event (GstPad * pad, GstEvent * event)
4760 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4761 g_return_val_if_fail (event != NULL, FALSE);
4762 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4764 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4766 GST_OBJECT_LOCK (pad);
4768 /* Two checks to be made:
4769 * . (un)set the FLUSHING flag for flushing events,
4770 * . handle pad blocking */
4771 switch (GST_EVENT_TYPE (event)) {
4772 case GST_EVENT_FLUSH_START:
4773 GST_PAD_SET_FLUSHING (pad);
4775 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4776 /* flush start will have set the FLUSHING flag and will then
4777 * unlock all threads doing a GCond wait on the blocking pad. This
4778 * will typically unblock the STREAMING thread blocked on a pad. */
4779 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4780 "doing block signal.");
4781 GST_PAD_BLOCK_BROADCAST (pad);
4785 case GST_EVENT_FLUSH_STOP:
4786 GST_PAD_UNSET_FLUSHING (pad);
4788 /* if we are blocked, flush away the FLUSH_STOP event */
4789 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4790 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4795 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4796 /* block the event as long as the pad is blocked */
4797 if (handle_pad_block (pad) != GST_FLOW_OK)
4803 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4804 GST_OBJECT_UNLOCK (pad);
4806 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4809 GST_OBJECT_LOCK (pad);
4811 peerpad = GST_PAD_PEER (pad);
4812 if (peerpad == NULL)
4815 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4816 GST_EVENT_TYPE_NAME (event), peerpad);
4817 gst_object_ref (peerpad);
4818 GST_OBJECT_UNLOCK (pad);
4820 result = gst_pad_send_event (peerpad, event);
4822 /* Note: we gave away ownership of the event at this point */
4823 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4825 gst_object_unref (peerpad);
4829 /* ERROR handling */
4832 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4833 gst_event_unref (event);
4838 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4839 gst_event_unref (event);
4840 GST_OBJECT_UNLOCK (pad);
4845 GST_DEBUG_OBJECT (pad,
4846 "Not forwarding event since we're flushing and blocking");
4847 gst_event_unref (event);
4848 GST_OBJECT_UNLOCK (pad);
4854 * gst_pad_send_event:
4855 * @pad: a #GstPad to send the event to.
4856 * @event: the #GstEvent to send to the pad.
4858 * Sends the event to the pad. This function can be used
4859 * by applications to send events in the pipeline.
4861 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4862 * sink pad, @event should be a downstream event. For example, you would not
4863 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4864 * Furthermore, some downstream events have to be serialized with data flow,
4865 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4866 * the event needs to be serialized with data flow, this function will take the
4867 * pad's stream lock while calling its event function.
4869 * To find out whether an event type is upstream, downstream, or downstream and
4870 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4871 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4872 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4873 * plugin doesn't need to bother itself with this information; the core handles
4874 * all necessary locks and checks.
4876 * This function takes owership of the provided event so you should
4877 * gst_event_ref() it if you want to reuse the event after this call.
4879 * Returns: TRUE if the event was handled.
4882 gst_pad_send_event (GstPad * pad, GstEvent * event)
4884 gboolean result = FALSE;
4885 GstPadEventFunction eventfunc;
4886 gboolean serialized, need_unlock = FALSE;
4888 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4889 g_return_val_if_fail (event != NULL, FALSE);
4891 GST_OBJECT_LOCK (pad);
4892 if (GST_PAD_IS_SINK (pad)) {
4893 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4894 goto wrong_direction;
4895 serialized = GST_EVENT_IS_SERIALIZED (event);
4896 } else if (GST_PAD_IS_SRC (pad)) {
4897 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4898 goto wrong_direction;
4899 /* events on srcpad never are serialized */
4902 goto unknown_direction;
4904 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4905 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4906 GST_EVENT_SRC (event) = gst_object_ref (pad);
4910 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4911 GST_OBJECT_UNLOCK (pad);
4913 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4916 GST_OBJECT_LOCK (pad);
4919 switch (GST_EVENT_TYPE (event)) {
4920 case GST_EVENT_FLUSH_START:
4921 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4922 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4924 /* can't even accept a flush begin event when flushing */
4925 if (GST_PAD_IS_FLUSHING (pad))
4927 GST_PAD_SET_FLUSHING (pad);
4928 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4930 case GST_EVENT_FLUSH_STOP:
4931 GST_PAD_UNSET_FLUSHING (pad);
4932 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4933 GST_OBJECT_UNLOCK (pad);
4934 /* grab stream lock */
4935 GST_PAD_STREAM_LOCK (pad);
4937 GST_OBJECT_LOCK (pad);
4940 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4941 GST_EVENT_TYPE_NAME (event));
4943 /* make this a little faster, no point in grabbing the lock
4944 * if the pad is allready flushing. */
4945 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4949 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4950 GST_OBJECT_UNLOCK (pad);
4951 GST_PAD_STREAM_LOCK (pad);
4953 GST_OBJECT_LOCK (pad);
4954 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4959 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4962 GST_OBJECT_UNLOCK (pad);
4964 result = eventfunc (pad, event);
4967 GST_PAD_STREAM_UNLOCK (pad);
4969 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4973 /* ERROR handling */
4976 g_warning ("pad %s:%s sending %s event in wrong direction",
4977 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4978 GST_OBJECT_UNLOCK (pad);
4979 gst_event_unref (event);
4984 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4985 GST_OBJECT_UNLOCK (pad);
4986 gst_event_unref (event);
4991 g_warning ("pad %s:%s has no event handler, file a bug.",
4992 GST_DEBUG_PAD_NAME (pad));
4993 GST_OBJECT_UNLOCK (pad);
4995 GST_PAD_STREAM_UNLOCK (pad);
4996 gst_event_unref (event);
5001 GST_OBJECT_UNLOCK (pad);
5003 GST_PAD_STREAM_UNLOCK (pad);
5004 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5005 "Received event on flushing pad. Discarding");
5006 gst_event_unref (event);
5011 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5012 gst_event_unref (event);
5018 * gst_pad_set_element_private:
5019 * @pad: the #GstPad to set the private data of.
5020 * @priv: The private data to attach to the pad.
5022 * Set the given private data gpointer on the pad.
5023 * This function can only be used by the element that owns the pad.
5024 * No locking is performed in this function.
5027 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5029 pad->element_private = priv;
5033 * gst_pad_get_element_private:
5034 * @pad: the #GstPad to get the private data of.
5036 * Gets the private data of a pad.
5037 * No locking is performed in this function.
5039 * Returns: a #gpointer to the private data.
5042 gst_pad_get_element_private (GstPad * pad)
5044 return pad->element_private;
5048 do_stream_status (GstPad * pad, GstStreamStatusType type,
5049 GThread * thread, GstTask * task)
5053 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5055 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5056 if (GST_IS_ELEMENT (parent)) {
5057 GstMessage *message;
5058 GValue value = { 0 };
5060 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5063 g_value_init (&value, GST_TYPE_TASK);
5064 g_value_set_object (&value, task);
5065 gst_message_set_stream_status_object (message, &value);
5066 g_value_unset (&value);
5068 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5069 gst_element_post_message (parent, message);
5071 gst_object_unref (parent);
5076 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5078 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5083 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5085 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5089 static GstTaskThreadCallbacks thr_callbacks = {
5095 * gst_pad_start_task:
5096 * @pad: the #GstPad to start the task of
5097 * @func: the task function to call
5098 * @data: data passed to the task function
5100 * Starts a task that repeatedly calls @func with @data. This function
5101 * is mostly used in pad activation functions to start the dataflow.
5102 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5103 * before @func is called.
5105 * Returns: a %TRUE if the task could be started.
5108 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5113 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5114 g_return_val_if_fail (func != NULL, FALSE);
5116 GST_DEBUG_OBJECT (pad, "start task");
5118 GST_OBJECT_LOCK (pad);
5119 task = GST_PAD_TASK (pad);
5121 task = gst_task_create (func, data);
5122 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5123 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5124 GST_DEBUG_OBJECT (pad, "created task");
5125 /* release lock to post the message */
5126 GST_OBJECT_UNLOCK (pad);
5128 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5130 GST_OBJECT_LOCK (pad);
5131 /* nobody else is supposed to have changed the pad now */
5132 if (GST_PAD_TASK (pad) != NULL)
5133 goto concurrent_start;
5134 GST_PAD_TASK (pad) = task;
5136 res = gst_task_set_state (task, GST_TASK_STARTED);
5137 GST_OBJECT_UNLOCK (pad);
5144 g_warning ("two threads started pad %s:%s at the same time",
5145 GST_DEBUG_PAD_NAME (pad));
5146 GST_OBJECT_UNLOCK (pad);
5147 gst_object_unref (task);
5153 * gst_pad_pause_task:
5154 * @pad: the #GstPad to pause the task of
5156 * Pause the task of @pad. This function will also wait until the
5157 * function executed by the task is finished if this function is not
5158 * called from the task function.
5160 * Returns: a TRUE if the task could be paused or FALSE when the pad
5164 gst_pad_pause_task (GstPad * pad)
5169 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5171 GST_DEBUG_OBJECT (pad, "pause task");
5173 GST_OBJECT_LOCK (pad);
5174 task = GST_PAD_TASK (pad);
5177 res = gst_task_set_state (task, GST_TASK_PAUSED);
5178 GST_OBJECT_UNLOCK (pad);
5180 /* wait for task function to finish, this lock is recursive so it does nothing
5181 * when the pause is called from the task itself */
5182 GST_PAD_STREAM_LOCK (pad);
5183 GST_PAD_STREAM_UNLOCK (pad);
5189 GST_DEBUG_OBJECT (pad, "pad has no task");
5190 GST_OBJECT_UNLOCK (pad);
5196 * gst_pad_stop_task:
5197 * @pad: the #GstPad to stop the task of
5199 * Stop the task of @pad. This function will also make sure that the
5200 * function executed by the task will effectively stop if not called
5201 * from the GstTaskFunction.
5203 * This function will deadlock if called from the GstTaskFunction of
5204 * the task. Use gst_task_pause() instead.
5206 * Regardless of whether the pad has a task, the stream lock is acquired and
5207 * released so as to ensure that streaming through this pad has finished.
5209 * Returns: a TRUE if the task could be stopped or FALSE on error.
5212 gst_pad_stop_task (GstPad * pad)
5217 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5219 GST_DEBUG_OBJECT (pad, "stop task");
5221 GST_OBJECT_LOCK (pad);
5222 task = GST_PAD_TASK (pad);
5225 GST_PAD_TASK (pad) = NULL;
5226 res = gst_task_set_state (task, GST_TASK_STOPPED);
5227 GST_OBJECT_UNLOCK (pad);
5229 GST_PAD_STREAM_LOCK (pad);
5230 GST_PAD_STREAM_UNLOCK (pad);
5232 if (!gst_task_join (task))
5235 gst_object_unref (task);
5241 GST_DEBUG_OBJECT (pad, "no task");
5242 GST_OBJECT_UNLOCK (pad);
5244 GST_PAD_STREAM_LOCK (pad);
5245 GST_PAD_STREAM_UNLOCK (pad);
5247 /* this is not an error */
5252 /* this is bad, possibly the application tried to join the task from
5253 * the task's thread. We install the task again so that it will be stopped
5254 * again from the right thread next time hopefully. */
5255 GST_OBJECT_LOCK (pad);
5256 GST_DEBUG_OBJECT (pad, "join failed");
5257 /* we can only install this task if there was no other task */
5258 if (GST_PAD_TASK (pad) == NULL)
5259 GST_PAD_TASK (pad) = task;
5260 GST_OBJECT_UNLOCK (pad);