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>
29 static GstPad *mysrcpad;
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34 GST_STATIC_CAPS ("application/x-gst-check")
38 setup_multifdsink (void)
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)
110 sink = setup_multifdsink ();
112 fail_if (pipe (pfd) == -1);
114 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
117 g_signal_emit_by_name (sink, "add", pfd[1]);
119 caps = gst_caps_from_string ("application/x-gst-check");
120 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
121 buffer = gst_buffer_new_and_alloc (4);
122 gst_buffer_set_caps (buffer, caps);
123 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
124 memcpy (GST_BUFFER_DATA (buffer), "dead", 4);
125 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
127 GST_DEBUG ("reading");
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);
184 g_value_init (&array, GST_TYPE_ARRAY);
186 g_value_init (&value, GST_TYPE_BUFFER);
187 /* we take a copy, set it on the array (which refs it), then unref our copy */
188 buf = gst_buffer_copy (*hbuf1);
189 gst_value_set_buffer (&value, buf);
190 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
191 gst_buffer_unref (buf);
192 gst_value_array_append_value (&array, &value);
193 g_value_unset (&value);
195 g_value_init (&value, GST_TYPE_BUFFER);
196 buf = gst_buffer_copy (*hbuf2);
197 gst_value_set_buffer (&value, buf);
198 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
199 gst_buffer_unref (buf);
200 gst_value_array_append_value (&array, &value);
201 g_value_unset (&value);
203 *caps = gst_caps_from_string ("application/x-gst-check");
204 structure = gst_caps_get_structure (*caps, 0);
206 gst_structure_set_value (structure, "streamheader", &array);
207 g_value_unset (&array);
208 ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
210 /* set our streamheadery caps on the buffers */
211 gst_buffer_set_caps (*hbuf1, *caps);
212 gst_buffer_set_caps (*hbuf2, *caps);
213 ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 3);
215 /* we want to keep them around for the tests */
216 gst_buffer_ref (*hbuf1);
217 gst_buffer_ref (*hbuf2);
219 GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
224 * - adds a first client
225 * - sets streamheader caps on the pad
226 * - pushes the IN_CAPS buffers
228 * - verifies that the client received all the data correctly, and did not
229 * get multiple copies of the streamheader
230 * - adds a second client
231 * - verifies that this second client receives the streamheader caps too, plus
234 GST_START_TEST (test_streamheader)
237 GstBuffer *hbuf1, *hbuf2, *buf;
239 int pfd1[2], pfd2[2];
241 sink = setup_multifdsink ();
243 fail_if (pipe (pfd1) == -1);
244 fail_if (pipe (pfd2) == -1);
246 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
248 /* add the first client */
249 g_signal_emit_by_name (sink, "add", pfd1[1]);
251 /* create caps with streamheader, set the caps, and push the IN_CAPS
253 gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
255 fail_unless (gst_pad_set_caps (mysrcpad, caps));
256 /* one is ours, two on the buffers, and one now on the pad */
257 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
259 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
260 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
263 //fail_if_can_read ("first client", pfd1[0]);
265 /* push a non-IN_CAPS buffer, this should trigger the client receiving the
266 * first three buffers */
267 buf = gst_buffer_new_and_alloc (4);
268 memcpy (GST_BUFFER_DATA (buf), "f00d", 4);
269 gst_pad_push (mysrcpad, buf);
271 fail_unless_read ("first client", pfd1[0], 4, "babe");
272 fail_unless_read ("first client", pfd1[0], 8, "deadbeef");
273 fail_unless_read ("first client", pfd1[0], 4, "f00d");
274 wait_bytes_served (sink, 16);
276 /* now add the second client */
277 g_signal_emit_by_name (sink, "add", pfd2[1]);
279 //fail_if_can_read ("second client", pfd2[0]);
281 /* now push another buffer, which will trigger streamheader for second
283 buf = gst_buffer_new_and_alloc (4);
284 memcpy (GST_BUFFER_DATA (buf), "deaf", 4);
285 gst_pad_push (mysrcpad, buf);
287 fail_unless_read ("first client", pfd1[0], 4, "deaf");
289 fail_unless_read ("second client", pfd2[0], 4, "babe");
290 fail_unless_read ("second client", pfd2[0], 8, "deadbeef");
291 /* we missed the f00d buffer */
292 fail_unless_read ("second client", pfd2[0], 4, "deaf");
293 wait_bytes_served (sink, 36);
295 GST_DEBUG ("cleaning up multifdsink");
297 g_signal_emit_by_name (sink, "remove", pfd1[1]);
298 g_signal_emit_by_name (sink, "remove", pfd2[1]);
300 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
301 cleanup_multifdsink (sink);
303 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
304 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
305 gst_buffer_unref (hbuf1);
306 gst_buffer_unref (hbuf2);
308 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
309 gst_caps_unref (caps);
314 /* this tests changing of streamheaders
315 * - set streamheader caps on the pad
316 * - pushes the IN_CAPS buffers
318 * - add a first client
319 * - verifies that this first client receives the first streamheader caps,
321 * - change streamheader caps
322 * - verify that the first client receives the new streamheader buffers as well
324 GST_START_TEST (test_change_streamheader)
327 GstBuffer *hbuf1, *hbuf2, *buf;
329 int pfd1[2], pfd2[2];
331 sink = setup_multifdsink ();
333 fail_if (pipe (pfd1) == -1);
334 fail_if (pipe (pfd2) == -1);
336 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
338 /* create caps with streamheader, set the caps, and push the IN_CAPS
340 gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
342 fail_unless (gst_pad_set_caps (mysrcpad, caps));
343 /* one is ours, two on the buffers, and one now on the pad */
344 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
346 /* one to hold for the test and one to give away */
347 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
348 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
350 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
351 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
353 /* add the first client */
354 g_signal_emit_by_name (sink, "add", pfd1[1]);
356 /* verify this hasn't triggered a write yet */
357 /* FIXME: possibly racy, since if it would write, we may not get it
359 //fail_if_can_read ("first client, no buffer", pfd1[0]);
361 /* now push a buffer and read */
362 buf = gst_buffer_new_and_alloc (4);
363 memcpy (GST_BUFFER_DATA (buf), "f00d", 4);
364 gst_pad_push (mysrcpad, buf);
366 fail_unless_read ("change: first client", pfd1[0], 5, "first");
367 fail_unless_read ("change: first client", pfd1[0], 6, "header");
368 fail_unless_read ("change: first client", pfd1[0], 4, "f00d");
369 //wait_bytes_served (sink, 16);
371 /* now add the second client */
372 g_signal_emit_by_name (sink, "add", pfd2[1]);
373 //fail_if_can_read ("second client, no buffer", pfd2[0]);
375 /* change the streamheader */
377 /* before we change, multifdsink still has a list of the old streamheaders */
378 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
379 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
380 gst_buffer_unref (hbuf1);
381 gst_buffer_unref (hbuf2);
383 /* drop our ref to the previous caps */
384 gst_caps_unref (caps);
386 gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
388 fail_unless (gst_pad_set_caps (mysrcpad, caps));
389 /* one to hold for the test and one to give away */
390 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
391 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
393 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
394 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
396 /* verify neither client has new data available to read */
397 //fail_if_can_read ("first client, changed streamheader", pfd1[0]);
398 //fail_if_can_read ("second client, changed streamheader", pfd2[0]);
400 /* now push another buffer, which will trigger streamheader for second
401 * client, but should also send new streamheaders to first client */
402 buf = gst_buffer_new_and_alloc (8);
403 memcpy (GST_BUFFER_DATA (buf), "deadbabe", 8);
404 gst_pad_push (mysrcpad, buf);
406 fail_unless_read ("first client", pfd1[0], 6, "second");
407 fail_unless_read ("first client", pfd1[0], 6, "header");
408 fail_unless_read ("first client", pfd1[0], 8, "deadbabe");
410 /* new streamheader data */
411 fail_unless_read ("second client", pfd2[0], 6, "second");
412 fail_unless_read ("second client", pfd2[0], 6, "header");
413 /* we missed the f00d buffer */
414 fail_unless_read ("second client", pfd2[0], 8, "deadbabe");
415 //wait_bytes_served (sink, 36);
417 GST_DEBUG ("cleaning up multifdsink");
418 g_signal_emit_by_name (sink, "remove", pfd1[1]);
419 g_signal_emit_by_name (sink, "remove", pfd2[1]);
420 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
422 /* setting to NULL should have cleared the streamheader */
423 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
424 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
425 gst_buffer_unref (hbuf1);
426 gst_buffer_unref (hbuf2);
427 cleanup_multifdsink (sink);
429 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
430 gst_caps_unref (caps);
435 /* keep 100 bytes and burst 80 bytes to clients */
436 GST_START_TEST (test_burst_client_bytes)
446 guint buffers_queued;
448 sink = setup_multifdsink ();
449 /* make sure we keep at least 100 bytes at all times */
450 g_object_set (sink, "bytes-min", 100, NULL);
451 g_object_set (sink, "sync-method", 3, NULL); /* 3 = burst */
452 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
453 g_object_set (sink, "burst-value", (guint64) 80, NULL);
455 fail_if (pipe (pfd1) == -1);
456 fail_if (pipe (pfd2) == -1);
457 fail_if (pipe (pfd3) == -1);
459 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
461 caps = gst_caps_from_string ("application/x-gst-check");
462 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
464 /* push buffers in, 9 * 16 bytes = 144 bytes */
465 for (i = 0; i < 9; i++) {
468 buffer = gst_buffer_new_and_alloc (16);
469 gst_buffer_set_caps (buffer, caps);
472 data = (gchar *) GST_BUFFER_DATA (buffer);
473 g_snprintf (data, 16, "deadbee%08x", i);
475 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
478 /* check that at least 7 buffers (112 bytes) are in the queue */
479 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
480 fail_if (buffers_queued != 7);
482 /* now add the clients */
483 g_signal_emit_by_name (sink, "add", pfd1[1]);
484 g_signal_emit_by_name (sink, "add_full", pfd2[1], 3,
485 3, (guint64) 50, 3, (guint64) 200);
486 g_signal_emit_by_name (sink, "add_full", pfd3[1], 3,
487 3, (guint64) 50, 3, (guint64) 50);
489 /* push last buffer to make client fds ready for reading */
490 for (i = 9; i < 10; i++) {
493 buffer = gst_buffer_new_and_alloc (16);
494 gst_buffer_set_caps (buffer, caps);
497 data = (gchar *) GST_BUFFER_DATA (buffer);
498 g_snprintf (data, 16, "deadbee%08x", i);
500 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
503 /* now we should only read the last 5 buffers (5 * 16 = 80 bytes) */
504 GST_DEBUG ("Reading from client 1");
505 fail_if (read (pfd1[0], data, 16) < 16);
506 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
507 fail_if (read (pfd1[0], data, 16) < 16);
508 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
509 fail_if (read (pfd1[0], data, 16) < 16);
510 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
511 fail_if (read (pfd1[0], data, 16) < 16);
512 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
513 fail_if (read (pfd1[0], data, 16) < 16);
514 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
516 /* second client only bursts 50 bytes = 4 buffers (we get 4 buffers since
517 * the max alows it) */
518 GST_DEBUG ("Reading from client 2");
519 fail_if (read (pfd2[0], data, 16) < 16);
520 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
521 fail_if (read (pfd2[0], data, 16) < 16);
522 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
523 fail_if (read (pfd2[0], data, 16) < 16);
524 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
525 fail_if (read (pfd2[0], data, 16) < 16);
526 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
528 /* third client only bursts 50 bytes = 4 buffers, we can't send
529 * more than 50 bytes so we only get 3 buffers (48 bytes). */
530 GST_DEBUG ("Reading from client 3");
531 fail_if (read (pfd3[0], data, 16) < 16);
532 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
533 fail_if (read (pfd3[0], data, 16) < 16);
534 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
535 fail_if (read (pfd3[0], data, 16) < 16);
536 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
538 GST_DEBUG ("cleaning up multifdsink");
539 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
540 cleanup_multifdsink (sink);
542 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
543 gst_caps_unref (caps);
548 /* keep 100 bytes and burst 80 bytes to clients */
549 GST_START_TEST (test_burst_client_bytes_keyframe)
559 guint buffers_queued;
561 sink = setup_multifdsink ();
562 /* make sure we keep at least 100 bytes at all times */
563 g_object_set (sink, "bytes-min", 100, NULL);
564 g_object_set (sink, "sync-method", 4, NULL); /* 3 = burst_keyframe */
565 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
566 g_object_set (sink, "burst-value", (guint64) 80, NULL);
568 fail_if (pipe (pfd1) == -1);
569 fail_if (pipe (pfd2) == -1);
570 fail_if (pipe (pfd3) == -1);
572 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
574 caps = gst_caps_from_string ("application/x-gst-check");
575 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
577 /* push buffers in, 9 * 16 bytes = 144 bytes */
578 for (i = 0; i < 9; i++) {
581 buffer = gst_buffer_new_and_alloc (16);
582 gst_buffer_set_caps (buffer, caps);
584 /* mark most buffers as delta */
585 if (i != 0 && i != 4 && i != 8)
586 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
589 data = (gchar *) GST_BUFFER_DATA (buffer);
590 g_snprintf (data, 16, "deadbee%08x", i);
592 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
595 /* check that at least 7 buffers (112 bytes) are in the queue */
596 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
597 fail_if (buffers_queued != 7);
599 /* now add the clients */
600 g_signal_emit_by_name (sink, "add", pfd1[1]);
601 g_signal_emit_by_name (sink, "add_full", pfd2[1], 4,
602 3, (guint64) 50, 3, (guint64) 90);
603 g_signal_emit_by_name (sink, "add_full", pfd3[1], 4,
604 3, (guint64) 50, 3, (guint64) 50);
606 /* push last buffer to make client fds ready for reading */
607 for (i = 9; i < 10; i++) {
610 buffer = gst_buffer_new_and_alloc (16);
611 gst_buffer_set_caps (buffer, caps);
612 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
615 data = (gchar *) GST_BUFFER_DATA (buffer);
616 g_snprintf (data, 16, "deadbee%08x", i);
618 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
621 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
622 * keyframe at buffer 4 */
623 GST_DEBUG ("Reading from client 1");
624 fail_if (read (pfd1[0], data, 16) < 16);
625 fail_unless (strncmp (data, "deadbee00000004", 16) == 0);
626 fail_if (read (pfd1[0], data, 16) < 16);
627 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
628 fail_if (read (pfd1[0], data, 16) < 16);
629 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
630 fail_if (read (pfd1[0], data, 16) < 16);
631 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
632 fail_if (read (pfd1[0], data, 16) < 16);
633 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
634 fail_if (read (pfd1[0], data, 16) < 16);
635 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
637 /* second client only bursts 50 bytes = 4 buffers, there is
638 * no keyframe above min and below max, so get one below min */
639 GST_DEBUG ("Reading from client 2");
640 fail_if (read (pfd2[0], data, 16) < 16);
641 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
642 fail_if (read (pfd2[0], data, 16) < 16);
643 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
645 /* third client only bursts 50 bytes = 4 buffers, we can't send
646 * more than 50 bytes so we only get 2 buffers (32 bytes). */
647 GST_DEBUG ("Reading from client 3");
648 fail_if (read (pfd3[0], data, 16) < 16);
649 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
650 fail_if (read (pfd3[0], data, 16) < 16);
651 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
653 GST_DEBUG ("cleaning up multifdsink");
654 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
655 cleanup_multifdsink (sink);
657 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
658 gst_caps_unref (caps);
663 /* keep 100 bytes and burst 80 bytes to clients */
664 GST_START_TEST (test_burst_client_bytes_with_keyframe)
674 guint buffers_queued;
676 sink = setup_multifdsink ();
677 /* make sure we keep at least 100 bytes at all times */
678 g_object_set (sink, "bytes-min", 100, NULL);
679 g_object_set (sink, "sync-method", 5, NULL); /* 3 = burst_with_keyframe */
680 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
681 g_object_set (sink, "burst-value", (guint64) 80, NULL);
683 fail_if (pipe (pfd1) == -1);
684 fail_if (pipe (pfd2) == -1);
685 fail_if (pipe (pfd3) == -1);
687 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
689 caps = gst_caps_from_string ("application/x-gst-check");
690 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
692 /* push buffers in, 9 * 16 bytes = 144 bytes */
693 for (i = 0; i < 9; i++) {
696 buffer = gst_buffer_new_and_alloc (16);
697 gst_buffer_set_caps (buffer, caps);
699 /* mark most buffers as delta */
700 if (i != 0 && i != 4 && i != 8)
701 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
704 data = (gchar *) GST_BUFFER_DATA (buffer);
705 g_snprintf (data, 16, "deadbee%08x", i);
707 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
710 /* check that at least 7 buffers (112 bytes) are in the queue */
711 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
712 fail_if (buffers_queued != 7);
714 /* now add the clients */
715 g_signal_emit_by_name (sink, "add", pfd1[1]);
716 g_signal_emit_by_name (sink, "add_full", pfd2[1], 5,
717 3, (guint64) 50, 3, (guint64) 90);
718 g_signal_emit_by_name (sink, "add_full", pfd3[1], 5,
719 3, (guint64) 50, 3, (guint64) 50);
721 /* push last buffer to make client fds ready for reading */
722 for (i = 9; i < 10; i++) {
725 buffer = gst_buffer_new_and_alloc (16);
726 gst_buffer_set_caps (buffer, caps);
727 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
730 data = (gchar *) GST_BUFFER_DATA (buffer);
731 g_snprintf (data, 16, "deadbee%08x", i);
733 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
736 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
737 * keyframe at buffer 4 */
738 GST_DEBUG ("Reading from client 1");
739 fail_if (read (pfd1[0], data, 16) < 16);
740 fail_unless (strncmp (data, "deadbee00000004", 16) == 0);
741 fail_if (read (pfd1[0], data, 16) < 16);
742 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
743 fail_if (read (pfd1[0], data, 16) < 16);
744 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
745 fail_if (read (pfd1[0], data, 16) < 16);
746 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
747 fail_if (read (pfd1[0], data, 16) < 16);
748 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
749 fail_if (read (pfd1[0], data, 16) < 16);
750 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
752 /* second client only bursts 50 bytes = 4 buffers, there is
753 * no keyframe above min and below max, so send min */
754 GST_DEBUG ("Reading from client 2");
755 fail_if (read (pfd2[0], data, 16) < 16);
756 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
757 fail_if (read (pfd2[0], data, 16) < 16);
758 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
759 fail_if (read (pfd2[0], data, 16) < 16);
760 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
761 fail_if (read (pfd2[0], data, 16) < 16);
762 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
764 /* third client only bursts 50 bytes = 4 buffers, we can't send
765 * more than 50 bytes so we only get 3 buffers (48 bytes). */
766 GST_DEBUG ("Reading from client 3");
767 fail_if (read (pfd3[0], data, 16) < 16);
768 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
769 fail_if (read (pfd3[0], data, 16) < 16);
770 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
771 fail_if (read (pfd3[0], data, 16) < 16);
772 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
774 GST_DEBUG ("cleaning up multifdsink");
775 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
776 cleanup_multifdsink (sink);
778 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
779 gst_caps_unref (caps);
784 /* Check that we can get data when multifdsink is configured in next-keyframe
786 GST_START_TEST (test_client_next_keyframe)
795 sink = setup_multifdsink ();
796 g_object_set (sink, "sync-method", 1, NULL); /* 1 = next-keyframe */
798 fail_if (pipe (pfd1) == -1);
800 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
802 caps = gst_caps_from_string ("application/x-gst-check");
803 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
805 /* now add our client */
806 g_signal_emit_by_name (sink, "add", pfd1[1]);
808 /* push buffers in: keyframe, then non-keyframe */
809 for (i = 0; i < 2; i++) {
812 buffer = gst_buffer_new_and_alloc (16);
813 gst_buffer_set_caps (buffer, caps);
816 data = (gchar *) GST_BUFFER_DATA (buffer);
817 g_snprintf (data, 16, "deadbee%08x", i);
819 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
821 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
824 /* now we should be able to read some data */
825 GST_DEBUG ("Reading from client 1");
826 fail_if (read (pfd1[0], data, 16) < 16);
827 fail_unless (strncmp (data, "deadbee00000000", 16) == 0);
828 fail_if (read (pfd1[0], data, 16) < 16);
829 fail_unless (strncmp (data, "deadbee00000001", 16) == 0);
831 GST_DEBUG ("cleaning up multifdsink");
832 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
833 cleanup_multifdsink (sink);
835 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
836 gst_caps_unref (caps);
841 /* FIXME: add test simulating chained oggs where:
842 * sync-method is burst-on-connect
843 * (when multifdsink actually does burst-on-connect based on byte size, not
844 "last keyframe" which any frame for audio :))
845 * an old client still needs to read from before the new streamheaders
846 * a new client gets the new streamheaders
849 multifdsink_suite (void)
851 Suite *s = suite_create ("multifdsink");
852 TCase *tc_chain = tcase_create ("general");
854 suite_add_tcase (s, tc_chain);
855 tcase_add_test (tc_chain, test_no_clients);
856 tcase_add_test (tc_chain, test_add_client);
857 tcase_add_test (tc_chain, test_streamheader);
858 tcase_add_test (tc_chain, test_change_streamheader);
859 tcase_add_test (tc_chain, test_burst_client_bytes);
860 tcase_add_test (tc_chain, test_burst_client_bytes_keyframe);
861 tcase_add_test (tc_chain, test_burst_client_bytes_with_keyframe);
862 tcase_add_test (tc_chain, test_client_next_keyframe);
867 GST_CHECK_MAIN (multifdsink);