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 /* we have a pending and an active event on the pad. On source pads only the
102 * active event is used. On sinkpads, events are copied to the pending entry and
103 * moved to the active event when the eventfunc returned TRUE. */
110 struct _GstPadPrivate
112 PadEvent events[GST_EVENT_MAX_STICKY];
117 static void gst_pad_dispose (GObject * object);
118 static void gst_pad_finalize (GObject * object);
119 static void gst_pad_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_pad_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static GstFlowReturn handle_pad_block (GstPad * pad);
125 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter);
126 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
127 static gboolean gst_pad_activate_default (GstPad * pad);
128 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
129 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
130 GstBufferList * list);
132 static GstObjectClass *parent_class = NULL;
133 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
135 static GParamSpec *pspec_caps = NULL;
137 /* quarks for probe signals */
138 static GQuark buffer_quark;
139 static GQuark buffer_list_quark;
140 static GQuark event_quark;
149 static GstFlowQuarks flow_quarks[] = {
150 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
151 {GST_FLOW_RESEND, "resend", 0},
152 {GST_FLOW_OK, "ok", 0},
153 {GST_FLOW_NOT_LINKED, "not-linked", 0},
154 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
155 {GST_FLOW_UNEXPECTED, "unexpected", 0},
156 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
157 {GST_FLOW_ERROR, "error", 0},
158 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
159 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
164 * @ret: a #GstFlowReturn to get the name of.
166 * Gets a string representing the given flow return.
168 * Returns: a static string with the name of the flow return.
170 G_CONST_RETURN gchar *
171 gst_flow_get_name (GstFlowReturn ret)
175 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
177 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
178 if (ret == flow_quarks[i].ret)
179 return flow_quarks[i].name;
186 * @ret: a #GstFlowReturn to get the quark of.
188 * Get the unique quark for the given GstFlowReturn.
190 * Returns: the quark associated with the flow return or 0 if an
191 * invalid return was specified.
194 gst_flow_to_quark (GstFlowReturn ret)
198 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
200 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
201 if (ret == flow_quarks[i].ret)
202 return flow_quarks[i].quark;
211 buffer_quark = g_quark_from_static_string ("buffer"); \
212 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
213 event_quark = g_quark_from_static_string ("event"); \
215 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
216 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
219 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
220 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
223 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
226 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
227 GValue * return_accu, const GValue * handler_return, gpointer dummy)
229 gboolean ret = g_value_get_boolean (handler_return);
231 GST_DEBUG ("accumulated %d", ret);
232 g_value_set_boolean (return_accu, ret);
238 default_have_data (GstPad * pad, GstMiniObject * o)
244 gst_pad_class_init (GstPadClass * klass)
246 GObjectClass *gobject_class;
247 GstObjectClass *gstobject_class;
249 gobject_class = G_OBJECT_CLASS (klass);
250 gstobject_class = GST_OBJECT_CLASS (klass);
252 g_type_class_add_private (klass, sizeof (GstPadPrivate));
254 parent_class = g_type_class_peek_parent (klass);
256 gobject_class->dispose = gst_pad_dispose;
257 gobject_class->finalize = gst_pad_finalize;
258 gobject_class->set_property = gst_pad_set_property;
259 gobject_class->get_property = gst_pad_get_property;
263 * @pad: the pad that emitted the signal
264 * @peer: the peer pad that has been connected
266 * Signals that a pad has been linked to the peer pad.
268 gst_pad_signals[PAD_LINKED] =
269 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
270 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
271 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
274 * @pad: the pad that emitted the signal
275 * @peer: the peer pad that has been disconnected
277 * Signals that a pad has been unlinked from the peer pad.
279 gst_pad_signals[PAD_UNLINKED] =
280 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
281 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
282 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
284 * GstPad::request-link:
285 * @pad: the pad that emitted the signal
286 * @peer: the peer pad for which a connection is requested
288 * Signals that a pad connection has been requested.
290 gst_pad_signals[PAD_REQUEST_LINK] =
291 g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
292 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
293 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
297 * @pad: the pad that emitted the signal
298 * @mini_obj: new data
300 * Signals that new data is available on the pad. This signal is used
301 * internally for implementing pad probes.
302 * See gst_pad_add_*_probe functions.
304 * Returns: %TRUE to keep the data, %FALSE to drop it
306 gst_pad_signals[PAD_HAVE_DATA] =
307 g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
308 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
309 G_STRUCT_OFFSET (GstPadClass, have_data),
310 _gst_do_pass_data_accumulator,
311 NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
313 pspec_caps = g_param_spec_boxed ("caps", "Caps",
314 "The capabilities of the pad", GST_TYPE_CAPS,
315 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
316 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
318 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
319 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
320 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
321 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
322 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
323 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
324 g_param_spec_object ("template", "Template",
325 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 gstobject_class->path_string_separator = ".";
330 /* Register common function pointer descriptions */
331 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
332 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
333 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_get_query_types_default);
334 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
335 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
336 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_acceptcaps_default);
337 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
339 klass->have_data = default_have_data;
343 gst_pad_init (GstPad * pad)
345 pad->priv = GST_PAD_GET_PRIVATE (pad);
347 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
349 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
350 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
351 GST_PAD_QUERYTYPEFUNC (pad) = gst_pad_get_query_types_default;
352 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
353 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
354 GST_PAD_ACCEPTCAPSFUNC (pad) = gst_pad_acceptcaps_default;
355 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
357 GST_PAD_SET_FLUSHING (pad);
359 /* FIXME 0.11: Store this directly in the instance struct */
360 pad->stream_rec_lock = g_slice_new (GStaticRecMutex);
361 g_static_rec_mutex_init (pad->stream_rec_lock);
363 pad->block_cond = g_cond_new ();
366 /* called when setting the pad inactive. It removes all sticky events from
369 clear_events (PadEvent events[])
373 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
374 gst_event_replace (&events[i].event, NULL);
375 gst_event_replace (&events[i].pending, NULL);
379 /* The sticky event with @idx from the srcpad is copied to the
380 * pending event on the sinkpad (when different).
381 * This function applies the pad offsets in case of segment events.
382 * This will make sure that we send the event to the sinkpad event
383 * function when the next buffer of event arrives.
384 * Should be called with the OBJECT lock of both pads.
385 * This function returns TRUE when there is a pending event on the
388 replace_event (GstPad * srcpad, GstPad * sinkpad, guint idx)
390 PadEvent *srcev, *sinkev;
392 gboolean pending = FALSE;
394 srcev = &srcpad->priv->events[idx];
396 if ((event = srcev->event)) {
397 sinkev = &sinkpad->priv->events[idx];
399 switch (GST_EVENT_TYPE (event)) {
400 case GST_EVENT_SEGMENT:
405 offset = srcpad->offset + sinkpad->offset;
407 gst_event_copy_segment (event, &segment);
408 /* adjust the base time. FIXME, check negative times, try to tweak the
409 * start to do clipping on negative times */
410 segment.base += offset;
411 /* make a new event from the updated segment */
412 event = gst_event_new_segment (&segment);
419 if (sinkev->event != event) {
420 /* put in the pending entry when different */
421 gst_event_replace (&sinkev->pending, event);
430 prepare_event_update (GstPad * srcpad, GstPad * sinkpad)
435 /* make sure we push the events from the source to this new peer, for this we
436 * copy the events on the sinkpad and mark EVENTS_PENDING */
438 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
439 pending |= replace_event (srcpad, sinkpad, i);
441 /* we had some new pending events, set our flag */
443 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_NEED_EVENTS);
446 /* should be called with the OBJECT_LOCK */
448 get_pad_caps (GstPad * pad)
450 GstCaps *caps = NULL;
454 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_CAPS);
455 /* we can only use the caps when we have successfully send the caps
456 * event to the event function and is thus in the active entry */
457 if ((event = pad->priv->events[idx].event))
458 gst_event_parse_caps (event, &caps);
464 gst_pad_dispose (GObject * object)
466 GstPad *pad = GST_PAD_CAST (object);
469 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
471 /* unlink the peer pad */
472 if ((peer = gst_pad_get_peer (pad))) {
473 /* window for MT unsafeness, someone else could unlink here
474 * and then we call unlink with wrong pads. The unlink
475 * function would catch this and safely return failed. */
476 if (GST_PAD_IS_SRC (pad))
477 gst_pad_unlink (pad, peer);
479 gst_pad_unlink (peer, pad);
481 gst_object_unref (peer);
484 gst_pad_set_pad_template (pad, NULL);
486 if (pad->block_destroy_data && pad->block_data) {
487 pad->block_destroy_data (pad->block_data);
488 pad->block_data = NULL;
491 clear_events (pad->priv->events);
493 G_OBJECT_CLASS (parent_class)->dispose (object);
497 gst_pad_finalize (GObject * object)
499 GstPad *pad = GST_PAD_CAST (object);
502 /* in case the task is still around, clean it up */
503 if ((task = GST_PAD_TASK (pad))) {
504 gst_task_join (task);
505 GST_PAD_TASK (pad) = NULL;
506 gst_object_unref (task);
509 if (pad->stream_rec_lock) {
510 g_static_rec_mutex_free (pad->stream_rec_lock);
511 g_slice_free (GStaticRecMutex, pad->stream_rec_lock);
512 pad->stream_rec_lock = NULL;
514 if (pad->block_cond) {
515 g_cond_free (pad->block_cond);
516 pad->block_cond = NULL;
519 G_OBJECT_CLASS (parent_class)->finalize (object);
523 gst_pad_set_property (GObject * object, guint prop_id,
524 const GValue * value, GParamSpec * pspec)
526 g_return_if_fail (GST_IS_PAD (object));
529 case PAD_PROP_DIRECTION:
530 GST_PAD_DIRECTION (object) = g_value_get_enum (value);
532 case PAD_PROP_TEMPLATE:
533 gst_pad_set_pad_template (GST_PAD_CAST (object),
534 (GstPadTemplate *) g_value_get_object (value));
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 gst_pad_get_property (GObject * object, guint prop_id,
544 GValue * value, GParamSpec * pspec)
546 g_return_if_fail (GST_IS_PAD (object));
550 GST_OBJECT_LOCK (object);
551 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
552 GST_OBJECT_UNLOCK (object);
554 case PAD_PROP_DIRECTION:
555 g_value_set_enum (value, GST_PAD_DIRECTION (object));
557 case PAD_PROP_TEMPLATE:
558 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
568 * @name: the name of the new pad.
569 * @direction: the #GstPadDirection of the pad.
571 * Creates a new pad with the given name in the given direction.
572 * If name is NULL, a guaranteed unique name (across all pads)
574 * This function makes a copy of the name so you can safely free the name.
576 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
581 gst_pad_new (const gchar * name, GstPadDirection direction)
583 return g_object_new (GST_TYPE_PAD,
584 "name", name, "direction", direction, NULL);
588 * gst_pad_new_from_template:
589 * @templ: the pad template to use
590 * @name: the name of the element
592 * Creates a new pad with the given name from the given template.
593 * If name is NULL, a guaranteed unique name (across all pads)
595 * This function makes a copy of the name so you can safely free the name.
597 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
600 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
602 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
604 return g_object_new (GST_TYPE_PAD,
605 "name", name, "direction", templ->direction, "template", templ, NULL);
609 * gst_pad_new_from_static_template:
610 * @templ: the #GstStaticPadTemplate to use
611 * @name: the name of the element
613 * Creates a new pad with the given name from the given static template.
614 * If name is NULL, a guaranteed unique name (across all pads)
616 * This function makes a copy of the name so you can safely free the name.
618 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
621 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
625 GstPadTemplate *template;
627 template = gst_static_pad_template_get (templ);
628 pad = gst_pad_new_from_template (template, name);
629 gst_object_unref (template);
634 * gst_pad_get_direction:
635 * @pad: a #GstPad to get the direction of.
637 * Gets the direction of the pad. The direction of the pad is
638 * decided at construction time so this function does not take
641 * Returns: the #GstPadDirection of the pad.
646 gst_pad_get_direction (GstPad * pad)
648 GstPadDirection result;
650 /* PAD_UNKNOWN is a little silly but we need some sort of
651 * error return value */
652 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
654 result = GST_PAD_DIRECTION (pad);
660 gst_pad_activate_default (GstPad * pad)
662 return gst_pad_activate_push (pad, TRUE);
666 pre_activate (GstPad * pad, GstActivateMode new_mode)
669 case GST_ACTIVATE_PUSH:
670 case GST_ACTIVATE_PULL:
671 GST_OBJECT_LOCK (pad);
672 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
674 GST_PAD_UNSET_FLUSHING (pad);
675 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
676 GST_OBJECT_UNLOCK (pad);
678 case GST_ACTIVATE_NONE:
679 GST_OBJECT_LOCK (pad);
680 GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
681 GST_PAD_SET_FLUSHING (pad);
682 GST_PAD_ACTIVATE_MODE (pad) = new_mode;
683 /* unlock blocked pads so element can resume and stop */
684 GST_PAD_BLOCK_BROADCAST (pad);
685 GST_OBJECT_UNLOCK (pad);
691 post_activate (GstPad * pad, GstActivateMode new_mode)
694 case GST_ACTIVATE_PUSH:
695 case GST_ACTIVATE_PULL:
698 case GST_ACTIVATE_NONE:
699 /* ensures that streaming stops */
700 GST_PAD_STREAM_LOCK (pad);
701 GST_DEBUG_OBJECT (pad, "stopped streaming");
702 GST_OBJECT_LOCK (pad);
703 clear_events (pad->priv->events);
704 GST_OBJECT_UNLOCK (pad);
705 GST_PAD_STREAM_UNLOCK (pad);
711 * gst_pad_set_active:
712 * @pad: the #GstPad to activate or deactivate.
713 * @active: whether or not the pad should be active.
715 * Activates or deactivates the given pad.
716 * Normally called from within core state change functions.
718 * If @active, makes sure the pad is active. If it is already active, either in
719 * push or pull mode, just return. Otherwise dispatches to the pad's activate
720 * function to perform the actual activation.
722 * If not @active, checks the pad's current mode and calls
723 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
726 * Returns: #TRUE if the operation was successful.
731 gst_pad_set_active (GstPad * pad, gboolean active)
734 gboolean ret = FALSE;
736 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
738 GST_OBJECT_LOCK (pad);
739 old = GST_PAD_ACTIVATE_MODE (pad);
740 GST_OBJECT_UNLOCK (pad);
744 case GST_ACTIVATE_PUSH:
745 GST_DEBUG_OBJECT (pad, "activating pad from push");
748 case GST_ACTIVATE_PULL:
749 GST_DEBUG_OBJECT (pad, "activating pad from pull");
752 case GST_ACTIVATE_NONE:
753 GST_DEBUG_OBJECT (pad, "activating pad from none");
754 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
757 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
762 case GST_ACTIVATE_PUSH:
763 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
764 ret = gst_pad_activate_push (pad, FALSE);
766 case GST_ACTIVATE_PULL:
767 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
768 ret = gst_pad_activate_pull (pad, FALSE);
770 case GST_ACTIVATE_NONE:
771 GST_DEBUG_OBJECT (pad, "deactivating pad from none");
775 GST_DEBUG_OBJECT (pad, "unknown activation mode!");
781 GST_OBJECT_LOCK (pad);
783 g_critical ("Failed to deactivate pad %s:%s, very bad",
784 GST_DEBUG_PAD_NAME (pad));
786 GST_WARNING_OBJECT (pad, "Failed to activate pad");
788 GST_OBJECT_UNLOCK (pad);
791 GST_OBJECT_LOCK (pad);
792 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
793 GST_OBJECT_UNLOCK (pad);
801 * gst_pad_activate_pull:
802 * @pad: the #GstPad to activate or deactivate.
803 * @active: whether or not the pad should be active.
805 * Activates or deactivates the given pad in pull mode via dispatching to the
806 * pad's activatepullfunc. For use from within pad activation functions only.
807 * When called on sink pads, will first proxy the call to the peer pad, which
808 * is expected to activate its internally linked pads from within its
809 * activate_pull function.
811 * If you don't know what this is, you probably don't want to call it.
813 * Returns: TRUE if the operation was successful.
818 gst_pad_activate_pull (GstPad * pad, gboolean active)
820 GstActivateMode old, new;
823 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
825 GST_OBJECT_LOCK (pad);
826 old = GST_PAD_ACTIVATE_MODE (pad);
827 GST_OBJECT_UNLOCK (pad);
831 case GST_ACTIVATE_PULL:
832 GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
834 case GST_ACTIVATE_PUSH:
835 GST_DEBUG_OBJECT (pad,
836 "activating pad from push, deactivate push first");
837 /* pad was activate in the wrong direction, deactivate it
838 * and reactivate it in pull mode */
839 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
840 goto deactivate_failed;
841 /* fallthrough, pad is deactivated now. */
842 case GST_ACTIVATE_NONE:
843 GST_DEBUG_OBJECT (pad, "activating pad from none");
848 case GST_ACTIVATE_NONE:
849 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
851 case GST_ACTIVATE_PUSH:
852 GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
853 /* pad was activated in the other direction, deactivate it
854 * in push mode, this should not happen... */
855 if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
856 goto deactivate_failed;
857 /* everything is fine now */
859 case GST_ACTIVATE_PULL:
860 GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
865 if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
866 if ((peer = gst_pad_get_peer (pad))) {
867 GST_DEBUG_OBJECT (pad, "calling peer");
868 if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
870 gst_object_unref (peer);
872 /* there is no peer, this is only fatal when we activate. When we
873 * deactivate, we must assume the application has unlinked the peer and
874 * will deactivate it eventually. */
878 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
881 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
882 goto failure; /* Can't activate pull on a src without a
886 new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
887 pre_activate (pad, new);
889 if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
890 if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
893 /* can happen for sinks of passthrough elements */
896 post_activate (pad, new);
898 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
899 active ? "activated" : "deactivated");
905 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
906 active ? "activated" : "deactivated");
911 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
912 "failed to %s in switch to pull from mode %d",
913 (active ? "activate" : "deactivate"), old);
918 GST_OBJECT_LOCK (peer);
919 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
920 "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
921 GST_OBJECT_UNLOCK (peer);
922 gst_object_unref (peer);
927 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
933 GST_OBJECT_LOCK (pad);
934 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
935 active ? "activate" : "deactivate");
936 GST_PAD_SET_FLUSHING (pad);
937 GST_PAD_ACTIVATE_MODE (pad) = old;
938 GST_OBJECT_UNLOCK (pad);
944 * gst_pad_activate_push:
945 * @pad: the #GstPad to activate or deactivate.
946 * @active: whether the pad should be active or not.
948 * Activates or deactivates the given pad in push mode via dispatching to the
949 * pad's activatepushfunc. For use from within pad activation functions only.
951 * If you don't know what this is, you probably don't want to call it.
953 * Returns: %TRUE if the operation was successful.
958 gst_pad_activate_push (GstPad * pad, gboolean active)
960 GstActivateMode old, new;
962 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
963 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
964 active ? "activated" : "deactivated");
966 GST_OBJECT_LOCK (pad);
967 old = GST_PAD_ACTIVATE_MODE (pad);
968 GST_OBJECT_UNLOCK (pad);
972 case GST_ACTIVATE_PUSH:
973 GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
975 case GST_ACTIVATE_PULL:
976 GST_DEBUG_OBJECT (pad,
977 "activating pad from push, deactivating pull first");
978 /* pad was activate in the wrong direction, deactivate it
979 * an reactivate it in push mode */
980 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
981 goto deactivate_failed;
982 /* fallthrough, pad is deactivated now. */
983 case GST_ACTIVATE_NONE:
984 GST_DEBUG_OBJECT (pad, "activating pad from none");
989 case GST_ACTIVATE_NONE:
990 GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
992 case GST_ACTIVATE_PULL:
993 GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
994 /* pad was activated in the other direction, deactivate it
995 * in pull mode, this should not happen... */
996 if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
997 goto deactivate_failed;
998 /* everything is fine now */
1000 case GST_ACTIVATE_PUSH:
1001 GST_DEBUG_OBJECT (pad, "deactivating pad from push");
1006 new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1007 pre_activate (pad, new);
1009 if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
1010 if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
1014 /* quite ok, element relies on state change func to prepare itself */
1017 post_activate (pad, new);
1019 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
1020 active ? "activated" : "deactivated");
1025 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
1026 active ? "activated" : "deactivated");
1031 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1032 "failed to %s in switch to push from mode %d",
1033 (active ? "activate" : "deactivate"), old);
1038 GST_OBJECT_LOCK (pad);
1039 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
1040 active ? "activate" : "deactivate");
1041 GST_PAD_SET_FLUSHING (pad);
1042 GST_PAD_ACTIVATE_MODE (pad) = old;
1043 GST_OBJECT_UNLOCK (pad);
1049 * gst_pad_is_active:
1050 * @pad: the #GstPad to query
1052 * Query if a pad is active
1054 * Returns: TRUE if the pad is active.
1059 gst_pad_is_active (GstPad * pad)
1061 gboolean result = FALSE;
1063 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1065 GST_OBJECT_LOCK (pad);
1066 result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
1067 GST_OBJECT_UNLOCK (pad);
1073 * gst_pad_set_blocked:
1074 * @pad: the #GstPad to block or unblock
1075 * @blocked: boolean indicating whether the pad should be blocked or unblocked
1076 * @callback: #GstPadBlockCallback that will be called when the
1077 * operation succeeds
1078 * @user_data: (closure): user data passed to the callback
1079 * @destroy_data: #GDestroyNotify for user_data
1081 * Blocks or unblocks the dataflow on a pad. The provided callback
1082 * is called when the operation succeeds; this happens right before the next
1083 * attempt at pushing a buffer on the pad.
1085 * This can take a while as the pad can only become blocked when real dataflow
1087 * When the pipeline is stalled, for example in PAUSED, this can
1088 * take an indeterminate amount of time.
1089 * You can pass NULL as the callback to make this call block. Be careful with
1090 * this blocking call as it might not return for reasons stated above.
1093 * Pad block handlers are only called for source pads in push mode
1094 * and sink pads in pull mode.
1097 * Returns: TRUE if the pad could be blocked. This function can fail if the
1098 * wrong parameters were passed or the pad was already in the requested state.
1103 gst_pad_set_blocked (GstPad * pad, gboolean blocked,
1104 GstPadBlockCallback callback, gpointer user_data,
1105 GDestroyNotify destroy_data)
1107 gboolean was_blocked = FALSE;
1109 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1111 GST_OBJECT_LOCK (pad);
1113 was_blocked = GST_PAD_IS_BLOCKED (pad);
1115 if (G_UNLIKELY (was_blocked == blocked))
1116 goto had_right_state;
1119 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
1121 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1123 if (pad->block_destroy_data && pad->block_data)
1124 pad->block_destroy_data (pad->block_data);
1126 pad->block_callback = callback;
1127 pad->block_data = user_data;
1128 pad->block_destroy_data = destroy_data;
1129 pad->block_callback_called = FALSE;
1131 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1132 GST_PAD_BLOCK_WAIT (pad);
1133 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1136 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1138 if (GST_PAD_IS_SRC (pad)) {
1140 /* a pad block dropped all events, make sure we copy any new events on the
1141 * srcpad to the sinkpad and schedule an update on the sinkpad */
1142 if ((peer = GST_PAD_PEER (pad))) {
1143 GST_OBJECT_LOCK (peer);
1144 prepare_event_update (pad, peer);
1145 GST_OBJECT_UNLOCK (peer);
1149 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1151 if (pad->block_destroy_data && pad->block_data)
1152 pad->block_destroy_data (pad->block_data);
1154 pad->block_callback = callback;
1155 pad->block_data = user_data;
1156 pad->block_destroy_data = destroy_data;
1157 pad->block_callback_called = FALSE;
1159 GST_PAD_BLOCK_BROADCAST (pad);
1161 /* no callback, wait for the unblock to happen */
1162 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1163 GST_PAD_BLOCK_WAIT (pad);
1164 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1167 GST_OBJECT_UNLOCK (pad);
1173 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1174 "pad was in right state (%d)", was_blocked);
1175 GST_OBJECT_UNLOCK (pad);
1182 * gst_pad_is_blocked:
1183 * @pad: the #GstPad to query
1185 * Checks if the pad is blocked or not. This function returns the
1186 * last requested state of the pad. It is not certain that the pad
1187 * is actually blocking at this point (see gst_pad_is_blocking()).
1189 * Returns: TRUE if the pad is blocked.
1194 gst_pad_is_blocked (GstPad * pad)
1196 gboolean result = FALSE;
1198 g_return_val_if_fail (GST_IS_PAD (pad), result);
1200 GST_OBJECT_LOCK (pad);
1201 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1202 GST_OBJECT_UNLOCK (pad);
1208 * gst_pad_is_blocking:
1209 * @pad: the #GstPad to query
1211 * Checks if the pad is blocking or not. This is a guaranteed state
1212 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1214 * Returns: TRUE if the pad is blocking.
1221 gst_pad_is_blocking (GstPad * pad)
1223 gboolean result = FALSE;
1225 g_return_val_if_fail (GST_IS_PAD (pad), result);
1227 GST_OBJECT_LOCK (pad);
1228 /* the blocking flag is only valid if the pad is not flushing */
1229 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1230 !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1231 GST_OBJECT_UNLOCK (pad);
1237 * gst_pad_set_activate_function:
1239 * @activate: the #GstPadActivateFunction to set.
1241 * Sets the given activate function for @pad. The activate function will
1242 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1243 * the actual activation. Only makes sense to set on sink pads.
1245 * Call this function if your sink pad can start a pull-based task.
1248 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1250 g_return_if_fail (GST_IS_PAD (pad));
1252 GST_PAD_ACTIVATEFUNC (pad) = activate;
1253 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1254 GST_DEBUG_FUNCPTR_NAME (activate));
1258 * gst_pad_set_activatepull_function:
1260 * @activatepull: the #GstPadActivateModeFunction to set.
1262 * Sets the given activate_pull function for the pad. An activate_pull function
1263 * prepares the element and any upstream connections for pulling. See XXX
1264 * part-activation.txt for details.
1267 gst_pad_set_activatepull_function (GstPad * pad,
1268 GstPadActivateModeFunction activatepull)
1270 g_return_if_fail (GST_IS_PAD (pad));
1272 GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1273 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1274 GST_DEBUG_FUNCPTR_NAME (activatepull));
1278 * gst_pad_set_activatepush_function:
1280 * @activatepush: the #GstPadActivateModeFunction to set.
1282 * Sets the given activate_push function for the pad. An activate_push function
1283 * prepares the element for pushing. See XXX part-activation.txt for details.
1286 gst_pad_set_activatepush_function (GstPad * pad,
1287 GstPadActivateModeFunction activatepush)
1289 g_return_if_fail (GST_IS_PAD (pad));
1291 GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1292 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1293 GST_DEBUG_FUNCPTR_NAME (activatepush));
1297 * gst_pad_set_chain_function:
1298 * @pad: a sink #GstPad.
1299 * @chain: the #GstPadChainFunction to set.
1301 * Sets the given chain function for the pad. The chain function is called to
1302 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1305 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1307 g_return_if_fail (GST_IS_PAD (pad));
1308 g_return_if_fail (GST_PAD_IS_SINK (pad));
1310 GST_PAD_CHAINFUNC (pad) = chain;
1311 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1312 GST_DEBUG_FUNCPTR_NAME (chain));
1316 * gst_pad_set_chain_list_function:
1317 * @pad: a sink #GstPad.
1318 * @chainlist: the #GstPadChainListFunction to set.
1320 * Sets the given chain list function for the pad. The chainlist function is
1321 * called to process a #GstBufferList input buffer list. See
1322 * #GstPadChainListFunction for more details.
1327 gst_pad_set_chain_list_function (GstPad * pad,
1328 GstPadChainListFunction chainlist)
1330 g_return_if_fail (GST_IS_PAD (pad));
1331 g_return_if_fail (GST_PAD_IS_SINK (pad));
1333 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1334 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1335 GST_DEBUG_FUNCPTR_NAME (chainlist));
1339 * gst_pad_set_getrange_function:
1340 * @pad: a source #GstPad.
1341 * @get: the #GstPadGetRangeFunction to set.
1343 * Sets the given getrange function for the pad. The getrange function is
1344 * called to produce a new #GstBuffer to start the processing pipeline. see
1345 * #GstPadGetRangeFunction for a description of the getrange function.
1348 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1350 g_return_if_fail (GST_IS_PAD (pad));
1351 g_return_if_fail (GST_PAD_IS_SRC (pad));
1353 GST_PAD_GETRANGEFUNC (pad) = get;
1355 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1356 GST_DEBUG_FUNCPTR_NAME (get));
1360 * gst_pad_set_event_function:
1361 * @pad: a #GstPad of either direction.
1362 * @event: the #GstPadEventFunction to set.
1364 * Sets the given event handler for the pad.
1367 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1369 g_return_if_fail (GST_IS_PAD (pad));
1371 GST_PAD_EVENTFUNC (pad) = event;
1373 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1374 GST_DEBUG_FUNCPTR_NAME (event));
1378 * gst_pad_set_query_function:
1379 * @pad: a #GstPad of either direction.
1380 * @query: the #GstPadQueryFunction to set.
1382 * Set the given query function for the pad.
1385 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1387 g_return_if_fail (GST_IS_PAD (pad));
1389 GST_PAD_QUERYFUNC (pad) = query;
1391 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1392 GST_DEBUG_FUNCPTR_NAME (query));
1396 * gst_pad_set_query_type_function:
1397 * @pad: a #GstPad of either direction.
1398 * @type_func: the #GstPadQueryTypeFunction to set.
1400 * Set the given query type function for the pad.
1403 gst_pad_set_query_type_function (GstPad * pad,
1404 GstPadQueryTypeFunction type_func)
1406 g_return_if_fail (GST_IS_PAD (pad));
1408 GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1410 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1411 GST_DEBUG_FUNCPTR_NAME (type_func));
1415 * gst_pad_get_query_types:
1418 * Get an array of supported queries that can be performed
1421 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1424 const GstQueryType *
1425 gst_pad_get_query_types (GstPad * pad)
1427 GstPadQueryTypeFunction func;
1429 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1431 if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1443 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1445 *data = gst_pad_get_query_types (pad);
1451 * gst_pad_get_query_types_default:
1454 * Invoke the default dispatcher for the query types on
1457 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
1458 * of #GstQueryType, or NULL if none of the internally-linked pads has a
1459 * query types function.
1461 const GstQueryType *
1462 gst_pad_get_query_types_default (GstPad * pad)
1464 GstQueryType *result = NULL;
1466 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1468 gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1469 gst_pad_get_query_types_dispatcher, &result);
1475 * gst_pad_set_iterate_internal_links_function:
1476 * @pad: a #GstPad of either direction.
1477 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1479 * Sets the given internal link iterator function for the pad.
1484 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1485 GstPadIterIntLinkFunction iterintlink)
1487 g_return_if_fail (GST_IS_PAD (pad));
1489 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1490 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1491 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1495 * gst_pad_set_link_function:
1497 * @link: the #GstPadLinkFunction to set.
1499 * Sets the given link function for the pad. It will be called when
1500 * the pad is linked with another pad.
1502 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1505 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1506 * cannot be made for some reason.
1508 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1509 * of the peer sink pad, if present.
1512 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1514 g_return_if_fail (GST_IS_PAD (pad));
1516 GST_PAD_LINKFUNC (pad) = link;
1517 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1518 GST_DEBUG_FUNCPTR_NAME (link));
1522 * gst_pad_set_unlink_function:
1524 * @unlink: the #GstPadUnlinkFunction to set.
1526 * Sets the given unlink function for the pad. It will be called
1527 * when the pad is unlinked.
1530 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1532 g_return_if_fail (GST_IS_PAD (pad));
1534 GST_PAD_UNLINKFUNC (pad) = unlink;
1535 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1536 GST_DEBUG_FUNCPTR_NAME (unlink));
1540 * gst_pad_set_getcaps_function:
1542 * @getcaps: the #GstPadGetCapsFunction to set.
1544 * Sets the given getcaps function for the pad. @getcaps should return the
1545 * allowable caps for a pad in the context of the element's state, its link to
1546 * other elements, and the devices or files it has opened. These caps must be a
1547 * subset of the pad template caps. In the NULL state with no links, @getcaps
1548 * should ideally return the same caps as the pad template. In rare
1549 * circumstances, an object property can affect the caps returned by @getcaps,
1550 * but this is discouraged.
1552 * You do not need to call this function if @pad's allowed caps are always the
1553 * same as the pad template caps. This can only be true if the padtemplate
1554 * has fixed simple caps.
1556 * For most filters, the caps returned by @getcaps is directly affected by the
1557 * allowed caps on other pads. For demuxers and decoders, the caps returned by
1558 * the srcpad's getcaps function is directly related to the stream data. Again,
1559 * @getcaps should return the most specific caps it reasonably can, since this
1560 * helps with autoplugging.
1562 * Note that the return value from @getcaps is owned by the caller, so the
1563 * caller should unref the caps after usage.
1566 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1568 g_return_if_fail (GST_IS_PAD (pad));
1570 GST_PAD_GETCAPSFUNC (pad) = getcaps;
1571 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1572 GST_DEBUG_FUNCPTR_NAME (getcaps));
1576 * gst_pad_set_acceptcaps_function:
1578 * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1580 * Sets the given acceptcaps function for the pad. The acceptcaps function
1581 * will be called to check if the pad can accept the given caps. Setting the
1582 * acceptcaps function to NULL restores the default behaviour of allowing
1583 * any caps that matches the caps from gst_pad_get_caps().
1586 gst_pad_set_acceptcaps_function (GstPad * pad,
1587 GstPadAcceptCapsFunction acceptcaps)
1589 g_return_if_fail (GST_IS_PAD (pad));
1591 GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1592 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1593 GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1597 * gst_pad_set_fixatecaps_function:
1599 * @fixatecaps: the #GstPadFixateCapsFunction to set.
1601 * Sets the given fixatecaps function for the pad. The fixatecaps function
1602 * will be called whenever the default values for a GstCaps needs to be
1606 gst_pad_set_fixatecaps_function (GstPad * pad,
1607 GstPadFixateCapsFunction fixatecaps)
1609 g_return_if_fail (GST_IS_PAD (pad));
1611 GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1612 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1613 GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1617 * gst_pad_set_setcaps_function:
1619 * @setcaps: the #GstPadSetCapsFunction to set.
1621 * Sets the given setcaps function for the pad. The setcaps function
1622 * will be called whenever a buffer with a new media type is pushed or
1623 * pulled from the pad. The pad/element needs to update its internal
1624 * structures to process the new media type. If this new type is not
1625 * acceptable, the setcaps function should return FALSE.
1628 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1630 g_return_if_fail (GST_IS_PAD (pad));
1632 GST_PAD_SETCAPSFUNC (pad) = setcaps;
1633 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1634 GST_DEBUG_FUNCPTR_NAME (setcaps));
1639 * @srcpad: the source #GstPad to unlink.
1640 * @sinkpad: the sink #GstPad to unlink.
1642 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1643 * signal on both pads.
1645 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1646 * the pads were not linked together.
1651 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1653 gboolean result = FALSE;
1654 GstElement *parent = NULL;
1657 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1658 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1659 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1660 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1662 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1663 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1664 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1666 /* We need to notify the parent before taking any pad locks as the bin in
1667 * question might be waiting for a lock on the pad while holding its lock
1668 * that our message will try to take. */
1669 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1670 if (GST_IS_ELEMENT (parent)) {
1671 gst_element_post_message (parent,
1672 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1673 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1675 gst_object_unref (parent);
1680 GST_OBJECT_LOCK (srcpad);
1682 GST_OBJECT_LOCK (sinkpad);
1684 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1685 goto not_linked_together;
1687 if (GST_PAD_UNLINKFUNC (srcpad)) {
1688 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1690 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1691 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1694 /* first clear peers */
1695 GST_PAD_PEER (srcpad) = NULL;
1696 GST_PAD_PEER (sinkpad) = NULL;
1698 /* clear pending caps if any */
1699 for (i = 0; i < GST_EVENT_MAX_STICKY; i++)
1700 gst_event_replace (&sinkpad->priv->events[i].pending, NULL);
1702 GST_OBJECT_UNLOCK (sinkpad);
1703 GST_OBJECT_UNLOCK (srcpad);
1705 /* fire off a signal to each of the pads telling them
1706 * that they've been unlinked */
1707 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1708 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1710 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1711 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1716 if (parent != NULL) {
1717 gst_element_post_message (parent,
1718 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1719 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1720 gst_object_unref (parent);
1725 not_linked_together:
1727 /* we do not emit a warning in this case because unlinking cannot
1728 * be made MT safe.*/
1729 GST_OBJECT_UNLOCK (sinkpad);
1730 GST_OBJECT_UNLOCK (srcpad);
1736 * gst_pad_is_linked:
1737 * @pad: pad to check
1739 * Checks if a @pad is linked to another pad or not.
1741 * Returns: TRUE if the pad is linked, FALSE otherwise.
1746 gst_pad_is_linked (GstPad * pad)
1750 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1752 GST_OBJECT_LOCK (pad);
1753 result = (GST_PAD_PEER (pad) != NULL);
1754 GST_OBJECT_UNLOCK (pad);
1759 /* get the caps from both pads and see if the intersection
1762 * This function should be called with the pad LOCK on both
1766 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1767 GstPadLinkCheck flags)
1769 GstCaps *srccaps = NULL;
1770 GstCaps *sinkcaps = NULL;
1771 gboolean compatible = FALSE;
1773 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1776 /* Doing the expensive caps checking takes priority over only checking the template caps */
1777 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1778 srccaps = gst_pad_get_caps_unlocked (src, NULL);
1779 sinkcaps = gst_pad_get_caps_unlocked (sink, NULL);
1781 /* If one of the two pads doesn't have a template, consider the intersection
1783 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1784 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1788 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1790 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1793 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1795 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1798 /* if we have caps on both pads we can check the intersection. If one
1799 * of the caps is NULL, we return TRUE. */
1800 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1802 gst_caps_unref (srccaps);
1804 gst_caps_unref (sinkcaps);
1808 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1809 gst_caps_unref (srccaps);
1810 gst_caps_unref (sinkcaps);
1813 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1814 (compatible ? "" : "not"));
1819 /* check if the grandparents of both pads are the same.
1820 * This check is required so that we don't try to link
1821 * pads from elements in different bins without ghostpads.
1823 * The LOCK should be held on both pads
1826 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1828 GstObject *psrc, *psink;
1830 psrc = GST_OBJECT_PARENT (src);
1831 psink = GST_OBJECT_PARENT (sink);
1833 /* if one of the pads has no parent, we allow the link */
1834 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1837 /* only care about parents that are elements */
1838 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1839 goto no_element_parent;
1841 /* if the parents are the same, we have a loop */
1842 if (G_UNLIKELY (psrc == psink))
1845 /* if they both have a parent, we check the grandparents. We can not lock
1846 * the parent because we hold on the child (pad) and the locking order is
1847 * parent >> child. */
1848 psrc = GST_OBJECT_PARENT (psrc);
1849 psink = GST_OBJECT_PARENT (psink);
1851 /* if they have grandparents but they are not the same */
1852 if (G_UNLIKELY (psrc != psink))
1853 goto wrong_grandparents;
1860 GST_CAT_DEBUG (GST_CAT_CAPS,
1861 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1862 GST_PTR_FORMAT, psrc, psink);
1867 GST_CAT_DEBUG (GST_CAT_CAPS,
1868 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1869 GST_PTR_FORMAT, psrc, psink);
1874 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1880 GST_CAT_DEBUG (GST_CAT_CAPS,
1881 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1882 GST_PTR_FORMAT, psrc, psink);
1887 /* FIXME leftover from an attempt at refactoring... */
1888 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1889 * the two pads will be locked in the srcpad, sinkpad order. */
1890 static GstPadLinkReturn
1891 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1893 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1894 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1896 GST_OBJECT_LOCK (srcpad);
1898 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1899 goto src_was_linked;
1901 GST_OBJECT_LOCK (sinkpad);
1903 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1904 goto sink_was_linked;
1906 /* check hierarchy, pads can only be linked if the grandparents
1908 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1909 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1910 goto wrong_hierarchy;
1912 /* check pad caps for non-empty intersection */
1913 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1916 /* FIXME check pad scheduling for non-empty intersection */
1918 return GST_PAD_LINK_OK;
1922 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1923 GST_DEBUG_PAD_NAME (srcpad),
1924 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1925 /* we do not emit a warning in this case because unlinking cannot
1926 * be made MT safe.*/
1927 GST_OBJECT_UNLOCK (srcpad);
1928 return GST_PAD_LINK_WAS_LINKED;
1932 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1933 GST_DEBUG_PAD_NAME (sinkpad),
1934 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1935 /* we do not emit a warning in this case because unlinking cannot
1936 * be made MT safe.*/
1937 GST_OBJECT_UNLOCK (sinkpad);
1938 GST_OBJECT_UNLOCK (srcpad);
1939 return GST_PAD_LINK_WAS_LINKED;
1943 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1944 GST_OBJECT_UNLOCK (sinkpad);
1945 GST_OBJECT_UNLOCK (srcpad);
1946 return GST_PAD_LINK_WRONG_HIERARCHY;
1950 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1951 GST_OBJECT_UNLOCK (sinkpad);
1952 GST_OBJECT_UNLOCK (srcpad);
1953 return GST_PAD_LINK_NOFORMAT;
1959 * @srcpad: the source #GstPad.
1960 * @sinkpad: the sink #GstPad.
1962 * Checks if the source pad and the sink pad are compatible so they can be
1965 * Returns: TRUE if the pads can be linked.
1968 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1970 GstPadLinkReturn result;
1972 /* generic checks */
1973 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1974 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1976 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1977 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1979 /* gst_pad_link_prepare does everything for us, we only release the locks
1980 * on the pads that it gets us. If this function returns !OK the locks are not
1982 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1983 if (result != GST_PAD_LINK_OK)
1986 GST_OBJECT_UNLOCK (srcpad);
1987 GST_OBJECT_UNLOCK (sinkpad);
1990 return result == GST_PAD_LINK_OK;
1994 * gst_pad_link_full:
1995 * @srcpad: the source #GstPad to link.
1996 * @sinkpad: the sink #GstPad to link.
1997 * @flags: the checks to validate when linking
1999 * Links the source pad and the sink pad.
2001 * This variant of #gst_pad_link provides a more granular control on the
2002 * checks being done when linking. While providing some considerable speedups
2003 * the caller of this method must be aware that wrong usage of those flags
2004 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2005 * for more information.
2009 * Returns: A result code indicating if the connection worked or
2015 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2017 GstPadLinkReturn result;
2020 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2021 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2022 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2023 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2024 GST_PAD_LINK_WRONG_DIRECTION);
2026 /* Notify the parent early. See gst_pad_unlink for details. */
2027 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2028 if (GST_IS_ELEMENT (parent)) {
2029 gst_element_post_message (parent,
2030 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2031 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2033 gst_object_unref (parent);
2038 /* prepare will also lock the two pads */
2039 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2041 if (result != GST_PAD_LINK_OK)
2044 /* must set peers before calling the link function */
2045 GST_PAD_PEER (srcpad) = sinkpad;
2046 GST_PAD_PEER (sinkpad) = srcpad;
2048 /* make sure we update events */
2049 prepare_event_update (srcpad, sinkpad);
2051 GST_OBJECT_UNLOCK (sinkpad);
2052 GST_OBJECT_UNLOCK (srcpad);
2054 /* FIXME released the locks here, concurrent thread might link
2055 * something else. */
2056 if (GST_PAD_LINKFUNC (srcpad)) {
2057 /* this one will call the peer link function */
2058 result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2059 } else if (GST_PAD_LINKFUNC (sinkpad)) {
2060 /* if no source link function, we need to call the sink link
2061 * function ourselves. */
2062 result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2064 result = GST_PAD_LINK_OK;
2067 GST_OBJECT_LOCK (srcpad);
2068 GST_OBJECT_LOCK (sinkpad);
2070 if (result == GST_PAD_LINK_OK) {
2071 GST_OBJECT_UNLOCK (sinkpad);
2072 GST_OBJECT_UNLOCK (srcpad);
2074 /* fire off a signal to each of the pads telling them
2075 * that they've been linked */
2076 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2077 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2079 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2080 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2082 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2084 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2085 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2087 GST_PAD_PEER (srcpad) = NULL;
2088 GST_PAD_PEER (sinkpad) = NULL;
2090 GST_OBJECT_UNLOCK (sinkpad);
2091 GST_OBJECT_UNLOCK (srcpad);
2096 gst_element_post_message (parent,
2097 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2098 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2099 gst_object_unref (parent);
2107 * @srcpad: the source #GstPad to link.
2108 * @sinkpad: the sink #GstPad to link.
2110 * Links the source pad and the sink pad.
2112 * Returns: A result code indicating if the connection worked or
2118 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2120 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2124 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2126 GstPadTemplate **template_p;
2128 /* this function would need checks if it weren't static */
2130 GST_OBJECT_LOCK (pad);
2131 template_p = &pad->padtemplate;
2132 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2133 GST_OBJECT_UNLOCK (pad);
2136 gst_pad_template_pad_created (templ, pad);
2140 * gst_pad_get_pad_template:
2143 * Gets the template for @pad.
2145 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2146 * instantiated, or %NULL if this pad has no template. Unref after
2150 gst_pad_get_pad_template (GstPad * pad)
2152 GstPadTemplate *templ;
2154 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2156 templ = GST_PAD_PAD_TEMPLATE (pad);
2158 return (templ ? gst_object_ref (templ) : NULL);
2161 /* should be called with the pad LOCK held */
2162 /* refs the caps, so caller is responsible for getting it unreffed */
2164 gst_pad_get_caps_unlocked (GstPad * pad, GstCaps * filter)
2166 GstCaps *result = NULL;
2167 GstPadTemplate *templ;
2168 gboolean fixed_caps;
2170 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2172 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2174 if (!fixed_caps && GST_PAD_GETCAPSFUNC (pad)) {
2175 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2176 "dispatching to pad getcaps function with "
2177 "filter %" GST_PTR_FORMAT, filter);
2179 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2180 GST_OBJECT_UNLOCK (pad);
2181 result = GST_PAD_GETCAPSFUNC (pad) (pad, filter);
2182 GST_OBJECT_LOCK (pad);
2183 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2185 if (result == NULL) {
2186 g_critical ("pad %s:%s returned NULL caps from getcaps function",
2187 GST_DEBUG_PAD_NAME (pad));
2189 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2190 "pad getcaps returned %" GST_PTR_FORMAT, result);
2191 #ifndef G_DISABLE_ASSERT
2192 /* check that the returned caps are a real subset of the template caps */
2193 if (GST_PAD_PAD_TEMPLATE (pad)) {
2194 const GstCaps *templ_caps =
2195 GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2196 if (!gst_caps_is_subset (result, templ_caps)) {
2199 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2200 "pad returned caps %" GST_PTR_FORMAT
2201 " which are not a real subset of its template caps %"
2202 GST_PTR_FORMAT, result, templ_caps);
2204 ("pad %s:%s returned caps which are not a real "
2205 "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2206 temp = gst_caps_intersect (templ_caps, result);
2207 gst_caps_unref (result);
2212 if (!gst_caps_is_subset (result, filter)) {
2215 GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2216 "pad returned caps %" GST_PTR_FORMAT
2217 " which are not a real subset of the filter caps %"
2218 GST_PTR_FORMAT, result, filter);
2219 g_warning ("pad %s:%s returned caps which are not a real "
2220 "subset of the filter caps", GST_DEBUG_PAD_NAME (pad));
2221 /* FIXME: Order? But shouldn't happen anyway... */
2223 gst_caps_intersect_full (filter, result,
2224 GST_CAPS_INTERSECT_FIRST);
2225 gst_caps_unref (result);
2233 if (fixed_caps && (result = get_pad_caps (pad))) {
2235 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2236 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2237 GST_PTR_FORMAT, result, result, filter, filter);
2239 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2243 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2244 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2245 result = gst_caps_ref (result);
2249 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2250 result = GST_PAD_TEMPLATE_CAPS (templ);
2253 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2254 "using pad template %p with caps %p %" GST_PTR_FORMAT
2255 " and filter %p %" GST_PTR_FORMAT, templ, result, result, filter,
2258 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2259 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2262 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2263 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2265 result = gst_caps_ref (result);
2270 if (!fixed_caps && (result = get_pad_caps (pad))) {
2272 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2273 "using pad caps %p %" GST_PTR_FORMAT " with filter %p %"
2274 GST_PTR_FORMAT, result, result, filter, filter);
2276 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2277 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2280 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2281 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2282 result = gst_caps_ref (result);
2288 /* this almost never happens */
2289 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2290 result = gst_caps_new_empty ();
2297 * gst_pad_has_current_caps:
2298 * @pad: a #GstPad to check
2300 * Check if @pad has caps set on it with gst_pad_set_caps().
2302 * Returns: TRUE when @pad has caps associated with it.
2305 gst_pad_has_current_caps (GstPad * pad)
2309 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2311 GST_OBJECT_LOCK (pad);
2312 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "check current pad caps");
2313 result = (get_pad_caps (pad) != NULL);
2314 GST_OBJECT_UNLOCK (pad);
2320 * gst_pad_get_current_caps:
2321 * @pad: a #GstPad to get the current capabilities of.
2323 * Gets the capabilities currently configured on @pad with the last call to
2324 * gst_pad_set_caps().
2326 * Returns: the current caps of the pad with incremented ref-count.
2329 gst_pad_get_current_caps (GstPad * pad)
2333 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2335 GST_OBJECT_LOCK (pad);
2336 if ((result = get_pad_caps (pad)))
2337 gst_caps_ref (result);
2338 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2339 "get current pad caps %" GST_PTR_FORMAT, result);
2340 GST_OBJECT_UNLOCK (pad);
2347 * @pad: a #GstPad to get the capabilities of.
2348 * @filter: suggested #GstCaps.
2350 * Gets the capabilities this pad can produce or consume.
2351 * Note that this method doesn't necessarily return the caps set by
2352 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
2353 * gst_pad_get_caps returns all possible caps a pad can operate with, using
2354 * the pad's get_caps function;
2355 * this returns the pad template caps if not explicitly set.
2357 * When called on sinkpads @filter contains the caps that
2358 * upstream could produce in the order preferred by upstream. When
2359 * called on srcpads @filter contains the caps accepted by
2360 * downstream in the preffered order. @filter might be %NULL but
2361 * if it is not %NULL the returned caps will be a subset of @filter.
2363 * Note that this function does not return writable #GstCaps, use
2364 * gst_caps_make_writable() before modifying the caps.
2366 * Returns: the caps of the pad with incremented ref-count.
2369 gst_pad_get_caps (GstPad * pad, GstCaps * filter)
2371 GstCaps *result = NULL;
2373 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2374 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2376 GST_OBJECT_LOCK (pad);
2378 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2380 result = gst_pad_get_caps_unlocked (pad, filter);
2382 GST_OBJECT_UNLOCK (pad);
2389 * gst_pad_peer_get_caps:
2390 * @pad: a #GstPad to get the capabilities of.
2391 * @filter: a #GstCaps filter.
2393 * Gets the capabilities of the peer connected to this pad. Similar to
2394 * gst_pad_get_caps().
2396 * When called on srcpads @filter contains the caps that
2397 * upstream could produce in the order preferred by upstream. When
2398 * called on sinkpads @filter contains the caps accepted by
2399 * downstream in the preffered order. @filter might be %NULL but
2400 * if it is not %NULL the returned caps will be a subset of @filter.
2402 * Returns: the caps of the peer pad with incremented ref-count. This function
2403 * returns %NULL when there is no peer pad.
2406 gst_pad_peer_get_caps (GstPad * pad, GstCaps * filter)
2409 GstCaps *result = NULL;
2411 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2412 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2414 GST_OBJECT_LOCK (pad);
2416 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2418 peerpad = GST_PAD_PEER (pad);
2419 if (G_UNLIKELY (peerpad == NULL))
2422 gst_object_ref (peerpad);
2423 GST_OBJECT_UNLOCK (pad);
2425 result = gst_pad_get_caps (peerpad, filter);
2427 gst_object_unref (peerpad);
2433 GST_OBJECT_UNLOCK (pad);
2439 fixate_value (GValue * dest, const GValue * src)
2441 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2442 g_value_init (dest, G_TYPE_INT);
2443 g_value_set_int (dest, gst_value_get_int_range_min (src));
2444 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2445 g_value_init (dest, G_TYPE_DOUBLE);
2446 g_value_set_double (dest, gst_value_get_double_range_min (src));
2447 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2448 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2449 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2450 GValue temp = { 0 };
2452 /* list could be empty */
2453 if (gst_value_list_get_size (src) <= 0)
2456 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2458 if (!fixate_value (dest, &temp))
2459 gst_value_init_and_copy (dest, &temp);
2460 g_value_unset (&temp);
2461 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2462 gboolean res = FALSE;
2465 len = gst_value_array_get_size (src);
2466 g_value_init (dest, GST_TYPE_ARRAY);
2467 for (n = 0; n < len; n++) {
2469 const GValue *orig_kid = gst_value_array_get_value (src, n);
2471 if (!fixate_value (&kid, orig_kid))
2472 gst_value_init_and_copy (&kid, orig_kid);
2475 gst_value_array_append_value (dest, &kid);
2476 g_value_unset (&kid);
2480 g_value_unset (dest);
2491 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2493 GstStructure *s = data;
2496 if (fixate_value (&v, value)) {
2497 gst_structure_id_set_value (s, field_id, &v);
2505 * gst_pad_fixate_caps:
2506 * @pad: a #GstPad to fixate
2507 * @caps: the #GstCaps to fixate
2509 * Fixate a caps on the given pad. Modifies the caps in place, so you should
2510 * make sure that the caps are actually writable (see gst_caps_make_writable()).
2513 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2515 GstPadFixateCapsFunction fixatefunc;
2518 g_return_if_fail (GST_IS_PAD (pad));
2519 g_return_if_fail (caps != NULL);
2520 g_return_if_fail (!gst_caps_is_empty (caps));
2521 g_return_if_fail (!gst_caps_is_any (caps));
2523 if (gst_caps_is_fixed (caps) || gst_caps_is_any (caps))
2526 fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2528 fixatefunc (pad, caps);
2531 /* default fixation */
2532 gst_caps_truncate (caps);
2533 s = gst_caps_get_structure (caps, 0);
2534 gst_structure_foreach (s, gst_pad_default_fixate, s);
2537 /* Default accept caps implementation just checks against
2538 * against the allowed caps for the pad */
2540 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2542 /* get the caps and see if it intersects to something not empty */
2544 gboolean result = FALSE;
2546 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2548 allowed = gst_pad_get_caps (pad, NULL);
2550 goto nothing_allowed;
2552 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2554 result = gst_caps_can_intersect (allowed, caps);
2556 gst_caps_unref (allowed);
2563 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2569 * gst_pad_accept_caps:
2570 * @pad: a #GstPad to check
2571 * @caps: a #GstCaps to check on the pad
2573 * Check if the given pad accepts the caps.
2575 * Returns: TRUE if the pad can accept the caps.
2578 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2581 GstPadAcceptCapsFunction acceptfunc;
2583 GstCaps *existing = NULL;
2586 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2588 /* any pad can be unnegotiated */
2592 /* lock for checking the existing caps */
2593 GST_OBJECT_LOCK (pad);
2594 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2596 /* The current caps on a pad are trivially acceptable */
2597 if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2598 if (caps == existing || gst_caps_is_equal (caps, existing))
2602 acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2603 GST_OBJECT_UNLOCK (pad);
2605 if (G_LIKELY (acceptfunc)) {
2606 /* we can call the function */
2607 result = acceptfunc (pad, caps);
2608 GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2610 /* Only null if the element explicitly unset it */
2611 result = gst_pad_acceptcaps_default (pad, caps);
2612 GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2619 GST_DEBUG_OBJECT (pad, "pad had same caps");
2620 GST_OBJECT_UNLOCK (pad);
2627 * gst_pad_peer_accept_caps:
2628 * @pad: a #GstPad to check the peer of
2629 * @caps: a #GstCaps to check on the pad
2631 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2634 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2637 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2642 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2644 GST_OBJECT_LOCK (pad);
2646 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2648 peerpad = GST_PAD_PEER (pad);
2649 if (G_UNLIKELY (peerpad == NULL))
2652 gst_object_ref (peerpad);
2653 /* release lock before calling external methods but keep ref to pad */
2654 GST_OBJECT_UNLOCK (pad);
2656 result = gst_pad_accept_caps (peerpad, caps);
2658 gst_object_unref (peerpad);
2664 GST_OBJECT_UNLOCK (pad);
2671 * @pad: a #GstPad to set the capabilities of.
2672 * @caps: (transfer none): a #GstCaps to set.
2674 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2675 * caps on the pad will be unreffed. This function refs the caps so you should
2676 * unref if as soon as you don't need it anymore.
2677 * It is possible to set NULL caps, which will make the pad unnegotiated
2680 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2681 * or bad parameters were provided to this function.
2686 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2689 gboolean res = TRUE;
2691 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2692 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2694 event = gst_event_new_caps (caps);
2696 if (GST_PAD_IS_SRC (pad))
2697 gst_pad_push_event (pad, event);
2699 res = gst_pad_send_event (pad, event);
2705 gst_pad_call_setcaps (GstPad * pad, GstCaps * caps)
2707 GstPadSetCapsFunction setcaps;
2709 GST_OBJECT_LOCK (pad);
2710 setcaps = GST_PAD_SETCAPSFUNC (pad);
2712 /* call setcaps function to configure the pad only if the
2713 * caps is not NULL */
2714 if (setcaps != NULL) {
2715 if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2716 GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2717 GST_OBJECT_UNLOCK (pad);
2718 if (!setcaps (pad, caps))
2719 goto setcaps_failed;
2720 GST_OBJECT_LOCK (pad);
2721 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2723 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2726 GST_OBJECT_UNLOCK (pad);
2728 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
2740 do_event_function (GstPad * pad, GstEvent * event,
2741 GstPadEventFunction eventfunc)
2743 gboolean result = TRUE;
2744 GstCaps *caps, *templ;
2746 switch (GST_EVENT_TYPE (event)) {
2747 case GST_EVENT_CAPS:
2749 /* backwards compatibility mode for caps */
2750 gst_event_parse_caps (event, &caps);
2752 /* See if pad accepts the caps */
2753 templ = gst_pad_get_pad_template_caps (pad);
2754 if (!gst_caps_can_intersect (caps, templ))
2757 if (!gst_pad_call_setcaps (pad, caps))
2760 gst_caps_unref (templ);
2767 GST_DEBUG_OBJECT (pad, "calling event function with event %p", event);
2768 result = eventfunc (pad, event);
2775 gst_caps_unref (templ);
2776 gst_event_unref (event);
2777 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2778 "caps %" GST_PTR_FORMAT " not accepted", caps);
2783 /* function to send all pending events on the sinkpad to the event
2784 * function and collect the results. This function should be called with
2785 * the object lock. The object lock might be released by this function.
2787 static GstFlowReturn
2788 gst_pad_update_events (GstPad * pad)
2790 GstFlowReturn ret = GST_FLOW_OK;
2792 GstPadEventFunction eventfunc;
2795 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
2799 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
2803 ev = &pad->priv->events[i];
2805 /* skip without pending event */
2806 if ((event = ev->pending) == NULL)
2809 gst_event_ref (event);
2810 GST_OBJECT_UNLOCK (pad);
2812 res = do_event_function (pad, event, eventfunc);
2814 GST_OBJECT_LOCK (pad);
2815 /* things could have changed while we release the lock, check if we still
2816 * are handling the same event, if we don't something changed and we have
2817 * to try again. FIXME. we need a cookie here. */
2818 if (event != ev->pending) {
2819 GST_DEBUG_OBJECT (pad, "events changed, restarting");
2823 /* remove the event from the pending entry in all cases */
2827 /* make the event active */
2829 gst_event_unref (ev->event);
2832 gst_event_unref (event);
2833 ret = GST_FLOW_ERROR;
2836 /* when we get here all events were successfully updated. */
2843 g_warning ("pad %s:%s has no event handler, file a bug.",
2844 GST_DEBUG_PAD_NAME (pad));
2845 return GST_FLOW_NOT_SUPPORTED;
2850 * gst_pad_get_pad_template_caps:
2851 * @pad: a #GstPad to get the template capabilities from.
2853 * Gets the capabilities for @pad's template.
2855 * Returns: (transfer full): the #GstCaps of this pad template.
2856 * Unref after usage.
2859 gst_pad_get_pad_template_caps (GstPad * pad)
2861 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2863 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2865 if (GST_PAD_PAD_TEMPLATE (pad))
2866 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2868 return gst_static_caps_get (&anycaps);
2873 * @pad: a #GstPad to get the peer of.
2875 * Gets the peer of @pad. This function refs the peer pad so
2876 * you need to unref it after use.
2878 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2883 gst_pad_get_peer (GstPad * pad)
2887 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2889 GST_OBJECT_LOCK (pad);
2890 result = GST_PAD_PEER (pad);
2892 gst_object_ref (result);
2893 GST_OBJECT_UNLOCK (pad);
2899 * gst_pad_get_allowed_caps:
2902 * Gets the capabilities of the allowed media types that can flow through
2903 * @pad and its peer.
2905 * The allowed capabilities is calculated as the intersection of the results of
2906 * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2907 * on the resulting caps.
2909 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2910 * caps when you no longer need it. This function returns NULL when @pad
2916 gst_pad_get_allowed_caps (GstPad * pad)
2923 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2925 GST_OBJECT_LOCK (pad);
2926 peer = GST_PAD_PEER (pad);
2927 if (G_UNLIKELY (peer == NULL))
2930 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2932 gst_object_ref (peer);
2933 GST_OBJECT_UNLOCK (pad);
2934 mycaps = gst_pad_get_caps (pad, NULL);
2936 peercaps = gst_pad_get_caps (peer, NULL);
2937 gst_object_unref (peer);
2939 caps = gst_caps_intersect (mycaps, peercaps);
2940 gst_caps_unref (peercaps);
2941 gst_caps_unref (mycaps);
2943 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2950 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2951 GST_OBJECT_UNLOCK (pad);
2958 * gst_pad_get_negotiated_caps:
2961 * Gets the capabilities of the media type that currently flows through @pad
2964 * This function can be used on both src and sinkpads. Note that srcpads are
2965 * always negotiated before sinkpads so it is possible that the negotiated caps
2966 * on the srcpad do not match the negotiated caps of the peer.
2968 * Returns: (transfer full): the negotiated #GstCaps of the pad link. Unref
2969 * the caps when you no longer need it. This function returns NULL when
2970 * the @pad has no peer or is not negotiated yet.
2975 gst_pad_get_negotiated_caps (GstPad * pad)
2977 GstCaps *caps = NULL;
2980 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2982 GST_OBJECT_LOCK (pad);
2984 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2987 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2989 if ((caps = get_pad_caps (pad)))
2990 gst_caps_ref (caps);
2992 GST_OBJECT_UNLOCK (pad);
2994 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
3001 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
3002 GST_OBJECT_UNLOCK (pad);
3008 * gst_pad_iterate_internal_links_default:
3009 * @pad: the #GstPad to get the internal links of.
3011 * Iterate the list of pads to which the given pad is linked to inside of
3012 * the parent element.
3013 * This is the default handler, and thus returns an iterator of all of the
3014 * pads inside the parent element with opposite direction.
3016 * The caller must free this iterator after use with gst_iterator_free().
3018 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3019 * returned pad with gst_object_unref().
3024 gst_pad_iterate_internal_links_default (GstPad * pad)
3032 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3037 GST_OBJECT_LOCK (pad);
3038 parent = GST_PAD_PARENT (pad);
3039 if (!parent || !GST_IS_ELEMENT (parent))
3042 gst_object_ref (parent);
3043 GST_OBJECT_UNLOCK (pad);
3045 if (pad->direction == GST_PAD_SRC)
3046 padlist = &parent->sinkpads;
3048 padlist = &parent->srcpads;
3050 GST_DEBUG_OBJECT (pad, "Making iterator");
3052 cookie = &parent->pads_cookie;
3054 lock = GST_OBJECT_GET_LOCK (parent);
3057 res = gst_iterator_new_list (GST_TYPE_PAD,
3058 lock, cookie, padlist, (GObject *) owner, NULL);
3060 gst_object_unref (owner);
3067 GST_OBJECT_UNLOCK (pad);
3068 GST_DEBUG_OBJECT (pad, "no parent element");
3074 * gst_pad_iterate_internal_links:
3075 * @pad: the GstPad to get the internal links of.
3077 * Gets an iterator for the pads to which the given pad is linked to inside
3078 * of the parent element.
3080 * Each #GstPad element yielded by the iterator will have its refcount increased,
3081 * so unref after use.
3083 * Free-function: gst_iterator_free
3085 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
3086 * pad does not have an iterator function configured. Use
3087 * gst_iterator_free() after usage.
3092 gst_pad_iterate_internal_links (GstPad * pad)
3094 GstIterator *res = NULL;
3096 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3098 if (GST_PAD_ITERINTLINKFUNC (pad))
3099 res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3105 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3107 gboolean result = FALSE;
3109 gboolean done = FALSE;
3110 GValue item = { 0, };
3112 GList *pushed_pads = NULL;
3114 GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3115 event, GST_EVENT_TYPE_NAME (event));
3117 iter = gst_pad_iterate_internal_links (pad);
3123 switch (gst_iterator_next (iter, &item)) {
3124 case GST_ITERATOR_OK:
3125 eventpad = g_value_get_object (&item);
3127 /* if already pushed, skip */
3128 if (g_list_find (pushed_pads, eventpad)) {
3129 g_value_reset (&item);
3133 if (GST_PAD_IS_SRC (eventpad)) {
3134 /* for each pad we send to, we should ref the event; it's up
3135 * to downstream to unref again when handled. */
3136 GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3137 event, GST_EVENT_TYPE_NAME (event),
3138 GST_DEBUG_PAD_NAME (eventpad));
3139 gst_event_ref (event);
3140 result |= gst_pad_push_event (eventpad, event);
3142 /* we only send the event on one pad, multi-sinkpad elements
3143 * should implement a handler */
3144 GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3145 event, GST_EVENT_TYPE_NAME (event),
3146 GST_DEBUG_PAD_NAME (eventpad));
3147 result = gst_pad_push_event (eventpad, event);
3152 pushed_pads = g_list_prepend (pushed_pads, eventpad);
3154 g_value_reset (&item);
3156 case GST_ITERATOR_RESYNC:
3157 /* We don't reset the result here because we don't push the event
3158 * again on pads that got the event already and because we need
3159 * to consider the result of the previous pushes */
3160 gst_iterator_resync (iter);
3162 case GST_ITERATOR_ERROR:
3163 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3166 case GST_ITERATOR_DONE:
3171 g_value_unset (&item);
3172 gst_iterator_free (iter);
3176 /* If this is a sinkpad and we don't have pads to send the event to, we
3177 * return TRUE. This is so that when using the default handler on a sink
3178 * element, we don't fail to push it. */
3180 result = GST_PAD_IS_SINK (pad);
3182 g_list_free (pushed_pads);
3184 /* we handled the incoming event so we unref once */
3186 GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3187 gst_event_unref (event);
3194 * gst_pad_event_default:
3195 * @pad: a #GstPad to call the default event handler on.
3196 * @event: (transfer full): the #GstEvent to handle.
3198 * Invokes the default event handler for the given pad. End-of-stream and
3199 * discontinuity events are handled specially, and then the event is sent to all
3200 * pads internally linked to @pad. Note that if there are many possible sink
3201 * pads that are internally linked to @pad, only one will be sent an event.
3202 * Multi-sinkpad elements should implement custom event handlers.
3204 * Returns: TRUE if the event was sent succesfully.
3207 gst_pad_event_default (GstPad * pad, GstEvent * event)
3209 gboolean result = TRUE, forward = TRUE;
3211 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3212 g_return_val_if_fail (event != NULL, FALSE);
3214 GST_LOG_OBJECT (pad, "default event handler");
3216 switch (GST_EVENT_TYPE (event)) {
3219 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3220 gst_pad_pause_task (pad);
3223 case GST_EVENT_CAPS:
3225 /* don't forward by default */
3234 result = gst_pad_event_default_dispatch (pad, event);
3236 gst_event_unref (event);
3242 * gst_pad_dispatcher:
3243 * @pad: a #GstPad to dispatch.
3244 * @dispatch: the #GstPadDispatcherFunction to call.
3245 * @data: (closure): gpointer user data passed to the dispatcher function.
3247 * Invokes the given dispatcher function on each respective peer of
3248 * all pads that are internally linked to the given pad.
3249 * The GstPadDispatcherFunction should return TRUE when no further pads
3250 * need to be processed.
3252 * Returns: TRUE if one of the dispatcher functions returned TRUE.
3255 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3258 gboolean res = FALSE;
3259 GstIterator *iter = NULL;
3260 gboolean done = FALSE;
3261 GValue item = { 0, };
3263 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3264 g_return_val_if_fail (dispatch != NULL, FALSE);
3266 iter = gst_pad_iterate_internal_links (pad);
3272 switch (gst_iterator_next (iter, &item)) {
3273 case GST_ITERATOR_OK:
3275 GstPad *int_pad = g_value_get_object (&item);
3276 GstPad *int_peer = gst_pad_get_peer (int_pad);
3279 GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3280 GST_DEBUG_PAD_NAME (int_peer));
3281 done = res = dispatch (int_peer, data);
3282 gst_object_unref (int_peer);
3284 GST_DEBUG_OBJECT (int_pad, "no peer");
3286 g_value_reset (&item);
3289 case GST_ITERATOR_RESYNC:
3290 gst_iterator_resync (iter);
3292 case GST_ITERATOR_ERROR:
3294 GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3296 case GST_ITERATOR_DONE:
3301 g_value_unset (&item);
3302 gst_iterator_free (iter);
3304 GST_DEBUG_OBJECT (pad, "done, result %d", res);
3313 * @pad: a #GstPad to invoke the default query on.
3314 * @query: (transfer none): the #GstQuery to perform.
3316 * Dispatches a query to a pad. The query should have been allocated by the
3317 * caller via one of the type-specific allocation functions. The element that
3318 * the pad belongs to is responsible for filling the query with an appropriate
3319 * response, which should then be parsed with a type-specific query parsing
3322 * Again, the caller is responsible for both the allocation and deallocation of
3323 * the query structure.
3325 * Please also note that some queries might need a running pipeline to work.
3327 * Returns: TRUE if the query could be performed.
3330 gst_pad_query (GstPad * pad, GstQuery * query)
3333 GstPadQueryFunction func;
3335 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3336 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3338 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3339 GST_QUERY_TYPE_NAME (query));
3341 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3344 res = func (pad, query);
3346 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3347 GST_QUERY_TYPE_NAME (query), res);
3353 GST_DEBUG_OBJECT (pad, "had no query function");
3359 * gst_pad_peer_query:
3360 * @pad: a #GstPad to invoke the peer query on.
3361 * @query: (transfer none): the #GstQuery to perform.
3363 * Performs gst_pad_query() on the peer of @pad.
3365 * The caller is responsible for both the allocation and deallocation of
3366 * the query structure.
3368 * Returns: TRUE if the query could be performed. This function returns %FALSE
3369 * if @pad has no peer.
3374 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3379 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3380 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3382 GST_OBJECT_LOCK (pad);
3384 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3385 GST_QUERY_TYPE_NAME (query));
3387 peerpad = GST_PAD_PEER (pad);
3388 if (G_UNLIKELY (peerpad == NULL))
3391 gst_object_ref (peerpad);
3392 GST_OBJECT_UNLOCK (pad);
3394 result = gst_pad_query (peerpad, query);
3396 gst_object_unref (peerpad);
3403 GST_WARNING_OBJECT (pad, "pad has no peer");
3404 GST_OBJECT_UNLOCK (pad);
3410 * gst_pad_query_default:
3411 * @pad: a #GstPad to call the default query handler on.
3412 * @query: (transfer none): the #GstQuery to handle.
3414 * Invokes the default query handler for the given pad.
3415 * The query is sent to all pads internally linked to @pad. Note that
3416 * if there are many possible sink pads that are internally linked to
3417 * @pad, only one will be sent the query.
3418 * Multi-sinkpad elements should implement custom query handlers.
3420 * Returns: TRUE if the query was performed succesfully.
3423 gst_pad_query_default (GstPad * pad, GstQuery * query)
3425 switch (GST_QUERY_TYPE (query)) {
3426 case GST_QUERY_POSITION:
3427 case GST_QUERY_SEEKING:
3428 case GST_QUERY_FORMATS:
3429 case GST_QUERY_LATENCY:
3430 case GST_QUERY_JITTER:
3431 case GST_QUERY_RATE:
3432 case GST_QUERY_CONVERT:
3434 return gst_pad_dispatcher
3435 (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3440 * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3441 * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3444 * This function performs the pad blocking when an event, buffer push
3445 * is performed on a _SRC_ pad. It blocks the streaming thread after
3446 * informing the pad has been blocked.
3448 * An application can with this method wait and block any streaming
3449 * thread and perform operations such as seeking or linking.
3451 * Two methods are available for notifying the application of the
3453 * - the callback method, which happens in the STREAMING thread with
3454 * the STREAM_LOCK held. With this method, the most useful way of
3455 * dealing with the callback is to post a message to the main thread
3456 * where the pad block can then be handled outside of the streaming
3457 * thread. With the last method one can perform all operations such
3458 * as doing a state change, linking, unblocking, seeking etc on the
3460 * - the GCond signal method, which makes any thread unblock when
3461 * the pad block happens.
3463 * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3464 * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3468 static GstFlowReturn
3469 handle_pad_block (GstPad * pad)
3471 GstPadBlockCallback callback;
3473 GstFlowReturn ret = GST_FLOW_OK;
3475 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3477 /* flushing, don't bother trying to block and return WRONG_STATE
3479 if (GST_PAD_IS_FLUSHING (pad))
3480 goto flushingnonref;
3482 /* we grab an extra ref for the callbacks, this is probably not
3483 * needed (callback code does not have a ref and cannot unref). I
3484 * think this was done to make it possible to unref the element in
3485 * the callback, which is in the end totally impossible as it
3486 * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3487 * all taken when calling this function. */
3488 gst_object_ref (pad);
3490 while (GST_PAD_IS_BLOCKED (pad)) {
3492 /* we either have a callback installed to notify the block or
3493 * some other thread is doing a GCond wait. */
3494 callback = pad->block_callback;
3495 pad->block_callback_called = TRUE;
3497 /* there is a callback installed, call it. We release the
3498 * lock so that the callback can do something usefull with the
3500 user_data = pad->block_data;
3501 GST_OBJECT_UNLOCK (pad);
3502 callback (pad, TRUE, user_data);
3503 GST_OBJECT_LOCK (pad);
3505 /* we released the lock, recheck flushing */
3506 if (GST_PAD_IS_FLUSHING (pad))
3509 /* no callback, signal the thread that is doing a GCond wait
3511 GST_PAD_BLOCK_BROADCAST (pad);
3513 } while (pad->block_callback_called == FALSE && GST_PAD_IS_BLOCKED (pad));
3515 /* OBJECT_LOCK could have been released when we did the callback, which
3516 * then could have made the pad unblock so we need to check the blocking
3517 * condition again. */
3518 if (!GST_PAD_IS_BLOCKED (pad))
3521 /* now we block the streaming thread. It can be unlocked when we
3522 * deactivate the pad (which will also set the FLUSHING flag) or
3523 * when the pad is unblocked. A flushing event will also unblock
3524 * the pad after setting the FLUSHING flag. */
3525 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3526 "Waiting to be unblocked or set flushing");
3527 GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3528 GST_PAD_BLOCK_WAIT (pad);
3529 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3531 /* see if we got unblocked by a flush or not */
3532 if (GST_PAD_IS_FLUSHING (pad))
3536 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3538 /* when we get here, the pad is unblocked again and we perform
3539 * the needed unblock code. */
3540 callback = pad->block_callback;
3542 /* we need to call the callback */
3543 user_data = pad->block_data;
3544 GST_OBJECT_UNLOCK (pad);
3545 callback (pad, FALSE, user_data);
3546 GST_OBJECT_LOCK (pad);
3548 /* we need to signal the thread waiting on the GCond */
3549 GST_PAD_BLOCK_BROADCAST (pad);
3552 gst_object_unref (pad);
3558 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3559 return GST_FLOW_WRONG_STATE;
3563 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3564 gst_object_unref (pad);
3565 return GST_FLOW_WRONG_STATE;
3572 * gst_pad_get_offset:
3575 * Get the offset applied to the running time of @pad. @pad has to be a source
3578 * Returns: the offset.
3581 gst_pad_get_offset (GstPad * pad)
3585 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3587 GST_OBJECT_LOCK (pad);
3588 result = pad->offset;
3589 GST_OBJECT_UNLOCK (pad);
3595 * gst_pad_set_offset:
3597 * @offset: the offset
3599 * Set the offset that will be applied to the running time of @pad.
3602 gst_pad_set_offset (GstPad * pad, gint64 offset)
3608 g_return_if_fail (GST_IS_PAD (pad));
3610 GST_OBJECT_LOCK (pad);
3611 /* if nothing changed, do nothing */
3612 if (pad->offset == offset)
3615 pad->offset = offset;
3617 /* if no peer, we just updated the offset */
3618 if ((peer = GST_PAD_PEER (pad)) == NULL)
3621 /* switch pads around when dealing with a sinkpad */
3622 if (GST_PAD_IS_SINK (pad)) {
3623 /* ref the peer so it doesn't go away when we release the lock */
3624 tmp = gst_object_ref (peer);
3625 /* make sure we get the peer (the srcpad) */
3626 GST_OBJECT_UNLOCK (pad);
3632 GST_OBJECT_LOCK (pad);
3633 /* check if the pad didn't get relinked */
3634 if (GST_PAD_PEER (pad) != peer)
3637 /* we can release the ref now */
3638 gst_object_unref (peer);
3641 /* the index of the segment event in the array */
3642 idx = GST_EVENT_STICKY_IDX_TYPE (GST_EVENT_SEGMENT);
3644 /* lock order is srcpad >> sinkpad */
3645 GST_OBJECT_LOCK (peer);
3646 /* take the current segment event, adjust it and then place
3647 * it on the sinkpad. events on the srcpad are always active. */
3648 if (replace_event (pad, peer, idx))
3649 GST_OBJECT_FLAG_SET (peer, GST_PAD_NEED_EVENTS);
3651 GST_OBJECT_UNLOCK (peer);
3654 GST_OBJECT_UNLOCK (pad);
3658 /**********************************************************************
3659 * Data passing functions
3663 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3666 GValue args[2] = { {0}, {0} };
3671 g_value_init (&ret, G_TYPE_BOOLEAN);
3672 g_value_set_boolean (&ret, TRUE);
3673 g_value_init (&args[0], GST_TYPE_PAD);
3674 g_value_set_object (&args[0], pad);
3675 g_value_init (&args[1], GST_MINI_OBJECT_TYPE (obj));
3676 g_value_set_boxed (&args[1], obj);
3678 if (GST_IS_EVENT (obj))
3679 detail = event_quark;
3680 else if (GST_IS_BUFFER (obj))
3681 detail = buffer_quark;
3682 else if (GST_IS_BUFFER_LIST (obj))
3683 detail = buffer_list_quark;
3688 g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3689 res = g_value_get_boolean (&ret);
3692 g_value_unset (&ret);
3693 g_value_unset (&args[0]);
3694 g_value_unset (&args[1]);
3700 gst_pad_data_unref (gboolean is_buffer, void *data)
3702 if (G_LIKELY (is_buffer)) {
3703 gst_buffer_unref (data);
3705 gst_buffer_list_unref (data);
3709 static GstFlowReturn
3710 pad_pre_chain (GstPad * pad, gpointer data)
3713 gboolean needs_events;
3714 gboolean emit_signal;
3716 GST_PAD_STREAM_LOCK (pad);
3718 GST_OBJECT_LOCK (pad);
3719 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3722 needs_events = GST_PAD_NEEDS_EVENTS (pad);
3723 if (G_UNLIKELY (needs_events)) {
3724 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
3726 GST_DEBUG_OBJECT (pad, "need to update all events");
3727 ret = gst_pad_update_events (pad);
3728 if (G_UNLIKELY (ret != GST_FLOW_OK))
3731 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3732 GST_OBJECT_UNLOCK (pad);
3734 /* see if the signal should be emited */
3735 if (G_UNLIKELY (emit_signal)) {
3736 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3744 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3745 "pushing, but pad was flushing");
3746 GST_OBJECT_UNLOCK (pad);
3747 GST_PAD_STREAM_UNLOCK (pad);
3748 return GST_FLOW_WRONG_STATE;
3752 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "events were not accepted");
3753 GST_OBJECT_UNLOCK (pad);
3754 GST_PAD_STREAM_UNLOCK (pad);
3759 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
3760 GST_PAD_STREAM_UNLOCK (pad);
3761 return GST_FLOW_CUSTOM_SUCCESS;
3766 pad_post_chain (GstPad * pad)
3768 GST_PAD_STREAM_UNLOCK (pad);
3771 /* this is the chain function that does not perform the additional argument
3772 * checking for that little extra speed.
3774 static inline GstFlowReturn
3775 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
3779 if (G_UNLIKELY ((ret = pad_pre_chain (pad, data)) != GST_FLOW_OK))
3782 /* NOTE: we read the chainfunc unlocked.
3783 * we cannot hold the lock for the pad so we might send
3784 * the data to the wrong function. This is not really a
3785 * problem since functions are assigned at creation time
3786 * and don't change that often... */
3787 if (G_LIKELY (is_buffer)) {
3788 GstPadChainFunction chainfunc;
3790 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3793 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3794 "calling chainfunction &%s with buffer %p",
3795 GST_DEBUG_FUNCPTR_NAME (chainfunc), data);
3797 ret = chainfunc (pad, GST_BUFFER_CAST (data));
3799 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3800 "called chainfunction &%s with buffer %p, returned %s",
3801 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3803 GstPadChainListFunction chainlistfunc;
3805 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3808 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3809 "calling chainlistfunction &%s",
3810 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3812 ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
3814 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3815 "called chainlistfunction &%s, returned %s",
3816 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3819 pad_post_chain (pad);
3826 gst_pad_data_unref (is_buffer, data);
3829 case GST_FLOW_CUSTOM_SUCCESS:
3830 GST_DEBUG_OBJECT (pad, "dropped buffer");
3834 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3841 gst_pad_data_unref (is_buffer, data);
3842 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3843 "pushing, but not chainhandler");
3844 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3845 ("push on pad %s:%s but it has no chainfunction",
3846 GST_DEBUG_PAD_NAME (pad)));
3847 pad_post_chain (pad);
3848 return GST_FLOW_NOT_SUPPORTED;
3854 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3855 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3858 * Chain a buffer to @pad.
3860 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3862 * If the caps on @buffer are different from the current caps on @pad, this
3863 * function will call any setcaps function (see gst_pad_set_setcaps_function())
3864 * installed on @pad. If the new caps are not acceptable for @pad, this
3865 * function returns #GST_FLOW_NOT_NEGOTIATED.
3867 * The function proceeds calling the chain function installed on @pad (see
3868 * gst_pad_set_chain_function()) and the return value of that function is
3869 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3872 * In all cases, success or failure, the caller loses its reference to @buffer
3873 * after calling this function.
3875 * Returns: a #GstFlowReturn from the pad.
3880 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3882 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3883 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3884 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3886 return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
3889 static GstFlowReturn
3890 gst_pad_chain_list_default (GstPad * pad, GstBufferList * list)
3896 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3898 len = gst_buffer_list_len (list);
3901 for (i = 0; i < len; i++) {
3902 buffer = gst_buffer_list_get (list, i);
3903 ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_ref (buffer));
3904 if (ret != GST_FLOW_OK)
3907 gst_buffer_list_unref (list);
3913 * gst_pad_chain_list:
3914 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3915 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3918 * Chain a bufferlist to @pad.
3920 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3922 * If the caps on the first buffer of @list are different from the current
3923 * caps on @pad, this function will call any setcaps function
3924 * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
3925 * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
3927 * The function proceeds calling the chainlist function installed on @pad (see
3928 * gst_pad_set_chain_list_function()) and the return value of that function is
3929 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3930 * chainlist function.
3932 * In all cases, success or failure, the caller loses its reference to @list
3933 * after calling this function.
3937 * Returns: a #GstFlowReturn from the pad.
3942 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3944 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3945 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3946 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3948 return gst_pad_chain_data_unchecked (pad, FALSE, list);
3951 static GstFlowReturn
3952 pad_pre_push (GstPad * pad, GstPad ** peer, gpointer data)
3955 gboolean need_probes, did_probes = FALSE;
3958 GST_OBJECT_LOCK (pad);
3959 /* FIXME: this check can go away; pad_set_blocked could be implemented with
3960 * probes completely or probes with an extended pad block. */
3961 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
3962 if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
3965 need_probes = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
3967 /* we emit signals on the pad arg, the peer will have a chance to
3968 * emit in the _chain() function */
3969 if (G_UNLIKELY (need_probes && !did_probes)) {
3971 /* unlock before emitting */
3972 GST_OBJECT_UNLOCK (pad);
3974 /* if the signal handler returned FALSE, it means we should just drop the
3976 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (data)))
3982 if (G_UNLIKELY ((*peer = GST_PAD_PEER (pad)) == NULL))
3985 /* take ref to peer pad before releasing the lock */
3986 gst_object_ref (*peer);
3988 GST_OBJECT_UNLOCK (pad);
3995 GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
3996 GST_OBJECT_UNLOCK (pad);
4001 GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4002 return GST_FLOW_CUSTOM_SUCCESS;
4006 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4007 "pushing, but it was not linked");
4008 GST_OBJECT_UNLOCK (pad);
4009 return GST_FLOW_NOT_LINKED;
4014 pad_post_push (GstPad * pad)
4016 GST_OBJECT_LOCK (pad);
4018 GST_OBJECT_UNLOCK (pad);
4021 static GstFlowReturn
4022 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4027 if ((ret = pad_pre_push (pad, &peer, data)) != GST_FLOW_OK)
4030 ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4032 gst_object_unref (peer);
4034 pad_post_push (pad);
4038 /* ERROR recovery here */
4041 gst_pad_data_unref (is_buffer, data);
4044 case GST_FLOW_CUSTOM_SUCCESS:
4045 GST_DEBUG_OBJECT (pad, "dropped buffer");
4049 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4058 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4059 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4062 * Pushes a buffer to the peer of @pad.
4064 * This function will call an installed pad block before triggering any
4065 * installed pad probes.
4067 * If the caps on @buffer are different from the currently configured caps on
4068 * @pad, this function will call any installed setcaps function on @pad (see
4069 * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4070 * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4072 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4073 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4076 * In all cases, success or failure, the caller loses its reference to @buffer
4077 * after calling this function.
4079 * Returns: a #GstFlowReturn from the peer pad.
4084 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4086 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4087 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4088 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4090 return gst_pad_push_data (pad, TRUE, buffer);
4094 * gst_pad_push_list:
4095 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4096 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4099 * Pushes a buffer list to the peer of @pad.
4101 * This function will call an installed pad block before triggering any
4102 * installed pad probes.
4104 * If the caps on the first buffer in the first group of @list are different
4105 * from the currently configured caps on @pad, this function will call any
4106 * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4107 * case of failure to renegotiate the new format, this function returns
4108 * #GST_FLOW_NOT_NEGOTIATED.
4110 * If there are any probes installed on @pad every group of the buffer list
4111 * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4112 * buffer list will be unreffed.
4114 * The function proceeds calling the chain function on the peer pad and returns
4115 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4116 * be returned. If the peer pad does not have any installed chainlist function
4117 * every group buffer of the list will be merged into a normal #GstBuffer and
4118 * chained via gst_pad_chain().
4120 * In all cases, success or failure, the caller loses its reference to @list
4121 * after calling this function.
4123 * Returns: a #GstFlowReturn from the peer pad.
4130 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4132 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4133 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4134 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4136 return gst_pad_push_data (pad, FALSE, list);
4139 static GstFlowReturn
4140 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4141 GstBuffer ** buffer)
4144 GstPadGetRangeFunction getrangefunc;
4145 gboolean emit_signal;
4147 GST_PAD_STREAM_LOCK (pad);
4149 GST_OBJECT_LOCK (pad);
4150 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4153 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4154 GST_OBJECT_UNLOCK (pad);
4156 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4159 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4160 "calling getrangefunc %s, offset %"
4161 G_GUINT64_FORMAT ", size %u",
4162 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4164 ret = getrangefunc (pad, offset, size, buffer);
4166 /* can only fire the signal if we have a valid buffer */
4167 if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4168 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4171 GST_PAD_STREAM_UNLOCK (pad);
4173 if (G_UNLIKELY (ret != GST_FLOW_OK))
4174 goto get_range_failed;
4181 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4182 "pulling range, but pad was flushing");
4183 GST_OBJECT_UNLOCK (pad);
4184 GST_PAD_STREAM_UNLOCK (pad);
4185 return GST_FLOW_WRONG_STATE;
4189 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4190 ("pullrange on pad %s:%s but it has no getrangefunction",
4191 GST_DEBUG_PAD_NAME (pad)));
4192 GST_PAD_STREAM_UNLOCK (pad);
4193 return GST_FLOW_NOT_SUPPORTED;
4197 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4198 "Dropping data after FALSE probe return");
4199 GST_PAD_STREAM_UNLOCK (pad);
4200 gst_buffer_unref (*buffer);
4202 return GST_FLOW_UNEXPECTED;
4207 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4208 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4209 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4215 * gst_pad_get_range:
4216 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4217 * @offset: The start offset of the buffer
4218 * @size: The length of the buffer
4219 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4220 * returns #GST_FLOW_ERROR if %NULL.
4222 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4223 * immediatly and @buffer is %NULL.
4225 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4226 * description of a getrange function. If @pad has no getrange function
4227 * installed (see gst_pad_set_getrange_function()) this function returns
4228 * #GST_FLOW_NOT_SUPPORTED.
4230 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4232 * Returns: a #GstFlowReturn from the pad.
4237 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4238 GstBuffer ** buffer)
4240 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4241 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4242 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4244 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4248 * gst_pad_pull_range:
4249 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4250 * @offset: The start offset of the buffer
4251 * @size: The length of the buffer
4252 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4253 * GST_FLOW_ERROR if %NULL.
4255 * Pulls a @buffer from the peer pad.
4257 * This function will first trigger the pad block signal if it was
4260 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4261 * function returns the result of gst_pad_get_range() on the peer pad.
4262 * See gst_pad_get_range() for a list of return values and for the
4263 * semantics of the arguments of this function.
4265 * @buffer's caps must either be unset or the same as what is already
4266 * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4269 * Returns: a #GstFlowReturn from the peer pad.
4270 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4271 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4272 * @buffer may not be used or freed when any other return value than
4273 * #GST_FLOW_OK is returned.
4278 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4279 GstBuffer ** buffer)
4283 gboolean emit_signal;
4284 gboolean needs_events;
4286 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4287 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4288 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4290 GST_OBJECT_LOCK (pad);
4292 while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4293 handle_pad_block (pad);
4295 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4298 /* signal emision for the pad, peer has chance to emit when
4299 * we call _get_range() */
4300 emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4302 gst_object_ref (peer);
4303 GST_OBJECT_UNLOCK (pad);
4305 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4307 gst_object_unref (peer);
4309 if (G_UNLIKELY (ret != GST_FLOW_OK))
4310 goto pull_range_failed;
4312 /* can only fire the signal if we have a valid buffer */
4313 if (G_UNLIKELY (emit_signal)) {
4314 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (*buffer)))
4318 GST_OBJECT_LOCK (pad);
4320 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4321 if (G_UNLIKELY (needs_events)) {
4322 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4324 GST_DEBUG_OBJECT (pad, "we need to update the events");
4325 ret = gst_pad_update_events (pad);
4326 if (G_UNLIKELY (ret != GST_FLOW_OK))
4329 GST_OBJECT_UNLOCK (pad);
4333 /* ERROR recovery here */
4336 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4337 "pulling range, but it was not linked");
4338 GST_OBJECT_UNLOCK (pad);
4339 return GST_FLOW_NOT_LINKED;
4344 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4345 (ret >= GST_FLOW_UNEXPECTED) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4346 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4351 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4352 "Dropping data after FALSE probe return");
4353 gst_buffer_unref (*buffer);
4355 return GST_FLOW_UNEXPECTED;
4359 GST_OBJECT_UNLOCK (pad);
4360 gst_buffer_unref (*buffer);
4362 GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4363 "pullrange returned events that were not accepted");
4369 * gst_pad_push_event:
4370 * @pad: a #GstPad to push the event to.
4371 * @event: (transfer full): the #GstEvent to send to the pad.
4373 * Sends the event to the peer of the given pad. This function is
4374 * mainly used by elements to send events to their peer
4377 * This function takes owership of the provided event so you should
4378 * gst_event_ref() it if you want to reuse the event after this call.
4380 * Returns: TRUE if the event was handled.
4385 gst_pad_push_event (GstPad * pad, GstEvent * event)
4388 gboolean result, need_probes, did_probes = FALSE, did_event_actions = FALSE;
4391 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4392 g_return_val_if_fail (event != NULL, FALSE);
4393 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4395 GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4398 GST_OBJECT_LOCK (pad);
4400 /* Two checks to be made:
4401 * . (un)set the FLUSHING flag for flushing events,
4402 * . handle pad blocking */
4403 switch (GST_EVENT_TYPE (event)) {
4404 case GST_EVENT_FLUSH_START:
4405 GST_PAD_SET_FLUSHING (pad);
4407 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4408 /* flush start will have set the FLUSHING flag and will then
4409 * unlock all threads doing a GCond wait on the blocking pad. This
4410 * will typically unblock the STREAMING thread blocked on a pad. */
4411 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4412 "doing block signal.");
4413 GST_PAD_BLOCK_BROADCAST (pad);
4417 case GST_EVENT_FLUSH_STOP:
4418 GST_PAD_UNSET_FLUSHING (pad);
4424 /* store the event on the pad, but only on srcpads */
4425 if (GST_PAD_IS_SRC (pad) && GST_EVENT_IS_STICKY (event)) {
4426 if (GST_PAD_IS_FLUSHING (pad)) {
4431 idx = GST_EVENT_STICKY_IDX (event);
4432 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4433 GST_EVENT_TYPE_NAME (event), idx);
4435 /* srcpad sticky events always become active immediately */
4436 gst_event_replace (&pad->priv->events[idx].event, event);
4440 /* drop all events when blocking. Sticky events will stay on the pad and will
4441 * be activated on the peer when unblocking. */
4442 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4443 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding event");
4447 offset = pad->offset;
4448 need_probes = !did_probes && (GST_PAD_DO_EVENT_SIGNALS (pad) > 0);
4449 peerpad = GST_PAD_PEER (pad);
4451 /* backwards compatibility mode for caps */
4452 if (!did_event_actions) {
4453 did_event_actions = TRUE;
4455 switch (GST_EVENT_TYPE (event)) {
4456 case GST_EVENT_CAPS:
4460 GST_OBJECT_UNLOCK (pad);
4462 gst_event_parse_caps (event, &caps);
4463 /* FIXME, this is awkward because we don't check flushing here which means
4464 * that we can call the setcaps functions on flushing pads, this is not
4465 * quite what we want, otoh, this code should just go away and elements
4466 * that set caps on their srcpad should just setup stuff themselves. */
4467 gst_pad_call_setcaps (pad, caps);
4469 /* recheck everything, we released the lock */
4472 case GST_EVENT_SEGMENT:
4473 /* check if we need to adjust the segment */
4474 if (offset != 0 && (need_probes || peerpad != NULL)) {
4477 /* copy segment values */
4478 gst_event_copy_segment (event, &segment);
4479 gst_event_unref (event);
4481 /* adjust and make a new event with the offset applied */
4482 segment.base += offset;
4483 event = gst_event_new_segment (&segment);
4486 case GST_EVENT_RECONFIGURE:
4487 if (GST_PAD_IS_SINK (pad))
4488 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4495 /* send probes after modifying the events above */
4496 if (G_UNLIKELY (need_probes)) {
4498 GST_OBJECT_UNLOCK (pad);
4500 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4503 /* retry, we released the lock */
4507 /* now check the peer pad */
4508 if (peerpad == NULL)
4511 gst_object_ref (peerpad);
4513 GST_OBJECT_UNLOCK (pad);
4515 GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4516 GST_EVENT_TYPE_NAME (event), peerpad);
4518 result = gst_pad_send_event (peerpad, event);
4520 /* Note: we gave away ownership of the event at this point */
4521 GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4524 gst_object_unref (peerpad);
4526 GST_OBJECT_LOCK (pad);
4528 GST_OBJECT_UNLOCK (pad);
4532 /* ERROR handling */
4535 GST_DEBUG_OBJECT (pad,
4536 "Not forwarding event since we're flushing and blocking");
4537 gst_event_unref (event);
4538 GST_OBJECT_UNLOCK (pad);
4543 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4544 gst_event_unref (event);
4549 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4550 GST_OBJECT_UNLOCK (pad);
4551 gst_event_unref (event);
4556 GST_DEBUG_OBJECT (pad, "Dropping event because pad is flushing");
4557 GST_OBJECT_UNLOCK (pad);
4558 gst_event_unref (event);
4564 * gst_pad_send_event:
4565 * @pad: a #GstPad to send the event to.
4566 * @event: (transfer full): the #GstEvent to send to the pad.
4568 * Sends the event to the pad. This function can be used
4569 * by applications to send events in the pipeline.
4571 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4572 * sink pad, @event should be a downstream event. For example, you would not
4573 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4574 * Furthermore, some downstream events have to be serialized with data flow,
4575 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4576 * the event needs to be serialized with data flow, this function will take the
4577 * pad's stream lock while calling its event function.
4579 * To find out whether an event type is upstream, downstream, or downstream and
4580 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4581 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4582 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4583 * plugin doesn't need to bother itself with this information; the core handles
4584 * all necessary locks and checks.
4586 * This function takes owership of the provided event so you should
4587 * gst_event_ref() it if you want to reuse the event after this call.
4589 * Returns: TRUE if the event was handled.
4592 gst_pad_send_event (GstPad * pad, GstEvent * event)
4594 gboolean result = FALSE;
4595 gboolean serialized, need_unlock = FALSE, needs_events, sticky;
4597 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4598 g_return_val_if_fail (event != NULL, FALSE);
4600 GST_OBJECT_LOCK (pad);
4601 if (GST_PAD_IS_SINK (pad)) {
4602 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4603 goto wrong_direction;
4604 serialized = GST_EVENT_IS_SERIALIZED (event);
4605 sticky = GST_EVENT_IS_STICKY (event);
4606 } else if (GST_PAD_IS_SRC (pad)) {
4607 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4608 goto wrong_direction;
4609 /* events on srcpad never are serialized and sticky */
4610 serialized = sticky = FALSE;
4612 goto unknown_direction;
4615 if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4616 GST_OBJECT_UNLOCK (pad);
4618 if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4621 GST_OBJECT_LOCK (pad);
4623 /* get the flag first, we clear it when we have a FLUSH or a non-serialized
4625 needs_events = GST_PAD_NEEDS_EVENTS (pad);
4627 switch (GST_EVENT_TYPE (event)) {
4628 case GST_EVENT_FLUSH_START:
4629 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4630 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4632 /* can't even accept a flush begin event when flushing */
4633 if (GST_PAD_IS_FLUSHING (pad))
4636 GST_PAD_SET_FLUSHING (pad);
4637 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4638 needs_events = FALSE;
4640 case GST_EVENT_FLUSH_STOP:
4641 if (G_LIKELY (GST_PAD_ACTIVATE_MODE (pad) != GST_ACTIVATE_NONE)) {
4642 GST_PAD_UNSET_FLUSHING (pad);
4643 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4645 GST_OBJECT_UNLOCK (pad);
4646 /* grab stream lock */
4647 GST_PAD_STREAM_LOCK (pad);
4649 GST_OBJECT_LOCK (pad);
4650 needs_events = FALSE;
4652 case GST_EVENT_RECONFIGURE:
4653 if (GST_PAD_IS_SRC (pad))
4654 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_RECONFIGURE);
4656 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4657 GST_EVENT_TYPE_NAME (event));
4660 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4661 GST_OBJECT_UNLOCK (pad);
4662 GST_PAD_STREAM_LOCK (pad);
4664 GST_OBJECT_LOCK (pad);
4666 /* don't forward events on non-serialized events */
4667 needs_events = FALSE;
4670 /* store the event on the pad, but only on srcpads. We need to store the
4671 * event before checking the flushing flag. */
4676 switch (GST_EVENT_TYPE (event)) {
4677 case GST_EVENT_SEGMENT:
4678 if (pad->offset != 0) {
4681 /* copy segment values */
4682 gst_event_copy_segment (event, &segment);
4683 gst_event_unref (event);
4685 /* adjust and make a new event with the offset applied */
4686 segment.base += pad->offset;
4687 event = gst_event_new_segment (&segment);
4694 idx = GST_EVENT_STICKY_IDX (event);
4695 ev = &pad->priv->events[idx];
4697 GST_LOG_OBJECT (pad, "storing sticky event %s at index %u",
4698 GST_EVENT_TYPE_NAME (event), idx);
4700 if (ev->event != event) {
4701 gst_event_replace (&ev->pending, event);
4702 /* set the flag so that we update the events next time. We would
4703 * usually update below but we might be flushing too. */
4704 GST_OBJECT_FLAG_SET (pad, GST_PAD_NEED_EVENTS);
4705 needs_events = TRUE;
4708 /* now check the flushing flag */
4709 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4715 if (G_UNLIKELY (needs_events)) {
4718 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_EVENTS);
4720 GST_DEBUG_OBJECT (pad, "need to update all events");
4721 ret = gst_pad_update_events (pad);
4722 if (ret != GST_FLOW_OK)
4724 GST_OBJECT_UNLOCK (pad);
4726 gst_event_unref (event);
4730 GstPadEventFunction eventfunc;
4732 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4735 GST_OBJECT_UNLOCK (pad);
4737 result = do_event_function (pad, event, eventfunc);
4741 GST_PAD_STREAM_UNLOCK (pad);
4743 GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4747 /* ERROR handling */
4750 g_warning ("pad %s:%s sending %s event in wrong direction",
4751 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4752 GST_OBJECT_UNLOCK (pad);
4753 gst_event_unref (event);
4758 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4759 GST_OBJECT_UNLOCK (pad);
4760 gst_event_unref (event);
4765 g_warning ("pad %s:%s has no event handler, file a bug.",
4766 GST_DEBUG_PAD_NAME (pad));
4767 GST_OBJECT_UNLOCK (pad);
4769 GST_PAD_STREAM_UNLOCK (pad);
4770 gst_event_unref (event);
4775 GST_OBJECT_UNLOCK (pad);
4777 GST_PAD_STREAM_UNLOCK (pad);
4778 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4779 "Received event on flushing pad. Discarding");
4780 gst_event_unref (event);
4785 GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4786 gst_event_unref (event);
4791 GST_OBJECT_UNLOCK (pad);
4793 GST_PAD_STREAM_UNLOCK (pad);
4794 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad, "Update events failed");
4795 gst_event_unref (event);
4801 * gst_pad_set_element_private:
4802 * @pad: the #GstPad to set the private data of.
4803 * @priv: The private data to attach to the pad.
4805 * Set the given private data gpointer on the pad.
4806 * This function can only be used by the element that owns the pad.
4807 * No locking is performed in this function.
4810 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4812 pad->element_private = priv;
4816 * gst_pad_get_element_private:
4817 * @pad: the #GstPad to get the private data of.
4819 * Gets the private data of a pad.
4820 * No locking is performed in this function.
4822 * Returns: (transfer none): a #gpointer to the private data.
4825 gst_pad_get_element_private (GstPad * pad)
4827 return pad->element_private;
4831 * gst_pad_get_sticky_event:
4832 * @pad: the #GstPad to get the event from.
4833 * @event_type: the #GstEventType that should be retrieved.
4835 * Returns a new reference of the sticky event of type @event_type
4838 * Returns: (transfer full): a #GstEvent of type @event_type. Unref after usage.
4841 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type)
4843 GstEvent *event = NULL;
4846 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4847 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4849 idx = GST_EVENT_STICKY_IDX_TYPE (event_type);
4851 GST_OBJECT_LOCK (pad);
4852 if ((event = pad->priv->events[idx].event)) {
4853 gst_event_ref (event);
4855 GST_OBJECT_UNLOCK (pad);
4861 * gst_pad_sticky_events_foreach:
4862 * @pad: the #GstPad that should be used for iteration.
4863 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that should be called for every event.
4864 * @user_data: (closure): the optional user data.
4866 * Iterates all active sticky events on @pad and calls @foreach_func for every
4867 * event. If @foreach_func returns something else than GST_FLOW_OK the iteration
4868 * is immediately stopped.
4870 * Returns: GST_FLOW_OK if iteration was successful
4873 gst_pad_sticky_events_foreach (GstPad * pad,
4874 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4876 GstFlowReturn ret = GST_FLOW_OK;
4880 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4881 g_return_val_if_fail (foreach_func != NULL, GST_FLOW_ERROR);
4883 GST_OBJECT_LOCK (pad);
4886 for (i = 0; i < GST_EVENT_MAX_STICKY; i++) {
4890 ev = &pad->priv->events[i];
4892 /* skip without active event */
4893 if ((event = ev->event) == NULL)
4896 gst_event_ref (event);
4897 GST_OBJECT_UNLOCK (pad);
4899 res = foreach_func (pad, event, user_data);
4901 GST_OBJECT_LOCK (pad);
4902 gst_event_unref (event);
4904 if (res != GST_FLOW_OK) {
4909 /* things could have changed while we release the lock, check if we still
4910 * are handling the same event, if we don't something changed and we have
4911 * to try again. FIXME. we need a cookie here. */
4912 if (event != ev->event) {
4913 GST_DEBUG_OBJECT (pad, "events changed, restarting");
4917 GST_OBJECT_UNLOCK (pad);
4923 do_stream_status (GstPad * pad, GstStreamStatusType type,
4924 GThread * thread, GstTask * task)
4928 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4930 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4931 if (GST_IS_ELEMENT (parent)) {
4932 GstMessage *message;
4933 GValue value = { 0 };
4935 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4936 gchar *tname, *ename, *pname;
4938 /* create a good task name */
4939 ename = gst_element_get_name (parent);
4940 pname = gst_pad_get_name (pad);
4941 tname = g_strdup_printf ("%s:%s", ename, pname);
4945 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4949 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4952 g_value_init (&value, GST_TYPE_TASK);
4953 g_value_set_object (&value, task);
4954 gst_message_set_stream_status_object (message, &value);
4955 g_value_unset (&value);
4957 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4958 gst_element_post_message (parent, message);
4960 gst_object_unref (parent);
4965 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4967 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4972 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4974 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4978 static GstTaskThreadCallbacks thr_callbacks = {
4984 * gst_pad_start_task:
4985 * @pad: the #GstPad to start the task of
4986 * @func: the task function to call
4987 * @data: data passed to the task function
4989 * Starts a task that repeatedly calls @func with @data. This function
4990 * is mostly used in pad activation functions to start the dataflow.
4991 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4992 * before @func is called.
4994 * Returns: a %TRUE if the task could be started.
4997 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5002 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5003 g_return_val_if_fail (func != NULL, FALSE);
5005 GST_DEBUG_OBJECT (pad, "start task");
5007 GST_OBJECT_LOCK (pad);
5008 task = GST_PAD_TASK (pad);
5010 task = gst_task_create (func, data);
5011 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5012 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5013 GST_DEBUG_OBJECT (pad, "created task");
5014 GST_PAD_TASK (pad) = task;
5015 gst_object_ref (task);
5016 /* release lock to post the message */
5017 GST_OBJECT_UNLOCK (pad);
5019 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5021 gst_object_unref (task);
5023 GST_OBJECT_LOCK (pad);
5024 /* nobody else is supposed to have changed the pad now */
5025 if (GST_PAD_TASK (pad) != task)
5026 goto concurrent_stop;
5028 res = gst_task_set_state (task, GST_TASK_STARTED);
5029 GST_OBJECT_UNLOCK (pad);
5036 GST_OBJECT_UNLOCK (pad);
5042 * gst_pad_pause_task:
5043 * @pad: the #GstPad to pause the task of
5045 * Pause the task of @pad. This function will also wait until the
5046 * function executed by the task is finished if this function is not
5047 * called from the task function.
5049 * Returns: a TRUE if the task could be paused or FALSE when the pad
5053 gst_pad_pause_task (GstPad * pad)
5058 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5060 GST_DEBUG_OBJECT (pad, "pause task");
5062 GST_OBJECT_LOCK (pad);
5063 task = GST_PAD_TASK (pad);
5066 res = gst_task_set_state (task, GST_TASK_PAUSED);
5067 GST_OBJECT_UNLOCK (pad);
5069 /* wait for task function to finish, this lock is recursive so it does nothing
5070 * when the pause is called from the task itself */
5071 GST_PAD_STREAM_LOCK (pad);
5072 GST_PAD_STREAM_UNLOCK (pad);
5078 GST_DEBUG_OBJECT (pad, "pad has no task");
5079 GST_OBJECT_UNLOCK (pad);
5085 * gst_pad_stop_task:
5086 * @pad: the #GstPad to stop the task of
5088 * Stop the task of @pad. This function will also make sure that the
5089 * function executed by the task will effectively stop if not called
5090 * from the GstTaskFunction.
5092 * This function will deadlock if called from the GstTaskFunction of
5093 * the task. Use gst_task_pause() instead.
5095 * Regardless of whether the pad has a task, the stream lock is acquired and
5096 * released so as to ensure that streaming through this pad has finished.
5098 * Returns: a TRUE if the task could be stopped or FALSE on error.
5101 gst_pad_stop_task (GstPad * pad)
5106 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5108 GST_DEBUG_OBJECT (pad, "stop task");
5110 GST_OBJECT_LOCK (pad);
5111 task = GST_PAD_TASK (pad);
5114 GST_PAD_TASK (pad) = NULL;
5115 res = gst_task_set_state (task, GST_TASK_STOPPED);
5116 GST_OBJECT_UNLOCK (pad);
5118 GST_PAD_STREAM_LOCK (pad);
5119 GST_PAD_STREAM_UNLOCK (pad);
5121 if (!gst_task_join (task))
5124 gst_object_unref (task);
5130 GST_DEBUG_OBJECT (pad, "no task");
5131 GST_OBJECT_UNLOCK (pad);
5133 GST_PAD_STREAM_LOCK (pad);
5134 GST_PAD_STREAM_UNLOCK (pad);
5136 /* this is not an error */
5141 /* this is bad, possibly the application tried to join the task from
5142 * the task's thread. We install the task again so that it will be stopped
5143 * again from the right thread next time hopefully. */
5144 GST_OBJECT_LOCK (pad);
5145 GST_DEBUG_OBJECT (pad, "join failed");
5146 /* we can only install this task if there was no other task */
5147 if (GST_PAD_TASK (pad) == NULL)
5148 GST_PAD_TASK (pad) = task;
5149 GST_OBJECT_UNLOCK (pad);