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. */
216 res = gst_proxy_pad_query_caps (pad, query);
221 target = gst_proxy_pad_get_target (pad);
223 res = gst_pad_query (target, query);
224 gst_object_unref (target);
226 GST_DEBUG_OBJECT (pad, "no target pad");
236 * gst_proyx_pad_iterate_internal_links_default:
237 * @pad: the #GstPad to get the internal links of.
238 * @parent: the parent of @pad or NULL
240 * Invoke the default iterate internal links function of the proxy pad.
242 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
243 * returned pad with gst_object_unref().
248 gst_proxy_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
250 GstIterator *res = NULL;
254 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
256 internal = GST_PROXY_PAD_INTERNAL (pad);
257 g_value_init (&v, GST_TYPE_PAD);
258 g_value_set_object (&v, internal);
259 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
266 * gst_proxy_pad_chain_default:
267 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
268 * @parent: the parent of @pad or NULL
269 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
272 * Invoke the default chain function of the proxy pad.
274 * Returns: a #GstFlowReturn from the pad.
279 gst_proxy_pad_chain_default (GstPad * pad, GstObject * parent,
285 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
286 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
288 internal = GST_PROXY_PAD_INTERNAL (pad);
289 res = gst_pad_push (internal, buffer);
295 * gst_proxy_pad_chain_list_default:
296 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
297 * @parent: the parent of @pad or NULL
298 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
301 * Invoke the default chain list function of the proxy pad.
303 * Returns: a #GstFlowReturn from the pad.
308 gst_proxy_pad_chain_list_default (GstPad * pad, GstObject * parent,
309 GstBufferList * list)
314 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
315 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
317 internal = GST_PROXY_PAD_INTERNAL (pad);
318 res = gst_pad_push_list (internal, list);
324 * gst_proxy_pad_get_range_default:
325 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
326 * @offset: The start offset of the buffer
327 * @size: The length of the buffer
328 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
329 * returns #GST_FLOW_ERROR if %NULL.
331 * Invoke the default getrange function of the proxy pad.
333 * Returns: a #GstFlowReturn from the pad.
338 gst_proxy_pad_getrange_default (GstPad * pad, GstObject * parent,
339 guint64 offset, guint size, GstBuffer ** buffer)
344 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
345 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
347 internal = GST_PROXY_PAD_INTERNAL (pad);
348 res = gst_pad_pull_range (internal, offset, size, buffer);
354 gst_proxy_pad_get_target (GstPad * pad)
358 GST_OBJECT_LOCK (pad);
359 target = GST_PROXY_PAD_TARGET (pad);
361 gst_object_ref (target);
362 GST_OBJECT_UNLOCK (pad);
368 * gst_proxy_pad_get_internal:
369 * @pad: the #GstProxyPad
371 * Get the internal pad of @pad. Unref target pad after usage.
373 * The internal pad of a #GstGhostPad is the internally used
374 * pad of opposite direction, which is used to link to the target.
376 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
377 * Unref target pad after usage.
382 gst_proxy_pad_get_internal (GstProxyPad * pad)
386 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
388 GST_OBJECT_LOCK (pad);
389 internal = GST_PROXY_PAD_INTERNAL (pad);
391 gst_object_ref (internal);
392 GST_OBJECT_UNLOCK (pad);
394 return GST_PROXY_PAD_CAST (internal);
398 * gst_proxy_pad_unlink_default:
399 * @pad: a #GstPad to unlink
401 * Invoke the default unlink function of the proxy pad.
406 gst_proxy_pad_unlink_default (GstPad * pad)
408 /* nothing to do anymore */
409 GST_DEBUG_OBJECT (pad, "pad is unlinked");
413 gst_proxy_pad_class_init (GstProxyPadClass * klass)
415 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
417 /* Register common function pointer descriptions */
418 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
422 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
423 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
424 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
428 gst_proxy_pad_init (GstProxyPad * ppad)
430 GstPad *pad = (GstPad *) ppad;
432 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
433 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
435 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
436 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
437 gst_pad_set_iterate_internal_links_function (pad,
438 gst_proxy_pad_iterate_internal_links_default);
440 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
444 /***********************************************************************
445 * Ghost pads, implemented as a pair of proxy pads (sort of)
449 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
451 struct _GstGhostPadPrivate
453 /* with PROXY_LOCK */
454 gboolean constructed;
457 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
459 static void gst_ghost_pad_dispose (GObject * object);
462 * gst_ghost_pad_internal_activate_push_default:
463 * @pad: the #GstPad to activate or deactivate.
464 * @parent: the parent of @pad or NULL
465 * @active: whether the pad should be active or not.
467 * Invoke the default activate push function of a proxy pad that is
468 * owned by a ghost pad.
470 * Returns: %TRUE if the operation was successful.
475 gst_ghost_pad_internal_activate_push_default (GstPad * pad, GstObject * parent,
481 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
483 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
484 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
486 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
487 * will be activated later (or already in case of a ghost sinkpad). */
488 other = GST_PROXY_PAD_INTERNAL (pad);
489 ret = gst_pad_activate_push (other, active);
495 * gst_ghost_pad_internal_activate_pull_default:
496 * @pad: the #GstPad to activate or deactivate.
497 * @parent: the parent of @pad or NULL
498 * @active: whether the pad should be active or not.
500 * Invoke the default activate pull function of a proxy pad that is
501 * owned by a ghost pad.
503 * Returns: %TRUE if the operation was successful.
508 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
514 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
516 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
517 GST_DEBUG_PAD_NAME (pad));
519 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
520 /* we are activated in pull mode by our peer element, which is a sinkpad
521 * that wants to operate in pull mode. This activation has to propagate
522 * upstream through the pipeline. We call the internal activation function,
523 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
524 * further upstream */
525 GST_LOG_OBJECT (pad, "pad is src, activate internal");
526 other = GST_PROXY_PAD_INTERNAL (pad);
527 ret = gst_pad_activate_pull (other, active);
528 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
529 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
530 * since we hold a pointer to the upstream peer. */
531 GST_LOG_OBJECT (pad, "activating peer");
532 ret = gst_pad_activate_pull (other, active);
533 gst_object_unref (other);
535 /* this is failure, we can't activate pull if there is no peer */
536 GST_LOG_OBJECT (pad, "not src and no peer, failing");
544 * gst_ghost_pad_activate_push_default:
545 * @pad: the #GstPad to activate or deactivate.
546 * @parent: the parent of @pad or NULL
547 * @active: whether the pad should be active or not.
549 * Invoke the default activate push function of a ghost pad.
551 * Returns: %TRUE if the operation was successful.
556 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
562 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
564 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
565 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
567 /* just activate the internal pad */
568 other = GST_PROXY_PAD_INTERNAL (pad);
569 ret = gst_pad_activate_push (other, active);
575 * gst_ghost_pad_activate_pull_default:
576 * @pad: the #GstPad to activate or deactivate.
577 * @parent: the parent of @pad or NULL
578 * @active: whether the pad should be active or not.
580 * Invoke the default activate pull function of a ghost pad.
582 * Returns: %TRUE if the operation was successful.
587 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
593 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
595 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
596 GST_DEBUG_PAD_NAME (pad));
598 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
599 /* the ghostpad is SRC and activated in pull mode by its peer, call the
600 * activation function of the internal pad to propagate the activation
602 GST_LOG_OBJECT (pad, "pad is src, activate internal");
603 other = GST_PROXY_PAD_INTERNAL (pad);
604 ret = gst_pad_activate_pull (other, active);
605 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
606 /* We are SINK and activated by the internal pad, propagate activation
607 * upstream because we hold a ref to the upstream peer */
608 GST_LOG_OBJECT (pad, "activating peer");
609 ret = gst_pad_activate_pull (other, active);
610 gst_object_unref (other);
612 /* no peer, we fail */
613 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
621 * gst_ghost_pad_link_default:
622 * @pad: the #GstPad to link.
623 * @peer: the #GstPad peer
625 * Invoke the default link function of a ghost pad.
627 * Returns: #GstPadLinkReturn of the operation
632 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
634 GstPadLinkReturn ret;
636 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
637 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
639 GST_DEBUG_OBJECT (pad, "linking ghostpad");
641 ret = GST_PAD_LINK_OK;
642 /* if we are a source pad, we should call the peer link function
643 * if the peer has one, see design docs. */
644 if (GST_PAD_IS_SRC (pad)) {
645 if (GST_PAD_LINKFUNC (peer)) {
646 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
647 if (ret != GST_PAD_LINK_OK)
648 GST_DEBUG_OBJECT (pad, "linking failed");
655 * gst_ghost_pad_unlink_default:
656 * @pad: the #GstPad to link.
658 * Invoke the default unlink function of a ghost pad.
663 gst_ghost_pad_unlink_default (GstPad * pad)
665 g_return_if_fail (GST_IS_GHOST_PAD (pad));
667 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
671 gst_ghost_pad_class_init (GstGhostPadClass * klass)
673 GObjectClass *gobject_class = (GObjectClass *) klass;
675 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
677 gobject_class->dispose = gst_ghost_pad_dispose;
679 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
680 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
681 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
685 gst_ghost_pad_init (GstGhostPad * pad)
687 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
688 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
690 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
691 gst_ghost_pad_activate_pull_default);
692 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
693 gst_ghost_pad_activate_push_default);
697 gst_ghost_pad_dispose (GObject * object)
703 pad = GST_PAD (object);
705 GST_DEBUG_OBJECT (pad, "dispose");
707 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
709 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
710 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
711 peer = gst_pad_get_peer (pad);
713 if (GST_PAD_IS_SRC (pad))
714 gst_pad_unlink (pad, peer);
716 gst_pad_unlink (peer, pad);
718 gst_object_unref (peer);
721 GST_OBJECT_LOCK (pad);
722 internal = GST_PROXY_PAD_INTERNAL (pad);
724 gst_pad_set_activatepull_function (internal, NULL);
725 gst_pad_set_activatepush_function (internal, NULL);
727 /* disposes of the internal pad, since the ghostpad is the only possible object
728 * that has a refcount on the internal pad. */
729 gst_object_unparent (GST_OBJECT_CAST (internal));
730 GST_PROXY_PAD_INTERNAL (pad) = NULL;
732 GST_OBJECT_UNLOCK (pad);
734 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
738 * gst_ghost_pad_construct:
739 * @gpad: the newly allocated ghost pad
741 * Finish initialization of a newly allocated ghost pad.
743 * This function is most useful in language bindings and when subclassing
744 * #GstGhostPad; plugin and application developers normally will not call this
745 * function. Call this function directly after a call to g_object_new
746 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
748 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
753 gst_ghost_pad_construct (GstGhostPad * gpad)
755 GstPadDirection dir, otherdir;
756 GstPadTemplate *templ;
757 GstPad *pad, *internal;
759 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
760 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
763 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
765 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
767 pad = GST_PAD (gpad);
769 /* Set directional padfunctions for ghostpad */
770 if (dir == GST_PAD_SINK) {
771 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
772 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
774 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
777 /* link/unlink functions */
778 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
779 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
781 /* INTERNAL PAD, it always exists and is child of the ghostpad */
782 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
785 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
786 "direction", otherdir, "template", templ, NULL);
787 /* release ref obtained via g_object_get */
788 gst_object_unref (templ);
791 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
792 "direction", otherdir, NULL);
794 GST_PAD_UNSET_FLUSHING (internal);
796 /* Set directional padfunctions for internal pad */
797 if (dir == GST_PAD_SRC) {
798 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
799 gst_pad_set_chain_list_function (internal,
800 gst_proxy_pad_chain_list_default);
802 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
805 GST_OBJECT_LOCK (pad);
807 /* now make the ghostpad a parent of the internal pad */
808 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
809 GST_OBJECT_CAST (pad)))
812 /* The ghostpad is the parent of the internal pad and is the only object that
813 * can have a refcount on the internal pad.
814 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
816 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
817 * its refcount on the internal pad in the dispose method by un-parenting it.
818 * This is why we don't take extra refcounts in the assignments below
820 GST_PROXY_PAD_INTERNAL (pad) = internal;
821 GST_PROXY_PAD_INTERNAL (internal) = pad;
823 /* special activation functions for the internal pad */
824 gst_pad_set_activatepull_function (internal,
825 gst_ghost_pad_internal_activate_pull_default);
826 gst_pad_set_activatepush_function (internal,
827 gst_ghost_pad_internal_activate_push_default);
829 GST_OBJECT_UNLOCK (pad);
831 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
837 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
838 GST_DEBUG_PAD_NAME (internal));
839 g_critical ("Could not set internal pad %s:%s",
840 GST_DEBUG_PAD_NAME (internal));
841 GST_OBJECT_UNLOCK (pad);
842 gst_object_unref (internal);
848 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
849 GstPadTemplate * templ)
853 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
855 /* OBJECT CREATION */
857 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
858 "direction", dir, "template", templ, NULL);
860 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
861 "direction", dir, NULL);
864 if (!gst_ghost_pad_construct (ret))
865 goto construct_failed;
867 return GST_PAD_CAST (ret);
871 gst_object_unref (ret);
876 * gst_ghost_pad_new_no_target:
877 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
878 * @dir: the direction of the ghostpad
880 * Create a new ghostpad without a target with the given direction.
881 * A target can be set on the ghostpad later with the
882 * gst_ghost_pad_set_target() function.
884 * The created ghostpad will not have a padtemplate.
886 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
889 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
893 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
895 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
897 ret = gst_ghost_pad_new_full (name, dir, NULL);
904 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
905 * @target: (transfer none): the pad to ghost.
907 * Create a new ghostpad with @target as the target. The direction will be taken
908 * from the target pad. @target must be unlinked.
910 * Will ref the target.
912 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
915 gst_ghost_pad_new (const gchar * name, GstPad * target)
919 g_return_val_if_fail (GST_IS_PAD (target), NULL);
920 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
922 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
923 GST_DEBUG_PAD_NAME (target));
925 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
926 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
927 goto set_target_failed;
934 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
935 GST_DEBUG_PAD_NAME (target));
936 gst_object_unref (ret);
942 * gst_ghost_pad_new_from_template:
943 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
944 * @target: (transfer none): the pad to ghost.
945 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
947 * Create a new ghostpad with @target as the target. The direction will be taken
948 * from the target pad. The template used on the ghostpad will be @template.
950 * Will ref the target.
952 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
958 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
959 GstPadTemplate * templ)
963 g_return_val_if_fail (GST_IS_PAD (target), NULL);
964 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
965 g_return_val_if_fail (templ != NULL, NULL);
966 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
967 GST_PAD_DIRECTION (target), NULL);
969 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
970 GST_DEBUG_PAD_NAME (target), templ);
972 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
973 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
974 goto set_target_failed;
981 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
982 GST_DEBUG_PAD_NAME (target));
983 gst_object_unref (ret);
989 * gst_ghost_pad_new_no_target_from_template:
990 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
991 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
993 * Create a new ghostpad based on @templ, without setting a target. The
994 * direction will be taken from the @templ.
996 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1001 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1002 GstPadTemplate * templ)
1006 g_return_val_if_fail (templ != NULL, NULL);
1009 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1015 * gst_ghost_pad_get_target:
1016 * @gpad: the #GstGhostPad
1018 * Get the target pad of @gpad. Unref target pad after usage.
1020 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1021 * has no target set. Unref target pad after usage.
1024 gst_ghost_pad_get_target (GstGhostPad * gpad)
1028 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1030 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1032 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1038 * gst_ghost_pad_set_target:
1039 * @gpad: the #GstGhostPad
1040 * @newtarget: (transfer none) (allow-none): the new pad target
1042 * Set the new target of the ghostpad @gpad. Any existing target
1043 * is unlinked and links to the new target are established. if @newtarget is
1044 * NULL the target will be cleared.
1046 * Returns: (transfer full): TRUE if the new target could be set. This function
1047 * can return FALSE when the internal pads could not be linked.
1050 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1054 GstPadLinkReturn lret;
1056 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1057 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1058 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1060 /* no need for locking, the internal pad's lifecycle is directly linked to the
1062 internal = GST_PROXY_PAD_INTERNAL (gpad);
1065 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1067 GST_DEBUG_OBJECT (gpad, "clearing target");
1069 /* clear old target */
1070 GST_OBJECT_LOCK (gpad);
1071 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1072 GST_OBJECT_UNLOCK (gpad);
1074 /* unlink internal pad */
1075 if (GST_PAD_IS_SRC (internal))
1076 gst_pad_unlink (internal, oldtarget);
1078 gst_pad_unlink (oldtarget, internal);
1080 GST_OBJECT_UNLOCK (gpad);
1084 /* and link to internal pad without any checks */
1085 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1087 if (GST_PAD_IS_SRC (internal))
1089 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1092 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1094 if (lret != GST_PAD_LINK_OK)
1103 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",