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 if (!(target = gst_proxy_pad_get_target (pad)))
102 res = gst_pad_get_query_types (target);
103 gst_object_unref (target);
110 GST_DEBUG_OBJECT (pad, "no target pad");
116 * gst_proxy_pad_event_default:
117 * @pad: a #GstPad to push the event to.
118 * @event: (transfer full): the #GstEvent to send to the pad.
120 * Invoke the default event of the proxy pad.
122 * Returns: TRUE if the event was handled.
127 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
132 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
133 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
135 internal = GST_PROXY_PAD_INTERNAL (pad);
136 res = gst_pad_push_event (internal, event);
142 * gst_proxy_pad_query_default:
143 * @pad: a #GstPad to invoke the default query on.
144 * @query: (transfer none): the #GstQuery to perform.
146 * Invoke the default query function of the proxy pad.
148 * Returns: TRUE if the query could be performed.
153 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
158 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
159 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
161 if (!(target = gst_proxy_pad_get_target (pad)))
164 res = gst_pad_query (target, query);
165 gst_object_unref (target);
172 GST_DEBUG_OBJECT (pad, "no target pad");
178 * gst_proyx_pad_iterate_internal_links_default:
179 * @pad: the #GstPad to get the internal links of.
181 * Invoke the default iterate internal links function of the proxy pad.
183 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
184 * returned pad with gst_object_unref().
189 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
191 GstIterator *res = NULL;
195 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
197 internal = GST_PROXY_PAD_INTERNAL (pad);
198 g_value_init (&v, GST_TYPE_PAD);
199 g_value_set_object (&v, internal);
200 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
207 * gst_proxy_pad_chain_default:
208 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
209 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
212 * Invoke the default chain function of the proxy pad.
214 * Returns: a #GstFlowReturn from the pad.
219 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
224 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
225 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
227 internal = GST_PROXY_PAD_INTERNAL (pad);
228 res = gst_pad_push (internal, buffer);
234 * gst_proxy_pad_chain_list_default:
235 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
236 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
239 * Invoke the default chain list function of the proxy pad.
241 * Returns: a #GstFlowReturn from the pad.
246 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
251 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
252 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
254 internal = GST_PROXY_PAD_INTERNAL (pad);
255 res = gst_pad_push_list (internal, list);
261 * gst_proxy_pad_get_range_default:
262 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
263 * @offset: The start offset of the buffer
264 * @size: The length of the buffer
265 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
266 * returns #GST_FLOW_ERROR if %NULL.
268 * Invoke the default getrange function of the proxy pad.
270 * Returns: a #GstFlowReturn from the pad.
275 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
281 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
282 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
284 internal = GST_PROXY_PAD_INTERNAL (pad);
285 res = gst_pad_pull_range (internal, offset, size, buffer);
291 * gst_proxy_pad_getcaps_default:
292 * @pad: a #GstPad to get the capabilities of.
293 * @filter: a #GstCaps filter.
295 * Invoke the default getcaps function of the proxy pad.
297 * Returns: (transfer full): the caps of the pad with incremented ref-count
302 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
306 GstPadTemplate *templ;
308 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
310 templ = GST_PAD_PAD_TEMPLATE (pad);
311 target = gst_proxy_pad_get_target (pad);
313 /* if we have a real target, proxy the call */
314 res = gst_pad_get_caps (target, filter);
316 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
317 GST_DEBUG_PAD_NAME (target), res);
319 gst_object_unref (target);
321 /* filter against the template */
325 filt = GST_PAD_TEMPLATE_CAPS (templ);
327 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
328 gst_caps_unref (res);
330 GST_DEBUG_OBJECT (pad,
331 "filtered against template gives %" GST_PTR_FORMAT, res);
335 /* else, if we have a template, use its caps. */
337 res = GST_PAD_TEMPLATE_CAPS (templ);
338 GST_DEBUG_OBJECT (pad,
339 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
341 res = gst_caps_ref (res);
344 GstCaps *intersection =
345 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
347 gst_caps_unref (res);
354 /* If there's a filter, return that */
355 if (filter != NULL) {
356 res = gst_caps_ref (filter);
360 /* last resort, any caps */
361 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
362 res = gst_caps_new_any ();
370 * gst_proxy_pad_acceptcaps_default:
371 * @pad: a #GstPad to check
372 * @caps: a #GstCaps to check on the pad
374 * Invoke the default acceptcaps function of the proxy pad.
376 * Returns: TRUE if the pad can accept the caps.
381 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
386 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
387 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
389 target = gst_proxy_pad_get_target (pad);
391 res = gst_pad_accept_caps (target, caps);
392 gst_object_unref (target);
394 GST_DEBUG_OBJECT (pad, "no target");
395 /* We don't have a target, we return TRUE and we assume that any future
396 * target will be able to deal with any configured caps. */
404 * gst_proxy_pad_fixatecaps_default:
405 * @pad: a #GstPad to fixate
406 * @caps: the #GstCaps to fixate
408 * Invoke the default fixatecaps function of the proxy pad.
413 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
417 g_return_if_fail (GST_IS_PROXY_PAD (pad));
418 g_return_if_fail (GST_IS_CAPS (caps));
420 if (!(target = gst_proxy_pad_get_target (pad)))
423 gst_pad_fixate_caps (target, caps);
424 gst_object_unref (target);
431 GST_DEBUG_OBJECT (pad, "no target");
437 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
442 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
444 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
445 goto wrong_direction;
447 GST_LOG_OBJECT (pad, "clearing target");
449 /* clear old target */
450 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
451 gst_object_unref (oldtarget);
453 /* set and ref new target if any */
455 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
457 GST_PROXY_PAD_TARGET (pad) = NULL;
464 GST_ERROR_OBJECT (pad,
465 "target pad doesn't have the same direction as ourself");
471 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
475 GST_PROXY_LOCK (pad);
476 result = gst_proxy_pad_set_target_unlocked (pad, target);
477 GST_PROXY_UNLOCK (pad);
483 gst_proxy_pad_get_target (GstPad * pad)
487 GST_PROXY_LOCK (pad);
488 target = GST_PROXY_PAD_TARGET (pad);
490 gst_object_ref (target);
491 GST_PROXY_UNLOCK (pad);
497 * gst_proxy_pad_get_internal:
498 * @pad: the #GstProxyPad
500 * Get the internal pad of @pad. Unref target pad after usage.
502 * The internal pad of a #GstGhostPad is the internally used
503 * pad of opposite direction, which is used to link to the target.
505 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
506 * Unref target pad after usage.
511 gst_proxy_pad_get_internal (GstProxyPad * pad)
515 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
517 GST_PROXY_LOCK (pad);
518 internal = GST_PROXY_PAD_INTERNAL (pad);
520 gst_object_ref (internal);
521 GST_PROXY_UNLOCK (pad);
523 return GST_PROXY_PAD_CAST (internal);
527 * gst_proxy_pad_unlink_default:
528 * @pad: a #GstPad to unlink
530 * Invoke the default unlink function of the proxy pad.
535 gst_proxy_pad_unlink_default (GstPad * pad)
539 /* don't do anything if this unlink resulted from retargeting the pad
540 * controlled by the ghostpad. We only want to invalidate the target pad when
541 * the element suddenly unlinked with our internal pad. */
542 if (GST_PROXY_PAD_RETARGET (pad))
545 internal = GST_PROXY_PAD_INTERNAL (pad);
547 GST_DEBUG_OBJECT (pad, "pad is unlinked");
549 gst_proxy_pad_set_target (internal, NULL);
553 gst_proxy_pad_class_init (GstProxyPadClass * klass)
555 GObjectClass *gobject_class = (GObjectClass *) klass;
557 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
559 gobject_class->dispose = gst_proxy_pad_dispose;
560 gobject_class->finalize = gst_proxy_pad_finalize;
562 /* Register common function pointer descriptions */
563 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
564 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
565 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
566 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
567 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
568 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
569 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
570 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
571 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
572 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
573 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
577 gst_proxy_pad_dispose (GObject * object)
579 GstPad *pad = GST_PAD (object);
582 GST_PROXY_LOCK (pad);
583 /* remove and unref the target */
584 target_p = &GST_PROXY_PAD_TARGET (pad);
585 gst_object_replace ((GstObject **) target_p, NULL);
586 /* The internal is only cleared by GstGhostPad::dispose, since it is the
587 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
589 GST_PROXY_UNLOCK (pad);
591 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
595 gst_proxy_pad_finalize (GObject * object)
597 GstProxyPad *pad = GST_PROXY_PAD (object);
599 g_mutex_free (GST_PROXY_GET_LOCK (pad));
600 GST_PROXY_GET_LOCK (pad) = NULL;
602 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
606 gst_proxy_pad_init (GstProxyPad * ppad)
608 GstPad *pad = (GstPad *) ppad;
610 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
611 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
612 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
614 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
615 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
616 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
617 gst_pad_set_iterate_internal_links_function (pad,
618 gst_proxy_pad_iterate_internal_links_default);
620 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
621 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
622 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
623 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
627 /***********************************************************************
628 * Ghost pads, implemented as a pair of proxy pads (sort of)
632 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
634 struct _GstGhostPadPrivate
636 /* with PROXY_LOCK */
637 gboolean constructed;
640 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
642 static void gst_ghost_pad_dispose (GObject * object);
645 * gst_ghost_pad_internal_activate_push_default:
646 * @pad: the #GstPad to activate or deactivate.
647 * @active: whether the pad should be active or not.
649 * Invoke the default activate push function of a proxy pad that is
650 * owned by a ghost pad.
652 * Returns: %TRUE if the operation was successful.
657 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
662 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
664 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
665 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
667 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
668 * will be activated later (or already in case of a ghost sinkpad). */
669 other = GST_PROXY_PAD_INTERNAL (pad);
670 ret = gst_pad_activate_push (other, active);
676 * gst_ghost_pad_internal_activate_pull_default:
677 * @pad: the #GstPad to activate or deactivate.
678 * @active: whether the pad should be active or not.
680 * Invoke the default activate pull function of a proxy pad that is
681 * owned by a ghost pad.
683 * Returns: %TRUE if the operation was successful.
688 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
693 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
695 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
696 GST_DEBUG_PAD_NAME (pad));
698 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
699 /* we are activated in pull mode by our peer element, which is a sinkpad
700 * that wants to operate in pull mode. This activation has to propagate
701 * upstream through the pipeline. We call the internal activation function,
702 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
703 * further upstream */
704 GST_LOG_OBJECT (pad, "pad is src, activate internal");
705 other = GST_PROXY_PAD_INTERNAL (pad);
706 ret = gst_pad_activate_pull (other, active);
707 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
708 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
709 * since we hold a pointer to the upstream peer. */
710 GST_LOG_OBJECT (pad, "activating peer");
711 ret = gst_pad_activate_pull (other, active);
712 gst_object_unref (other);
714 /* this is failure, we can't activate pull if there is no peer */
715 GST_LOG_OBJECT (pad, "not src and no peer, failing");
723 * gst_ghost_pad_activate_push_default:
724 * @pad: the #GstPad to activate or deactivate.
725 * @active: whether the pad should be active or not.
727 * Invoke the default activate push function of a ghost pad.
729 * Returns: %TRUE if the operation was successful.
734 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
739 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
741 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
742 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
744 /* just activate the internal pad */
745 other = GST_PROXY_PAD_INTERNAL (pad);
746 ret = gst_pad_activate_push (other, active);
752 * gst_ghost_pad_activate_pull_default:
753 * @pad: the #GstPad to activate or deactivate.
754 * @active: whether the pad should be active or not.
756 * Invoke the default activate pull function of a ghost pad.
758 * Returns: %TRUE if the operation was successful.
763 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
768 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
770 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
771 GST_DEBUG_PAD_NAME (pad));
773 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
774 /* the ghostpad is SRC and activated in pull mode by its peer, call the
775 * activation function of the internal pad to propagate the activation
777 GST_LOG_OBJECT (pad, "pad is src, activate internal");
778 other = GST_PROXY_PAD_INTERNAL (pad);
779 ret = gst_pad_activate_pull (other, active);
780 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
781 /* We are SINK and activated by the internal pad, propagate activation
782 * upstream because we hold a ref to the upstream peer */
783 GST_LOG_OBJECT (pad, "activating peer");
784 ret = gst_pad_activate_pull (other, active);
785 gst_object_unref (other);
787 /* no peer, we fail */
788 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
796 * gst_ghost_pad_link_default:
797 * @pad: the #GstPad to link.
798 * @peer: the #GstPad peer
800 * Invoke the default link function of a ghost pad.
802 * Returns: #GstPadLinkReturn of the operation
807 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
809 GstPadLinkReturn ret;
812 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
813 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
815 GST_DEBUG_OBJECT (pad, "linking ghostpad");
817 internal = GST_PROXY_PAD_INTERNAL (pad);
818 if (!gst_proxy_pad_set_target (internal, peer))
821 ret = GST_PAD_LINK_OK;
822 /* if we are a source pad, we should call the peer link function
823 * if the peer has one, see design docs. */
824 if (GST_PAD_IS_SRC (pad)) {
825 if (GST_PAD_LINKFUNC (peer)) {
826 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
827 if (ret != GST_PAD_LINK_OK)
836 GST_DEBUG_OBJECT (pad, "setting target failed");
837 return GST_PAD_LINK_REFUSED;
841 GST_DEBUG_OBJECT (pad, "linking failed");
842 /* clear target again */
843 gst_proxy_pad_set_target (internal, NULL);
849 * gst_ghost_pad_unlink_default:
850 * @pad: the #GstPad to link.
852 * Invoke the default unlink function of a ghost pad.
857 gst_ghost_pad_unlink_default (GstPad * pad)
861 g_return_if_fail (GST_IS_GHOST_PAD (pad));
863 internal = GST_PROXY_PAD_INTERNAL (pad);
865 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
867 /* The target of the internal pad is no longer valid */
868 gst_proxy_pad_set_target (internal, NULL);
872 gst_ghost_pad_class_init (GstGhostPadClass * klass)
874 GObjectClass *gobject_class = (GObjectClass *) klass;
876 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
878 gobject_class->dispose = gst_ghost_pad_dispose;
880 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
881 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
882 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
886 gst_ghost_pad_init (GstGhostPad * pad)
888 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
889 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
891 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
892 gst_ghost_pad_activate_pull_default);
893 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
894 gst_ghost_pad_activate_push_default);
898 gst_ghost_pad_dispose (GObject * object)
904 pad = GST_PAD (object);
906 GST_DEBUG_OBJECT (pad, "dispose");
908 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
910 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
911 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
912 peer = gst_pad_get_peer (pad);
914 if (GST_PAD_IS_SRC (pad))
915 gst_pad_unlink (pad, peer);
917 gst_pad_unlink (peer, pad);
919 gst_object_unref (peer);
922 GST_PROXY_LOCK (pad);
923 internal = GST_PROXY_PAD_INTERNAL (pad);
925 gst_pad_set_activatepull_function (internal, NULL);
926 gst_pad_set_activatepush_function (internal, NULL);
928 /* disposes of the internal pad, since the ghostpad is the only possible object
929 * that has a refcount on the internal pad. */
930 gst_object_unparent (GST_OBJECT_CAST (internal));
931 GST_PROXY_PAD_INTERNAL (pad) = NULL;
933 GST_PROXY_UNLOCK (pad);
935 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
939 * gst_ghost_pad_construct:
940 * @gpad: the newly allocated ghost pad
942 * Finish initialization of a newly allocated ghost pad.
944 * This function is most useful in language bindings and when subclassing
945 * #GstGhostPad; plugin and application developers normally will not call this
946 * function. Call this function directly after a call to g_object_new
947 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
949 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
954 gst_ghost_pad_construct (GstGhostPad * gpad)
956 GstPadDirection dir, otherdir;
957 GstPadTemplate *templ;
958 GstPad *pad, *internal;
960 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
961 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
964 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
966 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
968 pad = GST_PAD (gpad);
970 /* Set directional padfunctions for ghostpad */
971 if (dir == GST_PAD_SINK) {
972 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
973 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
975 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
978 /* link/unlink functions */
979 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
980 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
982 /* INTERNAL PAD, it always exists and is child of the ghostpad */
983 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
986 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
987 "direction", otherdir, "template", templ, NULL);
988 /* release ref obtained via g_object_get */
989 gst_object_unref (templ);
992 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
993 "direction", otherdir, NULL);
995 GST_PAD_UNSET_FLUSHING (internal);
997 /* Set directional padfunctions for internal pad */
998 if (dir == GST_PAD_SRC) {
999 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
1000 gst_pad_set_chain_list_function (internal,
1001 gst_proxy_pad_chain_list_default);
1003 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
1006 GST_PROXY_LOCK (pad);
1008 /* now make the ghostpad a parent of the internal pad */
1009 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
1010 GST_OBJECT_CAST (pad)))
1013 /* The ghostpad is the parent of the internal pad and is the only object that
1014 * can have a refcount on the internal pad.
1015 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
1017 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
1018 * its refcount on the internal pad in the dispose method by un-parenting it.
1019 * This is why we don't take extra refcounts in the assignments below
1021 GST_PROXY_PAD_INTERNAL (pad) = internal;
1022 GST_PROXY_PAD_INTERNAL (internal) = pad;
1024 /* special activation functions for the internal pad */
1025 gst_pad_set_activatepull_function (internal,
1026 gst_ghost_pad_internal_activate_pull_default);
1027 gst_pad_set_activatepush_function (internal,
1028 gst_ghost_pad_internal_activate_push_default);
1030 GST_PROXY_UNLOCK (pad);
1032 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
1038 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
1039 GST_DEBUG_PAD_NAME (internal));
1040 g_critical ("Could not set internal pad %s:%s",
1041 GST_DEBUG_PAD_NAME (internal));
1042 GST_PROXY_UNLOCK (pad);
1043 gst_object_unref (internal);
1049 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
1050 GstPadTemplate * templ)
1054 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1056 /* OBJECT CREATION */
1058 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1059 "direction", dir, "template", templ, NULL);
1061 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1062 "direction", dir, NULL);
1065 if (!gst_ghost_pad_construct (ret))
1066 goto construct_failed;
1068 return GST_PAD_CAST (ret);
1071 /* already logged */
1072 gst_object_unref (ret);
1077 * gst_ghost_pad_new_no_target:
1078 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1079 * @dir: the direction of the ghostpad
1081 * Create a new ghostpad without a target with the given direction.
1082 * A target can be set on the ghostpad later with the
1083 * gst_ghost_pad_set_target() function.
1085 * The created ghostpad will not have a padtemplate.
1087 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1090 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1094 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1096 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1098 ret = gst_ghost_pad_new_full (name, dir, NULL);
1104 * gst_ghost_pad_new:
1105 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1106 * @target: (transfer none): the pad to ghost.
1108 * Create a new ghostpad with @target as the target. The direction will be taken
1109 * from the target pad. @target must be unlinked.
1111 * Will ref the target.
1113 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1116 gst_ghost_pad_new (const gchar * name, GstPad * target)
1120 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1121 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1123 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1124 GST_DEBUG_PAD_NAME (target));
1126 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1127 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1128 goto set_target_failed;
1135 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1136 GST_DEBUG_PAD_NAME (target));
1137 gst_object_unref (ret);
1143 * gst_ghost_pad_new_from_template:
1144 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1145 * @target: (transfer none): the pad to ghost.
1146 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1148 * Create a new ghostpad with @target as the target. The direction will be taken
1149 * from the target pad. The template used on the ghostpad will be @template.
1151 * Will ref the target.
1153 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1159 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1160 GstPadTemplate * templ)
1164 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1165 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1166 g_return_val_if_fail (templ != NULL, NULL);
1167 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1168 GST_PAD_DIRECTION (target), NULL);
1170 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1171 GST_DEBUG_PAD_NAME (target), templ);
1173 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1174 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1175 goto set_target_failed;
1182 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1183 GST_DEBUG_PAD_NAME (target));
1184 gst_object_unref (ret);
1190 * gst_ghost_pad_new_no_target_from_template:
1191 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1192 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1194 * Create a new ghostpad based on @templ, without setting a target. The
1195 * direction will be taken from the @templ.
1197 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1202 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1203 GstPadTemplate * templ)
1207 g_return_val_if_fail (templ != NULL, NULL);
1210 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1216 * gst_ghost_pad_get_target:
1217 * @gpad: the #GstGhostPad
1219 * Get the target pad of @gpad. Unref target pad after usage.
1221 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1222 * has no target set. Unref target pad after usage.
1225 gst_ghost_pad_get_target (GstGhostPad * gpad)
1229 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1231 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1233 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1239 * gst_ghost_pad_set_target:
1240 * @gpad: the #GstGhostPad
1241 * @newtarget: (transfer none) (allow-none): the new pad target
1243 * Set the new target of the ghostpad @gpad. Any existing target
1244 * is unlinked and links to the new target are established. if @newtarget is
1245 * NULL the target will be cleared.
1247 * Returns: (transfer full): TRUE if the new target could be set. This function
1248 * can return FALSE when the internal pads could not be linked.
1251 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1256 GstPadLinkReturn lret;
1258 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1259 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1260 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1262 /* no need for locking, the internal pad's lifecycle is directly linked to the
1264 internal = GST_PROXY_PAD_INTERNAL (gpad);
1267 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1269 GST_DEBUG_OBJECT (gpad, "clearing target");
1271 /* clear old target */
1272 GST_PROXY_LOCK (gpad);
1273 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1275 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1277 /* unlink internal pad */
1278 if (GST_PAD_IS_SRC (internal))
1279 gst_pad_unlink (internal, oldtarget);
1281 gst_pad_unlink (oldtarget, internal);
1283 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1286 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1287 GST_PROXY_UNLOCK (gpad);
1289 if (result && newtarget) {
1290 /* and link to internal pad without any checks */
1291 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1293 if (GST_PAD_IS_SRC (internal))
1295 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1298 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1300 if (lret != GST_PAD_LINK_OK)
1309 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1311 /* and unset target again */
1312 GST_PROXY_LOCK (gpad);
1313 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1314 GST_PROXY_UNLOCK (gpad);