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)
147 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
148 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
150 if (!(target = gst_proxy_pad_get_target (pad)))
153 res = gst_pad_query (target, query);
154 gst_object_unref (target);
161 GST_DEBUG_OBJECT (pad, "no target pad");
167 * gst_proyx_pad_iterate_internal_links_default:
168 * @pad: the #GstPad to get the internal links of.
170 * Invoke the default iterate internal links function of the proxy pad.
172 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
173 * returned pad with gst_object_unref().
178 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
180 GstIterator *res = NULL;
184 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
186 internal = GST_PROXY_PAD_INTERNAL (pad);
187 g_value_init (&v, GST_TYPE_PAD);
188 g_value_set_object (&v, internal);
189 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
196 * gst_proxy_pad_chain_default:
197 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
198 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
201 * Invoke the default chain function of the proxy pad.
203 * Returns: a #GstFlowReturn from the pad.
208 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
213 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
214 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
216 internal = GST_PROXY_PAD_INTERNAL (pad);
217 res = gst_pad_push (internal, buffer);
223 * gst_proxy_pad_chain_list_default:
224 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
225 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
228 * Invoke the default chain list function of the proxy pad.
230 * Returns: a #GstFlowReturn from the pad.
235 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
240 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
241 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
243 internal = GST_PROXY_PAD_INTERNAL (pad);
244 res = gst_pad_push_list (internal, list);
250 * gst_proxy_pad_get_range_default:
251 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
252 * @offset: The start offset of the buffer
253 * @size: The length of the buffer
254 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
255 * returns #GST_FLOW_ERROR if %NULL.
257 * Invoke the default getrange function of the proxy pad.
259 * Returns: a #GstFlowReturn from the pad.
264 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
270 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
271 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
273 internal = GST_PROXY_PAD_INTERNAL (pad);
274 res = gst_pad_pull_range (internal, offset, size, buffer);
280 * gst_proxy_pad_getcaps_default:
281 * @pad: a #GstPad to get the capabilities of.
282 * @filter: a #GstCaps filter.
284 * Invoke the default getcaps function of the proxy pad.
286 * Returns: (transfer full): the caps of the pad with incremented ref-count
291 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
295 GstPadTemplate *templ;
297 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
299 templ = GST_PAD_PAD_TEMPLATE (pad);
300 target = gst_proxy_pad_get_target (pad);
302 /* if we have a real target, proxy the call */
303 res = gst_pad_get_caps (target, filter);
305 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
306 GST_DEBUG_PAD_NAME (target), res);
308 gst_object_unref (target);
310 /* filter against the template */
314 filt = GST_PAD_TEMPLATE_CAPS (templ);
316 tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
317 gst_caps_unref (res);
319 GST_DEBUG_OBJECT (pad,
320 "filtered against template gives %" GST_PTR_FORMAT, res);
324 /* else, if we have a template, use its caps. */
326 res = GST_PAD_TEMPLATE_CAPS (templ);
327 GST_DEBUG_OBJECT (pad,
328 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
330 res = gst_caps_ref (res);
333 GstCaps *intersection =
334 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
336 gst_caps_unref (res);
343 /* If there's a filter, return that */
344 if (filter != NULL) {
345 res = gst_caps_ref (filter);
349 /* last resort, any caps */
350 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
351 res = gst_caps_new_any ();
359 * gst_proxy_pad_acceptcaps_default:
360 * @pad: a #GstPad to check
361 * @caps: a #GstCaps to check on the pad
363 * Invoke the default acceptcaps function of the proxy pad.
365 * Returns: TRUE if the pad can accept the caps.
370 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
375 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
376 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
378 target = gst_proxy_pad_get_target (pad);
380 res = gst_pad_accept_caps (target, caps);
381 gst_object_unref (target);
383 GST_DEBUG_OBJECT (pad, "no target");
384 /* We don't have a target, we return TRUE and we assume that any future
385 * target will be able to deal with any configured caps. */
393 * gst_proxy_pad_fixatecaps_default:
394 * @pad: a #GstPad to fixate
395 * @caps: the #GstCaps to fixate
397 * Invoke the default fixatecaps function of the proxy pad.
402 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
406 g_return_if_fail (GST_IS_PROXY_PAD (pad));
407 g_return_if_fail (GST_IS_CAPS (caps));
409 if (!(target = gst_proxy_pad_get_target (pad)))
412 gst_pad_fixate_caps (target, caps);
413 gst_object_unref (target);
420 GST_DEBUG_OBJECT (pad, "no target");
426 gst_proxy_pad_get_target (GstPad * pad)
430 GST_OBJECT_LOCK (pad);
431 target = GST_PROXY_PAD_TARGET (pad);
433 gst_object_ref (target);
434 GST_OBJECT_UNLOCK (pad);
440 * gst_proxy_pad_get_internal:
441 * @pad: the #GstProxyPad
443 * Get the internal pad of @pad. Unref target pad after usage.
445 * The internal pad of a #GstGhostPad is the internally used
446 * pad of opposite direction, which is used to link to the target.
448 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
449 * Unref target pad after usage.
454 gst_proxy_pad_get_internal (GstProxyPad * pad)
458 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
460 GST_OBJECT_LOCK (pad);
461 internal = GST_PROXY_PAD_INTERNAL (pad);
463 gst_object_ref (internal);
464 GST_OBJECT_UNLOCK (pad);
466 return GST_PROXY_PAD_CAST (internal);
470 * gst_proxy_pad_unlink_default:
471 * @pad: a #GstPad to unlink
473 * Invoke the default unlink function of the proxy pad.
478 gst_proxy_pad_unlink_default (GstPad * pad)
480 /* nothing to do anymore */
481 GST_DEBUG_OBJECT (pad, "pad is unlinked");
485 gst_proxy_pad_class_init (GstProxyPadClass * klass)
487 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
489 /* Register common function pointer descriptions */
490 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
491 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
492 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
493 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
494 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
495 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
496 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
497 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
498 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
499 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
500 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
504 gst_proxy_pad_init (GstProxyPad * ppad)
506 GstPad *pad = (GstPad *) ppad;
508 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
509 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
511 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
512 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
513 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
514 gst_pad_set_iterate_internal_links_function (pad,
515 gst_proxy_pad_iterate_internal_links_default);
517 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
518 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
519 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
520 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
524 /***********************************************************************
525 * Ghost pads, implemented as a pair of proxy pads (sort of)
529 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
531 struct _GstGhostPadPrivate
533 /* with PROXY_LOCK */
534 gboolean constructed;
537 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
539 static void gst_ghost_pad_dispose (GObject * object);
542 * gst_ghost_pad_internal_activate_push_default:
543 * @pad: the #GstPad to activate or deactivate.
544 * @active: whether the pad should be active or not.
546 * Invoke the default activate push function of a proxy pad that is
547 * owned by a ghost pad.
549 * Returns: %TRUE if the operation was successful.
554 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
559 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
561 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
562 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
564 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
565 * will be activated later (or already in case of a ghost sinkpad). */
566 other = GST_PROXY_PAD_INTERNAL (pad);
567 ret = gst_pad_activate_push (other, active);
573 * gst_ghost_pad_internal_activate_pull_default:
574 * @pad: the #GstPad to activate or deactivate.
575 * @active: whether the pad should be active or not.
577 * Invoke the default activate pull function of a proxy pad that is
578 * owned by a ghost pad.
580 * Returns: %TRUE if the operation was successful.
585 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
590 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
592 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
593 GST_DEBUG_PAD_NAME (pad));
595 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
596 /* we are activated in pull mode by our peer element, which is a sinkpad
597 * that wants to operate in pull mode. This activation has to propagate
598 * upstream through the pipeline. We call the internal activation function,
599 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
600 * further upstream */
601 GST_LOG_OBJECT (pad, "pad is src, activate internal");
602 other = GST_PROXY_PAD_INTERNAL (pad);
603 ret = gst_pad_activate_pull (other, active);
604 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
605 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
606 * since we hold a pointer to the upstream peer. */
607 GST_LOG_OBJECT (pad, "activating peer");
608 ret = gst_pad_activate_pull (other, active);
609 gst_object_unref (other);
611 /* this is failure, we can't activate pull if there is no peer */
612 GST_LOG_OBJECT (pad, "not src and no peer, failing");
620 * gst_ghost_pad_activate_push_default:
621 * @pad: the #GstPad to activate or deactivate.
622 * @active: whether the pad should be active or not.
624 * Invoke the default activate push function of a ghost pad.
626 * Returns: %TRUE if the operation was successful.
631 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
636 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
638 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
639 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
641 /* just activate the internal pad */
642 other = GST_PROXY_PAD_INTERNAL (pad);
643 ret = gst_pad_activate_push (other, active);
649 * gst_ghost_pad_activate_pull_default:
650 * @pad: the #GstPad to activate or deactivate.
651 * @active: whether the pad should be active or not.
653 * Invoke the default activate pull function of a ghost pad.
655 * Returns: %TRUE if the operation was successful.
660 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
665 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
667 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
668 GST_DEBUG_PAD_NAME (pad));
670 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
671 /* the ghostpad is SRC and activated in pull mode by its peer, call the
672 * activation function of the internal pad to propagate the activation
674 GST_LOG_OBJECT (pad, "pad is src, activate internal");
675 other = GST_PROXY_PAD_INTERNAL (pad);
676 ret = gst_pad_activate_pull (other, active);
677 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
678 /* We are SINK and activated by the internal pad, propagate activation
679 * upstream because we hold a ref to the upstream peer */
680 GST_LOG_OBJECT (pad, "activating peer");
681 ret = gst_pad_activate_pull (other, active);
682 gst_object_unref (other);
684 /* no peer, we fail */
685 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
693 * gst_ghost_pad_link_default:
694 * @pad: the #GstPad to link.
695 * @peer: the #GstPad peer
697 * Invoke the default link function of a ghost pad.
699 * Returns: #GstPadLinkReturn of the operation
704 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
706 GstPadLinkReturn ret;
708 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
709 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
711 GST_DEBUG_OBJECT (pad, "linking ghostpad");
713 ret = GST_PAD_LINK_OK;
714 /* if we are a source pad, we should call the peer link function
715 * if the peer has one, see design docs. */
716 if (GST_PAD_IS_SRC (pad)) {
717 if (GST_PAD_LINKFUNC (peer)) {
718 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
719 if (ret != GST_PAD_LINK_OK)
720 GST_DEBUG_OBJECT (pad, "linking failed");
727 * gst_ghost_pad_unlink_default:
728 * @pad: the #GstPad to link.
730 * Invoke the default unlink function of a ghost pad.
735 gst_ghost_pad_unlink_default (GstPad * pad)
737 g_return_if_fail (GST_IS_GHOST_PAD (pad));
739 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
743 gst_ghost_pad_class_init (GstGhostPadClass * klass)
745 GObjectClass *gobject_class = (GObjectClass *) klass;
747 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
749 gobject_class->dispose = gst_ghost_pad_dispose;
751 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
752 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
753 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
757 gst_ghost_pad_init (GstGhostPad * pad)
759 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
760 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
762 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
763 gst_ghost_pad_activate_pull_default);
764 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
765 gst_ghost_pad_activate_push_default);
769 gst_ghost_pad_dispose (GObject * object)
775 pad = GST_PAD (object);
777 GST_DEBUG_OBJECT (pad, "dispose");
779 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
781 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
782 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
783 peer = gst_pad_get_peer (pad);
785 if (GST_PAD_IS_SRC (pad))
786 gst_pad_unlink (pad, peer);
788 gst_pad_unlink (peer, pad);
790 gst_object_unref (peer);
793 GST_OBJECT_LOCK (pad);
794 internal = GST_PROXY_PAD_INTERNAL (pad);
796 gst_pad_set_activatepull_function (internal, NULL);
797 gst_pad_set_activatepush_function (internal, NULL);
799 /* disposes of the internal pad, since the ghostpad is the only possible object
800 * that has a refcount on the internal pad. */
801 gst_object_unparent (GST_OBJECT_CAST (internal));
802 GST_PROXY_PAD_INTERNAL (pad) = NULL;
804 GST_OBJECT_UNLOCK (pad);
806 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
810 * gst_ghost_pad_construct:
811 * @gpad: the newly allocated ghost pad
813 * Finish initialization of a newly allocated ghost pad.
815 * This function is most useful in language bindings and when subclassing
816 * #GstGhostPad; plugin and application developers normally will not call this
817 * function. Call this function directly after a call to g_object_new
818 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
820 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
825 gst_ghost_pad_construct (GstGhostPad * gpad)
827 GstPadDirection dir, otherdir;
828 GstPadTemplate *templ;
829 GstPad *pad, *internal;
831 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
832 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
835 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
837 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
839 pad = GST_PAD (gpad);
841 /* Set directional padfunctions for ghostpad */
842 if (dir == GST_PAD_SINK) {
843 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
844 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
846 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
849 /* link/unlink functions */
850 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
851 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
853 /* INTERNAL PAD, it always exists and is child of the ghostpad */
854 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
857 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
858 "direction", otherdir, "template", templ, NULL);
859 /* release ref obtained via g_object_get */
860 gst_object_unref (templ);
863 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
864 "direction", otherdir, NULL);
866 GST_PAD_UNSET_FLUSHING (internal);
868 /* Set directional padfunctions for internal pad */
869 if (dir == GST_PAD_SRC) {
870 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
871 gst_pad_set_chain_list_function (internal,
872 gst_proxy_pad_chain_list_default);
874 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
877 GST_OBJECT_LOCK (pad);
879 /* now make the ghostpad a parent of the internal pad */
880 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
881 GST_OBJECT_CAST (pad)))
884 /* The ghostpad is the parent of the internal pad and is the only object that
885 * can have a refcount on the internal pad.
886 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
888 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
889 * its refcount on the internal pad in the dispose method by un-parenting it.
890 * This is why we don't take extra refcounts in the assignments below
892 GST_PROXY_PAD_INTERNAL (pad) = internal;
893 GST_PROXY_PAD_INTERNAL (internal) = pad;
895 /* special activation functions for the internal pad */
896 gst_pad_set_activatepull_function (internal,
897 gst_ghost_pad_internal_activate_pull_default);
898 gst_pad_set_activatepush_function (internal,
899 gst_ghost_pad_internal_activate_push_default);
901 GST_OBJECT_UNLOCK (pad);
903 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
909 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
910 GST_DEBUG_PAD_NAME (internal));
911 g_critical ("Could not set internal pad %s:%s",
912 GST_DEBUG_PAD_NAME (internal));
913 GST_OBJECT_UNLOCK (pad);
914 gst_object_unref (internal);
920 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
921 GstPadTemplate * templ)
925 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
927 /* OBJECT CREATION */
929 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
930 "direction", dir, "template", templ, NULL);
932 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
933 "direction", dir, NULL);
936 if (!gst_ghost_pad_construct (ret))
937 goto construct_failed;
939 return GST_PAD_CAST (ret);
943 gst_object_unref (ret);
948 * gst_ghost_pad_new_no_target:
949 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
950 * @dir: the direction of the ghostpad
952 * Create a new ghostpad without a target with the given direction.
953 * A target can be set on the ghostpad later with the
954 * gst_ghost_pad_set_target() function.
956 * The created ghostpad will not have a padtemplate.
958 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
961 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
965 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
967 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
969 ret = gst_ghost_pad_new_full (name, dir, NULL);
976 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
977 * @target: (transfer none): the pad to ghost.
979 * Create a new ghostpad with @target as the target. The direction will be taken
980 * from the target pad. @target must be unlinked.
982 * Will ref the target.
984 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
987 gst_ghost_pad_new (const gchar * name, GstPad * target)
991 g_return_val_if_fail (GST_IS_PAD (target), NULL);
992 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
994 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
995 GST_DEBUG_PAD_NAME (target));
997 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
998 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
999 goto set_target_failed;
1006 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1007 GST_DEBUG_PAD_NAME (target));
1008 gst_object_unref (ret);
1014 * gst_ghost_pad_new_from_template:
1015 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1016 * @target: (transfer none): the pad to ghost.
1017 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1019 * Create a new ghostpad with @target as the target. The direction will be taken
1020 * from the target pad. The template used on the ghostpad will be @template.
1022 * Will ref the target.
1024 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1030 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1031 GstPadTemplate * templ)
1035 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1036 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1037 g_return_val_if_fail (templ != NULL, NULL);
1038 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1039 GST_PAD_DIRECTION (target), NULL);
1041 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1042 GST_DEBUG_PAD_NAME (target), templ);
1044 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1045 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1046 goto set_target_failed;
1053 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1054 GST_DEBUG_PAD_NAME (target));
1055 gst_object_unref (ret);
1061 * gst_ghost_pad_new_no_target_from_template:
1062 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1063 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1065 * Create a new ghostpad based on @templ, without setting a target. The
1066 * direction will be taken from the @templ.
1068 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1073 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1074 GstPadTemplate * templ)
1078 g_return_val_if_fail (templ != NULL, NULL);
1081 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1087 * gst_ghost_pad_get_target:
1088 * @gpad: the #GstGhostPad
1090 * Get the target pad of @gpad. Unref target pad after usage.
1092 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1093 * has no target set. Unref target pad after usage.
1096 gst_ghost_pad_get_target (GstGhostPad * gpad)
1100 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1102 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1104 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1110 * gst_ghost_pad_set_target:
1111 * @gpad: the #GstGhostPad
1112 * @newtarget: (transfer none) (allow-none): the new pad target
1114 * Set the new target of the ghostpad @gpad. Any existing target
1115 * is unlinked and links to the new target are established. if @newtarget is
1116 * NULL the target will be cleared.
1118 * Returns: (transfer full): TRUE if the new target could be set. This function
1119 * can return FALSE when the internal pads could not be linked.
1122 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1126 GstPadLinkReturn lret;
1128 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1129 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1130 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1132 /* no need for locking, the internal pad's lifecycle is directly linked to the
1134 internal = GST_PROXY_PAD_INTERNAL (gpad);
1137 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1139 GST_DEBUG_OBJECT (gpad, "clearing target");
1141 /* clear old target */
1142 GST_OBJECT_LOCK (gpad);
1143 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1144 GST_OBJECT_UNLOCK (gpad);
1146 /* unlink internal pad */
1147 if (GST_PAD_IS_SRC (internal))
1148 gst_pad_unlink (internal, oldtarget);
1150 gst_pad_unlink (oldtarget, internal);
1152 GST_OBJECT_UNLOCK (gpad);
1156 /* and link to internal pad without any checks */
1157 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1159 if (GST_PAD_IS_SRC (internal))
1161 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1164 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1166 if (lret != GST_PAD_LINK_OK)
1175 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",