proxypad: Hold a reference to the internal pad while pushing through it
[platform/upstream/gstreamer.git] / gst / gstghostpad.c
1 /* GStreamer
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>
6  *
7  * gstghostpad.c: Proxy pads
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:gstghostpad
27  * @short_description: Pseudo link pads
28  * @see_also: #GstPad
29  *
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.
36  *
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.
41  *
42  * Note that GhostPads add overhead to the data processing of a pipeline.
43  *
44  * Last reviewed on 2005-11-18 (0.9.5)
45  */
46
47 #include "gst_private.h"
48 #include "gstinfo.h"
49
50 #include "gstghostpad.h"
51 #include "gst.h"
52
53 #define GST_CAT_DEFAULT GST_CAT_PADS
54
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)
59
60 #define GST_PROXY_PAD_ACQUIRE_INTERNAL(pad, internal, retval)           \
61   internal =                                                            \
62       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad))); \
63   if (internal == NULL)                                                 \
64     return retval;
65
66 #define GST_PROXY_PAD_RELEASE_INTERNAL(internal) gst_object_unref (internal);
67
68 struct _GstProxyPadPrivate
69 {
70   GstPad *internal;
71 };
72
73 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
74
75 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
76
77 /**
78  * gst_proxy_pad_iterate_internal_links_default:
79  * @pad: the #GstPad to get the internal links of.
80  * @parent: the parent of @pad or NULL
81  *
82  * Invoke the default iterate internal links function of the proxy pad.
83  *
84  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
85  * returned pad with gst_object_unref().
86  */
87 GstIterator *
88 gst_proxy_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
89 {
90   GstIterator *res = NULL;
91   GstPad *internal;
92   GValue v = { 0, };
93
94   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
95
96   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, NULL);
97
98   g_value_init (&v, GST_TYPE_PAD);
99   g_value_take_object (&v, internal);
100   res = gst_iterator_new_single (GST_TYPE_PAD, &v);
101   g_value_unset (&v);
102
103   return res;
104 }
105
106 /**
107  * gst_proxy_pad_chain_default:
108  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
109  * @parent: the parent of @pad or NULL
110  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
111  *     if not.
112  *
113  * Invoke the default chain function of the proxy pad.
114  *
115  * Returns: a #GstFlowReturn from the pad.
116  */
117 GstFlowReturn
118 gst_proxy_pad_chain_default (GstPad * pad, GstObject * parent,
119     GstBuffer * buffer)
120 {
121   GstFlowReturn res;
122   GstPad *internal;
123
124   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
125   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
126
127   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
128   res = gst_pad_push (internal, buffer);
129   GST_PROXY_PAD_RELEASE_INTERNAL (internal);
130
131   return res;
132 }
133
134 /**
135  * gst_proxy_pad_chain_list_default:
136  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
137  * @parent: the parent of @pad or NULL
138  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
139  *     if not.
140  *
141  * Invoke the default chain list function of the proxy pad.
142  *
143  * Returns: a #GstFlowReturn from the pad.
144  */
145 GstFlowReturn
146 gst_proxy_pad_chain_list_default (GstPad * pad, GstObject * parent,
147     GstBufferList * list)
148 {
149   GstFlowReturn res;
150   GstPad *internal;
151
152   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
153   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
154
155   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
156   res = gst_pad_push_list (internal, list);
157   GST_PROXY_PAD_RELEASE_INTERNAL (internal);
158
159   return res;
160 }
161
162 /**
163  * gst_proxy_pad_getrange_default:
164  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
165  * @parent: the parent of @pad
166  * @offset: The start offset of the buffer
167  * @size: The length of the buffer
168  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
169  *     returns #GST_FLOW_ERROR if %NULL.
170  *
171  * Invoke the default getrange function of the proxy pad.
172  *
173  * Returns: a #GstFlowReturn from the pad.
174  */
175 GstFlowReturn
176 gst_proxy_pad_getrange_default (GstPad * pad, GstObject * parent,
177     guint64 offset, guint size, GstBuffer ** buffer)
178 {
179   GstFlowReturn res;
180   GstPad *internal;
181
182   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
183   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
184
185   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
186   res = gst_pad_pull_range (internal, offset, size, buffer);
187   GST_PROXY_PAD_RELEASE_INTERNAL (internal);
188
189   return res;
190 }
191
192 static GstPad *
193 gst_proxy_pad_get_target (GstPad * pad)
194 {
195   GstPad *target;
196
197   GST_OBJECT_LOCK (pad);
198   target = GST_PROXY_PAD_TARGET (pad);
199   if (target)
200     gst_object_ref (target);
201   GST_OBJECT_UNLOCK (pad);
202
203   return target;
204 }
205
206 /**
207  * gst_proxy_pad_get_internal:
208  * @pad: the #GstProxyPad
209  *
210  * Get the internal pad of @pad. Unref target pad after usage.
211  *
212  * The internal pad of a #GstGhostPad is the internally used
213  * pad of opposite direction, which is used to link to the target.
214  *
215  * Returns: (transfer full): the target #GstProxyPad, can be NULL.
216  * Unref target pad after usage.
217  */
218 GstProxyPad *
219 gst_proxy_pad_get_internal (GstProxyPad * pad)
220 {
221   GstPad *internal;
222
223   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
224
225   GST_OBJECT_LOCK (pad);
226   internal = GST_PROXY_PAD_INTERNAL (pad);
227   if (internal)
228     gst_object_ref (internal);
229   GST_OBJECT_UNLOCK (pad);
230
231   return GST_PROXY_PAD_CAST (internal);
232 }
233
234 static void
235 gst_proxy_pad_class_init (GstProxyPadClass * klass)
236 {
237   g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
238
239   /* Register common function pointer descriptions */
240   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
241   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
242   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
243   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
244 }
245
246 static void
247 gst_proxy_pad_init (GstProxyPad * ppad)
248 {
249   GstPad *pad = (GstPad *) ppad;
250
251   GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
252       GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
253
254   gst_pad_set_iterate_internal_links_function (pad,
255       gst_proxy_pad_iterate_internal_links_default);
256
257   GST_PAD_SET_PROXY_CAPS (pad);
258   GST_PAD_SET_PROXY_SCHEDULING (pad);
259   GST_PAD_SET_PROXY_ALLOCATION (pad);
260 }
261
262
263 /***********************************************************************
264  * Ghost pads, implemented as a pair of proxy pads (sort of)
265  */
266
267
268 #define GST_GHOST_PAD_PRIVATE(obj)      (GST_GHOST_PAD_CAST (obj)->priv)
269
270 struct _GstGhostPadPrivate
271 {
272   /* with PROXY_LOCK */
273   gboolean constructed;
274 };
275
276 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
277
278 static void gst_ghost_pad_dispose (GObject * object);
279
280 static gboolean
281 gst_ghost_pad_internal_activate_push_default (GstPad * pad, GstObject * parent,
282     gboolean active)
283 {
284   gboolean ret;
285   GstPad *other;
286
287   GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
288       (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
289
290   /* in both cases (SRC and SINK) we activate just the internal pad. The targets
291    * will be activated later (or already in case of a ghost sinkpad). */
292   other = GST_PROXY_PAD_INTERNAL (pad);
293   ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
294
295   return ret;
296 }
297
298 static gboolean
299 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
300     gboolean active)
301 {
302   gboolean ret;
303   GstPad *other;
304
305   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
306       GST_DEBUG_PAD_NAME (pad));
307
308   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
309     /* we are activated in pull mode by our peer element, which is a sinkpad
310      * that wants to operate in pull mode. This activation has to propagate
311      * upstream through the pipeline. We call the internal activation function,
312      * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
313      * further upstream */
314     GST_LOG_OBJECT (pad, "pad is src, activate internal");
315     other = GST_PROXY_PAD_INTERNAL (pad);
316     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
317   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
318     /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
319      * since we hold a pointer to the upstream peer. */
320     GST_LOG_OBJECT (pad, "activating peer");
321     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
322     gst_object_unref (other);
323   } else {
324     /* this is failure, we can't activate pull if there is no peer */
325     GST_LOG_OBJECT (pad, "not src and no peer, failing");
326     ret = FALSE;
327   }
328
329   return ret;
330 }
331
332 /**
333  * gst_ghost_pad_internal_activate_mode_default:
334  * @pad: the #GstPad to activate or deactivate.
335  * @parent: the parent of @pad or NULL
336  * @mode: the requested activation mode
337  * @active: whether the pad should be active or not.
338  *
339  * Invoke the default activate mode function of a proxy pad that is
340  * owned by a ghost pad.
341  *
342  * Returns: %TRUE if the operation was successful.
343  */
344 gboolean
345 gst_ghost_pad_internal_activate_mode_default (GstPad * pad, GstObject * parent,
346     GstPadMode mode, gboolean active)
347 {
348   gboolean res;
349
350   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
351
352   switch (mode) {
353     case GST_PAD_MODE_PULL:
354       res = gst_ghost_pad_internal_activate_pull_default (pad, parent, active);
355       break;
356     case GST_PAD_MODE_PUSH:
357       res = gst_ghost_pad_internal_activate_push_default (pad, parent, active);
358       break;
359     default:
360       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
361       res = FALSE;
362       break;
363   }
364   return res;
365 }
366
367 static gboolean
368 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
369     gboolean active)
370 {
371   gboolean ret;
372   GstPad *other;
373
374   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
375
376   GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
377       (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
378
379   /* just activate the internal pad */
380   other = GST_PROXY_PAD_INTERNAL (pad);
381   ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
382
383   return ret;
384 }
385
386 static gboolean
387 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
388     gboolean active)
389 {
390   gboolean ret;
391   GstPad *other;
392
393   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
394       GST_DEBUG_PAD_NAME (pad));
395
396   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
397     /* the ghostpad is SRC and activated in pull mode by its peer, call the
398      * activation function of the internal pad to propagate the activation
399      * upstream */
400     GST_LOG_OBJECT (pad, "pad is src, activate internal");
401     other = GST_PROXY_PAD_INTERNAL (pad);
402     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
403   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
404     /* We are SINK and activated by the internal pad, propagate activation
405      * upstream because we hold a ref to the upstream peer */
406     GST_LOG_OBJECT (pad, "activating peer");
407     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
408     gst_object_unref (other);
409   } else {
410     /* no peer, we fail */
411     GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
412     ret = FALSE;
413   }
414
415   return ret;
416 }
417
418 /**
419  * gst_ghost_pad_activate_mode_default:
420  * @pad: the #GstPad to activate or deactivate.
421  * @parent: the parent of @pad or NULL
422  * @mode: the requested activation mode
423  * @active: whether the pad should be active or not.
424  *
425  * Invoke the default activate mode function of a ghost pad.
426  *
427  * Returns: %TRUE if the operation was successful.
428  */
429 gboolean
430 gst_ghost_pad_activate_mode_default (GstPad * pad, GstObject * parent,
431     GstPadMode mode, gboolean active)
432 {
433   gboolean res;
434
435   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
436
437   switch (mode) {
438     case GST_PAD_MODE_PULL:
439       res = gst_ghost_pad_activate_pull_default (pad, parent, active);
440       break;
441     case GST_PAD_MODE_PUSH:
442       res = gst_ghost_pad_activate_push_default (pad, parent, active);
443       break;
444     default:
445       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
446       res = FALSE;
447       break;
448   }
449   return res;
450 }
451
452 static void
453 gst_ghost_pad_class_init (GstGhostPadClass * klass)
454 {
455   GObjectClass *gobject_class = (GObjectClass *) klass;
456
457   g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
458
459   gobject_class->dispose = gst_ghost_pad_dispose;
460
461   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
462   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
463 }
464
465 static void
466 gst_ghost_pad_init (GstGhostPad * pad)
467 {
468   GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
469       GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
470
471   gst_pad_set_activatemode_function (GST_PAD_CAST (pad),
472       gst_ghost_pad_activate_mode_default);
473 }
474
475 static void
476 gst_ghost_pad_dispose (GObject * object)
477 {
478   GstPad *pad;
479   GstPad *internal;
480   GstPad *peer;
481
482   pad = GST_PAD (object);
483
484   GST_DEBUG_OBJECT (pad, "dispose");
485
486   gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
487
488   /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
489    * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
490   peer = gst_pad_get_peer (pad);
491   if (peer) {
492     if (GST_PAD_IS_SRC (pad))
493       gst_pad_unlink (pad, peer);
494     else
495       gst_pad_unlink (peer, pad);
496
497     gst_object_unref (peer);
498   }
499
500   GST_OBJECT_LOCK (pad);
501   internal = GST_PROXY_PAD_INTERNAL (pad);
502
503   gst_pad_set_activatemode_function (internal, NULL);
504
505   /* disposes of the internal pad, since the ghostpad is the only possible object
506    * that has a refcount on the internal pad. */
507   gst_object_unparent (GST_OBJECT_CAST (internal));
508   GST_PROXY_PAD_INTERNAL (pad) = NULL;
509
510   GST_OBJECT_UNLOCK (pad);
511
512   G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
513 }
514
515 /**
516  * gst_ghost_pad_construct:
517  * @gpad: the newly allocated ghost pad
518  *
519  * Finish initialization of a newly allocated ghost pad.
520  *
521  * This function is most useful in language bindings and when subclassing
522  * #GstGhostPad; plugin and application developers normally will not call this
523  * function. Call this function directly after a call to g_object_new
524  * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
525  *
526  * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
527  */
528 gboolean
529 gst_ghost_pad_construct (GstGhostPad * gpad)
530 {
531   GstPadDirection dir, otherdir;
532   GstPadTemplate *templ;
533   GstPad *pad, *internal;
534
535   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
536   g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
537       FALSE);
538
539   g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
540
541   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
542
543   pad = GST_PAD (gpad);
544
545   /* Set directional padfunctions for ghostpad */
546   if (dir == GST_PAD_SINK) {
547     gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
548     gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
549   } else {
550     gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
551   }
552
553   /* INTERNAL PAD, it always exists and is child of the ghostpad */
554   otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
555   if (templ) {
556     internal =
557         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
558         "direction", otherdir, "template", templ, NULL);
559     /* release ref obtained via g_object_get */
560     gst_object_unref (templ);
561   } else {
562     internal =
563         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
564         "direction", otherdir, NULL);
565   }
566   GST_PAD_UNSET_FLUSHING (internal);
567
568   /* Set directional padfunctions for internal pad */
569   if (dir == GST_PAD_SRC) {
570     gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
571     gst_pad_set_chain_list_function (internal,
572         gst_proxy_pad_chain_list_default);
573   } else {
574     gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
575   }
576
577   GST_OBJECT_LOCK (pad);
578
579   /* now make the ghostpad a parent of the internal pad */
580   if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
581           GST_OBJECT_CAST (pad)))
582     goto parent_failed;
583
584   /* The ghostpad is the parent of the internal pad and is the only object that
585    * can have a refcount on the internal pad.
586    * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
587    * a refcount of 1.
588    * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
589    * its refcount on the internal pad in the dispose method by un-parenting it.
590    * This is why we don't take extra refcounts in the assignments below
591    */
592   GST_PROXY_PAD_INTERNAL (pad) = internal;
593   GST_PROXY_PAD_INTERNAL (internal) = pad;
594
595   /* special activation functions for the internal pad */
596   gst_pad_set_activatemode_function (internal,
597       gst_ghost_pad_internal_activate_mode_default);
598
599   GST_OBJECT_UNLOCK (pad);
600
601   GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
602   return TRUE;
603
604   /* ERRORS */
605 parent_failed:
606   {
607     GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
608         GST_DEBUG_PAD_NAME (internal));
609     g_critical ("Could not set internal pad %s:%s",
610         GST_DEBUG_PAD_NAME (internal));
611     GST_OBJECT_UNLOCK (pad);
612     gst_object_unref (internal);
613     return FALSE;
614   }
615 }
616
617 static GstPad *
618 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
619     GstPadTemplate * templ)
620 {
621   GstGhostPad *ret;
622
623   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
624
625   /* OBJECT CREATION */
626   if (templ) {
627     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
628         "direction", dir, "template", templ, NULL);
629   } else {
630     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
631         "direction", dir, NULL);
632   }
633
634   if (!gst_ghost_pad_construct (ret))
635     goto construct_failed;
636
637   return GST_PAD_CAST (ret);
638
639 construct_failed:
640   /* already logged */
641   gst_object_unref (ret);
642   return NULL;
643 }
644
645 /**
646  * gst_ghost_pad_new_no_target:
647  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
648  * @dir: the direction of the ghostpad
649  *
650  * Create a new ghostpad without a target with the given direction.
651  * A target can be set on the ghostpad later with the
652  * gst_ghost_pad_set_target() function.
653  *
654  * The created ghostpad will not have a padtemplate.
655  *
656  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
657  */
658 GstPad *
659 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
660 {
661   GstPad *ret;
662
663   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
664
665   GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
666
667   ret = gst_ghost_pad_new_full (name, dir, NULL);
668
669   return ret;
670 }
671
672 /**
673  * gst_ghost_pad_new:
674  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
675  * @target: (transfer none): the pad to ghost.
676  *
677  * Create a new ghostpad with @target as the target. The direction will be taken
678  * from the target pad. @target must be unlinked.
679  *
680  * Will ref the target.
681  *
682  * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
683  */
684 GstPad *
685 gst_ghost_pad_new (const gchar * name, GstPad * target)
686 {
687   GstPad *ret;
688
689   g_return_val_if_fail (GST_IS_PAD (target), NULL);
690   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
691
692   GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
693       GST_DEBUG_PAD_NAME (target));
694
695   if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
696     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
697       goto set_target_failed;
698
699   return ret;
700
701   /* ERRORS */
702 set_target_failed:
703   {
704     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
705         GST_DEBUG_PAD_NAME (target));
706     gst_object_unref (ret);
707     return NULL;
708   }
709 }
710
711 /**
712  * gst_ghost_pad_new_from_template:
713  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
714  * @target: (transfer none): the pad to ghost.
715  * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
716  *
717  * Create a new ghostpad with @target as the target. The direction will be taken
718  * from the target pad. The template used on the ghostpad will be @template.
719  *
720  * Will ref the target.
721  *
722  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
723  */
724
725 GstPad *
726 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
727     GstPadTemplate * templ)
728 {
729   GstPad *ret;
730
731   g_return_val_if_fail (GST_IS_PAD (target), NULL);
732   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
733   g_return_val_if_fail (templ != NULL, NULL);
734   g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
735       GST_PAD_DIRECTION (target), NULL);
736
737   GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
738       GST_DEBUG_PAD_NAME (target), templ);
739
740   if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
741     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
742       goto set_target_failed;
743
744   return ret;
745
746   /* ERRORS */
747 set_target_failed:
748   {
749     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
750         GST_DEBUG_PAD_NAME (target));
751     gst_object_unref (ret);
752     return NULL;
753   }
754 }
755
756 /**
757  * gst_ghost_pad_new_no_target_from_template:
758  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
759  * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
760  *
761  * Create a new ghostpad based on @templ, without setting a target. The
762  * direction will be taken from the @templ.
763  *
764  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
765  */
766 GstPad *
767 gst_ghost_pad_new_no_target_from_template (const gchar * name,
768     GstPadTemplate * templ)
769 {
770   GstPad *ret;
771
772   g_return_val_if_fail (templ != NULL, NULL);
773
774   ret =
775       gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
776
777   return ret;
778 }
779
780 /**
781  * gst_ghost_pad_get_target:
782  * @gpad: the #GstGhostPad
783  *
784  * Get the target pad of @gpad. Unref target pad after usage.
785  *
786  * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
787  * has no target set. Unref target pad after usage.
788  */
789 GstPad *
790 gst_ghost_pad_get_target (GstGhostPad * gpad)
791 {
792   GstPad *ret;
793
794   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
795
796   ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
797
798   GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
799
800   return ret;
801 }
802
803 /**
804  * gst_ghost_pad_set_target:
805  * @gpad: the #GstGhostPad
806  * @newtarget: (transfer none) (allow-none): the new pad target
807  *
808  * Set the new target of the ghostpad @gpad. Any existing target
809  * is unlinked and links to the new target are established. if @newtarget is
810  * NULL the target will be cleared.
811  *
812  * Returns: (transfer full): TRUE if the new target could be set. This function
813  *     can return FALSE when the internal pads could not be linked.
814  */
815 gboolean
816 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
817 {
818   GstPad *internal;
819   GstPad *oldtarget;
820   GstPadLinkReturn lret;
821
822   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
823   g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
824   g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
825
826   GST_OBJECT_LOCK (gpad);
827   internal = GST_PROXY_PAD_INTERNAL (gpad);
828
829   if (newtarget)
830     GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
831   else
832     GST_DEBUG_OBJECT (gpad, "clearing target");
833
834   /* clear old target */
835   if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
836     GST_OBJECT_UNLOCK (gpad);
837
838     /* unlink internal pad */
839     if (GST_PAD_IS_SRC (internal))
840       gst_pad_unlink (internal, oldtarget);
841     else
842       gst_pad_unlink (oldtarget, internal);
843   } else {
844     GST_OBJECT_UNLOCK (gpad);
845   }
846
847   if (newtarget) {
848     /* and link to internal pad without any checks */
849     GST_DEBUG_OBJECT (gpad, "connecting internal pad to target %"
850         GST_PTR_FORMAT, newtarget);
851
852     if (GST_PAD_IS_SRC (internal))
853       lret =
854           gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
855     else
856       lret =
857           gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
858
859     if (lret != GST_PAD_LINK_OK)
860       goto link_failed;
861   }
862
863   return TRUE;
864
865   /* ERRORS */
866 link_failed:
867   {
868     GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
869         lret);
870     return FALSE;
871   }
872 }