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 * @parent: the parent of @pad or NULL
73 * @event: (transfer full): the #GstEvent to send to the pad.
75 * Invoke the default event of the proxy pad.
77 * Returns: TRUE if the event was handled.
82 gst_proxy_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
87 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
88 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
90 internal = GST_PROXY_PAD_INTERNAL (pad);
91 res = gst_pad_push_event (internal, event);
97 gst_proxy_pad_query_caps (GstPad * pad, GstQuery * query)
102 GstPadTemplate *templ;
104 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
106 templ = GST_PAD_PAD_TEMPLATE (pad);
107 target = gst_proxy_pad_get_target (pad);
109 /* if we have a real target, proxy the call */
110 res = gst_pad_query (target, query);
112 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
113 GST_DEBUG_PAD_NAME (target), query);
115 gst_object_unref (target);
117 /* filter against the template */
121 filt = GST_PAD_TEMPLATE_CAPS (templ);
123 gst_query_parse_caps_result (query, &result);
124 tmp = gst_caps_intersect_full (result, filt, GST_CAPS_INTERSECT_FIRST);
125 gst_query_set_caps_result (query, tmp);
126 GST_DEBUG_OBJECT (pad,
127 "filtered against template gives %" GST_PTR_FORMAT, tmp);
128 gst_caps_unref (tmp);
136 gst_query_parse_caps (query, &filter);
138 /* else, if we have a template, use its caps. */
140 result = GST_PAD_TEMPLATE_CAPS (templ);
141 GST_DEBUG_OBJECT (pad,
142 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
146 GstCaps *intersection;
148 GST_DEBUG_OBJECT (pad, "intersect with filter");
150 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
151 gst_query_set_caps_result (query, intersection);
152 gst_caps_unref (intersection);
154 gst_query_set_caps_result (query, result);
159 /* If there's a filter, return that */
160 if (filter != NULL) {
161 GST_DEBUG_OBJECT (pad, "return filter");
162 gst_query_set_caps_result (query, filter);
166 /* last resort, any caps */
167 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
168 result = gst_caps_new_any ();
169 gst_query_set_caps_result (query, result);
170 gst_caps_unref (result);
178 * gst_proxy_pad_query_default:
179 * @pad: a #GstPad to invoke the default query on.
180 * @parent: the parent of @pad or NULL
181 * @query: (transfer none): the #GstQuery to perform.
183 * Invoke the default query function of the proxy pad.
185 * Returns: TRUE if the query could be performed.
190 gst_proxy_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
195 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
196 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
199 switch (GST_QUERY_TYPE (query)) {
200 case GST_QUERY_ACCEPT_CAPS:
202 target = gst_proxy_pad_get_target (pad);
204 res = gst_pad_query (target, query);
205 gst_object_unref (target);
207 GST_DEBUG_OBJECT (pad, "no target");
208 /* We don't have a target, we return TRUE and we assume that any future
209 * target will be able to deal with any configured caps. */
210 gst_query_set_accept_caps_result (query, TRUE);
217 res = gst_proxy_pad_query_caps (pad, query);
222 target = gst_proxy_pad_get_target (pad);
224 res = gst_pad_query (target, query);
225 gst_object_unref (target);
227 GST_DEBUG_OBJECT (pad, "no target pad");
237 * gst_proxy_pad_iterate_internal_links_default:
238 * @pad: the #GstPad to get the internal links of.
239 * @parent: the parent of @pad or NULL
241 * Invoke the default iterate internal links function of the proxy pad.
243 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
244 * returned pad with gst_object_unref().
249 gst_proxy_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
251 GstIterator *res = NULL;
255 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
257 internal = GST_PROXY_PAD_INTERNAL (pad);
258 g_value_init (&v, GST_TYPE_PAD);
259 g_value_set_object (&v, internal);
260 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
267 * gst_proxy_pad_chain_default:
268 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
269 * @parent: the parent of @pad or NULL
270 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
273 * Invoke the default chain function of the proxy pad.
275 * Returns: a #GstFlowReturn from the pad.
280 gst_proxy_pad_chain_default (GstPad * pad, GstObject * parent,
286 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
287 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
289 internal = GST_PROXY_PAD_INTERNAL (pad);
290 res = gst_pad_push (internal, buffer);
296 * gst_proxy_pad_chain_list_default:
297 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
298 * @parent: the parent of @pad or NULL
299 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
302 * Invoke the default chain list function of the proxy pad.
304 * Returns: a #GstFlowReturn from the pad.
309 gst_proxy_pad_chain_list_default (GstPad * pad, GstObject * parent,
310 GstBufferList * list)
315 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
316 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
318 internal = GST_PROXY_PAD_INTERNAL (pad);
319 res = gst_pad_push_list (internal, list);
325 * gst_proxy_pad_getrange_default:
326 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
327 * @parent: the parent of @pad
328 * @offset: The start offset of the buffer
329 * @size: The length of the buffer
330 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
331 * returns #GST_FLOW_ERROR if %NULL.
333 * Invoke the default getrange function of the proxy pad.
335 * Returns: a #GstFlowReturn from the pad.
340 gst_proxy_pad_getrange_default (GstPad * pad, GstObject * parent,
341 guint64 offset, guint size, GstBuffer ** buffer)
346 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
347 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
349 internal = GST_PROXY_PAD_INTERNAL (pad);
350 res = gst_pad_pull_range (internal, offset, size, buffer);
356 gst_proxy_pad_get_target (GstPad * pad)
360 GST_OBJECT_LOCK (pad);
361 target = GST_PROXY_PAD_TARGET (pad);
363 gst_object_ref (target);
364 GST_OBJECT_UNLOCK (pad);
370 * gst_proxy_pad_get_internal:
371 * @pad: the #GstProxyPad
373 * Get the internal pad of @pad. Unref target pad after usage.
375 * The internal pad of a #GstGhostPad is the internally used
376 * pad of opposite direction, which is used to link to the target.
378 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
379 * Unref target pad after usage.
384 gst_proxy_pad_get_internal (GstProxyPad * pad)
388 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
390 GST_OBJECT_LOCK (pad);
391 internal = GST_PROXY_PAD_INTERNAL (pad);
393 gst_object_ref (internal);
394 GST_OBJECT_UNLOCK (pad);
396 return GST_PROXY_PAD_CAST (internal);
400 * gst_proxy_pad_unlink_default:
401 * @pad: a #GstPad to unlink
403 * Invoke the default unlink function of the proxy pad.
408 gst_proxy_pad_unlink_default (GstPad * pad)
410 /* nothing to do anymore */
411 GST_DEBUG_OBJECT (pad, "pad is unlinked");
415 gst_proxy_pad_class_init (GstProxyPadClass * klass)
417 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
419 /* Register common function pointer descriptions */
420 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
422 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
423 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
424 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
425 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
426 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
430 gst_proxy_pad_init (GstProxyPad * ppad)
432 GstPad *pad = (GstPad *) ppad;
434 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
435 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
437 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
438 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
439 gst_pad_set_iterate_internal_links_function (pad,
440 gst_proxy_pad_iterate_internal_links_default);
442 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
446 /***********************************************************************
447 * Ghost pads, implemented as a pair of proxy pads (sort of)
451 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
453 struct _GstGhostPadPrivate
455 /* with PROXY_LOCK */
456 gboolean constructed;
459 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
461 static void gst_ghost_pad_dispose (GObject * object);
464 gst_ghost_pad_internal_activate_push_default (GstPad * pad, GstObject * parent,
470 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
471 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
473 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
474 * will be activated later (or already in case of a ghost sinkpad). */
475 other = GST_PROXY_PAD_INTERNAL (pad);
476 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
482 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
488 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
489 GST_DEBUG_PAD_NAME (pad));
491 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
492 /* we are activated in pull mode by our peer element, which is a sinkpad
493 * that wants to operate in pull mode. This activation has to propagate
494 * upstream through the pipeline. We call the internal activation function,
495 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
496 * further upstream */
497 GST_LOG_OBJECT (pad, "pad is src, activate internal");
498 other = GST_PROXY_PAD_INTERNAL (pad);
499 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
500 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
501 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
502 * since we hold a pointer to the upstream peer. */
503 GST_LOG_OBJECT (pad, "activating peer");
504 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
505 gst_object_unref (other);
507 /* this is failure, we can't activate pull if there is no peer */
508 GST_LOG_OBJECT (pad, "not src and no peer, failing");
516 * gst_ghost_pad_internal_activate_mode_default:
517 * @pad: the #GstPad to activate or deactivate.
518 * @parent: the parent of @pad or NULL
519 * @mode: the requested activation mode
520 * @active: whether the pad should be active or not.
522 * Invoke the default activate mode function of a proxy pad that is
523 * owned by a ghost pad.
525 * Returns: %TRUE if the operation was successful.
528 gst_ghost_pad_internal_activate_mode_default (GstPad * pad, GstObject * parent,
529 GstPadMode mode, gboolean active)
533 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
536 case GST_PAD_MODE_PULL:
537 res = gst_ghost_pad_internal_activate_pull_default (pad, parent, active);
539 case GST_PAD_MODE_PUSH:
540 res = gst_ghost_pad_internal_activate_push_default (pad, parent, active);
543 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
551 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
557 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
559 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
560 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
562 /* just activate the internal pad */
563 other = GST_PROXY_PAD_INTERNAL (pad);
564 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
570 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
576 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
577 GST_DEBUG_PAD_NAME (pad));
579 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
580 /* the ghostpad is SRC and activated in pull mode by its peer, call the
581 * activation function of the internal pad to propagate the activation
583 GST_LOG_OBJECT (pad, "pad is src, activate internal");
584 other = GST_PROXY_PAD_INTERNAL (pad);
585 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
586 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
587 /* We are SINK and activated by the internal pad, propagate activation
588 * upstream because we hold a ref to the upstream peer */
589 GST_LOG_OBJECT (pad, "activating peer");
590 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
591 gst_object_unref (other);
593 /* no peer, we fail */
594 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
602 * gst_ghost_pad_activate_mode_default:
603 * @pad: the #GstPad to activate or deactivate.
604 * @parent: the parent of @pad or NULL
605 * @mode: the requested activation mode
606 * @active: whether the pad should be active or not.
608 * Invoke the default activate mode function of a ghost pad.
610 * Returns: %TRUE if the operation was successful.
613 gst_ghost_pad_activate_mode_default (GstPad * pad, GstObject * parent,
614 GstPadMode mode, gboolean active)
618 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
621 case GST_PAD_MODE_PULL:
622 res = gst_ghost_pad_activate_pull_default (pad, parent, active);
624 case GST_PAD_MODE_PUSH:
625 res = gst_ghost_pad_activate_push_default (pad, parent, active);
628 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
636 * gst_ghost_pad_link_default:
637 * @pad: the #GstPad to link.
638 * @peer: the #GstPad peer
640 * Invoke the default link function of a ghost pad.
642 * Returns: #GstPadLinkReturn of the operation
647 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
649 GstPadLinkReturn ret;
651 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
652 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
654 GST_DEBUG_OBJECT (pad, "linking ghostpad");
656 ret = GST_PAD_LINK_OK;
657 /* if we are a source pad, we should call the peer link function
658 * if the peer has one, see design docs. */
659 if (GST_PAD_IS_SRC (pad)) {
660 if (GST_PAD_LINKFUNC (peer)) {
661 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
662 if (ret != GST_PAD_LINK_OK)
663 GST_DEBUG_OBJECT (pad, "linking failed");
670 * gst_ghost_pad_unlink_default:
671 * @pad: the #GstPad to link.
673 * Invoke the default unlink function of a ghost pad.
678 gst_ghost_pad_unlink_default (GstPad * pad)
680 g_return_if_fail (GST_IS_GHOST_PAD (pad));
682 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
686 gst_ghost_pad_class_init (GstGhostPadClass * klass)
688 GObjectClass *gobject_class = (GObjectClass *) klass;
690 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
692 gobject_class->dispose = gst_ghost_pad_dispose;
694 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
695 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
696 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
700 gst_ghost_pad_init (GstGhostPad * pad)
702 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
703 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
705 gst_pad_set_activatemode_function (GST_PAD_CAST (pad),
706 gst_ghost_pad_activate_mode_default);
710 gst_ghost_pad_dispose (GObject * object)
716 pad = GST_PAD (object);
718 GST_DEBUG_OBJECT (pad, "dispose");
720 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
722 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
723 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
724 peer = gst_pad_get_peer (pad);
726 if (GST_PAD_IS_SRC (pad))
727 gst_pad_unlink (pad, peer);
729 gst_pad_unlink (peer, pad);
731 gst_object_unref (peer);
734 GST_OBJECT_LOCK (pad);
735 internal = GST_PROXY_PAD_INTERNAL (pad);
737 gst_pad_set_activatemode_function (internal, NULL);
739 /* disposes of the internal pad, since the ghostpad is the only possible object
740 * that has a refcount on the internal pad. */
741 gst_object_unparent (GST_OBJECT_CAST (internal));
742 GST_PROXY_PAD_INTERNAL (pad) = NULL;
744 GST_OBJECT_UNLOCK (pad);
746 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
750 * gst_ghost_pad_construct:
751 * @gpad: the newly allocated ghost pad
753 * Finish initialization of a newly allocated ghost pad.
755 * This function is most useful in language bindings and when subclassing
756 * #GstGhostPad; plugin and application developers normally will not call this
757 * function. Call this function directly after a call to g_object_new
758 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
760 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
765 gst_ghost_pad_construct (GstGhostPad * gpad)
767 GstPadDirection dir, otherdir;
768 GstPadTemplate *templ;
769 GstPad *pad, *internal;
771 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
772 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
775 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
777 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
779 pad = GST_PAD (gpad);
781 /* Set directional padfunctions for ghostpad */
782 if (dir == GST_PAD_SINK) {
783 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
784 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
786 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
789 /* link/unlink functions */
790 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
791 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
793 /* INTERNAL PAD, it always exists and is child of the ghostpad */
794 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
797 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
798 "direction", otherdir, "template", templ, NULL);
799 /* release ref obtained via g_object_get */
800 gst_object_unref (templ);
803 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
804 "direction", otherdir, NULL);
806 GST_PAD_UNSET_FLUSHING (internal);
808 /* Set directional padfunctions for internal pad */
809 if (dir == GST_PAD_SRC) {
810 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
811 gst_pad_set_chain_list_function (internal,
812 gst_proxy_pad_chain_list_default);
814 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
817 GST_OBJECT_LOCK (pad);
819 /* now make the ghostpad a parent of the internal pad */
820 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
821 GST_OBJECT_CAST (pad)))
824 /* The ghostpad is the parent of the internal pad and is the only object that
825 * can have a refcount on the internal pad.
826 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
828 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
829 * its refcount on the internal pad in the dispose method by un-parenting it.
830 * This is why we don't take extra refcounts in the assignments below
832 GST_PROXY_PAD_INTERNAL (pad) = internal;
833 GST_PROXY_PAD_INTERNAL (internal) = pad;
835 /* special activation functions for the internal pad */
836 gst_pad_set_activatemode_function (internal,
837 gst_ghost_pad_internal_activate_mode_default);
839 GST_OBJECT_UNLOCK (pad);
841 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
847 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
848 GST_DEBUG_PAD_NAME (internal));
849 g_critical ("Could not set internal pad %s:%s",
850 GST_DEBUG_PAD_NAME (internal));
851 GST_OBJECT_UNLOCK (pad);
852 gst_object_unref (internal);
858 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
859 GstPadTemplate * templ)
863 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
865 /* OBJECT CREATION */
867 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
868 "direction", dir, "template", templ, NULL);
870 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
871 "direction", dir, NULL);
874 if (!gst_ghost_pad_construct (ret))
875 goto construct_failed;
877 return GST_PAD_CAST (ret);
881 gst_object_unref (ret);
886 * gst_ghost_pad_new_no_target:
887 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
888 * @dir: the direction of the ghostpad
890 * Create a new ghostpad without a target with the given direction.
891 * A target can be set on the ghostpad later with the
892 * gst_ghost_pad_set_target() function.
894 * The created ghostpad will not have a padtemplate.
896 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
899 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
903 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
905 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
907 ret = gst_ghost_pad_new_full (name, dir, NULL);
914 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
915 * @target: (transfer none): the pad to ghost.
917 * Create a new ghostpad with @target as the target. The direction will be taken
918 * from the target pad. @target must be unlinked.
920 * Will ref the target.
922 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
925 gst_ghost_pad_new (const gchar * name, GstPad * target)
929 g_return_val_if_fail (GST_IS_PAD (target), NULL);
930 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
932 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
933 GST_DEBUG_PAD_NAME (target));
935 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
936 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
937 goto set_target_failed;
944 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
945 GST_DEBUG_PAD_NAME (target));
946 gst_object_unref (ret);
952 * gst_ghost_pad_new_from_template:
953 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
954 * @target: (transfer none): the pad to ghost.
955 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
957 * Create a new ghostpad with @target as the target. The direction will be taken
958 * from the target pad. The template used on the ghostpad will be @template.
960 * Will ref the target.
962 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
968 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
969 GstPadTemplate * templ)
973 g_return_val_if_fail (GST_IS_PAD (target), NULL);
974 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
975 g_return_val_if_fail (templ != NULL, NULL);
976 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
977 GST_PAD_DIRECTION (target), NULL);
979 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
980 GST_DEBUG_PAD_NAME (target), templ);
982 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
983 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
984 goto set_target_failed;
991 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
992 GST_DEBUG_PAD_NAME (target));
993 gst_object_unref (ret);
999 * gst_ghost_pad_new_no_target_from_template:
1000 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1001 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1003 * Create a new ghostpad based on @templ, without setting a target. The
1004 * direction will be taken from the @templ.
1006 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1011 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1012 GstPadTemplate * templ)
1016 g_return_val_if_fail (templ != NULL, NULL);
1019 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1025 * gst_ghost_pad_get_target:
1026 * @gpad: the #GstGhostPad
1028 * Get the target pad of @gpad. Unref target pad after usage.
1030 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1031 * has no target set. Unref target pad after usage.
1034 gst_ghost_pad_get_target (GstGhostPad * gpad)
1038 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1040 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1042 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1048 * gst_ghost_pad_set_target:
1049 * @gpad: the #GstGhostPad
1050 * @newtarget: (transfer none) (allow-none): the new pad target
1052 * Set the new target of the ghostpad @gpad. Any existing target
1053 * is unlinked and links to the new target are established. if @newtarget is
1054 * NULL the target will be cleared.
1056 * Returns: (transfer full): TRUE if the new target could be set. This function
1057 * can return FALSE when the internal pads could not be linked.
1060 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1064 GstPadLinkReturn lret;
1066 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1067 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1068 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1070 /* no need for locking, the internal pad's lifecycle is directly linked to the
1072 internal = GST_PROXY_PAD_INTERNAL (gpad);
1075 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1077 GST_DEBUG_OBJECT (gpad, "clearing target");
1079 /* clear old target */
1080 GST_OBJECT_LOCK (gpad);
1081 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1082 GST_OBJECT_UNLOCK (gpad);
1084 /* unlink internal pad */
1085 if (GST_PAD_IS_SRC (internal))
1086 gst_pad_unlink (internal, oldtarget);
1088 gst_pad_unlink (oldtarget, internal);
1090 GST_OBJECT_UNLOCK (gpad);
1094 /* and link to internal pad without any checks */
1095 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1097 if (GST_PAD_IS_SRC (internal))
1099 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1102 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1104 if (lret != GST_PAD_LINK_OK)
1113 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",