2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Andy Wingo <wingo@pobox.com>
5 * 2006 Edward Hervey <bilboed@bilboed.com>
7 * gstghostpad.c: Proxy pads
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 * @short_description: Pseudo link pads
30 * GhostPads are useful when organizing pipelines with #GstBin like elements.
31 * The idea here is to create hierarchical element graphs. The bin element
32 * contains a sub-graph. Now one would like to treat the bin-element like any
33 * other #GstElement. This is where GhostPads come into play. A GhostPad acts as
34 * a proxy for another pad. Thus the bin can have sink and source ghost-pads
35 * that are associated with sink and source pads of the child elements.
37 * If the target pad is known at creation time, gst_ghost_pad_new() is the
38 * function to use to get a ghost-pad. Otherwise one can use gst_ghost_pad_new_no_target()
39 * to create the ghost-pad and use gst_ghost_pad_set_target() to establish the
40 * association later on.
42 * Note that GhostPads add overhead to the data processing of a pipeline.
44 * Last reviewed on 2005-11-18 (0.9.5)
47 #include "gst_private.h"
50 #include "gstghostpad.h"
53 #define GST_CAT_DEFAULT GST_CAT_PADS
55 #define GST_PROXY_PAD_CAST(obj) ((GstProxyPad *)obj)
56 #define GST_PROXY_PAD_PRIVATE(obj) (GST_PROXY_PAD_CAST (obj)->priv)
57 #define GST_PROXY_PAD_TARGET(pad) (GST_PROXY_PAD_PRIVATE (pad)->target)
58 #define GST_PROXY_PAD_INTERNAL(pad) (GST_PROXY_PAD_PRIVATE (pad)->internal)
59 #define GST_PROXY_PAD_RETARGET(pad) (GST_PROXY_PAD_PRIVATE (pad)->retarget)
60 #define GST_PROXY_GET_LOCK(pad) (GST_PROXY_PAD_PRIVATE (pad)->proxy_lock)
61 #define GST_PROXY_LOCK(pad) (g_mutex_lock (GST_PROXY_GET_LOCK (pad)))
62 #define GST_PROXY_UNLOCK(pad) (g_mutex_unlock (GST_PROXY_GET_LOCK (pad)))
64 struct _GstProxyPadPrivate
73 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
75 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
77 static void gst_proxy_pad_dispose (GObject * object);
78 static void gst_proxy_pad_finalize (GObject * object);
81 * gst_proxy_pad_query_type_default:
84 * Invoke the default query type handler of the proxy pad.
86 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
92 gst_proxy_pad_query_type_default (GstPad * pad)
95 const GstQueryType *res = NULL;
97 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
99 target = gst_proxy_pad_get_target (pad);
101 res = gst_pad_get_query_types (target);
102 gst_object_unref (target);
108 * gst_proxy_pad_event_default:
109 * @pad: a #GstPad to push the event to.
110 * @event: (transfer full): the #GstEvent to send to the pad.
112 * Invoke the default event of the proxy pad.
114 * Returns: TRUE if the event was handled.
119 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
121 gboolean res = FALSE;
124 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
125 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
128 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad)));
130 res = gst_pad_push_event (internal, event);
131 gst_object_unref (internal);
138 * gst_proxy_pad_query_default:
139 * @pad: a #GstPad to invoke the default query on.
140 * @query: (transfer none): the #GstQuery to perform.
142 * Invoke the default query function of the proxy pad.
144 * Returns: TRUE if the query could be performed.
149 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
151 gboolean res = FALSE;
154 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
155 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
157 target = gst_proxy_pad_get_target (pad);
159 res = gst_pad_query (target, query);
160 gst_object_unref (target);
167 * gst_proyx_pad_iterate_internal_links_default:
168 * @pad: the #GstPad to get the internal links of.
170 * Invoke the default iterate internal links function of the proxy pad.
172 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
173 * returned pad with gst_object_unref().
178 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
180 GstIterator *res = NULL;
183 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
186 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad)));
191 g_value_init (&v, GST_TYPE_PAD);
192 g_value_set_object (&v, internal);
193 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
195 gst_object_unref (internal);
202 * gst_proxy_pad_chain_default:
203 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
204 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
207 * Invoke the default chain function of the proxy pad.
209 * Returns: a #GstFlowReturn from the pad.
214 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
219 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
220 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
222 internal = GST_PROXY_PAD_INTERNAL (pad);
223 res = gst_pad_push (internal, buffer);
229 * gst_proxy_pad_chain_list_default:
230 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
231 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
234 * Invoke the default chain list function of the proxy pad.
236 * Returns: a #GstFlowReturn from the pad.
241 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
246 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
247 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
249 internal = GST_PROXY_PAD_INTERNAL (pad);
250 res = gst_pad_push_list (internal, list);
256 * gst_proxy_pad_get_range_default:
257 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
258 * @offset: The start offset of the buffer
259 * @size: The length of the buffer
260 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
261 * returns #GST_FLOW_ERROR if %NULL.
263 * Invoke the default getrange function of the proxy pad.
265 * Returns: a #GstFlowReturn from the pad.
270 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
276 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
277 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
279 internal = GST_PROXY_PAD_INTERNAL (pad);
280 res = gst_pad_pull_range (internal, offset, size, buffer);
286 * gst_proxy_pad_checkgetrange_default:
287 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
289 * Invoke the default checkgetrange function of the proxy pad.
291 * Returns: a #gboolean from the pad.
296 gst_proxy_pad_checkgetrange_default (GstPad * pad)
301 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
303 internal = GST_PROXY_PAD_INTERNAL (pad);
304 result = gst_pad_check_pull_range (internal);
310 * gst_proxy_pad_getcaps_default:
311 * @pad: a #GstPad to get the capabilities of.
312 * @filter: a #GstCaps filter.
314 * Invoke the default getcaps function of the proxy pad.
316 * Returns: (transfer full): the caps of the pad with incremented ref-count
321 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
325 GstPadTemplate *templ;
327 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
329 templ = GST_PAD_PAD_TEMPLATE (pad);
330 target = gst_proxy_pad_get_target (pad);
332 /* if we have a real target, proxy the call */
333 res = gst_pad_get_caps (target, filter);
335 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
336 GST_DEBUG_PAD_NAME (target), res);
338 gst_object_unref (target);
340 /* filter against the template */
344 filt = GST_PAD_TEMPLATE_CAPS (templ);
346 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
347 gst_caps_unref (res);
349 GST_DEBUG_OBJECT (pad,
350 "filtered against template gives %" GST_PTR_FORMAT, res);
354 /* else, if we have a template, use its caps. */
356 res = GST_PAD_TEMPLATE_CAPS (templ);
357 GST_DEBUG_OBJECT (pad,
358 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
360 res = gst_caps_ref (res);
363 GstCaps *intersection =
364 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
366 gst_caps_unref (res);
373 /* last resort, any caps */
374 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
375 res = gst_caps_new_any ();
383 * gst_proxy_pad_acceptcaps_default:
384 * @pad: a #GstPad to check
385 * @caps: a #GstCaps to check on the pad
387 * Invoke the default acceptcaps function of the proxy pad.
389 * Returns: TRUE if the pad can accept the caps.
394 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
399 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
400 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
402 target = gst_proxy_pad_get_target (pad);
404 res = gst_pad_accept_caps (target, caps);
405 gst_object_unref (target);
407 /* We don't have a target, we return TRUE and we assume that any future
408 * target will be able to deal with any configured caps. */
416 * gst_proxy_pad_fixatecaps_default:
417 * @pad: a #GstPad to fixate
418 * @caps: the #GstCaps to fixate
420 * Invoke the default fixatecaps function of the proxy pad.
425 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
429 g_return_if_fail (GST_IS_PROXY_PAD (pad));
430 g_return_if_fail (GST_IS_CAPS (caps));
432 target = gst_proxy_pad_get_target (pad);
434 gst_pad_fixate_caps (target, caps);
435 gst_object_unref (target);
440 * gst_proxy_pad_setcaps_default:
441 * @pad: a #GstPad to set the capabilities of.
442 * @caps: (transfer none): a #GstCaps to set.
444 * Invoke the default setcaps function of the proxy pad.
446 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
447 * or bad parameters were provided to this function.
452 gst_proxy_pad_setcaps_default (GstPad * pad, GstCaps * caps)
457 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
458 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
460 target = gst_proxy_pad_get_target (pad);
463 /* FIXME, not needed, the caps event will propagate over the pads
465 res = gst_pad_set_caps (target, caps);
468 gst_object_unref (target);
470 /* We don't have any target, but we shouldn't return FALSE since this
471 * would stop the actual push of a buffer (which might trigger a pad block
472 * or probe, or properly return GST_FLOW_NOT_LINKED.
480 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
485 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
487 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
488 goto wrong_direction;
490 GST_LOG_OBJECT (pad, "clearing target");
492 /* clear old target */
493 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
494 gst_object_unref (oldtarget);
496 /* set and ref new target if any */
498 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
500 GST_PROXY_PAD_TARGET (pad) = NULL;
507 GST_ERROR_OBJECT (pad,
508 "target pad doesn't have the same direction as ourself");
514 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
518 GST_PROXY_LOCK (pad);
519 result = gst_proxy_pad_set_target_unlocked (pad, target);
520 GST_PROXY_UNLOCK (pad);
526 gst_proxy_pad_get_target (GstPad * pad)
530 GST_PROXY_LOCK (pad);
531 target = GST_PROXY_PAD_TARGET (pad);
533 gst_object_ref (target);
534 GST_PROXY_UNLOCK (pad);
540 * gst_proxy_pad_get_internal:
541 * @pad: the #GstProxyPad
543 * Get the internal pad of @pad. Unref target pad after usage.
545 * The internal pad of a #GstGhostPad is the internally used
546 * pad of opposite direction, which is used to link to the target.
548 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
549 * Unref target pad after usage.
554 gst_proxy_pad_get_internal (GstProxyPad * pad)
558 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
560 GST_PROXY_LOCK (pad);
561 internal = GST_PROXY_PAD_INTERNAL (pad);
563 gst_object_ref (internal);
564 GST_PROXY_UNLOCK (pad);
566 return GST_PROXY_PAD_CAST (internal);
570 * gst_proxy_pad_unlink_default:
571 * @pad: a #GstPad to unlink
573 * Invoke the default unlink function of the proxy pad.
578 gst_proxy_pad_unlink_default (GstPad * pad)
582 /* don't do anything if this unlink resulted from retargeting the pad
583 * controlled by the ghostpad. We only want to invalidate the target pad when
584 * the element suddently unlinked with our internal pad. */
585 if (GST_PROXY_PAD_RETARGET (pad))
588 internal = GST_PROXY_PAD_INTERNAL (pad);
590 GST_DEBUG_OBJECT (pad, "pad is unlinked");
592 gst_proxy_pad_set_target (internal, NULL);
596 gst_proxy_pad_class_init (GstProxyPadClass * klass)
598 GObjectClass *gobject_class = (GObjectClass *) klass;
600 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
602 gobject_class->dispose = gst_proxy_pad_dispose;
603 gobject_class->finalize = gst_proxy_pad_finalize;
605 /* Register common function pointer descriptions */
606 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
607 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
608 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
609 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
610 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
611 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
612 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
613 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_setcaps_default);
614 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
615 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
616 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
617 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
618 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_checkgetrange_default);
622 gst_proxy_pad_dispose (GObject * object)
624 GstPad *pad = GST_PAD (object);
627 GST_PROXY_LOCK (pad);
628 /* remove and unref the target */
629 target_p = &GST_PROXY_PAD_TARGET (pad);
630 gst_object_replace ((GstObject **) target_p, NULL);
631 /* The internal is only cleared by GstGhostPad::dispose, since it is the
632 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
634 GST_PROXY_UNLOCK (pad);
636 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
640 gst_proxy_pad_finalize (GObject * object)
642 GstProxyPad *pad = GST_PROXY_PAD (object);
644 g_mutex_free (GST_PROXY_GET_LOCK (pad));
645 GST_PROXY_GET_LOCK (pad) = NULL;
647 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
651 gst_proxy_pad_init (GstProxyPad * ppad)
653 GstPad *pad = (GstPad *) ppad;
655 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
656 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
657 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
659 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
660 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
661 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
662 gst_pad_set_iterate_internal_links_function (pad,
663 gst_proxy_pad_iterate_internal_links_default);
665 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
666 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
667 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
668 gst_pad_set_setcaps_function (pad, gst_proxy_pad_setcaps_default);
669 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
673 /***********************************************************************
674 * Ghost pads, implemented as a pair of proxy pads (sort of)
678 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
680 struct _GstGhostPadPrivate
682 /* with PROXY_LOCK */
683 gboolean constructed;
686 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
688 static void gst_ghost_pad_dispose (GObject * object);
691 * gst_ghost_pad_internal_activate_push_default:
692 * @pad: the #GstPad to activate or deactivate.
693 * @active: whether the pad should be active or not.
695 * Invoke the default activate push function of a proxy pad that is
696 * owned by a ghost pad.
698 * Returns: %TRUE if the operation was successful.
703 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
708 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
710 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
711 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
713 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
714 * will be activated later (or already in case of a ghost sinkpad). */
715 other = GST_PROXY_PAD_INTERNAL (pad);
716 ret = gst_pad_activate_push (other, active);
722 * gst_ghost_pad_internal_activate_pull_default:
723 * @pad: the #GstPad to activate or deactivate.
724 * @active: whether the pad should be active or not.
726 * Invoke the default activate pull function of a proxy pad that is
727 * owned by a ghost pad.
729 * Returns: %TRUE if the operation was successful.
734 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
739 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
741 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
742 GST_DEBUG_PAD_NAME (pad));
744 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
745 /* we are activated in pull mode by our peer element, which is a sinkpad
746 * that wants to operate in pull mode. This activation has to propagate
747 * upstream throught the pipeline. We call the internal activation function,
748 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
749 * further upstream */
750 GST_LOG_OBJECT (pad, "pad is src, activate internal");
751 other = GST_PROXY_PAD_INTERNAL (pad);
752 ret = gst_pad_activate_pull (other, active);
753 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
754 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
755 * since we hold a pointer to the upstream peer. */
756 GST_LOG_OBJECT (pad, "activating peer");
757 ret = gst_pad_activate_pull (other, active);
758 gst_object_unref (other);
760 /* this is failure, we can't activate pull if there is no peer */
761 GST_LOG_OBJECT (pad, "not src and no peer, failing");
769 * gst_ghost_pad_activate_push_default:
770 * @pad: the #GstPad to activate or deactivate.
771 * @active: whether the pad should be active or not.
773 * Invoke the default activate push function of a ghost pad.
775 * Returns: %TRUE if the operation was successful.
780 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
785 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
787 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
788 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
790 /* just activate the internal pad */
791 other = GST_PROXY_PAD_INTERNAL (pad);
792 ret = gst_pad_activate_push (other, active);
798 * gst_ghost_pad_activate_pull_default:
799 * @pad: the #GstPad to activate or deactivate.
800 * @active: whether the pad should be active or not.
802 * Invoke the default activate pull function of a ghost pad.
804 * Returns: %TRUE if the operation was successful.
809 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
814 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
816 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
817 GST_DEBUG_PAD_NAME (pad));
819 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
820 /* the ghostpad is SRC and activated in pull mode by its peer, call the
821 * activation function of the internal pad to propagate the activation
823 GST_LOG_OBJECT (pad, "pad is src, activate internal");
824 other = GST_PROXY_PAD_INTERNAL (pad);
825 ret = gst_pad_activate_pull (other, active);
826 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
827 /* We are SINK and activated by the internal pad, propagate activation
828 * upstream because we hold a ref to the upstream peer */
829 GST_LOG_OBJECT (pad, "activating peer");
830 ret = gst_pad_activate_pull (other, active);
831 gst_object_unref (other);
833 /* no peer, we fail */
834 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
842 * gst_ghost_pad_link_default:
843 * @pad: the #GstPad to link.
844 * @peer: the #GstPad peer
846 * Invoke the default link function of a ghost pad.
848 * Returns: #GstPadLinkReturn of the operation
853 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
855 GstPadLinkReturn ret;
858 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
859 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
861 GST_DEBUG_OBJECT (pad, "linking ghostpad");
863 internal = GST_PROXY_PAD_INTERNAL (pad);
864 if (!gst_proxy_pad_set_target (internal, peer))
867 ret = GST_PAD_LINK_OK;
868 /* if we are a source pad, we should call the peer link function
869 * if the peer has one, see design docs. */
870 if (GST_PAD_IS_SRC (pad)) {
871 if (GST_PAD_LINKFUNC (peer)) {
872 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
873 if (ret != GST_PAD_LINK_OK)
882 GST_DEBUG_OBJECT (pad, "setting target failed");
883 return GST_PAD_LINK_REFUSED;
887 GST_DEBUG_OBJECT (pad, "linking failed");
888 /* clear target again */
889 gst_proxy_pad_set_target (internal, NULL);
895 * gst_ghost_pad_unlink_default:
896 * @pad: the #GstPad to link.
898 * Invoke the default unlink function of a ghost pad.
903 gst_ghost_pad_unlink_default (GstPad * pad)
907 g_return_if_fail (GST_IS_GHOST_PAD (pad));
909 internal = GST_PROXY_PAD_INTERNAL (pad);
911 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
913 /* The target of the internal pad is no longer valid */
914 gst_proxy_pad_set_target (internal, NULL);
918 * gst_ghost_pad_setcaps_default:
919 * @pad: the #GstPad to link.
920 * @caps: (transfer none): the #GstCaps to set
922 * Invoke the default setcaps function of a ghost pad.
924 * Returns: %TRUE if the operation was successful
929 gst_ghost_pad_setcaps_default (GstPad * pad, GstCaps * caps)
931 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
932 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
934 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
937 return gst_proxy_pad_setcaps_default (pad, caps);
941 gst_ghost_pad_class_init (GstGhostPadClass * klass)
943 GObjectClass *gobject_class = (GObjectClass *) klass;
945 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
947 gobject_class->dispose = gst_ghost_pad_dispose;
949 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_setcaps_default);
950 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
951 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
952 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
956 gst_ghost_pad_init (GstGhostPad * pad)
958 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
959 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
961 gst_pad_set_setcaps_function (GST_PAD_CAST (pad),
962 gst_ghost_pad_setcaps_default);
963 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
964 gst_ghost_pad_activate_pull_default);
965 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
966 gst_ghost_pad_activate_push_default);
970 gst_ghost_pad_dispose (GObject * object)
976 pad = GST_PAD (object);
978 GST_DEBUG_OBJECT (pad, "dispose");
980 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
982 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
983 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
984 peer = gst_pad_get_peer (pad);
986 if (GST_PAD_IS_SRC (pad))
987 gst_pad_unlink (pad, peer);
989 gst_pad_unlink (peer, pad);
991 gst_object_unref (peer);
994 GST_PROXY_LOCK (pad);
995 internal = GST_PROXY_PAD_INTERNAL (pad);
997 gst_pad_set_activatepull_function (internal, NULL);
998 gst_pad_set_activatepush_function (internal, NULL);
1000 /* disposes of the internal pad, since the ghostpad is the only possible object
1001 * that has a refcount on the internal pad. */
1002 gst_object_unparent (GST_OBJECT_CAST (internal));
1003 GST_PROXY_PAD_INTERNAL (pad) = NULL;
1005 GST_PROXY_UNLOCK (pad);
1007 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
1011 * gst_ghost_pad_construct:
1012 * @gpad: the newly allocated ghost pad
1014 * Finish initialization of a newly allocated ghost pad.
1016 * This function is most useful in language bindings and when subclassing
1017 * #GstGhostPad; plugin and application developers normally will not call this
1018 * function. Call this function directly after a call to g_object_new
1019 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
1021 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
1026 gst_ghost_pad_construct (GstGhostPad * gpad)
1028 GstPadDirection dir, otherdir;
1029 GstPadTemplate *templ;
1030 GstPad *pad, *internal;
1032 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1033 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
1036 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
1038 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
1040 pad = GST_PAD (gpad);
1042 /* Set directional padfunctions for ghostpad */
1043 if (dir == GST_PAD_SINK) {
1044 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
1045 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
1047 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
1048 gst_pad_set_checkgetrange_function (pad,
1049 gst_proxy_pad_checkgetrange_default);
1052 /* link/unlink functions */
1053 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
1054 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
1056 /* INTERNAL PAD, it always exists and is child of the ghostpad */
1057 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
1060 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
1061 "direction", otherdir, "template", templ, NULL);
1062 /* release ref obtained via g_object_get */
1063 gst_object_unref (templ);
1066 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
1067 "direction", otherdir, NULL);
1069 GST_PAD_UNSET_FLUSHING (internal);
1071 /* Set directional padfunctions for internal pad */
1072 if (dir == GST_PAD_SRC) {
1073 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
1074 gst_pad_set_chain_list_function (internal,
1075 gst_proxy_pad_chain_list_default);
1077 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
1078 gst_pad_set_checkgetrange_function (internal,
1079 gst_proxy_pad_checkgetrange_default);
1082 GST_PROXY_LOCK (pad);
1084 /* now make the ghostpad a parent of the internal pad */
1085 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
1086 GST_OBJECT_CAST (pad)))
1089 /* The ghostpad is the parent of the internal pad and is the only object that
1090 * can have a refcount on the internal pad.
1091 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
1093 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
1094 * its refcount on the internal pad in the dispose method by un-parenting it.
1095 * This is why we don't take extra refcounts in the assignments below
1097 GST_PROXY_PAD_INTERNAL (pad) = internal;
1098 GST_PROXY_PAD_INTERNAL (internal) = pad;
1100 /* special activation functions for the internal pad */
1101 gst_pad_set_activatepull_function (internal,
1102 gst_ghost_pad_internal_activate_pull_default);
1103 gst_pad_set_activatepush_function (internal,
1104 gst_ghost_pad_internal_activate_push_default);
1106 GST_PROXY_UNLOCK (pad);
1108 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
1114 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
1115 GST_DEBUG_PAD_NAME (internal));
1116 g_critical ("Could not set internal pad %s:%s",
1117 GST_DEBUG_PAD_NAME (internal));
1118 GST_PROXY_UNLOCK (pad);
1119 gst_object_unref (internal);
1125 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
1126 GstPadTemplate * templ)
1130 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1132 /* OBJECT CREATION */
1134 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1135 "direction", dir, "template", templ, NULL);
1137 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1138 "direction", dir, NULL);
1141 if (!gst_ghost_pad_construct (ret))
1142 goto construct_failed;
1144 return GST_PAD_CAST (ret);
1147 /* already logged */
1148 gst_object_unref (ret);
1153 * gst_ghost_pad_new_no_target:
1154 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1155 * @dir: the direction of the ghostpad
1157 * Create a new ghostpad without a target with the given direction.
1158 * A target can be set on the ghostpad later with the
1159 * gst_ghost_pad_set_target() function.
1161 * The created ghostpad will not have a padtemplate.
1163 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1166 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1170 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1172 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1174 ret = gst_ghost_pad_new_full (name, dir, NULL);
1180 * gst_ghost_pad_new:
1181 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1182 * @target: (transfer none): the pad to ghost.
1184 * Create a new ghostpad with @target as the target. The direction will be taken
1185 * from the target pad. @target must be unlinked.
1187 * Will ref the target.
1189 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1192 gst_ghost_pad_new (const gchar * name, GstPad * target)
1196 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1197 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1199 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1200 GST_DEBUG_PAD_NAME (target));
1202 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1203 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1204 goto set_target_failed;
1211 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1212 GST_DEBUG_PAD_NAME (target));
1213 gst_object_unref (ret);
1219 * gst_ghost_pad_new_from_template:
1220 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1221 * @target: (transfer none): the pad to ghost.
1222 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1224 * Create a new ghostpad with @target as the target. The direction will be taken
1225 * from the target pad. The template used on the ghostpad will be @template.
1227 * Will ref the target.
1229 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1235 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1236 GstPadTemplate * templ)
1240 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1241 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1242 g_return_val_if_fail (templ != NULL, NULL);
1243 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1244 GST_PAD_DIRECTION (target), NULL);
1246 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1247 GST_DEBUG_PAD_NAME (target), templ);
1249 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1250 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1251 goto set_target_failed;
1258 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1259 GST_DEBUG_PAD_NAME (target));
1260 gst_object_unref (ret);
1266 * gst_ghost_pad_new_no_target_from_template:
1267 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1268 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1270 * Create a new ghostpad based on @templ, without setting a target. The
1271 * direction will be taken from the @templ.
1273 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1278 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1279 GstPadTemplate * templ)
1283 g_return_val_if_fail (templ != NULL, NULL);
1286 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1292 * gst_ghost_pad_get_target:
1293 * @gpad: the #GstGhostPad
1295 * Get the target pad of @gpad. Unref target pad after usage.
1297 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1298 * has no target set. Unref target pad after usage.
1301 gst_ghost_pad_get_target (GstGhostPad * gpad)
1305 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1307 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1309 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1315 * gst_ghost_pad_set_target:
1316 * @gpad: the #GstGhostPad
1317 * @newtarget: (transfer none) (allow-none): the new pad target
1319 * Set the new target of the ghostpad @gpad. Any existing target
1320 * is unlinked and links to the new target are established. if @newtarget is
1321 * NULL the target will be cleared.
1323 * Returns: (transfer full): TRUE if the new target could be set. This function
1324 * can return FALSE when the internal pads could not be linked.
1327 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1332 GstPadLinkReturn lret;
1334 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1335 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1336 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1338 /* no need for locking, the internal pad's lifecycle is directly linked to the
1340 internal = GST_PROXY_PAD_INTERNAL (gpad);
1343 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1345 GST_DEBUG_OBJECT (gpad, "clearing target");
1347 /* clear old target */
1348 GST_PROXY_LOCK (gpad);
1349 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1351 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1353 /* unlink internal pad */
1354 if (GST_PAD_IS_SRC (internal))
1355 gst_pad_unlink (internal, oldtarget);
1357 gst_pad_unlink (oldtarget, internal);
1359 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1362 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1363 GST_PROXY_UNLOCK (gpad);
1365 if (result && newtarget) {
1366 /* and link to internal pad without any checks */
1367 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1369 if (GST_PAD_IS_SRC (internal))
1371 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1374 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1376 if (lret != GST_PAD_LINK_OK)
1385 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1387 /* and unset target again */
1388 GST_PROXY_LOCK (gpad);
1389 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1390 GST_PROXY_UNLOCK (gpad);