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);
69 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
70 #ifdef GST_DISABLE_DEPRECATED
71 #include <libxml/parser.h>
73 static xmlNodePtr gst_proxy_pad_save_thyself (GstObject * object,
77 static void on_src_target_notify (GstPad * target,
78 GParamSpec * unused, gpointer user_data);
80 static GParamSpec *pspec_caps = NULL;
83 * gst_proxy_pad_query_type_default:
86 * Invoke the default query type handler of the proxy pad.
88 * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
94 gst_proxy_pad_query_type_default (GstPad * pad)
97 const GstQueryType *res = NULL;
99 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
101 target = gst_proxy_pad_get_target (pad);
103 res = gst_pad_get_query_types (target);
104 gst_object_unref (target);
110 * gst_proxy_pad_event_default:
111 * @pad: a #GstPad to push the event to.
112 * @event: (transfer full): the #GstEvent to send to the pad.
114 * Invoke the default event of the proxy pad.
116 * Returns: TRUE if the event was handled.
121 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
123 gboolean res = FALSE;
126 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
127 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
130 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad)));
132 res = gst_pad_push_event (internal, event);
133 gst_object_unref (internal);
140 * gst_proxy_pad_query_default:
141 * @pad: a #GstPad to invoke the default query on.
142 * @query: (transfer none): the #GstQuery to perform.
144 * Invoke the default query function of the proxy pad.
146 * Returns: TRUE if the query could be performed.
151 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
153 gboolean res = FALSE;
156 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
157 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
159 target = gst_proxy_pad_get_target (pad);
161 res = gst_pad_query (target, query);
162 gst_object_unref (target);
169 * gst_proyx_pad_iterate_internal_links_default:
170 * @pad: the #GstPad to get the internal links of.
172 * Invoke the default iterate internal links function of the proxy pad.
174 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
175 * returned pad with gst_object_unref().
180 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
182 GstIterator *res = NULL;
185 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
188 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad)));
192 gst_iterator_new_single (GST_TYPE_PAD, internal,
193 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
194 gst_object_unref (internal);
201 * gst_proxy_pad_bufferalloc_default:
202 * @pad: a source #GstPad
203 * @offset: the offset of the new buffer in the stream
204 * @size: the size of the new buffer
205 * @caps: the caps of the new buffer
206 * @buf: a newly allocated buffer
208 * Invoke the default bufferalloc function of the proxy pad.
210 * Returns: a result code indicating success of the operation. Any
211 * result code other than #GST_FLOW_OK is an error and @buf should
213 * An error can occur if the pad is not connected or when the downstream
214 * peer elements cannot provide an acceptable buffer.
219 gst_proxy_pad_bufferalloc_default (GstPad * pad, guint64 offset, guint size,
220 GstCaps * caps, GstBuffer ** buf)
222 GstFlowReturn result = GST_FLOW_WRONG_STATE;
225 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
226 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), GST_FLOW_ERROR);
227 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
230 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad)));
232 result = gst_pad_alloc_buffer (internal, offset, size, caps, buf);
233 gst_object_unref (internal);
240 * gst_proxy_pad_chain_default:
241 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
242 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
245 * Invoke the default chain function of the proxy pad.
247 * Returns: a #GstFlowReturn from the pad.
252 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
257 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
258 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
260 internal = GST_PROXY_PAD_INTERNAL (pad);
261 res = gst_pad_push (internal, buffer);
267 * gst_proxy_pad_chain_list_default:
268 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
269 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
272 * Invoke the default chain list function of the proxy pad.
274 * Returns: a #GstFlowReturn from the pad.
279 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
284 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
285 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
287 internal = GST_PROXY_PAD_INTERNAL (pad);
288 res = gst_pad_push_list (internal, list);
294 * gst_proxy_pad_get_range_default:
295 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
296 * @offset: The start offset of the buffer
297 * @size: The length of the buffer
298 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
299 * returns #GST_FLOW_ERROR if %NULL.
301 * Invoke the default getrange function of the proxy pad.
303 * Returns: a #GstFlowReturn from the pad.
308 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
314 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
315 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
317 internal = GST_PROXY_PAD_INTERNAL (pad);
318 res = gst_pad_pull_range (internal, offset, size, buffer);
324 * gst_proxy_pad_checkgetrange_default:
325 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
327 * Invoke the default checkgetrange function of the proxy pad.
329 * Returns: a #gboolean from the pad.
334 gst_proxy_pad_checkgetrange_default (GstPad * pad)
339 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
341 internal = GST_PROXY_PAD_INTERNAL (pad);
342 result = gst_pad_check_pull_range (internal);
348 * gst_proxy_pad_getcaps_default:
349 * @pad: a #GstPad to get the capabilities of.
351 * Invoke the default getcaps function of the proxy pad.
353 * Returns: (transfer full): the caps of the pad with incremented ref-count
358 gst_proxy_pad_getcaps_default (GstPad * pad)
362 GstPadTemplate *templ;
364 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
366 templ = GST_PAD_PAD_TEMPLATE (pad);
367 target = gst_proxy_pad_get_target (pad);
369 /* if we have a real target, proxy the call */
370 res = gst_pad_get_caps_reffed (target);
372 GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
373 GST_DEBUG_PAD_NAME (target), res);
375 gst_object_unref (target);
377 /* filter against the template */
381 filt = GST_PAD_TEMPLATE_CAPS (templ);
383 tmp = gst_caps_intersect (filt, res);
384 gst_caps_unref (res);
386 GST_DEBUG_OBJECT (pad,
387 "filtered against template gives %" GST_PTR_FORMAT, res);
391 /* else, if we have a template, use its caps. */
393 res = GST_PAD_TEMPLATE_CAPS (templ);
394 GST_DEBUG_OBJECT (pad,
395 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
397 res = gst_caps_ref (res);
401 /* last resort, any caps */
402 GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
403 res = gst_caps_new_any ();
411 * gst_proxy_pad_acceptcaps_default:
412 * @pad: a #GstPad to check
413 * @caps: a #GstCaps to check on the pad
415 * Invoke the default acceptcaps function of the proxy pad.
417 * Returns: TRUE if the pad can accept the caps.
422 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
427 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
428 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
430 target = gst_proxy_pad_get_target (pad);
432 res = gst_pad_accept_caps (target, caps);
433 gst_object_unref (target);
435 /* We don't have a target, we return TRUE and we assume that any future
436 * target will be able to deal with any configured caps. */
444 * gst_proxy_pad_fixatecaps_default:
445 * @pad: a #GstPad to fixate
446 * @caps: the #GstCaps to fixate
448 * Invoke the default fixatecaps function of the proxy pad.
453 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
457 g_return_if_fail (GST_IS_PROXY_PAD (pad));
458 g_return_if_fail (GST_IS_CAPS (caps));
460 target = gst_proxy_pad_get_target (pad);
462 gst_pad_fixate_caps (target, caps);
463 gst_object_unref (target);
468 * gst_proxy_pad_setcaps_default:
469 * @pad: a #GstPad to set the capabilities of.
470 * @caps: (transfer none): a #GstCaps to set.
472 * Invoke the default setcaps function of the proxy pad.
474 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
475 * or bad parameters were provided to this function.
480 gst_proxy_pad_setcaps_default (GstPad * pad, GstCaps * caps)
485 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
486 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
488 target = gst_proxy_pad_get_target (pad);
490 res = gst_pad_set_caps (target, caps);
491 gst_object_unref (target);
493 /* We don't have any target, but we shouldn't return FALSE since this
494 * would stop the actual push of a buffer (which might trigger a pad block
495 * or probe, or properly return GST_FLOW_NOT_LINKED.
503 gst_proxy_pad_get_target (GstPad * pad)
507 GST_OBJECT_LOCK (pad);
508 target = GST_PROXY_PAD_TARGET (pad);
510 gst_object_ref (target);
511 GST_OBJECT_UNLOCK (pad);
517 * gst_proxy_pad_get_internal:
518 * @pad: the #GstProxyPad
520 * Get the internal pad of @pad. Unref target pad after usage.
522 * The internal pad of a #GstGhostPad is the internally used
523 * pad of opposite direction, which is used to link to the target.
525 * Returns: (transfer full): the target #GstProxyPad, can be NULL.
526 * Unref target pad after usage.
531 gst_proxy_pad_get_internal (GstProxyPad * pad)
535 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
537 GST_OBJECT_LOCK (pad);
538 internal = GST_PROXY_PAD_INTERNAL (pad);
540 gst_object_ref (internal);
541 GST_OBJECT_UNLOCK (pad);
543 return GST_PROXY_PAD_CAST (internal);
547 * gst_proxy_pad_unlink_default:
548 * @pad: a #GstPad to unlink
550 * Invoke the default unlink function of the proxy pad.
555 gst_proxy_pad_unlink_default (GstPad * pad)
557 /* nothing to do anymore */
558 GST_DEBUG_OBJECT (pad, "pad is unlinked");
562 gst_proxy_pad_class_init (GstProxyPadClass * klass)
564 g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
566 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
568 GstObjectClass *gstobject_class = (GstObjectClass *) klass;
570 gstobject_class->save_thyself =
571 ((gpointer (*)(GstObject * object,
573 GST_DEBUG_FUNCPTR (gst_proxy_pad_save_thyself));
576 /* Register common function pointer descriptions */
577 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
578 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
579 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
580 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
581 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
582 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
583 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
584 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_setcaps_default);
585 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
586 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_bufferalloc_default);
587 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
588 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
589 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
590 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_checkgetrange_default);
594 gst_proxy_pad_init (GstProxyPad * ppad)
596 GstPad *pad = (GstPad *) ppad;
598 GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
599 GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
601 gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
602 gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
603 gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
604 gst_pad_set_iterate_internal_links_function (pad,
605 gst_proxy_pad_iterate_internal_links_default);
607 gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
608 gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
609 gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
610 gst_pad_set_setcaps_function (pad, gst_proxy_pad_setcaps_default);
611 gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
614 #if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
616 * gst_proxy_pad_save_thyself:
617 * @pad: a ghost #GstPad to save.
618 * @parent: the parent #xmlNodePtr to save the description in.
620 * Saves the ghost pad into an xml representation.
622 * Returns: the #xmlNodePtr representation of the pad.
625 gst_proxy_pad_save_thyself (GstObject * object, xmlNodePtr parent)
628 GstProxyPad *proxypad;
632 g_return_val_if_fail (GST_IS_PROXY_PAD (object), NULL);
634 self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
635 xmlNewChild (self, NULL, (xmlChar *) "name",
636 (xmlChar *) GST_OBJECT_NAME (object));
637 xmlNewChild (self, NULL, (xmlChar *) "parent",
638 (xmlChar *) GST_OBJECT_NAME (GST_OBJECT_PARENT (object)));
640 proxypad = GST_PROXY_PAD_CAST (object);
641 pad = GST_PAD_CAST (proxypad);
642 peer = GST_PAD_CAST (pad->peer);
644 if (GST_IS_PAD (pad)) {
645 if (GST_PAD_IS_SRC (pad))
646 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "source");
647 else if (GST_PAD_IS_SINK (pad))
648 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
650 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
652 xmlNewChild (self, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
654 if (GST_IS_PAD (peer)) {
655 gchar *content = g_strdup_printf ("%s.%s",
656 GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
658 xmlNewChild (self, NULL, (xmlChar *) "peer", (xmlChar *) content);
661 xmlNewChild (self, NULL, (xmlChar *) "peer", NULL);
666 #endif /* GST_DISABLE_LOADSAVE */
669 /***********************************************************************
670 * Ghost pads, implemented as a pair of proxy pads (sort of)
674 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
676 struct _GstGhostPadPrivate
678 /* with PROXY_LOCK */
681 gboolean constructed;
684 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
686 static void gst_ghost_pad_dispose (GObject * object);
689 * gst_ghost_pad_internal_activate_push_default:
690 * @pad: the #GstPad to activate or deactivate.
691 * @active: whether the pad should be active or not.
693 * Invoke the default activate push function of a proxy pad that is
694 * owned by a ghost pad.
696 * Returns: %TRUE if the operation was successful.
701 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
706 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
708 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
709 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
711 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
712 * will be activated later (or already in case of a ghost sinkpad). */
713 other = GST_PROXY_PAD_INTERNAL (pad);
714 ret = gst_pad_activate_push (other, active);
720 * gst_ghost_pad_internal_activate_pull_default:
721 * @pad: the #GstPad to activate or deactivate.
722 * @active: whether the pad should be active or not.
724 * Invoke the default activate pull function of a proxy pad that is
725 * owned by a ghost pad.
727 * Returns: %TRUE if the operation was successful.
732 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
737 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
739 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
740 GST_DEBUG_PAD_NAME (pad));
742 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
743 /* we are activated in pull mode by our peer element, which is a sinkpad
744 * that wants to operate in pull mode. This activation has to propagate
745 * upstream through the pipeline. We call the internal activation function,
746 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
747 * further upstream */
748 GST_LOG_OBJECT (pad, "pad is src, activate internal");
749 other = GST_PROXY_PAD_INTERNAL (pad);
750 ret = gst_pad_activate_pull (other, active);
751 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
752 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
753 * since we hold a pointer to the upstream peer. */
754 GST_LOG_OBJECT (pad, "activating peer");
755 ret = gst_pad_activate_pull (other, active);
756 gst_object_unref (other);
758 /* this is failure, we can't activate pull if there is no peer */
759 GST_LOG_OBJECT (pad, "not src and no peer, failing");
767 * gst_ghost_pad_activate_push_default:
768 * @pad: the #GstPad to activate or deactivate.
769 * @active: whether the pad should be active or not.
771 * Invoke the default activate push function of a ghost pad.
773 * Returns: %TRUE if the operation was successful.
778 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
783 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
785 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
786 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
788 /* just activate the internal pad */
789 other = GST_PROXY_PAD_INTERNAL (pad);
790 ret = gst_pad_activate_push (other, active);
796 * gst_ghost_pad_activate_pull_default:
797 * @pad: the #GstPad to activate or deactivate.
798 * @active: whether the pad should be active or not.
800 * Invoke the default activate pull function of a ghost pad.
802 * Returns: %TRUE if the operation was successful.
807 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
812 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
814 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
815 GST_DEBUG_PAD_NAME (pad));
817 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
818 /* the ghostpad is SRC and activated in pull mode by its peer, call the
819 * activation function of the internal pad to propagate the activation
821 GST_LOG_OBJECT (pad, "pad is src, activate internal");
822 other = GST_PROXY_PAD_INTERNAL (pad);
823 ret = gst_pad_activate_pull (other, active);
824 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
825 /* We are SINK and activated by the internal pad, propagate activation
826 * upstream because we hold a ref to the upstream peer */
827 GST_LOG_OBJECT (pad, "activating peer");
828 ret = gst_pad_activate_pull (other, active);
829 gst_object_unref (other);
831 /* no peer, we fail */
832 GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
840 * gst_ghost_pad_link_default:
841 * @pad: the #GstPad to link.
842 * @peer: the #GstPad peer
844 * Invoke the default link function of a ghost pad.
846 * Returns: #GstPadLinkReturn of the operation
851 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
853 GstPadLinkReturn ret;
855 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
856 g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
858 GST_DEBUG_OBJECT (pad, "linking ghostpad");
860 ret = GST_PAD_LINK_OK;
861 /* if we are a source pad, we should call the peer link function
862 * if the peer has one, see design docs. */
863 if (GST_PAD_IS_SRC (pad)) {
864 if (GST_PAD_LINKFUNC (peer)) {
865 ret = GST_PAD_LINKFUNC (peer) (peer, pad);
866 if (ret != GST_PAD_LINK_OK)
867 GST_DEBUG_OBJECT (pad, "linking failed");
874 * gst_ghost_pad_unlink_default:
875 * @pad: the #GstPad to link.
877 * Invoke the default unlink function of a ghost pad.
882 gst_ghost_pad_unlink_default (GstPad * pad)
884 g_return_if_fail (GST_IS_GHOST_PAD (pad));
886 GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
890 on_int_notify (GstPad * internal, GParamSpec * unused, GstGhostPad * pad)
894 g_object_get (internal, "caps", &caps, NULL);
896 GST_DEBUG_OBJECT (pad, "notified %p %" GST_PTR_FORMAT, caps, caps);
897 gst_pad_set_caps (GST_PAD_CAST (pad), caps);
900 gst_caps_unref (caps);
904 on_src_target_notify (GstPad * target, GParamSpec * unused, gpointer user_data)
906 GstProxyPad *proxypad;
910 g_object_get (target, "caps", &caps, NULL);
912 GST_OBJECT_LOCK (target);
913 /* First check if the peer is still available and our proxy pad */
914 if (!GST_PAD_PEER (target) || !GST_IS_PROXY_PAD (GST_PAD_PEER (target))) {
915 GST_OBJECT_UNLOCK (target);
919 proxypad = GST_PROXY_PAD (GST_PAD_PEER (target));
920 GST_OBJECT_LOCK (proxypad);
921 /* Now check if the proxypad's internal pad is still there and
923 if (!GST_PROXY_PAD_INTERNAL (proxypad) ||
924 !GST_IS_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad))) {
925 GST_OBJECT_UNLOCK (proxypad);
926 GST_OBJECT_UNLOCK (target);
929 gpad = GST_GHOST_PAD (GST_PROXY_PAD_INTERNAL (proxypad));
931 GST_OBJECT_UNLOCK (proxypad);
932 GST_OBJECT_UNLOCK (target);
934 gst_pad_set_caps (GST_PAD_CAST (gpad), caps);
936 g_object_unref (gpad);
940 gst_caps_unref (caps);
944 on_src_target_unlinked (GstPad * pad, GstPad * peer, gpointer user_data)
946 g_signal_handlers_disconnect_by_func (pad,
947 (gpointer) on_src_target_notify, NULL);
951 * gst_ghost_pad_setcaps_default:
952 * @pad: the #GstPad to link.
953 * @caps: (transfer none): the #GstCaps to set
955 * Invoke the default setcaps function of a ghost pad.
957 * Returns: %TRUE if the operation was successful
962 gst_ghost_pad_setcaps_default (GstPad * pad, GstCaps * caps)
964 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
965 g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
967 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
970 return gst_proxy_pad_setcaps_default (pad, caps);
974 gst_ghost_pad_class_init (GstGhostPadClass * klass)
976 GObjectClass *gobject_class = (GObjectClass *) klass;
978 g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
980 pspec_caps = g_object_class_find_property (gobject_class, "caps");
982 gobject_class->dispose = gst_ghost_pad_dispose;
984 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_setcaps_default);
985 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
986 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
987 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
991 gst_ghost_pad_init (GstGhostPad * pad)
993 GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
994 GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
996 gst_pad_set_setcaps_function (GST_PAD_CAST (pad),
997 gst_ghost_pad_setcaps_default);
998 gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
999 gst_ghost_pad_activate_pull_default);
1000 gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
1001 gst_ghost_pad_activate_push_default);
1005 gst_ghost_pad_dispose (GObject * object)
1011 pad = GST_PAD (object);
1013 GST_DEBUG_OBJECT (pad, "dispose");
1015 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
1017 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
1018 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
1019 peer = gst_pad_get_peer (pad);
1021 if (GST_PAD_IS_SRC (pad))
1022 gst_pad_unlink (pad, peer);
1024 gst_pad_unlink (peer, pad);
1026 gst_object_unref (peer);
1029 GST_OBJECT_LOCK (pad);
1030 internal = GST_PROXY_PAD_INTERNAL (pad);
1032 gst_pad_set_activatepull_function (internal, NULL);
1033 gst_pad_set_activatepush_function (internal, NULL);
1035 g_signal_handler_disconnect (internal,
1036 GST_GHOST_PAD_PRIVATE (pad)->notify_id);
1038 /* disposes of the internal pad, since the ghostpad is the only possible object
1039 * that has a refcount on the internal pad. */
1040 gst_object_unparent (GST_OBJECT_CAST (internal));
1041 GST_PROXY_PAD_INTERNAL (pad) = NULL;
1043 GST_OBJECT_UNLOCK (pad);
1045 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
1049 * gst_ghost_pad_construct:
1050 * @gpad: the newly allocated ghost pad
1052 * Finish initialization of a newly allocated ghost pad.
1054 * This function is most useful in language bindings and when subclassing
1055 * #GstGhostPad; plugin and application developers normally will not call this
1056 * function. Call this function directly after a call to g_object_new
1057 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
1059 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
1064 gst_ghost_pad_construct (GstGhostPad * gpad)
1066 GstPadDirection dir, otherdir;
1067 GstPadTemplate *templ;
1068 GstPad *pad, *internal;
1070 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1071 g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
1074 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
1076 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
1078 pad = GST_PAD (gpad);
1080 /* Set directional padfunctions for ghostpad */
1081 if (dir == GST_PAD_SINK) {
1082 gst_pad_set_bufferalloc_function (pad, gst_proxy_pad_bufferalloc_default);
1083 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
1084 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
1086 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
1087 gst_pad_set_checkgetrange_function (pad,
1088 gst_proxy_pad_checkgetrange_default);
1091 /* link/unlink functions */
1092 gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
1093 gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
1095 /* INTERNAL PAD, it always exists and is child of the ghostpad */
1096 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
1099 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
1100 "direction", otherdir, "template", templ, NULL);
1101 /* release ref obtained via g_object_get */
1102 gst_object_unref (templ);
1105 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
1106 "direction", otherdir, NULL);
1108 GST_PAD_UNSET_FLUSHING (internal);
1110 /* Set directional padfunctions for internal pad */
1111 if (dir == GST_PAD_SRC) {
1112 gst_pad_set_bufferalloc_function (internal,
1113 gst_proxy_pad_bufferalloc_default);
1114 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
1115 gst_pad_set_chain_list_function (internal,
1116 gst_proxy_pad_chain_list_default);
1118 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
1119 gst_pad_set_checkgetrange_function (internal,
1120 gst_proxy_pad_checkgetrange_default);
1123 GST_OBJECT_LOCK (pad);
1125 /* now make the ghostpad a parent of the internal pad */
1126 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
1127 GST_OBJECT_CAST (pad)))
1130 /* The ghostpad is the parent of the internal pad and is the only object that
1131 * can have a refcount on the internal pad.
1132 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
1134 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
1135 * its refcount on the internal pad in the dispose method by un-parenting it.
1136 * This is why we don't take extra refcounts in the assignments below
1138 GST_PROXY_PAD_INTERNAL (pad) = internal;
1139 GST_PROXY_PAD_INTERNAL (internal) = pad;
1141 /* could be more general here, iterating over all writable properties...
1142 * taking the short road for now tho */
1143 GST_GHOST_PAD_PRIVATE (pad)->notify_id =
1144 g_signal_connect (internal, "notify::caps", G_CALLBACK (on_int_notify),
1147 /* special activation functions for the internal pad */
1148 gst_pad_set_activatepull_function (internal,
1149 gst_ghost_pad_internal_activate_pull_default);
1150 gst_pad_set_activatepush_function (internal,
1151 gst_ghost_pad_internal_activate_push_default);
1153 GST_OBJECT_UNLOCK (pad);
1155 /* call function to init values of the pad caps */
1156 on_int_notify (internal, NULL, GST_GHOST_PAD_CAST (pad));
1158 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
1164 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
1165 GST_DEBUG_PAD_NAME (internal));
1166 g_critical ("Could not set internal pad %s:%s",
1167 GST_DEBUG_PAD_NAME (internal));
1168 GST_OBJECT_UNLOCK (pad);
1169 gst_object_unref (internal);
1175 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
1176 GstPadTemplate * templ)
1180 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1182 /* OBJECT CREATION */
1184 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1185 "direction", dir, "template", templ, NULL);
1187 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
1188 "direction", dir, NULL);
1191 if (!gst_ghost_pad_construct (ret))
1192 goto construct_failed;
1194 return GST_PAD_CAST (ret);
1197 /* already logged */
1198 gst_object_unref (ret);
1203 * gst_ghost_pad_new_no_target:
1204 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1205 * @dir: the direction of the ghostpad
1207 * Create a new ghostpad without a target with the given direction.
1208 * A target can be set on the ghostpad later with the
1209 * gst_ghost_pad_set_target() function.
1211 * The created ghostpad will not have a padtemplate.
1213 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1216 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
1220 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
1222 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
1224 ret = gst_ghost_pad_new_full (name, dir, NULL);
1230 * gst_ghost_pad_new:
1231 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1232 * @target: (transfer none): the pad to ghost.
1234 * Create a new ghostpad with @target as the target. The direction will be taken
1235 * from the target pad. @target must be unlinked.
1237 * Will ref the target.
1239 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1242 gst_ghost_pad_new (const gchar * name, GstPad * target)
1246 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1247 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1249 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
1250 GST_DEBUG_PAD_NAME (target));
1252 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
1253 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1254 goto set_target_failed;
1261 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1262 GST_DEBUG_PAD_NAME (target));
1263 gst_object_unref (ret);
1269 * gst_ghost_pad_new_from_template:
1270 * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1271 * @target: (transfer none): the pad to ghost.
1272 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1274 * Create a new ghostpad with @target as the target. The direction will be taken
1275 * from the target pad. The template used on the ghostpad will be @template.
1277 * Will ref the target.
1279 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1285 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1286 GstPadTemplate * templ)
1290 g_return_val_if_fail (GST_IS_PAD (target), NULL);
1291 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1292 g_return_val_if_fail (templ != NULL, NULL);
1293 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1294 GST_PAD_DIRECTION (target), NULL);
1296 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1297 GST_DEBUG_PAD_NAME (target), templ);
1299 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1300 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1301 goto set_target_failed;
1308 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1309 GST_DEBUG_PAD_NAME (target));
1310 gst_object_unref (ret);
1316 * gst_ghost_pad_new_no_target_from_template:
1317 * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1318 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1320 * Create a new ghostpad based on @templ, without setting a target. The
1321 * direction will be taken from the @templ.
1323 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1328 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1329 GstPadTemplate * templ)
1333 g_return_val_if_fail (templ != NULL, NULL);
1336 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1342 * gst_ghost_pad_get_target:
1343 * @gpad: the #GstGhostPad
1345 * Get the target pad of @gpad. Unref target pad after usage.
1347 * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1348 * has no target set. Unref target pad after usage.
1351 gst_ghost_pad_get_target (GstGhostPad * gpad)
1355 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1357 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1359 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1365 * gst_ghost_pad_set_target:
1366 * @gpad: the #GstGhostPad
1367 * @newtarget: (transfer none) (allow-none): the new pad target
1369 * Set the new target of the ghostpad @gpad. Any existing target
1370 * is unlinked and links to the new target are established. if @newtarget is
1371 * NULL the target will be cleared.
1373 * Returns: (transfer full): TRUE if the new target could be set. This function
1374 * can return FALSE when the internal pads could not be linked.
1377 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1381 GstPadLinkReturn lret;
1383 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1384 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1385 g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1387 /* no need for locking, the internal pad's lifecycle is directly linked to the
1389 internal = GST_PROXY_PAD_INTERNAL (gpad);
1392 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1394 GST_DEBUG_OBJECT (gpad, "clearing target");
1396 /* clear old target */
1397 GST_OBJECT_LOCK (gpad);
1398 if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1399 GST_OBJECT_UNLOCK (gpad);
1401 /* unlink internal pad */
1402 if (GST_PAD_IS_SRC (internal))
1403 gst_pad_unlink (internal, oldtarget);
1405 gst_pad_unlink (oldtarget, internal);
1407 GST_OBJECT_UNLOCK (gpad);
1411 if (GST_PAD_IS_SRC (newtarget)) {
1412 g_signal_connect (newtarget, "notify::caps",
1413 G_CALLBACK (on_src_target_notify), NULL);
1414 g_signal_connect (newtarget, "unlinked",
1415 G_CALLBACK (on_src_target_unlinked), NULL);
1418 /* and link to internal pad without any checks */
1419 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1421 if (GST_PAD_IS_SRC (internal))
1423 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1426 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1428 if (lret != GST_PAD_LINK_OK)
1437 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",