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_PROXY_PAD_PRIVATE (pad)->target)
58 #define GST_PROXY_PAD_INTERNAL(pad) (GST_PROXY_PAD_PRIVATE (pad)->internal)
59 #define GST_PROXY_PAD_RETARGET(pad) (GST_PROXY_PAD_PRIVATE (pad)->retarget)
60 #define GST_PROXY_GET_LOCK(pad) (GST_PROXY_PAD_PRIVATE (pad)->proxy_lock)
61 #define GST_PROXY_LOCK(pad) (g_mutex_lock (GST_PROXY_GET_LOCK (pad)))
62 #define GST_PROXY_UNLOCK(pad) (g_mutex_unlock (GST_PROXY_GET_LOCK (pad)))
64 struct _GstProxyPadPrivate
73 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
75 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
76 static GstPad *gst_proxy_pad_get_internal (GstPad * pad);
78 static void gst_proxy_pad_dispose (GObject * object);
79 static void gst_proxy_pad_finalize (GObject * object);
81 static void on_src_target_notify (GstPad * target,
82 GParamSpec * unused, gpointer user_data);
84 static GParamSpec *pspec_caps = NULL;
86 static const GstQueryType *
87 gst_proxy_pad_do_query_type (GstPad * pad)
89 GstPad *target = gst_proxy_pad_get_target (pad);
90 const GstQueryType *res = NULL;
93 res = gst_pad_get_query_types (target);
94 gst_object_unref (target);
100 gst_proxy_pad_do_event (GstPad * pad, GstEvent * event)
102 gboolean res = FALSE;
103 GstPad *internal = gst_proxy_pad_get_internal (pad);
106 res = gst_pad_push_event (internal, event);
107 gst_object_unref (internal);
114 gst_proxy_pad_do_query (GstPad * pad, GstQuery * query)
116 gboolean res = FALSE;
117 GstPad *target = gst_proxy_pad_get_target (pad);
120 res = gst_pad_query (target, query);
121 gst_object_unref (target);
128 gst_proxy_pad_do_iterate_internal_links (GstPad * pad)
130 GstIterator *res = NULL;
131 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
135 gst_iterator_new_single (GST_TYPE_PAD, internal,
136 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
143 gst_proxy_pad_do_chain (GstPad * pad, GstBuffer * buffer)
146 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
148 res = gst_pad_push (internal, buffer);
154 gst_proxy_pad_do_chain_list (GstPad * pad, GstBufferList * list)
157 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
159 res = gst_pad_push_list (internal, list);
165 gst_proxy_pad_do_getrange (GstPad * pad, guint64 offset, guint size,
169 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
171 res = gst_pad_pull_range (internal, offset, size, buffer);
177 gst_proxy_pad_do_checkgetrange (GstPad * pad)
180 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
182 result = gst_pad_check_pull_range (internal);
188 gst_proxy_pad_do_getcaps (GstPad * pad)
190 GstPad *target = gst_proxy_pad_get_target (pad);
192 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
195 /* if we have a real target, proxy the call */
196 res = gst_pad_get_caps (target);
198 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
199 GST_DEBUG_PAD_NAME (target), res);
201 gst_object_unref (target);
203 /* filter against the template */
207 filt = GST_PAD_TEMPLATE_CAPS (templ);
209 tmp = gst_caps_intersect (filt, res);
210 gst_caps_unref (res);
212 GST_DEBUG_OBJECT (pad,
213 "filtered against template gives %" GST_PTR_FORMAT, res);
217 /* else, if we have a template, use its caps. */
219 res = GST_PAD_TEMPLATE_CAPS (templ);
220 GST_DEBUG_OBJECT (pad,
221 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
223 res = gst_caps_ref (res);
227 /* last resort, any caps */
228 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
229 res = gst_caps_new_any ();
237 gst_proxy_pad_do_acceptcaps (GstPad * pad, GstCaps * caps)
239 GstPad *target = gst_proxy_pad_get_target (pad);
243 res = gst_pad_accept_caps (target, caps);
244 gst_object_unref (target);
246 /* We don't have a target, we return TRUE and we assume that any future
247 * target will be able to deal with any configured caps. */
255 gst_proxy_pad_do_fixatecaps (GstPad * pad, GstCaps * caps)
257 GstPad *target = gst_proxy_pad_get_target (pad);
260 gst_pad_fixate_caps (target, caps);
261 gst_object_unref (target);
266 gst_proxy_pad_do_setcaps (GstPad * pad, GstCaps * caps)
268 GstPad *target = gst_proxy_pad_get_target (pad);
272 res = gst_pad_set_caps (target, caps);
273 gst_object_unref (target);
275 /* We don't have any target, but we shouldn't return FALSE since this
276 * would stop the actual push of a buffer (which might trigger a pad block
277 * or probe, or properly return GST_FLOW_NOT_LINKED.
285 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
290 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
292 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
293 goto wrong_direction;
295 GST_LOG_OBJECT (pad, "clearing target");
297 /* clear old target */
298 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
299 gst_object_unref (oldtarget);
301 /* set and ref new target if any */
303 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
305 GST_PROXY_PAD_TARGET (pad) = NULL;
312 GST_ERROR_OBJECT (pad,
313 "target pad doesn't have the same direction as ourself");
319 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
323 GST_PROXY_LOCK (pad);
324 result = gst_proxy_pad_set_target_unlocked (pad, target);
325 GST_PROXY_UNLOCK (pad);
331 gst_proxy_pad_get_target (GstPad * pad)
335 GST_PROXY_LOCK (pad);
336 target = GST_PROXY_PAD_TARGET (pad);
338 gst_object_ref (target);
339 GST_PROXY_UNLOCK (pad);
345 gst_proxy_pad_get_internal (GstPad * pad)
349 GST_PROXY_LOCK (pad);
350 internal = GST_PROXY_PAD_INTERNAL (pad);
352 gst_object_ref (internal);
353 GST_PROXY_UNLOCK (pad);
359 gst_proxy_pad_do_unlink (GstPad * pad)
363 /* don't do anything if this unlink resulted from retargeting the pad
364 * controlled by the ghostpad. We only want to invalidate the target pad when
365 * the element suddently unlinked with our internal pad. */
366 if (GST_PROXY_PAD_RETARGET (pad))
369 internal = GST_PROXY_PAD_INTERNAL (pad);
371 GST_DEBUG_OBJECT (pad, "pad is unlinked");
373 gst_proxy_pad_set_target (internal, NULL);
377 gst_proxy_pad_class_init (GstProxyPadClass * klass)
379 GObjectClass *gobject_class = (GObjectClass *) klass;
381 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
383 gobject_class->dispose = gst_proxy_pad_dispose;
384 gobject_class->finalize = gst_proxy_pad_finalize;
386 /* Register common function pointer descriptions */
387 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_query_type);
388 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_event);
389 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_query);
390 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_iterate_internal_links);
391 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_getcaps);
392 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_acceptcaps);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_fixatecaps);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_setcaps);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_unlink);
396 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_chain);
397 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_chain_list);
398 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_getrange);
399 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_checkgetrange);
403 gst_proxy_pad_dispose (GObject * object)
405 GstPad *pad = GST_PAD (object);
408 GST_PROXY_LOCK (pad);
409 /* remove and unref the target */
410 target_p = &GST_PROXY_PAD_TARGET (pad);
411 gst_object_replace ((GstObject **) target_p, NULL);
412 /* The internal is only cleared by GstGhostPad::dispose, since it is the
413 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
415 GST_PROXY_UNLOCK (pad);
417 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
421 gst_proxy_pad_finalize (GObject * object)
423 GstProxyPad *pad = GST_PROXY_PAD (object);
425 g_mutex_free (GST_PROXY_GET_LOCK (pad));
426 GST_PROXY_GET_LOCK (pad) = NULL;
428 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
432 gst_proxy_pad_init (GstProxyPad * ppad)
434 GstPad *pad = (GstPad *) ppad;
436 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
437 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
438 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
440 gst_pad_set_query_type_function (pad, gst_proxy_pad_do_query_type);
441 gst_pad_set_event_function (pad, gst_proxy_pad_do_event);
442 gst_pad_set_query_function (pad, gst_proxy_pad_do_query);
443 gst_pad_set_iterate_internal_links_function (pad,
444 gst_proxy_pad_do_iterate_internal_links);
446 gst_pad_set_getcaps_function (pad, gst_proxy_pad_do_getcaps);
447 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_do_acceptcaps);
448 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_do_fixatecaps);
449 gst_pad_set_setcaps_function (pad, gst_proxy_pad_do_setcaps);
450 gst_pad_set_unlink_function (pad, gst_proxy_pad_do_unlink);
454 /***********************************************************************
455 * Ghost pads, implemented as a pair of proxy pads (sort of)
459 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
461 struct _GstGhostPadPrivate
463 /* with PROXY_LOCK */
466 gboolean constructed;
469 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
471 static void gst_ghost_pad_dispose (GObject * object);
473 /* see gstghostpad design docs */
475 gst_ghost_pad_internal_do_activate_push (GstPad * pad, gboolean active)
480 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
481 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
483 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
484 * will be activated later (or already in case of a ghost sinkpad). */
485 other = GST_PROXY_PAD_INTERNAL (pad);
486 ret = gst_pad_activate_push (other, active);
492 gst_ghost_pad_internal_do_activate_pull (GstPad * pad, gboolean active)
497 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
498 GST_DEBUG_PAD_NAME (pad));
500 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
501 /* we are activated in pull mode by our peer element, which is a sinkpad
502 * that wants to operate in pull mode. This activation has to propagate
503 * upstream throught the pipeline. We call the internal activation function,
504 * which will trigger gst_ghost_pad_do_activate_pull, which propagates even
505 * further upstream */
506 GST_LOG_OBJECT (pad, "pad is src, activate internal");
507 other = GST_PROXY_PAD_INTERNAL (pad);
508 ret = gst_pad_activate_pull (other, active);
509 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
510 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
511 * since we hold a pointer to the upstream peer. */
512 GST_LOG_OBJECT (pad, "activating peer");
513 ret = gst_pad_activate_pull (other, active);
514 gst_object_unref (other);
516 /* this is failure, we can't activate pull if there is no peer */
517 GST_LOG_OBJECT (pad, "not src and no peer, failing");
525 gst_ghost_pad_do_activate_push (GstPad * pad, gboolean active)
530 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
531 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
533 /* just activate the internal pad */
534 other = GST_PROXY_PAD_INTERNAL (pad);
535 ret = gst_pad_activate_push (other, active);
541 gst_ghost_pad_do_activate_pull (GstPad * pad, gboolean active)
546 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
547 GST_DEBUG_PAD_NAME (pad));
549 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
550 /* the ghostpad is SRC and activated in pull mode by its peer, call the
551 * activation function of the internal pad to propagate the activation
553 GST_LOG_OBJECT (pad, "pad is src, activate internal");
554 other = GST_PROXY_PAD_INTERNAL (pad);
555 ret = gst_pad_activate_pull (other, active);
556 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
557 /* We are SINK and activated by the internal pad, propagate activation
558 * upstream because we hold a ref to the upstream peer */
559 GST_LOG_OBJECT (pad, "activating peer");
560 ret = gst_pad_activate_pull (other, active);
561 gst_object_unref (other);
563 /* no peer, we fail */
564 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
571 static GstPadLinkReturn
572 gst_ghost_pad_do_link (GstPad * pad, GstPad * peer)
574 GstPadLinkReturn ret;
577 GST_DEBUG_OBJECT (pad, "linking ghostpad");
579 internal = GST_PROXY_PAD_INTERNAL (pad);
580 if (!gst_proxy_pad_set_target (internal, peer))
583 ret = GST_PAD_LINK_OK;
584 /* if we are a source pad, we should call the peer link function
585 * if the peer has one, see design docs. */
586 if (GST_PAD_IS_SRC (pad)) {
587 if (GST_PAD_LINKFUNC (peer)) {
588 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
589 if (ret != GST_PAD_LINK_OK)
598 GST_DEBUG_OBJECT (pad, "setting target failed");
599 return GST_PAD_LINK_REFUSED;
603 GST_DEBUG_OBJECT (pad, "linking failed");
604 /* clear target again */
605 gst_proxy_pad_set_target (internal, NULL);
611 gst_ghost_pad_do_unlink (GstPad * pad)
615 internal = GST_PROXY_PAD_INTERNAL (pad);
617 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
619 /* The target of the internal pad is no longer valid */
620 gst_proxy_pad_set_target (internal, NULL);
624 on_int_notify (GstPad * internal, GParamSpec * unused, GstGhostPad * pad)
629 g_object_get (internal, "caps", &caps, NULL);
631 GST_DEBUG_OBJECT (pad, "notified %p %" GST_PTR_FORMAT, caps, caps);
633 GST_OBJECT_LOCK (pad);
634 changed = (GST_PAD_CAPS (pad) != caps);
636 gst_caps_replace (&(GST_PAD_CAPS (pad)), caps);
637 GST_OBJECT_UNLOCK (pad);
640 #if GLIB_CHECK_VERSION(2,26,0)
641 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
643 g_object_notify ((GObject *) pad, "caps");
648 gst_caps_unref (caps);
652 on_src_target_notify (GstPad * target, GParamSpec * unused, gpointer user_data)
654 GstProxyPad *proxypad;
659 g_object_get (target, "caps", &caps, NULL);
661 GST_OBJECT_LOCK (target);
662 /* First check if the peer is still available and our proxy pad */
663 if (!GST_PAD_PEER (target) || !GST_IS_PROXY_PAD (GST_PAD_PEER (target))) {
664 GST_OBJECT_UNLOCK (target);
668 proxypad = GST_PROXY_PAD (GST_PAD_PEER (target));
669 GST_PROXY_LOCK (proxypad);
670 /* Now check if the proxypad's internal pad is still there and
672 if (!GST_PROXY_PAD_INTERNAL (proxypad) ||
673 !GST_IS_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad))) {
674 GST_OBJECT_UNLOCK (target);
675 GST_PROXY_UNLOCK (proxypad);
678 gpad = GST_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad));
680 GST_PROXY_UNLOCK (proxypad);
681 GST_OBJECT_UNLOCK (target);
683 GST_OBJECT_LOCK (gpad);
685 GST_DEBUG_OBJECT (gpad, "notified %p %" GST_PTR_FORMAT, caps, caps);
687 changed = (GST_PAD_CAPS (gpad) != caps);
689 gst_caps_replace (&(GST_PAD_CAPS (gpad)), caps);
690 GST_OBJECT_UNLOCK (gpad);
693 #if GLIB_CHECK_VERSION(2,26,0)
694 g_object_notify_by_pspec ((GObject *) gpad, pspec_caps);
696 g_object_notify ((GObject *) gpad, "caps");
700 g_object_unref (gpad);
704 gst_caps_unref (caps);
708 gst_ghost_pad_do_setcaps (GstPad * pad, GstCaps * caps)
710 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
713 return gst_proxy_pad_do_setcaps (pad, caps);
717 gst_ghost_pad_do_iterate_internal_links (GstPad * pad)
719 GstIterator *res = NULL;
720 GstPad *internal = GST_PROXY_PAD_INTERNAL (GST_GHOST_PAD_CAST (pad));
724 gst_iterator_new_single (GST_TYPE_PAD, internal,
725 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
732 gst_ghost_pad_class_init (GstGhostPadClass * klass)
734 GObjectClass *gobject_class = (GObjectClass *) klass;
736 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
738 pspec_caps = g_object_class_find_property (gobject_class, "caps");
740 gobject_class->dispose = gst_ghost_pad_dispose;
742 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_setcaps);
743 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_activate_pull);
744 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_activate_push);
745 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_link);
749 gst_ghost_pad_init (GstGhostPad * pad)
751 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
752 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
754 gst_pad_set_setcaps_function (GST_PAD_CAST (pad), gst_ghost_pad_do_setcaps);
755 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
756 gst_ghost_pad_do_activate_pull);
757 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
758 gst_ghost_pad_do_activate_push);
759 gst_pad_set_iterate_internal_links_function (GST_PAD_CAST (pad),
760 gst_ghost_pad_do_iterate_internal_links);
764 gst_ghost_pad_dispose (GObject * object)
770 pad = GST_PAD (object);
772 GST_DEBUG_OBJECT (pad, "dispose");
774 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
776 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
777 * gst_ghost_pad_do_unlink when the ghost pad is in an inconsistent state */
778 peer = gst_pad_get_peer (pad);
780 if (GST_PAD_IS_SRC (pad))
781 gst_pad_unlink (pad, peer);
783 gst_pad_unlink (peer, pad);
785 gst_object_unref (peer);
788 GST_PROXY_LOCK (pad);
789 internal = GST_PROXY_PAD_INTERNAL (pad);
791 gst_pad_set_activatepull_function (internal, NULL);
792 gst_pad_set_activatepush_function (internal, NULL);
794 g_signal_handler_disconnect (internal,
795 GST_GHOST_PAD_PRIVATE (pad)->notify_id);
797 /* disposes of the internal pad, since the ghostpad is the only possible object
798 * that has a refcount on the internal pad. */
799 gst_object_unparent (GST_OBJECT_CAST (internal));
800 GST_PROXY_PAD_INTERNAL (pad) = NULL;
802 GST_PROXY_UNLOCK (pad);
804 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
808 * gst_ghost_pad_construct:
809 * @gpad: the newly allocated ghost pad
811 * Finish initialization of a newly allocated ghost pad.
813 * This function is most useful in language bindings and when subclassing
814 * #GstGhostPad; plugin and application developers normally will not call this
815 * function. Call this function directly after a call to g_object_new
816 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
818 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
823 gst_ghost_pad_construct (GstGhostPad * gpad)
825 GstPadDirection dir, otherdir;
826 GstPadTemplate *templ;
827 GstPad *pad, *internal;
829 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
830 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
833 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
835 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
837 pad = GST_PAD (gpad);
839 /* Set directional padfunctions for ghostpad */
840 if (dir == GST_PAD_SINK) {
841 gst_pad_set_chain_function (pad, gst_proxy_pad_do_chain);
842 gst_pad_set_chain_list_function (pad, gst_proxy_pad_do_chain_list);
844 gst_pad_set_getrange_function (pad, gst_proxy_pad_do_getrange);
845 gst_pad_set_checkgetrange_function (pad, gst_proxy_pad_do_checkgetrange);
848 /* link/unlink functions */
849 gst_pad_set_link_function (pad, gst_ghost_pad_do_link);
850 gst_pad_set_unlink_function (pad, gst_ghost_pad_do_unlink);
852 /* INTERNAL PAD, it always exists and is child of the ghostpad */
853 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
856 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
857 "direction", otherdir, "template", templ, NULL);
858 /* release ref obtained via g_object_get */
859 gst_object_unref (templ);
862 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
863 "direction", otherdir, NULL);
865 GST_PAD_UNSET_FLUSHING (internal);
867 /* Set directional padfunctions for internal pad */
868 if (dir == GST_PAD_SRC) {
869 gst_pad_set_chain_function (internal, gst_proxy_pad_do_chain);
870 gst_pad_set_chain_list_function (internal, gst_proxy_pad_do_chain_list);
872 gst_pad_set_getrange_function (internal, gst_proxy_pad_do_getrange);
873 gst_pad_set_checkgetrange_function (internal,
874 gst_proxy_pad_do_checkgetrange);
877 GST_PROXY_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 /* could be more general here, iterating over all writable properties...
896 * taking the short road for now tho */
897 GST_GHOST_PAD_PRIVATE (pad)->notify_id =
898 g_signal_connect (internal, "notify::caps", G_CALLBACK (on_int_notify),
901 /* call function to init values of the pad caps */
902 on_int_notify (internal, NULL, GST_GHOST_PAD_CAST (pad));
904 /* special activation functions for the internal pad */
905 gst_pad_set_activatepull_function (internal,
906 gst_ghost_pad_internal_do_activate_pull);
907 gst_pad_set_activatepush_function (internal,
908 gst_ghost_pad_internal_do_activate_push);
910 GST_PROXY_UNLOCK (pad);
912 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
918 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
919 GST_DEBUG_PAD_NAME (internal));
920 g_critical ("Could not set internal pad %s:%s",
921 GST_DEBUG_PAD_NAME (internal));
922 GST_PROXY_UNLOCK (pad);
923 gst_object_unref (internal);
929 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
930 GstPadTemplate * templ)
934 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
936 /* OBJECT CREATION */
938 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
939 "direction", dir, "template", templ, NULL);
941 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
942 "direction", dir, NULL);
945 if (!gst_ghost_pad_construct (ret))
946 goto construct_failed;
948 return GST_PAD_CAST (ret);
952 gst_object_unref (ret);
957 * gst_ghost_pad_new_no_target:
958 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
959 * @dir: the direction of the ghostpad
961 * Create a new ghostpad without a target with the given direction.
962 * A target can be set on the ghostpad later with the
963 * gst_ghost_pad_set_target() function.
965 * The created ghostpad will not have a padtemplate.
967 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
970 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
974 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
976 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
978 ret = gst_ghost_pad_new_full (name, dir, NULL);
985 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
986 * @target: (transfer none): the pad to ghost.
988 * Create a new ghostpad with @target as the target. The direction will be taken
989 * from the target pad. @target must be unlinked.
991 * Will ref the target.
993 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
996 gst_ghost_pad_new (const gchar * name, GstPad * target)
1000 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1001 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1003 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1004 GST_DEBUG_PAD_NAME (target));
1006 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1007 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1008 goto set_target_failed;
1015 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1016 GST_DEBUG_PAD_NAME (target));
1017 gst_object_unref (ret);
1023 * gst_ghost_pad_new_from_template:
1024 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1025 * @target: (transfer none): the pad to ghost.
1026 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1028 * Create a new ghostpad with @target as the target. The direction will be taken
1029 * from the target pad. The template used on the ghostpad will be @template.
1031 * Will ref the target.
1033 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1039 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1040 GstPadTemplate * templ)
1044 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1045 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1046 g_return_val_if_fail (templ != NULL, NULL);
1047 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1048 GST_PAD_DIRECTION (target), NULL);
1050 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1051 GST_DEBUG_PAD_NAME (target), templ);
1053 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1054 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1055 goto set_target_failed;
1062 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1063 GST_DEBUG_PAD_NAME (target));
1064 gst_object_unref (ret);
1070 * gst_ghost_pad_new_no_target_from_template:
1071 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1072 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1074 * Create a new ghostpad based on @templ, without setting a target. The
1075 * direction will be taken from the @templ.
1077 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1082 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1083 GstPadTemplate * templ)
1087 g_return_val_if_fail (templ != NULL, NULL);
1090 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1096 * gst_ghost_pad_get_target:
1097 * @gpad: the #GstGhostPad
1099 * Get the target pad of @gpad. Unref target pad after usage.
1101 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1102 * has no target set. Unref target pad after usage.
1105 gst_ghost_pad_get_target (GstGhostPad * gpad)
1109 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1111 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1113 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1119 * gst_ghost_pad_set_target:
1120 * @gpad: the #GstGhostPad
1121 * @newtarget: (transfer none) (allow-none): the new pad target
1123 * Set the new target of the ghostpad @gpad. Any existing target
1124 * is unlinked and links to the new target are established. if @newtarget is
1125 * NULL the target will be cleared.
1127 * Returns: (transfer full): TRUE if the new target could be set. This function
1128 * can return FALSE when the internal pads could not be linked.
1131 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1136 GstPadLinkReturn lret;
1138 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1139 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1140 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1142 /* no need for locking, the internal pad's lifecycle is directly linked to the
1144 internal = GST_PROXY_PAD_INTERNAL (gpad);
1147 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1149 GST_DEBUG_OBJECT (gpad, "clearing target");
1151 /* clear old target */
1152 GST_PROXY_LOCK (gpad);
1153 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1154 if (GST_PAD_IS_SRC (oldtarget)) {
1155 g_signal_handlers_disconnect_by_func (oldtarget,
1156 (gpointer) on_src_target_notify, NULL);
1159 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1161 /* unlink internal pad */
1162 if (GST_PAD_IS_SRC (internal))
1163 gst_pad_unlink (internal, oldtarget);
1165 gst_pad_unlink (oldtarget, internal);
1167 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1170 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1171 GST_PROXY_UNLOCK (gpad);
1173 if (result && newtarget) {
1174 if (GST_PAD_IS_SRC (newtarget)) {
1175 g_signal_connect (newtarget, "notify::caps",
1176 G_CALLBACK (on_src_target_notify), NULL);
1179 /* and link to internal pad without any checks */
1180 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1182 if (GST_PAD_IS_SRC (internal))
1184 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1187 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1189 if (lret != GST_PAD_LINK_OK)
1198 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1200 /* and unset target again */
1201 GST_PROXY_LOCK (gpad);
1202 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1203 GST_PROXY_UNLOCK (gpad);