2 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
4 * gstpad.c: Unit test for GstPad
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 GST_START_TEST (test_link)
32 src = gst_pad_new ("source", GST_PAD_SRC);
33 fail_if (src == NULL);
34 ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
36 name = gst_pad_get_name (src);
37 fail_unless (strcmp (name, "source") == 0);
38 ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
41 sink = gst_pad_new ("sink", GST_PAD_SINK);
42 fail_if (sink == NULL);
44 /* linking without templates or caps should fail */
45 ret = gst_pad_link (src, sink);
46 ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
47 ASSERT_OBJECT_REFCOUNT (sink, "sink pad", 1);
48 fail_unless (ret == GST_PAD_LINK_NOFORMAT);
50 ASSERT_CRITICAL (gst_pad_get_pad_template (NULL));
52 srct = gst_pad_get_pad_template (src);
53 fail_unless (srct == NULL);
54 ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
57 ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
58 gst_object_unref (src);
59 gst_object_unref (sink);
64 /* threaded link/unlink */
66 static GstPad *src, *sink;
69 thread_link_unlink (gpointer data)
73 while (THREAD_TEST_RUNNING ()) {
74 gst_pad_link (src, sink);
75 gst_pad_unlink (src, sink);
80 GST_START_TEST (test_link_unlink_threaded)
85 src = gst_pad_new ("source", GST_PAD_SRC);
86 fail_if (src == NULL);
87 sink = gst_pad_new ("sink", GST_PAD_SINK);
88 fail_if (sink == NULL);
90 caps = gst_caps_from_string ("foo/bar");
91 gst_pad_set_caps (src, caps);
92 gst_pad_set_caps (sink, caps);
93 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
95 MAIN_START_THREADS (5, thread_link_unlink, NULL);
96 for (i = 0; i < 1000; ++i) {
97 gst_pad_is_linked (src);
98 gst_pad_is_linked (sink);
101 MAIN_STOP_THREADS ();
103 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
104 gst_caps_unref (caps);
106 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
107 gst_object_unref (src);
108 gst_object_unref (sink);
113 GST_START_TEST (test_refcount)
117 GstPadLinkReturn plr;
119 sink = gst_pad_new ("sink", GST_PAD_SINK);
120 fail_if (sink == NULL);
122 src = gst_pad_new ("src", GST_PAD_SRC);
123 fail_if (src == NULL);
125 caps = gst_caps_from_string ("foo/bar");
127 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
129 gst_pad_set_caps (src, caps);
130 gst_pad_set_caps (sink, caps);
131 /* one for me and one for each set_caps */
132 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
134 plr = gst_pad_link (src, sink);
135 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
136 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
138 gst_pad_unlink (src, sink);
139 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
142 gst_object_unref (src);
143 gst_object_unref (sink);
144 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
146 gst_caps_unref (caps);
151 GST_START_TEST (test_get_allowed_caps)
154 GstCaps *caps, *gotcaps;
156 GstPadLinkReturn plr;
158 ASSERT_CRITICAL (gst_pad_get_allowed_caps (NULL));
160 buffer = gst_buffer_new ();
161 ASSERT_CRITICAL (gst_pad_get_allowed_caps ((GstPad *) buffer));
162 gst_buffer_unref (buffer);
164 src = gst_pad_new ("src", GST_PAD_SRC);
165 fail_if (src == NULL);
166 caps = gst_pad_get_allowed_caps (src);
167 fail_unless (caps == NULL);
169 caps = gst_caps_from_string ("foo/bar");
171 sink = gst_pad_new ("sink", GST_PAD_SINK);
172 gst_pad_set_caps (src, caps);
173 gst_pad_set_caps (sink, caps);
174 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
176 plr = gst_pad_link (src, sink);
177 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
179 gotcaps = gst_pad_get_allowed_caps (src);
180 fail_if (gotcaps == NULL);
181 fail_unless (gst_caps_is_equal (gotcaps, caps));
183 ASSERT_CAPS_REFCOUNT (gotcaps, "gotcaps", 1);
184 gst_caps_unref (gotcaps);
186 gst_pad_unlink (src, sink);
189 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
190 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
191 ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
193 gst_object_unref (src);
194 gst_object_unref (sink);
196 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
197 gst_caps_unref (caps);
203 name_is_valid (const gchar * name, GstPadPresence presence)
206 GstCaps *any = GST_CAPS_ANY;
208 new = gst_pad_template_new (name, GST_PAD_SRC, presence, any);
210 gst_object_unref (GST_OBJECT (new));
211 /* FIXME : We should not have to unref those caps, but due to
212 * a bug in gst_pad_template_new() not stealing the refcount of
213 * the given caps we have to. */
214 gst_caps_unref (any);
220 GST_START_TEST (test_name_is_valid)
222 gboolean result = FALSE;
224 fail_unless (name_is_valid ("src", GST_PAD_ALWAYS));
225 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_ALWAYS));
226 ASSERT_WARNING (result = name_is_valid ("src%d", GST_PAD_ALWAYS));
229 fail_unless (name_is_valid ("src", GST_PAD_REQUEST));
230 ASSERT_WARNING (name_is_valid ("src%s%s", GST_PAD_REQUEST));
231 ASSERT_WARNING (name_is_valid ("src%c", GST_PAD_REQUEST));
232 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_REQUEST));
233 ASSERT_WARNING (name_is_valid ("src%dsrc", GST_PAD_REQUEST));
235 fail_unless (name_is_valid ("src", GST_PAD_SOMETIMES));
236 fail_unless (name_is_valid ("src%c", GST_PAD_SOMETIMES));
242 _probe_handler (GstPad * pad, GstBuffer * buffer, gpointer userdata)
244 gint ret = GPOINTER_TO_INT (userdata);
251 GST_START_TEST (test_push_unlinked)
258 src = gst_pad_new ("src", GST_PAD_SRC);
259 fail_if (src == NULL);
260 caps = gst_pad_get_allowed_caps (src);
261 fail_unless (caps == NULL);
263 caps = gst_caps_from_string ("foo/bar");
265 gst_pad_set_caps (src, caps);
266 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
268 /* pushing on an unlinked pad will drop the buffer */
269 buffer = gst_buffer_new ();
270 gst_buffer_ref (buffer);
271 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
272 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
273 gst_buffer_unref (buffer);
275 /* adding a probe that returns FALSE will drop the buffer without trying
277 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
278 GINT_TO_POINTER (0));
279 buffer = gst_buffer_new ();
280 gst_buffer_ref (buffer);
281 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
282 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
283 gst_buffer_unref (buffer);
284 gst_pad_remove_buffer_probe (src, id);
286 /* adding a probe that returns TRUE will still chain the buffer,
287 * and hence drop because pad is unlinked */
288 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
289 GINT_TO_POINTER (1));
290 buffer = gst_buffer_new ();
291 gst_buffer_ref (buffer);
292 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
293 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
294 gst_buffer_unref (buffer);
295 gst_pad_remove_buffer_probe (src, id);
299 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
300 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
302 gst_object_unref (src);
304 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
305 gst_caps_unref (caps);
310 GST_START_TEST (test_push_linked)
313 GstPadLinkReturn plr;
319 sink = gst_pad_new ("sink", GST_PAD_SINK);
320 fail_if (sink == NULL);
321 gst_pad_set_chain_function (sink, gst_check_chain_func);
323 src = gst_pad_new ("src", GST_PAD_SRC);
324 fail_if (src == NULL);
326 caps = gst_caps_from_string ("foo/bar");
328 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
330 gst_pad_set_caps (src, caps);
331 gst_pad_set_caps (sink, caps);
332 /* one for me and one for each set_caps */
333 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
335 plr = gst_pad_link (src, sink);
336 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
337 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
339 buffer = gst_buffer_new ();
341 /* FIXME, new pad should be flushing */
342 gst_buffer_ref (buffer);
343 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
344 gst_buffer_ref (buffer);
345 fail_unless (gst_pad_chain (sink, buffer) == GST_FLOW_WRONG_STATE);
349 gst_pad_set_active (src, TRUE);
350 gst_pad_set_active (sink, TRUE);
353 /* pushing on a linked pad will drop the ref to the buffer */
354 gst_buffer_ref (buffer);
355 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
356 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
357 gst_buffer_unref (buffer);
358 fail_unless_equals_int (g_list_length (buffers), 1);
359 buffer = GST_BUFFER (buffers->data);
360 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
361 gst_buffer_unref (buffer);
362 g_list_free (buffers);
365 /* adding a probe that returns FALSE will drop the buffer without trying
367 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
368 GINT_TO_POINTER (0));
369 buffer = gst_buffer_new ();
370 gst_buffer_ref (buffer);
371 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
372 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
373 gst_buffer_unref (buffer);
374 gst_pad_remove_buffer_probe (src, id);
375 fail_unless_equals_int (g_list_length (buffers), 0);
377 /* adding a probe that returns TRUE will still chain the buffer */
378 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
379 GINT_TO_POINTER (1));
380 buffer = gst_buffer_new ();
381 gst_buffer_ref (buffer);
382 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
383 gst_pad_remove_buffer_probe (src, id);
385 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
386 gst_buffer_unref (buffer);
387 fail_unless_equals_int (g_list_length (buffers), 1);
388 buffer = GST_BUFFER (buffers->data);
389 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
390 gst_buffer_unref (buffer);
391 g_list_free (buffers);
395 gst_pad_unlink (src, sink);
396 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
397 gst_object_unref (src);
398 gst_object_unref (sink);
399 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
401 gst_caps_unref (caps);
406 GST_START_TEST (test_flowreturn)
411 /* test some of the macros */
412 ret = GST_FLOW_UNEXPECTED;
413 fail_unless (GST_FLOW_IS_FATAL (ret));
414 fail_if (GST_FLOW_IS_SUCCESS (ret));
415 fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
416 quark = gst_flow_to_quark (ret);
417 fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
419 ret = GST_FLOW_RESEND;
420 fail_if (GST_FLOW_IS_FATAL (ret));
421 fail_unless (GST_FLOW_IS_SUCCESS (ret));
422 fail_if (strcmp (gst_flow_get_name (ret), "resend"));
423 quark = gst_flow_to_quark (ret);
424 fail_if (strcmp (g_quark_to_string (quark), "resend"));
427 ret = GST_FLOW_CUSTOM_SUCCESS;
428 fail_if (GST_FLOW_IS_FATAL (ret));
429 fail_unless (GST_FLOW_IS_SUCCESS (ret));
430 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
431 quark = gst_flow_to_quark (ret);
432 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
434 ret = GST_FLOW_CUSTOM_ERROR;
435 fail_unless (GST_FLOW_IS_FATAL (ret));
436 fail_if (GST_FLOW_IS_SUCCESS (ret));
437 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
438 quark = gst_flow_to_quark (ret);
439 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
441 /* custom returns clamping */
442 ret = GST_FLOW_CUSTOM_SUCCESS + 2;
443 fail_if (GST_FLOW_IS_FATAL (ret));
444 fail_unless (GST_FLOW_IS_SUCCESS (ret));
445 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
446 quark = gst_flow_to_quark (ret);
447 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
449 ret = GST_FLOW_CUSTOM_ERROR - 2;
450 fail_unless (GST_FLOW_IS_FATAL (ret));
451 fail_if (GST_FLOW_IS_SUCCESS (ret));
452 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
453 quark = gst_flow_to_quark (ret);
454 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
457 ret = GST_FLOW_CUSTOM_ERROR + 2;
458 fail_unless (GST_FLOW_IS_FATAL (ret));
459 fail_if (GST_FLOW_IS_SUCCESS (ret));
460 fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
461 quark = gst_flow_to_quark (ret);
462 fail_unless (quark == 0);
467 GST_START_TEST (test_push_negotiation)
470 GstPadLinkReturn plr;
472 gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}");
474 gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}");
475 GstPadTemplate *src_template;
476 GstPadTemplate *sink_template;
481 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
482 GST_PAD_ALWAYS, srccaps);
483 sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
484 GST_PAD_ALWAYS, sinkcaps);
485 /* FIXME : We should not have to unref those caps, but due to
486 * a bug in gst_pad_template_new() not stealing the refcount of
487 * the given caps we have to. */
488 gst_caps_unref (srccaps);
489 gst_caps_unref (sinkcaps);
491 sink = gst_pad_new_from_template (sink_template, "sink");
492 fail_if (sink == NULL);
493 gst_pad_set_chain_function (sink, gst_check_chain_func);
495 src = gst_pad_new_from_template (src_template, "src");
496 fail_if (src == NULL);
498 plr = gst_pad_link (src, sink);
499 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
501 buffer = gst_buffer_new ();
504 gst_pad_set_active (src, TRUE);
505 gst_pad_set_active (sink, TRUE);
507 caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
509 /* Should fail if src pad caps are incompatible with sink pad caps */
510 gst_pad_set_caps (src, caps);
511 gst_buffer_ref (buffer);
512 gst_buffer_set_caps (buffer, caps);
513 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_NEGOTIATED);
514 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
515 gst_buffer_unref (buffer);
518 gst_pad_unlink (src, sink);
519 gst_object_unref (src);
520 gst_object_unref (sink);
521 gst_caps_unref (caps);
522 gst_object_unref (sink_template);
523 gst_object_unref (src_template);
528 /* see that an unref also unlinks the pads */
529 GST_START_TEST (test_src_unref_unlink)
533 GstPadLinkReturn plr;
535 sink = gst_pad_new ("sink", GST_PAD_SINK);
536 fail_if (sink == NULL);
538 src = gst_pad_new ("src", GST_PAD_SRC);
539 fail_if (src == NULL);
541 caps = gst_caps_from_string ("foo/bar");
543 gst_pad_set_caps (src, caps);
544 gst_pad_set_caps (sink, caps);
546 plr = gst_pad_link (src, sink);
547 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
549 /* unref the srcpad */
550 gst_object_unref (src);
552 /* sink should be unlinked now */
553 fail_if (gst_pad_is_linked (sink));
556 gst_object_unref (sink);
557 gst_caps_unref (caps);
562 /* see that an unref also unlinks the pads */
563 GST_START_TEST (test_sink_unref_unlink)
567 GstPadLinkReturn plr;
569 sink = gst_pad_new ("sink", GST_PAD_SINK);
570 fail_if (sink == NULL);
572 src = gst_pad_new ("src", GST_PAD_SRC);
573 fail_if (src == NULL);
575 caps = gst_caps_from_string ("foo/bar");
577 gst_pad_set_caps (src, caps);
578 gst_pad_set_caps (sink, caps);
580 plr = gst_pad_link (src, sink);
581 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
583 /* unref the sinkpad */
584 gst_object_unref (sink);
586 /* src should be unlinked now */
587 fail_if (gst_pad_is_linked (src));
590 gst_object_unref (src);
591 gst_caps_unref (caps);
596 /* gst_pad_get_caps should return a copy of the caps */
597 GST_START_TEST (test_get_caps_must_be_copy)
601 GstPadTemplate *templ;
603 caps = gst_caps_new_any ();
605 gst_pad_template_new ("test_templ", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
606 /* FIXME : This is not correct behaviour, but due to a bug with
607 * gst_pad_template_new() not stealing the refcount of the given caps,
608 * we need to unref it */
609 gst_caps_unref (caps);
611 pad = gst_pad_new_from_template (templ, NULL);
612 fail_unless (GST_PAD_CAPS (pad) == NULL, "caps present on pad");
613 /* This is a writable copy ! */
614 caps = gst_pad_get_caps (pad);
616 /* we must own the caps */
617 ASSERT_OBJECT_REFCOUNT (caps, "caps", 1);
620 gst_object_unref (templ);
621 gst_caps_unref (caps);
622 gst_object_unref (pad);
630 Suite *s = suite_create ("GstPad");
631 TCase *tc_chain = tcase_create ("general");
633 /* turn off timeout */
634 tcase_set_timeout (tc_chain, 60);
636 suite_add_tcase (s, tc_chain);
637 tcase_add_test (tc_chain, test_link);
638 tcase_add_test (tc_chain, test_refcount);
639 tcase_add_test (tc_chain, test_get_allowed_caps);
640 tcase_add_test (tc_chain, test_link_unlink_threaded);
641 tcase_add_test (tc_chain, test_name_is_valid);
642 tcase_add_test (tc_chain, test_push_unlinked);
643 tcase_add_test (tc_chain, test_push_linked);
644 tcase_add_test (tc_chain, test_flowreturn);
645 tcase_add_test (tc_chain, test_push_negotiation);
646 tcase_add_test (tc_chain, test_src_unref_unlink);
647 tcase_add_test (tc_chain, test_sink_unref_unlink);
648 tcase_add_test (tc_chain, test_get_caps_must_be_copy);
653 GST_CHECK_MAIN (gst_pad);