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