ghostpad: also ref the internal pad for activate functions
[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   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
293   ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
294   GST_PROXY_PAD_RELEASE_INTERNAL (other);
295
296   return ret;
297 }
298
299 static gboolean
300 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
301     gboolean active)
302 {
303   gboolean ret;
304   GstPad *other;
305
306   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
307       GST_DEBUG_PAD_NAME (pad));
308
309   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
310     /* we are activated in pull mode by our peer element, which is a sinkpad
311      * that wants to operate in pull mode. This activation has to propagate
312      * upstream through the pipeline. We call the internal activation function,
313      * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
314      * further upstream */
315     GST_LOG_OBJECT (pad, "pad is src, activate internal");
316     GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
317     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
318     GST_PROXY_PAD_RELEASE_INTERNAL (other);
319   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
320     /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
321      * since we hold a pointer to the upstream peer. */
322     GST_LOG_OBJECT (pad, "activating peer");
323     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
324     gst_object_unref (other);
325   } else {
326     /* this is failure, we can't activate pull if there is no peer */
327     GST_LOG_OBJECT (pad, "not src and no peer, failing");
328     ret = FALSE;
329   }
330
331   return ret;
332 }
333
334 /**
335  * gst_ghost_pad_internal_activate_mode_default:
336  * @pad: the #GstPad to activate or deactivate.
337  * @parent: the parent of @pad or NULL
338  * @mode: the requested activation mode
339  * @active: whether the pad should be active or not.
340  *
341  * Invoke the default activate mode function of a proxy pad that is
342  * owned by a ghost pad.
343  *
344  * Returns: %TRUE if the operation was successful.
345  */
346 gboolean
347 gst_ghost_pad_internal_activate_mode_default (GstPad * pad, GstObject * parent,
348     GstPadMode mode, gboolean active)
349 {
350   gboolean res;
351
352   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
353
354   switch (mode) {
355     case GST_PAD_MODE_PULL:
356       res = gst_ghost_pad_internal_activate_pull_default (pad, parent, active);
357       break;
358     case GST_PAD_MODE_PUSH:
359       res = gst_ghost_pad_internal_activate_push_default (pad, parent, active);
360       break;
361     default:
362       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
363       res = FALSE;
364       break;
365   }
366   return res;
367 }
368
369 static gboolean
370 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
371     gboolean active)
372 {
373   gboolean ret;
374   GstPad *other;
375
376   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
377
378   GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
379       (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
380
381   /* just activate the internal pad */
382   GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
383   ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
384   GST_PROXY_PAD_RELEASE_INTERNAL (other);
385
386   return ret;
387 }
388
389 static gboolean
390 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
391     gboolean active)
392 {
393   gboolean ret;
394   GstPad *other;
395
396   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
397       GST_DEBUG_PAD_NAME (pad));
398
399   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
400     /* the ghostpad is SRC and activated in pull mode by its peer, call the
401      * activation function of the internal pad to propagate the activation
402      * upstream */
403     GST_LOG_OBJECT (pad, "pad is src, activate internal");
404     GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
405     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
406     GST_PROXY_PAD_RELEASE_INTERNAL (other);
407   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
408     /* We are SINK and activated by the internal pad, propagate activation
409      * upstream because we hold a ref to the upstream peer */
410     GST_LOG_OBJECT (pad, "activating peer");
411     ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
412     gst_object_unref (other);
413   } else {
414     /* no peer, we fail */
415     GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
416     ret = FALSE;
417   }
418
419   return ret;
420 }
421
422 /**
423  * gst_ghost_pad_activate_mode_default:
424  * @pad: the #GstPad to activate or deactivate.
425  * @parent: the parent of @pad or NULL
426  * @mode: the requested activation mode
427  * @active: whether the pad should be active or not.
428  *
429  * Invoke the default activate mode function of a ghost pad.
430  *
431  * Returns: %TRUE if the operation was successful.
432  */
433 gboolean
434 gst_ghost_pad_activate_mode_default (GstPad * pad, GstObject * parent,
435     GstPadMode mode, gboolean active)
436 {
437   gboolean res;
438
439   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
440
441   switch (mode) {
442     case GST_PAD_MODE_PULL:
443       res = gst_ghost_pad_activate_pull_default (pad, parent, active);
444       break;
445     case GST_PAD_MODE_PUSH:
446       res = gst_ghost_pad_activate_push_default (pad, parent, active);
447       break;
448     default:
449       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
450       res = FALSE;
451       break;
452   }
453   return res;
454 }
455
456 static void
457 gst_ghost_pad_class_init (GstGhostPadClass * klass)
458 {
459   GObjectClass *gobject_class = (GObjectClass *) klass;
460
461   g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
462
463   gobject_class->dispose = gst_ghost_pad_dispose;
464
465   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
466   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
467 }
468
469 static void
470 gst_ghost_pad_init (GstGhostPad * pad)
471 {
472   GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
473       GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
474
475   gst_pad_set_activatemode_function (GST_PAD_CAST (pad),
476       gst_ghost_pad_activate_mode_default);
477 }
478
479 static void
480 gst_ghost_pad_dispose (GObject * object)
481 {
482   GstPad *pad;
483   GstPad *internal;
484   GstPad *peer;
485
486   pad = GST_PAD (object);
487
488   GST_DEBUG_OBJECT (pad, "dispose");
489
490   gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
491
492   /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
493    * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
494   peer = gst_pad_get_peer (pad);
495   if (peer) {
496     if (GST_PAD_IS_SRC (pad))
497       gst_pad_unlink (pad, peer);
498     else
499       gst_pad_unlink (peer, pad);
500
501     gst_object_unref (peer);
502   }
503
504   GST_OBJECT_LOCK (pad);
505   internal = GST_PROXY_PAD_INTERNAL (pad);
506
507   gst_pad_set_activatemode_function (internal, NULL);
508
509   /* disposes of the internal pad, since the ghostpad is the only possible object
510    * that has a refcount on the internal pad. */
511   gst_object_unparent (GST_OBJECT_CAST (internal));
512   GST_PROXY_PAD_INTERNAL (pad) = NULL;
513
514   GST_OBJECT_UNLOCK (pad);
515
516   G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
517 }
518
519 /**
520  * gst_ghost_pad_construct:
521  * @gpad: the newly allocated ghost pad
522  *
523  * Finish initialization of a newly allocated ghost pad.
524  *
525  * This function is most useful in language bindings and when subclassing
526  * #GstGhostPad; plugin and application developers normally will not call this
527  * function. Call this function directly after a call to g_object_new
528  * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
529  *
530  * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
531  */
532 gboolean
533 gst_ghost_pad_construct (GstGhostPad * gpad)
534 {
535   GstPadDirection dir, otherdir;
536   GstPadTemplate *templ;
537   GstPad *pad, *internal;
538
539   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
540   g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
541       FALSE);
542
543   g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
544
545   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
546
547   pad = GST_PAD (gpad);
548
549   /* Set directional padfunctions for ghostpad */
550   if (dir == GST_PAD_SINK) {
551     gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
552     gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
553   } else {
554     gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
555   }
556
557   /* INTERNAL PAD, it always exists and is child of the ghostpad */
558   otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
559   if (templ) {
560     internal =
561         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
562         "direction", otherdir, "template", templ, NULL);
563     /* release ref obtained via g_object_get */
564     gst_object_unref (templ);
565   } else {
566     internal =
567         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
568         "direction", otherdir, NULL);
569   }
570   GST_PAD_UNSET_FLUSHING (internal);
571
572   /* Set directional padfunctions for internal pad */
573   if (dir == GST_PAD_SRC) {
574     gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
575     gst_pad_set_chain_list_function (internal,
576         gst_proxy_pad_chain_list_default);
577   } else {
578     gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
579   }
580
581   GST_OBJECT_LOCK (pad);
582
583   /* now make the ghostpad a parent of the internal pad */
584   if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
585           GST_OBJECT_CAST (pad)))
586     goto parent_failed;
587
588   /* The ghostpad is the parent of the internal pad and is the only object that
589    * can have a refcount on the internal pad.
590    * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
591    * a refcount of 1.
592    * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
593    * its refcount on the internal pad in the dispose method by un-parenting it.
594    * This is why we don't take extra refcounts in the assignments below
595    */
596   GST_PROXY_PAD_INTERNAL (pad) = internal;
597   GST_PROXY_PAD_INTERNAL (internal) = pad;
598
599   /* special activation functions for the internal pad */
600   gst_pad_set_activatemode_function (internal,
601       gst_ghost_pad_internal_activate_mode_default);
602
603   GST_OBJECT_UNLOCK (pad);
604
605   GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
606   return TRUE;
607
608   /* ERRORS */
609 parent_failed:
610   {
611     GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
612         GST_DEBUG_PAD_NAME (internal));
613     g_critical ("Could not set internal pad %s:%s",
614         GST_DEBUG_PAD_NAME (internal));
615     GST_OBJECT_UNLOCK (pad);
616     gst_object_unref (internal);
617     return FALSE;
618   }
619 }
620
621 static GstPad *
622 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
623     GstPadTemplate * templ)
624 {
625   GstGhostPad *ret;
626
627   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
628
629   /* OBJECT CREATION */
630   if (templ) {
631     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
632         "direction", dir, "template", templ, NULL);
633   } else {
634     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
635         "direction", dir, NULL);
636   }
637
638   if (!gst_ghost_pad_construct (ret))
639     goto construct_failed;
640
641   return GST_PAD_CAST (ret);
642
643 construct_failed:
644   /* already logged */
645   gst_object_unref (ret);
646   return NULL;
647 }
648
649 /**
650  * gst_ghost_pad_new_no_target:
651  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
652  * @dir: the direction of the ghostpad
653  *
654  * Create a new ghostpad without a target with the given direction.
655  * A target can be set on the ghostpad later with the
656  * gst_ghost_pad_set_target() function.
657  *
658  * The created ghostpad will not have a padtemplate.
659  *
660  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
661  */
662 GstPad *
663 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
664 {
665   GstPad *ret;
666
667   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
668
669   GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
670
671   ret = gst_ghost_pad_new_full (name, dir, NULL);
672
673   return ret;
674 }
675
676 /**
677  * gst_ghost_pad_new:
678  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
679  * @target: (transfer none): the pad to ghost.
680  *
681  * Create a new ghostpad with @target as the target. The direction will be taken
682  * from the target pad. @target must be unlinked.
683  *
684  * Will ref the target.
685  *
686  * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
687  */
688 GstPad *
689 gst_ghost_pad_new (const gchar * name, GstPad * target)
690 {
691   GstPad *ret;
692
693   g_return_val_if_fail (GST_IS_PAD (target), NULL);
694   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
695
696   GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
697       GST_DEBUG_PAD_NAME (target));
698
699   if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
700     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
701       goto set_target_failed;
702
703   return ret;
704
705   /* ERRORS */
706 set_target_failed:
707   {
708     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
709         GST_DEBUG_PAD_NAME (target));
710     gst_object_unref (ret);
711     return NULL;
712   }
713 }
714
715 /**
716  * gst_ghost_pad_new_from_template:
717  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
718  * @target: (transfer none): the pad to ghost.
719  * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
720  *
721  * Create a new ghostpad with @target as the target. The direction will be taken
722  * from the target pad. The template used on the ghostpad will be @template.
723  *
724  * Will ref the target.
725  *
726  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
727  */
728
729 GstPad *
730 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
731     GstPadTemplate * templ)
732 {
733   GstPad *ret;
734
735   g_return_val_if_fail (GST_IS_PAD (target), NULL);
736   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
737   g_return_val_if_fail (templ != NULL, NULL);
738   g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
739       GST_PAD_DIRECTION (target), NULL);
740
741   GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
742       GST_DEBUG_PAD_NAME (target), templ);
743
744   if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
745     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
746       goto set_target_failed;
747
748   return ret;
749
750   /* ERRORS */
751 set_target_failed:
752   {
753     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
754         GST_DEBUG_PAD_NAME (target));
755     gst_object_unref (ret);
756     return NULL;
757   }
758 }
759
760 /**
761  * gst_ghost_pad_new_no_target_from_template:
762  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
763  * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
764  *
765  * Create a new ghostpad based on @templ, without setting a target. The
766  * direction will be taken from the @templ.
767  *
768  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
769  */
770 GstPad *
771 gst_ghost_pad_new_no_target_from_template (const gchar * name,
772     GstPadTemplate * templ)
773 {
774   GstPad *ret;
775
776   g_return_val_if_fail (templ != NULL, NULL);
777
778   ret =
779       gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
780
781   return ret;
782 }
783
784 /**
785  * gst_ghost_pad_get_target:
786  * @gpad: the #GstGhostPad
787  *
788  * Get the target pad of @gpad. Unref target pad after usage.
789  *
790  * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
791  * has no target set. Unref target pad after usage.
792  */
793 GstPad *
794 gst_ghost_pad_get_target (GstGhostPad * gpad)
795 {
796   GstPad *ret;
797
798   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
799
800   ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
801
802   GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
803
804   return ret;
805 }
806
807 /**
808  * gst_ghost_pad_set_target:
809  * @gpad: the #GstGhostPad
810  * @newtarget: (transfer none) (allow-none): the new pad target
811  *
812  * Set the new target of the ghostpad @gpad. Any existing target
813  * is unlinked and links to the new target are established. if @newtarget is
814  * NULL the target will be cleared.
815  *
816  * Returns: (transfer full): TRUE if the new target could be set. This function
817  *     can return FALSE when the internal pads could not be linked.
818  */
819 gboolean
820 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
821 {
822   GstPad *internal;
823   GstPad *oldtarget;
824   GstPadLinkReturn lret;
825
826   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
827   g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
828   g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
829
830   GST_OBJECT_LOCK (gpad);
831   internal = GST_PROXY_PAD_INTERNAL (gpad);
832
833   if (newtarget)
834     GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
835   else
836     GST_DEBUG_OBJECT (gpad, "clearing target");
837
838   /* clear old target */
839   if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
840     GST_OBJECT_UNLOCK (gpad);
841
842     /* unlink internal pad */
843     if (GST_PAD_IS_SRC (internal))
844       gst_pad_unlink (internal, oldtarget);
845     else
846       gst_pad_unlink (oldtarget, internal);
847   } else {
848     GST_OBJECT_UNLOCK (gpad);
849   }
850
851   if (newtarget) {
852     /* and link to internal pad without any checks */
853     GST_DEBUG_OBJECT (gpad, "connecting internal pad to target %"
854         GST_PTR_FORMAT, newtarget);
855
856     if (GST_PAD_IS_SRC (internal))
857       lret =
858           gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
859     else
860       lret =
861           gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
862
863     if (lret != GST_PAD_LINK_OK)
864       goto link_failed;
865   }
866
867   return TRUE;
868
869   /* ERRORS */
870 link_failed:
871   {
872     GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
873         lret);
874     return FALSE;
875   }
876 }