3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include <sys/ioctl.h>
23 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
24 #include <sys/filio.h>
27 #include <gst/check/gstcheck.h>
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34 GST_STATIC_CAPS ("application/x-gst-check")
40 GstElement *multifdsink;
42 GST_DEBUG ("setup_multifdsink");
43 multifdsink = gst_check_setup_element ("multifdsink");
44 mysrcpad = gst_check_setup_src_pad (multifdsink, &srctemplate, NULL);
50 cleanup_multifdsink (GstElement * multifdsink)
52 GST_DEBUG ("cleanup_multifdsink");
54 gst_check_teardown_src_pad (multifdsink);
55 gst_check_teardown_element (multifdsink);
59 wait_bytes_served (GstElement * sink, guint64 bytes)
61 guint64 bytes_served = 0;
63 while (bytes_served != bytes) {
64 g_object_get (sink, "bytes-served", &bytes_served, NULL);
68 /* FIXME: possibly racy, since if it would write, we may not get it
70 #define fail_if_can_read(msg,fd) \
74 fail_if (ioctl (fd, FIONREAD, &avail) < 0, "%s: could not ioctl", msg); \
75 fail_if (avail > 0, "%s: has bytes available to read"); \
79 GST_START_TEST (test_no_clients)
85 sink = setup_multifdsink ();
87 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
89 caps = gst_caps_from_string ("application/x-gst-check");
90 buffer = gst_buffer_new_and_alloc (4);
91 gst_buffer_set_caps (buffer, caps);
92 gst_caps_unref (caps);
93 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
95 GST_DEBUG ("cleaning up multifdsink");
96 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
97 cleanup_multifdsink (sink);
102 GST_START_TEST (test_add_client)
109 guint64 bytes_served;
111 sink = setup_multifdsink ();
113 fail_if (pipe (pfd) == -1);
115 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
118 g_signal_emit_by_name (sink, "add", pfd[1]);
120 caps = gst_caps_from_string ("application/x-gst-check");
121 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
122 buffer = gst_buffer_new_and_alloc (4);
123 gst_buffer_set_caps (buffer, caps);
124 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
125 memcpy (GST_BUFFER_DATA (buffer), "dead", 4);
126 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
128 fail_if (read (pfd[0], data, 4) < 4);
129 fail_unless (strncmp (data, "dead", 4) == 0);
130 wait_bytes_served (sink, 4);
132 GST_DEBUG ("cleaning up multifdsink");
133 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
134 cleanup_multifdsink (sink);
136 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
137 gst_caps_unref (caps);
142 #define fail_unless_read(msg,fd,size,ref) \
144 char data[size + 1]; \
147 GST_DEBUG ("%s: reading %d bytes", msg, size); \
148 nbytes = read (fd, data, size); \
150 GST_DEBUG ("%s: read %d bytes", msg, nbytes); \
151 fail_if (nbytes < size); \
152 fail_unless (memcmp (data, ref, size) == 0, \
153 "data read '%s' differs from '%s'", data, ref); \
156 /* from the given two data buffers, create two streamheader buffers and
157 * some caps that match it, and store them in the given pointers
158 * returns one ref to each of the buffers and the caps */
160 gst_multifdsink_create_streamheader (const gchar * data1,
161 const gchar * data2, GstBuffer ** hbuf1, GstBuffer ** hbuf2,
165 GValue array = { 0 };
166 GValue value = { 0 };
167 GstStructure *structure;
168 guint size1 = strlen (data1);
169 guint size2 = strlen (data2);
171 fail_if (hbuf1 == NULL);
172 fail_if (hbuf2 == NULL);
173 fail_if (caps == NULL);
175 /* create caps with streamheader, set the caps, and push the IN_CAPS
177 *hbuf1 = gst_buffer_new_and_alloc (size1);
178 GST_BUFFER_FLAG_SET (*hbuf1, GST_BUFFER_FLAG_IN_CAPS);
179 memcpy (GST_BUFFER_DATA (*hbuf1), data1, size1);
180 *hbuf2 = gst_buffer_new_and_alloc (size2);
181 GST_BUFFER_FLAG_SET (*hbuf2, GST_BUFFER_FLAG_IN_CAPS);
182 memcpy (GST_BUFFER_DATA (*hbuf2), data2, size2);
183 /* we want to keep them around for the tests */
184 gst_buffer_ref (*hbuf1);
185 gst_buffer_ref (*hbuf2);
187 g_value_init (&array, GST_TYPE_ARRAY);
189 g_value_init (&value, GST_TYPE_BUFFER);
190 /* we take a copy, set it on the array (which refs it), then unref our copy */
191 buf = gst_buffer_copy (*hbuf1);
192 gst_value_set_buffer (&value, buf);
193 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
194 gst_buffer_unref (buf);
195 gst_value_array_append_value (&array, &value);
196 g_value_unset (&value);
198 g_value_init (&value, GST_TYPE_BUFFER);
199 buf = gst_buffer_copy (*hbuf2);
200 gst_value_set_buffer (&value, buf);
201 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
202 gst_buffer_unref (buf);
203 gst_value_array_append_value (&array, &value);
204 g_value_unset (&value);
206 *caps = gst_caps_from_string ("application/x-gst-check");
207 structure = gst_caps_get_structure (*caps, 0);
209 gst_structure_set_value (structure, "streamheader", &array);
210 g_value_unset (&array);
211 ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
213 /* set our streamheadery caps on the buffers */
214 gst_buffer_set_caps (*hbuf1, *caps);
215 gst_buffer_set_caps (*hbuf2, *caps);
216 ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 3);
218 GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
223 * - adds a first client
224 * - sets streamheader caps on the pad
225 * - pushes the IN_CAPS buffers
227 * - verifies that the client received all the data correctly, and did not
228 * get multiple copies of the streamheader
229 * - adds a second client
230 * - verifies that this second client receives the streamheader caps too, plus
233 GST_START_TEST (test_streamheader)
236 GstBuffer *hbuf1, *hbuf2, *buf;
238 GstStructure *structure;
239 int pfd1[2], pfd2[2];
241 guint64 bytes_served;
244 sink = setup_multifdsink ();
246 fail_if (pipe (pfd1) == -1);
247 fail_if (pipe (pfd2) == -1);
249 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
251 /* add the first client */
252 g_signal_emit_by_name (sink, "add", pfd1[1]);
254 /* create caps with streamheader, set the caps, and push the IN_CAPS
256 gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
258 fail_unless (gst_pad_set_caps (mysrcpad, caps));
259 /* one is ours, two on the buffers, and one now on the pad */
260 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
262 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
263 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
266 //fail_if_can_read ("first client", pfd1[0]);
268 /* push a non-IN_CAPS buffer, this should trigger the client receiving the
269 * first three buffers */
270 buf = gst_buffer_new_and_alloc (4);
271 memcpy (GST_BUFFER_DATA (buf), "f00d", 4);
272 gst_pad_push (mysrcpad, buf);
274 fail_unless_read ("first client", pfd1[0], 4, "babe");
275 fail_unless_read ("first client", pfd1[0], 8, "deadbeef");
276 fail_unless_read ("first client", pfd1[0], 4, "f00d");
277 wait_bytes_served (sink, 16);
279 /* now add the second client */
280 g_signal_emit_by_name (sink, "add", pfd2[1]);
282 //fail_if_can_read ("second client", pfd2[0]);
284 /* now push another buffer, which will trigger streamheader for second
286 buf = gst_buffer_new_and_alloc (4);
287 memcpy (GST_BUFFER_DATA (buf), "deaf", 4);
288 gst_pad_push (mysrcpad, buf);
290 fail_unless_read ("first client", pfd1[0], 4, "deaf");
292 fail_unless_read ("second client", pfd2[0], 4, "babe");
293 fail_unless_read ("second client", pfd2[0], 8, "deadbeef");
294 /* we missed the f00d buffer */
295 fail_unless_read ("second client", pfd2[0], 4, "deaf");
296 wait_bytes_served (sink, 36);
298 GST_DEBUG ("cleaning up multifdsink");
300 g_signal_emit_by_name (sink, "remove", pfd1[1]);
301 g_signal_emit_by_name (sink, "remove", pfd2[1]);
303 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
304 cleanup_multifdsink (sink);
306 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
307 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
308 gst_buffer_unref (hbuf1);
309 gst_buffer_unref (hbuf2);
311 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
312 gst_caps_unref (caps);
317 /* this tests changing of streamheaders
318 * - set streamheader caps on the pad
319 * - pushes the IN_CAPS buffers
321 * - add a first client
322 * - verifies that this first client receives the first streamheader caps,
324 * - change streamheader caps
325 * - verify that the first client receives the new streamheader buffers as well
327 GST_START_TEST (test_change_streamheader)
330 GstBuffer *hbuf1, *hbuf2, *buf;
332 GstStructure *structure;
333 int pfd1[2], pfd2[2];
335 GValue array = { 0 };
336 GValue value = { 0 };
337 guint64 bytes_served;
340 sink = setup_multifdsink ();
342 fail_if (pipe (pfd1) == -1);
343 fail_if (pipe (pfd2) == -1);
345 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
347 /* create caps with streamheader, set the caps, and push the IN_CAPS
349 gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
351 fail_unless (gst_pad_set_caps (mysrcpad, caps));
352 /* one is ours, two on the buffers, and one now on the pad */
353 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
355 /* one to hold for the test and one to give away */
356 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
357 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
359 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
360 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
362 /* add the first client */
363 g_signal_emit_by_name (sink, "add", pfd1[1]);
365 /* verify this hasn't triggered a write yet */
366 /* FIXME: possibly racy, since if it would write, we may not get it
368 fail_if_can_read ("first client, no buffer", pfd1[0]);
370 /* now push a buffer and read */
371 buf = gst_buffer_new_and_alloc (4);
372 memcpy (GST_BUFFER_DATA (buf), "f00d", 4);
373 gst_pad_push (mysrcpad, buf);
375 fail_unless_read ("change: first client", pfd1[0], 5, "first");
376 fail_unless_read ("change: first client", pfd1[0], 6, "header");
377 fail_unless_read ("change: first client", pfd1[0], 4, "f00d");
378 //wait_bytes_served (sink, 16);
380 /* now add the second client */
381 g_signal_emit_by_name (sink, "add", pfd2[1]);
382 fail_if_can_read ("second client, no buffer", pfd2[0]);
384 /* change the streamheader */
386 /* before we change, multifdsink still has a list of the old streamheaders */
387 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
388 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
389 gst_buffer_unref (hbuf1);
390 gst_buffer_unref (hbuf2);
392 /* drop our ref to the previous caps */
393 gst_caps_unref (caps);
395 gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
397 fail_unless (gst_pad_set_caps (mysrcpad, caps));
398 /* one to hold for the test and one to give away */
399 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
400 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
402 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
403 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
405 /* verify neither client has new data available to read */
406 fail_if_can_read ("first client, changed streamheader", pfd1[0]);
407 fail_if_can_read ("second client, changed streamheader", pfd2[0]);
409 /* now push another buffer, which will trigger streamheader for second
410 * client, but should also send new streamheaders to first client */
411 buf = gst_buffer_new_and_alloc (8);
412 memcpy (GST_BUFFER_DATA (buf), "deadbabe", 8);
413 gst_pad_push (mysrcpad, buf);
415 fail_unless_read ("first client", pfd1[0], 6, "second");
416 fail_unless_read ("first client", pfd1[0], 6, "header");
417 fail_unless_read ("first client", pfd1[0], 8, "deadbabe");
419 /* new streamheader data */
420 fail_unless_read ("second client", pfd2[0], 6, "second");
421 fail_unless_read ("second client", pfd2[0], 6, "header");
422 /* we missed the f00d buffer */
423 fail_unless_read ("second client", pfd2[0], 8, "deadbabe");
424 //wait_bytes_served (sink, 36);
426 GST_DEBUG ("cleaning up multifdsink");
427 g_signal_emit_by_name (sink, "remove", pfd1[1]);
428 g_signal_emit_by_name (sink, "remove", pfd2[1]);
429 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
431 /* setting to NULL should have cleared the streamheader */
432 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
433 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
434 gst_buffer_unref (hbuf1);
435 gst_buffer_unref (hbuf2);
436 cleanup_multifdsink (sink);
438 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
439 gst_caps_unref (caps);
444 /* FIXME: add test simulating chained oggs where:
445 * sync-method is burst-on-connect
446 * (when multifdsink actually does burst-on-connect based on byte size, not
447 "last keyframe" which any frame for audio :))
448 * an old client still needs to read from before the new streamheaders
449 * a new client gets the new streamheaders
453 multifdsink_suite (void)
455 Suite *s = suite_create ("multifdsink");
456 TCase *tc_chain = tcase_create ("general");
458 suite_add_tcase (s, tc_chain);
459 tcase_add_test (tc_chain, test_no_clients);
460 tcase_add_test (tc_chain, test_add_client);
461 tcase_add_test (tc_chain, test_streamheader);
462 tcase_add_test (tc_chain, test_change_streamheader);
468 main (int argc, char **argv)
472 Suite *s = multifdsink_suite ();
473 SRunner *sr = srunner_create (s);
475 gst_check_init (&argc, &argv);
477 srunner_run_all (sr, CK_NORMAL);
478 nf = srunner_ntests_failed (sr);