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 */
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)
207 new = gst_pad_template_new (name, GST_PAD_SRC, presence, GST_CAPS_ANY);
209 gst_object_unref (GST_OBJECT (new));
215 GST_START_TEST (test_name_is_valid)
217 gboolean result = FALSE;
219 fail_unless (name_is_valid ("src", GST_PAD_ALWAYS));
220 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_ALWAYS));
221 ASSERT_WARNING (result = name_is_valid ("src%d", GST_PAD_ALWAYS));
224 fail_unless (name_is_valid ("src", GST_PAD_REQUEST));
225 ASSERT_WARNING (name_is_valid ("src%s%s", GST_PAD_REQUEST));
226 ASSERT_WARNING (name_is_valid ("src%c", GST_PAD_REQUEST));
227 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_REQUEST));
228 ASSERT_WARNING (name_is_valid ("src%dsrc", GST_PAD_REQUEST));
230 fail_unless (name_is_valid ("src", GST_PAD_SOMETIMES));
231 fail_unless (name_is_valid ("src%c", GST_PAD_SOMETIMES));
237 _probe_handler (GstPad * pad, GstBuffer * buffer, gpointer userdata)
239 gint ret = GPOINTER_TO_INT (userdata);
246 GST_START_TEST (test_push_unlinked)
253 src = gst_pad_new ("src", GST_PAD_SRC);
254 fail_if (src == NULL);
255 caps = gst_pad_get_allowed_caps (src);
256 fail_unless (caps == NULL);
258 caps = gst_caps_from_string ("foo/bar");
260 gst_pad_set_caps (src, caps);
261 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
263 /* pushing on an unlinked pad will drop the buffer */
264 buffer = gst_buffer_new ();
265 gst_buffer_ref (buffer);
266 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
267 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
268 gst_buffer_unref (buffer);
270 /* adding a probe that returns FALSE will drop the buffer without trying
272 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
273 GINT_TO_POINTER (0));
274 buffer = gst_buffer_new ();
275 gst_buffer_ref (buffer);
276 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
277 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
278 gst_buffer_unref (buffer);
279 gst_pad_remove_buffer_probe (src, id);
281 /* adding a probe that returns TRUE will still chain the buffer,
282 * and hence drop because pad is unlinked */
283 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
284 GINT_TO_POINTER (1));
285 buffer = gst_buffer_new ();
286 gst_buffer_ref (buffer);
287 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
288 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
289 gst_buffer_unref (buffer);
290 gst_pad_remove_buffer_probe (src, id);
294 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
295 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
297 gst_object_unref (src);
299 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
300 gst_caps_unref (caps);
305 GST_START_TEST (test_push_linked)
308 GstPadLinkReturn plr;
314 sink = gst_pad_new ("sink", GST_PAD_SINK);
315 fail_if (sink == NULL);
316 gst_pad_set_chain_function (sink, gst_check_chain_func);
318 src = gst_pad_new ("src", GST_PAD_SRC);
319 fail_if (src == NULL);
321 caps = gst_caps_from_string ("foo/bar");
323 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
325 gst_pad_set_caps (src, caps);
326 gst_pad_set_caps (sink, caps);
327 /* one for me and one for each set_caps */
328 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
330 plr = gst_pad_link (src, sink);
331 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
332 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
334 buffer = gst_buffer_new ();
336 /* FIXME, new pad should be flushing */
337 gst_buffer_ref (buffer);
338 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
339 gst_buffer_ref (buffer);
340 fail_unless (gst_pad_chain (sink, buffer) == GST_FLOW_WRONG_STATE);
344 gst_pad_set_active (src, TRUE);
345 gst_pad_set_active (sink, TRUE);
348 /* pushing on a linked pad will drop the ref to the buffer */
349 gst_buffer_ref (buffer);
350 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
351 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
352 gst_buffer_unref (buffer);
353 fail_unless_equals_int (g_list_length (buffers), 1);
354 buffer = GST_BUFFER (buffers->data);
355 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
356 gst_buffer_unref (buffer);
357 g_list_free (buffers);
360 /* adding a probe that returns FALSE will drop the buffer without trying
362 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
363 GINT_TO_POINTER (0));
364 buffer = gst_buffer_new ();
365 gst_buffer_ref (buffer);
366 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
367 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
368 gst_buffer_unref (buffer);
369 gst_pad_remove_buffer_probe (src, id);
370 fail_unless_equals_int (g_list_length (buffers), 0);
372 /* adding a probe that returns TRUE will still chain the buffer */
373 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
374 GINT_TO_POINTER (1));
375 buffer = gst_buffer_new ();
376 gst_buffer_ref (buffer);
377 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
378 gst_pad_remove_buffer_probe (src, id);
380 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
381 gst_buffer_unref (buffer);
382 fail_unless_equals_int (g_list_length (buffers), 1);
383 buffer = GST_BUFFER (buffers->data);
384 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
385 gst_buffer_unref (buffer);
386 g_list_free (buffers);
390 gst_pad_unlink (src, sink);
391 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
392 gst_object_unref (src);
393 gst_object_unref (sink);
394 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
396 gst_caps_unref (caps);
401 GST_START_TEST (test_flowreturn)
406 /* test some of the macros */
407 ret = GST_FLOW_UNEXPECTED;
408 fail_unless (GST_FLOW_IS_FATAL (ret));
409 fail_if (GST_FLOW_IS_SUCCESS (ret));
410 fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
411 quark = gst_flow_to_quark (ret);
412 fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
414 ret = GST_FLOW_RESEND;
415 fail_if (GST_FLOW_IS_FATAL (ret));
416 fail_unless (GST_FLOW_IS_SUCCESS (ret));
417 fail_if (strcmp (gst_flow_get_name (ret), "resend"));
418 quark = gst_flow_to_quark (ret);
419 fail_if (strcmp (g_quark_to_string (quark), "resend"));
422 ret = GST_FLOW_CUSTOM_SUCCESS;
423 fail_if (GST_FLOW_IS_FATAL (ret));
424 fail_unless (GST_FLOW_IS_SUCCESS (ret));
425 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
426 quark = gst_flow_to_quark (ret);
427 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
429 ret = GST_FLOW_CUSTOM_ERROR;
430 fail_unless (GST_FLOW_IS_FATAL (ret));
431 fail_if (GST_FLOW_IS_SUCCESS (ret));
432 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
433 quark = gst_flow_to_quark (ret);
434 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
436 /* custom returns clamping */
437 ret = GST_FLOW_CUSTOM_SUCCESS + 2;
438 fail_if (GST_FLOW_IS_FATAL (ret));
439 fail_unless (GST_FLOW_IS_SUCCESS (ret));
440 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
441 quark = gst_flow_to_quark (ret);
442 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
444 ret = GST_FLOW_CUSTOM_ERROR - 2;
445 fail_unless (GST_FLOW_IS_FATAL (ret));
446 fail_if (GST_FLOW_IS_SUCCESS (ret));
447 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
448 quark = gst_flow_to_quark (ret);
449 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
452 ret = GST_FLOW_CUSTOM_ERROR + 2;
453 fail_unless (GST_FLOW_IS_FATAL (ret));
454 fail_if (GST_FLOW_IS_SUCCESS (ret));
455 fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
456 quark = gst_flow_to_quark (ret);
457 fail_unless (quark == 0);
462 GST_START_TEST (test_push_negotiation)
465 GstPadLinkReturn plr;
466 GstPadTemplate *src_template = gst_pad_template_new ("src", GST_PAD_SRC,
468 gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}"));
469 GstPadTemplate *sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
471 gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}"));
476 sink = gst_pad_new_from_template (sink_template, "sink");
477 fail_if (sink == NULL);
478 gst_pad_set_chain_function (sink, gst_check_chain_func);
480 src = gst_pad_new_from_template (src_template, "src");
481 fail_if (src == NULL);
483 plr = gst_pad_link (src, sink);
484 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
486 buffer = gst_buffer_new ();
489 gst_pad_set_active (src, TRUE);
490 gst_pad_set_active (sink, TRUE);
492 caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
494 /* Should fail if src pad caps are incompatible with sink pad caps */
495 gst_pad_set_caps (src, caps);
496 gst_buffer_ref (buffer);
497 gst_buffer_set_caps (buffer, caps);
498 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_NEGOTIATED);
499 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
500 gst_buffer_unref (buffer);
503 gst_pad_unlink (src, sink);
504 gst_object_unref (src);
505 gst_object_unref (sink);
506 gst_caps_unref (caps);
507 gst_object_unref (sink_template);
508 gst_object_unref (src_template);
513 /* see that an unref also unlinks the pads */
514 GST_START_TEST (test_src_unref_unlink)
518 GstPadLinkReturn plr;
520 sink = gst_pad_new ("sink", GST_PAD_SINK);
521 fail_if (sink == NULL);
523 src = gst_pad_new ("src", GST_PAD_SRC);
524 fail_if (src == NULL);
526 caps = gst_caps_from_string ("foo/bar");
528 gst_pad_set_caps (src, caps);
529 gst_pad_set_caps (sink, caps);
531 plr = gst_pad_link (src, sink);
532 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
534 /* unref the srcpad */
535 gst_object_unref (src);
537 /* sink should be unlinked now */
538 fail_if (gst_pad_is_linked (sink));
541 gst_object_unref (sink);
542 gst_caps_unref (caps);
547 /* see that an unref also unlinks the pads */
548 GST_START_TEST (test_sink_unref_unlink)
552 GstPadLinkReturn plr;
554 sink = gst_pad_new ("sink", GST_PAD_SINK);
555 fail_if (sink == NULL);
557 src = gst_pad_new ("src", GST_PAD_SRC);
558 fail_if (src == NULL);
560 caps = gst_caps_from_string ("foo/bar");
562 gst_pad_set_caps (src, caps);
563 gst_pad_set_caps (sink, caps);
565 plr = gst_pad_link (src, sink);
566 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
568 /* unref the sinkpad */
569 gst_object_unref (sink);
571 /* src should be unlinked now */
572 fail_if (gst_pad_is_linked (src));
575 gst_object_unref (src);
576 gst_caps_unref (caps);
581 /* gst_pad_get_caps should return a copy of the caps */
582 GST_START_TEST (test_get_caps_must_be_copy)
586 GstPadTemplate *templ;
588 caps = gst_caps_new_any ();
590 gst_pad_template_new ("test_templ", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
591 pad = gst_pad_new_from_template (templ, NULL);
592 fail_unless (GST_PAD_CAPS (pad) == NULL, "caps present on pad");
593 caps = gst_pad_get_caps (pad);
595 /* we must own the caps */
596 ASSERT_OBJECT_REFCOUNT (caps, "caps", 1);
599 gst_caps_unref (caps);
600 gst_object_unref (pad);
601 gst_object_unref (templ);
609 Suite *s = suite_create ("GstPad");
610 TCase *tc_chain = tcase_create ("general");
612 /* turn off timeout */
613 tcase_set_timeout (tc_chain, 60);
615 suite_add_tcase (s, tc_chain);
616 tcase_add_test (tc_chain, test_link);
617 tcase_add_test (tc_chain, test_refcount);
618 tcase_add_test (tc_chain, test_get_allowed_caps);
619 tcase_add_test (tc_chain, test_link_unlink_threaded);
620 tcase_add_test (tc_chain, test_name_is_valid);
621 tcase_add_test (tc_chain, test_push_unlinked);
622 tcase_add_test (tc_chain, test_push_linked);
623 tcase_add_test (tc_chain, test_flowreturn);
624 tcase_add_test (tc_chain, test_push_negotiation);
625 tcase_add_test (tc_chain, test_src_unref_unlink);
626 tcase_add_test (tc_chain, test_sink_unref_unlink);
627 tcase_add_test (tc_chain, test_get_caps_must_be_copy);
632 GST_CHECK_MAIN (gst_pad);