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