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));
216 GST_START_TEST (test_name_is_valid)
218 gboolean result = FALSE;
220 fail_unless (name_is_valid ("src", GST_PAD_ALWAYS));
221 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_ALWAYS));
222 ASSERT_WARNING (result = name_is_valid ("src%d", GST_PAD_ALWAYS));
225 fail_unless (name_is_valid ("src", GST_PAD_REQUEST));
226 ASSERT_WARNING (name_is_valid ("src%s%s", GST_PAD_REQUEST));
227 ASSERT_WARNING (name_is_valid ("src%c", GST_PAD_REQUEST));
228 ASSERT_WARNING (name_is_valid ("src%", GST_PAD_REQUEST));
229 ASSERT_WARNING (name_is_valid ("src%dsrc", GST_PAD_REQUEST));
231 fail_unless (name_is_valid ("src", GST_PAD_SOMETIMES));
232 fail_unless (name_is_valid ("src%c", GST_PAD_SOMETIMES));
238 _probe_handler (GstPad * pad, GstBuffer * buffer, gpointer userdata)
240 gint ret = GPOINTER_TO_INT (userdata);
247 GST_START_TEST (test_push_unlinked)
254 src = gst_pad_new ("src", GST_PAD_SRC);
255 fail_if (src == NULL);
256 caps = gst_pad_get_allowed_caps (src);
257 fail_unless (caps == NULL);
259 caps = gst_caps_from_string ("foo/bar");
261 gst_pad_set_caps (src, caps);
262 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
264 /* pushing on an unlinked pad will drop the buffer */
265 buffer = gst_buffer_new ();
266 gst_buffer_ref (buffer);
267 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
268 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
269 gst_buffer_unref (buffer);
271 /* adding a probe that returns FALSE will drop the buffer without trying
273 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
274 GINT_TO_POINTER (0));
275 buffer = gst_buffer_new ();
276 gst_buffer_ref (buffer);
277 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
278 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
279 gst_buffer_unref (buffer);
280 gst_pad_remove_buffer_probe (src, id);
282 /* adding a probe that returns TRUE will still chain the buffer,
283 * and hence drop because pad is unlinked */
284 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
285 GINT_TO_POINTER (1));
286 buffer = gst_buffer_new ();
287 gst_buffer_ref (buffer);
288 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
289 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
290 gst_buffer_unref (buffer);
291 gst_pad_remove_buffer_probe (src, id);
295 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
296 ASSERT_OBJECT_REFCOUNT (src, "src", 1);
298 gst_object_unref (src);
300 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
301 gst_caps_unref (caps);
306 GST_START_TEST (test_push_linked)
309 GstPadLinkReturn plr;
315 sink = gst_pad_new ("sink", GST_PAD_SINK);
316 fail_if (sink == NULL);
317 gst_pad_set_chain_function (sink, gst_check_chain_func);
319 src = gst_pad_new ("src", GST_PAD_SRC);
320 fail_if (src == NULL);
322 caps = gst_caps_from_string ("foo/bar");
324 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
326 gst_pad_set_caps (src, caps);
327 gst_pad_set_caps (sink, caps);
328 /* one for me and one for each set_caps */
329 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
331 plr = gst_pad_link (src, sink);
332 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
333 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
335 buffer = gst_buffer_new ();
337 /* FIXME, new pad should be flushing */
338 gst_buffer_ref (buffer);
339 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
340 gst_buffer_ref (buffer);
341 fail_unless (gst_pad_chain (sink, buffer) == GST_FLOW_WRONG_STATE);
345 gst_pad_set_active (src, TRUE);
346 gst_pad_set_active (sink, TRUE);
349 /* pushing on a linked pad will drop the ref to the buffer */
350 gst_buffer_ref (buffer);
351 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
352 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
353 gst_buffer_unref (buffer);
354 fail_unless_equals_int (g_list_length (buffers), 1);
355 buffer = GST_BUFFER (buffers->data);
356 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
357 gst_buffer_unref (buffer);
358 g_list_free (buffers);
361 /* adding a probe that returns FALSE will drop the buffer without trying
363 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
364 GINT_TO_POINTER (0));
365 buffer = gst_buffer_new ();
366 gst_buffer_ref (buffer);
367 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
368 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
369 gst_buffer_unref (buffer);
370 gst_pad_remove_buffer_probe (src, id);
371 fail_unless_equals_int (g_list_length (buffers), 0);
373 /* adding a probe that returns TRUE will still chain the buffer */
374 id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
375 GINT_TO_POINTER (1));
376 buffer = gst_buffer_new ();
377 gst_buffer_ref (buffer);
378 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
379 gst_pad_remove_buffer_probe (src, id);
381 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
382 gst_buffer_unref (buffer);
383 fail_unless_equals_int (g_list_length (buffers), 1);
384 buffer = GST_BUFFER (buffers->data);
385 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
386 gst_buffer_unref (buffer);
387 g_list_free (buffers);
391 gst_pad_unlink (src, sink);
392 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
393 gst_object_unref (src);
394 gst_object_unref (sink);
395 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
397 gst_caps_unref (caps);
403 buffer_from_string (const gchar * str)
409 buf = gst_buffer_new_and_alloc (size);
410 memcpy (GST_BUFFER_DATA (buf), str, size);
411 GST_BUFFER_SIZE (buf) = size;
416 GST_START_TEST (test_push_buffer_list_compat)
419 GstPadLinkReturn plr;
422 GstBufferListIterator *it;
426 sink = gst_pad_new ("sink", GST_PAD_SINK);
427 fail_if (sink == NULL);
428 gst_pad_set_chain_function (sink, gst_check_chain_func);
429 /* leave chainlistfunc unset */
431 src = gst_pad_new ("src", GST_PAD_SRC);
432 fail_if (src == NULL);
434 caps = gst_caps_from_string ("foo/bar");
436 gst_pad_set_caps (src, caps);
437 gst_pad_set_caps (sink, caps);
439 plr = gst_pad_link (src, sink);
440 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
442 list = gst_buffer_list_new ();
445 gst_pad_set_active (src, TRUE);
446 gst_pad_set_active (sink, TRUE);
449 /* adding to a buffer list will drop the ref to the buffer */
450 it = gst_buffer_list_iterate (list);
451 gst_buffer_list_iterator_add_group (it);
452 gst_buffer_list_iterator_add (it, buffer_from_string ("List"));
453 gst_buffer_list_iterator_add (it, buffer_from_string ("Group"));
454 gst_buffer_list_iterator_add_group (it);
455 gst_buffer_list_iterator_add (it, buffer_from_string ("Another"));
456 gst_buffer_list_iterator_add (it, buffer_from_string ("List"));
457 gst_buffer_list_iterator_add (it, buffer_from_string ("Group"));
458 gst_buffer_list_iterator_free (it);
459 fail_unless (gst_pad_push_list (src, list) == GST_FLOW_OK);
460 fail_unless_equals_int (g_list_length (buffers), 2);
461 buffer = GST_BUFFER (buffers->data);
462 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
463 fail_unless (memcmp (GST_BUFFER_DATA (buffer), "ListGroup", 9) == 0);
464 gst_buffer_unref (buffer);
465 buffers = g_list_delete_link (buffers, buffers);
466 buffer = GST_BUFFER (buffers->data);
467 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
468 fail_unless (memcmp (GST_BUFFER_DATA (buffer), "AnotherListGroup", 16) == 0);
469 gst_buffer_unref (buffer);
470 buffers = g_list_delete_link (buffers, buffers);
471 fail_unless (buffers == NULL);
474 gst_pad_unlink (src, sink);
475 gst_object_unref (src);
476 gst_object_unref (sink);
477 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
478 gst_caps_unref (caps);
483 GST_START_TEST (test_flowreturn)
488 /* test some of the macros */
489 ret = GST_FLOW_UNEXPECTED;
490 fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
491 quark = gst_flow_to_quark (ret);
492 fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
494 ret = GST_FLOW_RESEND;
495 fail_if (strcmp (gst_flow_get_name (ret), "resend"));
496 quark = gst_flow_to_quark (ret);
497 fail_if (strcmp (g_quark_to_string (quark), "resend"));
500 ret = GST_FLOW_CUSTOM_SUCCESS;
501 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
502 quark = gst_flow_to_quark (ret);
503 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
505 ret = GST_FLOW_CUSTOM_ERROR;
506 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
507 quark = gst_flow_to_quark (ret);
508 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
510 /* custom returns clamping */
511 ret = GST_FLOW_CUSTOM_SUCCESS + 2;
512 fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
513 quark = gst_flow_to_quark (ret);
514 fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
516 ret = GST_FLOW_CUSTOM_ERROR - 2;
517 fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
518 quark = gst_flow_to_quark (ret);
519 fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
522 ret = GST_FLOW_CUSTOM_ERROR + 2;
523 fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
524 quark = gst_flow_to_quark (ret);
525 fail_unless (quark == 0);
530 GST_START_TEST (test_push_negotiation)
533 GstPadLinkReturn plr;
535 gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}");
537 gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}");
538 GstPadTemplate *src_template;
539 GstPadTemplate *sink_template;
544 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
545 GST_PAD_ALWAYS, srccaps);
546 sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
547 GST_PAD_ALWAYS, sinkcaps);
549 sink = gst_pad_new_from_template (sink_template, "sink");
550 fail_if (sink == NULL);
551 gst_pad_set_chain_function (sink, gst_check_chain_func);
553 src = gst_pad_new_from_template (src_template, "src");
554 fail_if (src == NULL);
556 plr = gst_pad_link (src, sink);
557 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
559 buffer = gst_buffer_new ();
562 gst_pad_set_active (src, TRUE);
563 gst_pad_set_active (sink, TRUE);
565 caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
567 /* Should fail if src pad caps are incompatible with sink pad caps */
568 gst_pad_set_caps (src, caps);
569 gst_buffer_set_caps (buffer, caps);
570 gst_buffer_ref (buffer);
571 fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_NEGOTIATED);
572 ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
573 gst_buffer_unref (buffer);
576 gst_pad_unlink (src, sink);
577 gst_object_unref (src);
578 gst_object_unref (sink);
579 gst_caps_unref (caps);
580 gst_object_unref (sink_template);
581 gst_object_unref (src_template);
586 /* see that an unref also unlinks the pads */
587 GST_START_TEST (test_src_unref_unlink)
591 GstPadLinkReturn plr;
593 sink = gst_pad_new ("sink", GST_PAD_SINK);
594 fail_if (sink == NULL);
596 src = gst_pad_new ("src", GST_PAD_SRC);
597 fail_if (src == NULL);
599 caps = gst_caps_from_string ("foo/bar");
601 gst_pad_set_caps (src, caps);
602 gst_pad_set_caps (sink, caps);
604 plr = gst_pad_link (src, sink);
605 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
607 /* unref the srcpad */
608 gst_object_unref (src);
610 /* sink should be unlinked now */
611 fail_if (gst_pad_is_linked (sink));
614 gst_object_unref (sink);
615 gst_caps_unref (caps);
620 /* see that an unref also unlinks the pads */
621 GST_START_TEST (test_sink_unref_unlink)
625 GstPadLinkReturn plr;
627 sink = gst_pad_new ("sink", GST_PAD_SINK);
628 fail_if (sink == NULL);
630 src = gst_pad_new ("src", GST_PAD_SRC);
631 fail_if (src == NULL);
633 caps = gst_caps_from_string ("foo/bar");
635 gst_pad_set_caps (src, caps);
636 gst_pad_set_caps (sink, caps);
638 plr = gst_pad_link (src, sink);
639 fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
641 /* unref the sinkpad */
642 gst_object_unref (sink);
644 /* src should be unlinked now */
645 fail_if (gst_pad_is_linked (src));
648 gst_object_unref (src);
649 gst_caps_unref (caps);
655 unblock_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
657 gboolean *bool_user_data = (gboolean *) user_data;
659 /* here we should have blocked == 1 unblocked == 0 */
660 fail_unless (bool_user_data[0] == TRUE);
661 fail_unless (bool_user_data[1] == FALSE);
663 bool_user_data[1] = TRUE;
667 block_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
669 gboolean *bool_user_data = (gboolean *) user_data;
671 /* here we should have blocked == 0 unblocked == 0 */
672 fail_unless (bool_user_data[0] == FALSE);
673 fail_unless (bool_user_data[1] == FALSE);
675 bool_user_data[0] = blocked;
677 gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, user_data);
680 GST_START_TEST (test_block_async)
683 /* we set data[0] = TRUE when the pad is blocked, data[1] = TRUE when it's
685 gboolean data[2] = { FALSE, FALSE };
687 pad = gst_pad_new ("src", GST_PAD_SRC);
688 fail_unless (pad != NULL);
690 gst_pad_set_active (pad, TRUE);
691 gst_pad_set_blocked_async (pad, TRUE, block_async_cb, &data);
693 fail_unless (data[0] == FALSE);
694 fail_unless (data[1] == FALSE);
695 gst_pad_push (pad, gst_buffer_new ());
697 gst_object_unref (pad);
704 block_async_second (GstPad * pad, gboolean blocked, gpointer user_data)
706 gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, NULL);
710 block_async_first (GstPad * pad, gboolean blocked, gpointer user_data)
712 static int n_calls = 0;
713 gboolean *bool_user_data = (gboolean *) user_data;
716 /* we expect this callback to be called only once */
717 g_warn_if_reached ();
719 *bool_user_data = blocked;
721 /* replace block_async_first with block_async_second so next time the pad is
722 * blocked the latter should be called */
723 gst_pad_set_blocked_async (pad, TRUE, block_async_second, NULL);
725 /* unblock temporarily, in the next push block_async_second should be called
727 gst_pad_push_event (pad, gst_event_new_flush_start ());
730 GST_START_TEST (test_block_async_replace_callback)
735 pad = gst_pad_new ("src", GST_PAD_SRC);
736 fail_unless (pad != NULL);
737 gst_pad_set_active (pad, TRUE);
739 gst_pad_set_blocked_async (pad, TRUE, block_async_first, &blocked);
742 gst_pad_push (pad, gst_buffer_new ());
743 fail_unless (blocked == TRUE);
744 /* block_async_first flushes to unblock */
745 gst_pad_push_event (pad, gst_event_new_flush_stop ());
747 /* push again, this time block_async_second should be called */
748 gst_pad_push (pad, gst_buffer_new ());
749 fail_unless (blocked == TRUE);
751 gst_object_unref (pad);
758 block_async_full_destroy (gpointer user_data)
760 gint *state = (gint *) user_data;
762 fail_unless (*state < 2);
764 GST_DEBUG ("setting state to 2");
769 block_async_full_cb (GstPad * pad, gboolean blocked, gpointer user_data)
771 *(gint *) user_data = (gint) blocked;
773 gst_pad_push_event (pad, gst_event_new_flush_start ());
774 GST_DEBUG ("setting state to 1");
777 GST_START_TEST (test_block_async_full_destroy)
780 /* 0 = unblocked, 1 = blocked, 2 = destroyed */
783 pad = gst_pad_new ("src", GST_PAD_SRC);
784 fail_unless (pad != NULL);
785 gst_pad_set_active (pad, TRUE);
787 gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
788 &state, block_async_full_destroy);
789 fail_unless (state == 0);
791 gst_pad_push (pad, gst_buffer_new ());
792 /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
794 fail_unless (state == 1);
795 gst_pad_push_event (pad, gst_event_new_flush_stop ());
797 /* pad was already blocked so nothing happens */
798 gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
799 &state, block_async_full_destroy);
800 fail_unless (state == 1);
802 /* unblock with the same data, callback is called */
803 gst_pad_set_blocked_async_full (pad, FALSE, block_async_full_cb,
804 &state, block_async_full_destroy);
805 fail_unless (state == 2);
807 /* block with the same data, callback is called */
809 gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
810 &state, block_async_full_destroy);
811 fail_unless (state == 2);
813 /* now change user_data (to NULL in this case) so destroy_notify should be
816 gst_pad_set_blocked_async_full (pad, FALSE, block_async_full_cb,
817 NULL, block_async_full_destroy);
818 fail_unless (state == 2);
820 gst_object_unref (pad);
825 GST_START_TEST (test_block_async_full_destroy_dispose)
828 /* 0 = unblocked, 1 = blocked, 2 = destroyed */
831 pad = gst_pad_new ("src", GST_PAD_SRC);
832 fail_unless (pad != NULL);
833 gst_pad_set_active (pad, TRUE);
835 gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
836 &state, block_async_full_destroy);
838 gst_pad_push (pad, gst_buffer_new ());
839 /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
841 fail_unless_equals_int (state, 1);
842 gst_pad_push_event (pad, gst_event_new_flush_stop ());
844 /* gst_pad_dispose calls the destroy_notify function if necessary */
845 gst_object_unref (pad);
847 fail_unless_equals_int (state, 2);
854 unblock_async_no_flush_cb (GstPad * pad, gboolean blocked, gpointer user_data)
856 gboolean *bool_user_data = (gboolean *) user_data;
858 /* here we should have blocked == 1 unblocked == 0 */
860 fail_unless (blocked == FALSE);
862 fail_unless (bool_user_data[0] == TRUE);
863 fail_unless (bool_user_data[1] == TRUE);
864 fail_unless (bool_user_data[2] == FALSE);
866 bool_user_data[2] = TRUE;
871 unblock_async_not_called (GstPad * pad, gboolean blocked, gpointer user_data)
873 g_warn_if_reached ();
877 block_async_second_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
879 gboolean *bool_user_data = (gboolean *) user_data;
881 fail_unless (blocked == TRUE);
883 fail_unless (bool_user_data[0] == TRUE);
884 fail_unless (bool_user_data[1] == FALSE);
885 fail_unless (bool_user_data[2] == FALSE);
887 bool_user_data[1] = TRUE;
889 fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_no_flush_cb,
894 block_async_first_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
896 static int n_calls = 0;
897 gboolean *bool_user_data = (gboolean *) user_data;
899 fail_unless (blocked == TRUE);
902 /* we expect this callback to be called only once */
903 g_warn_if_reached ();
905 *bool_user_data = blocked;
907 fail_unless (bool_user_data[0] == TRUE);
908 fail_unless (bool_user_data[1] == FALSE);
909 fail_unless (bool_user_data[2] == FALSE);
911 fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_not_called,
914 /* replace block_async_first with block_async_second so next time the pad is
915 * blocked the latter should be called */
916 fail_unless (gst_pad_set_blocked_async (pad, TRUE,
917 block_async_second_no_flush, user_data));
920 GST_START_TEST (test_block_async_replace_callback_no_flush)
923 gboolean bool_user_data[3] = { FALSE, FALSE, FALSE };
925 pad = gst_pad_new ("src", GST_PAD_SRC);
926 fail_unless (pad != NULL);
927 gst_pad_set_active (pad, TRUE);
929 fail_unless (gst_pad_set_blocked_async (pad, TRUE, block_async_first_no_flush,
932 gst_pad_push (pad, gst_buffer_new ());
933 fail_unless (bool_user_data[0] == TRUE);
934 fail_unless (bool_user_data[1] == TRUE);
935 fail_unless (bool_user_data[2] == TRUE);
937 gst_object_unref (pad);
946 Suite *s = suite_create ("GstPad");
947 TCase *tc_chain = tcase_create ("general");
949 /* turn off timeout */
950 tcase_set_timeout (tc_chain, 60);
952 suite_add_tcase (s, tc_chain);
953 tcase_add_test (tc_chain, test_link);
954 tcase_add_test (tc_chain, test_refcount);
955 tcase_add_test (tc_chain, test_get_allowed_caps);
956 tcase_add_test (tc_chain, test_link_unlink_threaded);
957 tcase_add_test (tc_chain, test_name_is_valid);
958 tcase_add_test (tc_chain, test_push_unlinked);
959 tcase_add_test (tc_chain, test_push_linked);
960 tcase_add_test (tc_chain, test_push_buffer_list_compat);
961 tcase_add_test (tc_chain, test_flowreturn);
962 tcase_add_test (tc_chain, test_push_negotiation);
963 tcase_add_test (tc_chain, test_src_unref_unlink);
964 tcase_add_test (tc_chain, test_sink_unref_unlink);
965 tcase_add_test (tc_chain, test_block_async);
967 tcase_add_test (tc_chain, test_block_async_replace_callback);
969 tcase_add_test (tc_chain, test_block_async_full_destroy);
970 tcase_add_test (tc_chain, test_block_async_full_destroy_dispose);
971 tcase_add_test (tc_chain, test_block_async_replace_callback_no_flush);
976 GST_CHECK_MAIN (gst_pad);