tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / gst / gstghostpad.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstghostpad.c: Unit test for GstGhostPad
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 /* test if removing a bin also cleans up the ghostpads
25  */
26 GST_START_TEST (test_remove1)
27 {
28   GstElement *b1, *b2, *src, *sink;
29   GstPad *srcpad, *sinkpad;
30   GstPadLinkReturn ret;
31
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);
38
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);
47
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);
51
52   srcpad = gst_element_get_pad (src, "src");
53   /* get the ghostpad */
54   sinkpad = gst_element_get_pad (b2, "sink");
55
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);
60
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);
65
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);
70
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);
76 }
77
78 GST_END_TEST;
79
80 /* test if removing a bin also cleans up the ghostpads
81  */
82 GST_START_TEST (test_remove2)
83 {
84   GstElement *b1, *b2, *src, *sink;
85   GstPad *srcpad, *sinkpad;
86   GstPadLinkReturn ret;
87
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);
93
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);
98
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);
102
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 */
108
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);
119
120   /* now remove the sink from the bin */
121   gst_bin_remove (GST_BIN (b2), sink);
122
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);
130
131   /* cleanup */
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);
136
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);
141
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);
147 }
148
149 GST_END_TEST;
150
151 #if 0
152 /* test if a ghost pad without a target can be linked
153  * It can't because it has incompatible caps...
154  */
155 GST_START_TEST (test_ghost_pad_notarget)
156 {
157   GstElement *b1, *b2, *sink;
158   GstPad *srcpad, *sinkpad;
159   GstPadLinkReturn ret;
160
161   b1 = gst_element_factory_make ("pipeline", NULL);
162   b2 = gst_element_factory_make ("bin", NULL);
163   sink = gst_element_factory_make ("fakesink", NULL);
164
165   fail_unless (gst_bin_add (GST_BIN (b1), sink));
166   fail_unless (gst_bin_add (GST_BIN (b1), b2));
167
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);
172
173   ret = gst_pad_link (srcpad, sinkpad);
174   fail_unless (ret == GST_PAD_LINK_OK);
175 }
176
177 GST_END_TEST;
178 #endif
179
180 /* test if linking fails over different bins using a pipeline
181  * like this:
182  *
183  * fakesrc num_buffers=10 ! ( fakesink )
184  *
185  */
186 GST_START_TEST (test_link)
187 {
188   GstElement *b1, *b2, *src, *sink;
189   GstPad *srcpad, *sinkpad, *gpad;
190   GstPadLinkReturn ret;
191
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);
196
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));
200
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);
205
206   /* linking in different hierarchies should fail */
207   ret = gst_pad_link (srcpad, sinkpad);
208   fail_unless (ret == GST_PAD_LINK_WRONG_HIERARCHY);
209
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);
216
217   /* our new sinkpad */
218   sinkpad = gpad;
219
220   /* and linking should work now */
221   ret = gst_pad_link (srcpad, sinkpad);
222   fail_unless (ret == GST_PAD_LINK_OK);
223
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);
228 }
229
230 GST_END_TEST;
231
232 /* test if ghostpads are created automagically when using
233  * gst_element_link_pads.
234  *
235  * fakesrc num_buffers=10 ! ( identity ) ! fakesink
236  */
237 GST_START_TEST (test_ghost_pads)
238 {
239   GstElement *b1, *b2, *src, *i1, *sink;
240   GstPad *gsink, *gsrc, *gisrc, *gisink, *isink, *isrc, *fsrc, *fsink;
241   GstStateChangeReturn ret;
242
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);
249
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);
262
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);
271
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);
280
281   /* all objects above have one refcount owned by us as well */
282
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 */
287
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 */
292
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);
296
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);
300
301   gst_object_unref (b1);
302   /* unreffing the bin will unref all elements, which will unlink and unparent
303    * all pads */
304
305   /* wait for thread to settle down */
306   while (GST_OBJECT_REFCOUNT_VALUE (fsrc) > 2)
307     THREAD_SWITCH ();
308
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 */
313
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 */
318
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);
325
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);
332
333   gst_object_unref (fsrc);
334   gst_object_unref (isrc);
335   gst_object_unref (isink);
336   gst_object_unref (fsink);
337 }
338
339 GST_END_TEST;
340
341 GST_START_TEST (test_ghost_pads_bin)
342 {
343   GstBin *pipeline;
344   GstBin *srcbin;
345   GstBin *sinkbin;
346   GstElement *src;
347   GstElement *sink;
348   GstPad *srcghost;
349   GstPad *sinkghost;
350
351   pipeline = GST_BIN (gst_pipeline_new ("pipe"));
352   ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);
353
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 */
357
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 */
361
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);
366
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);
371
372   gst_element_link (GST_ELEMENT (srcbin), GST_ELEMENT (sinkbin));
373
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);
380
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);
385 }
386
387 GST_END_TEST;
388
389 typedef struct
390 {
391   GMutex *mutex;
392   GCond *cond;
393 } BlockData;
394
395 static void
396 block_callback (GstPad * pad, gboolean blocked, gpointer user_data)
397 {
398   BlockData *block_data = (BlockData *) user_data;
399
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);
404 }
405
406 GST_START_TEST (test_ghost_pads_block)
407 {
408   GstBin *pipeline;
409   GstBin *srcbin;
410   GstElement *src;
411   GstPad *srcpad;
412   GstPad *srcghost;
413   BlockData block_data;
414
415   pipeline = GST_BIN (gst_pipeline_new ("pipeline"));
416
417   srcbin = GST_BIN (gst_bin_new ("srcbin"));
418   gst_bin_add (pipeline, GST_ELEMENT (srcbin));
419
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);
426
427   block_data.mutex = g_mutex_new ();
428   block_data.cond = g_cond_new ();
429
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);
433   /* and wait now */
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);
437
438   g_mutex_free (block_data.mutex);
439   g_cond_free (block_data.cond);
440 }
441
442 GST_END_TEST;
443
444 GST_START_TEST (test_ghost_pads_probes)
445 {
446   GstBin *pipeline;
447   GstBin *srcbin;
448   GstElement *src;
449   GstPad *srcpad;
450   GstPad *srcghost;
451   BlockData block_data;
452
453   pipeline = GST_BIN (gst_pipeline_new ("pipeline"));
454
455   srcbin = GST_BIN (gst_bin_new ("srcbin"));
456   gst_bin_add (pipeline, GST_ELEMENT (srcbin));
457
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);
464
465   block_data.mutex = g_mutex_new ();
466   block_data.cond = g_cond_new ();
467
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);
471   /* and wait now */
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);
475
476   g_mutex_free (block_data.mutex);
477   g_cond_free (block_data.cond);
478 }
479
480 GST_END_TEST;
481
482 Suite *
483 gst_ghost_pad_suite (void)
484 {
485   Suite *s = suite_create ("GstGhostPad");
486   TCase *tc_chain = tcase_create ("ghost pad tests");
487
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);
497
498   return s;
499 }
500
501 GST_CHECK_MAIN (gst_ghost_pad);