5 * Copyright (C) <2007> Wim Taymans <wim dot taymans at gmail dot com>
6 * Copyright (C) <2008> Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
7 * Copyright (C) <2008> Christian Berentsen <christian.berentsen@tandberg.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <gst/check/gstcheck.h>
33 handoff (GstElement * fakesink, GstBuffer * buf, GstPad * pad, guint * count)
38 /* construct fakesrc num-buffers=3 ! tee name=t ! queue ! fakesink t. ! queue !
39 * fakesink. Each fakesink should exactly receive 3 buffers.
41 GST_START_TEST (test_num_buffers)
43 #define NUM_SUBSTREAMS 15
45 GstElement *pipeline, *src, *tee;
46 GstElement *queues[NUM_SUBSTREAMS];
47 GstElement *sinks[NUM_SUBSTREAMS];
48 GstPad *req_pads[NUM_SUBSTREAMS];
49 guint counts[NUM_SUBSTREAMS];
54 pipeline = gst_pipeline_new ("pipeline");
55 src = gst_check_setup_element ("fakesrc");
56 g_object_set (src, "num-buffers", NUM_BUFFERS, NULL);
57 tee = gst_check_setup_element ("tee");
58 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
59 fail_unless (gst_bin_add (GST_BIN (pipeline), tee));
60 fail_unless (gst_element_link (src, tee));
62 for (i = 0; i < NUM_SUBSTREAMS; ++i) {
68 queues[i] = gst_check_setup_element ("queue");
69 g_snprintf (name, 32, "queue%d", i);
70 gst_object_set_name (GST_OBJECT (queues[i]), name);
71 fail_unless (gst_bin_add (GST_BIN (pipeline), queues[i]));
73 sinks[i] = gst_check_setup_element ("fakesink");
74 g_snprintf (name, 32, "sink%d", i);
75 gst_object_set_name (GST_OBJECT (sinks[i]), name);
76 fail_unless (gst_bin_add (GST_BIN (pipeline), sinks[i]));
77 fail_unless (gst_element_link (queues[i], sinks[i]));
78 g_object_set (sinks[i], "signal-handoffs", TRUE, NULL);
79 g_signal_connect (sinks[i], "handoff", (GCallback) handoff, &counts[i]);
81 req_pads[i] = gst_element_get_request_pad (tee, "src%d");
82 fail_unless (req_pads[i] != NULL);
84 qpad = gst_element_get_static_pad (queues[i], "sink");
85 fail_unless_equals_int (gst_pad_link (req_pads[i], qpad), GST_PAD_LINK_OK);
86 gst_object_unref (qpad);
89 bus = gst_element_get_bus (pipeline);
90 fail_if (bus == NULL);
91 gst_element_set_state (pipeline, GST_STATE_PLAYING);
93 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
94 fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
95 gst_message_unref (msg);
97 for (i = 0; i < NUM_SUBSTREAMS; ++i) {
98 fail_unless_equals_int (counts[i], NUM_BUFFERS);
101 gst_element_set_state (pipeline, GST_STATE_NULL);
102 gst_object_unref (bus);
104 for (i = 0; i < NUM_SUBSTREAMS; ++i) {
105 gst_element_release_request_pad (tee, req_pads[i]);
106 gst_object_unref (req_pads[i]);
108 gst_object_unref (pipeline);
113 /* we use fakesrc ! tee ! fakesink and then randomly request/release and link
114 * some pads from tee. This should happily run without any errors. */
115 GST_START_TEST (test_stress)
117 GstElement *pipeline;
124 /* Pump 1000 buffers (10 bytes each) per second through tee for 5 secs */
125 desc = "fakesrc datarate=10000 sizemin=10 sizemax=10 num-buffers=5000 ! "
126 "video/x-raw-rgb,framerate=25/1 ! tee name=t ! "
127 "queue max-size-buffers=2 ! fakesink sync=true";
129 pipeline = gst_parse_launch (desc, NULL);
130 fail_if (pipeline == NULL);
132 tee = gst_bin_get_by_name (GST_BIN (pipeline), "t");
133 fail_if (tee == NULL);
135 /* bring the pipeline to PLAYING, then start switching */
136 bus = gst_element_get_bus (pipeline);
137 fail_if (bus == NULL);
138 gst_element_set_state (pipeline, GST_STATE_PLAYING);
139 /* Wait for the pipeline to hit playing so that parse_launch can do the
140 * initial link, otherwise we perform linking from multiple threads and cause
142 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
144 for (i = 0; i < 50000; i++) {
147 pad = gst_element_get_request_pad (tee, "src%d");
148 gst_element_release_request_pad (tee, pad);
149 gst_object_unref (pad);
151 if ((msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, 0)))
155 /* now wait for completion or error */
157 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
158 fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
159 gst_message_unref (msg);
161 gst_element_set_state (pipeline, GST_STATE_NULL);
162 gst_object_unref (tee);
163 gst_object_unref (bus);
164 gst_object_unref (pipeline);
170 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
171 GstCaps * caps, GstBuffer ** buf);
177 GstPad *start_srcpad;
180 GstPad *final_sinkpad;
183 gboolean app_thread_prepped;
184 gboolean bufferalloc_blocked;
185 } BufferAllocHarness;
188 buffer_alloc_harness_setup (BufferAllocHarness * h, gint countdown)
190 h->tee = gst_check_setup_element ("tee");
191 fail_if (h->tee == NULL);
193 h->countdown = countdown;
195 fail_unless_equals_int (gst_element_set_state (h->tee, GST_STATE_PLAYING),
198 h->caps = gst_caps_new_simple ("video/x-raw-yuv", NULL);
200 h->start_srcpad = gst_pad_new ("src", GST_PAD_SRC);
201 fail_if (h->start_srcpad == NULL);
202 fail_unless (gst_pad_set_caps (h->start_srcpad, h->caps) == TRUE);
203 fail_unless (gst_pad_set_active (h->start_srcpad, TRUE) == TRUE);
205 h->tee_sinkpad = gst_element_get_static_pad (h->tee, "sink");
206 fail_if (h->tee_sinkpad == NULL);
208 h->tee_srcpad = gst_element_get_request_pad (h->tee, "src%d");
209 fail_if (h->tee_srcpad == NULL);
211 h->final_sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
212 fail_if (h->final_sinkpad == NULL);
213 gst_pad_set_bufferalloc_function (h->final_sinkpad,
214 final_sinkpad_bufferalloc);
215 fail_unless (gst_pad_set_caps (h->final_sinkpad, h->caps) == TRUE);
216 fail_unless (gst_pad_set_active (h->final_sinkpad, TRUE) == TRUE);
217 g_object_set_qdata (G_OBJECT (h->final_sinkpad),
218 g_quark_from_static_string ("buffer-alloc-harness"), h);
220 fail_unless_equals_int (gst_pad_link (h->start_srcpad, h->tee_sinkpad),
222 fail_unless_equals_int (gst_pad_link (h->tee_srcpad, h->final_sinkpad),
227 buffer_alloc_harness_teardown (BufferAllocHarness * h)
229 g_thread_join (h->app_thread);
231 gst_pad_set_active (h->final_sinkpad, FALSE);
232 gst_object_unref (h->final_sinkpad);
233 gst_object_unref (h->tee_srcpad);
234 gst_object_unref (h->tee_sinkpad);
235 gst_pad_set_active (h->start_srcpad, FALSE);
236 gst_object_unref (h->start_srcpad);
237 gst_caps_unref (h->caps);
238 gst_check_teardown_element (h->tee);
242 app_thread_func (gpointer data)
244 BufferAllocHarness *h = data;
246 /* Signal that we are about to call release_request_pad(). */
247 g_mutex_lock (check_mutex);
248 h->app_thread_prepped = TRUE;
249 g_cond_signal (check_cond);
250 g_mutex_unlock (check_mutex);
252 /* Simulate that the app releases the pad while the streaming thread is in
253 * buffer_alloc below. */
254 gst_element_release_request_pad (h->tee, h->tee_srcpad);
256 /* Signal the bufferalloc function below if it's still waiting. */
257 g_mutex_lock (check_mutex);
258 h->bufferalloc_blocked = FALSE;
259 g_cond_signal (check_cond);
260 g_mutex_unlock (check_mutex);
266 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
267 GstCaps * caps, GstBuffer ** buf)
269 BufferAllocHarness *h;
272 h = g_object_get_qdata (G_OBJECT (pad),
273 g_quark_from_static_string ("buffer-alloc-harness"));
274 g_assert (h != NULL);
276 if (--(h->countdown) == 0) {
277 /* Time to make the app release the pad. */
278 h->app_thread_prepped = FALSE;
279 h->bufferalloc_blocked = TRUE;
281 h->app_thread = g_thread_create (app_thread_func, h, TRUE, NULL);
282 fail_if (h->app_thread == NULL);
284 /* Wait for the app thread to get ready to call release_request_pad(). */
285 g_mutex_lock (check_mutex);
286 while (!h->app_thread_prepped)
287 g_cond_wait (check_cond, check_mutex);
288 g_mutex_unlock (check_mutex);
290 /* Now wait for it to do that within a second, to avoid deadlocking
291 * in the event of future changes to the locking semantics. */
292 g_mutex_lock (check_mutex);
293 g_get_current_time (&deadline);
294 deadline.tv_sec += 1;
295 while (h->bufferalloc_blocked) {
296 if (!g_cond_timed_wait (check_cond, check_mutex, &deadline))
299 g_mutex_unlock (check_mutex);
302 *buf = gst_buffer_new_and_alloc (size);
303 gst_buffer_set_caps (*buf, caps);
308 /* Simulate an app releasing the pad while the first alloc_buffer() is in
310 GST_START_TEST (test_release_while_buffer_alloc)
312 BufferAllocHarness h;
315 buffer_alloc_harness_setup (&h, 1);
317 fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
319 gst_buffer_unref (buf);
321 buffer_alloc_harness_teardown (&h);
326 /* Simulate an app releasing the pad while the second alloc_buffer() is in
328 GST_START_TEST (test_release_while_second_buffer_alloc)
330 BufferAllocHarness h;
333 buffer_alloc_harness_setup (&h, 2);
335 fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
337 gst_buffer_unref (buf);
339 fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
341 gst_buffer_unref (buf);
343 buffer_alloc_harness_teardown (&h);
348 /* Check the internal pads of tee */
349 GST_START_TEST (test_internal_links)
352 GstPad *sinkpad, *srcpad1, *srcpad2;
354 GstIteratorResult res;
357 tee = gst_check_setup_element ("tee");
359 sinkpad = gst_element_get_static_pad (tee, "sink");
360 fail_unless (sinkpad != NULL);
361 it = gst_pad_iterate_internal_links (sinkpad);
362 fail_unless (it != NULL);
364 /* iterator should not return anything */
366 res = gst_iterator_next (it, &val1);
367 fail_unless (res == GST_ITERATOR_DONE);
368 fail_unless (val1 == NULL);
370 srcpad1 = gst_element_get_request_pad (tee, "src%d");
371 fail_unless (srcpad1 != NULL);
373 /* iterator should resync */
374 res = gst_iterator_next (it, &val1);
375 fail_unless (res == GST_ITERATOR_RESYNC);
376 fail_unless (val1 == NULL);
377 gst_iterator_resync (it);
379 /* we should get something now */
380 res = gst_iterator_next (it, &val1);
381 fail_unless (res == GST_ITERATOR_OK);
382 fail_unless (GST_PAD_CAST (val1) == srcpad1);
384 gst_object_unref (val1);
387 res = gst_iterator_next (it, &val1);
388 fail_unless (res == GST_ITERATOR_DONE);
389 fail_unless (val1 == NULL);
391 srcpad2 = gst_element_get_request_pad (tee, "src%d");
392 fail_unless (srcpad2 != NULL);
394 /* iterator should resync */
395 res = gst_iterator_next (it, &val1);
396 fail_unless (res == GST_ITERATOR_RESYNC);
397 fail_unless (val1 == NULL);
398 gst_iterator_resync (it);
400 /* we should get one of the 2 pads now */
401 res = gst_iterator_next (it, &val1);
402 fail_unless (res == GST_ITERATOR_OK);
403 fail_unless (GST_PAD_CAST (val1) == srcpad1
404 || GST_PAD_CAST (val1) == srcpad2);
407 res = gst_iterator_next (it, &val2);
408 fail_unless (res == GST_ITERATOR_OK);
409 fail_unless (GST_PAD_CAST (val2) == srcpad1
410 || GST_PAD_CAST (val2) == srcpad2);
411 fail_unless (val1 != val2);
412 gst_object_unref (val1);
413 gst_object_unref (val2);
416 res = gst_iterator_next (it, &val1);
417 fail_unless (res == GST_ITERATOR_DONE);
418 fail_unless (val1 == NULL);
420 gst_iterator_free (it);
422 /* get an iterator for the other direction */
423 it = gst_pad_iterate_internal_links (srcpad1);
424 fail_unless (it != NULL);
426 res = gst_iterator_next (it, &val1);
427 fail_unless (res == GST_ITERATOR_OK);
428 fail_unless (GST_PAD_CAST (val1) == sinkpad);
429 gst_object_unref (val1);
431 res = gst_iterator_next (it, &val1);
432 fail_unless (res == GST_ITERATOR_DONE);
433 gst_iterator_free (it);
435 it = gst_pad_iterate_internal_links (srcpad2);
436 fail_unless (it != NULL);
438 res = gst_iterator_next (it, &val1);
439 fail_unless (res == GST_ITERATOR_OK);
440 fail_unless (GST_PAD_CAST (val1) == sinkpad);
441 gst_object_unref (val1);
443 res = gst_iterator_next (it, &val1);
444 fail_unless (res == GST_ITERATOR_DONE);
446 gst_iterator_free (it);
447 gst_object_unref (srcpad1);
448 gst_object_unref (srcpad2);
449 gst_object_unref (sinkpad);
450 gst_object_unref (tee);
456 _fake_chain (GstPad * pad, GstBuffer * buffer)
458 gst_buffer_unref (buffer);
463 _fake_chain_error (GstPad * pad, GstBuffer * buffer)
465 gst_buffer_unref (buffer);
466 return GST_FLOW_ERROR;
469 GST_START_TEST (test_flow_aggregation)
471 GstPad *mysrc, *mysink1, *mysink2;
472 GstPad *teesink, *teesrc1, *teesrc2;
477 caps = gst_caps_new_simple ("test/test", NULL);
479 tee = gst_element_factory_make ("tee", NULL);
480 fail_unless (tee != NULL);
481 teesink = gst_element_get_static_pad (tee, "sink");
482 fail_unless (teesink != NULL);
483 teesrc1 = gst_element_get_request_pad (tee, "src%d");
484 fail_unless (teesrc1 != NULL);
485 teesrc2 = gst_element_get_request_pad (tee, "src%d");
486 fail_unless (teesrc2 != NULL);
488 mysink1 = gst_pad_new ("mysink1", GST_PAD_SINK);
489 gst_pad_set_caps (mysink1, caps);
490 mysink2 = gst_pad_new ("mysink2", GST_PAD_SINK);
491 gst_pad_set_caps (mysink2, caps);
492 mysrc = gst_pad_new ("mysrc", GST_PAD_SRC);
493 gst_pad_set_caps (mysrc, caps);
495 gst_pad_set_chain_function (mysink1, _fake_chain);
496 gst_pad_set_active (mysink1, TRUE);
497 gst_pad_set_chain_function (mysink2, _fake_chain);
498 gst_pad_set_active (mysink2, TRUE);
500 fail_unless (gst_pad_link (mysrc, teesink) == GST_PAD_LINK_OK);
501 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
502 fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
504 fail_unless (gst_element_set_state (tee,
505 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
507 buffer = gst_buffer_new ();
508 gst_buffer_set_caps (buffer, caps);
510 /* First check if everything works in normal state */
511 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
513 /* One pad being in wrong state must result in wrong state */
514 gst_pad_set_active (mysink2, FALSE);
515 fail_unless (gst_pad_push (mysrc,
516 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
518 gst_pad_set_active (mysink1, FALSE);
519 gst_pad_set_active (mysink2, TRUE);
520 fail_unless (gst_pad_push (mysrc,
521 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
523 gst_pad_set_active (mysink2, FALSE);
524 fail_unless (gst_pad_push (mysrc,
525 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
527 /* Test if everything still works in normal state */
528 gst_pad_set_active (mysink1, TRUE);
529 gst_pad_set_active (mysink2, TRUE);
530 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
532 /* One unlinked pad must return OK, two unlinked pads must return NOT_LINKED */
533 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
534 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
536 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
537 fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
538 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
540 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
541 fail_unless (gst_pad_push (mysrc,
542 gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
544 /* Test if everything still works in normal state */
545 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
546 fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
547 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
549 /* One pad returning ERROR should result in ERROR */
550 gst_pad_set_chain_function (mysink1, _fake_chain_error);
551 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
553 gst_pad_set_chain_function (mysink1, _fake_chain);
554 gst_pad_set_chain_function (mysink2, _fake_chain_error);
555 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
557 gst_pad_set_chain_function (mysink1, _fake_chain_error);
558 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
560 /* And now everything still needs to work */
561 gst_pad_set_chain_function (mysink1, _fake_chain);
562 gst_pad_set_chain_function (mysink2, _fake_chain);
563 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
565 fail_unless (gst_element_set_state (tee,
566 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
568 fail_unless (gst_pad_unlink (mysrc, teesink) == TRUE);
569 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
570 fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
573 gst_object_unref (teesink);
574 gst_object_unref (teesrc1);
575 gst_object_unref (teesrc2);
576 gst_element_release_request_pad (tee, teesrc1);
577 gst_element_release_request_pad (tee, teesrc2);
578 gst_object_unref (tee);
580 gst_object_unref (mysink1);
581 gst_object_unref (mysink2);
582 gst_object_unref (mysrc);
583 gst_caps_unref (caps);
584 gst_buffer_unref (buffer);
592 Suite *s = suite_create ("tee");
593 TCase *tc_chain = tcase_create ("general");
595 /* Set the timeout to a much larger time - 3 minutes */
596 tcase_set_timeout (tc_chain, 180);
598 suite_add_tcase (s, tc_chain);
599 tcase_add_test (tc_chain, test_num_buffers);
600 tcase_add_test (tc_chain, test_stress);
601 tcase_add_test (tc_chain, test_release_while_buffer_alloc);
602 tcase_add_test (tc_chain, test_release_while_second_buffer_alloc);
603 tcase_add_test (tc_chain, test_internal_links);
604 tcase_add_test (tc_chain, test_flow_aggregation);
609 GST_CHECK_MAIN (tee);