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);
173 GstPad *start_srcpad;
176 GstPad *final_sinkpad;
179 gboolean app_thread_prepped;
180 gboolean bufferalloc_blocked;
181 } BufferAllocHarness;
184 buffer_alloc_harness_setup (BufferAllocHarness * h, gint countdown)
186 h->tee = gst_check_setup_element ("tee");
187 fail_if (h->tee == NULL);
189 h->countdown = countdown;
191 fail_unless_equals_int (gst_element_set_state (h->tee, GST_STATE_PLAYING),
194 h->caps = gst_caps_new_simple ("video/x-raw-yuv", NULL);
196 h->start_srcpad = gst_pad_new ("src", GST_PAD_SRC);
197 fail_if (h->start_srcpad == NULL);
198 fail_unless (gst_pad_set_caps (h->start_srcpad, h->caps) == TRUE);
199 fail_unless (gst_pad_set_active (h->start_srcpad, TRUE) == TRUE);
201 h->tee_sinkpad = gst_element_get_static_pad (h->tee, "sink");
202 fail_if (h->tee_sinkpad == NULL);
204 h->tee_srcpad = gst_element_get_request_pad (h->tee, "src%d");
205 fail_if (h->tee_srcpad == NULL);
207 h->final_sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
208 fail_if (h->final_sinkpad == NULL);
209 fail_unless (gst_pad_set_caps (h->final_sinkpad, h->caps) == TRUE);
210 fail_unless (gst_pad_set_active (h->final_sinkpad, TRUE) == TRUE);
211 g_object_set_qdata (G_OBJECT (h->final_sinkpad),
212 g_quark_from_static_string ("buffer-alloc-harness"), h);
214 fail_unless_equals_int (gst_pad_link (h->start_srcpad, h->tee_sinkpad),
216 fail_unless_equals_int (gst_pad_link (h->tee_srcpad, h->final_sinkpad),
221 buffer_alloc_harness_teardown (BufferAllocHarness * h)
224 g_thread_join (h->app_thread);
226 gst_pad_set_active (h->final_sinkpad, FALSE);
227 gst_object_unref (h->final_sinkpad);
228 gst_object_unref (h->tee_srcpad);
229 gst_object_unref (h->tee_sinkpad);
230 gst_pad_set_active (h->start_srcpad, FALSE);
231 gst_object_unref (h->start_srcpad);
232 gst_caps_unref (h->caps);
233 gst_check_teardown_element (h->tee);
238 app_thread_func (gpointer data)
240 BufferAllocHarness *h = data;
242 /* Signal that we are about to call release_request_pad(). */
243 g_mutex_lock (check_mutex);
244 h->app_thread_prepped = TRUE;
245 g_cond_signal (check_cond);
246 g_mutex_unlock (check_mutex);
248 /* Simulate that the app releases the pad while the streaming thread is in
249 * buffer_alloc below. */
250 gst_element_release_request_pad (h->tee, h->tee_srcpad);
252 /* Signal the bufferalloc function below if it's still waiting. */
253 g_mutex_lock (check_mutex);
254 h->bufferalloc_blocked = FALSE;
255 g_cond_signal (check_cond);
256 g_mutex_unlock (check_mutex);
264 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
265 GstCaps * caps, GstBuffer ** buf)
267 BufferAllocHarness *h;
270 h = g_object_get_qdata (G_OBJECT (pad),
271 g_quark_from_static_string ("buffer-alloc-harness"));
272 g_assert (h != NULL);
274 if (--(h->countdown) == 0) {
275 /* Time to make the app release the pad. */
276 h->app_thread_prepped = FALSE;
277 h->bufferalloc_blocked = TRUE;
279 h->app_thread = g_thread_create (app_thread_func, h, TRUE, NULL);
280 fail_if (h->app_thread == NULL);
282 /* Wait for the app thread to get ready to call release_request_pad(). */
283 g_mutex_lock (check_mutex);
284 while (!h->app_thread_prepped)
285 g_cond_wait (check_cond, check_mutex);
286 g_mutex_unlock (check_mutex);
288 /* Now wait for it to do that within a second, to avoid deadlocking
289 * in the event of future changes to the locking semantics. */
290 g_mutex_lock (check_mutex);
291 g_get_current_time (&deadline);
292 deadline.tv_sec += 1;
293 while (h->bufferalloc_blocked) {
294 if (!g_cond_timed_wait (check_cond, check_mutex, &deadline))
297 g_mutex_unlock (check_mutex);
300 *buf = gst_buffer_new_and_alloc (size);
301 gst_buffer_set_caps (*buf, caps);
307 /* Simulate an app releasing the pad while the first alloc_buffer() is in
309 GST_START_TEST (test_release_while_buffer_alloc)
311 BufferAllocHarness h;
313 buffer_alloc_harness_setup (&h, 1);
315 buffer_alloc_harness_teardown (&h);
320 /* Simulate an app releasing the pad while the second alloc_buffer() is in
322 GST_START_TEST (test_release_while_second_buffer_alloc)
324 BufferAllocHarness h;
326 buffer_alloc_harness_setup (&h, 2);
328 buffer_alloc_harness_teardown (&h);
333 /* Check the internal pads of tee */
334 GST_START_TEST (test_internal_links)
337 GstPad *sinkpad, *srcpad1, *srcpad2;
339 GstIteratorResult res;
342 tee = gst_check_setup_element ("tee");
344 sinkpad = gst_element_get_static_pad (tee, "sink");
345 fail_unless (sinkpad != NULL);
346 it = gst_pad_iterate_internal_links (sinkpad);
347 fail_unless (it != NULL);
349 /* iterator should not return anything */
351 res = gst_iterator_next (it, &val1);
352 fail_unless (res == GST_ITERATOR_DONE);
353 fail_unless (val1 == NULL);
355 srcpad1 = gst_element_get_request_pad (tee, "src%d");
356 fail_unless (srcpad1 != NULL);
358 /* iterator should resync */
359 res = gst_iterator_next (it, &val1);
360 fail_unless (res == GST_ITERATOR_RESYNC);
361 fail_unless (val1 == NULL);
362 gst_iterator_resync (it);
364 /* we should get something now */
365 res = gst_iterator_next (it, &val1);
366 fail_unless (res == GST_ITERATOR_OK);
367 fail_unless (GST_PAD_CAST (val1) == srcpad1);
369 gst_object_unref (val1);
372 res = gst_iterator_next (it, &val1);
373 fail_unless (res == GST_ITERATOR_DONE);
374 fail_unless (val1 == NULL);
376 srcpad2 = gst_element_get_request_pad (tee, "src%d");
377 fail_unless (srcpad2 != NULL);
379 /* iterator should resync */
380 res = gst_iterator_next (it, &val1);
381 fail_unless (res == GST_ITERATOR_RESYNC);
382 fail_unless (val1 == NULL);
383 gst_iterator_resync (it);
385 /* we should get one of the 2 pads now */
386 res = gst_iterator_next (it, &val1);
387 fail_unless (res == GST_ITERATOR_OK);
388 fail_unless (GST_PAD_CAST (val1) == srcpad1
389 || GST_PAD_CAST (val1) == srcpad2);
392 res = gst_iterator_next (it, &val2);
393 fail_unless (res == GST_ITERATOR_OK);
394 fail_unless (GST_PAD_CAST (val2) == srcpad1
395 || GST_PAD_CAST (val2) == srcpad2);
396 fail_unless (val1 != val2);
397 gst_object_unref (val1);
398 gst_object_unref (val2);
401 res = gst_iterator_next (it, &val1);
402 fail_unless (res == GST_ITERATOR_DONE);
403 fail_unless (val1 == NULL);
405 gst_iterator_free (it);
407 /* get an iterator for the other direction */
408 it = gst_pad_iterate_internal_links (srcpad1);
409 fail_unless (it != NULL);
411 res = gst_iterator_next (it, &val1);
412 fail_unless (res == GST_ITERATOR_OK);
413 fail_unless (GST_PAD_CAST (val1) == sinkpad);
414 gst_object_unref (val1);
416 res = gst_iterator_next (it, &val1);
417 fail_unless (res == GST_ITERATOR_DONE);
418 gst_iterator_free (it);
420 it = gst_pad_iterate_internal_links (srcpad2);
421 fail_unless (it != NULL);
423 res = gst_iterator_next (it, &val1);
424 fail_unless (res == GST_ITERATOR_OK);
425 fail_unless (GST_PAD_CAST (val1) == sinkpad);
426 gst_object_unref (val1);
428 res = gst_iterator_next (it, &val1);
429 fail_unless (res == GST_ITERATOR_DONE);
431 gst_iterator_free (it);
432 gst_object_unref (srcpad1);
433 gst_object_unref (srcpad2);
434 gst_object_unref (sinkpad);
435 gst_object_unref (tee);
441 _fake_chain (GstPad * pad, GstBuffer * buffer)
443 gst_buffer_unref (buffer);
448 _fake_chain_error (GstPad * pad, GstBuffer * buffer)
450 gst_buffer_unref (buffer);
451 return GST_FLOW_ERROR;
454 GST_START_TEST (test_flow_aggregation)
456 GstPad *mysrc, *mysink1, *mysink2;
457 GstPad *teesink, *teesrc1, *teesrc2;
462 caps = gst_caps_new_simple ("test/test", NULL);
464 tee = gst_element_factory_make ("tee", NULL);
465 fail_unless (tee != NULL);
466 teesink = gst_element_get_static_pad (tee, "sink");
467 fail_unless (teesink != NULL);
468 teesrc1 = gst_element_get_request_pad (tee, "src%d");
469 fail_unless (teesrc1 != NULL);
470 teesrc2 = gst_element_get_request_pad (tee, "src%d");
471 fail_unless (teesrc2 != NULL);
473 mysink1 = gst_pad_new ("mysink1", GST_PAD_SINK);
474 gst_pad_set_caps (mysink1, caps);
475 mysink2 = gst_pad_new ("mysink2", GST_PAD_SINK);
476 gst_pad_set_caps (mysink2, caps);
477 mysrc = gst_pad_new ("mysrc", GST_PAD_SRC);
478 gst_pad_set_caps (mysrc, caps);
480 gst_pad_set_chain_function (mysink1, _fake_chain);
481 gst_pad_set_active (mysink1, TRUE);
482 gst_pad_set_chain_function (mysink2, _fake_chain);
483 gst_pad_set_active (mysink2, TRUE);
485 fail_unless (gst_pad_link (mysrc, teesink) == GST_PAD_LINK_OK);
486 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
487 fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
489 fail_unless (gst_element_set_state (tee,
490 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
492 buffer = gst_buffer_new ();
493 //gst_buffer_set_caps (buffer, caps);
495 /* First check if everything works in normal state */
496 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
498 /* One pad being in wrong state must result in wrong state */
499 gst_pad_set_active (mysink2, FALSE);
500 fail_unless (gst_pad_push (mysrc,
501 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
503 gst_pad_set_active (mysink1, FALSE);
504 gst_pad_set_active (mysink2, TRUE);
505 fail_unless (gst_pad_push (mysrc,
506 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
508 gst_pad_set_active (mysink2, FALSE);
509 fail_unless (gst_pad_push (mysrc,
510 gst_buffer_ref (buffer)) == GST_FLOW_WRONG_STATE);
512 /* Test if everything still works in normal state */
513 gst_pad_set_active (mysink1, TRUE);
514 gst_pad_set_active (mysink2, TRUE);
515 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
517 /* One unlinked pad must return OK, two unlinked pads must return NOT_LINKED */
518 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
519 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
521 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
522 fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
523 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
525 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
526 fail_unless (gst_pad_push (mysrc,
527 gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
529 /* Test if everything still works in normal state */
530 fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
531 fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
532 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
534 /* One pad returning ERROR should result in ERROR */
535 gst_pad_set_chain_function (mysink1, _fake_chain_error);
536 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
538 gst_pad_set_chain_function (mysink1, _fake_chain);
539 gst_pad_set_chain_function (mysink2, _fake_chain_error);
540 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
542 gst_pad_set_chain_function (mysink1, _fake_chain_error);
543 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
545 /* And now everything still needs to work */
546 gst_pad_set_chain_function (mysink1, _fake_chain);
547 gst_pad_set_chain_function (mysink2, _fake_chain);
548 fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
550 fail_unless (gst_element_set_state (tee,
551 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
553 fail_unless (gst_pad_unlink (mysrc, teesink) == TRUE);
554 fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
555 fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
558 gst_object_unref (teesink);
559 gst_object_unref (teesrc1);
560 gst_object_unref (teesrc2);
561 gst_element_release_request_pad (tee, teesrc1);
562 gst_element_release_request_pad (tee, teesrc2);
563 gst_object_unref (tee);
565 gst_object_unref (mysink1);
566 gst_object_unref (mysink2);
567 gst_object_unref (mysrc);
568 gst_caps_unref (caps);
569 gst_buffer_unref (buffer);
577 Suite *s = suite_create ("tee");
578 TCase *tc_chain = tcase_create ("general");
580 /* Set the timeout to a much larger time - 3 minutes */
581 tcase_set_timeout (tc_chain, 180);
583 suite_add_tcase (s, tc_chain);
584 tcase_add_test (tc_chain, test_num_buffers);
585 tcase_add_test (tc_chain, test_stress);
586 tcase_add_test (tc_chain, test_release_while_buffer_alloc);
587 tcase_add_test (tc_chain, test_release_while_second_buffer_alloc);
588 tcase_add_test (tc_chain, test_internal_links);
589 tcase_add_test (tc_chain, test_flow_aggregation);
594 GST_CHECK_MAIN (tee);