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_getcaps_default:
287 * @pad: a #GstPad to get the capabilities of.
288 * @filter: a #GstCaps filter.
290 * Invoke the default getcaps function of the proxy pad.
292 * Returns: (transfer full): the caps of the pad with incremented ref-count
297 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
301 GstPadTemplate *templ;
303 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
305 templ = GST_PAD_PAD_TEMPLATE (pad);
306 target = gst_proxy_pad_get_target (pad);
308 /* if we have a real target, proxy the call */
309 res = gst_pad_get_caps (target, filter);
311 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
312 GST_DEBUG_PAD_NAME (target), res);
314 gst_object_unref (target);
316 /* filter against the template */
320 filt = GST_PAD_TEMPLATE_CAPS (templ);
322 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
323 gst_caps_unref (res);
325 GST_DEBUG_OBJECT (pad,
326 "filtered against template gives %" GST_PTR_FORMAT, res);
330 /* else, if we have a template, use its caps. */
332 res = GST_PAD_TEMPLATE_CAPS (templ);
333 GST_DEBUG_OBJECT (pad,
334 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
336 res = gst_caps_ref (res);
339 GstCaps *intersection =
340 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
342 gst_caps_unref (res);
349 /* last resort, any caps */
350 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
351 res = gst_caps_new_any ();
359 * gst_proxy_pad_acceptcaps_default:
360 * @pad: a #GstPad to check
361 * @caps: a #GstCaps to check on the pad
363 * Invoke the default acceptcaps function of the proxy pad.
365 * Returns: TRUE if the pad can accept the caps.
370 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
375 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
376 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
378 target = gst_proxy_pad_get_target (pad);
380 res = gst_pad_accept_caps (target, caps);
381 gst_object_unref (target);
383 /* We don't have a target, we return TRUE and we assume that any future
384 * target will be able to deal with any configured caps. */
392 * gst_proxy_pad_fixatecaps_default:
393 * @pad: a #GstPad to fixate
394 * @caps: the #GstCaps to fixate
396 * Invoke the default fixatecaps function of the proxy pad.
401 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
405 g_return_if_fail (GST_IS_PROXY_PAD (pad));
406 g_return_if_fail (GST_IS_CAPS (caps));
408 target = gst_proxy_pad_get_target (pad);
410 gst_pad_fixate_caps (target, caps);
411 gst_object_unref (target);
416 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
421 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
423 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
424 goto wrong_direction;
426 GST_LOG_OBJECT (pad, "clearing target");
428 /* clear old target */
429 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
430 gst_object_unref (oldtarget);
432 /* set and ref new target if any */
434 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
436 GST_PROXY_PAD_TARGET (pad) = NULL;
443 GST_ERROR_OBJECT (pad,
444 "target pad doesn't have the same direction as ourself");
450 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
454 GST_PROXY_LOCK (pad);
455 result = gst_proxy_pad_set_target_unlocked (pad, target);
456 GST_PROXY_UNLOCK (pad);
462 gst_proxy_pad_get_target (GstPad * pad)
466 GST_PROXY_LOCK (pad);
467 target = GST_PROXY_PAD_TARGET (pad);
469 gst_object_ref (target);
470 GST_PROXY_UNLOCK (pad);
476 * gst_proxy_pad_get_internal:
477 * @pad: the #GstProxyPad
479 * Get the internal pad of @pad. Unref target pad after usage.
481 * The internal pad of a #GstGhostPad is the internally used
482 * pad of opposite direction, which is used to link to the target.
484 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
485 * Unref target pad after usage.
490 gst_proxy_pad_get_internal (GstProxyPad * pad)
494 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
496 GST_PROXY_LOCK (pad);
497 internal = GST_PROXY_PAD_INTERNAL (pad);
499 gst_object_ref (internal);
500 GST_PROXY_UNLOCK (pad);
502 return GST_PROXY_PAD_CAST (internal);
506 * gst_proxy_pad_unlink_default:
507 * @pad: a #GstPad to unlink
509 * Invoke the default unlink function of the proxy pad.
514 gst_proxy_pad_unlink_default (GstPad * pad)
518 /* don't do anything if this unlink resulted from retargeting the pad
519 * controlled by the ghostpad. We only want to invalidate the target pad when
520 * the element suddently unlinked with our internal pad. */
521 if (GST_PROXY_PAD_RETARGET (pad))
524 internal = GST_PROXY_PAD_INTERNAL (pad);
526 GST_DEBUG_OBJECT (pad, "pad is unlinked");
528 gst_proxy_pad_set_target (internal, NULL);
532 gst_proxy_pad_class_init (GstProxyPadClass * klass)
534 GObjectClass *gobject_class = (GObjectClass *) klass;
536 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
538 gobject_class->dispose = gst_proxy_pad_dispose;
539 gobject_class->finalize = gst_proxy_pad_finalize;
541 /* Register common function pointer descriptions */
542 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
543 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
544 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
545 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
546 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
547 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
548 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
549 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
550 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
551 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
552 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
556 gst_proxy_pad_dispose (GObject * object)
558 GstPad *pad = GST_PAD (object);
561 GST_PROXY_LOCK (pad);
562 /* remove and unref the target */
563 target_p = &GST_PROXY_PAD_TARGET (pad);
564 gst_object_replace ((GstObject **) target_p, NULL);
565 /* The internal is only cleared by GstGhostPad::dispose, since it is the
566 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
568 GST_PROXY_UNLOCK (pad);
570 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
574 gst_proxy_pad_finalize (GObject * object)
576 GstProxyPad *pad = GST_PROXY_PAD (object);
578 g_mutex_free (GST_PROXY_GET_LOCK (pad));
579 GST_PROXY_GET_LOCK (pad) = NULL;
581 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
585 gst_proxy_pad_init (GstProxyPad * ppad)
587 GstPad *pad = (GstPad *) ppad;
589 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
590 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
591 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
593 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
594 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
595 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
596 gst_pad_set_iterate_internal_links_function (pad,
597 gst_proxy_pad_iterate_internal_links_default);
599 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
600 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
601 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
602 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
606 /***********************************************************************
607 * Ghost pads, implemented as a pair of proxy pads (sort of)
611 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
613 struct _GstGhostPadPrivate
615 /* with PROXY_LOCK */
616 gboolean constructed;
619 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
621 static void gst_ghost_pad_dispose (GObject * object);
624 * gst_ghost_pad_internal_activate_push_default:
625 * @pad: the #GstPad to activate or deactivate.
626 * @active: whether the pad should be active or not.
628 * Invoke the default activate push function of a proxy pad that is
629 * owned by a ghost pad.
631 * Returns: %TRUE if the operation was successful.
636 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
641 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
643 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
644 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
646 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
647 * will be activated later (or already in case of a ghost sinkpad). */
648 other = GST_PROXY_PAD_INTERNAL (pad);
649 ret = gst_pad_activate_push (other, active);
655 * gst_ghost_pad_internal_activate_pull_default:
656 * @pad: the #GstPad to activate or deactivate.
657 * @active: whether the pad should be active or not.
659 * Invoke the default activate pull function of a proxy pad that is
660 * owned by a ghost pad.
662 * Returns: %TRUE if the operation was successful.
667 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
672 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
674 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
675 GST_DEBUG_PAD_NAME (pad));
677 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
678 /* we are activated in pull mode by our peer element, which is a sinkpad
679 * that wants to operate in pull mode. This activation has to propagate
680 * upstream throught the pipeline. We call the internal activation function,
681 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
682 * further upstream */
683 GST_LOG_OBJECT (pad, "pad is src, activate internal");
684 other = GST_PROXY_PAD_INTERNAL (pad);
685 ret = gst_pad_activate_pull (other, active);
686 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
687 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
688 * since we hold a pointer to the upstream peer. */
689 GST_LOG_OBJECT (pad, "activating peer");
690 ret = gst_pad_activate_pull (other, active);
691 gst_object_unref (other);
693 /* this is failure, we can't activate pull if there is no peer */
694 GST_LOG_OBJECT (pad, "not src and no peer, failing");
702 * gst_ghost_pad_activate_push_default:
703 * @pad: the #GstPad to activate or deactivate.
704 * @active: whether the pad should be active or not.
706 * Invoke the default activate push function of a ghost pad.
708 * Returns: %TRUE if the operation was successful.
713 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
718 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
720 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
721 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
723 /* just activate the internal pad */
724 other = GST_PROXY_PAD_INTERNAL (pad);
725 ret = gst_pad_activate_push (other, active);
731 * gst_ghost_pad_activate_pull_default:
732 * @pad: the #GstPad to activate or deactivate.
733 * @active: whether the pad should be active or not.
735 * Invoke the default activate pull function of a ghost pad.
737 * Returns: %TRUE if the operation was successful.
742 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
747 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
749 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
750 GST_DEBUG_PAD_NAME (pad));
752 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
753 /* the ghostpad is SRC and activated in pull mode by its peer, call the
754 * activation function of the internal pad to propagate the activation
756 GST_LOG_OBJECT (pad, "pad is src, activate internal");
757 other = GST_PROXY_PAD_INTERNAL (pad);
758 ret = gst_pad_activate_pull (other, active);
759 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
760 /* We are SINK and activated by the internal pad, propagate activation
761 * upstream because we hold a ref to the upstream peer */
762 GST_LOG_OBJECT (pad, "activating peer");
763 ret = gst_pad_activate_pull (other, active);
764 gst_object_unref (other);
766 /* no peer, we fail */
767 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
775 * gst_ghost_pad_link_default:
776 * @pad: the #GstPad to link.
777 * @peer: the #GstPad peer
779 * Invoke the default link function of a ghost pad.
781 * Returns: #GstPadLinkReturn of the operation
786 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
788 GstPadLinkReturn ret;
791 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
792 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
794 GST_DEBUG_OBJECT (pad, "linking ghostpad");
796 internal = GST_PROXY_PAD_INTERNAL (pad);
797 if (!gst_proxy_pad_set_target (internal, peer))
800 ret = GST_PAD_LINK_OK;
801 /* if we are a source pad, we should call the peer link function
802 * if the peer has one, see design docs. */
803 if (GST_PAD_IS_SRC (pad)) {
804 if (GST_PAD_LINKFUNC (peer)) {
805 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
806 if (ret != GST_PAD_LINK_OK)
815 GST_DEBUG_OBJECT (pad, "setting target failed");
816 return GST_PAD_LINK_REFUSED;
820 GST_DEBUG_OBJECT (pad, "linking failed");
821 /* clear target again */
822 gst_proxy_pad_set_target (internal, NULL);
828 * gst_ghost_pad_unlink_default:
829 * @pad: the #GstPad to link.
831 * Invoke the default unlink function of a ghost pad.
836 gst_ghost_pad_unlink_default (GstPad * pad)
840 g_return_if_fail (GST_IS_GHOST_PAD (pad));
842 internal = GST_PROXY_PAD_INTERNAL (pad);
844 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
846 /* The target of the internal pad is no longer valid */
847 gst_proxy_pad_set_target (internal, NULL);
851 gst_ghost_pad_class_init (GstGhostPadClass * klass)
853 GObjectClass *gobject_class = (GObjectClass *) klass;
855 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
857 gobject_class->dispose = gst_ghost_pad_dispose;
859 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
860 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
861 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
865 gst_ghost_pad_init (GstGhostPad * pad)
867 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
868 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
870 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
871 gst_ghost_pad_activate_pull_default);
872 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
873 gst_ghost_pad_activate_push_default);
877 gst_ghost_pad_dispose (GObject * object)
883 pad = GST_PAD (object);
885 GST_DEBUG_OBJECT (pad, "dispose");
887 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
889 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
890 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
891 peer = gst_pad_get_peer (pad);
893 if (GST_PAD_IS_SRC (pad))
894 gst_pad_unlink (pad, peer);
896 gst_pad_unlink (peer, pad);
898 gst_object_unref (peer);
901 GST_PROXY_LOCK (pad);
902 internal = GST_PROXY_PAD_INTERNAL (pad);
904 gst_pad_set_activatepull_function (internal, NULL);
905 gst_pad_set_activatepush_function (internal, NULL);
907 /* disposes of the internal pad, since the ghostpad is the only possible object
908 * that has a refcount on the internal pad. */
909 gst_object_unparent (GST_OBJECT_CAST (internal));
910 GST_PROXY_PAD_INTERNAL (pad) = NULL;
912 GST_PROXY_UNLOCK (pad);
914 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
918 * gst_ghost_pad_construct:
919 * @gpad: the newly allocated ghost pad
921 * Finish initialization of a newly allocated ghost pad.
923 * This function is most useful in language bindings and when subclassing
924 * #GstGhostPad; plugin and application developers normally will not call this
925 * function. Call this function directly after a call to g_object_new
926 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
928 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
933 gst_ghost_pad_construct (GstGhostPad * gpad)
935 GstPadDirection dir, otherdir;
936 GstPadTemplate *templ;
937 GstPad *pad, *internal;
939 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
940 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
943 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
945 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
947 pad = GST_PAD (gpad);
949 /* Set directional padfunctions for ghostpad */
950 if (dir == GST_PAD_SINK) {
951 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
952 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
954 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
957 /* link/unlink functions */
958 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
959 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
961 /* INTERNAL PAD, it always exists and is child of the ghostpad */
962 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
965 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
966 "direction", otherdir, "template", templ, NULL);
967 /* release ref obtained via g_object_get */
968 gst_object_unref (templ);
971 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
972 "direction", otherdir, NULL);
974 GST_PAD_UNSET_FLUSHING (internal);
976 /* Set directional padfunctions for internal pad */
977 if (dir == GST_PAD_SRC) {
978 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
979 gst_pad_set_chain_list_function (internal,
980 gst_proxy_pad_chain_list_default);
982 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
985 GST_PROXY_LOCK (pad);
987 /* now make the ghostpad a parent of the internal pad */
988 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
989 GST_OBJECT_CAST (pad)))
992 /* The ghostpad is the parent of the internal pad and is the only object that
993 * can have a refcount on the internal pad.
994 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
996 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
997 * its refcount on the internal pad in the dispose method by un-parenting it.
998 * This is why we don't take extra refcounts in the assignments below
1000 GST_PROXY_PAD_INTERNAL (pad) = internal;
1001 GST_PROXY_PAD_INTERNAL (internal) = pad;
1003 /* special activation functions for the internal pad */
1004 gst_pad_set_activatepull_function (internal,
1005 gst_ghost_pad_internal_activate_pull_default);
1006 gst_pad_set_activatepush_function (internal,
1007 gst_ghost_pad_internal_activate_push_default);
1009 GST_PROXY_UNLOCK (pad);
1011 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
1017 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
1018 GST_DEBUG_PAD_NAME (internal));
1019 g_critical ("Could not set internal pad %s:%s",
1020 GST_DEBUG_PAD_NAME (internal));
1021 GST_PROXY_UNLOCK (pad);
1022 gst_object_unref (internal);
1028 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
1029 GstPadTemplate * templ)
1033 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1035 /* OBJECT CREATION */
1037 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1038 "direction", dir, "template", templ, NULL);
1040 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1041 "direction", dir, NULL);
1044 if (!gst_ghost_pad_construct (ret))
1045 goto construct_failed;
1047 return GST_PAD_CAST (ret);
1050 /* already logged */
1051 gst_object_unref (ret);
1056 * gst_ghost_pad_new_no_target:
1057 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1058 * @dir: the direction of the ghostpad
1060 * Create a new ghostpad without a target with the given direction.
1061 * A target can be set on the ghostpad later with the
1062 * gst_ghost_pad_set_target() function.
1064 * The created ghostpad will not have a padtemplate.
1066 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1069 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1073 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1075 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1077 ret = gst_ghost_pad_new_full (name, dir, NULL);
1083 * gst_ghost_pad_new:
1084 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1085 * @target: (transfer none): the pad to ghost.
1087 * Create a new ghostpad with @target as the target. The direction will be taken
1088 * from the target pad. @target must be unlinked.
1090 * Will ref the target.
1092 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1095 gst_ghost_pad_new (const gchar * name, GstPad * target)
1099 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1100 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1102 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1103 GST_DEBUG_PAD_NAME (target));
1105 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1106 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1107 goto set_target_failed;
1114 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1115 GST_DEBUG_PAD_NAME (target));
1116 gst_object_unref (ret);
1122 * gst_ghost_pad_new_from_template:
1123 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1124 * @target: (transfer none): the pad to ghost.
1125 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1127 * Create a new ghostpad with @target as the target. The direction will be taken
1128 * from the target pad. The template used on the ghostpad will be @template.
1130 * Will ref the target.
1132 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1138 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1139 GstPadTemplate * templ)
1143 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1144 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1145 g_return_val_if_fail (templ != NULL, NULL);
1146 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1147 GST_PAD_DIRECTION (target), NULL);
1149 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1150 GST_DEBUG_PAD_NAME (target), templ);
1152 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1153 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1154 goto set_target_failed;
1161 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1162 GST_DEBUG_PAD_NAME (target));
1163 gst_object_unref (ret);
1169 * gst_ghost_pad_new_no_target_from_template:
1170 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1171 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1173 * Create a new ghostpad based on @templ, without setting a target. The
1174 * direction will be taken from the @templ.
1176 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1181 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1182 GstPadTemplate * templ)
1186 g_return_val_if_fail (templ != NULL, NULL);
1189 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1195 * gst_ghost_pad_get_target:
1196 * @gpad: the #GstGhostPad
1198 * Get the target pad of @gpad. Unref target pad after usage.
1200 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1201 * has no target set. Unref target pad after usage.
1204 gst_ghost_pad_get_target (GstGhostPad * gpad)
1208 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1210 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1212 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1218 * gst_ghost_pad_set_target:
1219 * @gpad: the #GstGhostPad
1220 * @newtarget: (transfer none) (allow-none): the new pad target
1222 * Set the new target of the ghostpad @gpad. Any existing target
1223 * is unlinked and links to the new target are established. if @newtarget is
1224 * NULL the target will be cleared.
1226 * Returns: (transfer full): TRUE if the new target could be set. This function
1227 * can return FALSE when the internal pads could not be linked.
1230 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1235 GstPadLinkReturn lret;
1237 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1238 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1239 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1241 /* no need for locking, the internal pad's lifecycle is directly linked to the
1243 internal = GST_PROXY_PAD_INTERNAL (gpad);
1246 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1248 GST_DEBUG_OBJECT (gpad, "clearing target");
1250 /* clear old target */
1251 GST_PROXY_LOCK (gpad);
1252 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1254 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1256 /* unlink internal pad */
1257 if (GST_PAD_IS_SRC (internal))
1258 gst_pad_unlink (internal, oldtarget);
1260 gst_pad_unlink (oldtarget, internal);
1262 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1265 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1266 GST_PROXY_UNLOCK (gpad);
1268 if (result && newtarget) {
1269 /* and link to internal pad without any checks */
1270 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1272 if (GST_PAD_IS_SRC (internal))
1274 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1277 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1279 if (lret != GST_PAD_LINK_OK)
1288 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1290 /* and unset target again */
1291 GST_PROXY_LOCK (gpad);
1292 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1293 GST_PROXY_UNLOCK (gpad);