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