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_event_default:
71 * @pad: a #GstPad to push the event to.
72 * @event: (transfer full): the #GstEvent to send to the pad.
74 * Invoke the default event of the proxy pad.
76 * Returns: TRUE if the event was handled.
81 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
86 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
87 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
89 internal = GST_PROXY_PAD_INTERNAL (pad);
90 res = gst_pad_push_event (internal, event);
96 * gst_proxy_pad_query_default:
97 * @pad: a #GstPad to invoke the default query on.
98 * @query: (transfer none): the #GstQuery to perform.
100 * Invoke the default query function of the proxy pad.
102 * Returns: TRUE if the query could be performed.
107 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
112 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
113 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
115 target = gst_proxy_pad_get_target (pad);
117 switch (GST_QUERY_TYPE (query)) {
118 case GST_QUERY_ACCEPT_CAPS:
121 res = gst_pad_query (target, query);
122 gst_object_unref (target);
124 GST_DEBUG_OBJECT (pad, "no target");
125 /* We don't have a target, we return TRUE and we assume that any future
126 * target will be able to deal with any configured caps. */
134 res = gst_pad_query (target, query);
135 gst_object_unref (target);
137 GST_DEBUG_OBJECT (pad, "no target pad");
147 * gst_proyx_pad_iterate_internal_links_default:
148 * @pad: the #GstPad to get the internal links of.
150 * Invoke the default iterate internal links function of the proxy pad.
152 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
153 * returned pad with gst_object_unref().
158 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
160 GstIterator *res = NULL;
164 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
166 internal = GST_PROXY_PAD_INTERNAL (pad);
167 g_value_init (&v, GST_TYPE_PAD);
168 g_value_set_object (&v, internal);
169 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
176 * gst_proxy_pad_chain_default:
177 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
178 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
181 * Invoke the default chain function of the proxy pad.
183 * Returns: a #GstFlowReturn from the pad.
188 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
193 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
194 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
196 internal = GST_PROXY_PAD_INTERNAL (pad);
197 res = gst_pad_push (internal, buffer);
203 * gst_proxy_pad_chain_list_default:
204 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
205 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
208 * Invoke the default chain list function of the proxy pad.
210 * Returns: a #GstFlowReturn from the pad.
215 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
220 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
221 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
223 internal = GST_PROXY_PAD_INTERNAL (pad);
224 res = gst_pad_push_list (internal, list);
230 * gst_proxy_pad_get_range_default:
231 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
232 * @offset: The start offset of the buffer
233 * @size: The length of the buffer
234 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
235 * returns #GST_FLOW_ERROR if %NULL.
237 * Invoke the default getrange function of the proxy pad.
239 * Returns: a #GstFlowReturn from the pad.
244 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
250 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
251 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
253 internal = GST_PROXY_PAD_INTERNAL (pad);
254 res = gst_pad_pull_range (internal, offset, size, buffer);
260 * gst_proxy_pad_getcaps_default:
261 * @pad: a #GstPad to get the capabilities of.
262 * @filter: a #GstCaps filter.
264 * Invoke the default getcaps function of the proxy pad.
266 * Returns: (transfer full): the caps of the pad with incremented ref-count
271 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
275 GstPadTemplate *templ;
277 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
279 templ = GST_PAD_PAD_TEMPLATE (pad);
280 target = gst_proxy_pad_get_target (pad);
282 /* if we have a real target, proxy the call */
283 res = gst_pad_get_caps (target, filter);
285 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
286 GST_DEBUG_PAD_NAME (target), res);
288 gst_object_unref (target);
290 /* filter against the template */
294 filt = GST_PAD_TEMPLATE_CAPS (templ);
296 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
297 gst_caps_unref (res);
299 GST_DEBUG_OBJECT (pad,
300 "filtered against template gives %" GST_PTR_FORMAT, res);
304 /* else, if we have a template, use its caps. */
306 res = GST_PAD_TEMPLATE_CAPS (templ);
307 GST_DEBUG_OBJECT (pad,
308 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
310 res = gst_caps_ref (res);
313 GstCaps *intersection =
314 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
316 gst_caps_unref (res);
323 /* If there's a filter, return that */
324 if (filter != NULL) {
325 res = gst_caps_ref (filter);
329 /* last resort, any caps */
330 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
331 res = gst_caps_new_any ();
339 * gst_proxy_pad_acceptcaps_default:
340 * @pad: a #GstPad to check
341 * @caps: a #GstCaps to check on the pad
343 * Invoke the default acceptcaps function of the proxy pad.
345 * Returns: TRUE if the pad can accept the caps.
350 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
355 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
356 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
358 target = gst_proxy_pad_get_target (pad);
360 res = gst_pad_accept_caps (target, caps);
361 gst_object_unref (target);
363 GST_DEBUG_OBJECT (pad, "no target");
364 /* We don't have a target, we return TRUE and we assume that any future
365 * target will be able to deal with any configured caps. */
373 gst_proxy_pad_get_target (GstPad * pad)
377 GST_OBJECT_LOCK (pad);
378 target = GST_PROXY_PAD_TARGET (pad);
380 gst_object_ref (target);
381 GST_OBJECT_UNLOCK (pad);
387 * gst_proxy_pad_get_internal:
388 * @pad: the #GstProxyPad
390 * Get the internal pad of @pad. Unref target pad after usage.
392 * The internal pad of a #GstGhostPad is the internally used
393 * pad of opposite direction, which is used to link to the target.
395 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
396 * Unref target pad after usage.
401 gst_proxy_pad_get_internal (GstProxyPad * pad)
405 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
407 GST_OBJECT_LOCK (pad);
408 internal = GST_PROXY_PAD_INTERNAL (pad);
410 gst_object_ref (internal);
411 GST_OBJECT_UNLOCK (pad);
413 return GST_PROXY_PAD_CAST (internal);
417 * gst_proxy_pad_unlink_default:
418 * @pad: a #GstPad to unlink
420 * Invoke the default unlink function of the proxy pad.
425 gst_proxy_pad_unlink_default (GstPad * pad)
427 /* nothing to do anymore */
428 GST_DEBUG_OBJECT (pad, "pad is unlinked");
432 gst_proxy_pad_class_init (GstProxyPadClass * klass)
434 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
436 /* Register common function pointer descriptions */
437 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
438 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
439 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
440 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
441 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
442 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
443 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
444 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
445 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
449 gst_proxy_pad_init (GstProxyPad * ppad)
451 GstPad *pad = (GstPad *) ppad;
453 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
454 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
456 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
457 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
458 gst_pad_set_iterate_internal_links_function (pad,
459 gst_proxy_pad_iterate_internal_links_default);
461 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
462 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
466 /***********************************************************************
467 * Ghost pads, implemented as a pair of proxy pads (sort of)
471 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
473 struct _GstGhostPadPrivate
475 /* with PROXY_LOCK */
476 gboolean constructed;
479 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
481 static void gst_ghost_pad_dispose (GObject * object);
484 * gst_ghost_pad_internal_activate_push_default:
485 * @pad: the #GstPad to activate or deactivate.
486 * @active: whether the pad should be active or not.
488 * Invoke the default activate push function of a proxy pad that is
489 * owned by a ghost pad.
491 * Returns: %TRUE if the operation was successful.
496 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
501 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
503 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
504 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
506 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
507 * will be activated later (or already in case of a ghost sinkpad). */
508 other = GST_PROXY_PAD_INTERNAL (pad);
509 ret = gst_pad_activate_push (other, active);
515 * gst_ghost_pad_internal_activate_pull_default:
516 * @pad: the #GstPad to activate or deactivate.
517 * @active: whether the pad should be active or not.
519 * Invoke the default activate pull function of a proxy pad that is
520 * owned by a ghost pad.
522 * Returns: %TRUE if the operation was successful.
527 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
532 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
534 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
535 GST_DEBUG_PAD_NAME (pad));
537 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
538 /* we are activated in pull mode by our peer element, which is a sinkpad
539 * that wants to operate in pull mode. This activation has to propagate
540 * upstream through the pipeline. We call the internal activation function,
541 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
542 * further upstream */
543 GST_LOG_OBJECT (pad, "pad is src, activate internal");
544 other = GST_PROXY_PAD_INTERNAL (pad);
545 ret = gst_pad_activate_pull (other, active);
546 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
547 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
548 * since we hold a pointer to the upstream peer. */
549 GST_LOG_OBJECT (pad, "activating peer");
550 ret = gst_pad_activate_pull (other, active);
551 gst_object_unref (other);
553 /* this is failure, we can't activate pull if there is no peer */
554 GST_LOG_OBJECT (pad, "not src and no peer, failing");
562 * gst_ghost_pad_activate_push_default:
563 * @pad: the #GstPad to activate or deactivate.
564 * @active: whether the pad should be active or not.
566 * Invoke the default activate push function of a ghost pad.
568 * Returns: %TRUE if the operation was successful.
573 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
578 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
580 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
581 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
583 /* just activate the internal pad */
584 other = GST_PROXY_PAD_INTERNAL (pad);
585 ret = gst_pad_activate_push (other, active);
591 * gst_ghost_pad_activate_pull_default:
592 * @pad: the #GstPad to activate or deactivate.
593 * @active: whether the pad should be active or not.
595 * Invoke the default activate pull function of a ghost pad.
597 * Returns: %TRUE if the operation was successful.
602 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
607 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
609 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
610 GST_DEBUG_PAD_NAME (pad));
612 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
613 /* the ghostpad is SRC and activated in pull mode by its peer, call the
614 * activation function of the internal pad to propagate the activation
616 GST_LOG_OBJECT (pad, "pad is src, activate internal");
617 other = GST_PROXY_PAD_INTERNAL (pad);
618 ret = gst_pad_activate_pull (other, active);
619 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
620 /* We are SINK and activated by the internal pad, propagate activation
621 * upstream because we hold a ref to the upstream peer */
622 GST_LOG_OBJECT (pad, "activating peer");
623 ret = gst_pad_activate_pull (other, active);
624 gst_object_unref (other);
626 /* no peer, we fail */
627 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
635 * gst_ghost_pad_link_default:
636 * @pad: the #GstPad to link.
637 * @peer: the #GstPad peer
639 * Invoke the default link function of a ghost pad.
641 * Returns: #GstPadLinkReturn of the operation
646 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
648 GstPadLinkReturn ret;
650 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
651 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
653 GST_DEBUG_OBJECT (pad, "linking ghostpad");
655 ret = GST_PAD_LINK_OK;
656 /* if we are a source pad, we should call the peer link function
657 * if the peer has one, see design docs. */
658 if (GST_PAD_IS_SRC (pad)) {
659 if (GST_PAD_LINKFUNC (peer)) {
660 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
661 if (ret != GST_PAD_LINK_OK)
662 GST_DEBUG_OBJECT (pad, "linking failed");
669 * gst_ghost_pad_unlink_default:
670 * @pad: the #GstPad to link.
672 * Invoke the default unlink function of a ghost pad.
677 gst_ghost_pad_unlink_default (GstPad * pad)
679 g_return_if_fail (GST_IS_GHOST_PAD (pad));
681 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
685 gst_ghost_pad_class_init (GstGhostPadClass * klass)
687 GObjectClass *gobject_class = (GObjectClass *) klass;
689 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
691 gobject_class->dispose = gst_ghost_pad_dispose;
693 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
694 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
695 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
699 gst_ghost_pad_init (GstGhostPad * pad)
701 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
702 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
704 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
705 gst_ghost_pad_activate_pull_default);
706 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
707 gst_ghost_pad_activate_push_default);
711 gst_ghost_pad_dispose (GObject * object)
717 pad = GST_PAD (object);
719 GST_DEBUG_OBJECT (pad, "dispose");
721 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
723 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
724 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
725 peer = gst_pad_get_peer (pad);
727 if (GST_PAD_IS_SRC (pad))
728 gst_pad_unlink (pad, peer);
730 gst_pad_unlink (peer, pad);
732 gst_object_unref (peer);
735 GST_OBJECT_LOCK (pad);
736 internal = GST_PROXY_PAD_INTERNAL (pad);
738 gst_pad_set_activatepull_function (internal, NULL);
739 gst_pad_set_activatepush_function (internal, NULL);
741 /* disposes of the internal pad, since the ghostpad is the only possible object
742 * that has a refcount on the internal pad. */
743 gst_object_unparent (GST_OBJECT_CAST (internal));
744 GST_PROXY_PAD_INTERNAL (pad) = NULL;
746 GST_OBJECT_UNLOCK (pad);
748 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
752 * gst_ghost_pad_construct:
753 * @gpad: the newly allocated ghost pad
755 * Finish initialization of a newly allocated ghost pad.
757 * This function is most useful in language bindings and when subclassing
758 * #GstGhostPad; plugin and application developers normally will not call this
759 * function. Call this function directly after a call to g_object_new
760 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
762 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
767 gst_ghost_pad_construct (GstGhostPad * gpad)
769 GstPadDirection dir, otherdir;
770 GstPadTemplate *templ;
771 GstPad *pad, *internal;
773 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
774 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
777 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
779 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
781 pad = GST_PAD (gpad);
783 /* Set directional padfunctions for ghostpad */
784 if (dir == GST_PAD_SINK) {
785 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
786 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
788 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
791 /* link/unlink functions */
792 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
793 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
795 /* INTERNAL PAD, it always exists and is child of the ghostpad */
796 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
799 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
800 "direction", otherdir, "template", templ, NULL);
801 /* release ref obtained via g_object_get */
802 gst_object_unref (templ);
805 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
806 "direction", otherdir, NULL);
808 GST_PAD_UNSET_FLUSHING (internal);
810 /* Set directional padfunctions for internal pad */
811 if (dir == GST_PAD_SRC) {
812 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
813 gst_pad_set_chain_list_function (internal,
814 gst_proxy_pad_chain_list_default);
816 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
819 GST_OBJECT_LOCK (pad);
821 /* now make the ghostpad a parent of the internal pad */
822 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
823 GST_OBJECT_CAST (pad)))
826 /* The ghostpad is the parent of the internal pad and is the only object that
827 * can have a refcount on the internal pad.
828 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
830 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
831 * its refcount on the internal pad in the dispose method by un-parenting it.
832 * This is why we don't take extra refcounts in the assignments below
834 GST_PROXY_PAD_INTERNAL (pad) = internal;
835 GST_PROXY_PAD_INTERNAL (internal) = pad;
837 /* special activation functions for the internal pad */
838 gst_pad_set_activatepull_function (internal,
839 gst_ghost_pad_internal_activate_pull_default);
840 gst_pad_set_activatepush_function (internal,
841 gst_ghost_pad_internal_activate_push_default);
843 GST_OBJECT_UNLOCK (pad);
845 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
851 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
852 GST_DEBUG_PAD_NAME (internal));
853 g_critical ("Could not set internal pad %s:%s",
854 GST_DEBUG_PAD_NAME (internal));
855 GST_OBJECT_UNLOCK (pad);
856 gst_object_unref (internal);
862 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
863 GstPadTemplate * templ)
867 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
869 /* OBJECT CREATION */
871 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
872 "direction", dir, "template", templ, NULL);
874 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
875 "direction", dir, NULL);
878 if (!gst_ghost_pad_construct (ret))
879 goto construct_failed;
881 return GST_PAD_CAST (ret);
885 gst_object_unref (ret);
890 * gst_ghost_pad_new_no_target:
891 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
892 * @dir: the direction of the ghostpad
894 * Create a new ghostpad without a target with the given direction.
895 * A target can be set on the ghostpad later with the
896 * gst_ghost_pad_set_target() function.
898 * The created ghostpad will not have a padtemplate.
900 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
903 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
907 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
909 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
911 ret = gst_ghost_pad_new_full (name, dir, NULL);
918 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
919 * @target: (transfer none): the pad to ghost.
921 * Create a new ghostpad with @target as the target. The direction will be taken
922 * from the target pad. @target must be unlinked.
924 * Will ref the target.
926 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
929 gst_ghost_pad_new (const gchar * name, GstPad * target)
933 g_return_val_if_fail (GST_IS_PAD (target), NULL);
934 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
936 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
937 GST_DEBUG_PAD_NAME (target));
939 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
940 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
941 goto set_target_failed;
948 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
949 GST_DEBUG_PAD_NAME (target));
950 gst_object_unref (ret);
956 * gst_ghost_pad_new_from_template:
957 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
958 * @target: (transfer none): the pad to ghost.
959 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
961 * Create a new ghostpad with @target as the target. The direction will be taken
962 * from the target pad. The template used on the ghostpad will be @template.
964 * Will ref the target.
966 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
972 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
973 GstPadTemplate * templ)
977 g_return_val_if_fail (GST_IS_PAD (target), NULL);
978 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
979 g_return_val_if_fail (templ != NULL, NULL);
980 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
981 GST_PAD_DIRECTION (target), NULL);
983 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
984 GST_DEBUG_PAD_NAME (target), templ);
986 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
987 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
988 goto set_target_failed;
995 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
996 GST_DEBUG_PAD_NAME (target));
997 gst_object_unref (ret);
1003 * gst_ghost_pad_new_no_target_from_template:
1004 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1005 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1007 * Create a new ghostpad based on @templ, without setting a target. The
1008 * direction will be taken from the @templ.
1010 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1015 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1016 GstPadTemplate * templ)
1020 g_return_val_if_fail (templ != NULL, NULL);
1023 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1029 * gst_ghost_pad_get_target:
1030 * @gpad: the #GstGhostPad
1032 * Get the target pad of @gpad. Unref target pad after usage.
1034 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1035 * has no target set. Unref target pad after usage.
1038 gst_ghost_pad_get_target (GstGhostPad * gpad)
1042 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1044 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1046 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1052 * gst_ghost_pad_set_target:
1053 * @gpad: the #GstGhostPad
1054 * @newtarget: (transfer none) (allow-none): the new pad target
1056 * Set the new target of the ghostpad @gpad. Any existing target
1057 * is unlinked and links to the new target are established. if @newtarget is
1058 * NULL the target will be cleared.
1060 * Returns: (transfer full): TRUE if the new target could be set. This function
1061 * can return FALSE when the internal pads could not be linked.
1064 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1068 GstPadLinkReturn lret;
1070 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1071 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1072 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1074 /* no need for locking, the internal pad's lifecycle is directly linked to the
1076 internal = GST_PROXY_PAD_INTERNAL (gpad);
1079 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1081 GST_DEBUG_OBJECT (gpad, "clearing target");
1083 /* clear old target */
1084 GST_OBJECT_LOCK (gpad);
1085 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1086 GST_OBJECT_UNLOCK (gpad);
1088 /* unlink internal pad */
1089 if (GST_PAD_IS_SRC (internal))
1090 gst_pad_unlink (internal, oldtarget);
1092 gst_pad_unlink (oldtarget, internal);
1094 GST_OBJECT_UNLOCK (gpad);
1098 /* and link to internal pad without any checks */
1099 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1101 if (GST_PAD_IS_SRC (internal))
1103 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1106 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1108 if (lret != GST_PAD_LINK_OK)
1117 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",