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);
77 static void gst_proxy_pad_dispose (GObject * object);
78 static void gst_proxy_pad_finalize (GObject * object);
80 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
81 #ifdef GST_DISABLE_DEPRECATED
82 #include <libxml/parser.h>
84 static xmlNodePtr gst_proxy_pad_save_thyself (GstObject * object,
88 static void on_src_target_notify (GstPad * target,
89 GParamSpec * unused, gpointer user_data);
91 static GParamSpec *pspec_caps = NULL;
93 static const GstQueryType *
94 gst_proxy_pad_do_query_type (GstPad * pad)
96 GstPad *target = gst_proxy_pad_get_target (pad);
97 const GstQueryType *res = NULL;
100 res = gst_pad_get_query_types (target);
101 gst_object_unref (target);
107 gst_proxy_pad_do_event (GstPad * pad, GstEvent * event)
109 gboolean res = FALSE;
110 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
112 res = gst_pad_push_event (internal, event);
118 gst_proxy_pad_do_query (GstPad * pad, GstQuery * query)
120 gboolean res = FALSE;
121 GstPad *target = gst_proxy_pad_get_target (pad);
124 res = gst_pad_query (target, query);
125 gst_object_unref (target);
132 gst_proxy_pad_do_iterate_internal_links (GstPad * pad)
134 GstIterator *res = NULL;
135 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
139 gst_iterator_new_single (GST_TYPE_PAD, internal,
140 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
147 gst_proxy_pad_do_bufferalloc (GstPad * pad, guint64 offset, guint size,
148 GstCaps * caps, GstBuffer ** buf)
150 GstFlowReturn result;
151 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
153 result = gst_pad_alloc_buffer (internal, offset, size, caps, buf);
159 gst_proxy_pad_do_chain (GstPad * pad, GstBuffer * buffer)
162 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
164 res = gst_pad_push (internal, buffer);
170 gst_proxy_pad_do_chain_list (GstPad * pad, GstBufferList * list)
173 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
175 res = gst_pad_push_list (internal, list);
181 gst_proxy_pad_do_getrange (GstPad * pad, guint64 offset, guint size,
185 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
187 res = gst_pad_pull_range (internal, offset, size, buffer);
193 gst_proxy_pad_do_checkgetrange (GstPad * pad)
196 GstPad *internal = GST_PROXY_PAD_INTERNAL (pad);
198 result = gst_pad_check_pull_range (internal);
204 gst_proxy_pad_do_getcaps (GstPad * pad)
206 GstPad *target = gst_proxy_pad_get_target (pad);
208 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
211 /* if we have a real target, proxy the call */
212 res = gst_pad_get_caps_reffed (target);
214 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
215 GST_DEBUG_PAD_NAME (target), res);
217 gst_object_unref (target);
219 /* filter against the template */
223 filt = GST_PAD_TEMPLATE_CAPS (templ);
225 tmp = gst_caps_intersect (filt, res);
226 gst_caps_unref (res);
228 GST_DEBUG_OBJECT (pad,
229 "filtered against template gives %" GST_PTR_FORMAT, res);
233 /* else, if we have a template, use its caps. */
235 res = GST_PAD_TEMPLATE_CAPS (templ);
236 GST_DEBUG_OBJECT (pad,
237 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
239 res = gst_caps_ref (res);
243 /* last resort, any caps */
244 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
245 res = gst_caps_new_any ();
253 gst_proxy_pad_do_acceptcaps (GstPad * pad, GstCaps * caps)
255 GstPad *target = gst_proxy_pad_get_target (pad);
259 res = gst_pad_accept_caps (target, caps);
260 gst_object_unref (target);
262 /* We don't have a target, we return TRUE and we assume that any future
263 * target will be able to deal with any configured caps. */
271 gst_proxy_pad_do_fixatecaps (GstPad * pad, GstCaps * caps)
273 GstPad *target = gst_proxy_pad_get_target (pad);
276 gst_pad_fixate_caps (target, caps);
277 gst_object_unref (target);
282 gst_proxy_pad_do_setcaps (GstPad * pad, GstCaps * caps)
284 GstPad *target = gst_proxy_pad_get_target (pad);
288 res = gst_pad_set_caps (target, caps);
289 gst_object_unref (target);
291 /* We don't have any target, but we shouldn't return FALSE since this
292 * would stop the actual push of a buffer (which might trigger a pad block
293 * or probe, or properly return GST_FLOW_NOT_LINKED.
301 gst_proxy_pad_set_target_unlocked (GstPad * pad, GstPad * target)
306 GST_LOG_OBJECT (pad, "setting target %s:%s", GST_DEBUG_PAD_NAME (target));
308 if (G_UNLIKELY (GST_PAD_DIRECTION (pad) != GST_PAD_DIRECTION (target)))
309 goto wrong_direction;
311 GST_LOG_OBJECT (pad, "clearing target");
313 /* clear old target */
314 if ((oldtarget = GST_PROXY_PAD_TARGET (pad)))
315 gst_object_unref (oldtarget);
317 /* set and ref new target if any */
319 GST_PROXY_PAD_TARGET (pad) = gst_object_ref (target);
321 GST_PROXY_PAD_TARGET (pad) = NULL;
328 GST_ERROR_OBJECT (pad,
329 "target pad doesn't have the same direction as ourself");
335 gst_proxy_pad_set_target (GstPad * pad, GstPad * target)
339 GST_PROXY_LOCK (pad);
340 result = gst_proxy_pad_set_target_unlocked (pad, target);
341 GST_PROXY_UNLOCK (pad);
347 gst_proxy_pad_get_target (GstPad * pad)
351 GST_PROXY_LOCK (pad);
352 target = GST_PROXY_PAD_TARGET (pad);
354 gst_object_ref (target);
355 GST_PROXY_UNLOCK (pad);
361 gst_proxy_pad_do_unlink (GstPad * pad)
365 /* don't do anything if this unlink resulted from retargeting the pad
366 * controlled by the ghostpad. We only want to invalidate the target pad when
367 * the element suddently unlinked with our internal pad. */
368 if (GST_PROXY_PAD_RETARGET (pad))
371 internal = GST_PROXY_PAD_INTERNAL (pad);
373 GST_DEBUG_OBJECT (pad, "pad is unlinked");
375 gst_proxy_pad_set_target (internal, NULL);
379 gst_proxy_pad_class_init (GstProxyPadClass * klass)
381 GObjectClass *gobject_class = (GObjectClass *) klass;
383 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
385 gobject_class->dispose = gst_proxy_pad_dispose;
386 gobject_class->finalize = gst_proxy_pad_finalize;
388 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
390 GstObjectClass *gstobject_class = (GstObjectClass *) klass;
392 gstobject_class->save_thyself =
393 ((gpointer (*)(GstObject * object,
395 GST_DEBUG_FUNCPTR (gst_proxy_pad_save_thyself));
398 /* Register common function pointer descriptions */
399 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_query_type);
400 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_event);
401 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_query);
402 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_iterate_internal_links);
403 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_getcaps);
404 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_acceptcaps);
405 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_fixatecaps);
406 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_setcaps);
407 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_unlink);
408 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_bufferalloc);
409 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_chain);
410 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_chain_list);
411 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_getrange);
412 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_do_checkgetrange);
416 gst_proxy_pad_dispose (GObject * object)
418 GstPad *pad = GST_PAD (object);
421 GST_PROXY_LOCK (pad);
422 /* remove and unref the target */
423 target_p = &GST_PROXY_PAD_TARGET (pad);
424 gst_object_replace ((GstObject **) target_p, NULL);
425 /* The internal is only cleared by GstGhostPad::dispose, since it is the
426 * parent of non-ghost GstProxyPad and owns the refcount on the internal.
428 GST_PROXY_UNLOCK (pad);
430 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->dispose (object);
434 gst_proxy_pad_finalize (GObject * object)
436 GstProxyPad *pad = GST_PROXY_PAD (object);
438 g_mutex_free (GST_PROXY_GET_LOCK (pad));
439 GST_PROXY_GET_LOCK (pad) = NULL;
441 G_OBJECT_CLASS (gst_proxy_pad_parent_class)->finalize (object);
445 gst_proxy_pad_init (GstProxyPad * ppad)
447 GstPad *pad = (GstPad *) ppad;
449 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
450 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
451 GST_PROXY_GET_LOCK (pad) = g_mutex_new ();
453 gst_pad_set_query_type_function (pad, gst_proxy_pad_do_query_type);
454 gst_pad_set_event_function (pad, gst_proxy_pad_do_event);
455 gst_pad_set_query_function (pad, gst_proxy_pad_do_query);
456 gst_pad_set_iterate_internal_links_function (pad,
457 gst_proxy_pad_do_iterate_internal_links);
459 gst_pad_set_getcaps_function (pad, gst_proxy_pad_do_getcaps);
460 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_do_acceptcaps);
461 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_do_fixatecaps);
462 gst_pad_set_setcaps_function (pad, gst_proxy_pad_do_setcaps);
463 gst_pad_set_unlink_function (pad, gst_proxy_pad_do_unlink);
466 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
468 * gst_proxy_pad_save_thyself:
469 * @pad: a ghost #GstPad to save.
470 * @parent: the parent #xmlNodePtr to save the description in.
472 * Saves the ghost pad into an xml representation.
474 * Returns: the #xmlNodePtr representation of the pad.
477 gst_proxy_pad_save_thyself (GstObject * object, xmlNodePtr parent)
480 GstProxyPad *proxypad;
484 g_return_val_if_fail (GST_IS_PROXY_PAD (object), NULL);
486 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
487 xmlNewChild (self, NULL, (xmlChar *) "name",
488 (xmlChar *) GST_OBJECT_NAME (object));
489 xmlNewChild (self, NULL, (xmlChar *) "parent",
490 (xmlChar *) GST_OBJECT_NAME (GST_OBJECT_PARENT (object)));
492 proxypad = GST_PROXY_PAD_CAST (object);
493 pad = GST_PAD_CAST (proxypad);
494 peer = GST_PAD_CAST (pad->peer);
496 if (GST_IS_PAD (pad)) {
497 if (GST_PAD_IS_SRC (pad))
498 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "source");
499 else if (GST_PAD_IS_SINK (pad))
500 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
502 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
504 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
506 if (GST_IS_PAD (peer)) {
507 gchar *content = g_strdup_printf ("%s.%s",
508 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
510 xmlNewChild (self, NULL, (xmlChar *) "peer", (xmlChar *) content);
513 xmlNewChild (self, NULL, (xmlChar *) "peer", NULL);
518 #endif /* GST_DISABLE_LOADSAVE */
521 /***********************************************************************
522 * Ghost pads, implemented as a pair of proxy pads (sort of)
526 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
528 struct _GstGhostPadPrivate
530 /* with PROXY_LOCK */
533 gboolean constructed;
536 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
538 static void gst_ghost_pad_dispose (GObject * object);
540 /* see gstghostpad design docs */
542 gst_ghost_pad_internal_do_activate_push (GstPad * pad, gboolean active)
547 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
548 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
550 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
551 * will be activated later (or already in case of a ghost sinkpad). */
552 other = GST_PROXY_PAD_INTERNAL (pad);
553 ret = gst_pad_activate_push (other, active);
559 gst_ghost_pad_internal_do_activate_pull (GstPad * pad, gboolean active)
564 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
565 GST_DEBUG_PAD_NAME (pad));
567 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
568 /* we are activated in pull mode by our peer element, which is a sinkpad
569 * that wants to operate in pull mode. This activation has to propagate
570 * upstream throught the pipeline. We call the internal activation function,
571 * which will trigger gst_ghost_pad_do_activate_pull, which propagates even
572 * further upstream */
573 GST_LOG_OBJECT (pad, "pad is src, activate internal");
574 other = GST_PROXY_PAD_INTERNAL (pad);
575 ret = gst_pad_activate_pull (other, active);
576 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
577 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
578 * since we hold a pointer to the upstream peer. */
579 GST_LOG_OBJECT (pad, "activating peer");
580 ret = gst_pad_activate_pull (other, active);
581 gst_object_unref (other);
583 /* this is failure, we can't activate pull if there is no peer */
584 GST_LOG_OBJECT (pad, "not src and no peer, failing");
592 gst_ghost_pad_do_activate_push (GstPad * pad, gboolean active)
597 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
598 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
600 /* just activate the internal pad */
601 other = GST_PROXY_PAD_INTERNAL (pad);
602 ret = gst_pad_activate_push (other, active);
608 gst_ghost_pad_do_activate_pull (GstPad * pad, gboolean active)
613 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
614 GST_DEBUG_PAD_NAME (pad));
616 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
617 /* the ghostpad is SRC and activated in pull mode by its peer, call the
618 * activation function of the internal pad to propagate the activation
620 GST_LOG_OBJECT (pad, "pad is src, activate internal");
621 other = GST_PROXY_PAD_INTERNAL (pad);
622 ret = gst_pad_activate_pull (other, active);
623 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
624 /* We are SINK and activated by the internal pad, propagate activation
625 * upstream because we hold a ref to the upstream peer */
626 GST_LOG_OBJECT (pad, "activating peer");
627 ret = gst_pad_activate_pull (other, active);
628 gst_object_unref (other);
630 /* no peer, we fail */
631 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
638 static GstPadLinkReturn
639 gst_ghost_pad_do_link (GstPad * pad, GstPad * peer)
641 GstPadLinkReturn ret;
644 GST_DEBUG_OBJECT (pad, "linking ghostpad");
646 internal = GST_PROXY_PAD_INTERNAL (pad);
647 if (!gst_proxy_pad_set_target (internal, peer))
650 ret = GST_PAD_LINK_OK;
651 /* if we are a source pad, we should call the peer link function
652 * if the peer has one, see design docs. */
653 if (GST_PAD_IS_SRC (pad)) {
654 if (GST_PAD_LINKFUNC (peer)) {
655 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
656 if (ret != GST_PAD_LINK_OK)
665 GST_DEBUG_OBJECT (pad, "setting target failed");
666 return GST_PAD_LINK_REFUSED;
670 GST_DEBUG_OBJECT (pad, "linking failed");
671 /* clear target again */
672 gst_proxy_pad_set_target (internal, NULL);
678 gst_ghost_pad_do_unlink (GstPad * pad)
682 internal = GST_PROXY_PAD_INTERNAL (pad);
684 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
686 /* The target of the internal pad is no longer valid */
687 gst_proxy_pad_set_target (internal, NULL);
691 on_int_notify (GstPad * internal, GParamSpec * unused, GstGhostPad * pad)
696 g_object_get (internal, "caps", &caps, NULL);
698 GST_DEBUG_OBJECT (pad, "notified %p %" GST_PTR_FORMAT, caps, caps);
700 GST_OBJECT_LOCK (pad);
701 changed = (GST_PAD_CAPS (pad) != caps);
703 gst_caps_replace (&(GST_PAD_CAPS (pad)), caps);
704 GST_OBJECT_UNLOCK (pad);
707 #if GLIB_CHECK_VERSION(2,26,0)
708 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
710 g_object_notify ((GObject *) pad, "caps");
715 gst_caps_unref (caps);
719 on_src_target_notify (GstPad * target, GParamSpec * unused, gpointer user_data)
721 GstProxyPad *proxypad;
726 g_object_get (target, "caps", &caps, NULL);
728 GST_OBJECT_LOCK (target);
729 /* First check if the peer is still available and our proxy pad */
730 if (!GST_PAD_PEER (target) || !GST_IS_PROXY_PAD (GST_PAD_PEER (target))) {
731 GST_OBJECT_UNLOCK (target);
735 proxypad = GST_PROXY_PAD (GST_PAD_PEER (target));
736 GST_PROXY_LOCK (proxypad);
737 /* Now check if the proxypad's internal pad is still there and
739 if (!GST_PROXY_PAD_INTERNAL (proxypad) ||
740 !GST_IS_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad))) {
741 GST_OBJECT_UNLOCK (target);
742 GST_PROXY_UNLOCK (proxypad);
745 gpad = GST_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad));
747 GST_PROXY_UNLOCK (proxypad);
748 GST_OBJECT_UNLOCK (target);
750 GST_OBJECT_LOCK (gpad);
752 GST_DEBUG_OBJECT (gpad, "notified %p %" GST_PTR_FORMAT, caps, caps);
754 changed = (GST_PAD_CAPS (gpad) != caps);
756 gst_caps_replace (&(GST_PAD_CAPS (gpad)), caps);
757 GST_OBJECT_UNLOCK (gpad);
760 #if GLIB_CHECK_VERSION(2,26,0)
761 g_object_notify_by_pspec ((GObject *) gpad, pspec_caps);
763 g_object_notify ((GObject *) gpad, "caps");
767 g_object_unref (gpad);
771 gst_caps_unref (caps);
775 gst_ghost_pad_do_setcaps (GstPad * pad, GstCaps * caps)
777 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
780 return gst_proxy_pad_do_setcaps (pad, caps);
784 gst_ghost_pad_do_iterate_internal_links (GstPad * pad)
786 GstIterator *res = NULL;
787 GstPad *internal = GST_PROXY_PAD_INTERNAL (GST_GHOST_PAD_CAST (pad));
791 gst_iterator_new_single (GST_TYPE_PAD, internal,
792 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
799 gst_ghost_pad_class_init (GstGhostPadClass * klass)
801 GObjectClass *gobject_class = (GObjectClass *) klass;
803 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
805 pspec_caps = g_object_class_find_property (gobject_class, "caps");
807 gobject_class->dispose = gst_ghost_pad_dispose;
809 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_setcaps);
810 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_activate_pull);
811 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_activate_push);
812 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_do_link);
816 gst_ghost_pad_init (GstGhostPad * pad)
818 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
819 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
821 gst_pad_set_setcaps_function (GST_PAD_CAST (pad), gst_ghost_pad_do_setcaps);
822 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
823 gst_ghost_pad_do_activate_pull);
824 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
825 gst_ghost_pad_do_activate_push);
826 gst_pad_set_iterate_internal_links_function (GST_PAD_CAST (pad),
827 gst_ghost_pad_do_iterate_internal_links);
831 gst_ghost_pad_dispose (GObject * object)
837 pad = GST_PAD (object);
839 GST_DEBUG_OBJECT (pad, "dispose");
841 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
843 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
844 * gst_ghost_pad_do_unlink when the ghost pad is in an inconsistent state */
845 peer = gst_pad_get_peer (pad);
847 if (GST_PAD_IS_SRC (pad))
848 gst_pad_unlink (pad, peer);
850 gst_pad_unlink (peer, pad);
852 gst_object_unref (peer);
855 GST_PROXY_LOCK (pad);
856 internal = GST_PROXY_PAD_INTERNAL (pad);
858 gst_pad_set_activatepull_function (internal, NULL);
859 gst_pad_set_activatepush_function (internal, NULL);
861 g_signal_handler_disconnect (internal,
862 GST_GHOST_PAD_PRIVATE (pad)->notify_id);
864 /* disposes of the internal pad, since the ghostpad is the only possible object
865 * that has a refcount on the internal pad. */
866 gst_object_unparent (GST_OBJECT_CAST (internal));
867 GST_PROXY_PAD_INTERNAL (pad) = NULL;
869 GST_PROXY_UNLOCK (pad);
871 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
875 * gst_ghost_pad_construct:
876 * @gpad: the newly allocated ghost pad
878 * Finish initialization of a newly allocated ghost pad.
880 * This function is most useful in language bindings and when subclassing
881 * #GstGhostPad; plugin and application developers normally will not call this
882 * function. Call this function directly after a call to g_object_new
883 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
885 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
890 gst_ghost_pad_construct (GstGhostPad * gpad)
892 GstPadDirection dir, otherdir;
893 GstPadTemplate *templ;
894 GstPad *pad, *internal;
896 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
897 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
900 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
902 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
904 pad = GST_PAD (gpad);
906 /* Set directional padfunctions for ghostpad */
907 if (dir == GST_PAD_SINK) {
908 gst_pad_set_bufferalloc_function (pad, gst_proxy_pad_do_bufferalloc);
909 gst_pad_set_chain_function (pad, gst_proxy_pad_do_chain);
910 gst_pad_set_chain_list_function (pad, gst_proxy_pad_do_chain_list);
912 gst_pad_set_getrange_function (pad, gst_proxy_pad_do_getrange);
913 gst_pad_set_checkgetrange_function (pad, gst_proxy_pad_do_checkgetrange);
916 /* link/unlink functions */
917 gst_pad_set_link_function (pad, gst_ghost_pad_do_link);
918 gst_pad_set_unlink_function (pad, gst_ghost_pad_do_unlink);
920 /* INTERNAL PAD, it always exists and is child of the ghostpad */
921 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
924 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
925 "direction", otherdir, "template", templ, NULL);
926 /* release ref obtained via g_object_get */
927 gst_object_unref (templ);
930 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
931 "direction", otherdir, NULL);
933 GST_PAD_UNSET_FLUSHING (internal);
935 /* Set directional padfunctions for internal pad */
936 if (dir == GST_PAD_SRC) {
937 gst_pad_set_bufferalloc_function (internal, gst_proxy_pad_do_bufferalloc);
938 gst_pad_set_chain_function (internal, gst_proxy_pad_do_chain);
939 gst_pad_set_chain_list_function (internal, gst_proxy_pad_do_chain_list);
941 gst_pad_set_getrange_function (internal, gst_proxy_pad_do_getrange);
942 gst_pad_set_checkgetrange_function (internal,
943 gst_proxy_pad_do_checkgetrange);
946 GST_PROXY_LOCK (pad);
948 /* now make the ghostpad a parent of the internal pad */
949 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
950 GST_OBJECT_CAST (pad)))
953 /* The ghostpad is the parent of the internal pad and is the only object that
954 * can have a refcount on the internal pad.
955 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
957 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
958 * its refcount on the internal pad in the dispose method by un-parenting it.
959 * This is why we don't take extra refcounts in the assignments below
961 GST_PROXY_PAD_INTERNAL (pad) = internal;
962 GST_PROXY_PAD_INTERNAL (internal) = pad;
964 /* could be more general here, iterating over all writable properties...
965 * taking the short road for now tho */
966 GST_GHOST_PAD_PRIVATE (pad)->notify_id =
967 g_signal_connect (internal, "notify::caps", G_CALLBACK (on_int_notify),
970 /* call function to init values of the pad caps */
971 on_int_notify (internal, NULL, GST_GHOST_PAD_CAST (pad));
973 /* special activation functions for the internal pad */
974 gst_pad_set_activatepull_function (internal,
975 gst_ghost_pad_internal_do_activate_pull);
976 gst_pad_set_activatepush_function (internal,
977 gst_ghost_pad_internal_do_activate_push);
979 GST_PROXY_UNLOCK (pad);
981 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
987 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
988 GST_DEBUG_PAD_NAME (internal));
989 g_critical ("Could not set internal pad %s:%s",
990 GST_DEBUG_PAD_NAME (internal));
991 GST_PROXY_UNLOCK (pad);
992 gst_object_unref (internal);
998 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
999 GstPadTemplate * templ)
1003 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1005 /* OBJECT CREATION */
1007 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1008 "direction", dir, "template", templ, NULL);
1010 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1011 "direction", dir, NULL);
1014 if (!gst_ghost_pad_construct (ret))
1015 goto construct_failed;
1017 return GST_PAD_CAST (ret);
1020 /* already logged */
1021 gst_object_unref (ret);
1026 * gst_ghost_pad_new_no_target:
1027 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1028 * @dir: the direction of the ghostpad
1030 * Create a new ghostpad without a target with the given direction.
1031 * A target can be set on the ghostpad later with the
1032 * gst_ghost_pad_set_target() function.
1034 * The created ghostpad will not have a padtemplate.
1036 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1039 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1043 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1045 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1047 ret = gst_ghost_pad_new_full (name, dir, NULL);
1053 * gst_ghost_pad_new:
1054 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1055 * @target: (transfer none): the pad to ghost.
1057 * Create a new ghostpad with @target as the target. The direction will be taken
1058 * from the target pad. @target must be unlinked.
1060 * Will ref the target.
1062 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1065 gst_ghost_pad_new (const gchar * name, GstPad * target)
1069 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1070 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1072 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1073 GST_DEBUG_PAD_NAME (target));
1075 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1076 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1077 goto set_target_failed;
1084 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1085 GST_DEBUG_PAD_NAME (target));
1086 gst_object_unref (ret);
1092 * gst_ghost_pad_new_from_template:
1093 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1094 * @target: (transfer none): the pad to ghost.
1095 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1097 * Create a new ghostpad with @target as the target. The direction will be taken
1098 * from the target pad. The template used on the ghostpad will be @template.
1100 * Will ref the target.
1102 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1108 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1109 GstPadTemplate * templ)
1113 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1114 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1115 g_return_val_if_fail (templ != NULL, NULL);
1116 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1117 GST_PAD_DIRECTION (target), NULL);
1119 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1120 GST_DEBUG_PAD_NAME (target), templ);
1122 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1123 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1124 goto set_target_failed;
1131 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1132 GST_DEBUG_PAD_NAME (target));
1133 gst_object_unref (ret);
1139 * gst_ghost_pad_new_no_target_from_template:
1140 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1141 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1143 * Create a new ghostpad based on @templ, without setting a target. The
1144 * direction will be taken from the @templ.
1146 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1151 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1152 GstPadTemplate * templ)
1156 g_return_val_if_fail (templ != NULL, NULL);
1159 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1165 * gst_ghost_pad_get_target:
1166 * @gpad: the #GstGhostPad
1168 * Get the target pad of @gpad. Unref target pad after usage.
1170 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1171 * has no target set. Unref target pad after usage.
1174 gst_ghost_pad_get_target (GstGhostPad * gpad)
1178 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1180 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1182 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1188 * gst_ghost_pad_set_target:
1189 * @gpad: the #GstGhostPad
1190 * @newtarget: (transfer none) (allow-none): the new pad target
1192 * Set the new target of the ghostpad @gpad. Any existing target
1193 * is unlinked and links to the new target are established. if @newtarget is
1194 * NULL the target will be cleared.
1196 * Returns: (transfer full): TRUE if the new target could be set. This function
1197 * can return FALSE when the internal pads could not be linked.
1200 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1205 GstPadLinkReturn lret;
1207 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1208 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1209 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1211 /* no need for locking, the internal pad's lifecycle is directly linked to the
1213 internal = GST_PROXY_PAD_INTERNAL (gpad);
1216 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1218 GST_DEBUG_OBJECT (gpad, "clearing target");
1220 /* clear old target */
1221 GST_PROXY_LOCK (gpad);
1222 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1223 if (GST_PAD_IS_SRC (oldtarget)) {
1224 g_signal_handlers_disconnect_by_func (oldtarget,
1225 (gpointer) on_src_target_notify, NULL);
1228 GST_PROXY_PAD_RETARGET (internal) = TRUE;
1230 /* unlink internal pad */
1231 if (GST_PAD_IS_SRC (internal))
1232 gst_pad_unlink (internal, oldtarget);
1234 gst_pad_unlink (oldtarget, internal);
1236 GST_PROXY_PAD_RETARGET (internal) = FALSE;
1239 result = gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), newtarget);
1240 GST_PROXY_UNLOCK (gpad);
1242 if (result && newtarget) {
1243 if (GST_PAD_IS_SRC (newtarget)) {
1244 g_signal_connect (newtarget, "notify::caps",
1245 G_CALLBACK (on_src_target_notify), NULL);
1248 /* and link to internal pad without any checks */
1249 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1251 if (GST_PAD_IS_SRC (internal))
1253 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1256 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1258 if (lret != GST_PAD_LINK_OK)
1267 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1269 /* and unset target again */
1270 GST_PROXY_LOCK (gpad);
1271 gst_proxy_pad_set_target_unlocked (GST_PAD_CAST (gpad), NULL);
1272 GST_PROXY_UNLOCK (gpad);