2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Andy Wingo <wingo@pobox.com>
5 * 2006 Edward Hervey <bilboed@bilboed.com>
7 * gstghostpad.c: Proxy pads
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 * @short_description: Pseudo link pads
30 * GhostPads are useful when organizing pipelines with #GstBin like elements.
31 * The idea here is to create hierarchical element graphs. The bin element
32 * contains a sub-graph. Now one would like to treat the bin-element like any
33 * other #GstElement. This is where GhostPads come into play. A GhostPad acts as
34 * a proxy for another pad. Thus the bin can have sink and source ghost-pads
35 * that are associated with sink and source pads of the child elements.
37 * If the target pad is known at creation time, gst_ghost_pad_new() is the
38 * function to use to get a ghost-pad. Otherwise one can use gst_ghost_pad_new_no_target()
39 * to create the ghost-pad and use gst_ghost_pad_set_target() to establish the
40 * association later on.
42 * Note that GhostPads add overhead to the data processing of a pipeline.
44 * Last reviewed on 2005-11-18 (0.9.5)
47 #include "gst_private.h"
50 #include "gstghostpad.h"
53 #define GST_CAT_DEFAULT GST_CAT_PADS
55 #define GST_PROXY_PAD_CAST(obj) ((GstProxyPad *)obj)
56 #define GST_PROXY_PAD_PRIVATE(obj) (GST_PROXY_PAD_CAST (obj)->priv)
57 #define GST_PROXY_PAD_TARGET(pad) (GST_PAD_PEER (GST_PROXY_PAD_INTERNAL (pad)))
58 #define GST_PROXY_PAD_INTERNAL(pad) (GST_PROXY_PAD_PRIVATE (pad)->internal)
60 struct _GstProxyPadPrivate
65 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
67 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
70 * gst_proxy_pad_event_default:
71 * @pad: a #GstPad to push the event to.
72 * @parent: the parent of @pad or NULL
73 * @event: (transfer full): the #GstEvent to send to the pad.
75 * Invoke the default event of the proxy pad.
77 * Returns: TRUE if the event was handled.
80 gst_proxy_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
85 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
86 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
88 internal = GST_PROXY_PAD_INTERNAL (pad);
89 res = gst_pad_push_event (internal, event);
95 gst_proxy_pad_query_caps (GstPad * pad, GstQuery * query)
100 GstPadTemplate *templ;
102 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
104 templ = GST_PAD_PAD_TEMPLATE (pad);
105 target = gst_proxy_pad_get_target (pad);
107 /* if we have a real target, proxy the call */
108 res = gst_pad_query (target, query);
110 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
111 GST_DEBUG_PAD_NAME (target), query);
113 gst_object_unref (target);
115 /* filter against the template */
119 filt = GST_PAD_TEMPLATE_CAPS (templ);
121 gst_query_parse_caps_result (query, &result);
122 tmp = gst_caps_intersect_full (result, filt, GST_CAPS_INTERSECT_FIRST);
123 gst_query_set_caps_result (query, tmp);
124 GST_DEBUG_OBJECT (pad,
125 "filtered against template gives %" GST_PTR_FORMAT, tmp);
126 gst_caps_unref (tmp);
134 gst_query_parse_caps (query, &filter);
136 /* else, if we have a template, use its caps. */
138 result = GST_PAD_TEMPLATE_CAPS (templ);
139 GST_DEBUG_OBJECT (pad,
140 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
144 GstCaps *intersection;
146 GST_DEBUG_OBJECT (pad, "intersect with filter");
148 gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
149 gst_query_set_caps_result (query, intersection);
150 gst_caps_unref (intersection);
152 gst_query_set_caps_result (query, result);
157 /* If there's a filter, return that */
158 if (filter != NULL) {
159 GST_DEBUG_OBJECT (pad, "return filter");
160 gst_query_set_caps_result (query, filter);
164 /* last resort, any caps */
165 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
166 result = gst_caps_new_any ();
167 gst_query_set_caps_result (query, result);
168 gst_caps_unref (result);
176 * gst_proxy_pad_query_default:
177 * @pad: a #GstPad to invoke the default query on.
178 * @parent: the parent of @pad or NULL
179 * @query: (transfer none): the #GstQuery to perform.
181 * Invoke the default query function of the proxy pad.
183 * Returns: TRUE if the query could be performed.
186 gst_proxy_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
191 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
192 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
195 switch (GST_QUERY_TYPE (query)) {
196 case GST_QUERY_ACCEPT_CAPS:
198 target = gst_proxy_pad_get_target (pad);
200 res = gst_pad_query (target, query);
201 gst_object_unref (target);
203 GST_DEBUG_OBJECT (pad, "no target");
204 /* We don't have a target, we return TRUE and we assume that any future
205 * target will be able to deal with any configured caps. */
206 gst_query_set_accept_caps_result (query, TRUE);
213 res = gst_proxy_pad_query_caps (pad, query);
218 target = gst_proxy_pad_get_target (pad);
220 res = gst_pad_query (target, query);
221 gst_object_unref (target);
223 GST_DEBUG_OBJECT (pad, "no target pad");
233 * gst_proxy_pad_iterate_internal_links_default:
234 * @pad: the #GstPad to get the internal links of.
235 * @parent: the parent of @pad or NULL
237 * Invoke the default iterate internal links function of the proxy pad.
239 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
240 * returned pad with gst_object_unref().
243 gst_proxy_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
245 GstIterator *res = NULL;
249 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
251 internal = GST_PROXY_PAD_INTERNAL (pad);
252 g_value_init (&v, GST_TYPE_PAD);
253 g_value_set_object (&v, internal);
254 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
261 * gst_proxy_pad_chain_default:
262 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
263 * @parent: the parent of @pad or NULL
264 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
267 * Invoke the default chain function of the proxy pad.
269 * Returns: a #GstFlowReturn from the pad.
272 gst_proxy_pad_chain_default (GstPad * pad, GstObject * parent,
278 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
279 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
281 internal = GST_PROXY_PAD_INTERNAL (pad);
282 res = gst_pad_push (internal, buffer);
288 * gst_proxy_pad_chain_list_default:
289 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
290 * @parent: the parent of @pad or NULL
291 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
294 * Invoke the default chain list function of the proxy pad.
296 * Returns: a #GstFlowReturn from the pad.
299 gst_proxy_pad_chain_list_default (GstPad * pad, GstObject * parent,
300 GstBufferList * list)
305 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
306 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
308 internal = GST_PROXY_PAD_INTERNAL (pad);
309 res = gst_pad_push_list (internal, list);
315 * gst_proxy_pad_getrange_default:
316 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
317 * @parent: the parent of @pad
318 * @offset: The start offset of the buffer
319 * @size: The length of the buffer
320 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
321 * returns #GST_FLOW_ERROR if %NULL.
323 * Invoke the default getrange function of the proxy pad.
325 * Returns: a #GstFlowReturn from the pad.
328 gst_proxy_pad_getrange_default (GstPad * pad, GstObject * parent,
329 guint64 offset, guint size, GstBuffer ** buffer)
334 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
335 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
337 internal = GST_PROXY_PAD_INTERNAL (pad);
338 res = gst_pad_pull_range (internal, offset, size, buffer);
344 gst_proxy_pad_get_target (GstPad * pad)
348 GST_OBJECT_LOCK (pad);
349 target = GST_PROXY_PAD_TARGET (pad);
351 gst_object_ref (target);
352 GST_OBJECT_UNLOCK (pad);
358 * gst_proxy_pad_get_internal:
359 * @pad: the #GstProxyPad
361 * Get the internal pad of @pad. Unref target pad after usage.
363 * The internal pad of a #GstGhostPad is the internally used
364 * pad of opposite direction, which is used to link to the target.
366 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
367 * Unref target pad after usage.
370 gst_proxy_pad_get_internal (GstProxyPad * pad)
374 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
376 GST_OBJECT_LOCK (pad);
377 internal = GST_PROXY_PAD_INTERNAL (pad);
379 gst_object_ref (internal);
380 GST_OBJECT_UNLOCK (pad);
382 return GST_PROXY_PAD_CAST (internal);
386 * gst_proxy_pad_unlink_default:
387 * @pad: a #GstPad to unlink
389 * Invoke the default unlink function of the proxy pad.
392 gst_proxy_pad_unlink_default (GstPad * pad)
394 /* nothing to do anymore */
395 GST_DEBUG_OBJECT (pad, "pad is unlinked");
399 gst_proxy_pad_class_init (GstProxyPadClass * klass)
401 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
403 /* Register common function pointer descriptions */
404 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
405 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
406 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
407 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
408 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
409 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
410 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
414 gst_proxy_pad_init (GstProxyPad * ppad)
416 GstPad *pad = (GstPad *) ppad;
418 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
419 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
421 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
422 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
423 gst_pad_set_iterate_internal_links_function (pad,
424 gst_proxy_pad_iterate_internal_links_default);
426 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
430 /***********************************************************************
431 * Ghost pads, implemented as a pair of proxy pads (sort of)
435 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
437 struct _GstGhostPadPrivate
439 /* with PROXY_LOCK */
440 gboolean constructed;
443 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
445 static void gst_ghost_pad_dispose (GObject * object);
448 gst_ghost_pad_internal_activate_push_default (GstPad * pad, GstObject * parent,
454 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
455 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
457 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
458 * will be activated later (or already in case of a ghost sinkpad). */
459 other = GST_PROXY_PAD_INTERNAL (pad);
460 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
466 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
472 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
473 GST_DEBUG_PAD_NAME (pad));
475 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
476 /* we are activated in pull mode by our peer element, which is a sinkpad
477 * that wants to operate in pull mode. This activation has to propagate
478 * upstream through the pipeline. We call the internal activation function,
479 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
480 * further upstream */
481 GST_LOG_OBJECT (pad, "pad is src, activate internal");
482 other = GST_PROXY_PAD_INTERNAL (pad);
483 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
484 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
485 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
486 * since we hold a pointer to the upstream peer. */
487 GST_LOG_OBJECT (pad, "activating peer");
488 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
489 gst_object_unref (other);
491 /* this is failure, we can't activate pull if there is no peer */
492 GST_LOG_OBJECT (pad, "not src and no peer, failing");
500 * gst_ghost_pad_internal_activate_mode_default:
501 * @pad: the #GstPad to activate or deactivate.
502 * @parent: the parent of @pad or NULL
503 * @mode: the requested activation mode
504 * @active: whether the pad should be active or not.
506 * Invoke the default activate mode function of a proxy pad that is
507 * owned by a ghost pad.
509 * Returns: %TRUE if the operation was successful.
512 gst_ghost_pad_internal_activate_mode_default (GstPad * pad, GstObject * parent,
513 GstPadMode mode, gboolean active)
517 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
520 case GST_PAD_MODE_PULL:
521 res = gst_ghost_pad_internal_activate_pull_default (pad, parent, active);
523 case GST_PAD_MODE_PUSH:
524 res = gst_ghost_pad_internal_activate_push_default (pad, parent, active);
527 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
535 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
541 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
543 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
544 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
546 /* just activate the internal pad */
547 other = GST_PROXY_PAD_INTERNAL (pad);
548 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
554 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
560 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
561 GST_DEBUG_PAD_NAME (pad));
563 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
564 /* the ghostpad is SRC and activated in pull mode by its peer, call the
565 * activation function of the internal pad to propagate the activation
567 GST_LOG_OBJECT (pad, "pad is src, activate internal");
568 other = GST_PROXY_PAD_INTERNAL (pad);
569 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
570 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
571 /* We are SINK and activated by the internal pad, propagate activation
572 * upstream because we hold a ref to the upstream peer */
573 GST_LOG_OBJECT (pad, "activating peer");
574 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
575 gst_object_unref (other);
577 /* no peer, we fail */
578 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
586 * gst_ghost_pad_activate_mode_default:
587 * @pad: the #GstPad to activate or deactivate.
588 * @parent: the parent of @pad or NULL
589 * @mode: the requested activation mode
590 * @active: whether the pad should be active or not.
592 * Invoke the default activate mode function of a ghost pad.
594 * Returns: %TRUE if the operation was successful.
597 gst_ghost_pad_activate_mode_default (GstPad * pad, GstObject * parent,
598 GstPadMode mode, gboolean active)
602 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
605 case GST_PAD_MODE_PULL:
606 res = gst_ghost_pad_activate_pull_default (pad, parent, active);
608 case GST_PAD_MODE_PUSH:
609 res = gst_ghost_pad_activate_push_default (pad, parent, active);
612 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
620 * gst_ghost_pad_link_default:
621 * @pad: the #GstPad to link.
622 * @peer: the #GstPad peer
624 * Invoke the default link function of a ghost pad.
626 * Returns: #GstPadLinkReturn of the operation
629 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
631 GstPadLinkReturn ret;
633 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
634 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
636 GST_DEBUG_OBJECT (pad, "linking ghostpad");
638 ret = GST_PAD_LINK_OK;
639 /* if we are a source pad, we should call the peer link function
640 * if the peer has one, see design docs. */
641 if (GST_PAD_IS_SRC (pad)) {
642 if (GST_PAD_LINKFUNC (peer)) {
643 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
644 if (ret != GST_PAD_LINK_OK)
645 GST_DEBUG_OBJECT (pad, "linking failed");
652 * gst_ghost_pad_unlink_default:
653 * @pad: the #GstPad to link.
655 * Invoke the default unlink function of a ghost pad.
658 gst_ghost_pad_unlink_default (GstPad * pad)
660 g_return_if_fail (GST_IS_GHOST_PAD (pad));
662 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
666 gst_ghost_pad_class_init (GstGhostPadClass * klass)
668 GObjectClass *gobject_class = (GObjectClass *) klass;
670 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
672 gobject_class->dispose = gst_ghost_pad_dispose;
674 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
675 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
676 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
680 gst_ghost_pad_init (GstGhostPad * pad)
682 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
683 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
685 gst_pad_set_activatemode_function (GST_PAD_CAST (pad),
686 gst_ghost_pad_activate_mode_default);
690 gst_ghost_pad_dispose (GObject * object)
696 pad = GST_PAD (object);
698 GST_DEBUG_OBJECT (pad, "dispose");
700 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
702 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
703 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
704 peer = gst_pad_get_peer (pad);
706 if (GST_PAD_IS_SRC (pad))
707 gst_pad_unlink (pad, peer);
709 gst_pad_unlink (peer, pad);
711 gst_object_unref (peer);
714 GST_OBJECT_LOCK (pad);
715 internal = GST_PROXY_PAD_INTERNAL (pad);
717 gst_pad_set_activatemode_function (internal, NULL);
719 /* disposes of the internal pad, since the ghostpad is the only possible object
720 * that has a refcount on the internal pad. */
721 gst_object_unparent (GST_OBJECT_CAST (internal));
722 GST_PROXY_PAD_INTERNAL (pad) = NULL;
724 GST_OBJECT_UNLOCK (pad);
726 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
730 * gst_ghost_pad_construct:
731 * @gpad: the newly allocated ghost pad
733 * Finish initialization of a newly allocated ghost pad.
735 * This function is most useful in language bindings and when subclassing
736 * #GstGhostPad; plugin and application developers normally will not call this
737 * function. Call this function directly after a call to g_object_new
738 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
740 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
743 gst_ghost_pad_construct (GstGhostPad * gpad)
745 GstPadDirection dir, otherdir;
746 GstPadTemplate *templ;
747 GstPad *pad, *internal;
749 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
750 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
753 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
755 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
757 pad = GST_PAD (gpad);
759 /* Set directional padfunctions for ghostpad */
760 if (dir == GST_PAD_SINK) {
761 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
762 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
764 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
767 /* link/unlink functions */
768 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
769 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
771 /* INTERNAL PAD, it always exists and is child of the ghostpad */
772 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
775 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
776 "direction", otherdir, "template", templ, NULL);
777 /* release ref obtained via g_object_get */
778 gst_object_unref (templ);
781 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
782 "direction", otherdir, NULL);
784 GST_PAD_UNSET_FLUSHING (internal);
786 /* Set directional padfunctions for internal pad */
787 if (dir == GST_PAD_SRC) {
788 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
789 gst_pad_set_chain_list_function (internal,
790 gst_proxy_pad_chain_list_default);
792 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
795 GST_OBJECT_LOCK (pad);
797 /* now make the ghostpad a parent of the internal pad */
798 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
799 GST_OBJECT_CAST (pad)))
802 /* The ghostpad is the parent of the internal pad and is the only object that
803 * can have a refcount on the internal pad.
804 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
806 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
807 * its refcount on the internal pad in the dispose method by un-parenting it.
808 * This is why we don't take extra refcounts in the assignments below
810 GST_PROXY_PAD_INTERNAL (pad) = internal;
811 GST_PROXY_PAD_INTERNAL (internal) = pad;
813 /* special activation functions for the internal pad */
814 gst_pad_set_activatemode_function (internal,
815 gst_ghost_pad_internal_activate_mode_default);
817 GST_OBJECT_UNLOCK (pad);
819 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
825 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
826 GST_DEBUG_PAD_NAME (internal));
827 g_critical ("Could not set internal pad %s:%s",
828 GST_DEBUG_PAD_NAME (internal));
829 GST_OBJECT_UNLOCK (pad);
830 gst_object_unref (internal);
836 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
837 GstPadTemplate * templ)
841 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
843 /* OBJECT CREATION */
845 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
846 "direction", dir, "template", templ, NULL);
848 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
849 "direction", dir, NULL);
852 if (!gst_ghost_pad_construct (ret))
853 goto construct_failed;
855 return GST_PAD_CAST (ret);
859 gst_object_unref (ret);
864 * gst_ghost_pad_new_no_target:
865 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
866 * @dir: the direction of the ghostpad
868 * Create a new ghostpad without a target with the given direction.
869 * A target can be set on the ghostpad later with the
870 * gst_ghost_pad_set_target() function.
872 * The created ghostpad will not have a padtemplate.
874 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
877 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
881 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
883 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
885 ret = gst_ghost_pad_new_full (name, dir, NULL);
892 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
893 * @target: (transfer none): the pad to ghost.
895 * Create a new ghostpad with @target as the target. The direction will be taken
896 * from the target pad. @target must be unlinked.
898 * Will ref the target.
900 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
903 gst_ghost_pad_new (const gchar * name, GstPad * target)
907 g_return_val_if_fail (GST_IS_PAD (target), NULL);
908 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
910 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
911 GST_DEBUG_PAD_NAME (target));
913 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
914 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
915 goto set_target_failed;
922 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
923 GST_DEBUG_PAD_NAME (target));
924 gst_object_unref (ret);
930 * gst_ghost_pad_new_from_template:
931 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
932 * @target: (transfer none): the pad to ghost.
933 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
935 * Create a new ghostpad with @target as the target. The direction will be taken
936 * from the target pad. The template used on the ghostpad will be @template.
938 * Will ref the target.
940 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
944 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
945 GstPadTemplate * templ)
949 g_return_val_if_fail (GST_IS_PAD (target), NULL);
950 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
951 g_return_val_if_fail (templ != NULL, NULL);
952 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
953 GST_PAD_DIRECTION (target), NULL);
955 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
956 GST_DEBUG_PAD_NAME (target), templ);
958 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
959 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
960 goto set_target_failed;
967 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
968 GST_DEBUG_PAD_NAME (target));
969 gst_object_unref (ret);
975 * gst_ghost_pad_new_no_target_from_template:
976 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
977 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
979 * Create a new ghostpad based on @templ, without setting a target. The
980 * direction will be taken from the @templ.
982 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
985 gst_ghost_pad_new_no_target_from_template (const gchar * name,
986 GstPadTemplate * templ)
990 g_return_val_if_fail (templ != NULL, NULL);
993 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
999 * gst_ghost_pad_get_target:
1000 * @gpad: the #GstGhostPad
1002 * Get the target pad of @gpad. Unref target pad after usage.
1004 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1005 * has no target set. Unref target pad after usage.
1008 gst_ghost_pad_get_target (GstGhostPad * gpad)
1012 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1014 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1016 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1022 * gst_ghost_pad_set_target:
1023 * @gpad: the #GstGhostPad
1024 * @newtarget: (transfer none) (allow-none): the new pad target
1026 * Set the new target of the ghostpad @gpad. Any existing target
1027 * is unlinked and links to the new target are established. if @newtarget is
1028 * NULL the target will be cleared.
1030 * Returns: (transfer full): TRUE if the new target could be set. This function
1031 * can return FALSE when the internal pads could not be linked.
1034 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1038 GstPadLinkReturn lret;
1040 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1041 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1042 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1044 /* no need for locking, the internal pad's lifecycle is directly linked to the
1046 internal = GST_PROXY_PAD_INTERNAL (gpad);
1049 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1051 GST_DEBUG_OBJECT (gpad, "clearing target");
1053 /* clear old target */
1054 GST_OBJECT_LOCK (gpad);
1055 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1056 GST_OBJECT_UNLOCK (gpad);
1058 /* unlink internal pad */
1059 if (GST_PAD_IS_SRC (internal))
1060 gst_pad_unlink (internal, oldtarget);
1062 gst_pad_unlink (oldtarget, internal);
1064 GST_OBJECT_UNLOCK (gpad);
1068 /* and link to internal pad without any checks */
1069 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1071 if (GST_PAD_IS_SRC (internal))
1073 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1076 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1078 if (lret != GST_PAD_LINK_OK)
1087 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",