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