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);
202 gst_object_unref (internal);
208 * gst_proxy_pad_chain_default:
209 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
210 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
213 * Invoke the default chain function of the proxy pad.
215 * Returns: a #GstFlowReturn from the pad.
220 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
225 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
226 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
228 internal = GST_PROXY_PAD_INTERNAL (pad);
229 res = gst_pad_push (internal, buffer);
235 * gst_proxy_pad_chain_list_default:
236 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
237 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
240 * Invoke the default chain list function of the proxy pad.
242 * Returns: a #GstFlowReturn from the pad.
247 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
252 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
253 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
255 internal = GST_PROXY_PAD_INTERNAL (pad);
256 res = gst_pad_push_list (internal, list);
262 * gst_proxy_pad_get_range_default:
263 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
264 * @offset: The start offset of the buffer
265 * @size: The length of the buffer
266 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
267 * returns #GST_FLOW_ERROR if %NULL.
269 * Invoke the default getrange function of the proxy pad.
271 * Returns: a #GstFlowReturn from the pad.
276 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
282 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
283 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
285 internal = GST_PROXY_PAD_INTERNAL (pad);
286 res = gst_pad_pull_range (internal, offset, size, buffer);
292 * gst_proxy_pad_getcaps_default:
293 * @pad: a #GstPad to get the capabilities of.
294 * @filter: a #GstCaps filter.
296 * Invoke the default getcaps function of the proxy pad.
298 * Returns: (transfer full): the caps of the pad with incremented ref-count
303 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
307 GstPadTemplate *templ;
309 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
311 templ = GST_PAD_PAD_TEMPLATE (pad);
312 target = gst_proxy_pad_get_target (pad);
314 /* if we have a real target, proxy the call */
315 res = gst_pad_get_caps (target, filter);
317 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
318 GST_DEBUG_PAD_NAME (target), res);
320 gst_object_unref (target);
322 /* filter against the template */
326 filt = GST_PAD_TEMPLATE_CAPS (templ);
328 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
329 gst_caps_unref (res);
331 GST_DEBUG_OBJECT (pad,
332 "filtered against template gives %" GST_PTR_FORMAT, res);
336 /* else, if we have a template, use its caps. */
338 res = GST_PAD_TEMPLATE_CAPS (templ);
339 GST_DEBUG_OBJECT (pad,
340 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
342 res = gst_caps_ref (res);
345 GstCaps *intersection =
346 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
348 gst_caps_unref (res);
355 /* last resort, any caps */
356 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
357 res = gst_caps_new_any ();
365 * gst_proxy_pad_acceptcaps_default:
366 * @pad: a #GstPad to check
367 * @caps: a #GstCaps to check on the pad
369 * Invoke the default acceptcaps function of the proxy pad.
371 * Returns: TRUE if the pad can accept the caps.
376 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
381 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
382 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
384 target = gst_proxy_pad_get_target (pad);
386 res = gst_pad_accept_caps (target, caps);
387 gst_object_unref (target);
389 GST_DEBUG_OBJECT (pad, "no target");
390 /* We don't have a target, we return TRUE and we assume that any future
391 * target will be able to deal with any configured caps. */
399 * gst_proxy_pad_fixatecaps_default:
400 * @pad: a #GstPad to fixate
401 * @caps: the #GstCaps to fixate
403 * Invoke the default fixatecaps function of the proxy pad.
408 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
412 g_return_if_fail (GST_IS_PROXY_PAD (pad));
413 g_return_if_fail (GST_IS_CAPS (caps));
415 if (!(target = gst_proxy_pad_get_target (pad)))
418 gst_pad_fixate_caps (target, caps);
419 gst_object_unref (target);
426 GST_DEBUG_OBJECT (pad, "no target");
432 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
437 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
439 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
440 goto wrong_direction;
442 GST_LOG_OBJECT (pad, "clearing target");
444 /* clear old target */
445 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
446 gst_object_unref (oldtarget);
448 /* set and ref new target if any */
450 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
452 GST_PROXY_PAD_TARGET (pad) = NULL;
459 GST_ERROR_OBJECT (pad,
460 "target pad doesn't have the same direction as ourself");
466 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
470 GST_PROXY_LOCK (pad);
471 result = gst_proxy_pad_set_target_unlocked (pad, target);
472 GST_PROXY_UNLOCK (pad);
478 gst_proxy_pad_get_target (GstPad * pad)
482 GST_PROXY_LOCK (pad);
483 target = GST_PROXY_PAD_TARGET (pad);
485 gst_object_ref (target);
486 GST_PROXY_UNLOCK (pad);
492 * gst_proxy_pad_get_internal:
493 * @pad: the #GstProxyPad
495 * Get the internal pad of @pad. Unref target pad after usage.
497 * The internal pad of a #GstGhostPad is the internally used
498 * pad of opposite direction, which is used to link to the target.
500 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
501 * Unref target pad after usage.
506 gst_proxy_pad_get_internal (GstProxyPad * pad)
510 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
512 GST_PROXY_LOCK (pad);
513 internal = GST_PROXY_PAD_INTERNAL (pad);
515 gst_object_ref (internal);
516 GST_PROXY_UNLOCK (pad);
518 return GST_PROXY_PAD_CAST (internal);
522 * gst_proxy_pad_unlink_default:
523 * @pad: a #GstPad to unlink
525 * Invoke the default unlink function of the proxy pad.
530 gst_proxy_pad_unlink_default (GstPad * pad)
534 /* don't do anything if this unlink resulted from retargeting the pad
535 * controlled by the ghostpad. We only want to invalidate the target pad when
536 * the element suddently unlinked with our internal pad. */
537 if (GST_PROXY_PAD_RETARGET (pad))
540 internal = GST_PROXY_PAD_INTERNAL (pad);
542 GST_DEBUG_OBJECT (pad, "pad is unlinked");
544 gst_proxy_pad_set_target (internal, NULL);
548 gst_proxy_pad_class_init (GstProxyPadClass * klass)
550 GObjectClass *gobject_class = (GObjectClass *) klass;
552 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
554 gobject_class->dispose = gst_proxy_pad_dispose;
555 gobject_class->finalize = gst_proxy_pad_finalize;
557 /* Register common function pointer descriptions */
558 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
559 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
560 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
561 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
562 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
563 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
564 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
565 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
566 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
567 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
568 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
572 gst_proxy_pad_dispose (GObject * object)
574 GstPad *pad = GST_PAD (object);
577 GST_PROXY_LOCK (pad);
578 /* remove and unref the target */
579 target_p = &GST_PROXY_PAD_TARGET (pad);
580 gst_object_replace ((GstObject **) target_p, NULL);
581 /* The internal is only cleared by GstGhostPad::dispose, since it is the
582 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
584 GST_PROXY_UNLOCK (pad);
586 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
590 gst_proxy_pad_finalize (GObject * object)
592 GstProxyPad *pad = GST_PROXY_PAD (object);
594 g_mutex_free (GST_PROXY_GET_LOCK (pad));
595 GST_PROXY_GET_LOCK (pad) = NULL;
597 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
601 gst_proxy_pad_init (GstProxyPad * ppad)
603 GstPad *pad = (GstPad *) ppad;
605 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
606 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
607 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
609 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
610 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
611 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
612 gst_pad_set_iterate_internal_links_function (pad,
613 gst_proxy_pad_iterate_internal_links_default);
615 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
616 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
617 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
618 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
622 /***********************************************************************
623 * Ghost pads, implemented as a pair of proxy pads (sort of)
627 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
629 struct _GstGhostPadPrivate
631 /* with PROXY_LOCK */
632 gboolean constructed;
635 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
637 static void gst_ghost_pad_dispose (GObject * object);
640 * gst_ghost_pad_internal_activate_push_default:
641 * @pad: the #GstPad to activate or deactivate.
642 * @active: whether the pad should be active or not.
644 * Invoke the default activate push function of a proxy pad that is
645 * owned by a ghost pad.
647 * Returns: %TRUE if the operation was successful.
652 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
657 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
659 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
660 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
662 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
663 * will be activated later (or already in case of a ghost sinkpad). */
664 other = GST_PROXY_PAD_INTERNAL (pad);
665 ret = gst_pad_activate_push (other, active);
671 * gst_ghost_pad_internal_activate_pull_default:
672 * @pad: the #GstPad to activate or deactivate.
673 * @active: whether the pad should be active or not.
675 * Invoke the default activate pull function of a proxy pad that is
676 * owned by a ghost pad.
678 * Returns: %TRUE if the operation was successful.
683 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
688 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
690 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
691 GST_DEBUG_PAD_NAME (pad));
693 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
694 /* we are activated in pull mode by our peer element, which is a sinkpad
695 * that wants to operate in pull mode. This activation has to propagate
696 * upstream throught the pipeline. We call the internal activation function,
697 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
698 * further upstream */
699 GST_LOG_OBJECT (pad, "pad is src, activate internal");
700 other = GST_PROXY_PAD_INTERNAL (pad);
701 ret = gst_pad_activate_pull (other, active);
702 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
703 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
704 * since we hold a pointer to the upstream peer. */
705 GST_LOG_OBJECT (pad, "activating peer");
706 ret = gst_pad_activate_pull (other, active);
707 gst_object_unref (other);
709 /* this is failure, we can't activate pull if there is no peer */
710 GST_LOG_OBJECT (pad, "not src and no peer, failing");
718 * gst_ghost_pad_activate_push_default:
719 * @pad: the #GstPad to activate or deactivate.
720 * @active: whether the pad should be active or not.
722 * Invoke the default activate push function of a ghost pad.
724 * Returns: %TRUE if the operation was successful.
729 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
734 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
736 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
737 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
739 /* just activate the internal pad */
740 other = GST_PROXY_PAD_INTERNAL (pad);
741 ret = gst_pad_activate_push (other, active);
747 * gst_ghost_pad_activate_pull_default:
748 * @pad: the #GstPad to activate or deactivate.
749 * @active: whether the pad should be active or not.
751 * Invoke the default activate pull function of a ghost pad.
753 * Returns: %TRUE if the operation was successful.
758 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
763 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
765 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
766 GST_DEBUG_PAD_NAME (pad));
768 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
769 /* the ghostpad is SRC and activated in pull mode by its peer, call the
770 * activation function of the internal pad to propagate the activation
772 GST_LOG_OBJECT (pad, "pad is src, activate internal");
773 other = GST_PROXY_PAD_INTERNAL (pad);
774 ret = gst_pad_activate_pull (other, active);
775 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
776 /* We are SINK and activated by the internal pad, propagate activation
777 * upstream because we hold a ref to the upstream peer */
778 GST_LOG_OBJECT (pad, "activating peer");
779 ret = gst_pad_activate_pull (other, active);
780 gst_object_unref (other);
782 /* no peer, we fail */
783 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
791 * gst_ghost_pad_link_default:
792 * @pad: the #GstPad to link.
793 * @peer: the #GstPad peer
795 * Invoke the default link function of a ghost pad.
797 * Returns: #GstPadLinkReturn of the operation
802 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
804 GstPadLinkReturn ret;
807 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
808 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
810 GST_DEBUG_OBJECT (pad, "linking ghostpad");
812 internal = GST_PROXY_PAD_INTERNAL (pad);
813 if (!gst_proxy_pad_set_target (internal, peer))
816 ret = GST_PAD_LINK_OK;
817 /* if we are a source pad, we should call the peer link function
818 * if the peer has one, see design docs. */
819 if (GST_PAD_IS_SRC (pad)) {
820 if (GST_PAD_LINKFUNC (peer)) {
821 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
822 if (ret != GST_PAD_LINK_OK)
831 GST_DEBUG_OBJECT (pad, "setting target failed");
832 return GST_PAD_LINK_REFUSED;
836 GST_DEBUG_OBJECT (pad, "linking failed");
837 /* clear target again */
838 gst_proxy_pad_set_target (internal, NULL);
844 * gst_ghost_pad_unlink_default:
845 * @pad: the #GstPad to link.
847 * Invoke the default unlink function of a ghost pad.
852 gst_ghost_pad_unlink_default (GstPad * pad)
856 g_return_if_fail (GST_IS_GHOST_PAD (pad));
858 internal = GST_PROXY_PAD_INTERNAL (pad);
860 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
862 /* The target of the internal pad is no longer valid */
863 gst_proxy_pad_set_target (internal, NULL);
867 gst_ghost_pad_class_init (GstGhostPadClass * klass)
869 GObjectClass *gobject_class = (GObjectClass *) klass;
871 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
873 gobject_class->dispose = gst_ghost_pad_dispose;
875 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
876 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
877 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
881 gst_ghost_pad_init (GstGhostPad * pad)
883 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
884 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
886 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
887 gst_ghost_pad_activate_pull_default);
888 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
889 gst_ghost_pad_activate_push_default);
893 gst_ghost_pad_dispose (GObject * object)
899 pad = GST_PAD (object);
901 GST_DEBUG_OBJECT (pad, "dispose");
903 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
905 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
906 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
907 peer = gst_pad_get_peer (pad);
909 if (GST_PAD_IS_SRC (pad))
910 gst_pad_unlink (pad, peer);
912 gst_pad_unlink (peer, pad);
914 gst_object_unref (peer);
917 GST_PROXY_LOCK (pad);
918 internal = GST_PROXY_PAD_INTERNAL (pad);
920 gst_pad_set_activatepull_function (internal, NULL);
921 gst_pad_set_activatepush_function (internal, NULL);
923 /* disposes of the internal pad, since the ghostpad is the only possible object
924 * that has a refcount on the internal pad. */
925 gst_object_unparent (GST_OBJECT_CAST (internal));
926 GST_PROXY_PAD_INTERNAL (pad) = NULL;
928 GST_PROXY_UNLOCK (pad);
930 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
934 * gst_ghost_pad_construct:
935 * @gpad: the newly allocated ghost pad
937 * Finish initialization of a newly allocated ghost pad.
939 * This function is most useful in language bindings and when subclassing
940 * #GstGhostPad; plugin and application developers normally will not call this
941 * function. Call this function directly after a call to g_object_new
942 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
944 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
949 gst_ghost_pad_construct (GstGhostPad * gpad)
951 GstPadDirection dir, otherdir;
952 GstPadTemplate *templ;
953 GstPad *pad, *internal;
955 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
956 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
959 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
961 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
963 pad = GST_PAD (gpad);
965 /* Set directional padfunctions for ghostpad */
966 if (dir == GST_PAD_SINK) {
967 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
968 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
970 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
973 /* link/unlink functions */
974 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
975 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
977 /* INTERNAL PAD, it always exists and is child of the ghostpad */
978 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
981 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
982 "direction", otherdir, "template", templ, NULL);
983 /* release ref obtained via g_object_get */
984 gst_object_unref (templ);
987 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
988 "direction", otherdir, NULL);
990 GST_PAD_UNSET_FLUSHING (internal);
992 /* Set directional padfunctions for internal pad */
993 if (dir == GST_PAD_SRC) {
994 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
995 gst_pad_set_chain_list_function (internal,
996 gst_proxy_pad_chain_list_default);
998 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
1001 GST_PROXY_LOCK (pad);
1003 /* now make the ghostpad a parent of the internal pad */
1004 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
1005 GST_OBJECT_CAST (pad)))
1008 /* The ghostpad is the parent of the internal pad and is the only object that
1009 * can have a refcount on the internal pad.
1010 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
1012 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
1013 * its refcount on the internal pad in the dispose method by un-parenting it.
1014 * This is why we don't take extra refcounts in the assignments below
1016 GST_PROXY_PAD_INTERNAL (pad) = internal;
1017 GST_PROXY_PAD_INTERNAL (internal) = pad;
1019 /* special activation functions for the internal pad */
1020 gst_pad_set_activatepull_function (internal,
1021 gst_ghost_pad_internal_activate_pull_default);
1022 gst_pad_set_activatepush_function (internal,
1023 gst_ghost_pad_internal_activate_push_default);
1025 GST_PROXY_UNLOCK (pad);
1027 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
1033 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
1034 GST_DEBUG_PAD_NAME (internal));
1035 g_critical ("Could not set internal pad %s:%s",
1036 GST_DEBUG_PAD_NAME (internal));
1037 GST_PROXY_UNLOCK (pad);
1038 gst_object_unref (internal);
1044 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
1045 GstPadTemplate * templ)
1049 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1051 /* OBJECT CREATION */
1053 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1054 "direction", dir, "template", templ, NULL);
1056 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1057 "direction", dir, NULL);
1060 if (!gst_ghost_pad_construct (ret))
1061 goto construct_failed;
1063 return GST_PAD_CAST (ret);
1066 /* already logged */
1067 gst_object_unref (ret);
1072 * gst_ghost_pad_new_no_target:
1073 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1074 * @dir: the direction of the ghostpad
1076 * Create a new ghostpad without a target with the given direction.
1077 * A target can be set on the ghostpad later with the
1078 * gst_ghost_pad_set_target() function.
1080 * The created ghostpad will not have a padtemplate.
1082 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1085 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1089 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1091 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1093 ret = gst_ghost_pad_new_full (name, dir, NULL);
1099 * gst_ghost_pad_new:
1100 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1101 * @target: (transfer none): the pad to ghost.
1103 * Create a new ghostpad with @target as the target. The direction will be taken
1104 * from the target pad. @target must be unlinked.
1106 * Will ref the target.
1108 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1111 gst_ghost_pad_new (const gchar * name, GstPad * target)
1115 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1116 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1118 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1119 GST_DEBUG_PAD_NAME (target));
1121 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1122 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1123 goto set_target_failed;
1130 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1131 GST_DEBUG_PAD_NAME (target));
1132 gst_object_unref (ret);
1138 * gst_ghost_pad_new_from_template:
1139 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1140 * @target: (transfer none): the pad to ghost.
1141 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1143 * Create a new ghostpad with @target as the target. The direction will be taken
1144 * from the target pad. The template used on the ghostpad will be @template.
1146 * Will ref the target.
1148 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1154 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1155 GstPadTemplate * templ)
1159 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1160 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1161 g_return_val_if_fail (templ != NULL, NULL);
1162 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1163 GST_PAD_DIRECTION (target), NULL);
1165 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1166 GST_DEBUG_PAD_NAME (target), templ);
1168 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1169 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1170 goto set_target_failed;
1177 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1178 GST_DEBUG_PAD_NAME (target));
1179 gst_object_unref (ret);
1185 * gst_ghost_pad_new_no_target_from_template:
1186 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1187 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1189 * Create a new ghostpad based on @templ, without setting a target. The
1190 * direction will be taken from the @templ.
1192 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1197 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1198 GstPadTemplate * templ)
1202 g_return_val_if_fail (templ != NULL, NULL);
1205 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1211 * gst_ghost_pad_get_target:
1212 * @gpad: the #GstGhostPad
1214 * Get the target pad of @gpad. Unref target pad after usage.
1216 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1217 * has no target set. Unref target pad after usage.
1220 gst_ghost_pad_get_target (GstGhostPad * gpad)
1224 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1226 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1228 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1234 * gst_ghost_pad_set_target:
1235 * @gpad: the #GstGhostPad
1236 * @newtarget: (transfer none) (allow-none): the new pad target
1238 * Set the new target of the ghostpad @gpad. Any existing target
1239 * is unlinked and links to the new target are established. if @newtarget is
1240 * NULL the target will be cleared.
1242 * Returns: (transfer full): TRUE if the new target could be set. This function
1243 * can return FALSE when the internal pads could not be linked.
1246 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1251 GstPadLinkReturn lret;
1253 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1254 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1255 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1257 /* no need for locking, the internal pad's lifecycle is directly linked to the
1259 internal = GST_PROXY_PAD_INTERNAL (gpad);
1262 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1264 GST_DEBUG_OBJECT (gpad, "clearing target");
1266 /* clear old target */
1267 GST_PROXY_LOCK (gpad);
1268 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1270 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1272 /* unlink internal pad */
1273 if (GST_PAD_IS_SRC (internal))
1274 gst_pad_unlink (internal, oldtarget);
1276 gst_pad_unlink (oldtarget, internal);
1278 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1281 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1282 GST_PROXY_UNLOCK (gpad);
1284 if (result && newtarget) {
1285 /* and link to internal pad without any checks */
1286 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1288 if (GST_PAD_IS_SRC (internal))
1290 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1293 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1295 if (lret != GST_PAD_LINK_OK)
1304 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1306 /* and unset target again */
1307 GST_PROXY_LOCK (gpad);
1308 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1309 GST_PROXY_UNLOCK (gpad);