Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstghostpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Andy Wingo <wingo@pobox.com>
5  *                    2006 Edward Hervey <bilboed@bilboed.com>
6  *
7  * gstghostpad.c: Proxy pads
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /**
26  * SECTION:gstghostpad
27  * @short_description: Pseudo link pads
28  * @see_also: #GstPad
29  *
30  * GhostPads are useful when organizing pipelines with #GstBin like elements.
31  * The idea here is to create hierarchical element graphs. The bin element
32  * contains a sub-graph. Now one would like to treat the bin-element like any
33  * other #GstElement. This is where GhostPads come into play. A GhostPad acts as
34  * a proxy for another pad. Thus the bin can have sink and source ghost-pads
35  * that are associated with sink and source pads of the child elements.
36  *
37  * If the target pad is known at creation time, gst_ghost_pad_new() is the
38  * function to use to get a ghost-pad. Otherwise one can use gst_ghost_pad_new_no_target()
39  * to create the ghost-pad and use gst_ghost_pad_set_target() to establish the
40  * association later on.
41  *
42  * Note that GhostPads add overhead to the data processing of a pipeline.
43  *
44  * Last reviewed on 2005-11-18 (0.9.5)
45  */
46
47 #include "gst_private.h"
48 #include "gstinfo.h"
49
50 #include "gstghostpad.h"
51 #include "gst.h"
52
53 #define GST_CAT_DEFAULT GST_CAT_PADS
54
55 #define GST_PROXY_PAD_CAST(obj)         ((GstProxyPad *)obj)
56 #define GST_PROXY_PAD_PRIVATE(obj)      (GST_PROXY_PAD_CAST (obj)->priv)
57 #define GST_PROXY_PAD_TARGET(pad)       (GST_PAD_PEER (GST_PROXY_PAD_INTERNAL (pad)))
58 #define GST_PROXY_PAD_INTERNAL(pad)     (GST_PROXY_PAD_PRIVATE (pad)->internal)
59
60 struct _GstProxyPadPrivate
61 {
62   GstPad *internal;
63 };
64
65 G_DEFINE_TYPE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
66
67 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
68
69 /**
70  * gst_proxy_pad_query_type_default:
71  * @pad: a #GstPad.
72  *
73  * Invoke the default query type handler of the proxy pad.
74  *
75  * Returns: (transfer none) (array zero-terminated=1): a zero-terminated array
76  *     of #GstQueryType.
77  *
78  * Since: 0.10.35
79  */
80 const GstQueryType *
81 gst_proxy_pad_query_type_default (GstPad * pad)
82 {
83   GstPad *target;
84   const GstQueryType *res = NULL;
85
86   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
87
88   if (!(target = gst_proxy_pad_get_target (pad)))
89     goto no_target;
90
91   res = gst_pad_get_query_types (target);
92   gst_object_unref (target);
93
94   return res;
95
96   /* ERRORS */
97 no_target:
98   {
99     GST_DEBUG_OBJECT (pad, "no target pad");
100     return FALSE;
101   }
102 }
103
104 /**
105  * gst_proxy_pad_event_default:
106  * @pad: a #GstPad to push the event to.
107  * @event: (transfer full): the #GstEvent to send to the pad.
108  *
109  * Invoke the default event of the proxy pad.
110  *
111  * Returns: TRUE if the event was handled.
112  *
113  * Since: 0.10.35
114  */
115 gboolean
116 gst_proxy_pad_event_default (GstPad * pad, GstEvent * event)
117 {
118   gboolean res;
119   GstPad *internal;
120
121   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
122   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
123
124   internal = GST_PROXY_PAD_INTERNAL (pad);
125   res = gst_pad_push_event (internal, event);
126
127   return res;
128 }
129
130 /**
131  * gst_proxy_pad_query_default:
132  * @pad: a #GstPad to invoke the default query on.
133  * @query: (transfer none): the #GstQuery to perform.
134  *
135  * Invoke the default query function of the proxy pad.
136  *
137  * Returns: TRUE if the query could be performed.
138  *
139  * Since: 0.10.35
140  */
141 gboolean
142 gst_proxy_pad_query_default (GstPad * pad, GstQuery * query)
143 {
144   gboolean res;
145   GstPad *target;
146
147   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
148   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
149
150   if (!(target = gst_proxy_pad_get_target (pad)))
151     goto no_target;
152
153   res = gst_pad_query (target, query);
154   gst_object_unref (target);
155
156   return res;
157
158   /* ERRORS */
159 no_target:
160   {
161     GST_DEBUG_OBJECT (pad, "no target pad");
162     return FALSE;
163   }
164 }
165
166 /**
167  * gst_proyx_pad_iterate_internal_links_default:
168  * @pad: the #GstPad to get the internal links of.
169  *
170  * Invoke the default iterate internal links function of the proxy pad.
171  *
172  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
173  * returned pad with gst_object_unref().
174  *
175  * Since: 0.10.35
176  */
177 GstIterator *
178 gst_proxy_pad_iterate_internal_links_default (GstPad * pad)
179 {
180   GstIterator *res = NULL;
181   GstPad *internal;
182   GValue v = { 0, };
183
184   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
185
186   internal = GST_PROXY_PAD_INTERNAL (pad);
187   g_value_init (&v, GST_TYPE_PAD);
188   g_value_set_object (&v, internal);
189   res = gst_iterator_new_single (GST_TYPE_PAD, &v);
190   g_value_unset (&v);
191
192   return res;
193 }
194
195 /**
196  * gst_proxy_pad_chain_default:
197  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
198  * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
199  *     if not.
200  *
201  * Invoke the default chain function of the proxy pad.
202  *
203  * Returns: a #GstFlowReturn from the pad.
204  *
205  * Since: 0.10.35
206  */
207 GstFlowReturn
208 gst_proxy_pad_chain_default (GstPad * pad, GstBuffer * buffer)
209 {
210   GstFlowReturn res;
211   GstPad *internal;
212
213   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
214   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
215
216   internal = GST_PROXY_PAD_INTERNAL (pad);
217   res = gst_pad_push (internal, buffer);
218
219   return res;
220 }
221
222 /**
223  * gst_proxy_pad_chain_list_default:
224  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
225  * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
226  *     if not.
227  *
228  * Invoke the default chain list function of the proxy pad.
229  *
230  * Returns: a #GstFlowReturn from the pad.
231  *
232  * Since: 0.10.35
233  */
234 GstFlowReturn
235 gst_proxy_pad_chain_list_default (GstPad * pad, GstBufferList * list)
236 {
237   GstFlowReturn res;
238   GstPad *internal;
239
240   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
241   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
242
243   internal = GST_PROXY_PAD_INTERNAL (pad);
244   res = gst_pad_push_list (internal, list);
245
246   return res;
247 }
248
249 /**
250  * gst_proxy_pad_get_range_default:
251  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
252  * @offset: The start offset of the buffer
253  * @size: The length of the buffer
254  * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
255  *     returns #GST_FLOW_ERROR if %NULL.
256  *
257  * Invoke the default getrange function of the proxy pad.
258  *
259  * Returns: a #GstFlowReturn from the pad.
260  *
261  * Since: 0.10.35
262  */
263 GstFlowReturn
264 gst_proxy_pad_getrange_default (GstPad * pad, guint64 offset, guint size,
265     GstBuffer ** buffer)
266 {
267   GstFlowReturn res;
268   GstPad *internal;
269
270   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
271   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
272
273   internal = GST_PROXY_PAD_INTERNAL (pad);
274   res = gst_pad_pull_range (internal, offset, size, buffer);
275
276   return res;
277 }
278
279 /**
280  * gst_proxy_pad_getcaps_default:
281  * @pad: a #GstPad to get the capabilities of.
282  * @filter: a #GstCaps filter.
283  *
284  * Invoke the default getcaps function of the proxy pad.
285  *
286  * Returns: (transfer full): the caps of the pad with incremented ref-count
287  *
288  * Since: 0.10.35
289  */
290 GstCaps *
291 gst_proxy_pad_getcaps_default (GstPad * pad, GstCaps * filter)
292 {
293   GstPad *target;
294   GstCaps *res;
295   GstPadTemplate *templ;
296
297   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
298
299   templ = GST_PAD_PAD_TEMPLATE (pad);
300   target = gst_proxy_pad_get_target (pad);
301   if (target) {
302     /* if we have a real target, proxy the call */
303     res = gst_pad_get_caps (target, filter);
304
305     GST_DEBUG_OBJECT (pad, "get caps of target %s:%s : %" GST_PTR_FORMAT,
306         GST_DEBUG_PAD_NAME (target), res);
307
308     gst_object_unref (target);
309
310     /* filter against the template */
311     if (templ && res) {
312       GstCaps *filt, *tmp;
313
314       filt = GST_PAD_TEMPLATE_CAPS (templ);
315       if (filt) {
316         tmp = gst_caps_intersect_full (res, filt, GST_CAPS_INTERSECT_FIRST);
317         gst_caps_unref (res);
318         res = tmp;
319         GST_DEBUG_OBJECT (pad,
320             "filtered against template gives %" GST_PTR_FORMAT, res);
321       }
322     }
323   } else {
324     /* else, if we have a template, use its caps. */
325     if (templ) {
326       res = GST_PAD_TEMPLATE_CAPS (templ);
327       GST_DEBUG_OBJECT (pad,
328           "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, res,
329           res);
330       res = gst_caps_ref (res);
331
332       if (filter) {
333         GstCaps *intersection =
334             gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
335
336         gst_caps_unref (res);
337         res = intersection;
338       }
339
340       goto done;
341     }
342
343     /* If there's a filter, return that */
344     if (filter != NULL) {
345       res = gst_caps_ref (filter);
346       goto done;
347     }
348
349     /* last resort, any caps */
350     GST_DEBUG_OBJECT (pad, "pad has no template, returning ANY");
351     res = gst_caps_new_any ();
352   }
353
354 done:
355   return res;
356 }
357
358 /**
359  * gst_proxy_pad_acceptcaps_default:
360  * @pad: a #GstPad to check
361  * @caps: a #GstCaps to check on the pad
362  *
363  * Invoke the default acceptcaps function of the proxy pad.
364  *
365  * Returns: TRUE if the pad can accept the caps.
366  *
367  * Since: 0.10.35
368  */
369 gboolean
370 gst_proxy_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
371 {
372   GstPad *target;
373   gboolean res;
374
375   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
376   g_return_val_if_fail (caps == NULL || GST_IS_CAPS (caps), FALSE);
377
378   target = gst_proxy_pad_get_target (pad);
379   if (target) {
380     res = gst_pad_accept_caps (target, caps);
381     gst_object_unref (target);
382   } else {
383     GST_DEBUG_OBJECT (pad, "no target");
384     /* We don't have a target, we return TRUE and we assume that any future
385      * target will be able to deal with any configured caps. */
386     res = TRUE;
387   }
388
389   return res;
390 }
391
392 /**
393  * gst_proxy_pad_fixatecaps_default:
394  * @pad: a  #GstPad to fixate
395  * @caps: the  #GstCaps to fixate
396  *
397  * Invoke the default fixatecaps function of the proxy pad.
398  *
399  * Since: 0.10.35
400  */
401 void
402 gst_proxy_pad_fixatecaps_default (GstPad * pad, GstCaps * caps)
403 {
404   GstPad *target;
405
406   g_return_if_fail (GST_IS_PROXY_PAD (pad));
407   g_return_if_fail (GST_IS_CAPS (caps));
408
409   if (!(target = gst_proxy_pad_get_target (pad)))
410     goto no_target;
411
412   gst_pad_fixate_caps (target, caps);
413   gst_object_unref (target);
414
415   return;
416
417   /* ERRORS */
418 no_target:
419   {
420     GST_DEBUG_OBJECT (pad, "no target");
421     return;
422   }
423 }
424
425 static GstPad *
426 gst_proxy_pad_get_target (GstPad * pad)
427 {
428   GstPad *target;
429
430   GST_OBJECT_LOCK (pad);
431   target = GST_PROXY_PAD_TARGET (pad);
432   if (target)
433     gst_object_ref (target);
434   GST_OBJECT_UNLOCK (pad);
435
436   return target;
437 }
438
439 /**
440  * gst_proxy_pad_get_internal:
441  * @pad: the #GstProxyPad
442  *
443  * Get the internal pad of @pad. Unref target pad after usage.
444  *
445  * The internal pad of a #GstGhostPad is the internally used
446  * pad of opposite direction, which is used to link to the target.
447  *
448  * Returns: (transfer full): the target #GstProxyPad, can be NULL.
449  * Unref target pad after usage.
450  *
451  * Since: 0.10.35
452  */
453 GstProxyPad *
454 gst_proxy_pad_get_internal (GstProxyPad * pad)
455 {
456   GstPad *internal;
457
458   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
459
460   GST_OBJECT_LOCK (pad);
461   internal = GST_PROXY_PAD_INTERNAL (pad);
462   if (internal)
463     gst_object_ref (internal);
464   GST_OBJECT_UNLOCK (pad);
465
466   return GST_PROXY_PAD_CAST (internal);
467 }
468
469 /**
470  * gst_proxy_pad_unlink_default:
471  * @pad: a #GstPad to unlink
472  *
473  * Invoke the default unlink function of the proxy pad.
474  *
475  * Since: 0.10.35
476  */
477 void
478 gst_proxy_pad_unlink_default (GstPad * pad)
479 {
480   /* nothing to do anymore */
481   GST_DEBUG_OBJECT (pad, "pad is unlinked");
482 }
483
484 static void
485 gst_proxy_pad_class_init (GstProxyPadClass * klass)
486 {
487   g_type_class_add_private (klass, sizeof (GstProxyPadPrivate));
488
489   /* Register common function pointer descriptions */
490   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_type_default);
491   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_event_default);
492   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_query_default);
493   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
494   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getcaps_default);
495   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_acceptcaps_default);
496   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_fixatecaps_default);
497   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_unlink_default);
498   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
499   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
500   GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
501 }
502
503 static void
504 gst_proxy_pad_init (GstProxyPad * ppad)
505 {
506   GstPad *pad = (GstPad *) ppad;
507
508   GST_PROXY_PAD_PRIVATE (ppad) = G_TYPE_INSTANCE_GET_PRIVATE (ppad,
509       GST_TYPE_PROXY_PAD, GstProxyPadPrivate);
510
511   gst_pad_set_query_type_function (pad, gst_proxy_pad_query_type_default);
512   gst_pad_set_event_function (pad, gst_proxy_pad_event_default);
513   gst_pad_set_query_function (pad, gst_proxy_pad_query_default);
514   gst_pad_set_iterate_internal_links_function (pad,
515       gst_proxy_pad_iterate_internal_links_default);
516
517   gst_pad_set_getcaps_function (pad, gst_proxy_pad_getcaps_default);
518   gst_pad_set_acceptcaps_function (pad, gst_proxy_pad_acceptcaps_default);
519   gst_pad_set_fixatecaps_function (pad, gst_proxy_pad_fixatecaps_default);
520   gst_pad_set_unlink_function (pad, gst_proxy_pad_unlink_default);
521 }
522
523
524 /***********************************************************************
525  * Ghost pads, implemented as a pair of proxy pads (sort of)
526  */
527
528
529 #define GST_GHOST_PAD_PRIVATE(obj)      (GST_GHOST_PAD_CAST (obj)->priv)
530
531 struct _GstGhostPadPrivate
532 {
533   /* with PROXY_LOCK */
534   gboolean constructed;
535 };
536
537 G_DEFINE_TYPE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
538
539 static void gst_ghost_pad_dispose (GObject * object);
540
541 /**
542  * gst_ghost_pad_internal_activate_push_default:
543  * @pad: the #GstPad to activate or deactivate.
544  * @active: whether the pad should be active or not.
545  *
546  * Invoke the default activate push function of a proxy pad that is
547  * owned by a ghost pad.
548  *
549  * Returns: %TRUE if the operation was successful.
550  *
551  * Since: 0.10.35
552  */
553 gboolean
554 gst_ghost_pad_internal_activate_push_default (GstPad * pad, gboolean active)
555 {
556   gboolean ret;
557   GstPad *other;
558
559   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
560
561   GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
562       (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
563
564   /* in both cases (SRC and SINK) we activate just the internal pad. The targets
565    * will be activated later (or already in case of a ghost sinkpad). */
566   other = GST_PROXY_PAD_INTERNAL (pad);
567   ret = gst_pad_activate_push (other, active);
568
569   return ret;
570 }
571
572 /**
573  * gst_ghost_pad_internal_activate_pull_default:
574  * @pad: the #GstPad to activate or deactivate.
575  * @active: whether the pad should be active or not.
576  *
577  * Invoke the default activate pull function of a proxy pad that is
578  * owned by a ghost pad.
579  *
580  * Returns: %TRUE if the operation was successful.
581  *
582  * Since: 0.10.35
583  */
584 gboolean
585 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, gboolean active)
586 {
587   gboolean ret;
588   GstPad *other;
589
590   g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
591
592   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
593       GST_DEBUG_PAD_NAME (pad));
594
595   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
596     /* we are activated in pull mode by our peer element, which is a sinkpad
597      * that wants to operate in pull mode. This activation has to propagate
598      * upstream through the pipeline. We call the internal activation function,
599      * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
600      * further upstream */
601     GST_LOG_OBJECT (pad, "pad is src, activate internal");
602     other = GST_PROXY_PAD_INTERNAL (pad);
603     ret = gst_pad_activate_pull (other, active);
604   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
605     /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
606      * since we hold a pointer to the upstream peer. */
607     GST_LOG_OBJECT (pad, "activating peer");
608     ret = gst_pad_activate_pull (other, active);
609     gst_object_unref (other);
610   } else {
611     /* this is failure, we can't activate pull if there is no peer */
612     GST_LOG_OBJECT (pad, "not src and no peer, failing");
613     ret = FALSE;
614   }
615
616   return ret;
617 }
618
619 /**
620  * gst_ghost_pad_activate_push_default:
621  * @pad: the #GstPad to activate or deactivate.
622  * @active: whether the pad should be active or not.
623  *
624  * Invoke the default activate push function of a ghost pad.
625  *
626  * Returns: %TRUE if the operation was successful.
627  *
628  * Since: 0.10.35
629  */
630 gboolean
631 gst_ghost_pad_activate_push_default (GstPad * pad, gboolean active)
632 {
633   gboolean ret;
634   GstPad *other;
635
636   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
637
638   GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
639       (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
640
641   /* just activate the internal pad */
642   other = GST_PROXY_PAD_INTERNAL (pad);
643   ret = gst_pad_activate_push (other, active);
644
645   return ret;
646 }
647
648 /**
649  * gst_ghost_pad_activate_pull_default:
650  * @pad: the #GstPad to activate or deactivate.
651  * @active: whether the pad should be active or not.
652  *
653  * Invoke the default activate pull function of a ghost pad.
654  *
655  * Returns: %TRUE if the operation was successful.
656  *
657  * Since: 0.10.35
658  */
659 gboolean
660 gst_ghost_pad_activate_pull_default (GstPad * pad, gboolean active)
661 {
662   gboolean ret;
663   GstPad *other;
664
665   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
666
667   GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
668       GST_DEBUG_PAD_NAME (pad));
669
670   if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
671     /* the ghostpad is SRC and activated in pull mode by its peer, call the
672      * activation function of the internal pad to propagate the activation
673      * upstream */
674     GST_LOG_OBJECT (pad, "pad is src, activate internal");
675     other = GST_PROXY_PAD_INTERNAL (pad);
676     ret = gst_pad_activate_pull (other, active);
677   } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
678     /* We are SINK and activated by the internal pad, propagate activation
679      * upstream because we hold a ref to the upstream peer */
680     GST_LOG_OBJECT (pad, "activating peer");
681     ret = gst_pad_activate_pull (other, active);
682     gst_object_unref (other);
683   } else {
684     /* no peer, we fail */
685     GST_LOG_OBJECT (pad, "pad not src and no peer, failing");
686     ret = FALSE;
687   }
688
689   return ret;
690 }
691
692 /**
693  * gst_ghost_pad_link_default:
694  * @pad: the #GstPad to link.
695  * @peer: the #GstPad peer
696  *
697  * Invoke the default link function of a ghost pad.
698  *
699  * Returns: #GstPadLinkReturn of the operation
700  *
701  * Since: 0.10.35
702  */
703 GstPadLinkReturn
704 gst_ghost_pad_link_default (GstPad * pad, GstPad * peer)
705 {
706   GstPadLinkReturn ret;
707
708   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), GST_PAD_LINK_REFUSED);
709   g_return_val_if_fail (GST_IS_PAD (peer), GST_PAD_LINK_REFUSED);
710
711   GST_DEBUG_OBJECT (pad, "linking ghostpad");
712
713   ret = GST_PAD_LINK_OK;
714   /* if we are a source pad, we should call the peer link function
715    * if the peer has one, see design docs. */
716   if (GST_PAD_IS_SRC (pad)) {
717     if (GST_PAD_LINKFUNC (peer)) {
718       ret = GST_PAD_LINKFUNC (peer) (peer, pad);
719       if (ret != GST_PAD_LINK_OK)
720         GST_DEBUG_OBJECT (pad, "linking failed");
721     }
722   }
723   return ret;
724 }
725
726 /**
727  * gst_ghost_pad_unlink_default:
728  * @pad: the #GstPad to link.
729  *
730  * Invoke the default unlink function of a ghost pad.
731  *
732  * Since: 0.10.35
733  */
734 void
735 gst_ghost_pad_unlink_default (GstPad * pad)
736 {
737   g_return_if_fail (GST_IS_GHOST_PAD (pad));
738
739   GST_DEBUG_OBJECT (pad, "unlinking ghostpad");
740 }
741
742 static void
743 gst_ghost_pad_class_init (GstGhostPadClass * klass)
744 {
745   GObjectClass *gobject_class = (GObjectClass *) klass;
746
747   g_type_class_add_private (klass, sizeof (GstGhostPadPrivate));
748
749   gobject_class->dispose = gst_ghost_pad_dispose;
750
751   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
752   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
753   GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_link_default);
754 }
755
756 static void
757 gst_ghost_pad_init (GstGhostPad * pad)
758 {
759   GST_GHOST_PAD_PRIVATE (pad) = G_TYPE_INSTANCE_GET_PRIVATE (pad,
760       GST_TYPE_GHOST_PAD, GstGhostPadPrivate);
761
762   gst_pad_set_activatepull_function (GST_PAD_CAST (pad),
763       gst_ghost_pad_activate_pull_default);
764   gst_pad_set_activatepush_function (GST_PAD_CAST (pad),
765       gst_ghost_pad_activate_push_default);
766 }
767
768 static void
769 gst_ghost_pad_dispose (GObject * object)
770 {
771   GstPad *pad;
772   GstPad *internal;
773   GstPad *peer;
774
775   pad = GST_PAD (object);
776
777   GST_DEBUG_OBJECT (pad, "dispose");
778
779   gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
780
781   /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
782    * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
783   peer = gst_pad_get_peer (pad);
784   if (peer) {
785     if (GST_PAD_IS_SRC (pad))
786       gst_pad_unlink (pad, peer);
787     else
788       gst_pad_unlink (peer, pad);
789
790     gst_object_unref (peer);
791   }
792
793   GST_OBJECT_LOCK (pad);
794   internal = GST_PROXY_PAD_INTERNAL (pad);
795
796   gst_pad_set_activatepull_function (internal, NULL);
797   gst_pad_set_activatepush_function (internal, NULL);
798
799   /* disposes of the internal pad, since the ghostpad is the only possible object
800    * that has a refcount on the internal pad. */
801   gst_object_unparent (GST_OBJECT_CAST (internal));
802   GST_PROXY_PAD_INTERNAL (pad) = NULL;
803
804   GST_OBJECT_UNLOCK (pad);
805
806   G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
807 }
808
809 /**
810  * gst_ghost_pad_construct:
811  * @gpad: the newly allocated ghost pad
812  *
813  * Finish initialization of a newly allocated ghost pad.
814  *
815  * This function is most useful in language bindings and when subclassing
816  * #GstGhostPad; plugin and application developers normally will not call this
817  * function. Call this function directly after a call to g_object_new
818  * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
819  *
820  * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
821  *
822  * Since: 0.10.22
823  */
824 gboolean
825 gst_ghost_pad_construct (GstGhostPad * gpad)
826 {
827   GstPadDirection dir, otherdir;
828   GstPadTemplate *templ;
829   GstPad *pad, *internal;
830
831   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
832   g_return_val_if_fail (GST_GHOST_PAD_PRIVATE (gpad)->constructed == FALSE,
833       FALSE);
834
835   g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
836
837   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
838
839   pad = GST_PAD (gpad);
840
841   /* Set directional padfunctions for ghostpad */
842   if (dir == GST_PAD_SINK) {
843     gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
844     gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
845   } else {
846     gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
847   }
848
849   /* link/unlink functions */
850   gst_pad_set_link_function (pad, gst_ghost_pad_link_default);
851   gst_pad_set_unlink_function (pad, gst_ghost_pad_unlink_default);
852
853   /* INTERNAL PAD, it always exists and is child of the ghostpad */
854   otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
855   if (templ) {
856     internal =
857         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
858         "direction", otherdir, "template", templ, NULL);
859     /* release ref obtained via g_object_get */
860     gst_object_unref (templ);
861   } else {
862     internal =
863         g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
864         "direction", otherdir, NULL);
865   }
866   GST_PAD_UNSET_FLUSHING (internal);
867
868   /* Set directional padfunctions for internal pad */
869   if (dir == GST_PAD_SRC) {
870     gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
871     gst_pad_set_chain_list_function (internal,
872         gst_proxy_pad_chain_list_default);
873   } else {
874     gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
875   }
876
877   GST_OBJECT_LOCK (pad);
878
879   /* now make the ghostpad a parent of the internal pad */
880   if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
881           GST_OBJECT_CAST (pad)))
882     goto parent_failed;
883
884   /* The ghostpad is the parent of the internal pad and is the only object that
885    * can have a refcount on the internal pad.
886    * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
887    * a refcount of 1.
888    * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
889    * its refcount on the internal pad in the dispose method by un-parenting it.
890    * This is why we don't take extra refcounts in the assignments below
891    */
892   GST_PROXY_PAD_INTERNAL (pad) = internal;
893   GST_PROXY_PAD_INTERNAL (internal) = pad;
894
895   /* special activation functions for the internal pad */
896   gst_pad_set_activatepull_function (internal,
897       gst_ghost_pad_internal_activate_pull_default);
898   gst_pad_set_activatepush_function (internal,
899       gst_ghost_pad_internal_activate_push_default);
900
901   GST_OBJECT_UNLOCK (pad);
902
903   GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
904   return TRUE;
905
906   /* ERRORS */
907 parent_failed:
908   {
909     GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
910         GST_DEBUG_PAD_NAME (internal));
911     g_critical ("Could not set internal pad %s:%s",
912         GST_DEBUG_PAD_NAME (internal));
913     GST_OBJECT_UNLOCK (pad);
914     gst_object_unref (internal);
915     return FALSE;
916   }
917 }
918
919 static GstPad *
920 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
921     GstPadTemplate * templ)
922 {
923   GstGhostPad *ret;
924
925   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
926
927   /* OBJECT CREATION */
928   if (templ) {
929     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
930         "direction", dir, "template", templ, NULL);
931   } else {
932     ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
933         "direction", dir, NULL);
934   }
935
936   if (!gst_ghost_pad_construct (ret))
937     goto construct_failed;
938
939   return GST_PAD_CAST (ret);
940
941 construct_failed:
942   /* already logged */
943   gst_object_unref (ret);
944   return NULL;
945 }
946
947 /**
948  * gst_ghost_pad_new_no_target:
949  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
950  * @dir: the direction of the ghostpad
951  *
952  * Create a new ghostpad without a target with the given direction.
953  * A target can be set on the ghostpad later with the
954  * gst_ghost_pad_set_target() function.
955  *
956  * The created ghostpad will not have a padtemplate.
957  *
958  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
959  */
960 GstPad *
961 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
962 {
963   GstPad *ret;
964
965   g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
966
967   GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
968
969   ret = gst_ghost_pad_new_full (name, dir, NULL);
970
971   return ret;
972 }
973
974 /**
975  * gst_ghost_pad_new:
976  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
977  * @target: (transfer none): the pad to ghost.
978  *
979  * Create a new ghostpad with @target as the target. The direction will be taken
980  * from the target pad. @target must be unlinked.
981  *
982  * Will ref the target.
983  *
984  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
985  */
986 GstPad *
987 gst_ghost_pad_new (const gchar * name, GstPad * target)
988 {
989   GstPad *ret;
990
991   g_return_val_if_fail (GST_IS_PAD (target), NULL);
992   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
993
994   GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
995       GST_DEBUG_PAD_NAME (target));
996
997   if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
998     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
999       goto set_target_failed;
1000
1001   return ret;
1002
1003   /* ERRORS */
1004 set_target_failed:
1005   {
1006     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1007         GST_DEBUG_PAD_NAME (target));
1008     gst_object_unref (ret);
1009     return NULL;
1010   }
1011 }
1012
1013 /**
1014  * gst_ghost_pad_new_from_template:
1015  * @name: (allow-none): the name of the new pad, or NULL to assign a default name.
1016  * @target: (transfer none): the pad to ghost.
1017  * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
1018  *
1019  * Create a new ghostpad with @target as the target. The direction will be taken
1020  * from the target pad. The template used on the ghostpad will be @template.
1021  *
1022  * Will ref the target.
1023  *
1024  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1025  *
1026  * Since: 0.10.10
1027  */
1028
1029 GstPad *
1030 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
1031     GstPadTemplate * templ)
1032 {
1033   GstPad *ret;
1034
1035   g_return_val_if_fail (GST_IS_PAD (target), NULL);
1036   g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
1037   g_return_val_if_fail (templ != NULL, NULL);
1038   g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
1039       GST_PAD_DIRECTION (target), NULL);
1040
1041   GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
1042       GST_DEBUG_PAD_NAME (target), templ);
1043
1044   if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
1045     if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
1046       goto set_target_failed;
1047
1048   return ret;
1049
1050   /* ERRORS */
1051 set_target_failed:
1052   {
1053     GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
1054         GST_DEBUG_PAD_NAME (target));
1055     gst_object_unref (ret);
1056     return NULL;
1057   }
1058 }
1059
1060 /**
1061  * gst_ghost_pad_new_no_target_from_template:
1062  * @name: (allow-none): the name of the new pad, or NULL to assign a default name
1063  * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
1064  *
1065  * Create a new ghostpad based on @templ, without setting a target. The
1066  * direction will be taken from the @templ.
1067  *
1068  * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
1069  *
1070  * Since: 0.10.10
1071  */
1072 GstPad *
1073 gst_ghost_pad_new_no_target_from_template (const gchar * name,
1074     GstPadTemplate * templ)
1075 {
1076   GstPad *ret;
1077
1078   g_return_val_if_fail (templ != NULL, NULL);
1079
1080   ret =
1081       gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
1082
1083   return ret;
1084 }
1085
1086 /**
1087  * gst_ghost_pad_get_target:
1088  * @gpad: the #GstGhostPad
1089  *
1090  * Get the target pad of @gpad. Unref target pad after usage.
1091  *
1092  * Returns: (transfer full): the target #GstPad, can be NULL if the ghostpad
1093  * has no target set. Unref target pad after usage.
1094  */
1095 GstPad *
1096 gst_ghost_pad_get_target (GstGhostPad * gpad)
1097 {
1098   GstPad *ret;
1099
1100   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
1101
1102   ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
1103
1104   GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
1105
1106   return ret;
1107 }
1108
1109 /**
1110  * gst_ghost_pad_set_target:
1111  * @gpad: the #GstGhostPad
1112  * @newtarget: (transfer none) (allow-none): the new pad target
1113  *
1114  * Set the new target of the ghostpad @gpad. Any existing target
1115  * is unlinked and links to the new target are established. if @newtarget is
1116  * NULL the target will be cleared.
1117  *
1118  * Returns: (transfer full): TRUE if the new target could be set. This function
1119  *     can return FALSE when the internal pads could not be linked.
1120  */
1121 gboolean
1122 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
1123 {
1124   GstPad *internal;
1125   GstPad *oldtarget;
1126   GstPadLinkReturn lret;
1127
1128   g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
1129   g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
1130   g_return_val_if_fail (newtarget != GST_PROXY_PAD_INTERNAL (gpad), FALSE);
1131
1132   /* no need for locking, the internal pad's lifecycle is directly linked to the
1133    * ghostpad's */
1134   internal = GST_PROXY_PAD_INTERNAL (gpad);
1135
1136   if (newtarget)
1137     GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
1138   else
1139     GST_DEBUG_OBJECT (gpad, "clearing target");
1140
1141   /* clear old target */
1142   GST_OBJECT_LOCK (gpad);
1143   if ((oldtarget = GST_PROXY_PAD_TARGET (gpad))) {
1144     GST_OBJECT_UNLOCK (gpad);
1145
1146     /* unlink internal pad */
1147     if (GST_PAD_IS_SRC (internal))
1148       gst_pad_unlink (internal, oldtarget);
1149     else
1150       gst_pad_unlink (oldtarget, internal);
1151   } else {
1152     GST_OBJECT_UNLOCK (gpad);
1153   }
1154
1155   if (newtarget) {
1156     /* and link to internal pad without any checks */
1157     GST_DEBUG_OBJECT (gpad, "connecting internal pad to target");
1158
1159     if (GST_PAD_IS_SRC (internal))
1160       lret =
1161           gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
1162     else
1163       lret =
1164           gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
1165
1166     if (lret != GST_PAD_LINK_OK)
1167       goto link_failed;
1168   }
1169
1170   return TRUE;
1171
1172   /* ERRORS */
1173 link_failed:
1174   {
1175     GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%d",
1176         lret);
1177     return FALSE;
1178   }
1179 }