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_PAD_PEER (GST_PROXY_PAD_INTERNAL (pad)))
58 #define GST_PROXY_PAD_INTERNAL(pad) (GST_PROXY_PAD_PRIVATE (pad)->internal)
60 struct _GstProxyPadPrivate
65 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
67 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
70 * gst_proxy_pad_query_type_default:
73 * Invoke the default query type handler of the proxy pad.
75 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
81 gst_proxy_pad_query_type_default (GstPad * pad)
84 const GstQueryType *res = NULL;
86 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
88 if (!(target = gst_proxy_pad_get_target (pad)))
91 res = gst_pad_get_query_types (target);
92 gst_object_unref (target);
99 GST_DEBUG_OBJECT (pad, "no target pad");
105 * gst_proxy_pad_event_default:
106 * @pad: a #GstPad to push the event to.
107 * @event: (transfer full): the #GstEvent to send to the pad.
109 * Invoke the default event of the proxy pad.
111 * Returns: TRUE if the event was handled.
116 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
121 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
122 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
124 internal = GST_PROXY_PAD_INTERNAL (pad);
125 res = gst_pad_push_event (internal, event);
131 * gst_proxy_pad_query_default:
132 * @pad: a #GstPad to invoke the default query on.
133 * @query: (transfer none): the #GstQuery to perform.
135 * Invoke the default query function of the proxy pad.
137 * Returns: TRUE if the query could be performed.
142 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
146 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
147 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
149 switch (GST_QUERY_TYPE (query)) {
154 if (!(target = gst_proxy_pad_get_target (pad)))
157 res = gst_pad_query (target, query);
158 gst_object_unref (target);
168 GST_DEBUG_OBJECT (pad, "no target pad");
174 * gst_proyx_pad_iterate_internal_links_default:
175 * @pad: the #GstPad to get the internal links of.
177 * Invoke the default iterate internal links function of the proxy pad.
179 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
180 * returned pad with gst_object_unref().
185 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
187 GstIterator *res = NULL;
191 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
193 internal = GST_PROXY_PAD_INTERNAL (pad);
194 g_value_init (&v, GST_TYPE_PAD);
195 g_value_set_object (&v, internal);
196 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
203 * gst_proxy_pad_chain_default:
204 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
205 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
208 * Invoke the default chain function of the proxy pad.
210 * Returns: a #GstFlowReturn from the pad.
215 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
220 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
221 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
223 internal = GST_PROXY_PAD_INTERNAL (pad);
224 res = gst_pad_push (internal, buffer);
230 * gst_proxy_pad_chain_list_default:
231 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
232 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
235 * Invoke the default chain list function of the proxy pad.
237 * Returns: a #GstFlowReturn from the pad.
242 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
247 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
248 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
250 internal = GST_PROXY_PAD_INTERNAL (pad);
251 res = gst_pad_push_list (internal, list);
257 * gst_proxy_pad_get_range_default:
258 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
259 * @offset: The start offset of the buffer
260 * @size: The length of the buffer
261 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
262 * returns #GST_FLOW_ERROR if %NULL.
264 * Invoke the default getrange function of the proxy pad.
266 * Returns: a #GstFlowReturn from the pad.
271 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
277 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
278 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
280 internal = GST_PROXY_PAD_INTERNAL (pad);
281 res = gst_pad_pull_range (internal, offset, size, buffer);
287 * gst_proxy_pad_getcaps_default:
288 * @pad: a #GstPad to get the capabilities of.
289 * @filter: a #GstCaps filter.
291 * Invoke the default getcaps function of the proxy pad.
293 * Returns: (transfer full): the caps of the pad with incremented ref-count
298 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
302 GstPadTemplate *templ;
304 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
306 templ = GST_PAD_PAD_TEMPLATE (pad);
307 target = gst_proxy_pad_get_target (pad);
309 /* if we have a real target, proxy the call */
310 res = gst_pad_get_caps (target, filter);
312 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
313 GST_DEBUG_PAD_NAME (target), res);
315 gst_object_unref (target);
317 /* filter against the template */
321 filt = GST_PAD_TEMPLATE_CAPS (templ);
323 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
324 gst_caps_unref (res);
326 GST_DEBUG_OBJECT (pad,
327 "filtered against template gives %" GST_PTR_FORMAT, res);
331 /* else, if we have a template, use its caps. */
333 res = GST_PAD_TEMPLATE_CAPS (templ);
334 GST_DEBUG_OBJECT (pad,
335 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
337 res = gst_caps_ref (res);
340 GstCaps *intersection =
341 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
343 gst_caps_unref (res);
350 /* If there's a filter, return that */
351 if (filter != NULL) {
352 res = gst_caps_ref (filter);
356 /* last resort, any caps */
357 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
358 res = gst_caps_new_any ();
366 * gst_proxy_pad_acceptcaps_default:
367 * @pad: a #GstPad to check
368 * @caps: a #GstCaps to check on the pad
370 * Invoke the default acceptcaps function of the proxy pad.
372 * Returns: TRUE if the pad can accept the caps.
377 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
382 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
383 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
385 target = gst_proxy_pad_get_target (pad);
387 res = gst_pad_accept_caps (target, caps);
388 gst_object_unref (target);
390 GST_DEBUG_OBJECT (pad, "no target");
391 /* We don't have a target, we return TRUE and we assume that any future
392 * target will be able to deal with any configured caps. */
400 * gst_proxy_pad_fixatecaps_default:
401 * @pad: a #GstPad to fixate
402 * @caps: the #GstCaps to fixate
404 * Invoke the default fixatecaps function of the proxy pad.
409 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
413 g_return_if_fail (GST_IS_PROXY_PAD (pad));
414 g_return_if_fail (GST_IS_CAPS (caps));
416 if (!(target = gst_proxy_pad_get_target (pad)))
419 gst_pad_fixate_caps (target, caps);
420 gst_object_unref (target);
427 GST_DEBUG_OBJECT (pad, "no target");
433 gst_proxy_pad_get_target (GstPad * pad)
437 GST_OBJECT_LOCK (pad);
438 target = GST_PROXY_PAD_TARGET (pad);
440 gst_object_ref (target);
441 GST_OBJECT_UNLOCK (pad);
447 * gst_proxy_pad_get_internal:
448 * @pad: the #GstProxyPad
450 * Get the internal pad of @pad. Unref target pad after usage.
452 * The internal pad of a #GstGhostPad is the internally used
453 * pad of opposite direction, which is used to link to the target.
455 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
456 * Unref target pad after usage.
461 gst_proxy_pad_get_internal (GstProxyPad * pad)
465 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
467 GST_OBJECT_LOCK (pad);
468 internal = GST_PROXY_PAD_INTERNAL (pad);
470 gst_object_ref (internal);
471 GST_OBJECT_UNLOCK (pad);
473 return GST_PROXY_PAD_CAST (internal);
477 * gst_proxy_pad_unlink_default:
478 * @pad: a #GstPad to unlink
480 * Invoke the default unlink function of the proxy pad.
485 gst_proxy_pad_unlink_default (GstPad * pad)
487 /* nothing to do anymore */
488 GST_DEBUG_OBJECT (pad, "pad is unlinked");
492 gst_proxy_pad_class_init (GstProxyPadClass * klass)
494 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
496 /* Register common function pointer descriptions */
497 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
498 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
499 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
500 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
501 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
502 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
503 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
504 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
505 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
506 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
507 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
511 gst_proxy_pad_init (GstProxyPad * ppad)
513 GstPad *pad = (GstPad *) ppad;
515 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
516 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
518 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
519 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
520 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
521 gst_pad_set_iterate_internal_links_function (pad,
522 gst_proxy_pad_iterate_internal_links_default);
524 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
525 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
526 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
527 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
531 /***********************************************************************
532 * Ghost pads, implemented as a pair of proxy pads (sort of)
536 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
538 struct _GstGhostPadPrivate
540 /* with PROXY_LOCK */
541 gboolean constructed;
544 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
546 static void gst_ghost_pad_dispose (GObject * object);
549 * gst_ghost_pad_internal_activate_push_default:
550 * @pad: the #GstPad to activate or deactivate.
551 * @active: whether the pad should be active or not.
553 * Invoke the default activate push function of a proxy pad that is
554 * owned by a ghost pad.
556 * Returns: %TRUE if the operation was successful.
561 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
566 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
568 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
569 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
571 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
572 * will be activated later (or already in case of a ghost sinkpad). */
573 other = GST_PROXY_PAD_INTERNAL (pad);
574 ret = gst_pad_activate_push (other, active);
580 * gst_ghost_pad_internal_activate_pull_default:
581 * @pad: the #GstPad to activate or deactivate.
582 * @active: whether the pad should be active or not.
584 * Invoke the default activate pull function of a proxy pad that is
585 * owned by a ghost pad.
587 * Returns: %TRUE if the operation was successful.
592 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
597 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
599 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
600 GST_DEBUG_PAD_NAME (pad));
602 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
603 /* we are activated in pull mode by our peer element, which is a sinkpad
604 * that wants to operate in pull mode. This activation has to propagate
605 * upstream through the pipeline. We call the internal activation function,
606 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
607 * further upstream */
608 GST_LOG_OBJECT (pad, "pad is src, activate internal");
609 other = GST_PROXY_PAD_INTERNAL (pad);
610 ret = gst_pad_activate_pull (other, active);
611 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
612 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
613 * since we hold a pointer to the upstream peer. */
614 GST_LOG_OBJECT (pad, "activating peer");
615 ret = gst_pad_activate_pull (other, active);
616 gst_object_unref (other);
618 /* this is failure, we can't activate pull if there is no peer */
619 GST_LOG_OBJECT (pad, "not src and no peer, failing");
627 * gst_ghost_pad_activate_push_default:
628 * @pad: the #GstPad to activate or deactivate.
629 * @active: whether the pad should be active or not.
631 * Invoke the default activate push function of a ghost pad.
633 * Returns: %TRUE if the operation was successful.
638 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
643 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
645 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
646 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
648 /* just activate the internal pad */
649 other = GST_PROXY_PAD_INTERNAL (pad);
650 ret = gst_pad_activate_push (other, active);
656 * gst_ghost_pad_activate_pull_default:
657 * @pad: the #GstPad to activate or deactivate.
658 * @active: whether the pad should be active or not.
660 * Invoke the default activate pull function of a ghost pad.
662 * Returns: %TRUE if the operation was successful.
667 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
672 g_return_val_if_fail (GST_IS_GHOST_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 /* the ghostpad is SRC and activated in pull mode by its peer, call the
679 * activation function of the internal pad to propagate the activation
681 GST_LOG_OBJECT (pad, "pad is src, activate internal");
682 other = GST_PROXY_PAD_INTERNAL (pad);
683 ret = gst_pad_activate_pull (other, active);
684 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
685 /* We are SINK and activated by the internal pad, propagate activation
686 * upstream because we hold a ref to the upstream peer */
687 GST_LOG_OBJECT (pad, "activating peer");
688 ret = gst_pad_activate_pull (other, active);
689 gst_object_unref (other);
691 /* no peer, we fail */
692 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
700 * gst_ghost_pad_link_default:
701 * @pad: the #GstPad to link.
702 * @peer: the #GstPad peer
704 * Invoke the default link function of a ghost pad.
706 * Returns: #GstPadLinkReturn of the operation
711 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
713 GstPadLinkReturn ret;
715 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
716 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
718 GST_DEBUG_OBJECT (pad, "linking ghostpad");
720 ret = GST_PAD_LINK_OK;
721 /* if we are a source pad, we should call the peer link function
722 * if the peer has one, see design docs. */
723 if (GST_PAD_IS_SRC (pad)) {
724 if (GST_PAD_LINKFUNC (peer)) {
725 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
726 if (ret != GST_PAD_LINK_OK)
727 GST_DEBUG_OBJECT (pad, "linking failed");
734 * gst_ghost_pad_unlink_default:
735 * @pad: the #GstPad to link.
737 * Invoke the default unlink function of a ghost pad.
742 gst_ghost_pad_unlink_default (GstPad * pad)
744 g_return_if_fail (GST_IS_GHOST_PAD (pad));
746 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
750 gst_ghost_pad_class_init (GstGhostPadClass * klass)
752 GObjectClass *gobject_class = (GObjectClass *) klass;
754 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
756 gobject_class->dispose = gst_ghost_pad_dispose;
758 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
759 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
760 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
764 gst_ghost_pad_init (GstGhostPad * pad)
766 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
767 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
769 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
770 gst_ghost_pad_activate_pull_default);
771 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
772 gst_ghost_pad_activate_push_default);
776 gst_ghost_pad_dispose (GObject * object)
782 pad = GST_PAD (object);
784 GST_DEBUG_OBJECT (pad, "dispose");
786 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
788 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
789 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
790 peer = gst_pad_get_peer (pad);
792 if (GST_PAD_IS_SRC (pad))
793 gst_pad_unlink (pad, peer);
795 gst_pad_unlink (peer, pad);
797 gst_object_unref (peer);
800 GST_OBJECT_LOCK (pad);
801 internal = GST_PROXY_PAD_INTERNAL (pad);
803 gst_pad_set_activatepull_function (internal, NULL);
804 gst_pad_set_activatepush_function (internal, NULL);
806 /* disposes of the internal pad, since the ghostpad is the only possible object
807 * that has a refcount on the internal pad. */
808 gst_object_unparent (GST_OBJECT_CAST (internal));
809 GST_PROXY_PAD_INTERNAL (pad) = NULL;
811 GST_OBJECT_UNLOCK (pad);
813 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
817 * gst_ghost_pad_construct:
818 * @gpad: the newly allocated ghost pad
820 * Finish initialization of a newly allocated ghost pad.
822 * This function is most useful in language bindings and when subclassing
823 * #GstGhostPad; plugin and application developers normally will not call this
824 * function. Call this function directly after a call to g_object_new
825 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
827 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
832 gst_ghost_pad_construct (GstGhostPad * gpad)
834 GstPadDirection dir, otherdir;
835 GstPadTemplate *templ;
836 GstPad *pad, *internal;
838 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
839 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
842 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
844 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
846 pad = GST_PAD (gpad);
848 /* Set directional padfunctions for ghostpad */
849 if (dir == GST_PAD_SINK) {
850 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
851 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
853 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
856 /* link/unlink functions */
857 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
858 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
860 /* INTERNAL PAD, it always exists and is child of the ghostpad */
861 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
864 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
865 "direction", otherdir, "template", templ, NULL);
866 /* release ref obtained via g_object_get */
867 gst_object_unref (templ);
870 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
871 "direction", otherdir, NULL);
873 GST_PAD_UNSET_FLUSHING (internal);
875 /* Set directional padfunctions for internal pad */
876 if (dir == GST_PAD_SRC) {
877 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
878 gst_pad_set_chain_list_function (internal,
879 gst_proxy_pad_chain_list_default);
881 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
884 GST_OBJECT_LOCK (pad);
886 /* now make the ghostpad a parent of the internal pad */
887 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
888 GST_OBJECT_CAST (pad)))
891 /* The ghostpad is the parent of the internal pad and is the only object that
892 * can have a refcount on the internal pad.
893 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
895 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
896 * its refcount on the internal pad in the dispose method by un-parenting it.
897 * This is why we don't take extra refcounts in the assignments below
899 GST_PROXY_PAD_INTERNAL (pad) = internal;
900 GST_PROXY_PAD_INTERNAL (internal) = pad;
902 /* special activation functions for the internal pad */
903 gst_pad_set_activatepull_function (internal,
904 gst_ghost_pad_internal_activate_pull_default);
905 gst_pad_set_activatepush_function (internal,
906 gst_ghost_pad_internal_activate_push_default);
908 GST_OBJECT_UNLOCK (pad);
910 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
916 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
917 GST_DEBUG_PAD_NAME (internal));
918 g_critical ("Could not set internal pad %s:%s",
919 GST_DEBUG_PAD_NAME (internal));
920 GST_OBJECT_UNLOCK (pad);
921 gst_object_unref (internal);
927 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
928 GstPadTemplate * templ)
932 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
934 /* OBJECT CREATION */
936 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
937 "direction", dir, "template", templ, NULL);
939 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
940 "direction", dir, NULL);
943 if (!gst_ghost_pad_construct (ret))
944 goto construct_failed;
946 return GST_PAD_CAST (ret);
950 gst_object_unref (ret);
955 * gst_ghost_pad_new_no_target:
956 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
957 * @dir: the direction of the ghostpad
959 * Create a new ghostpad without a target with the given direction.
960 * A target can be set on the ghostpad later with the
961 * gst_ghost_pad_set_target() function.
963 * The created ghostpad will not have a padtemplate.
965 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
968 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
972 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
974 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
976 ret = gst_ghost_pad_new_full (name, dir, NULL);
983 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
984 * @target: (transfer none): the pad to ghost.
986 * Create a new ghostpad with @target as the target. The direction will be taken
987 * from the target pad. @target must be unlinked.
989 * Will ref the target.
991 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
994 gst_ghost_pad_new (const gchar * name, GstPad * target)
998 g_return_val_if_fail (GST_IS_PAD (target), NULL);
999 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1001 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1002 GST_DEBUG_PAD_NAME (target));
1004 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1005 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1006 goto set_target_failed;
1013 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1014 GST_DEBUG_PAD_NAME (target));
1015 gst_object_unref (ret);
1021 * gst_ghost_pad_new_from_template:
1022 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1023 * @target: (transfer none): the pad to ghost.
1024 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1026 * Create a new ghostpad with @target as the target. The direction will be taken
1027 * from the target pad. The template used on the ghostpad will be @template.
1029 * Will ref the target.
1031 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1037 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1038 GstPadTemplate * templ)
1042 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1043 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1044 g_return_val_if_fail (templ != NULL, NULL);
1045 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1046 GST_PAD_DIRECTION (target), NULL);
1048 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1049 GST_DEBUG_PAD_NAME (target), templ);
1051 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1052 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1053 goto set_target_failed;
1060 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1061 GST_DEBUG_PAD_NAME (target));
1062 gst_object_unref (ret);
1068 * gst_ghost_pad_new_no_target_from_template:
1069 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1070 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1072 * Create a new ghostpad based on @templ, without setting a target. The
1073 * direction will be taken from the @templ.
1075 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1080 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1081 GstPadTemplate * templ)
1085 g_return_val_if_fail (templ != NULL, NULL);
1088 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1094 * gst_ghost_pad_get_target:
1095 * @gpad: the #GstGhostPad
1097 * Get the target pad of @gpad. Unref target pad after usage.
1099 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1100 * has no target set. Unref target pad after usage.
1103 gst_ghost_pad_get_target (GstGhostPad * gpad)
1107 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1109 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1111 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1117 * gst_ghost_pad_set_target:
1118 * @gpad: the #GstGhostPad
1119 * @newtarget: (transfer none) (allow-none): the new pad target
1121 * Set the new target of the ghostpad @gpad. Any existing target
1122 * is unlinked and links to the new target are established. if @newtarget is
1123 * NULL the target will be cleared.
1125 * Returns: (transfer full): TRUE if the new target could be set. This function
1126 * can return FALSE when the internal pads could not be linked.
1129 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1133 GstPadLinkReturn lret;
1135 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1136 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1137 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1139 /* no need for locking, the internal pad's lifecycle is directly linked to the
1141 internal = GST_PROXY_PAD_INTERNAL (gpad);
1144 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1146 GST_DEBUG_OBJECT (gpad, "clearing target");
1148 /* clear old target */
1149 GST_OBJECT_LOCK (gpad);
1150 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1151 GST_OBJECT_UNLOCK (gpad);
1153 /* unlink internal pad */
1154 if (GST_PAD_IS_SRC (internal))
1155 gst_pad_unlink (internal, oldtarget);
1157 gst_pad_unlink (oldtarget, internal);
1159 GST_OBJECT_UNLOCK (gpad);
1163 /* and link to internal pad without any checks */
1164 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1166 if (GST_PAD_IS_SRC (internal))
1168 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1171 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1173 if (lret != GST_PAD_LINK_OK)
1182 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",