2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstghostpad.c: Unit test for GstGhostPad
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <gst/check/gstcheck.h>
24 /* test if removing a bin also cleans up the ghostpads
26 GST_START_TEST (test_remove1)
28 GstElement *b1, *b2, *src, *sink;
29 GstPad *srcpad, *sinkpad;
32 b1 = gst_element_factory_make ("pipeline", NULL);
33 b2 = gst_element_factory_make ("bin", NULL);
34 src = gst_element_factory_make ("fakesrc", NULL);
35 sink = gst_element_factory_make ("fakesink", NULL);
36 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 1);
37 ASSERT_OBJECT_REFCOUNT (b2, "bin", 1);
39 fail_unless (gst_bin_add (GST_BIN (b2), sink));
40 fail_unless (gst_bin_add (GST_BIN (b1), src));
41 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 1);
42 ASSERT_OBJECT_REFCOUNT (b2, "bin", 1);
43 fail_unless (gst_bin_add (GST_BIN (b1), b2));
44 /* adding the bin creates a clock provide message with a ref to pipeline */
45 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 2);
46 ASSERT_OBJECT_REFCOUNT (b2, "bin", 1);
48 sinkpad = gst_element_get_pad (sink, "sink");
49 gst_element_add_pad (b2, gst_ghost_pad_new ("sink", sinkpad));
50 gst_object_unref (sinkpad);
52 srcpad = gst_element_get_pad (src, "src");
53 /* get the ghostpad */
54 sinkpad = gst_element_get_pad (b2, "sink");
56 ret = gst_pad_link (srcpad, sinkpad);
57 fail_unless (ret == GST_PAD_LINK_OK);
58 gst_object_unref (srcpad);
59 gst_object_unref (sinkpad);
61 /* now remove the bin with the ghostpad, b2 is disposed now. */
62 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 2);
63 ASSERT_OBJECT_REFCOUNT (b2, "bin", 1);
64 gst_bin_remove (GST_BIN (b1), b2);
66 srcpad = gst_element_get_pad (src, "src");
67 /* pad cannot be linked now */
68 fail_if (gst_pad_is_linked (srcpad));
69 gst_object_unref (srcpad);
71 /* flush the message, dropping the b1 refcount to 1 */
72 gst_element_set_state (b1, GST_STATE_READY);
73 gst_element_set_state (b1, GST_STATE_NULL);
74 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 1);
75 gst_object_unref (b1);
80 /* test if removing a bin also cleans up the ghostpads
82 GST_START_TEST (test_remove2)
84 GstElement *b1, *b2, *src, *sink;
85 GstPad *srcpad, *sinkpad;
88 b1 = gst_element_factory_make ("pipeline", NULL);
89 b2 = gst_element_factory_make ("bin", NULL);
90 src = gst_element_factory_make ("fakesrc", NULL);
91 sink = gst_element_factory_make ("fakesink", NULL);
92 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
94 fail_unless (gst_bin_add (GST_BIN (b2), sink));
95 fail_unless (gst_bin_add (GST_BIN (b1), src));
96 fail_unless (gst_bin_add (GST_BIN (b1), b2));
97 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
99 sinkpad = gst_element_get_pad (sink, "sink");
100 gst_element_add_pad (b2, gst_ghost_pad_new ("sink", sinkpad));
101 gst_object_unref (sinkpad);
103 srcpad = gst_element_get_pad (src, "src");
104 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 2); /* since we got one */
105 /* get the ghostpad */
106 sinkpad = gst_element_get_pad (b2, "sink");
107 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2); /* since we got one */
109 GST_DEBUG ("linking srcpad and sinkpad");
110 ret = gst_pad_link (srcpad, sinkpad);
111 GST_DEBUG ("linked srcpad and sinkpad");
112 fail_unless (ret == GST_PAD_LINK_OK);
113 /* the linking causes a proxypad to be created for srcpad,
114 * to which sinkpad gets linked. This proxypad has a ref to srcpad */
115 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 3);
116 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
117 gst_object_unref (srcpad);
118 gst_object_unref (sinkpad);
120 /* now remove the sink from the bin */
121 gst_bin_remove (GST_BIN (b2), sink);
123 srcpad = gst_element_get_pad (src, "src");
124 /* pad is still linked to ghostpad */
125 fail_if (!gst_pad_is_linked (srcpad));
126 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
127 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 3);
128 gst_object_unref (srcpad);
129 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
132 /* now unlink the pads */
133 gst_pad_unlink (srcpad, sinkpad);
134 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1); /* proxy has dropped ref */
135 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
137 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
138 ASSERT_OBJECT_REFCOUNT (b2, "bin", 1);
139 /* remove b2 from b1 */
140 gst_bin_remove (GST_BIN (b1), b2);
142 /* flush the message, dropping the b1 refcount to 1 */
143 gst_element_set_state (b1, GST_STATE_READY);
144 gst_element_set_state (b1, GST_STATE_NULL);
145 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 1);
146 gst_object_unref (b1);
152 /* test if a ghost pad without a target can be linked
153 * It can't because it has incompatible caps...
155 GST_START_TEST (test_ghost_pad_notarget)
157 GstElement *b1, *b2, *sink;
158 GstPad *srcpad, *sinkpad;
159 GstPadLinkReturn ret;
161 b1 = gst_element_factory_make ("pipeline", NULL);
162 b2 = gst_element_factory_make ("bin", NULL);
163 sink = gst_element_factory_make ("fakesink", NULL);
165 fail_unless (gst_bin_add (GST_BIN (b1), sink));
166 fail_unless (gst_bin_add (GST_BIN (b1), b2));
168 srcpad = gst_ghost_pad_new_no_target ("src", GST_PAD_SRC);
169 fail_unless (srcpad != NULL);
170 sinkpad = gst_element_get_pad (sink, "sink");
171 fail_unless (sinkpad != NULL);
173 ret = gst_pad_link (srcpad, sinkpad);
174 fail_unless (ret == GST_PAD_LINK_OK);
180 /* test if linking fails over different bins using a pipeline
183 * fakesrc num_buffers=10 ! ( fakesink )
186 GST_START_TEST (test_link)
188 GstElement *b1, *b2, *src, *sink;
189 GstPad *srcpad, *sinkpad, *gpad;
190 GstPadLinkReturn ret;
192 b1 = gst_element_factory_make ("pipeline", NULL);
193 b2 = gst_element_factory_make ("bin", NULL);
194 src = gst_element_factory_make ("fakesrc", NULL);
195 sink = gst_element_factory_make ("fakesink", NULL);
197 fail_unless (gst_bin_add (GST_BIN (b2), sink));
198 fail_unless (gst_bin_add (GST_BIN (b1), src));
199 fail_unless (gst_bin_add (GST_BIN (b1), b2));
201 srcpad = gst_element_get_pad (src, "src");
202 fail_unless (srcpad != NULL);
203 sinkpad = gst_element_get_pad (sink, "sink");
204 fail_unless (sinkpad != NULL);
206 /* linking in different hierarchies should fail */
207 ret = gst_pad_link (srcpad, sinkpad);
208 fail_unless (ret == GST_PAD_LINK_WRONG_HIERARCHY);
210 /* now setup a ghostpad */
211 gpad = gst_ghost_pad_new ("sink", sinkpad);
212 gst_object_unref (sinkpad);
213 /* need to ref as _add_pad takes ownership */
214 gst_object_ref (gpad);
215 gst_element_add_pad (b2, gpad);
217 /* our new sinkpad */
220 /* and linking should work now */
221 ret = gst_pad_link (srcpad, sinkpad);
222 fail_unless (ret == GST_PAD_LINK_OK);
224 /* flush the message, dropping the b1 refcount to 1 */
225 gst_element_set_state (b1, GST_STATE_READY);
226 gst_element_set_state (b1, GST_STATE_NULL);
227 ASSERT_OBJECT_REFCOUNT (b1, "pipeline", 1);
232 /* test if ghostpads are created automagically when using
233 * gst_element_link_pads.
235 * fakesrc num_buffers=10 ! ( identity ) ! fakesink
237 GST_START_TEST (test_ghost_pads)
239 GstElement *b1, *b2, *src, *i1, *sink;
240 GstPad *gsink, *gsrc, *gisrc, *gisink, *isink, *isrc, *fsrc, *fsink;
241 GstStateChangeReturn ret;
243 b1 = gst_element_factory_make ("pipeline", NULL);
244 b2 = gst_element_factory_make ("bin", NULL);
245 src = gst_element_factory_make ("fakesrc", NULL);
246 g_object_set (src, "num-buffers", (int) 10, NULL);
247 i1 = gst_element_factory_make ("identity", NULL);
248 sink = gst_element_factory_make ("fakesink", NULL);
250 fail_unless (gst_bin_add (GST_BIN (b2), i1));
251 fail_unless (gst_bin_add (GST_BIN (b1), src));
252 fail_unless (gst_bin_add (GST_BIN (b1), b2));
253 fail_unless (gst_bin_add (GST_BIN (b1), sink));
254 fail_unless (gst_element_link_pads (src, NULL, i1, NULL));
255 fail_unless (gst_element_link_pads (i1, NULL, sink, NULL));
256 GST_OBJECT_LOCK (b2);
257 fail_unless (b2->numsinkpads == 1);
258 fail_unless (GST_IS_GHOST_PAD (b2->sinkpads->data));
259 fail_unless (b2->numsrcpads == 1);
260 fail_unless (GST_IS_GHOST_PAD (b2->srcpads->data));
261 GST_OBJECT_UNLOCK (b2);
263 fsrc = gst_element_get_pad (src, "src");
264 fail_unless (fsrc != NULL);
265 gsink = GST_PAD (gst_object_ref (b2->sinkpads->data));
266 fail_unless (gsink != NULL);
267 gsrc = GST_PAD (gst_object_ref (b2->srcpads->data));
268 fail_unless (gsrc != NULL);
269 fsink = gst_element_get_pad (sink, "sink");
270 fail_unless (fsink != NULL);
272 isink = gst_element_get_pad (i1, "sink");
273 fail_unless (isink != NULL);
274 isrc = gst_element_get_pad (i1, "src");
275 fail_unless (isrc != NULL);
276 gisrc = gst_pad_get_peer (isink);
277 fail_unless (gisrc != NULL);
278 gisink = gst_pad_get_peer (isrc);
279 fail_unless (gisink != NULL);
281 /* all objects above have one refcount owned by us as well */
283 ASSERT_OBJECT_REFCOUNT (fsrc, "fsrc", 3); /* parent and gisrc */
284 ASSERT_OBJECT_REFCOUNT (gsink, "gsink", 2); /* parent */
285 ASSERT_OBJECT_REFCOUNT (gsrc, "gsrc", 2); /* parent */
286 ASSERT_OBJECT_REFCOUNT (fsink, "fsink", 3); /* parent and gisink */
288 ASSERT_OBJECT_REFCOUNT (gisrc, "gisrc", 2); /* parent */
289 ASSERT_OBJECT_REFCOUNT (isink, "isink", 3); /* parent and gsink */
290 ASSERT_OBJECT_REFCOUNT (gisink, "gisink", 2); /* parent */
291 ASSERT_OBJECT_REFCOUNT (isrc, "isrc", 3); /* parent and gsrc */
293 ret = gst_element_set_state (b1, GST_STATE_PLAYING);
294 ret = gst_element_get_state (b1, NULL, NULL, GST_CLOCK_TIME_NONE);
295 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
297 ret = gst_element_set_state (b1, GST_STATE_NULL);
298 ret = gst_element_get_state (b1, NULL, NULL, GST_CLOCK_TIME_NONE);
299 fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
301 gst_object_unref (b1);
302 /* unreffing the bin will unref all elements, which will unlink and unparent
305 /* wait for thread to settle down */
306 while (GST_OBJECT_REFCOUNT_VALUE (fsrc) > 2)
309 ASSERT_OBJECT_REFCOUNT (fsrc, "fsrc", 2); /* gisrc */
310 ASSERT_OBJECT_REFCOUNT (gsink, "gsink", 1);
311 ASSERT_OBJECT_REFCOUNT (gsrc, "gsink", 1);
312 ASSERT_OBJECT_REFCOUNT (fsink, "fsink", 2); /* gisink */
314 ASSERT_OBJECT_REFCOUNT (gisrc, "gisrc", 1); /* gsink */
315 ASSERT_OBJECT_REFCOUNT (isink, "isink", 2); /* gsink */
316 ASSERT_OBJECT_REFCOUNT (gisink, "gisink", 1); /* gsrc */
317 ASSERT_OBJECT_REFCOUNT (isrc, "isrc", 2); /* gsrc */
319 gst_object_unref (gsink);
320 ASSERT_OBJECT_REFCOUNT (isink, "isink", 1);
321 ASSERT_OBJECT_REFCOUNT (gisrc, "gisrc", 1);
322 ASSERT_OBJECT_REFCOUNT (fsrc, "fsrc", 2); /* gisrc */
323 gst_object_unref (gisrc);
324 ASSERT_OBJECT_REFCOUNT (fsrc, "fsrc", 1);
326 gst_object_unref (gsrc);
327 ASSERT_OBJECT_REFCOUNT (isrc, "isrc", 1);
328 ASSERT_OBJECT_REFCOUNT (gisink, "gisink", 1);
329 ASSERT_OBJECT_REFCOUNT (fsink, "fsink", 2); /* gisrc */
330 gst_object_unref (gisink);
331 ASSERT_OBJECT_REFCOUNT (fsink, "fsink", 1);
333 gst_object_unref (fsrc);
334 gst_object_unref (isrc);
335 gst_object_unref (isink);
336 gst_object_unref (fsink);
341 GST_START_TEST (test_ghost_pads_bin)
351 pipeline = GST_BIN (gst_pipeline_new ("pipe"));
352 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
354 srcbin = GST_BIN (gst_bin_new ("srcbin"));
355 gst_bin_add (pipeline, GST_ELEMENT (srcbin));
356 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 2); /* provide-clock msg */
358 sinkbin = GST_BIN (gst_bin_new ("sinkbin"));
359 gst_bin_add (pipeline, GST_ELEMENT (sinkbin));
360 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 3); /* provide-clock msg */
362 src = gst_element_factory_make ("fakesrc", "src");
363 gst_bin_add (srcbin, src);
364 srcghost = gst_ghost_pad_new ("src", gst_element_get_pad (src, "src"));
365 gst_element_add_pad (GST_ELEMENT (srcbin), srcghost);
367 sink = gst_element_factory_make ("fakesink", "sink");
368 gst_bin_add (sinkbin, sink);
369 sinkghost = gst_ghost_pad_new ("sink", gst_element_get_pad (sink, "sink"));
370 gst_element_add_pad (GST_ELEMENT (sinkbin), sinkghost);
372 gst_element_link (GST_ELEMENT (srcbin), GST_ELEMENT (sinkbin));
374 fail_unless (GST_PAD_PEER (srcghost) != NULL);
375 fail_unless (GST_PAD_PEER (sinkghost) != NULL);
376 fail_unless (GST_PAD_PEER (gst_ghost_pad_get_target (GST_GHOST_PAD
377 (srcghost))) != NULL);
378 fail_unless (GST_PAD_PEER (gst_ghost_pad_get_target (GST_GHOST_PAD
379 (sinkghost))) != NULL);
381 /* flush the message, dropping the b1 refcount to 1 */
382 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
383 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
384 ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
396 block_callback (GstPad * pad, gboolean blocked, gpointer user_data)
398 BlockData *block_data = (BlockData *) user_data;
400 g_mutex_lock (block_data->mutex);
401 GST_DEBUG ("blocked\n");
402 g_cond_signal (block_data->cond);
403 g_mutex_unlock (block_data->mutex);
406 GST_START_TEST (test_ghost_pads_block)
413 BlockData block_data;
415 pipeline = GST_BIN (gst_pipeline_new ("pipeline"));
417 srcbin = GST_BIN (gst_bin_new ("srcbin"));
418 gst_bin_add (pipeline, GST_ELEMENT (srcbin));
420 src = gst_element_factory_make ("fakesrc", "src");
421 gst_bin_add (srcbin, src);
422 srcpad = gst_element_get_pad (src, "src");
423 srcghost = gst_ghost_pad_new ("src", srcpad);
424 gst_element_add_pad (GST_ELEMENT (srcbin), srcghost);
425 gst_object_unref (srcpad);
427 block_data.mutex = g_mutex_new ();
428 block_data.cond = g_cond_new ();
430 g_mutex_lock (block_data.mutex);
431 gst_pad_set_blocked_async (srcghost, TRUE, block_callback, &block_data);
432 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
434 g_cond_wait (block_data.cond, block_data.mutex);
435 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
436 g_mutex_unlock (block_data.mutex);
438 g_mutex_free (block_data.mutex);
439 g_cond_free (block_data.cond);
444 GST_START_TEST (test_ghost_pads_probes)
451 BlockData block_data;
453 pipeline = GST_BIN (gst_pipeline_new ("pipeline"));
455 srcbin = GST_BIN (gst_bin_new ("srcbin"));
456 gst_bin_add (pipeline, GST_ELEMENT (srcbin));
458 src = gst_element_factory_make ("fakesrc", "src");
459 gst_bin_add (srcbin, src);
460 srcpad = gst_element_get_pad (src, "src");
461 srcghost = gst_ghost_pad_new ("src", srcpad);
462 gst_element_add_pad (GST_ELEMENT (srcbin), srcghost);
463 gst_object_unref (srcpad);
465 block_data.mutex = g_mutex_new ();
466 block_data.cond = g_cond_new ();
468 g_mutex_lock (block_data.mutex);
469 gst_pad_set_blocked_async (srcghost, TRUE, block_callback, &block_data);
470 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
472 g_cond_wait (block_data.cond, block_data.mutex);
473 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
474 g_mutex_unlock (block_data.mutex);
476 g_mutex_free (block_data.mutex);
477 g_cond_free (block_data.cond);
483 gst_ghost_pad_suite (void)
485 Suite *s = suite_create ("GstGhostPad");
486 TCase *tc_chain = tcase_create ("ghost pad tests");
488 suite_add_tcase (s, tc_chain);
489 tcase_add_test (tc_chain, test_remove1);
490 tcase_add_test (tc_chain, test_remove2);
491 tcase_add_test (tc_chain, test_link);
492 tcase_add_test (tc_chain, test_ghost_pads);
493 tcase_add_test (tc_chain, test_ghost_pads_bin);
494 /* tcase_add_test (tc_chain, test_ghost_pad_notarget); */
495 tcase_add_test (tc_chain, test_ghost_pads_block);
496 tcase_add_test (tc_chain, test_ghost_pads_probes);
501 GST_CHECK_MAIN (gst_ghost_pad);