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