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 #define GST_PAD_GET_PRIVATE(obj) \
99 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
101 #define GST_PAD_CHAINLISTFUNC(pad) ((pad)->abidata.ABI.priv->chainlistfunc)
103 struct _GstPadPrivate
105 GstPadChainListFunction chainlistfunc;
108 static void gst_pad_dispose (GObject * object);
109 static void gst_pad_finalize (GObject * object);
110 static void gst_pad_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_pad_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static GstFlowReturn handle_pad_block (GstPad * pad);
116 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
117 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
118 static gboolean gst_pad_activate_default (GstPad * pad);
119 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
121 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
122 #ifdef GST_DISABLE_DEPRECATED
123 #include <libxml/parser.h>
125 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
126 void gst_pad_load_and_link (xmlNodePtr self, GstObject * parent);
129 /* Some deprecated stuff that we need inside here for
130 * backwards compatibility */
131 #ifdef GST_DISABLE_DEPRECATED
132 #ifndef GST_REMOVE_DEPRECATED
133 #define GST_PAD_INTLINKFUNC(pad) (GST_PAD_CAST(pad)->intlinkfunc)
134 GList *gst_pad_get_internal_links_default (GstPad * pad);
138 static GstObjectClass *parent_class = NULL;
139 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
141 static GParamSpec *pspec_caps = NULL;
143 /* quarks for probe signals */
144 static GQuark buffer_quark;
145 static GQuark event_quark;
154 static GstFlowQuarks flow_quarks[] = {
155 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
156 {GST_FLOW_RESEND, "resend", 0},
157 {GST_FLOW_OK, "ok", 0},
158 {GST_FLOW_NOT_LINKED, "not-linked", 0},
159 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
160 {GST_FLOW_UNEXPECTED, "unexpected", 0},
161 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
162 {GST_FLOW_ERROR, "error", 0},
163 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
164 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
169 * @ret: a #GstFlowReturn to get the name of.
171 * Gets a string representing the given flow return.
173 * Returns: a static string with the name of the flow return.
175 G_CONST_RETURN gchar *
176 gst_flow_get_name (GstFlowReturn ret)
180 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
182 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
183 if (ret == flow_quarks[i].ret)
184 return flow_quarks[i].name;
191 * @ret: a #GstFlowReturn to get the quark of.
193 * Get the unique quark for the given GstFlowReturn.
195 * Returns: the quark associated with the flow return or 0 if an
196 * invalid return was specified.
199 gst_flow_to_quark (GstFlowReturn ret)
203 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
205 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
206 if (ret == flow_quarks[i].ret)
207 return flow_quarks[i].quark;
216 buffer_quark = g_quark_from_static_string ("buffer"); \
217 event_quark = g_quark_from_static_string ("event"); \
219 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
220 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
223 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
224 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
227 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
230 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
231 GValue * return_accu, const GValue * handler_return, gpointer dummy)
233 gboolean ret = g_value_get_boolean (handler_return);
235 GST_DEBUG ("accumulated %d", ret);
236 g_value_set_boolean (return_accu, ret);
242 default_have_data (GstPad * pad, GstMiniObject * o)
248 gst_pad_class_init (GstPadClass * klass)
250 GObjectClass *gobject_class;
251 GstObjectClass *gstobject_class;
253 gobject_class = G_OBJECT_CLASS (klass);
254 gstobject_class = GST_OBJECT_CLASS (klass);
256 g_type_class_add_private (klass, sizeof (GstPadPrivate));
258 parent_class = g_type_class_peek_parent (klass);
260 gobject_class->dispose = gst_pad_dispose;
261 gobject_class->finalize = gst_pad_finalize;
262 gobject_class->set_property = gst_pad_set_property;
263 gobject_class->get_property = gst_pad_get_property;
267 * @pad: the pad that emitted the signal
268 * @peer: the peer pad that has been connected
270 * Signals that a pad has been linked to the peer pad.
272 gst_pad_signals[PAD_LINKED] =
273 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
274 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
275 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
278 * @pad: the pad that emitted the signal
279 * @peer: the peer pad that has been disconnected
281 * Signals that a pad has been unlinked from the peer pad.
283 gst_pad_signals[PAD_UNLINKED] =
284 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
285 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
286 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
288 * GstPad::request-link:
289 * @pad: the pad that emitted the signal
290 * @peer: the peer pad for which a connection is requested
292 * Signals that a pad connection has been requested.
294 gst_pad_signals[PAD_REQUEST_LINK] =
295 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
296 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
297 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
301 * @pad: the pad that emitted the signal
302 * @mini_obj: new data
304 * Signals that new data is available on the pad. This signal is used
305 * internally for implementing pad probes.
306 * See gst_pad_add_*_probe functions.
308 * Returns: %TRUE to keep the data, %FALSE to drop it
310 gst_pad_signals[PAD_HAVE_DATA] =
311 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
312 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
313 G_STRUCT_OFFSET (GstPadClass, have_data),
314 _gst_do_pass_data_accumulator,
315 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
316 GST_TYPE_MINI_OBJECT);
318 pspec_caps = g_param_spec_boxed ("caps", "Caps",
319 "The capabilities of the pad", GST_TYPE_CAPS,
320 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
321 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
323 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
324 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
325 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
326 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
327 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
328 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
329 g_param_spec_object ("template", "Template",
330 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
334 gstobject_class->save_thyself =
335 ((gpointer (*)(GstObject * object,
336 gpointer self)) * GST_DEBUG_FUNCPTR (gst_pad_save_thyself));
338 gstobject_class->path_string_separator = ".";
340 /* Register common function pointer descriptions */
341 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
342 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
343 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
344 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
345 #ifndef GST_REMOVE_DEPRECATED
346 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_internal_links_default);
348 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
349 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
351 klass->have_data = default_have_data;
355 gst_pad_init (GstPad * pad)
357 pad->abidata.ABI.priv = GST_PAD_GET_PRIVATE (pad);
359 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
360 GST_PAD_PEER (pad) = NULL;
362 GST_PAD_CHAINFUNC (pad) = NULL;
364 GST_PAD_LINKFUNC (pad) = NULL;
366 GST_PAD_CAPS (pad) = NULL;
367 GST_PAD_GETCAPSFUNC (pad) = NULL;
369 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
370 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
371 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
372 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
373 #ifndef GST_REMOVE_DEPRECATED
374 GST_PAD_INTLINKFUNC (pad) = gst_pad_get_internal_links_default;
376 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
378 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
380 pad->do_buffer_signals = 0;
381 pad->do_event_signals = 0;
383 GST_PAD_SET_FLUSHING (pad);
385 pad->preroll_lock = g_mutex_new ();
386 pad->preroll_cond = g_cond_new ();
388 /* FIXME 0.11: Store this directly in the instance struct */
389 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
390 g_static_rec_mutex_init (pad->stream_rec_lock);
392 pad->block_cond = g_cond_new ();
396 gst_pad_dispose (GObject * object)
398 GstPad *pad = GST_PAD_CAST (object);
401 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
403 /* unlink the peer pad */
404 if ((peer = gst_pad_get_peer (pad))) {
405 /* window for MT unsafeness, someone else could unlink here
406 * and then we call unlink with wrong pads. The unlink
407 * function would catch this and safely return failed. */
408 if (GST_PAD_IS_SRC (pad))
409 gst_pad_unlink (pad, peer);
411 gst_pad_unlink (peer, pad);
413 gst_object_unref (peer);
417 gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
419 gst_pad_set_pad_template (pad, NULL);
421 if (pad->block_destroy_data && pad->block_data) {
422 pad->block_destroy_data (pad->block_data);
423 pad->block_data = NULL;
426 G_OBJECT_CLASS (parent_class)->dispose (object);
430 gst_pad_finalize (GObject * object)
432 GstPad *pad = GST_PAD_CAST (object);
435 /* in case the task is still around, clean it up */
436 if ((task = GST_PAD_TASK (pad))) {
437 gst_task_join (task);
438 GST_PAD_TASK (pad) = NULL;
439 gst_object_unref (task);
442 if (pad->stream_rec_lock) {
443 g_static_rec_mutex_free (pad->stream_rec_lock);
444 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
445 pad->stream_rec_lock = NULL;
447 if (pad->preroll_lock) {
448 g_mutex_free (pad->preroll_lock);
449 g_cond_free (pad->preroll_cond);
450 pad->preroll_lock = NULL;
451 pad->preroll_cond = NULL;
453 if (pad->block_cond) {
454 g_cond_free (pad->block_cond);
455 pad->block_cond = NULL;
458 G_OBJECT_CLASS (parent_class)->finalize (object);
462 gst_pad_set_property (GObject * object, guint prop_id,
463 const GValue * value, GParamSpec * pspec)
465 g_return_if_fail (GST_IS_PAD (object));
468 case PAD_PROP_DIRECTION:
469 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
471 case PAD_PROP_TEMPLATE:
472 gst_pad_set_pad_template (GST_PAD_CAST (object),
473 (GstPadTemplate *) g_value_get_object (value));
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 gst_pad_get_property (GObject * object, guint prop_id,
483 GValue * value, GParamSpec * pspec)
485 g_return_if_fail (GST_IS_PAD (object));
489 GST_OBJECT_LOCK (object);
490 g_value_set_boxed (value, GST_PAD_CAPS (object));
491 GST_OBJECT_UNLOCK (object);
493 case PAD_PROP_DIRECTION:
494 g_value_set_enum (value, GST_PAD_DIRECTION (object));
496 case PAD_PROP_TEMPLATE:
497 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 * @name: the name of the new pad.
508 * @direction: the #GstPadDirection of the pad.
510 * Creates a new pad with the given name in the given direction.
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.
520 gst_pad_new (const gchar * name, GstPadDirection direction)
522 return g_object_new (GST_TYPE_PAD,
523 "name", name, "direction", direction, NULL);
527 * gst_pad_new_from_template:
528 * @templ: the pad template to use
529 * @name: the name of the element
531 * Creates a new pad with the given name from the given template.
532 * If name is NULL, a guaranteed unique name (across all pads)
534 * This function makes a copy of the name so you can safely free the name.
536 * Returns: a new #GstPad, or NULL in case of an error.
539 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
541 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
543 return g_object_new (GST_TYPE_PAD,
544 "name", name, "direction", templ->direction, "template", templ, NULL);
548 * gst_pad_new_from_static_template:
549 * @templ: the #GstStaticPadTemplate to use
550 * @name: the name of the element
552 * Creates a new pad with the given name from the given static template.
553 * If name is NULL, a guaranteed unique name (across all pads)
555 * This function makes a copy of the name so you can safely free the name.
557 * Returns: a new #GstPad, or NULL in case of an error.
560 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
564 GstPadTemplate *template;
566 template = gst_static_pad_template_get (templ);
567 pad = gst_pad_new_from_template (template, name);
568 gst_object_unref (template);
573 * gst_pad_get_direction:
574 * @pad: a #GstPad to get the direction of.
576 * Gets the direction of the pad. The direction of the pad is
577 * decided at construction time so this function does not take
580 * Returns: the #GstPadDirection of the pad.
585 gst_pad_get_direction (GstPad * pad)
587 GstPadDirection result;
589 /* PAD_UNKNOWN is a little silly but we need some sort of
590 * error return value */
591 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
593 result = GST_PAD_DIRECTION (pad);
599 gst_pad_activate_default (GstPad * pad)
601 return gst_pad_activate_push (pad, TRUE);
605 pre_activate (GstPad * pad, GstActivateMode new_mode)
608 case GST_ACTIVATE_PUSH:
609 case GST_ACTIVATE_PULL:
610 GST_OBJECT_LOCK (pad);
611 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
613 GST_PAD_UNSET_FLUSHING (pad);
614 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
615 GST_OBJECT_UNLOCK (pad);
617 case GST_ACTIVATE_NONE:
618 GST_OBJECT_LOCK (pad);
619 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
620 GST_PAD_SET_FLUSHING (pad);
621 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
622 /* unlock blocked pads so element can resume and stop */
623 GST_PAD_BLOCK_BROADCAST (pad);
624 GST_OBJECT_UNLOCK (pad);
630 post_activate (GstPad * pad, GstActivateMode new_mode)
633 case GST_ACTIVATE_PUSH:
634 case GST_ACTIVATE_PULL:
637 case GST_ACTIVATE_NONE:
638 /* ensures that streaming stops */
639 GST_PAD_STREAM_LOCK (pad);
640 GST_DEBUG_OBJECT (pad, "stopped streaming");
641 GST_PAD_STREAM_UNLOCK (pad);
647 * gst_pad_set_active:
648 * @pad: the #GstPad to activate or deactivate.
649 * @active: whether or not the pad should be active.
651 * Activates or deactivates the given pad.
652 * Normally called from within core state change functions.
654 * If @active, makes sure the pad is active. If it is already active, either in
655 * push or pull mode, just return. Otherwise dispatches to the pad's activate
656 * function to perform the actual activation.
658 * If not @active, checks the pad's current mode and calls
659 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
662 * Returns: #TRUE if the operation was successful.
667 gst_pad_set_active (GstPad * pad, gboolean active)
670 gboolean ret = FALSE;
672 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
674 GST_OBJECT_LOCK (pad);
675 old = GST_PAD_ACTIVATE_MODE (pad);
676 GST_OBJECT_UNLOCK (pad);
680 case GST_ACTIVATE_PUSH:
681 GST_DEBUG_OBJECT (pad, "activating pad from push");
684 case GST_ACTIVATE_PULL:
685 GST_DEBUG_OBJECT (pad, "activating pad from pull");
688 case GST_ACTIVATE_NONE:
689 GST_DEBUG_OBJECT (pad, "activating pad from none");
690 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
695 case GST_ACTIVATE_PUSH:
696 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
697 ret = gst_pad_activate_push (pad, FALSE);
699 case GST_ACTIVATE_PULL:
700 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
701 ret = gst_pad_activate_pull (pad, FALSE);
703 case GST_ACTIVATE_NONE:
704 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
711 GST_OBJECT_LOCK (pad);
713 g_critical ("Failed to deactivate pad %s:%s, very bad",
714 GST_DEBUG_PAD_NAME (pad));
716 GST_WARNING_OBJECT (pad, "Failed to activate pad");
718 GST_OBJECT_UNLOCK (pad);
725 * gst_pad_activate_pull:
726 * @pad: the #GstPad to activate or deactivate.
727 * @active: whether or not the pad should be active.
729 * Activates or deactivates the given pad in pull mode via dispatching to the
730 * pad's activatepullfunc. For use from within pad activation functions only.
731 * When called on sink pads, will first proxy the call to the peer pad, which
732 * is expected to activate its internally linked pads from within its
733 * activate_pull function.
735 * If you don't know what this is, you probably don't want to call it.
737 * Returns: TRUE if the operation was successful.
742 gst_pad_activate_pull (GstPad * pad, gboolean active)
744 GstActivateMode old, new;
747 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
749 GST_OBJECT_LOCK (pad);
750 old = GST_PAD_ACTIVATE_MODE (pad);
751 GST_OBJECT_UNLOCK (pad);
755 case GST_ACTIVATE_PULL:
756 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
758 case GST_ACTIVATE_PUSH:
759 GST_DEBUG_OBJECT (pad,
760 "activating pad from push, deactivate push first");
761 /* pad was activate in the wrong direction, deactivate it
762 * and reactivate it in pull mode */
763 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
764 goto deactivate_failed;
765 /* fallthrough, pad is deactivated now. */
766 case GST_ACTIVATE_NONE:
767 GST_DEBUG_OBJECT (pad, "activating pad from none");
772 case GST_ACTIVATE_NONE:
773 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
775 case GST_ACTIVATE_PUSH:
776 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
777 /* pad was activated in the other direction, deactivate it
778 * in push mode, this should not happen... */
779 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
780 goto deactivate_failed;
781 /* everything is fine now */
783 case GST_ACTIVATE_PULL:
784 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
789 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
790 if ((peer = gst_pad_get_peer (pad))) {
791 GST_DEBUG_OBJECT (pad, "calling peer");
792 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
794 gst_object_unref (peer);
796 /* there is no peer, this is only fatal when we activate. When we
797 * deactivate, we must assume the application has unlinked the peer and
798 * will deactivate it eventually. */
802 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
805 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
806 goto failure; /* Can't activate pull on a src without a
810 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
811 pre_activate (pad, new);
813 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
814 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
817 /* can happen for sinks of passthrough elements */
820 post_activate (pad, new);
822 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
823 active ? "activated" : "deactivated");
829 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
830 active ? "activated" : "deactivated");
835 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
836 "failed to %s in switch to pull from mode %d",
837 (active ? "activate" : "deactivate"), old);
842 GST_OBJECT_LOCK (peer);
843 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
844 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
845 GST_OBJECT_UNLOCK (peer);
846 gst_object_unref (peer);
851 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
857 GST_OBJECT_LOCK (pad);
858 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
859 active ? "activate" : "deactivate");
860 GST_PAD_SET_FLUSHING (pad);
861 GST_PAD_ACTIVATE_MODE (pad) = old;
862 GST_OBJECT_UNLOCK (pad);
868 * gst_pad_activate_push:
869 * @pad: the #GstPad to activate or deactivate.
870 * @active: whether the pad should be active or not.
872 * Activates or deactivates the given pad in push mode via dispatching to the
873 * pad's activatepushfunc. For use from within pad activation functions only.
875 * If you don't know what this is, you probably don't want to call it.
877 * Returns: %TRUE if the operation was successful.
882 gst_pad_activate_push (GstPad * pad, gboolean active)
884 GstActivateMode old, new;
886 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
887 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
888 active ? "activated" : "deactivated");
890 GST_OBJECT_LOCK (pad);
891 old = GST_PAD_ACTIVATE_MODE (pad);
892 GST_OBJECT_UNLOCK (pad);
896 case GST_ACTIVATE_PUSH:
897 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
899 case GST_ACTIVATE_PULL:
900 GST_DEBUG_OBJECT (pad,
901 "activating pad from push, deactivating pull first");
902 /* pad was activate in the wrong direction, deactivate it
903 * an reactivate it in push mode */
904 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
905 goto deactivate_failed;
906 /* fallthrough, pad is deactivated now. */
907 case GST_ACTIVATE_NONE:
908 GST_DEBUG_OBJECT (pad, "activating pad from none");
913 case GST_ACTIVATE_NONE:
914 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
916 case GST_ACTIVATE_PULL:
917 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
918 /* pad was activated in the other direction, deactivate it
919 * in pull mode, this should not happen... */
920 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
921 goto deactivate_failed;
922 /* everything is fine now */
924 case GST_ACTIVATE_PUSH:
925 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
930 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
931 pre_activate (pad, new);
933 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
934 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
938 /* quite ok, element relies on state change func to prepare itself */
941 post_activate (pad, new);
943 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
944 active ? "activated" : "deactivated");
949 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
950 active ? "activated" : "deactivated");
955 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
956 "failed to %s in switch to push from mode %d",
957 (active ? "activate" : "deactivate"), old);
962 GST_OBJECT_LOCK (pad);
963 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
964 active ? "activate" : "deactivate");
965 GST_PAD_SET_FLUSHING (pad);
966 GST_PAD_ACTIVATE_MODE (pad) = old;
967 GST_OBJECT_UNLOCK (pad);
974 * @pad: the #GstPad to query
976 * Query if a pad is active
978 * Returns: TRUE if the pad is active.
983 gst_pad_is_active (GstPad * pad)
985 gboolean result = FALSE;
987 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
989 GST_OBJECT_LOCK (pad);
990 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
991 GST_OBJECT_UNLOCK (pad);
997 * gst_pad_set_blocked_async_full:
998 * @pad: the #GstPad to block or unblock
999 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1000 * @callback: #GstPadBlockCallback that will be called when the
1001 * operation succeeds
1002 * @user_data: user data passed to the callback
1003 * @destroy_data: #GDestroyNotify for user_data
1005 * Blocks or unblocks the dataflow on a pad. The provided callback
1006 * is called when the operation succeeds; this happens right before the next
1007 * attempt at pushing a buffer on the pad.
1009 * This can take a while as the pad can only become blocked when real dataflow
1011 * When the pipeline is stalled, for example in PAUSED, this can
1012 * take an indeterminate amount of time.
1013 * You can pass NULL as the callback to make this call block. Be careful with
1014 * this blocking call as it might not return for reasons stated above.
1016 * Returns: TRUE if the pad could be blocked. This function can fail if the
1017 * wrong parameters were passed or the pad was already in the requested state.
1024 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
1025 GstPadBlockCallback callback, gpointer user_data,
1026 GDestroyNotify destroy_data)
1028 gboolean was_blocked = FALSE;
1030 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1032 GST_OBJECT_LOCK (pad);
1034 was_blocked = GST_PAD_IS_BLOCKED (pad);
1036 if (G_UNLIKELY (was_blocked == blocked))
1037 goto had_right_state;
1040 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1042 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1044 if (pad->block_destroy_data && pad->block_data)
1045 pad->block_destroy_data (pad->block_data);
1047 pad->block_callback = callback;
1048 pad->block_data = user_data;
1049 pad->block_destroy_data = destroy_data;
1050 pad->abidata.ABI.block_callback_called = FALSE;
1052 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1053 GST_PAD_BLOCK_WAIT (pad);
1054 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1057 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1059 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1061 if (pad->block_destroy_data && pad->block_data)
1062 pad->block_destroy_data (pad->block_data);
1064 pad->block_callback = callback;
1065 pad->block_data = user_data;
1066 pad->block_destroy_data = destroy_data;
1067 pad->abidata.ABI.block_callback_called = FALSE;
1069 GST_PAD_BLOCK_BROADCAST (pad);
1071 /* no callback, wait for the unblock to happen */
1072 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1073 GST_PAD_BLOCK_WAIT (pad);
1074 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1077 GST_OBJECT_UNLOCK (pad);
1083 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1084 "pad was in right state (%d)", was_blocked);
1085 GST_OBJECT_UNLOCK (pad);
1092 * gst_pad_set_blocked_async:
1093 * @pad: the #GstPad to block or unblock
1094 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1095 * @callback: #GstPadBlockCallback that will be called when the
1096 * operation succeeds
1097 * @user_data: user data passed to the callback
1099 * Blocks or unblocks the dataflow on a pad. The provided callback
1100 * is called when the operation succeeds; this happens right before the next
1101 * attempt at pushing a buffer on the pad.
1103 * This can take a while as the pad can only become blocked when real dataflow
1105 * When the pipeline is stalled, for example in PAUSED, this can
1106 * take an indeterminate amount of time.
1107 * You can pass NULL as the callback to make this call block. Be careful with
1108 * this blocking call as it might not return for reasons stated above.
1110 * Returns: TRUE if the pad could be blocked. This function can fail if the
1111 * wrong parameters were passed or the pad was already in the requested state.
1116 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1117 GstPadBlockCallback callback, gpointer user_data)
1119 return gst_pad_set_blocked_async_full (pad, blocked,
1120 callback, user_data, NULL);
1124 * gst_pad_set_blocked:
1125 * @pad: the #GstPad to block or unblock
1126 * @blocked: boolean indicating we should block or unblock
1128 * Blocks or unblocks the dataflow on a pad. This function is
1129 * a shortcut for gst_pad_set_blocked_async() with a NULL
1132 * Returns: TRUE if the pad could be blocked. This function can fail if the
1133 * wrong parameters were passed or the pad was already in the requested state.
1138 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1140 return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1144 * gst_pad_is_blocked:
1145 * @pad: the #GstPad to query
1147 * Checks if the pad is blocked or not. This function returns the
1148 * last requested state of the pad. It is not certain that the pad
1149 * is actually blocking at this point (see gst_pad_is_blocking()).
1151 * Returns: TRUE if the pad is blocked.
1156 gst_pad_is_blocked (GstPad * pad)
1158 gboolean result = FALSE;
1160 g_return_val_if_fail (GST_IS_PAD (pad), result);
1162 GST_OBJECT_LOCK (pad);
1163 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1164 GST_OBJECT_UNLOCK (pad);
1170 * gst_pad_is_blocking:
1171 * @pad: the #GstPad to query
1173 * Checks if the pad is blocking or not. This is a guaranteed state
1174 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1176 * Returns: TRUE if the pad is blocking.
1183 gst_pad_is_blocking (GstPad * pad)
1185 gboolean result = FALSE;
1187 g_return_val_if_fail (GST_IS_PAD (pad), result);
1189 GST_OBJECT_LOCK (pad);
1190 /* the blocking flag is only valid if the pad is not flushing */
1191 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1192 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1193 GST_OBJECT_UNLOCK (pad);
1199 * gst_pad_set_activate_function:
1201 * @activate: the #GstPadActivateFunction to set.
1203 * Sets the given activate function for @pad. The activate function will
1204 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1205 * the actual activation. Only makes sense to set on sink pads.
1207 * Call this function if your sink pad can start a pull-based task.
1210 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1212 g_return_if_fail (GST_IS_PAD (pad));
1214 GST_PAD_ACTIVATEFUNC (pad) = activate;
1215 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1216 GST_DEBUG_FUNCPTR_NAME (activate));
1220 * gst_pad_set_activatepull_function:
1222 * @activatepull: the #GstPadActivateModeFunction to set.
1224 * Sets the given activate_pull function for the pad. An activate_pull function
1225 * prepares the element and any upstream connections for pulling. See XXX
1226 * part-activation.txt for details.
1229 gst_pad_set_activatepull_function (GstPad * pad,
1230 GstPadActivateModeFunction activatepull)
1232 g_return_if_fail (GST_IS_PAD (pad));
1234 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1235 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1236 GST_DEBUG_FUNCPTR_NAME (activatepull));
1240 * gst_pad_set_activatepush_function:
1242 * @activatepush: the #GstPadActivateModeFunction to set.
1244 * Sets the given activate_push function for the pad. An activate_push function
1245 * prepares the element for pushing. See XXX part-activation.txt for details.
1248 gst_pad_set_activatepush_function (GstPad * pad,
1249 GstPadActivateModeFunction activatepush)
1251 g_return_if_fail (GST_IS_PAD (pad));
1253 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1254 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1255 GST_DEBUG_FUNCPTR_NAME (activatepush));
1259 * gst_pad_set_chain_function:
1260 * @pad: a sink #GstPad.
1261 * @chain: the #GstPadChainFunction to set.
1263 * Sets the given chain function for the pad. The chain function is called to
1264 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1267 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1269 g_return_if_fail (GST_IS_PAD (pad));
1270 g_return_if_fail (GST_PAD_IS_SINK (pad));
1272 GST_PAD_CHAINFUNC (pad) = chain;
1273 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1274 GST_DEBUG_FUNCPTR_NAME (chain));
1278 * gst_pad_set_chain_list_function:
1279 * @pad: a sink #GstPad.
1280 * @chainlist: the #GstPadChainListFunction to set.
1282 * Sets the given chain list function for the pad. The chainlist function is
1283 * called to process a #GstBufferList input buffer list. See
1284 * #GstPadChainListFunction for more details.
1289 gst_pad_set_chain_list_function (GstPad * pad,
1290 GstPadChainListFunction chainlist)
1292 g_return_if_fail (GST_IS_PAD (pad));
1293 g_return_if_fail (GST_PAD_IS_SINK (pad));
1295 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1296 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1297 GST_DEBUG_FUNCPTR_NAME (chainlist));
1301 * gst_pad_set_getrange_function:
1302 * @pad: a source #GstPad.
1303 * @get: the #GstPadGetRangeFunction to set.
1305 * Sets the given getrange function for the pad. The getrange function is
1306 * called to produce a new #GstBuffer to start the processing pipeline. see
1307 * #GstPadGetRangeFunction for a description of the getrange function.
1310 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1312 g_return_if_fail (GST_IS_PAD (pad));
1313 g_return_if_fail (GST_PAD_IS_SRC (pad));
1315 GST_PAD_GETRANGEFUNC (pad) = get;
1317 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1318 GST_DEBUG_FUNCPTR_NAME (get));
1322 * gst_pad_set_checkgetrange_function:
1323 * @pad: a source #GstPad.
1324 * @check: the #GstPadCheckGetRangeFunction to set.
1326 * Sets the given checkgetrange function for the pad. Implement this function
1327 * on a pad if you dynamically support getrange based scheduling on the pad.
1330 gst_pad_set_checkgetrange_function (GstPad * pad,
1331 GstPadCheckGetRangeFunction check)
1333 g_return_if_fail (GST_IS_PAD (pad));
1334 g_return_if_fail (GST_PAD_IS_SRC (pad));
1336 GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1338 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1339 GST_DEBUG_FUNCPTR_NAME (check));
1343 * gst_pad_set_event_function:
1344 * @pad: a #GstPad of either direction.
1345 * @event: the #GstPadEventFunction to set.
1347 * Sets the given event handler for the pad.
1350 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1352 g_return_if_fail (GST_IS_PAD (pad));
1354 GST_PAD_EVENTFUNC (pad) = event;
1356 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1357 GST_DEBUG_FUNCPTR_NAME (event));
1361 * gst_pad_set_query_function:
1362 * @pad: a #GstPad of either direction.
1363 * @query: the #GstPadQueryFunction to set.
1365 * Set the given query function for the pad.
1368 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1370 g_return_if_fail (GST_IS_PAD (pad));
1372 GST_PAD_QUERYFUNC (pad) = query;
1374 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1375 GST_DEBUG_FUNCPTR_NAME (query));
1379 * gst_pad_set_query_type_function:
1380 * @pad: a #GstPad of either direction.
1381 * @type_func: the #GstPadQueryTypeFunction to set.
1383 * Set the given query type function for the pad.
1386 gst_pad_set_query_type_function (GstPad * pad,
1387 GstPadQueryTypeFunction type_func)
1389 g_return_if_fail (GST_IS_PAD (pad));
1391 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1393 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1394 GST_DEBUG_FUNCPTR_NAME (type_func));
1398 * gst_pad_get_query_types:
1401 * Get an array of supported queries that can be performed
1404 * Returns: a zero-terminated array of #GstQueryType.
1406 const GstQueryType *
1407 gst_pad_get_query_types (GstPad * pad)
1409 GstPadQueryTypeFunction func;
1411 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1413 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1425 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1427 *data = gst_pad_get_query_types (pad);
1433 * gst_pad_get_query_types_default:
1436 * Invoke the default dispatcher for the query types on
1439 * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1440 * internally-linked pads has a query types function.
1442 const GstQueryType *
1443 gst_pad_get_query_types_default (GstPad * pad)
1445 GstQueryType *result = NULL;
1447 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1449 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1450 gst_pad_get_query_types_dispatcher, &result);
1456 * gst_pad_set_iterate_internal_links_function:
1457 * @pad: a #GstPad of either direction.
1458 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1460 * Sets the given internal link iterator function for the pad.
1465 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1466 GstPadIterIntLinkFunction iterintlink)
1468 g_return_if_fail (GST_IS_PAD (pad));
1470 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1471 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1472 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1476 * gst_pad_set_internal_link_function:
1477 * @pad: a #GstPad of either direction.
1478 * @intlink: the #GstPadIntLinkFunction to set.
1480 * Sets the given internal link function for the pad.
1482 * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1484 #ifndef GST_REMOVE_DEPRECATED
1485 #ifdef GST_DISABLE_DEPRECATED
1487 gst_pad_set_internal_link_function (GstPad * pad,
1488 GstPadIntLinkFunction intlink);
1491 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1493 g_return_if_fail (GST_IS_PAD (pad));
1495 GST_PAD_INTLINKFUNC (pad) = intlink;
1496 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1497 GST_DEBUG_FUNCPTR_NAME (intlink));
1499 #endif /* GST_REMOVE_DEPRECATED */
1502 * gst_pad_set_link_function:
1504 * @link: the #GstPadLinkFunction to set.
1506 * Sets the given link function for the pad. It will be called when
1507 * the pad is linked with another pad.
1509 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1512 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1513 * cannot be made for some reason.
1515 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1516 * of the peer sink pad, if present.
1519 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1521 g_return_if_fail (GST_IS_PAD (pad));
1523 GST_PAD_LINKFUNC (pad) = link;
1524 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1525 GST_DEBUG_FUNCPTR_NAME (link));
1529 * gst_pad_set_unlink_function:
1531 * @unlink: the #GstPadUnlinkFunction to set.
1533 * Sets the given unlink function for the pad. It will be called
1534 * when the pad is unlinked.
1537 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1539 g_return_if_fail (GST_IS_PAD (pad));
1541 GST_PAD_UNLINKFUNC (pad) = unlink;
1542 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1543 GST_DEBUG_FUNCPTR_NAME (unlink));
1547 * gst_pad_set_getcaps_function:
1549 * @getcaps: the #GstPadGetCapsFunction to set.
1551 * Sets the given getcaps function for the pad. @getcaps should return the
1552 * allowable caps for a pad in the context of the element's state, its link to
1553 * other elements, and the devices or files it has opened. These caps must be a
1554 * subset of the pad template caps. In the NULL state with no links, @getcaps
1555 * should ideally return the same caps as the pad template. In rare
1556 * circumstances, an object property can affect the caps returned by @getcaps,
1557 * but this is discouraged.
1559 * You do not need to call this function if @pad's allowed caps are always the
1560 * same as the pad template caps. This can only be true if the padtemplate
1561 * has fixed simple caps.
1563 * For most filters, the caps returned by @getcaps is directly affected by the
1564 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1565 * the srcpad's getcaps function is directly related to the stream data. Again,
1566 * @getcaps should return the most specific caps it reasonably can, since this
1567 * helps with autoplugging.
1569 * Note that the return value from @getcaps is owned by the caller, so the
1570 * caller should unref the caps after usage.
1573 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1575 g_return_if_fail (GST_IS_PAD (pad));
1577 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1578 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1579 GST_DEBUG_FUNCPTR_NAME (getcaps));
1583 * gst_pad_set_acceptcaps_function:
1585 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1587 * Sets the given acceptcaps function for the pad. The acceptcaps function
1588 * will be called to check if the pad can accept the given caps. Setting the
1589 * acceptcaps function to NULL restores the default behaviour of allowing
1590 * any caps that matches the caps from gst_pad_get_caps().
1593 gst_pad_set_acceptcaps_function (GstPad * pad,
1594 GstPadAcceptCapsFunction acceptcaps)
1596 g_return_if_fail (GST_IS_PAD (pad));
1598 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1599 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1600 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1604 * gst_pad_set_fixatecaps_function:
1606 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1608 * Sets the given fixatecaps function for the pad. The fixatecaps function
1609 * will be called whenever the default values for a GstCaps needs to be
1613 gst_pad_set_fixatecaps_function (GstPad * pad,
1614 GstPadFixateCapsFunction fixatecaps)
1616 g_return_if_fail (GST_IS_PAD (pad));
1618 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1619 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1620 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1624 * gst_pad_set_setcaps_function:
1626 * @setcaps: the #GstPadSetCapsFunction to set.
1628 * Sets the given setcaps function for the pad. The setcaps function
1629 * will be called whenever a buffer with a new media type is pushed or
1630 * pulled from the pad. The pad/element needs to update its internal
1631 * structures to process the new media type. If this new type is not
1632 * acceptable, the setcaps function should return FALSE.
1635 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1637 g_return_if_fail (GST_IS_PAD (pad));
1639 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1640 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1641 GST_DEBUG_FUNCPTR_NAME (setcaps));
1645 * gst_pad_set_bufferalloc_function:
1646 * @pad: a sink #GstPad.
1647 * @bufalloc: the #GstPadBufferAllocFunction to set.
1649 * Sets the given bufferalloc function for the pad. Note that the
1650 * bufferalloc function can only be set on sinkpads.
1653 gst_pad_set_bufferalloc_function (GstPad * pad,
1654 GstPadBufferAllocFunction bufalloc)
1656 g_return_if_fail (GST_IS_PAD (pad));
1657 g_return_if_fail (GST_PAD_IS_SINK (pad));
1659 GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1660 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1661 GST_DEBUG_FUNCPTR_NAME (bufalloc));
1666 * @srcpad: the source #GstPad to unlink.
1667 * @sinkpad: the sink #GstPad to unlink.
1669 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1670 * signal on both pads.
1672 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1673 * the pads were not linked together.
1678 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1680 gboolean result = FALSE;
1681 GstElement *parent = NULL;
1683 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1684 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1685 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1686 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1688 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1689 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1690 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1692 /* We need to notify the parent before taking any pad locks as the bin in
1693 * question might be waiting for a lock on the pad while holding its lock
1694 * that our message will try to take. */
1695 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1696 if (GST_IS_ELEMENT (parent)) {
1697 gst_element_post_message (parent,
1698 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1699 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1701 gst_object_unref (parent);
1706 GST_OBJECT_LOCK (srcpad);
1708 GST_OBJECT_LOCK (sinkpad);
1710 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1711 goto not_linked_together;
1713 if (GST_PAD_UNLINKFUNC (srcpad)) {
1714 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1716 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1717 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1720 /* first clear peers */
1721 GST_PAD_PEER (srcpad) = NULL;
1722 GST_PAD_PEER (sinkpad) = NULL;
1724 GST_OBJECT_UNLOCK (sinkpad);
1725 GST_OBJECT_UNLOCK (srcpad);
1727 /* fire off a signal to each of the pads telling them
1728 * that they've been unlinked */
1729 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1730 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1732 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1733 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1738 if (parent != NULL) {
1739 gst_element_post_message (parent,
1740 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1741 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1742 gst_object_unref (parent);
1747 not_linked_together:
1749 /* we do not emit a warning in this case because unlinking cannot
1750 * be made MT safe.*/
1751 GST_OBJECT_UNLOCK (sinkpad);
1752 GST_OBJECT_UNLOCK (srcpad);
1758 * gst_pad_is_linked:
1759 * @pad: pad to check
1761 * Checks if a @pad is linked to another pad or not.
1763 * Returns: TRUE if the pad is linked, FALSE otherwise.
1768 gst_pad_is_linked (GstPad * pad)
1772 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1774 GST_OBJECT_LOCK (pad);
1775 result = (GST_PAD_PEER (pad) != NULL);
1776 GST_OBJECT_UNLOCK (pad);
1781 /* get the caps from both pads and see if the intersection
1784 * This function should be called with the pad LOCK on both
1788 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1789 GstPadLinkCheck flags)
1791 GstCaps *srccaps = NULL;
1792 GstCaps *sinkcaps = NULL;
1793 gboolean compatible = FALSE;
1795 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1798 /* Doing the expensive caps checking takes priority over only checking the template caps */
1799 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1800 srccaps = gst_pad_get_caps_unlocked (src);
1801 sinkcaps = gst_pad_get_caps_unlocked (sink);
1803 /* If one of the two pads doesn't have a template, consider the intersection
1805 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1806 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1810 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1812 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1815 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1817 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1820 /* if we have caps on both pads we can check the intersection. If one
1821 * of the caps is NULL, we return TRUE. */
1822 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1824 gst_caps_unref (srccaps);
1826 gst_caps_unref (sinkcaps);
1830 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1831 gst_caps_unref (srccaps);
1832 gst_caps_unref (sinkcaps);
1835 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1836 (compatible ? "" : "not"));
1841 /* check if the grandparents of both pads are the same.
1842 * This check is required so that we don't try to link
1843 * pads from elements in different bins without ghostpads.
1845 * The LOCK should be held on both pads
1848 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1850 GstObject *psrc, *psink;
1852 psrc = GST_OBJECT_PARENT (src);
1853 psink = GST_OBJECT_PARENT (sink);
1855 /* if one of the pads has no parent, we allow the link */
1856 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1859 /* only care about parents that are elements */
1860 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1861 goto no_element_parent;
1863 /* if the parents are the same, we have a loop */
1864 if (G_UNLIKELY (psrc == psink))
1867 /* if they both have a parent, we check the grandparents. We can not lock
1868 * the parent because we hold on the child (pad) and the locking order is
1869 * parent >> child. */
1870 psrc = GST_OBJECT_PARENT (psrc);
1871 psink = GST_OBJECT_PARENT (psink);
1873 /* if they have grandparents but they are not the same */
1874 if (G_UNLIKELY (psrc != psink))
1875 goto wrong_grandparents;
1882 GST_CAT_DEBUG (GST_CAT_CAPS,
1883 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1884 GST_PTR_FORMAT, psrc, psink);
1889 GST_CAT_DEBUG (GST_CAT_CAPS,
1890 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1891 GST_PTR_FORMAT, psrc, psink);
1896 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1902 GST_CAT_DEBUG (GST_CAT_CAPS,
1903 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1904 GST_PTR_FORMAT, psrc, psink);
1909 /* FIXME leftover from an attempt at refactoring... */
1910 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1911 * the two pads will be locked in the srcpad, sinkpad order. */
1912 static GstPadLinkReturn
1913 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1915 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1916 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1918 GST_OBJECT_LOCK (srcpad);
1920 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1921 goto src_was_linked;
1923 GST_OBJECT_LOCK (sinkpad);
1925 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1926 goto sink_was_linked;
1928 /* check hierarchy, pads can only be linked if the grandparents
1930 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1931 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1932 goto wrong_hierarchy;
1934 /* check pad caps for non-empty intersection */
1935 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1938 /* FIXME check pad scheduling for non-empty intersection */
1940 return GST_PAD_LINK_OK;
1944 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1945 GST_DEBUG_PAD_NAME (srcpad),
1946 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1947 /* we do not emit a warning in this case because unlinking cannot
1948 * be made MT safe.*/
1949 GST_OBJECT_UNLOCK (srcpad);
1950 return GST_PAD_LINK_WAS_LINKED;
1954 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1955 GST_DEBUG_PAD_NAME (sinkpad),
1956 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1957 /* we do not emit a warning in this case because unlinking cannot
1958 * be made MT safe.*/
1959 GST_OBJECT_UNLOCK (sinkpad);
1960 GST_OBJECT_UNLOCK (srcpad);
1961 return GST_PAD_LINK_WAS_LINKED;
1965 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1966 GST_OBJECT_UNLOCK (sinkpad);
1967 GST_OBJECT_UNLOCK (srcpad);
1968 return GST_PAD_LINK_WRONG_HIERARCHY;
1972 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1973 GST_OBJECT_UNLOCK (sinkpad);
1974 GST_OBJECT_UNLOCK (srcpad);
1975 return GST_PAD_LINK_NOFORMAT;
1981 * @srcpad: the source #GstPad.
1982 * @sinkpad: the sink #GstPad.
1984 * Checks if the source pad and the sink pad are compatible so they can be
1987 * Returns: TRUE if the pads can be linked.
1990 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1992 GstPadLinkReturn result;
1994 /* generic checks */
1995 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1996 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1998 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1999 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2001 /* gst_pad_link_prepare does everything for us, we only release the locks
2002 * on the pads that it gets us. If this function returns !OK the locks are not
2004 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2005 if (result != GST_PAD_LINK_OK)
2008 GST_OBJECT_UNLOCK (srcpad);
2009 GST_OBJECT_UNLOCK (sinkpad);
2012 return result == GST_PAD_LINK_OK;
2016 * gst_pad_link_full:
2017 * @srcpad: the source #GstPad to link.
2018 * @sinkpad: the sink #GstPad to link.
2019 * @flags: the checks to validate when linking
2021 * Links the source pad and the sink pad.
2023 * This variant of #gst_pad_link provides a more granular control on the
2024 * checks being done when linking. While providing some considerable speedups
2025 * the caller of this method must be aware that wrong usage of those flags
2026 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2027 * for more information.
2031 * Returns: A result code indicating if the connection worked or
2037 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2039 GstPadLinkReturn result;
2042 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2043 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2044 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2045 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2046 GST_PAD_LINK_WRONG_DIRECTION);
2048 /* Notify the parent early. See gst_pad_unlink for details. */
2049 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2050 if (GST_IS_ELEMENT (parent)) {
2051 gst_element_post_message (parent,
2052 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2053 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2055 gst_object_unref (parent);
2060 /* prepare will also lock the two pads */
2061 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2063 if (result != GST_PAD_LINK_OK)
2066 /* must set peers before calling the link function */
2067 GST_PAD_PEER (srcpad) = sinkpad;
2068 GST_PAD_PEER (sinkpad) = srcpad;
2070 GST_OBJECT_UNLOCK (sinkpad);
2071 GST_OBJECT_UNLOCK (srcpad);
2073 /* FIXME released the locks here, concurrent thread might link
2074 * something else. */
2075 if (GST_PAD_LINKFUNC (srcpad)) {
2076 /* this one will call the peer link function */
2077 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2078 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2079 /* if no source link function, we need to call the sink link
2080 * function ourselves. */
2081 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2083 result = GST_PAD_LINK_OK;
2086 GST_OBJECT_LOCK (srcpad);
2087 GST_OBJECT_LOCK (sinkpad);
2089 if (result == GST_PAD_LINK_OK) {
2090 GST_OBJECT_UNLOCK (sinkpad);
2091 GST_OBJECT_UNLOCK (srcpad);
2093 /* fire off a signal to each of the pads telling them
2094 * that they've been linked */
2095 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2096 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2098 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2099 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2101 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2102 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2104 GST_PAD_PEER (srcpad) = NULL;
2105 GST_PAD_PEER (sinkpad) = NULL;
2107 GST_OBJECT_UNLOCK (sinkpad);
2108 GST_OBJECT_UNLOCK (srcpad);
2113 gst_element_post_message (parent,
2114 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2115 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2116 gst_object_unref (parent);
2124 * @srcpad: the source #GstPad to link.
2125 * @sinkpad: the sink #GstPad to link.
2127 * Links the source pad and the sink pad.
2129 * Returns: A result code indicating if the connection worked or
2135 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2137 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2141 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2143 GstPadTemplate **template_p;
2145 /* this function would need checks if it weren't static */
2147 GST_OBJECT_LOCK (pad);
2148 template_p = &pad->padtemplate;
2149 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2150 GST_OBJECT_UNLOCK (pad);
2153 gst_pad_template_pad_created (templ, pad);
2157 * gst_pad_get_pad_template:
2160 * Gets the template for @pad.
2162 * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2163 * if this pad has no template.
2165 * FIXME: currently returns an unrefcounted padtemplate.
2168 gst_pad_get_pad_template (GstPad * pad)
2170 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2172 return GST_PAD_PAD_TEMPLATE (pad);
2176 /* should be called with the pad LOCK held */
2177 /* refs the caps, so caller is responsible for getting it unreffed */
2179 gst_pad_get_caps_unlocked (GstPad * pad)
2181 GstCaps *result = NULL;
2182 GstPadTemplate *templ;
2184 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2186 if (GST_PAD_GETCAPSFUNC (pad)) {
2187 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2188 "dispatching to pad getcaps function");
2190 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2191 GST_OBJECT_UNLOCK (pad);
2192 result = GST_PAD_GETCAPSFUNC (pad) (pad);
2193 GST_OBJECT_LOCK (pad);
2194 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2196 if (result == NULL) {
2197 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2198 GST_DEBUG_PAD_NAME (pad));
2200 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2201 "pad getcaps returned %" GST_PTR_FORMAT, result);
2202 #ifndef G_DISABLE_ASSERT
2203 /* check that the returned caps are a real subset of the template caps */
2204 if (GST_PAD_PAD_TEMPLATE (pad)) {
2205 const GstCaps *templ_caps =
2206 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2207 if (!gst_caps_is_subset (result, templ_caps)) {
2210 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2211 "pad returned caps %" GST_PTR_FORMAT
2212 " which are not a real subset of its template caps %"
2213 GST_PTR_FORMAT, result, templ_caps);
2215 ("pad %s:%s returned caps which are not a real "
2216 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2217 temp = gst_caps_intersect (templ_caps, result);
2218 gst_caps_unref (result);
2226 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2227 result = GST_PAD_TEMPLATE_CAPS (templ);
2228 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2229 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2232 result = gst_caps_ref (result);
2235 if ((result = GST_PAD_CAPS (pad))) {
2236 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2237 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2239 result = gst_caps_ref (result);
2243 /* this almost never happens */
2244 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2245 result = gst_caps_new_empty ();
2251 /* FIXME-0.11: what about making this the default and using
2252 * gst_caps_make_writable() explicitely where needed
2255 * gst_pad_get_caps_reffed:
2256 * @pad: a #GstPad to get the capabilities of.
2258 * Gets the capabilities this pad can produce or consume. Preferred function if
2259 * one only wants to read or intersect the caps.
2261 * Returns: the caps of the pad with incremented ref-count.
2266 gst_pad_get_caps_reffed (GstPad * pad)
2268 GstCaps *result = NULL;
2270 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2272 GST_OBJECT_LOCK (pad);
2274 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2276 result = gst_pad_get_caps_unlocked (pad);
2278 GST_OBJECT_UNLOCK (pad);
2285 * @pad: a #GstPad to get the capabilities of.
2287 * Gets the capabilities this pad can produce or consume.
2288 * Note that this method doesn't necessarily return the caps set by
2289 * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2290 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2291 * the pad's get_caps function;
2292 * this returns the pad template caps if not explicitly set.
2294 * Returns: a newly allocated copy of the #GstCaps of this pad.
2299 gst_pad_get_caps (GstPad * pad)
2301 GstCaps *result = gst_pad_get_caps_reffed (pad);
2303 /* be sure that we have a copy */
2304 if (G_LIKELY (result))
2305 result = gst_caps_make_writable (result);
2310 /* FIXME-0.11: what about making this the default and using
2311 * gst_caps_make_writable() explicitely where needed
2314 * gst_pad_peer_get_caps_reffed:
2315 * @pad: a #GstPad to get the capabilities of.
2317 * Gets the capabilities of the peer connected to this pad. Preferred function
2318 * if one only wants to read or intersect the caps.
2320 * Returns: the caps of the pad with incremented ref-count.
2325 gst_pad_peer_get_caps_reffed (GstPad * pad)
2328 GstCaps *result = NULL;
2330 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2332 GST_OBJECT_LOCK (pad);
2334 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2336 peerpad = GST_PAD_PEER (pad);
2337 if (G_UNLIKELY (peerpad == NULL))
2340 gst_object_ref (peerpad);
2341 GST_OBJECT_UNLOCK (pad);
2343 result = gst_pad_get_caps_reffed (peerpad);
2345 gst_object_unref (peerpad);
2351 GST_OBJECT_UNLOCK (pad);
2357 * gst_pad_peer_get_caps:
2358 * @pad: a #GstPad to get the peer capabilities of.
2360 * Gets the capabilities of the peer connected to this pad. Similar to
2361 * gst_pad_get_caps().
2363 * Returns: a newly allocated copy of the #GstCaps of the peer pad. Use
2364 * gst_caps_unref() to get rid of it. This function returns %NULL if there is
2368 gst_pad_peer_get_caps (GstPad * pad)
2371 GstCaps *result = NULL;
2373 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2375 GST_OBJECT_LOCK (pad);
2377 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2379 peerpad = GST_PAD_PEER (pad);
2380 if (G_UNLIKELY (peerpad == NULL))
2383 gst_object_ref (peerpad);
2384 GST_OBJECT_UNLOCK (pad);
2386 result = gst_pad_get_caps (peerpad);
2388 gst_object_unref (peerpad);
2394 GST_OBJECT_UNLOCK (pad);
2400 fixate_value (GValue * dest, const GValue * src)
2402 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2403 g_value_init (dest, G_TYPE_INT);
2404 g_value_set_int (dest, gst_value_get_int_range_min (src));
2405 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2406 g_value_init (dest, G_TYPE_DOUBLE);
2407 g_value_set_double (dest, gst_value_get_double_range_min (src));
2408 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2409 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2410 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2411 GValue temp = { 0 };
2413 /* list could be empty */
2414 if (gst_value_list_get_size (src) <= 0)
2417 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2419 if (!fixate_value (dest, &temp))
2420 gst_value_init_and_copy (dest, &temp);
2421 g_value_unset (&temp);
2422 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2423 gboolean res = FALSE;
2426 len = gst_value_array_get_size (src);
2427 g_value_init (dest, GST_TYPE_ARRAY);
2428 for (n = 0; n < len; n++) {
2430 const GValue *orig_kid = gst_value_array_get_value (src, n);
2432 if (!fixate_value (&kid, orig_kid))
2433 gst_value_init_and_copy (&kid, orig_kid);
2436 gst_value_array_append_value (dest, &kid);
2437 g_value_unset (&kid);
2441 g_value_unset (dest);
2452 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2454 GstStructure *s = data;
2457 if (fixate_value (&v, value)) {
2458 gst_structure_id_set_value (s, field_id, &v);
2466 * gst_pad_fixate_caps:
2467 * @pad: a #GstPad to fixate
2468 * @caps: the #GstCaps to fixate
2470 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2471 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2474 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2476 GstPadFixateCapsFunction fixatefunc;
2479 g_return_if_fail (GST_IS_PAD (pad));
2480 g_return_if_fail (caps != NULL);
2481 g_return_if_fail (!gst_caps_is_empty (caps));
2482 /* FIXME-0.11: do not allow fixating any-caps
2483 * g_return_if_fail (!gst_caps_is_any (caps));
2486 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2489 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2491 fixatefunc (pad, caps);
2494 /* default fixation */
2495 gst_caps_truncate (caps);
2496 s = gst_caps_get_structure (caps, 0);
2497 gst_structure_foreach (s, gst_pad_default_fixate, s);
2500 /* Default accept caps implementation just checks against
2501 * against the allowed caps for the pad */
2503 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2505 /* get the caps and see if it intersects to something not empty */
2507 gboolean result = FALSE;
2509 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2511 allowed = gst_pad_get_caps_reffed (pad);
2513 goto nothing_allowed;
2515 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2517 result = gst_caps_can_intersect (allowed, caps);
2519 gst_caps_unref (allowed);
2526 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2532 * gst_pad_accept_caps:
2533 * @pad: a #GstPad to check
2534 * @caps: a #GstCaps to check on the pad
2536 * Check if the given pad accepts the caps.
2538 * Returns: TRUE if the pad can accept the caps.
2541 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2544 GstPadAcceptCapsFunction acceptfunc;
2545 GstCaps *existing = NULL;
2547 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2549 /* any pad can be unnegotiated */
2553 /* lock for checking the existing caps */
2554 GST_OBJECT_LOCK (pad);
2555 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2556 /* The current caps on a pad are trivially acceptable */
2557 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2558 if (caps == existing || gst_caps_is_equal (caps, existing))
2561 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2562 GST_OBJECT_UNLOCK (pad);
2564 if (G_LIKELY (acceptfunc)) {
2565 /* we can call the function */
2566 result = acceptfunc (pad, caps);
2567 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2569 /* Only null if the element explicitly unset it */
2570 result = gst_pad_acceptcaps_default (pad, caps);
2571 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2577 GST_DEBUG_OBJECT (pad, "pad had same caps");
2578 GST_OBJECT_UNLOCK (pad);
2584 * gst_pad_peer_accept_caps:
2585 * @pad: a #GstPad to check the peer of
2586 * @caps: a #GstCaps to check on the pad
2588 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2591 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2594 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2599 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2601 GST_OBJECT_LOCK (pad);
2603 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2605 peerpad = GST_PAD_PEER (pad);
2606 if (G_UNLIKELY (peerpad == NULL))
2609 gst_object_ref (peerpad);
2610 /* release lock before calling external methods but keep ref to pad */
2611 GST_OBJECT_UNLOCK (pad);
2613 result = gst_pad_accept_caps (peerpad, caps);
2615 gst_object_unref (peerpad);
2621 GST_OBJECT_UNLOCK (pad);
2628 * @pad: a #GstPad to set the capabilities of.
2629 * @caps: a #GstCaps to set.
2631 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2632 * caps on the pad will be unreffed. This function refs the caps so you should
2633 * unref if as soon as you don't need it anymore.
2634 * It is possible to set NULL caps, which will make the pad unnegotiated
2637 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2638 * or bad parameters were provided to this function.
2643 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2645 GstPadSetCapsFunction setcaps;
2648 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2649 g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2651 GST_OBJECT_LOCK (pad);
2652 existing = GST_PAD_CAPS (pad);
2653 if (existing == caps)
2656 if (gst_caps_is_equal (caps, existing))
2657 goto setting_same_caps;
2659 setcaps = GST_PAD_SETCAPSFUNC (pad);
2661 /* call setcaps function to configure the pad only if the
2662 * caps is not NULL */
2663 if (setcaps != NULL && caps) {
2664 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2665 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2666 GST_OBJECT_UNLOCK (pad);
2667 if (!setcaps (pad, caps))
2669 GST_OBJECT_LOCK (pad);
2670 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2672 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2676 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2677 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %p %" GST_PTR_FORMAT, caps,
2679 GST_OBJECT_UNLOCK (pad);
2681 #if GLIB_CHECK_VERSION(2,26,0)
2682 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2684 g_object_notify ((GObject *) pad, "caps");
2691 GST_OBJECT_UNLOCK (pad);
2696 gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2697 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2698 "caps %p %" GST_PTR_FORMAT " same as existing, updating ptr only", caps,
2700 GST_OBJECT_UNLOCK (pad);
2707 GST_OBJECT_LOCK (pad);
2708 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2709 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2710 "caps %" GST_PTR_FORMAT " could not be set", caps);
2711 GST_OBJECT_UNLOCK (pad);
2718 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2722 /* See if pad accepts the caps */
2723 if (!gst_caps_can_intersect (caps, gst_pad_get_pad_template_caps (pad)))
2726 /* set caps on pad if call succeeds */
2727 res = gst_pad_set_caps (pad, caps);
2728 /* no need to unref the caps here, set_caps takes a ref and
2729 * our ref goes away when we leave this function. */
2735 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2736 "caps %" GST_PTR_FORMAT " not accepted", caps);
2741 /* returns TRUE if the src pad could be configured to accept the given caps */
2743 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2748 /* See if pad accepts the caps */
2749 if (!gst_pad_accept_caps (pad, caps))
2752 res = gst_pad_set_caps (pad, caps);
2760 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2761 "caps %" GST_PTR_FORMAT " not accepted", caps);
2767 * gst_pad_get_pad_template_caps:
2768 * @pad: a #GstPad to get the template capabilities from.
2770 * Gets the capabilities for @pad's template.
2772 * Returns: the #GstCaps of this pad template. If you intend to keep a
2773 * reference on the caps, make a copy (see gst_caps_copy ()).
2776 gst_pad_get_pad_template_caps (GstPad * pad)
2778 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2780 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2782 if (GST_PAD_PAD_TEMPLATE (pad))
2783 return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2785 return gst_static_caps_get (&anycaps);
2790 * @pad: a #GstPad to get the peer of.
2792 * Gets the peer of @pad. This function refs the peer pad so
2793 * you need to unref it after use.
2795 * Returns: the peer #GstPad. Unref after usage.
2800 gst_pad_get_peer (GstPad * pad)
2804 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2806 GST_OBJECT_LOCK (pad);
2807 result = GST_PAD_PEER (pad);
2809 gst_object_ref (result);
2810 GST_OBJECT_UNLOCK (pad);
2816 * gst_pad_get_allowed_caps:
2819 * Gets the capabilities of the allowed media types that can flow through
2820 * @pad and its peer.
2822 * The allowed capabilities is calculated as the intersection of the results of
2823 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2824 * on the resulting caps.
2826 * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2827 * longer need it. This function returns NULL when @pad has no peer.
2832 gst_pad_get_allowed_caps (GstPad * pad)
2839 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2841 GST_OBJECT_LOCK (pad);
2843 peer = GST_PAD_PEER (pad);
2844 if (G_UNLIKELY (peer == NULL))
2847 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2849 gst_object_ref (peer);
2850 GST_OBJECT_UNLOCK (pad);
2851 mycaps = gst_pad_get_caps_reffed (pad);
2853 peercaps = gst_pad_get_caps_reffed (peer);
2854 gst_object_unref (peer);
2856 caps = gst_caps_intersect (mycaps, peercaps);
2857 gst_caps_unref (peercaps);
2858 gst_caps_unref (mycaps);
2860 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2867 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2868 GST_OBJECT_UNLOCK (pad);
2875 * gst_pad_get_negotiated_caps:
2878 * Gets the capabilities of the media type that currently flows through @pad
2881 * This function can be used on both src and sinkpads. Note that srcpads are
2882 * always negotiated before sinkpads so it is possible that the negotiated caps
2883 * on the srcpad do not match the negotiated caps of the peer.
2885 * Returns: the negotiated #GstCaps of the pad link. Unref the caps when
2886 * you no longer need it. This function returns NULL when the @pad has no
2887 * peer or is not negotiated yet.
2892 gst_pad_get_negotiated_caps (GstPad * pad)
2897 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2899 GST_OBJECT_LOCK (pad);
2901 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2904 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2906 caps = GST_PAD_CAPS (pad);
2908 gst_caps_ref (caps);
2909 GST_OBJECT_UNLOCK (pad);
2911 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2918 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2919 GST_OBJECT_UNLOCK (pad);
2925 /* calls the buffer_alloc function on the given pad */
2926 static GstFlowReturn
2927 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2928 GstCaps * caps, GstBuffer ** buf)
2931 GstPadBufferAllocFunction bufferallocfunc;
2933 GST_OBJECT_LOCK (pad);
2934 /* when the pad is flushing we cannot give a buffer */
2935 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2938 bufferallocfunc = pad->bufferallocfunc;
2940 if (offset == GST_BUFFER_OFFSET_NONE) {
2941 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2942 "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2943 GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2945 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2946 "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2947 G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2948 bufferallocfunc, size, offset);
2950 GST_OBJECT_UNLOCK (pad);
2952 /* G_LIKELY for now since most elements don't implement a buffer alloc
2953 * function and there is no default alloc proxy function as this is usually
2955 if (G_LIKELY (bufferallocfunc == NULL))
2958 ret = bufferallocfunc (pad, offset, size, caps, buf);
2960 if (G_UNLIKELY (ret != GST_FLOW_OK))
2963 /* no error, but NULL buffer means fallback to the default */
2964 if (G_UNLIKELY (*buf == NULL))
2967 /* If the buffer alloc function didn't set up the caps like it should,
2969 if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2970 GST_WARNING_OBJECT (pad,
2971 "Buffer allocation function did not set caps. Setting");
2972 gst_buffer_set_caps (*buf, caps);
2978 /* pad was flushing */
2979 GST_OBJECT_UNLOCK (pad);
2980 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2981 return GST_FLOW_WRONG_STATE;
2985 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2986 "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2991 /* fallback case, allocate a buffer of our own, add pad caps. */
2992 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2994 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2995 GST_BUFFER_OFFSET (*buf) = offset;
2996 gst_buffer_set_caps (*buf, caps);
2999 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3000 "out of memory allocating %d bytes", size);
3001 return GST_FLOW_ERROR;
3006 /* FIXME 0.11: size should be unsigned */
3007 static GstFlowReturn
3008 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
3009 GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
3014 gboolean caps_changed;
3016 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3017 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3018 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
3019 g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
3021 GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
3022 GST_PTR_FORMAT, offset, size, caps);
3024 GST_OBJECT_LOCK (pad);
3025 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3026 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3029 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3032 gst_object_ref (peer);
3033 GST_OBJECT_UNLOCK (pad);
3035 ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
3036 gst_object_unref (peer);
3038 if (G_UNLIKELY (ret != GST_FLOW_OK))
3041 /* FIXME, move capnego this into a base class? */
3042 newcaps = GST_BUFFER_CAPS (*buf);
3044 /* Lock for checking caps, pretty pointless as the _pad_push() function might
3045 * change it concurrently, one of the problems with automatic caps setting in
3046 * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
3047 * when there is heavy renegotiation going on in both directions. */
3048 GST_OBJECT_LOCK (pad);
3049 caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
3050 GST_OBJECT_UNLOCK (pad);
3052 /* we got a new datatype on the pad, see if it can handle it */
3053 if (G_UNLIKELY (caps_changed)) {
3054 GST_DEBUG_OBJECT (pad,
3055 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
3056 GST_PAD_CAPS (pad), newcaps, newcaps);
3057 if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
3058 goto not_negotiated;
3061 /* sanity check (only if caps are the same) */
3062 if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
3063 goto wrong_size_fallback;
3069 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
3070 GST_OBJECT_UNLOCK (pad);
3075 /* pad has no peer */
3076 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3077 "called bufferallocfunc but had no peer");
3078 GST_OBJECT_UNLOCK (pad);
3079 return GST_FLOW_NOT_LINKED;
3083 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3084 "alloc function returned error %s", gst_flow_get_name (ret));
3089 gst_buffer_unref (*buf);
3091 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3092 "alloc function returned unacceptable buffer");
3093 return GST_FLOW_NOT_NEGOTIATED;
3095 wrong_size_fallback:
3097 GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
3098 "function is too small (%u < %d), doing fallback buffer alloc",
3099 GST_BUFFER_SIZE (*buf), size);
3101 gst_buffer_unref (*buf);
3103 if ((*buf = gst_buffer_try_new_and_alloc (size))) {
3104 GST_BUFFER_OFFSET (*buf) = offset;
3105 gst_buffer_set_caps (*buf, caps);
3108 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3109 "out of memory allocating %d bytes", size);
3110 return GST_FLOW_ERROR;
3116 * gst_pad_alloc_buffer:
3117 * @pad: a source #GstPad
3118 * @offset: the offset of the new buffer in the stream
3119 * @size: the size of the new buffer
3120 * @caps: the caps of the new buffer
3121 * @buf: a newly allocated buffer
3123 * Allocates a new, empty buffer optimized to push to pad @pad. This
3124 * function only works if @pad is a source pad and has a peer.
3126 * A new, empty #GstBuffer will be put in the @buf argument.
3127 * You need to check the caps of the buffer after performing this
3128 * function and renegotiate to the format if needed. If the caps changed, it is
3129 * possible that the buffer returned in @buf is not of the right size for the
3130 * new format, @buf needs to be unreffed and reallocated if this is the case.
3132 * Returns: a result code indicating success of the operation. Any
3133 * result code other than #GST_FLOW_OK is an error and @buf should
3135 * An error can occur if the pad is not connected or when the downstream
3136 * peer elements cannot provide an acceptable buffer.
3141 /* FIXME 0.11: size should be unsigned */
3143 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3146 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3150 * gst_pad_alloc_buffer_and_set_caps:
3151 * @pad: a source #GstPad
3152 * @offset: the offset of the new buffer in the stream
3153 * @size: the size of the new buffer
3154 * @caps: the caps of the new buffer
3155 * @buf: a newly allocated buffer
3157 * In addition to the function gst_pad_alloc_buffer(), this function
3158 * automatically calls gst_pad_set_caps() when the caps of the
3159 * newly allocated buffer are different from the @pad caps.
3161 * After a renegotiation, the size of the new buffer returned in @buf could
3162 * be of the wrong size for the new format and must be unreffed an reallocated
3165 * Returns: a result code indicating success of the operation. Any
3166 * result code other than #GST_FLOW_OK is an error and @buf should
3168 * An error can occur if the pad is not connected or when the downstream
3169 * peer elements cannot provide an acceptable buffer.
3174 /* FIXME 0.11: size should be unsigned */
3176 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3177 GstCaps * caps, GstBuffer ** buf)
3179 return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3183 #ifndef GST_REMOVE_DEPRECATED
3191 int_link_iter_data_free (IntLinkIterData * data)
3193 g_list_free (data->list);
3194 g_slice_free (IntLinkIterData, data);
3198 static GstIteratorItem
3199 iterate_pad (GstIterator * it, GstPad * pad)
3201 gst_object_ref (pad);
3202 return GST_ITERATOR_ITEM_PASS;
3206 * gst_pad_iterate_internal_links_default:
3207 * @pad: the #GstPad to get the internal links of.
3209 * Iterate the list of pads to which the given pad is linked to inside of
3210 * the parent element.
3211 * This is the default handler, and thus returns an iterator of all of the
3212 * pads inside the parent element with opposite direction.
3214 * The caller must free this iterator after use with gst_iterator_free().
3216 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3217 * returned pad with gst_object_unref().
3222 gst_pad_iterate_internal_links_default (GstPad * pad)
3229 GstIteratorDisposeFunction dispose;
3231 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3233 #ifndef GST_REMOVE_DEPRECATED
3234 /* when we get here, the default handler for the iterate links is called,
3235 * which means that the user has not installed a custom one. We first check if
3236 * there is maybe a custom legacy function we can call. */
3237 if (GST_PAD_INTLINKFUNC (pad) &&
3238 GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3239 IntLinkIterData *data;
3241 /* make an iterator for the list. We can't protect the list with a
3242 * cookie. If we would take the cookie of the parent element, we need to
3243 * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3244 * cache the per-pad list and invalidate the list when a new call to
3245 * INTLINKFUNC() returned a different list but then this would only work if
3246 * two concurrent iterators were used and the last iterator would still be
3247 * thread-unsafe. Just don't use this method anymore. */
3248 data = g_slice_new (IntLinkIterData);
3249 data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3252 GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3254 cookie = &data->cookie;
3255 padlist = &data->list;
3257 dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3258 /* reuse the pad lock, it's all we have here */
3259 lock = GST_OBJECT_GET_LOCK (pad);
3265 GST_OBJECT_LOCK (pad);
3266 parent = GST_PAD_PARENT (pad);
3267 if (!parent || !GST_IS_ELEMENT (parent))
3270 gst_object_ref (parent);
3271 GST_OBJECT_UNLOCK (pad);
3273 if (pad->direction == GST_PAD_SRC)
3274 padlist = &parent->sinkpads;
3276 padlist = &parent->srcpads;
3278 GST_DEBUG_OBJECT (pad, "Making iterator");
3280 cookie = &parent->pads_cookie;
3282 dispose = (GstIteratorDisposeFunction) gst_object_unref;
3283 lock = GST_OBJECT_GET_LOCK (parent);
3286 res = gst_iterator_new_list (GST_TYPE_PAD,
3287 lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3295 GST_OBJECT_UNLOCK (pad);
3296 GST_DEBUG_OBJECT (pad, "no parent element");
3302 * gst_pad_iterate_internal_links:
3303 * @pad: the GstPad to get the internal links of.
3305 * Gets an iterator for the pads to which the given pad is linked to inside
3306 * of the parent element.
3308 * Each #GstPad element yielded by the iterator will have its refcount increased,
3309 * so unref after use.
3311 * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3312 * iterator function configured. Use gst_iterator_free() after usage.
3317 gst_pad_iterate_internal_links (GstPad * pad)
3319 GstIterator *res = NULL;
3321 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3323 if (GST_PAD_ITERINTLINKFUNC (pad))
3324 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3329 #ifndef GST_REMOVE_DEPRECATED
3331 add_unref_pad_to_list (GstPad * pad, GList ** list)
3333 *list = g_list_prepend (*list, pad);
3334 gst_object_unref (pad);
3339 * gst_pad_get_internal_links_default:
3340 * @pad: the #GstPad to get the internal links of.
3342 * Gets a list of pads to which the given pad is linked to
3343 * inside of the parent element.
3344 * This is the default handler, and thus returns a list of all of the
3345 * pads inside the parent element with opposite direction.
3347 * The caller must free this list after use with g_list_free().
3349 * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3353 * Deprecated: This function does not ref the pads in the list so that they
3354 * could become invalid by the time the application accesses them. It's also
3355 * possible that the list changes while handling the pads, which the caller of
3356 * this function is unable to know. Use the thread-safe
3357 * gst_pad_iterate_internal_links_default() instead.
3359 #ifndef GST_REMOVE_DEPRECATED
3361 gst_pad_get_internal_links_default (GstPad * pad)
3366 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3368 GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3370 /* when we get here, the default handler for get_internal_links is called,
3371 * which means that the user has not installed a custom one. We first check if
3372 * there is maybe a custom iterate function we can call. */
3373 if (GST_PAD_ITERINTLINKFUNC (pad) &&
3374 GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3376 GstIteratorResult ires;
3377 gboolean done = FALSE;
3379 it = gst_pad_iterate_internal_links (pad);
3380 /* loop over the iterator and put all elements into a list, we also
3381 * immediatly unref them, which is bad. */
3383 ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, &res);
3385 case GST_ITERATOR_OK:
3386 case GST_ITERATOR_DONE:
3387 case GST_ITERATOR_ERROR:
3390 case GST_ITERATOR_RESYNC:
3391 /* restart, discard previous list */
3392 gst_iterator_resync (it);
3399 gst_iterator_free (it);
3401 /* lock pad, check and ref parent */
3402 GST_OBJECT_LOCK (pad);
3403 parent = GST_PAD_PARENT (pad);
3404 if (!parent || !GST_IS_ELEMENT (parent))
3407 parent = gst_object_ref (parent);
3408 GST_OBJECT_UNLOCK (pad);
3410 /* now lock the parent while we copy the pads */
3411 GST_OBJECT_LOCK (parent);
3412 if (pad->direction == GST_PAD_SRC)
3413 res = g_list_copy (parent->sinkpads);
3415 res = g_list_copy (parent->srcpads);
3416 GST_OBJECT_UNLOCK (parent);
3418 gst_object_unref (parent);
3421 /* At this point pads can be changed and unreffed. Nothing we can do about it
3422 * because for compatibility reasons this function cannot ref the pads or
3423 * notify the app that the list changed. */
3429 GST_DEBUG_OBJECT (pad, "no parent");
3430 GST_OBJECT_UNLOCK (pad);
3434 #endif /* GST_REMOVE_DEPRECATED */
3437 * gst_pad_get_internal_links:
3438 * @pad: the #GstPad to get the internal links of.
3440 * Gets a list of pads to which the given pad is linked to
3441 * inside of the parent element.
3442 * The caller must free this list after use.
3446 * Returns: a newly allocated #GList of pads, free with g_list_free().
3448 * Deprecated: This function does not ref the pads in the list so that they
3449 * could become invalid by the time the application accesses them. It's also
3450 * possible that the list changes while handling the pads, which the caller of
3451 * this function is unable to know. Use the thread-safe
3452 * gst_pad_iterate_internal_links() instead.
3454 #ifndef GST_REMOVE_DEPRECATED
3455 #ifdef GST_DISABLE_DEPRECATED
3456 GList *gst_pad_get_internal_links (GstPad * pad);
3459 gst_pad_get_internal_links (GstPad * pad)
3463 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3465 GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3467 if (GST_PAD_INTLINKFUNC (pad))
3468 res = GST_PAD_INTLINKFUNC (pad) (pad);
3472 #endif /* GST_REMOVE_DEPRECATED */
3475 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3477 gboolean result = FALSE;
3479 gboolean done = FALSE;
3482 GList *pushed_pads = NULL;
3484 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3485 event, GST_EVENT_TYPE_NAME (event));
3487 iter = gst_pad_iterate_internal_links (pad);
3493 switch (gst_iterator_next (iter, &item)) {
3494 case GST_ITERATOR_OK:
3495 eventpad = GST_PAD_CAST (item);
3497 /* if already pushed, skip */
3498 if (g_list_find (pushed_pads, eventpad)) {
3499 gst_object_unref (item);
3503 if (GST_PAD_IS_SRC (eventpad)) {
3504 /* for each pad we send to, we should ref the event; it's up
3505 * to downstream to unref again when handled. */
3506 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3507 event, GST_EVENT_TYPE_NAME (event),
3508 GST_DEBUG_PAD_NAME (eventpad));
3509 gst_event_ref (event);
3510 result |= gst_pad_push_event (eventpad, event);
3512 /* we only send the event on one pad, multi-sinkpad elements
3513 * should implement a handler */
3514 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3515 event, GST_EVENT_TYPE_NAME (event),
3516 GST_DEBUG_PAD_NAME (eventpad));
3517 result = gst_pad_push_event (eventpad, event);
3522 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3524 gst_object_unref (item);
3526 case GST_ITERATOR_RESYNC:
3527 /* FIXME, if we want to reset the result value we need to remember which
3528 * pads pushed with which result */
3529 gst_iterator_resync (iter);
3531 case GST_ITERATOR_ERROR:
3532 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3535 case GST_ITERATOR_DONE:
3540 gst_iterator_free (iter);
3544 /* If this is a sinkpad and we don't have pads to send the event to, we
3545 * return TRUE. This is so that when using the default handler on a sink
3546 * element, we don't fail to push it. */
3548 result = GST_PAD_IS_SINK (pad);
3550 g_list_free (pushed_pads);
3552 /* we handled the incoming event so we unref once */
3554 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3555 gst_event_unref (event);
3562 * gst_pad_event_default:
3563 * @pad: a #GstPad to call the default event handler on.
3564 * @event: the #GstEvent to handle.
3566 * Invokes the default event handler for the given pad. End-of-stream and
3567 * discontinuity events are handled specially, and then the event is sent to all
3568 * pads internally linked to @pad. Note that if there are many possible sink
3569 * pads that are internally linked to @pad, only one will be sent an event.
3570 * Multi-sinkpad elements should implement custom event handlers.
3572 * Returns: TRUE if the event was sent succesfully.
3575 gst_pad_event_default (GstPad * pad, GstEvent * event)
3577 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3578 g_return_val_if_fail (event != NULL, FALSE);
3580 GST_LOG_OBJECT (pad, "default event handler");
3582 switch (GST_EVENT_TYPE (event)) {
3585 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3586 gst_pad_pause_task (pad);
3593 return gst_pad_event_default_dispatch (pad, event);
3597 * gst_pad_dispatcher:
3598 * @pad: a #GstPad to dispatch.
3599 * @dispatch: the #GstPadDispatcherFunction to call.
3600 * @data: gpointer user data passed to the dispatcher function.
3602 * Invokes the given dispatcher function on each respective peer of
3603 * all pads that are internally linked to the given pad.
3604 * The GstPadDispatcherFunction should return TRUE when no further pads
3605 * need to be processed.
3607 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3610 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3613 gboolean res = FALSE;
3614 GstIterator *iter = NULL;
3615 gboolean done = FALSE;
3618 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3619 g_return_val_if_fail (dispatch != NULL, FALSE);
3621 iter = gst_pad_iterate_internal_links (pad);
3627 switch (gst_iterator_next (iter, &item)) {
3628 case GST_ITERATOR_OK:
3630 GstPad *int_pad = GST_PAD_CAST (item);
3631 GstPad *int_peer = gst_pad_get_peer (int_pad);
3634 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3635 GST_DEBUG_PAD_NAME (int_peer));
3636 done = res = dispatch (int_peer, data);
3637 gst_object_unref (int_peer);
3639 GST_DEBUG_OBJECT (int_pad, "no peer");
3642 gst_object_unref (item);
3644 case GST_ITERATOR_RESYNC:
3645 gst_iterator_resync (iter);
3647 case GST_ITERATOR_ERROR:
3649 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3651 case GST_ITERATOR_DONE:
3656 gst_iterator_free (iter);
3658 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3667 * @pad: a #GstPad to invoke the default query on.
3668 * @query: the #GstQuery to perform.
3670 * Dispatches a query to a pad. The query should have been allocated by the
3671 * caller via one of the type-specific allocation functions in gstquery.h. The
3672 * element is responsible for filling the query with an appropriate response,
3673 * which should then be parsed with a type-specific query parsing function.
3675 * Again, the caller is responsible for both the allocation and deallocation of
3676 * the query structure.
3678 * Returns: TRUE if the query could be performed.
3681 gst_pad_query (GstPad * pad, GstQuery * query)
3683 GstPadQueryFunction func;
3685 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3686 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3688 GST_DEBUG_OBJECT (pad, "sending query %p", query);
3690 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3693 return func (pad, query);
3697 GST_DEBUG_OBJECT (pad, "had no query function");
3703 * gst_pad_peer_query:
3704 * @pad: a #GstPad to invoke the peer query on.
3705 * @query: the #GstQuery to perform.
3707 * Performs gst_pad_query() on the peer of @pad.
3709 * The caller is responsible for both the allocation and deallocation of
3710 * the query structure.
3712 * Returns: TRUE if the query could be performed. This function returns %FALSE
3713 * if @pad has no peer.
3718 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3723 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3724 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3726 GST_OBJECT_LOCK (pad);
3728 GST_DEBUG_OBJECT (pad, "peer query");
3730 peerpad = GST_PAD_PEER (pad);
3731 if (G_UNLIKELY (peerpad == NULL))
3734 gst_object_ref (peerpad);
3735 GST_OBJECT_UNLOCK (pad);
3737 result = gst_pad_query (peerpad, query);
3739 gst_object_unref (peerpad);
3746 GST_WARNING_OBJECT (pad, "pad has no peer");
3747 GST_OBJECT_UNLOCK (pad);
3753 * gst_pad_query_default:
3754 * @pad: a #GstPad to call the default query handler on.
3755 * @query: the #GstQuery to handle.
3757 * Invokes the default query handler for the given pad.
3758 * The query is sent to all pads internally linked to @pad. Note that
3759 * if there are many possible sink pads that are internally linked to
3760 * @pad, only one will be sent the query.
3761 * Multi-sinkpad elements should implement custom query handlers.
3763 * Returns: TRUE if the query was performed succesfully.
3766 gst_pad_query_default (GstPad * pad, GstQuery * query)
3768 switch (GST_QUERY_TYPE (query)) {
3769 case GST_QUERY_POSITION:
3770 case GST_QUERY_SEEKING:
3771 case GST_QUERY_FORMATS:
3772 case GST_QUERY_LATENCY:
3773 case GST_QUERY_JITTER:
3774 case GST_QUERY_RATE:
3775 case GST_QUERY_CONVERT:
3777 return gst_pad_dispatcher
3778 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3782 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
3783 /* FIXME: why isn't this on a GstElement ? */
3785 * gst_pad_load_and_link:
3786 * @self: an #xmlNodePtr to read the description from.
3787 * @parent: the #GstObject element that owns the pad.
3789 * Reads the pad definition from the XML node and links the given pad
3790 * in the element to a pad of an element up in the hierarchy.
3793 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3795 xmlNodePtr field = self->xmlChildrenNode;
3796 GstPad *pad = NULL, *targetpad;
3797 GstPadTemplate *tmpl;
3801 GstObject *grandparent;
3805 if (!strcmp ((char *) field->name, "name")) {
3806 name = (gchar *) xmlNodeGetContent (field);
3807 pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3808 if ((!pad) || ((tmpl = gst_pad_get_pad_template (pad))
3809 && (GST_PAD_REQUEST == GST_PAD_TEMPLATE_PRESENCE (tmpl))))
3810 pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3812 } else if (!strcmp ((char *) field->name, "peer")) {
3813 peer = (gchar *) xmlNodeGetContent (field);
3815 field = field->next;
3817 g_return_if_fail (pad != NULL);
3822 split = g_strsplit (peer, ".", 2);
3824 if (split[0] == NULL || split[1] == NULL) {
3825 GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3826 "Could not parse peer '%s', leaving unlinked", peer);
3833 g_return_if_fail (split[0] != NULL);
3834 g_return_if_fail (split[1] != NULL);
3836 grandparent = gst_object_get_parent (parent);
3838 if (grandparent && GST_IS_BIN (grandparent)) {
3839 target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3846 targetpad = gst_element_get_static_pad (target, split[1]);
3848 targetpad = gst_element_get_request_pad (target, split[1]);
3850 if (targetpad == NULL)
3853 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
3854 gst_pad_link (pad, targetpad);
3856 gst_pad_link (targetpad, pad);
3863 * gst_pad_save_thyself:
3864 * @pad: a #GstPad to save.
3865 * @parent: the parent #xmlNodePtr to save the description in.
3867 * Saves the pad into an xml representation.
3869 * Returns: the #xmlNodePtr representation of the pad.
3872 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3877 g_return_val_if_fail (GST_IS_PAD (object), NULL);
3879 pad = GST_PAD_CAST (object);
3881 xmlNewChild (parent, NULL, (xmlChar *) "name",
3882 (xmlChar *) GST_PAD_NAME (pad));
3884 if (GST_PAD_IS_SRC (pad)) {
3885 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3886 } else if (GST_PAD_IS_SINK (pad)) {
3887 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3889 xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3892 if (GST_PAD_PEER (pad) != NULL) {
3895 peer = GST_PAD_PEER (pad);
3896 /* first check to see if the peer's parent's parent is the same */
3897 /* we just save it off */
3898 content = g_strdup_printf ("%s.%s",
3899 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3900 xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3903 xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3910 * gst_ghost_pad_save_thyself:
3911 * @pad: a ghost #GstPad to save.
3912 * @parent: the parent #xmlNodePtr to save the description in.
3914 * Saves the ghost pad into an xml representation.
3916 * Returns: the #xmlNodePtr representation of the pad.
3919 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3923 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3925 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3926 xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3927 xmlNewChild (self, NULL, (xmlChar *) "parent",
3928 (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3930 /* FIXME FIXME FIXME! */
3935 #endif /* GST_DISABLE_LOADSAVE */
3938 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3939 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3942 * This function performs the pad blocking when an event, buffer push
3943 * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3944 * streaming thread after informing the pad has been blocked.
3946 * An application can with this method wait and block any streaming
3947 * thread and perform operations such as seeking or linking.
3949 * Two methods are available for notifying the application of the
3951 * - the callback method, which happens in the STREAMING thread with
3952 * the STREAM_LOCK held. With this method, the most useful way of
3953 * dealing with the callback is to post a message to the main thread
3954 * where the pad block can then be handled outside of the streaming
3955 * thread. With the last method one can perform all operations such
3956 * as doing a state change, linking, unblocking, seeking etc on the
3958 * - the GCond signal method, which makes any thread unblock when
3959 * the pad block happens.
3961 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3962 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3966 static GstFlowReturn
3967 handle_pad_block (GstPad * pad)
3969 GstPadBlockCallback callback;
3971 GstFlowReturn ret = GST_FLOW_OK;
3973 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3975 /* flushing, don't bother trying to block and return WRONG_STATE
3977 if (GST_PAD_IS_FLUSHING (pad))
3978 goto flushingnonref;
3980 /* we grab an extra ref for the callbacks, this is probably not
3981 * needed (callback code does not have a ref and cannot unref). I
3982 * think this was done to make it possible to unref the element in
3983 * the callback, which is in the end totally impossible as it
3984 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3985 * all taken when calling this function. */
3986 gst_object_ref (pad);
3988 while (GST_PAD_IS_BLOCKED (pad)) {
3990 /* we either have a callback installed to notify the block or
3991 * some other thread is doing a GCond wait. */
3992 callback = pad->block_callback;
3993 pad->abidata.ABI.block_callback_called = TRUE;
3995 /* there is a callback installed, call it. We release the
3996 * lock so that the callback can do something usefull with the
3998 user_data = pad->block_data;
3999 GST_OBJECT_UNLOCK (pad);
4000 callback (pad, TRUE, user_data);
4001 GST_OBJECT_LOCK (pad);
4003 /* we released the lock, recheck flushing */
4004 if (GST_PAD_IS_FLUSHING (pad))
4007 /* no callback, signal the thread that is doing a GCond wait
4009 GST_PAD_BLOCK_BROADCAST (pad);
4011 } while (pad->abidata.ABI.block_callback_called == FALSE
4012 && GST_PAD_IS_BLOCKED (pad));
4014 /* OBJECT_LOCK could have been released when we did the callback, which
4015 * then could have made the pad unblock so we need to check the blocking
4016 * condition again. */
4017 if (!GST_PAD_IS_BLOCKED (pad))
4020 /* now we block the streaming thread. It can be unlocked when we
4021 * deactivate the pad (which will also set the FLUSHING flag) or
4022 * when the pad is unblocked. A flushing event will also unblock
4023 * the pad after setting the FLUSHING flag. */
4024 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4025 "Waiting to be unblocked or set flushing");
4026 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
4027 GST_PAD_BLOCK_WAIT (pad);
4028 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
4030 /* see if we got unblocked by a flush or not */
4031 if (GST_PAD_IS_FLUSHING (pad))
4035 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
4037 /* when we get here, the pad is unblocked again and we perform
4038 * the needed unblock code. */
4039 callback = pad->block_callback;
4041 /* we need to call the callback */
4042 user_data = pad->block_data;
4043 GST_OBJECT_UNLOCK (pad);
4044 callback (pad, FALSE, user_data);
4045 GST_OBJECT_LOCK (pad);
4047 /* we need to signal the thread waiting on the GCond */
4048 GST_PAD_BLOCK_BROADCAST (pad);
4051 gst_object_unref (pad);
4057 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
4058 return GST_FLOW_WRONG_STATE;
4062 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
4063 gst_object_unref (pad);
4064 return GST_FLOW_WRONG_STATE;
4068 /**********************************************************************
4069 * Data passing functions
4073 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
4076 GValue args[2] = { {0}, {0} };
4081 g_value_init (&ret, G_TYPE_BOOLEAN);
4082 g_value_set_boolean (&ret, TRUE);
4083 g_value_init (&args[0], GST_TYPE_PAD);
4084 g_value_set_object (&args[0], pad);
4085 g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
4086 gst_value_set_mini_object (&args[1], obj);
4088 if (GST_IS_EVENT (obj))
4089 detail = event_quark;
4091 detail = buffer_quark;
4094 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
4095 res = g_value_get_boolean (&ret);
4098 g_value_unset (&ret);
4099 g_value_unset (&args[0]);
4100 g_value_unset (&args[1]);
4106 gst_pad_data_unref (gboolean is_buffer, void *data)
4108 if (G_LIKELY (is_buffer)) {
4109 gst_buffer_unref (data);
4111 gst_buffer_list_unref (data);
4116 gst_pad_data_get_caps (gboolean is_buffer, void *data)
4120 if (G_LIKELY (is_buffer)) {
4121 caps = GST_BUFFER_CAPS (data);
4125 if ((buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (data), 0, 0)))
4126 caps = GST_BUFFER_CAPS (buf);
4133 /* this is the chain function that does not perform the additional argument
4134 * checking for that little extra speed.
4136 static inline GstFlowReturn
4137 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
4140 gboolean caps_changed;
4142 gboolean emit_signal;
4144 GST_PAD_STREAM_LOCK (pad);
4146 GST_OBJECT_LOCK (pad);
4147 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4150 caps = gst_pad_data_get_caps (is_buffer, data);
4151 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4153 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4154 GST_OBJECT_UNLOCK (pad);
4156 /* see if the signal should be emited, we emit before caps nego as
4157 * we might drop the buffer and do capsnego for nothing. */
4158 if (G_UNLIKELY (emit_signal)) {
4159 if (G_LIKELY (is_buffer)) {
4160 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4163 /* chain all groups in the buffer list one by one to avoid problems with
4164 * buffer probes that push buffers or events */
4169 /* we got a new datatype on the pad, see if it can handle it */
4170 if (G_UNLIKELY (caps_changed)) {
4171 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4172 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4173 goto not_negotiated;
4176 /* NOTE: we read the chainfunc unlocked.
4177 * we cannot hold the lock for the pad so we might send
4178 * the data to the wrong function. This is not really a
4179 * problem since functions are assigned at creation time
4180 * and don't change that often... */
4181 if (G_LIKELY (is_buffer)) {
4182 GstPadChainFunction chainfunc;
4184 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4187 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4188 "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4190 ret = chainfunc (pad, GST_BUFFER_CAST (data));
4192 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4193 "called chainfunction &%s, returned %s",
4194 GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4196 GstPadChainListFunction chainlistfunc;
4198 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4201 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4202 "calling chainlistfunction &%s",
4203 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4205 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4207 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4208 "called chainlistfunction &%s, returned %s",
4209 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4212 GST_PAD_STREAM_UNLOCK (pad);
4218 GstBufferList *list;
4219 GstBufferListIterator *it;
4222 GST_PAD_STREAM_UNLOCK (pad);
4224 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4226 list = GST_BUFFER_LIST_CAST (data);
4227 it = gst_buffer_list_iterate (list);
4229 if (gst_buffer_list_iterator_next_group (it)) {
4231 group = gst_buffer_list_iterator_merge_group (it);
4232 if (group == NULL) {
4233 group = gst_buffer_new ();
4234 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4236 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4238 ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4239 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4241 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4242 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4245 gst_buffer_list_iterator_free (it);
4246 gst_buffer_list_unref (list);
4254 gst_pad_data_unref (is_buffer, data);
4255 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4256 "pushing, but pad was flushing");
4257 GST_OBJECT_UNLOCK (pad);
4258 GST_PAD_STREAM_UNLOCK (pad);
4259 return GST_FLOW_WRONG_STATE;
4263 gst_pad_data_unref (is_buffer, data);
4264 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4265 GST_PAD_STREAM_UNLOCK (pad);
4270 gst_pad_data_unref (is_buffer, data);
4271 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4272 "pushing data but pad did not accept");
4273 GST_PAD_STREAM_UNLOCK (pad);
4274 return GST_FLOW_NOT_NEGOTIATED;
4278 gst_pad_data_unref (is_buffer, data);
4279 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4280 "pushing, but not chainhandler");
4281 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4282 ("push on pad %s:%s but it has no chainfunction",
4283 GST_DEBUG_PAD_NAME (pad)));
4284 GST_PAD_STREAM_UNLOCK (pad);
4285 return GST_FLOW_NOT_SUPPORTED;
4291 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4292 * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4294 * Chain a buffer to @pad.
4296 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4298 * If the caps on @buffer are different from the current caps on @pad, this
4299 * function will call any setcaps function (see gst_pad_set_setcaps_function())
4300 * installed on @pad. If the new caps are not acceptable for @pad, this
4301 * function returns #GST_FLOW_NOT_NEGOTIATED.
4303 * The function proceeds calling the chain function installed on @pad (see
4304 * gst_pad_set_chain_function()) and the return value of that function is
4305 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4308 * In all cases, success or failure, the caller loses its reference to @buffer
4309 * after calling this function.
4311 * Returns: a #GstFlowReturn from the pad.
4316 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4318 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4319 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4320 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4322 return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4326 * gst_pad_chain_list:
4327 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4328 * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4330 * Chain a bufferlist to @pad.
4332 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4334 * If the caps on the first buffer of @list are different from the current
4335 * caps on @pad, this function will call any setcaps function
4336 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4337 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4339 * The function proceeds calling the chainlist function installed on @pad (see
4340 * gst_pad_set_chain_list_function()) and the return value of that function is
4341 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4342 * chainlist function.
4344 * In all cases, success or failure, the caller loses its reference to @list
4345 * after calling this function.
4349 * Returns: a #GstFlowReturn from the pad.
4354 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4356 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4357 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4358 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4360 return gst_pad_chain_data_unchecked (pad, FALSE, list);
4363 static GstFlowReturn
4364 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4369 gboolean caps_changed;
4371 GST_OBJECT_LOCK (pad);
4373 /* FIXME: this check can go away; pad_set_blocked could be implemented with
4374 * probes completely or probes with an extended pad block. */
4375 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4376 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4379 /* we emit signals on the pad arg, the peer will have a chance to
4380 * emit in the _chain() function */
4381 if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4382 /* unlock before emitting */
4383 GST_OBJECT_UNLOCK (pad);
4385 if (G_LIKELY (is_buffer)) {
4386 /* if the signal handler returned FALSE, it means we should just drop the
4388 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4391 /* push all buffers in the list */
4394 GST_OBJECT_LOCK (pad);
4397 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4400 /* Before pushing the buffer to the peer pad, ensure that caps
4401 * are set on this pad */
4402 caps = gst_pad_data_get_caps (is_buffer, data);
4403 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4405 /* take ref to peer pad before releasing the lock */
4406 gst_object_ref (peer);
4408 GST_OBJECT_UNLOCK (pad);
4410 /* we got a new datatype from the pad, it had better handle it */
4411 if (G_UNLIKELY (caps_changed)) {
4412 GST_DEBUG_OBJECT (pad,
4413 "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4414 GST_PAD_CAPS (pad), caps, caps);
4415 if (G_UNLIKELY (!gst_pad_set_caps (pad, caps)))
4416 goto not_negotiated;
4419 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4421 gst_object_unref (peer);
4427 GstBufferList *list;
4428 GstBufferListIterator *it;
4431 GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4433 list = GST_BUFFER_LIST_CAST (data);
4434 it = gst_buffer_list_iterate (list);
4436 if (gst_buffer_list_iterator_next_group (it)) {
4438 group = gst_buffer_list_iterator_merge_group (it);
4439 if (group == NULL) {
4440 group = gst_buffer_new ();
4441 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4443 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4445 ret = gst_pad_push_data (pad, TRUE, group);
4446 } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4448 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4449 ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4452 gst_buffer_list_iterator_free (it);
4453 gst_buffer_list_unref (list);
4458 /* ERROR recovery here */
4461 gst_pad_data_unref (is_buffer, data);
4462 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4463 GST_OBJECT_UNLOCK (pad);
4468 gst_pad_data_unref (is_buffer, data);
4469 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4474 gst_pad_data_unref (is_buffer, data);
4475 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4476 "pushing, but it was not linked");
4477 GST_OBJECT_UNLOCK (pad);
4478 return GST_FLOW_NOT_LINKED;
4482 gst_pad_data_unref (is_buffer, data);
4483 gst_object_unref (peer);
4484 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4485 "element pushed data then refused to accept the caps");
4486 return GST_FLOW_NOT_NEGOTIATED;
4492 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4493 * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4495 * Pushes a buffer to the peer of @pad.
4497 * This function will call an installed pad block before triggering any
4498 * installed pad probes.
4500 * If the caps on @buffer are different from the currently configured caps on
4501 * @pad, this function will call any installed setcaps function on @pad (see
4502 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4503 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4505 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4506 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4509 * In all cases, success or failure, the caller loses its reference to @buffer
4510 * after calling this function.
4512 * Returns: a #GstFlowReturn from the peer pad.
4517 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4519 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4520 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4521 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4523 return gst_pad_push_data (pad, TRUE, buffer);
4527 * gst_pad_push_list:
4528 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4529 * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4531 * Pushes a buffer list to the peer of @pad.
4533 * This function will call an installed pad block before triggering any
4534 * installed pad probes.
4536 * If the caps on the first buffer in the first group of @list are different
4537 * from the currently configured caps on @pad, this function will call any
4538 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4539 * case of failure to renegotiate the new format, this function returns
4540 * #GST_FLOW_NOT_NEGOTIATED.
4542 * If there are any probes installed on @pad every group of the buffer list
4543 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4544 * buffer list will be unreffed.
4546 * The function proceeds calling the chain function on the peer pad and returns
4547 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4548 * be returned. If the peer pad does not have any installed chainlist function
4549 * every group buffer of the list will be merged into a normal #GstBuffer and
4550 * chained via gst_pad_chain().
4552 * In all cases, success or failure, the caller loses its reference to @list
4553 * after calling this function.
4555 * Returns: a #GstFlowReturn from the peer pad.
4562 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4564 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4565 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4566 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4568 return gst_pad_push_data (pad, FALSE, list);
4572 * gst_pad_check_pull_range:
4573 * @pad: a sink #GstPad.
4575 * Checks if a gst_pad_pull_range() can be performed on the peer
4576 * source pad. This function is used by plugins that want to check
4577 * if they can use random access on the peer source pad.
4579 * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4580 * if it needs to perform some logic to determine if pull_range is
4583 * Returns: a gboolean with the result.
4588 gst_pad_check_pull_range (GstPad * pad)
4592 GstPadCheckGetRangeFunction checkgetrangefunc;
4594 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4596 GST_OBJECT_LOCK (pad);
4597 if (!GST_PAD_IS_SINK (pad))
4598 goto wrong_direction;
4600 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4603 gst_object_ref (peer);
4604 GST_OBJECT_UNLOCK (pad);
4606 /* see note in above function */
4607 if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4608 /* FIXME, kindoff ghetto */
4609 ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4610 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4611 "no checkgetrangefunc, assuming %d", ret);
4613 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4614 "calling checkgetrangefunc %s of peer pad %s:%s",
4615 GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4617 ret = checkgetrangefunc (peer);
4620 gst_object_unref (peer);
4624 /* ERROR recovery here */
4627 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4628 "checking pull range, but pad must be a sinkpad");
4629 GST_OBJECT_UNLOCK (pad);
4634 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4635 "checking pull range, but it was not linked");
4636 GST_OBJECT_UNLOCK (pad);
4641 static GstFlowReturn
4642 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4643 GstBuffer ** buffer)
4646 GstPadGetRangeFunction getrangefunc;
4647 gboolean emit_signal;
4649 gboolean caps_changed;
4651 GST_PAD_STREAM_LOCK (pad);
4653 GST_OBJECT_LOCK (pad);
4654 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4657 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4658 GST_OBJECT_UNLOCK (pad);
4660 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4663 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4664 "calling getrangefunc %s, offset %"
4665 G_GUINT64_FORMAT ", size %u",
4666 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4668 ret = getrangefunc (pad, offset, size, buffer);
4670 /* can only fire the signal if we have a valid buffer */
4671 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4672 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4676 GST_PAD_STREAM_UNLOCK (pad);
4678 if (G_UNLIKELY (ret != GST_FLOW_OK))
4679 goto get_range_failed;
4681 GST_OBJECT_LOCK (pad);
4682 /* Before pushing the buffer to the peer pad, ensure that caps
4683 * are set on this pad */
4684 caps = GST_BUFFER_CAPS (*buffer);
4685 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4686 GST_OBJECT_UNLOCK (pad);
4688 if (G_UNLIKELY (caps_changed)) {
4689 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4690 /* this should usually work because the element produced the buffer */
4691 if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4692 goto not_negotiated;
4699 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4700 "pulling range, but pad was flushing");
4701 GST_OBJECT_UNLOCK (pad);
4702 GST_PAD_STREAM_UNLOCK (pad);
4703 return GST_FLOW_WRONG_STATE;
4707 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4708 ("pullrange on pad %s:%s but it has no getrangefunction",
4709 GST_DEBUG_PAD_NAME (pad)));
4710 GST_PAD_STREAM_UNLOCK (pad);
4711 return GST_FLOW_NOT_SUPPORTED;
4715 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4716 "Dropping data after FALSE probe return");
4717 GST_PAD_STREAM_UNLOCK (pad);
4718 gst_buffer_unref (*buffer);
4720 return GST_FLOW_UNEXPECTED;
4725 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4726 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4727 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4732 gst_buffer_unref (*buffer);
4734 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4735 "getrange returned buffer of unaccaptable caps");
4736 return GST_FLOW_NOT_NEGOTIATED;
4741 * gst_pad_get_range:
4742 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4743 * @offset: The start offset of the buffer
4744 * @size: The length of the buffer
4745 * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4747 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4748 * immediatly and @buffer is %NULL.
4750 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4751 * description of a getrange function. If @pad has no getrange function
4752 * installed (see gst_pad_set_getrange_function()) this function returns
4753 * #GST_FLOW_NOT_SUPPORTED.
4755 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4757 * Returns: a #GstFlowReturn from the pad.
4762 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4763 GstBuffer ** buffer)
4765 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4766 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4767 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4769 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4773 * gst_pad_pull_range:
4774 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4775 * @offset: The start offset of the buffer
4776 * @size: The length of the buffer
4777 * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4779 * Pulls a @buffer from the peer pad.
4781 * This function will first trigger the pad block signal if it was
4784 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4785 * function returns the result of gst_pad_get_range() on the peer pad.
4786 * See gst_pad_get_range() for a list of return values and for the
4787 * semantics of the arguments of this function.
4789 * @buffer's caps must either be unset or the same as what is already
4790 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4793 * Returns: a #GstFlowReturn from the peer pad.
4794 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4795 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4796 * @buffer may not be used or freed when any other return value than
4797 * #GST_FLOW_OK is returned.
4802 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4803 GstBuffer ** buffer)
4807 gboolean emit_signal;
4809 gboolean caps_changed;
4811 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4812 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4813 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4815 GST_OBJECT_LOCK (pad);
4817 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4818 handle_pad_block (pad);
4820 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4823 /* signal emision for the pad, peer has chance to emit when
4824 * we call _get_range() */
4825 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4827 gst_object_ref (peer);
4828 GST_OBJECT_UNLOCK (pad);
4830 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4832 gst_object_unref (peer);
4834 if (G_UNLIKELY (ret != GST_FLOW_OK))
4835 goto pull_range_failed;
4837 /* can only fire the signal if we have a valid buffer */
4838 if (G_UNLIKELY (emit_signal)) {
4839 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4843 GST_OBJECT_LOCK (pad);
4844 /* Before pushing the buffer to the peer pad, ensure that caps
4845 * are set on this pad */
4846 caps = GST_BUFFER_CAPS (*buffer);
4847 caps_changed = caps && caps != GST_PAD_CAPS (pad);
4848 GST_OBJECT_UNLOCK (pad);
4850 /* we got a new datatype on the pad, see if it can handle it */
4851 if (G_UNLIKELY (caps_changed)) {
4852 GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4853 if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4854 goto not_negotiated;
4858 /* ERROR recovery here */
4861 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4862 "pulling range, but it was not linked");
4863 GST_OBJECT_UNLOCK (pad);
4864 return GST_FLOW_NOT_LINKED;
4869 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4870 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4871 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4876 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4877 "Dropping data after FALSE probe return");
4878 gst_buffer_unref (*buffer);
4880 return GST_FLOW_UNEXPECTED;
4884 gst_buffer_unref (*buffer);
4886 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4887 "pullrange returned buffer of different caps");
4888 return GST_FLOW_NOT_NEGOTIATED;
4893 * gst_pad_push_event:
4894 * @pad: a #GstPad to push the event to.
4895 * @event: the #GstEvent to send to the pad.
4897 * Sends the event to the peer of the given pad. This function is
4898 * mainly used by elements to send events to their peer
4901 * This function takes owership of the provided event so you should
4902 * gst_event_ref() it if you want to reuse the event after this call.
4904 * Returns: TRUE if the event was handled.
4909 gst_pad_push_event (GstPad * pad, GstEvent * event)
4914 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4915 g_return_val_if_fail (event != NULL, FALSE);
4916 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4918 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4920 GST_OBJECT_LOCK (pad);
4922 /* Two checks to be made:
4923 * . (un)set the FLUSHING flag for flushing events,
4924 * . handle pad blocking */
4925 switch (GST_EVENT_TYPE (event)) {
4926 case GST_EVENT_FLUSH_START:
4927 GST_PAD_SET_FLUSHING (pad);
4929 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4930 /* flush start will have set the FLUSHING flag and will then
4931 * unlock all threads doing a GCond wait on the blocking pad. This
4932 * will typically unblock the STREAMING thread blocked on a pad. */
4933 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4934 "doing block signal.");
4935 GST_PAD_BLOCK_BROADCAST (pad);
4939 case GST_EVENT_FLUSH_STOP:
4940 GST_PAD_UNSET_FLUSHING (pad);
4942 /* if we are blocked, flush away the FLUSH_STOP event */
4943 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4944 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4949 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4950 /* block the event as long as the pad is blocked */
4951 if (handle_pad_block (pad) != GST_FLOW_OK)
4957 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4958 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4959 GST_EVENT_SRC (event) = gst_object_ref (pad);
4962 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4963 GST_OBJECT_UNLOCK (pad);
4965 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4968 GST_OBJECT_LOCK (pad);
4970 peerpad = GST_PAD_PEER (pad);
4971 if (peerpad == NULL)
4974 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4975 GST_EVENT_TYPE_NAME (event), peerpad);
4976 gst_object_ref (peerpad);
4977 GST_OBJECT_UNLOCK (pad);
4979 result = gst_pad_send_event (peerpad, event);
4981 /* Note: we gave away ownership of the event at this point */
4982 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4984 gst_object_unref (peerpad);
4988 /* ERROR handling */
4991 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4992 gst_event_unref (event);
4997 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4998 gst_event_unref (event);
4999 GST_OBJECT_UNLOCK (pad);
5004 GST_DEBUG_OBJECT (pad,
5005 "Not forwarding event since we're flushing and blocking");
5006 gst_event_unref (event);
5007 GST_OBJECT_UNLOCK (pad);
5013 * gst_pad_send_event:
5014 * @pad: a #GstPad to send the event to.
5015 * @event: the #GstEvent to send to the pad.
5017 * Sends the event to the pad. This function can be used
5018 * by applications to send events in the pipeline.
5020 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5021 * sink pad, @event should be a downstream event. For example, you would not
5022 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5023 * Furthermore, some downstream events have to be serialized with data flow,
5024 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5025 * the event needs to be serialized with data flow, this function will take the
5026 * pad's stream lock while calling its event function.
5028 * To find out whether an event type is upstream, downstream, or downstream and
5029 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5030 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5031 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5032 * plugin doesn't need to bother itself with this information; the core handles
5033 * all necessary locks and checks.
5035 * This function takes owership of the provided event so you should
5036 * gst_event_ref() it if you want to reuse the event after this call.
5038 * Returns: TRUE if the event was handled.
5041 gst_pad_send_event (GstPad * pad, GstEvent * event)
5043 gboolean result = FALSE;
5044 GstPadEventFunction eventfunc;
5045 gboolean serialized, need_unlock = FALSE;
5047 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5048 g_return_val_if_fail (event != NULL, FALSE);
5050 GST_OBJECT_LOCK (pad);
5051 if (GST_PAD_IS_SINK (pad)) {
5052 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5053 goto wrong_direction;
5054 serialized = GST_EVENT_IS_SERIALIZED (event);
5055 } else if (GST_PAD_IS_SRC (pad)) {
5056 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5057 goto wrong_direction;
5058 /* events on srcpad never are serialized */
5061 goto unknown_direction;
5063 if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
5064 GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
5065 GST_EVENT_SRC (event) = gst_object_ref (pad);
5069 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
5070 GST_OBJECT_UNLOCK (pad);
5072 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
5075 GST_OBJECT_LOCK (pad);
5078 switch (GST_EVENT_TYPE (event)) {
5079 case GST_EVENT_FLUSH_START:
5080 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5081 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5083 /* can't even accept a flush begin event when flushing */
5084 if (GST_PAD_IS_FLUSHING (pad))
5086 GST_PAD_SET_FLUSHING (pad);
5087 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5089 case GST_EVENT_FLUSH_STOP:
5090 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
5091 GST_PAD_UNSET_FLUSHING (pad);
5092 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5094 GST_OBJECT_UNLOCK (pad);
5095 /* grab stream lock */
5096 GST_PAD_STREAM_LOCK (pad);
5098 GST_OBJECT_LOCK (pad);
5101 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
5102 GST_EVENT_TYPE_NAME (event));
5104 /* make this a little faster, no point in grabbing the lock
5105 * if the pad is allready flushing. */
5106 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5110 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5111 GST_OBJECT_UNLOCK (pad);
5112 GST_PAD_STREAM_LOCK (pad);
5114 GST_OBJECT_LOCK (pad);
5115 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5120 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5123 GST_OBJECT_UNLOCK (pad);
5125 result = eventfunc (pad, event);
5128 GST_PAD_STREAM_UNLOCK (pad);
5130 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
5134 /* ERROR handling */
5137 g_warning ("pad %s:%s sending %s event in wrong direction",
5138 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5139 GST_OBJECT_UNLOCK (pad);
5140 gst_event_unref (event);
5145 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5146 GST_OBJECT_UNLOCK (pad);
5147 gst_event_unref (event);
5152 g_warning ("pad %s:%s has no event handler, file a bug.",
5153 GST_DEBUG_PAD_NAME (pad));
5154 GST_OBJECT_UNLOCK (pad);
5156 GST_PAD_STREAM_UNLOCK (pad);
5157 gst_event_unref (event);
5162 GST_OBJECT_UNLOCK (pad);
5164 GST_PAD_STREAM_UNLOCK (pad);
5165 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5166 "Received event on flushing pad. Discarding");
5167 gst_event_unref (event);
5172 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5173 gst_event_unref (event);
5179 * gst_pad_set_element_private:
5180 * @pad: the #GstPad to set the private data of.
5181 * @priv: The private data to attach to the pad.
5183 * Set the given private data gpointer on the pad.
5184 * This function can only be used by the element that owns the pad.
5185 * No locking is performed in this function.
5188 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5190 pad->element_private = priv;
5194 * gst_pad_get_element_private:
5195 * @pad: the #GstPad to get the private data of.
5197 * Gets the private data of a pad.
5198 * No locking is performed in this function.
5200 * Returns: a #gpointer to the private data.
5203 gst_pad_get_element_private (GstPad * pad)
5205 return pad->element_private;
5209 do_stream_status (GstPad * pad, GstStreamStatusType type,
5210 GThread * thread, GstTask * task)
5214 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5216 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5217 if (GST_IS_ELEMENT (parent)) {
5218 GstMessage *message;
5219 GValue value = { 0 };
5221 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5222 gchar *tname, *ename, *pname;
5224 /* create a good task name */
5225 ename = gst_element_get_name (parent);
5226 pname = gst_pad_get_name (pad);
5227 tname = g_strdup_printf ("%s:%s", ename, pname);
5231 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5235 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5238 g_value_init (&value, GST_TYPE_TASK);
5239 g_value_set_object (&value, task);
5240 gst_message_set_stream_status_object (message, &value);
5241 g_value_unset (&value);
5243 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5244 gst_element_post_message (parent, message);
5246 gst_object_unref (parent);
5251 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5253 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5258 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5260 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5264 static GstTaskThreadCallbacks thr_callbacks = {
5270 * gst_pad_start_task:
5271 * @pad: the #GstPad to start the task of
5272 * @func: the task function to call
5273 * @data: data passed to the task function
5275 * Starts a task that repeatedly calls @func with @data. This function
5276 * is mostly used in pad activation functions to start the dataflow.
5277 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5278 * before @func is called.
5280 * Returns: a %TRUE if the task could be started.
5283 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5288 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5289 g_return_val_if_fail (func != NULL, FALSE);
5291 GST_DEBUG_OBJECT (pad, "start task");
5293 GST_OBJECT_LOCK (pad);
5294 task = GST_PAD_TASK (pad);
5296 task = gst_task_create (func, data);
5297 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5298 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5299 GST_DEBUG_OBJECT (pad, "created task");
5300 GST_PAD_TASK (pad) = task;
5301 gst_object_ref (task);
5302 /* release lock to post the message */
5303 GST_OBJECT_UNLOCK (pad);
5305 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5307 gst_object_unref (task);
5309 GST_OBJECT_LOCK (pad);
5310 /* nobody else is supposed to have changed the pad now */
5311 if (GST_PAD_TASK (pad) != task)
5312 goto concurrent_stop;
5314 res = gst_task_set_state (task, GST_TASK_STARTED);
5315 GST_OBJECT_UNLOCK (pad);
5322 GST_OBJECT_UNLOCK (pad);
5328 * gst_pad_pause_task:
5329 * @pad: the #GstPad to pause the task of
5331 * Pause the task of @pad. This function will also wait until the
5332 * function executed by the task is finished if this function is not
5333 * called from the task function.
5335 * Returns: a TRUE if the task could be paused or FALSE when the pad
5339 gst_pad_pause_task (GstPad * pad)
5344 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5346 GST_DEBUG_OBJECT (pad, "pause task");
5348 GST_OBJECT_LOCK (pad);
5349 task = GST_PAD_TASK (pad);
5352 res = gst_task_set_state (task, GST_TASK_PAUSED);
5353 GST_OBJECT_UNLOCK (pad);
5355 /* wait for task function to finish, this lock is recursive so it does nothing
5356 * when the pause is called from the task itself */
5357 GST_PAD_STREAM_LOCK (pad);
5358 GST_PAD_STREAM_UNLOCK (pad);
5364 GST_DEBUG_OBJECT (pad, "pad has no task");
5365 GST_OBJECT_UNLOCK (pad);
5371 * gst_pad_stop_task:
5372 * @pad: the #GstPad to stop the task of
5374 * Stop the task of @pad. This function will also make sure that the
5375 * function executed by the task will effectively stop if not called
5376 * from the GstTaskFunction.
5378 * This function will deadlock if called from the GstTaskFunction of
5379 * the task. Use gst_task_pause() instead.
5381 * Regardless of whether the pad has a task, the stream lock is acquired and
5382 * released so as to ensure that streaming through this pad has finished.
5384 * Returns: a TRUE if the task could be stopped or FALSE on error.
5387 gst_pad_stop_task (GstPad * pad)
5392 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5394 GST_DEBUG_OBJECT (pad, "stop task");
5396 GST_OBJECT_LOCK (pad);
5397 task = GST_PAD_TASK (pad);
5400 GST_PAD_TASK (pad) = NULL;
5401 res = gst_task_set_state (task, GST_TASK_STOPPED);
5402 GST_OBJECT_UNLOCK (pad);
5404 GST_PAD_STREAM_LOCK (pad);
5405 GST_PAD_STREAM_UNLOCK (pad);
5407 if (!gst_task_join (task))
5410 gst_object_unref (task);
5416 GST_DEBUG_OBJECT (pad, "no task");
5417 GST_OBJECT_UNLOCK (pad);
5419 GST_PAD_STREAM_LOCK (pad);
5420 GST_PAD_STREAM_UNLOCK (pad);
5422 /* this is not an error */
5427 /* this is bad, possibly the application tried to join the task from
5428 * the task's thread. We install the task again so that it will be stopped
5429 * again from the right thread next time hopefully. */
5430 GST_OBJECT_LOCK (pad);
5431 GST_DEBUG_OBJECT (pad, "join failed");
5432 /* we can only install this task if there was no other task */
5433 if (GST_PAD_TASK (pad) == NULL)
5434 GST_PAD_TASK (pad) = task;
5435 GST_OBJECT_UNLOCK (pad);