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 other
33 * #GstElements. This is where GhostPads come into play. A GhostPad acts as a
34 * proxy for another pad. Thus the bin can have sink and source ghost-pads that
35 * 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);
77 static void gst_proxy_pad_dispose (GObject * object);
78 static void gst_proxy_pad_finalize (GObject * object);
80 #ifndef GST_DISABLE_LOADSAVE
81 static xmlNodePtr gst_proxy_pad_save_thyself (GstObject * object,
85 static void on_src_target_notify (GstPad * target,
86 GParamSpec * unused, gpointer user_data);
90 gst_proxy_pad_class_init (GstProxyPadClass * klass)
92 GObjectClass *gobject_class = (GObjectClass *) klass;
94 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
96 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_proxy_pad_dispose);
97 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_proxy_pad_finalize);
99 #ifndef GST_DISABLE_LOADSAVE
101 GstObjectClass *gstobject_class = (GstObjectClass *) klass;
103 gstobject_class->save_thyself =
104 GST_DEBUG_FUNCPTR (gst_proxy_pad_save_thyself);
109 static const GstQueryType *
110 gst_proxy_pad_do_query_type (GstPad * pad)
112 GstPad *target = gst_proxy_pad_get_target (pad);
113 const GstQueryType *res = NULL;
116 res = gst_pad_get_query_types (target);
117 gst_object_unref (target);
123 gst_proxy_pad_do_event (GstPad * pad, GstEvent * event)
125 gboolean res = FALSE;
126 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
128 res = gst_pad_push_event (internal, event);
134 gst_proxy_pad_do_query (GstPad * pad, GstQuery * query)
136 gboolean res = FALSE;
137 GstPad *target = gst_proxy_pad_get_target (pad);
140 res = gst_pad_query (target, query);
141 gst_object_unref (target);
148 gst_proxy_pad_do_iterate_internal_links (GstPad * pad)
150 GstIterator *res = NULL;
151 GstPad *target = gst_proxy_pad_get_target (pad);
154 res = gst_pad_iterate_internal_links (target);
155 gst_object_unref (target);
162 gst_proxy_pad_do_bufferalloc (GstPad * pad, guint64 offset, guint size,
163 GstCaps * caps, GstBuffer ** buf)
165 GstFlowReturn result;
166 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
168 result = gst_pad_alloc_buffer (internal, offset, size, caps, buf);
174 gst_proxy_pad_do_chain (GstPad * pad, GstBuffer * buffer)
177 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
179 res = gst_pad_push (internal, buffer);
185 gst_proxy_pad_do_chain_list (GstPad * pad, GstBufferList * list)
188 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
190 res = gst_pad_push_list (internal, list);
196 gst_proxy_pad_do_getrange (GstPad * pad, guint64 offset, guint size,
200 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
202 res = gst_pad_pull_range (internal, offset, size, buffer);
208 gst_proxy_pad_do_checkgetrange (GstPad * pad)
211 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
213 result = gst_pad_check_pull_range (internal);
219 gst_proxy_pad_do_getcaps (GstPad * pad)
221 GstPad *target = gst_proxy_pad_get_target (pad);
223 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
226 /* if we have a real target, proxy the call */
227 res = gst_pad_get_caps (target);
229 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
230 GST_DEBUG_PAD_NAME (target), res);
232 gst_object_unref (target);
234 /* filter against the template */
238 filt = GST_PAD_TEMPLATE_CAPS (templ);
240 tmp = gst_caps_intersect (filt, res);
241 gst_caps_unref (res);
243 GST_DEBUG_OBJECT (pad,
244 "filtered against template gives %" GST_PTR_FORMAT, res);
248 /* else, if we have a template, use its caps. */
250 res = GST_PAD_TEMPLATE_CAPS (templ);
251 GST_DEBUG_OBJECT (pad,
252 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
254 res = gst_caps_ref (res);
258 /* last resort, any caps */
259 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
260 res = gst_caps_new_any ();
268 gst_proxy_pad_do_acceptcaps (GstPad * pad, GstCaps * caps)
270 GstPad *target = gst_proxy_pad_get_target (pad);
274 res = gst_pad_accept_caps (target, caps);
275 gst_object_unref (target);
277 /* We don't have a target, we return TRUE and we assume that any future
278 * target will be able to deal with any configured caps. */
286 gst_proxy_pad_do_fixatecaps (GstPad * pad, GstCaps * caps)
288 GstPad *target = gst_proxy_pad_get_target (pad);
291 gst_pad_fixate_caps (target, caps);
292 gst_object_unref (target);
297 gst_proxy_pad_do_setcaps (GstPad * pad, GstCaps * caps)
299 GstPad *target = gst_proxy_pad_get_target (pad);
303 res = gst_pad_set_caps (target, caps);
304 gst_object_unref (target);
306 /* We don't have any target, but we shouldn't return FALSE since this
307 * would stop the actual push of a buffer (which might trigger a pad block
308 * or probe, or properly return GST_FLOW_NOT_LINKED.
316 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
321 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
323 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
324 goto wrong_direction;
326 GST_LOG_OBJECT (pad, "clearing target");
328 /* clear old target */
329 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
330 gst_object_unref (oldtarget);
332 /* set and ref new target if any */
334 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
336 GST_PROXY_PAD_TARGET (pad) = NULL;
343 GST_ERROR_OBJECT (pad,
344 "target pad doesn't have the same direction as ourself");
350 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
354 GST_PROXY_LOCK (pad);
355 result = gst_proxy_pad_set_target_unlocked (pad, target);
356 GST_PROXY_UNLOCK (pad);
362 gst_proxy_pad_get_target (GstPad * pad)
366 GST_PROXY_LOCK (pad);
367 target = GST_PROXY_PAD_TARGET (pad);
369 gst_object_ref (target);
370 GST_PROXY_UNLOCK (pad);
376 gst_proxy_pad_do_unlink (GstPad * pad)
380 /* don't do anything if this unlink resulted from retargeting the pad
381 * controlled by the ghostpad. We only want to invalidate the target pad when
382 * the element suddently unlinked with our internal pad. */
383 if (GST_PROXY_PAD_RETARGET (pad))
386 internal = GST_PROXY_PAD_INTERNAL (pad);
388 GST_DEBUG_OBJECT (pad, "pad is unlinked");
390 gst_proxy_pad_set_target (internal, NULL);
394 gst_proxy_pad_dispose (GObject * object)
396 GstPad *pad = GST_PAD (object);
399 GST_PROXY_LOCK (pad);
400 /* remove and unref the target */
401 target_p = &GST_PROXY_PAD_TARGET (pad);
402 gst_object_replace ((GstObject **) target_p, NULL);
403 /* The internal is only cleared by GstGhostPad::dispose, since it is the
404 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
406 GST_PROXY_UNLOCK (pad);
408 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
412 gst_proxy_pad_finalize (GObject * object)
414 GstProxyPad *pad = GST_PROXY_PAD (object);
416 g_mutex_free (GST_PROXY_GET_LOCK (pad));
417 GST_PROXY_GET_LOCK (pad) = NULL;
419 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
423 gst_proxy_pad_init (GstProxyPad * ppad)
425 GstPad *pad = (GstPad *) ppad;
427 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
428 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
429 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
431 gst_pad_set_query_type_function (pad,
432 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_query_type));
433 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_proxy_pad_do_event));
434 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_proxy_pad_do_query));
435 gst_pad_set_iterate_internal_links_function (pad,
436 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_iterate_internal_links));
438 gst_pad_set_getcaps_function (pad,
439 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_getcaps));
440 gst_pad_set_acceptcaps_function (pad,
441 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_acceptcaps));
442 gst_pad_set_fixatecaps_function (pad,
443 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_fixatecaps));
444 gst_pad_set_setcaps_function (pad,
445 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_setcaps));
446 gst_pad_set_unlink_function (pad,
447 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_unlink));
450 #ifndef GST_DISABLE_LOADSAVE
452 * gst_proxy_pad_save_thyself:
453 * @pad: a ghost #GstPad to save.
454 * @parent: the parent #xmlNodePtr to save the description in.
456 * Saves the ghost pad into an xml representation.
458 * Returns: the #xmlNodePtr representation of the pad.
461 gst_proxy_pad_save_thyself (GstObject * object, xmlNodePtr parent)
464 GstProxyPad *proxypad;
468 g_return_val_if_fail (GST_IS_PROXY_PAD (object), NULL);
470 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
471 xmlNewChild (self, NULL, (xmlChar *) "name",
472 (xmlChar *) GST_OBJECT_NAME (object));
473 xmlNewChild (self, NULL, (xmlChar *) "parent",
474 (xmlChar *) GST_OBJECT_NAME (GST_OBJECT_PARENT (object)));
476 proxypad = GST_PROXY_PAD_CAST (object);
477 pad = GST_PAD_CAST (proxypad);
478 peer = GST_PAD_CAST (pad->peer);
480 if (GST_IS_PAD (pad)) {
481 if (GST_PAD_IS_SRC (pad))
482 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "source");
483 else if (GST_PAD_IS_SINK (pad))
484 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
486 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
488 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
490 if (GST_IS_PAD (peer)) {
491 gchar *content = g_strdup_printf ("%s.%s",
492 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
494 xmlNewChild (self, NULL, (xmlChar *) "peer", (xmlChar *) content);
497 xmlNewChild (self, NULL, (xmlChar *) "peer", NULL);
502 #endif /* GST_DISABLE_LOADSAVE */
505 /***********************************************************************
506 * Ghost pads, implemented as a pair of proxy pads (sort of)
510 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
512 struct _GstGhostPadPrivate
514 /* with PROXY_LOCK */
517 gboolean constructed;
520 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
522 static void gst_ghost_pad_dispose (GObject * object);
525 gst_ghost_pad_class_init (GstGhostPadClass * klass)
527 GObjectClass *gobject_class = (GObjectClass *) klass;
529 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
531 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ghost_pad_dispose);
534 /* see gstghostpad design docs */
536 gst_ghost_pad_internal_do_activate_push (GstPad * pad, gboolean active)
541 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
542 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
544 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
545 * will be activated later (or already in case of a ghost sinkpad). */
546 other = GST_PROXY_PAD_INTERNAL (pad);
547 ret = gst_pad_activate_push (other, active);
553 gst_ghost_pad_internal_do_activate_pull (GstPad * pad, gboolean active)
558 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
559 GST_DEBUG_PAD_NAME (pad));
561 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
562 /* we are activated in pull mode by our peer element, which is a sinkpad
563 * that wants to operate in pull mode. This activation has to propagate
564 * upstream throught the pipeline. We call the internal activation function,
565 * which will trigger gst_ghost_pad_do_activate_pull, which propagates even
566 * further upstream */
567 GST_LOG_OBJECT (pad, "pad is src, activate internal");
568 other = GST_PROXY_PAD_INTERNAL (pad);
569 ret = gst_pad_activate_pull (other, active);
570 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
571 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
572 * since we hold a pointer to the upstream peer. */
573 GST_LOG_OBJECT (pad, "activating peer");
574 ret = gst_pad_activate_pull (other, active);
575 gst_object_unref (other);
577 /* this is failure, we can't activate pull if there is no peer */
578 GST_LOG_OBJECT (pad, "not src and no peer, failing");
586 gst_ghost_pad_do_activate_push (GstPad * pad, gboolean active)
591 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
592 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
594 /* just activate the internal pad */
595 other = GST_PROXY_PAD_INTERNAL (pad);
596 ret = gst_pad_activate_push (other, active);
602 gst_ghost_pad_do_activate_pull (GstPad * pad, gboolean active)
607 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
608 GST_DEBUG_PAD_NAME (pad));
610 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
611 /* the ghostpad is SRC and activated in pull mode by its peer, call the
612 * activation function of the internal pad to propagate the activation
614 GST_LOG_OBJECT (pad, "pad is src, activate internal");
615 other = GST_PROXY_PAD_INTERNAL (pad);
616 ret = gst_pad_activate_pull (other, active);
617 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
618 /* We are SINK and activated by the internal pad, propagate activation
619 * upstream because we hold a ref to the upstream peer */
620 GST_LOG_OBJECT (pad, "activating peer");
621 ret = gst_pad_activate_pull (other, active);
622 gst_object_unref (other);
624 /* no peer, we fail */
625 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
632 static GstPadLinkReturn
633 gst_ghost_pad_do_link (GstPad * pad, GstPad * peer)
635 GstPadLinkReturn ret;
638 GST_DEBUG_OBJECT (pad, "linking ghostpad");
640 internal = GST_PROXY_PAD_INTERNAL (pad);
641 if (!gst_proxy_pad_set_target (internal, peer))
644 ret = GST_PAD_LINK_OK;
645 /* if we are a source pad, we should call the peer link function
646 * if the peer has one, see design docs. */
647 if (GST_PAD_IS_SRC (pad)) {
648 if (GST_PAD_LINKFUNC (peer)) {
649 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
650 if (ret != GST_PAD_LINK_OK)
659 GST_DEBUG_OBJECT (pad, "setting target failed");
660 return GST_PAD_LINK_REFUSED;
664 GST_DEBUG_OBJECT (pad, "linking failed");
665 /* clear target again */
666 gst_proxy_pad_set_target (internal, NULL);
672 gst_ghost_pad_do_unlink (GstPad * pad)
676 internal = GST_PROXY_PAD_INTERNAL (pad);
678 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
680 /* The target of the internal pad is no longer valid */
681 gst_proxy_pad_set_target (internal, NULL);
685 on_int_notify (GstPad * internal, GParamSpec * unused, GstGhostPad * pad)
690 g_object_get (internal, "caps", &caps, NULL);
692 GST_DEBUG_OBJECT (pad, "notified %p %" GST_PTR_FORMAT, caps, caps);
694 GST_OBJECT_LOCK (pad);
695 changed = (GST_PAD_CAPS (pad) != caps);
697 gst_caps_replace (&(GST_PAD_CAPS (pad)), caps);
698 GST_OBJECT_UNLOCK (pad);
701 g_object_notify (G_OBJECT (pad), "caps");
704 gst_caps_unref (caps);
708 on_src_target_notify (GstPad * target, GParamSpec * unused, gpointer user_data)
710 GstProxyPad *proxypad;
715 g_object_get (target, "caps", &caps, NULL);
717 GST_OBJECT_LOCK (target);
718 /* First check if the peer is still available and our proxy pad */
719 if (!GST_PAD_PEER (target) || !GST_IS_PROXY_PAD (GST_PAD_PEER (target))) {
720 GST_OBJECT_UNLOCK (target);
721 gst_caps_unref (caps);
725 proxypad = GST_PROXY_PAD (GST_PAD_PEER (target));
726 GST_PROXY_LOCK (proxypad);
727 /* Now check if the proxypad's internal pad is still there and
729 if (!GST_PROXY_PAD_INTERNAL (proxypad) ||
730 !GST_IS_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad))) {
731 GST_OBJECT_UNLOCK (target);
732 GST_PROXY_UNLOCK (proxypad);
733 gst_caps_unref (caps);
736 gpad = GST_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad));
738 GST_PROXY_UNLOCK (proxypad);
739 GST_OBJECT_UNLOCK (target);
741 GST_OBJECT_LOCK (gpad);
743 GST_DEBUG_OBJECT (gpad, "notified %p %" GST_PTR_FORMAT, caps, caps);
745 changed = (GST_PAD_CAPS (gpad) != caps);
747 gst_caps_replace (&(GST_PAD_CAPS (gpad)), caps);
748 GST_OBJECT_UNLOCK (gpad);
750 g_object_notify (G_OBJECT (gpad), "caps");
752 g_object_unref (gpad);
755 gst_caps_unref (caps);
759 gst_ghost_pad_do_setcaps (GstPad * pad, GstCaps * caps)
761 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
764 return gst_proxy_pad_do_setcaps (pad, caps);
768 gst_ghost_pad_init (GstGhostPad * pad)
770 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
771 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
773 gst_pad_set_setcaps_function (GST_PAD_CAST (pad),
774 GST_DEBUG_FUNCPTR (gst_ghost_pad_do_setcaps));
775 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
776 GST_DEBUG_FUNCPTR (gst_ghost_pad_do_activate_pull));
777 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
778 GST_DEBUG_FUNCPTR (gst_ghost_pad_do_activate_push));
782 gst_ghost_pad_dispose (GObject * object)
788 pad = GST_PAD (object);
790 GST_DEBUG_OBJECT (pad, "dispose");
792 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
794 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
795 * gst_ghost_pad_do_unlink when the ghost pad is in an inconsistent state */
796 peer = gst_pad_get_peer (pad);
798 if (GST_PAD_IS_SRC (pad))
799 gst_pad_unlink (pad, peer);
801 gst_pad_unlink (peer, pad);
803 gst_object_unref (peer);
806 GST_PROXY_LOCK (pad);
807 internal = GST_PROXY_PAD_INTERNAL (pad);
809 gst_pad_set_activatepull_function (internal, NULL);
810 gst_pad_set_activatepush_function (internal, NULL);
812 g_signal_handler_disconnect (internal,
813 GST_GHOST_PAD_PRIVATE (pad)->notify_id);
815 /* disposes of the internal pad, since the ghostpad is the only possible object
816 * that has a refcount on the internal pad. */
817 gst_object_unparent (GST_OBJECT_CAST (internal));
818 GST_PROXY_PAD_INTERNAL (pad) = NULL;
820 GST_PROXY_UNLOCK (pad);
822 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
826 * gst_ghost_pad_construct:
827 * @gpad: the newly allocated ghost pad
829 * Finish initialization of a newly allocated ghost pad.
831 * This function is most useful in language bindings and when subclassing
832 * #GstGhostPad; plugin and application developers normally will not call this
833 * function. Call this function directly after a call to g_object_new
834 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
836 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
841 gst_ghost_pad_construct (GstGhostPad * gpad)
843 GstPadDirection dir, otherdir;
844 GstPadTemplate *templ;
845 GstPad *pad, *internal;
847 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
848 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
851 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
853 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
855 pad = GST_PAD (gpad);
857 /* Set directional padfunctions for ghostpad */
858 if (dir == GST_PAD_SINK) {
859 gst_pad_set_bufferalloc_function (pad,
860 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_bufferalloc));
861 gst_pad_set_chain_function (pad,
862 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_chain));
863 gst_pad_set_chain_list_function (pad,
864 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_chain_list));
866 gst_pad_set_getrange_function (pad,
867 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_getrange));
868 gst_pad_set_checkgetrange_function (pad,
869 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_checkgetrange));
872 /* link/unlink functions */
873 gst_pad_set_link_function (pad, GST_DEBUG_FUNCPTR (gst_ghost_pad_do_link));
874 gst_pad_set_unlink_function (pad,
875 GST_DEBUG_FUNCPTR (gst_ghost_pad_do_unlink));
877 /* INTERNAL PAD, it always exists and is child of the ghostpad */
878 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
881 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
882 "direction", otherdir, "template", templ, NULL);
883 /* release ref obtained via g_object_get */
884 gst_object_unref (templ);
887 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
888 "direction", otherdir, NULL);
890 GST_PAD_UNSET_FLUSHING (internal);
892 /* Set directional padfunctions for internal pad */
893 if (dir == GST_PAD_SRC) {
894 gst_pad_set_bufferalloc_function (internal,
895 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_bufferalloc));
896 gst_pad_set_chain_function (internal,
897 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_chain));
898 gst_pad_set_chain_list_function (internal,
899 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_chain_list));
901 gst_pad_set_getrange_function (internal,
902 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_getrange));
903 gst_pad_set_checkgetrange_function (internal,
904 GST_DEBUG_FUNCPTR (gst_proxy_pad_do_checkgetrange));
907 GST_PROXY_LOCK (pad);
909 /* now make the ghostpad a parent of the internal pad */
910 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
911 GST_OBJECT_CAST (pad)))
914 /* The ghostpad is the parent of the internal pad and is the only object that
915 * can have a refcount on the internal pad.
916 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
918 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
919 * it's refcount on the internal pad in the dispose method by un-parenting it.
920 * This is why we don't take extra refcounts in the assignments below
922 GST_PROXY_PAD_INTERNAL (pad) = internal;
923 GST_PROXY_PAD_INTERNAL (internal) = pad;
925 /* could be more general here, iterating over all writable properties...
926 * taking the short road for now tho */
927 GST_GHOST_PAD_PRIVATE (pad)->notify_id =
928 g_signal_connect (internal, "notify::caps", G_CALLBACK (on_int_notify),
931 /* call function to init values of the pad caps */
932 on_int_notify (internal, NULL, GST_GHOST_PAD_CAST (pad));
934 /* special activation functions for the internal pad */
935 gst_pad_set_activatepull_function (internal,
936 GST_DEBUG_FUNCPTR (gst_ghost_pad_internal_do_activate_pull));
937 gst_pad_set_activatepush_function (internal,
938 GST_DEBUG_FUNCPTR (gst_ghost_pad_internal_do_activate_push));
940 GST_PROXY_UNLOCK (pad);
942 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
948 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
949 GST_DEBUG_PAD_NAME (internal));
950 g_critical ("Could not set internal pad %s:%s",
951 GST_DEBUG_PAD_NAME (internal));
952 GST_PROXY_UNLOCK (pad);
953 gst_object_unref (internal);
959 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
960 GstPadTemplate * templ)
964 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
966 /* OBJECT CREATION */
968 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
969 "direction", dir, "template", templ, NULL);
971 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
972 "direction", dir, NULL);
975 if (!gst_ghost_pad_construct (ret))
976 goto construct_failed;
978 return GST_PAD (ret);
982 gst_object_unref (ret);
987 * gst_ghost_pad_new_no_target:
988 * @name: the name of the new pad, or NULL to assign a default name.
989 * @dir: the direction of the ghostpad
991 * Create a new ghostpad without a target with the given direction.
992 * A target can be set on the ghostpad later with the
993 * gst_ghost_pad_set_target() function.
995 * The created ghostpad will not have a padtemplate.
997 * Returns: a new #GstPad, or NULL in case of an error.
1000 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1004 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1006 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1008 ret = gst_ghost_pad_new_full (name, dir, NULL);
1014 * gst_ghost_pad_new:
1015 * @name: the name of the new pad, or NULL to assign a default name.
1016 * @target: the pad to ghost.
1018 * Create a new ghostpad with @target as the target. The direction will be taken
1019 * from the target pad. @target must be unlinked.
1021 * Will ref the target.
1023 * Returns: a new #GstPad, or NULL in case of an error.
1026 gst_ghost_pad_new (const gchar * name, GstPad * target)
1030 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1031 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1033 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1034 GST_DEBUG_PAD_NAME (target));
1036 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1037 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1038 goto set_target_failed;
1045 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1046 GST_DEBUG_PAD_NAME (target));
1047 gst_object_unref (ret);
1053 * gst_ghost_pad_new_from_template:
1054 * @name: the name of the new pad, or NULL to assign a default name.
1055 * @target: the pad to ghost.
1056 * @templ: the #GstPadTemplate to use on the ghostpad.
1058 * Create a new ghostpad with @target as the target. The direction will be taken
1059 * from the target pad. The template used on the ghostpad will be @template.
1061 * Will ref the target.
1063 * Returns: a new #GstPad, or NULL in case of an error.
1069 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1070 GstPadTemplate * templ)
1074 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1075 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1076 g_return_val_if_fail (templ != NULL, NULL);
1077 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1078 GST_PAD_DIRECTION (target), NULL);
1080 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1081 GST_DEBUG_PAD_NAME (target), templ);
1083 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1084 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1085 goto set_target_failed;
1092 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1093 GST_DEBUG_PAD_NAME (target));
1094 gst_object_unref (ret);
1100 * gst_ghost_pad_new_no_target_from_template:
1101 * @name: the name of the new pad, or NULL to assign a default name.
1102 * @templ: the #GstPadTemplate to create the ghostpad from.
1104 * Create a new ghostpad based on @templ, without setting a target. The
1105 * direction will be taken from the @templ.
1107 * Returns: a new #GstPad, or NULL in case of an error.
1112 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1113 GstPadTemplate * templ)
1117 g_return_val_if_fail (templ != NULL, NULL);
1120 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1126 * gst_ghost_pad_get_target:
1127 * @gpad: the #GstGhostpad
1129 * Get the target pad of #gpad. Unref target pad after usage.
1131 * Returns: the target #GstPad, can be NULL if the ghostpad
1132 * has no target set. Unref target pad after usage.
1135 gst_ghost_pad_get_target (GstGhostPad * gpad)
1139 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1141 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1143 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1149 * gst_ghost_pad_set_target:
1150 * @gpad: the #GstGhostpad
1151 * @newtarget: the new pad target
1153 * Set the new target of the ghostpad @gpad. Any existing target
1154 * is unlinked and links to the new target are established. if @newtarget is
1155 * NULL the target will be cleared.
1157 * Returns: TRUE if the new target could be set. This function can return FALSE
1158 * when the internal pads could not be linked.
1161 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1166 GstPadLinkReturn lret;
1168 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1170 GST_PROXY_LOCK (gpad);
1171 internal = GST_PROXY_PAD_INTERNAL (gpad);
1172 g_assert (internal);
1175 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1177 GST_DEBUG_OBJECT (gpad, "clearing target");
1179 /* clear old target */
1180 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1181 if (GST_PAD_IS_SRC (oldtarget)) {
1182 g_signal_handlers_disconnect_by_func (oldtarget,
1183 (gpointer) on_src_target_notify, NULL);
1186 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1188 /* unlink internal pad */
1189 if (GST_PAD_IS_SRC (internal))
1190 gst_pad_unlink (internal, oldtarget);
1192 gst_pad_unlink (oldtarget, internal);
1194 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1197 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1199 if (result && newtarget) {
1200 if (GST_PAD_IS_SRC (newtarget)) {
1201 g_signal_connect (newtarget, "notify::caps",
1202 G_CALLBACK (on_src_target_notify), NULL);
1205 /* and link to internal pad */
1206 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1208 if (GST_PAD_IS_SRC (internal))
1209 lret = gst_pad_link (internal, newtarget);
1211 lret = gst_pad_link (newtarget, internal);
1213 if (lret != GST_PAD_LINK_OK)
1216 GST_PROXY_UNLOCK (gpad);
1223 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1225 /* and unset target again */
1226 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1227 GST_PROXY_UNLOCK (gpad);