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