1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Andy Wingo <wingo@pobox.com>
5 * 2006 Edward Hervey <bilboed@bilboed.com>
6 *
7 * gstghostpad.c: Proxy pads
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 /**
26 * SECTION:gstghostpad
27 * @title: GstGhostPad
28 * @short_description: Pseudo link pads
29 * @see_also: #GstPad
30 *
31 * GhostPads are useful when organizing pipelines with #GstBin like elements.
32 * The idea here is to create hierarchical element graphs. The bin element
33 * contains a sub-graph. Now one would like to treat the bin-element like any
34 * other #GstElement. This is where GhostPads come into play. A GhostPad acts as
35 * a proxy for another pad. Thus the bin can have sink and source ghost-pads
36 * that are associated with sink and source pads of the child elements.
37 *
38 * If the target pad is known at creation time, gst_ghost_pad_new() is the
39 * function to use to get a ghost-pad. Otherwise one can use gst_ghost_pad_new_no_target()
40 * to create the ghost-pad and use gst_ghost_pad_set_target() to establish the
41 * association later on.
42 *
43 * Note that GhostPads add overhead to the data processing of a pipeline.
44 */
45
46 #include "gst_private.h"
47 #include "gstinfo.h"
48
49 #include "gstghostpad.h"
50 #include "gst.h"
51
52 #define GST_CAT_DEFAULT GST_CAT_PADS
53
54 #define GST_PROXY_PAD_CAST(obj) ((GstProxyPad *)obj)
55 #define GST_PROXY_PAD_PRIVATE(obj) (GST_PROXY_PAD_CAST (obj)->priv)
56 #define GST_PROXY_PAD_TARGET(pad) (GST_PAD_PEER (GST_PROXY_PAD_INTERNAL (pad)))
57 #define GST_PROXY_PAD_INTERNAL(pad) (GST_PROXY_PAD_PRIVATE (pad)->internal)
58
59 #define GST_PROXY_PAD_ACQUIRE_INTERNAL(pad, internal, retval) \
60 internal = \
61 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD_CAST (pad))); \
62 if (internal == NULL) \
63 return retval;
64
65 #define GST_PROXY_PAD_RELEASE_INTERNAL(internal) gst_object_unref (internal);
66
67 struct _GstProxyPadPrivate
68 {
69 GstPad *internal;
70 };
71
72 G_DEFINE_TYPE_WITH_PRIVATE (GstProxyPad, gst_proxy_pad, GST_TYPE_PAD);
73
74 static GstPad *gst_proxy_pad_get_target (GstPad * pad);
75
76 /**
77 * gst_proxy_pad_iterate_internal_links_default:
78 * @pad: the #GstPad to get the internal links of.
79 * @parent: (allow-none): the parent of @pad or %NULL
80 *
81 * Invoke the default iterate internal links function of the proxy pad.
82 *
83 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
84 * has no parent. Unref each returned pad with gst_object_unref().
85 */
86 GstIterator *
gst_proxy_pad_iterate_internal_links_default(GstPad * pad,GstObject * parent)87 gst_proxy_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
88 {
89 GstIterator *res = NULL;
90 GstPad *internal;
91 GValue v = { 0, };
92
93 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
94
95 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, NULL);
96
97 g_value_init (&v, GST_TYPE_PAD);
98 g_value_take_object (&v, internal);
99 res = gst_iterator_new_single (GST_TYPE_PAD, &v);
100 g_value_unset (&v);
101
102 return res;
103 }
104
105 /**
106 * gst_proxy_pad_chain_default:
107 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
108 * @parent: (allow-none): the parent of @pad or %NULL
109 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
110 * if not.
111 *
112 * Invoke the default chain function of the proxy pad.
113 *
114 * Returns: a #GstFlowReturn from the pad.
115 */
116 GstFlowReturn
gst_proxy_pad_chain_default(GstPad * pad,GstObject * parent,GstBuffer * buffer)117 gst_proxy_pad_chain_default (GstPad * pad, GstObject * parent,
118 GstBuffer * buffer)
119 {
120 GstFlowReturn res;
121 GstPad *internal;
122
123 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
124 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
125
126 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
127 res = gst_pad_push (internal, buffer);
128 GST_PROXY_PAD_RELEASE_INTERNAL (internal);
129
130 return res;
131 }
132
133 /**
134 * gst_proxy_pad_chain_list_default:
135 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
136 * @parent: (allow-none): the parent of @pad or %NULL
137 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
138 * if not.
139 *
140 * Invoke the default chain list function of the proxy pad.
141 *
142 * Returns: a #GstFlowReturn from the pad.
143 */
144 GstFlowReturn
gst_proxy_pad_chain_list_default(GstPad * pad,GstObject * parent,GstBufferList * list)145 gst_proxy_pad_chain_list_default (GstPad * pad, GstObject * parent,
146 GstBufferList * list)
147 {
148 GstFlowReturn res;
149 GstPad *internal;
150
151 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
152 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
153
154 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
155 res = gst_pad_push_list (internal, list);
156 GST_PROXY_PAD_RELEASE_INTERNAL (internal);
157
158 return res;
159 }
160
161 /**
162 * gst_proxy_pad_getrange_default:
163 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
164 * @parent: the parent of @pad
165 * @offset: The start offset of the buffer
166 * @size: The length of the buffer
167 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
168 * returns #GST_FLOW_ERROR if %NULL.
169 *
170 * Invoke the default getrange function of the proxy pad.
171 *
172 * Returns: a #GstFlowReturn from the pad.
173 */
174 GstFlowReturn
gst_proxy_pad_getrange_default(GstPad * pad,GstObject * parent,guint64 offset,guint size,GstBuffer ** buffer)175 gst_proxy_pad_getrange_default (GstPad * pad, GstObject * parent,
176 guint64 offset, guint size, GstBuffer ** buffer)
177 {
178 GstFlowReturn res;
179 GstPad *internal;
180
181 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), GST_FLOW_ERROR);
182 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
183
184 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, internal, GST_FLOW_NOT_LINKED);
185 res = gst_pad_pull_range (internal, offset, size, buffer);
186 GST_PROXY_PAD_RELEASE_INTERNAL (internal);
187
188 return res;
189 }
190
191 static GstPad *
gst_proxy_pad_get_target(GstPad * pad)192 gst_proxy_pad_get_target (GstPad * pad)
193 {
194 GstPad *target;
195
196 GST_OBJECT_LOCK (pad);
197 target = gst_pad_get_peer (GST_PROXY_PAD_INTERNAL (pad));
198 GST_OBJECT_UNLOCK (pad);
199
200 return target;
201 }
202
203 /**
204 * gst_proxy_pad_get_internal:
205 * @pad: the #GstProxyPad
206 *
207 * Get the internal pad of @pad. Unref target pad after usage.
208 *
209 * The internal pad of a #GstGhostPad is the internally used
210 * pad of opposite direction, which is used to link to the target.
211 *
212 * Returns: (transfer full) (nullable): the target #GstProxyPad, can
213 * be %NULL. Unref target pad after usage.
214 */
215 GstProxyPad *
gst_proxy_pad_get_internal(GstProxyPad * pad)216 gst_proxy_pad_get_internal (GstProxyPad * pad)
217 {
218 GstPad *internal;
219
220 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), NULL);
221
222 GST_OBJECT_LOCK (pad);
223 internal = GST_PROXY_PAD_INTERNAL (pad);
224 if (internal)
225 gst_object_ref (internal);
226 GST_OBJECT_UNLOCK (pad);
227
228 return GST_PROXY_PAD_CAST (internal);
229 }
230
231 static void
gst_proxy_pad_class_init(GstProxyPadClass * klass)232 gst_proxy_pad_class_init (GstProxyPadClass * klass)
233 {
234 /* Register common function pointer descriptions */
235 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_iterate_internal_links_default);
236 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_default);
237 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_chain_list_default);
238 GST_DEBUG_REGISTER_FUNCPTR (gst_proxy_pad_getrange_default);
239 }
240
241 static void
gst_proxy_pad_init(GstProxyPad * ppad)242 gst_proxy_pad_init (GstProxyPad * ppad)
243 {
244 GstPad *pad = (GstPad *) ppad;
245
246 GST_PROXY_PAD_PRIVATE (ppad) = gst_proxy_pad_get_instance_private (ppad);
247
248 gst_pad_set_iterate_internal_links_function (pad,
249 gst_proxy_pad_iterate_internal_links_default);
250
251 GST_PAD_SET_PROXY_CAPS (pad);
252 GST_PAD_SET_PROXY_SCHEDULING (pad);
253 GST_PAD_SET_PROXY_ALLOCATION (pad);
254 }
255
256
257 /***********************************************************************
258 * Ghost pads, implemented as a pair of proxy pads (sort of)
259 */
260
261
262 #define GST_GHOST_PAD_PRIVATE(obj) (GST_GHOST_PAD_CAST (obj)->priv)
263
264 struct _GstGhostPadPrivate
265 {
266 /* with PROXY_LOCK */
267 gboolean constructed;
268 };
269
270 G_DEFINE_TYPE_WITH_PRIVATE (GstGhostPad, gst_ghost_pad, GST_TYPE_PROXY_PAD);
271
272 static void gst_ghost_pad_dispose (GObject * object);
273
274 static gboolean
gst_ghost_pad_internal_activate_push_default(GstPad * pad,GstObject * parent,gboolean active)275 gst_ghost_pad_internal_activate_push_default (GstPad * pad, GstObject * parent,
276 gboolean active)
277 {
278 gboolean ret;
279 GstPad *other;
280
281 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, we're ok",
282 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
283
284 /* in both cases (SRC and SINK) we activate just the internal pad. The targets
285 * will be activated later (or already in case of a ghost sinkpad). */
286 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
287 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
288 GST_PROXY_PAD_RELEASE_INTERNAL (other);
289
290 return ret;
291 }
292
293 static gboolean
gst_ghost_pad_internal_activate_pull_default(GstPad * pad,GstObject * parent,gboolean active)294 gst_ghost_pad_internal_activate_pull_default (GstPad * pad, GstObject * parent,
295 gboolean active)
296 {
297 gboolean ret;
298 GstPad *other;
299
300 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
301 GST_DEBUG_PAD_NAME (pad));
302
303 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
304 /* we are activated in pull mode by our peer element, which is a sinkpad
305 * that wants to operate in pull mode. This activation has to propagate
306 * upstream through the pipeline. We call the internal activation function,
307 * which will trigger gst_ghost_pad_activate_pull_default, which propagates even
308 * further upstream */
309 GST_LOG_OBJECT (pad, "pad is src, activate internal");
310 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
311 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
312 GST_PROXY_PAD_RELEASE_INTERNAL (other);
313 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
314 /* We are SINK, the ghostpad is SRC, we propagate the activation upstream
315 * since we hold a pointer to the upstream peer. */
316 GST_LOG_OBJECT (pad, "activating peer");
317 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
318 gst_object_unref (other);
319 } else if (active) {
320 /* this is failure, we can't activate pull if there is no peer */
321 GST_LOG_OBJECT (pad, "not src and no peer, failing");
322 ret = FALSE;
323 } else {
324 GST_LOG_OBJECT (pad, "deactivating pull, with no peer - allowing");
325 ret = TRUE;
326 }
327
328 return ret;
329 }
330
331 /**
332 * gst_ghost_pad_internal_activate_mode_default:
333 * @pad: the #GstPad to activate or deactivate.
334 * @parent: (allow-none): the parent of @pad or %NULL
335 * @mode: the requested activation mode
336 * @active: whether the pad should be active or not.
337 *
338 * Invoke the default activate mode function of a proxy pad that is
339 * owned by a ghost pad.
340 *
341 * Returns: %TRUE if the operation was successful.
342 */
343 gboolean
gst_ghost_pad_internal_activate_mode_default(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)344 gst_ghost_pad_internal_activate_mode_default (GstPad * pad, GstObject * parent,
345 GstPadMode mode, gboolean active)
346 {
347 gboolean res;
348
349 g_return_val_if_fail (GST_IS_PROXY_PAD (pad), FALSE);
350
351 switch (mode) {
352 case GST_PAD_MODE_PULL:
353 res = gst_ghost_pad_internal_activate_pull_default (pad, parent, active);
354 break;
355 case GST_PAD_MODE_PUSH:
356 res = gst_ghost_pad_internal_activate_push_default (pad, parent, active);
357 break;
358 default:
359 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
360 res = FALSE;
361 break;
362 }
363 return res;
364 }
365
366 static gboolean
gst_ghost_pad_activate_push_default(GstPad * pad,GstObject * parent,gboolean active)367 gst_ghost_pad_activate_push_default (GstPad * pad, GstObject * parent,
368 gboolean active)
369 {
370 gboolean ret;
371 GstPad *other;
372
373 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
374
375 GST_LOG_OBJECT (pad, "%sactivate push on %s:%s, proxy internal",
376 (active ? "" : "de"), GST_DEBUG_PAD_NAME (pad));
377
378 /* just activate the internal pad */
379 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
380 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PUSH, active);
381 GST_PROXY_PAD_RELEASE_INTERNAL (other);
382
383 return ret;
384 }
385
386 static gboolean
gst_ghost_pad_activate_pull_default(GstPad * pad,GstObject * parent,gboolean active)387 gst_ghost_pad_activate_pull_default (GstPad * pad, GstObject * parent,
388 gboolean active)
389 {
390 gboolean ret;
391 GstPad *other;
392
393 GST_LOG_OBJECT (pad, "%sactivate pull on %s:%s", (active ? "" : "de"),
394 GST_DEBUG_PAD_NAME (pad));
395
396 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC) {
397 /* the ghostpad is SRC and activated in pull mode by its peer, call the
398 * activation function of the internal pad to propagate the activation
399 * upstream */
400 GST_LOG_OBJECT (pad, "pad is src, activate internal");
401 GST_PROXY_PAD_ACQUIRE_INTERNAL (pad, other, FALSE);
402 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
403 GST_PROXY_PAD_RELEASE_INTERNAL (other);
404 } else if (G_LIKELY ((other = gst_pad_get_peer (pad)))) {
405 /* We are SINK and activated by the internal pad, propagate activation
406 * upstream because we hold a ref to the upstream peer */
407 GST_LOG_OBJECT (pad, "activating peer");
408 ret = gst_pad_activate_mode (other, GST_PAD_MODE_PULL, active);
409 gst_object_unref (other);
410 } else if (active) {
411 /* this is failure, we can't activate pull if there is no peer */
412 GST_LOG_OBJECT (pad, "not src and no peer, failing");
413 ret = FALSE;
414 } else {
415 GST_LOG_OBJECT (pad, "deactivating pull, with no peer - allowing");
416 ret = TRUE;
417 }
418
419 return ret;
420 }
421
422 /**
423 * gst_ghost_pad_activate_mode_default:
424 * @pad: the #GstPad to activate or deactivate.
425 * @parent: (allow-none): the parent of @pad or %NULL
426 * @mode: the requested activation mode
427 * @active: whether the pad should be active or not.
428 *
429 * Invoke the default activate mode function of a ghost pad.
430 *
431 * Returns: %TRUE if the operation was successful.
432 */
433 gboolean
gst_ghost_pad_activate_mode_default(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)434 gst_ghost_pad_activate_mode_default (GstPad * pad, GstObject * parent,
435 GstPadMode mode, gboolean active)
436 {
437 gboolean res;
438
439 g_return_val_if_fail (GST_IS_GHOST_PAD (pad), FALSE);
440
441 switch (mode) {
442 case GST_PAD_MODE_PULL:
443 res = gst_ghost_pad_activate_pull_default (pad, parent, active);
444 break;
445 case GST_PAD_MODE_PUSH:
446 res = gst_ghost_pad_activate_push_default (pad, parent, active);
447 break;
448 default:
449 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
450 res = FALSE;
451 break;
452 }
453 return res;
454 }
455
456 static void
gst_ghost_pad_class_init(GstGhostPadClass * klass)457 gst_ghost_pad_class_init (GstGhostPadClass * klass)
458 {
459 GObjectClass *gobject_class = (GObjectClass *) klass;
460
461 gobject_class->dispose = gst_ghost_pad_dispose;
462
463 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_pull_default);
464 GST_DEBUG_REGISTER_FUNCPTR (gst_ghost_pad_activate_push_default);
465 }
466
467 static void
gst_ghost_pad_init(GstGhostPad * pad)468 gst_ghost_pad_init (GstGhostPad * pad)
469 {
470 GST_GHOST_PAD_PRIVATE (pad) = gst_ghost_pad_get_instance_private (pad);
471
472 gst_pad_set_activatemode_function (GST_PAD_CAST (pad),
473 gst_ghost_pad_activate_mode_default);
474 }
475
476 static void
gst_ghost_pad_dispose(GObject * object)477 gst_ghost_pad_dispose (GObject * object)
478 {
479 GstPad *pad;
480 GstPad *internal;
481 GstPad *peer;
482
483 pad = GST_PAD (object);
484
485 GST_DEBUG_OBJECT (pad, "dispose");
486
487 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
488
489 /* Unlink here so that gst_pad_dispose doesn't. That would lead to a call to
490 * gst_ghost_pad_unlink_default when the ghost pad is in an inconsistent state */
491 peer = gst_pad_get_peer (pad);
492 if (peer) {
493 if (GST_PAD_IS_SRC (pad))
494 gst_pad_unlink (pad, peer);
495 else
496 gst_pad_unlink (peer, pad);
497
498 gst_object_unref (peer);
499 }
500
501 GST_OBJECT_LOCK (pad);
502 internal = GST_PROXY_PAD_INTERNAL (pad);
503 if (internal) {
504 gst_pad_set_activatemode_function (internal, NULL);
505
506 GST_PROXY_PAD_INTERNAL (pad) = NULL;
507 GST_PROXY_PAD_INTERNAL (internal) = NULL;
508
509 /* disposes of the internal pad, since the ghostpad is the only possible object
510 * that has a refcount on the internal pad. */
511 gst_object_unparent (GST_OBJECT_CAST (internal));
512 }
513
514 GST_OBJECT_UNLOCK (pad);
515
516 G_OBJECT_CLASS (gst_ghost_pad_parent_class)->dispose (object);
517 }
518
519 /**
520 * gst_ghost_pad_construct:
521 * @gpad: the newly allocated ghost pad
522 *
523 * Finish initialization of a newly allocated ghost pad.
524 *
525 * This function is most useful in language bindings and when subclassing
526 * #GstGhostPad; plugin and application developers normally will not call this
527 * function. Call this function directly after a call to g_object_new
528 * (GST_TYPE_GHOST_PAD, "direction", @dir, ..., NULL).
529 *
530 * Returns: %TRUE if the construction succeeds, %FALSE otherwise.
531 */
532 gboolean
gst_ghost_pad_construct(GstGhostPad * gpad)533 gst_ghost_pad_construct (GstGhostPad * gpad)
534 {
535 GstPadDirection dir, otherdir;
536 GstPadTemplate *templ;
537 GstPad *pad, *internal;
538
539 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
540 g_return_val_if_fail (!GST_GHOST_PAD_PRIVATE (gpad)->constructed, FALSE);
541
542 g_object_get (gpad, "direction", &dir, "template", &templ, NULL);
543
544 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, FALSE);
545
546 pad = GST_PAD (gpad);
547
548 /* Set directional padfunctions for ghostpad */
549 if (dir == GST_PAD_SINK) {
550 gst_pad_set_chain_function (pad, gst_proxy_pad_chain_default);
551 gst_pad_set_chain_list_function (pad, gst_proxy_pad_chain_list_default);
552 } else {
553 gst_pad_set_getrange_function (pad, gst_proxy_pad_getrange_default);
554 }
555
556 /* INTERNAL PAD, it always exists and is child of the ghostpad */
557 otherdir = (dir == GST_PAD_SRC) ? GST_PAD_SINK : GST_PAD_SRC;
558 if (templ) {
559 internal =
560 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
561 "direction", otherdir, "template", templ, NULL);
562 /* release ref obtained via g_object_get */
563 gst_object_unref (templ);
564 } else {
565 internal =
566 g_object_new (GST_TYPE_PROXY_PAD, "name", NULL,
567 "direction", otherdir, NULL);
568 }
569 GST_PAD_UNSET_FLUSHING (internal);
570
571 /* Set directional padfunctions for internal pad */
572 if (dir == GST_PAD_SRC) {
573 gst_pad_set_chain_function (internal, gst_proxy_pad_chain_default);
574 gst_pad_set_chain_list_function (internal,
575 gst_proxy_pad_chain_list_default);
576 } else {
577 gst_pad_set_getrange_function (internal, gst_proxy_pad_getrange_default);
578 }
579
580 GST_OBJECT_LOCK (pad);
581
582 /* now make the ghostpad a parent of the internal pad */
583 if (!gst_object_set_parent (GST_OBJECT_CAST (internal),
584 GST_OBJECT_CAST (pad)))
585 goto parent_failed;
586
587 /* The ghostpad is the parent of the internal pad and is the only object that
588 * can have a refcount on the internal pad.
589 * At this point, the GstGhostPad has a refcount of 1, and the internal pad has
590 * a refcount of 1.
591 * When the refcount of the GstGhostPad drops to 0, the ghostpad will dispose
592 * its refcount on the internal pad in the dispose method by un-parenting it.
593 * This is why we don't take extra refcounts in the assignments below
594 */
595 GST_PROXY_PAD_INTERNAL (pad) = internal;
596 GST_PROXY_PAD_INTERNAL (internal) = pad;
597
598 /* special activation functions for the internal pad */
599 gst_pad_set_activatemode_function (internal,
600 gst_ghost_pad_internal_activate_mode_default);
601
602 GST_OBJECT_UNLOCK (pad);
603
604 GST_GHOST_PAD_PRIVATE (gpad)->constructed = TRUE;
605 return TRUE;
606
607 /* ERRORS */
608 parent_failed:
609 {
610 GST_WARNING_OBJECT (gpad, "Could not set internal pad %s:%s",
611 GST_DEBUG_PAD_NAME (internal));
612 g_critical ("Could not set internal pad %s:%s",
613 GST_DEBUG_PAD_NAME (internal));
614 GST_OBJECT_UNLOCK (pad);
615 return FALSE;
616 }
617 }
618
619 static GstPad *
gst_ghost_pad_new_full(const gchar * name,GstPadDirection dir,GstPadTemplate * templ)620 gst_ghost_pad_new_full (const gchar * name, GstPadDirection dir,
621 GstPadTemplate * templ)
622 {
623 GstGhostPad *ret;
624 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
625
626 /* OBJECT CREATION */
627 if (templ) {
628 GType pad_type =
629 GST_PAD_TEMPLATE_GTYPE (templ) ==
630 G_TYPE_NONE ? GST_TYPE_GHOST_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
631
632 g_return_val_if_fail (g_type_is_a (pad_type, GST_TYPE_GHOST_PAD), NULL);
633
634 ret = g_object_new (pad_type, "name", name,
635 "direction", dir, "template", templ, NULL);
636 } else {
637 ret = g_object_new (GST_TYPE_GHOST_PAD, "name", name,
638 "direction", dir, NULL);
639 }
640
641 if (!gst_ghost_pad_construct (ret))
642 goto construct_failed;
643
644 return GST_PAD_CAST (ret);
645
646 construct_failed:
647 /* already logged */
648 gst_object_unref (ret);
649 return NULL;
650 }
651
652 /**
653 * gst_ghost_pad_new_no_target:
654 * @name: (allow-none): the name of the new pad, or %NULL to assign a default name.
655 * @dir: the direction of the ghostpad
656 *
657 * Create a new ghostpad without a target with the given direction.
658 * A target can be set on the ghostpad later with the
659 * gst_ghost_pad_set_target() function.
660 *
661 * The created ghostpad will not have a padtemplate.
662 *
663 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
664 * case of an error.
665 */
666 GstPad *
gst_ghost_pad_new_no_target(const gchar * name,GstPadDirection dir)667 gst_ghost_pad_new_no_target (const gchar * name, GstPadDirection dir)
668 {
669 GstPad *ret;
670
671 g_return_val_if_fail (dir != GST_PAD_UNKNOWN, NULL);
672
673 GST_LOG ("name:%s, direction:%d", GST_STR_NULL (name), dir);
674
675 ret = gst_ghost_pad_new_full (name, dir, NULL);
676
677 return ret;
678 }
679
680 /**
681 * gst_ghost_pad_new:
682 * @name: (allow-none): the name of the new pad, or %NULL to assign a default name
683 * @target: (transfer none): the pad to ghost.
684 *
685 * Create a new ghostpad with @target as the target. The direction will be taken
686 * from the target pad. @target must be unlinked.
687 *
688 * Will ref the target.
689 *
690 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
691 * case of an error.
692 */
693 GstPad *
gst_ghost_pad_new(const gchar * name,GstPad * target)694 gst_ghost_pad_new (const gchar * name, GstPad * target)
695 {
696 GstPad *ret;
697
698 g_return_val_if_fail (GST_IS_PAD (target), NULL);
699 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
700
701 GST_LOG ("name:%s, target:%s:%s", GST_STR_NULL (name),
702 GST_DEBUG_PAD_NAME (target));
703
704 if ((ret = gst_ghost_pad_new_no_target (name, GST_PAD_DIRECTION (target))))
705 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
706 goto set_target_failed;
707
708 return ret;
709
710 /* ERRORS */
711 set_target_failed:
712 {
713 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
714 GST_DEBUG_PAD_NAME (target));
715 gst_object_unref (ret);
716 return NULL;
717 }
718 }
719
720 /**
721 * gst_ghost_pad_new_from_template:
722 * @name: (allow-none): the name of the new pad, or %NULL to assign a default name.
723 * @target: (transfer none): the pad to ghost.
724 * @templ: (transfer none): the #GstPadTemplate to use on the ghostpad.
725 *
726 * Create a new ghostpad with @target as the target. The direction will be taken
727 * from the target pad. The template used on the ghostpad will be @template.
728 *
729 * Will ref the target.
730 *
731 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
732 * case of an error.
733 */
734
735 GstPad *
gst_ghost_pad_new_from_template(const gchar * name,GstPad * target,GstPadTemplate * templ)736 gst_ghost_pad_new_from_template (const gchar * name, GstPad * target,
737 GstPadTemplate * templ)
738 {
739 GstPad *ret;
740
741 g_return_val_if_fail (GST_IS_PAD (target), NULL);
742 g_return_val_if_fail (!gst_pad_is_linked (target), NULL);
743 g_return_val_if_fail (templ != NULL, NULL);
744 g_return_val_if_fail (GST_PAD_TEMPLATE_DIRECTION (templ) ==
745 GST_PAD_DIRECTION (target), NULL);
746
747 GST_LOG ("name:%s, target:%s:%s, templ:%p", GST_STR_NULL (name),
748 GST_DEBUG_PAD_NAME (target), templ);
749
750 if ((ret = gst_ghost_pad_new_full (name, GST_PAD_DIRECTION (target), templ)))
751 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ret), target))
752 goto set_target_failed;
753
754 return ret;
755
756 /* ERRORS */
757 set_target_failed:
758 {
759 GST_WARNING_OBJECT (ret, "failed to set target %s:%s",
760 GST_DEBUG_PAD_NAME (target));
761 gst_object_unref (ret);
762 return NULL;
763 }
764 }
765
766 /**
767 * gst_ghost_pad_new_no_target_from_template:
768 * @name: (allow-none): the name of the new pad, or %NULL to assign a default name
769 * @templ: (transfer none): the #GstPadTemplate to create the ghostpad from.
770 *
771 * Create a new ghostpad based on @templ, without setting a target. The
772 * direction will be taken from the @templ.
773 *
774 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
775 * case of an error.
776 */
777 GstPad *
gst_ghost_pad_new_no_target_from_template(const gchar * name,GstPadTemplate * templ)778 gst_ghost_pad_new_no_target_from_template (const gchar * name,
779 GstPadTemplate * templ)
780 {
781 GstPad *ret;
782
783 g_return_val_if_fail (templ != NULL, NULL);
784
785 ret =
786 gst_ghost_pad_new_full (name, GST_PAD_TEMPLATE_DIRECTION (templ), templ);
787
788 return ret;
789 }
790
791 /**
792 * gst_ghost_pad_get_target:
793 * @gpad: the #GstGhostPad
794 *
795 * Get the target pad of @gpad. Unref target pad after usage.
796 *
797 * Returns: (transfer full) (nullable): the target #GstPad, can be
798 * %NULL if the ghostpad has no target set. Unref target pad after
799 * usage.
800 */
801 GstPad *
gst_ghost_pad_get_target(GstGhostPad * gpad)802 gst_ghost_pad_get_target (GstGhostPad * gpad)
803 {
804 GstPad *ret;
805
806 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), NULL);
807
808 ret = gst_proxy_pad_get_target (GST_PAD_CAST (gpad));
809
810 GST_DEBUG_OBJECT (gpad, "get target %s:%s", GST_DEBUG_PAD_NAME (ret));
811
812 return ret;
813 }
814
815 /**
816 * gst_ghost_pad_set_target:
817 * @gpad: the #GstGhostPad
818 * @newtarget: (transfer none) (allow-none): the new pad target
819 *
820 * Set the new target of the ghostpad @gpad. Any existing target
821 * is unlinked and links to the new target are established. if @newtarget is
822 * %NULL the target will be cleared.
823 *
824 * Returns: %TRUE if the new target could be set. This function
825 * can return %FALSE when the internal pads could not be linked.
826 */
827 gboolean
gst_ghost_pad_set_target(GstGhostPad * gpad,GstPad * newtarget)828 gst_ghost_pad_set_target (GstGhostPad * gpad, GstPad * newtarget)
829 {
830 GstPad *internal;
831 GstPad *oldtarget;
832 GstPadLinkReturn lret;
833
834 g_return_val_if_fail (GST_IS_GHOST_PAD (gpad), FALSE);
835 g_return_val_if_fail (GST_PAD_CAST (gpad) != newtarget, FALSE);
836
837 GST_OBJECT_LOCK (gpad);
838 internal = GST_PROXY_PAD_INTERNAL (gpad);
839
840 if (newtarget == internal) {
841 GST_OBJECT_UNLOCK (gpad);
842 GST_WARNING_OBJECT (gpad, "Target has already been set to %s:%s",
843 GST_DEBUG_PAD_NAME (newtarget));
844 return TRUE;
845 }
846
847 if (newtarget)
848 GST_DEBUG_OBJECT (gpad, "set target %s:%s", GST_DEBUG_PAD_NAME (newtarget));
849 else
850 GST_DEBUG_OBJECT (gpad, "clearing target");
851
852 /* clear old target */
853 if ((oldtarget = gst_pad_get_peer (internal))) {
854 GST_OBJECT_UNLOCK (gpad);
855
856 /* unlink internal pad */
857 if (GST_PAD_IS_SRC (internal))
858 gst_pad_unlink (internal, oldtarget);
859 else
860 gst_pad_unlink (oldtarget, internal);
861
862 gst_object_unref (oldtarget);
863 } else {
864 GST_OBJECT_UNLOCK (gpad);
865 }
866
867 if (newtarget) {
868 /* and link to internal pad without any checks */
869 GST_DEBUG_OBJECT (gpad, "connecting internal pad to target %"
870 GST_PTR_FORMAT, newtarget);
871
872 if (GST_PAD_IS_SRC (internal))
873 lret =
874 gst_pad_link_full (internal, newtarget, GST_PAD_LINK_CHECK_NOTHING);
875 else
876 lret =
877 gst_pad_link_full (newtarget, internal, GST_PAD_LINK_CHECK_NOTHING);
878
879 if (lret != GST_PAD_LINK_OK)
880 goto link_failed;
881 }
882
883 return TRUE;
884
885 /* ERRORS */
886 link_failed:
887 {
888 GST_WARNING_OBJECT (gpad, "could not link internal and target, reason:%s",
889 gst_pad_link_get_name (lret));
890 return FALSE;
891 }
892 }
893