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