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);
351 Suite *s = suite_create ("tee");
352 TCase *tc_chain = tcase_create ("general");
354 suite_add_tcase (s, tc_chain);
355 tcase_add_test (tc_chain, test_num_buffers);
356 tcase_add_test (tc_chain, test_stress);
357 tcase_add_test (tc_chain, test_release_while_buffer_alloc);
358 tcase_add_test (tc_chain, test_release_while_second_buffer_alloc);
363 GST_CHECK_MAIN (tee);