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);
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_pad_set_caps (mysrcpad, 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_pad_set_caps (mysrcpad, caps);
123 ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
124 gst_buffer_fill (buffer, 0, "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 gst_buffer_fill (*hbuf1, 0, data1, size1);
180 *hbuf2 = gst_buffer_new_and_alloc (size2);
181 GST_BUFFER_FLAG_SET (*hbuf2, GST_BUFFER_FLAG_IN_CAPS);
182 gst_buffer_fill (*hbuf2, 0, 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 /* we want to keep them around for the tests */
211 gst_buffer_ref (*hbuf1);
212 gst_buffer_ref (*hbuf2);
214 GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
219 * - adds a first client
220 * - sets streamheader caps on the pad
221 * - pushes the IN_CAPS buffers
223 * - verifies that the client received all the data correctly, and did not
224 * get multiple copies of the streamheader
225 * - adds a second client
226 * - verifies that this second client receives the streamheader caps too, plus
229 GST_START_TEST (test_streamheader)
232 GstBuffer *hbuf1, *hbuf2, *buf;
234 int pfd1[2], pfd2[2];
236 sink = setup_multifdsink ();
238 fail_if (pipe (pfd1) == -1);
239 fail_if (pipe (pfd2) == -1);
241 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
243 /* add the first client */
244 g_signal_emit_by_name (sink, "add", pfd1[1]);
246 /* create caps with streamheader, set the caps, and push the IN_CAPS
248 gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
250 fail_unless (gst_pad_set_caps (mysrcpad, caps));
251 /* one is ours, two on the buffers, and one now on the pad */
252 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
254 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
255 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
258 //fail_if_can_read ("first client", pfd1[0]);
260 /* push a non-IN_CAPS buffer, this should trigger the client receiving the
261 * first three buffers */
262 buf = gst_buffer_new_and_alloc (4);
263 gst_buffer_fill (buf, 0, "f00d", 4);
264 gst_pad_push (mysrcpad, buf);
266 fail_unless_read ("first client", pfd1[0], 4, "babe");
267 fail_unless_read ("first client", pfd1[0], 8, "deadbeef");
268 fail_unless_read ("first client", pfd1[0], 4, "f00d");
269 wait_bytes_served (sink, 16);
271 /* now add the second client */
272 g_signal_emit_by_name (sink, "add", pfd2[1]);
274 //fail_if_can_read ("second client", pfd2[0]);
276 /* now push another buffer, which will trigger streamheader for second
278 buf = gst_buffer_new_and_alloc (4);
279 gst_buffer_fill (buf, 0, "deaf", 4);
280 gst_pad_push (mysrcpad, buf);
282 fail_unless_read ("first client", pfd1[0], 4, "deaf");
284 fail_unless_read ("second client", pfd2[0], 4, "babe");
285 fail_unless_read ("second client", pfd2[0], 8, "deadbeef");
286 /* we missed the f00d buffer */
287 fail_unless_read ("second client", pfd2[0], 4, "deaf");
288 wait_bytes_served (sink, 36);
290 GST_DEBUG ("cleaning up multifdsink");
292 g_signal_emit_by_name (sink, "remove", pfd1[1]);
293 g_signal_emit_by_name (sink, "remove", pfd2[1]);
295 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
296 cleanup_multifdsink (sink);
298 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
299 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
300 gst_buffer_unref (hbuf1);
301 gst_buffer_unref (hbuf2);
303 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
304 gst_caps_unref (caps);
309 /* this tests changing of streamheaders
310 * - set streamheader caps on the pad
311 * - pushes the IN_CAPS buffers
313 * - add a first client
314 * - verifies that this first client receives the first streamheader caps,
316 * - change streamheader caps
317 * - verify that the first client receives the new streamheader buffers as well
319 GST_START_TEST (test_change_streamheader)
322 GstBuffer *hbuf1, *hbuf2, *buf;
324 int pfd1[2], pfd2[2];
326 sink = setup_multifdsink ();
328 fail_if (pipe (pfd1) == -1);
329 fail_if (pipe (pfd2) == -1);
331 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
333 /* create caps with streamheader, set the caps, and push the IN_CAPS
335 gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
337 fail_unless (gst_pad_set_caps (mysrcpad, caps));
338 /* one is ours, two on the buffers, and one now on the pad */
339 ASSERT_CAPS_REFCOUNT (caps, "caps", 4);
341 /* one to hold for the test and one to give away */
342 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
343 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
345 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
346 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
348 /* add the first client */
349 g_signal_emit_by_name (sink, "add", pfd1[1]);
351 /* verify this hasn't triggered a write yet */
352 /* FIXME: possibly racy, since if it would write, we may not get it
354 //fail_if_can_read ("first client, no buffer", pfd1[0]);
356 /* now push a buffer and read */
357 buf = gst_buffer_new_and_alloc (4);
358 gst_buffer_fill (buf, 0, "f00d", 4);
359 gst_pad_push (mysrcpad, buf);
361 fail_unless_read ("change: first client", pfd1[0], 5, "first");
362 fail_unless_read ("change: first client", pfd1[0], 6, "header");
363 fail_unless_read ("change: first client", pfd1[0], 4, "f00d");
364 //wait_bytes_served (sink, 16);
366 /* now add the second client */
367 g_signal_emit_by_name (sink, "add", pfd2[1]);
368 //fail_if_can_read ("second client, no buffer", pfd2[0]);
370 /* change the streamheader */
372 /* before we change, multifdsink still has a list of the old streamheaders */
373 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
374 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
375 gst_buffer_unref (hbuf1);
376 gst_buffer_unref (hbuf2);
378 /* drop our ref to the previous caps */
379 gst_caps_unref (caps);
381 gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
383 fail_unless (gst_pad_set_caps (mysrcpad, caps));
384 /* one to hold for the test and one to give away */
385 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
386 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
388 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
389 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
391 /* verify neither client has new data available to read */
392 //fail_if_can_read ("first client, changed streamheader", pfd1[0]);
393 //fail_if_can_read ("second client, changed streamheader", pfd2[0]);
395 /* now push another buffer, which will trigger streamheader for second
396 * client, but should also send new streamheaders to first client */
397 buf = gst_buffer_new_and_alloc (8);
398 gst_buffer_fill (buf, 0, "deadbabe", 8);
399 gst_pad_push (mysrcpad, buf);
401 fail_unless_read ("first client", pfd1[0], 6, "second");
402 fail_unless_read ("first client", pfd1[0], 6, "header");
403 fail_unless_read ("first client", pfd1[0], 8, "deadbabe");
405 /* new streamheader data */
406 fail_unless_read ("second client", pfd2[0], 6, "second");
407 fail_unless_read ("second client", pfd2[0], 6, "header");
408 /* we missed the f00d buffer */
409 fail_unless_read ("second client", pfd2[0], 8, "deadbabe");
410 //wait_bytes_served (sink, 36);
412 GST_DEBUG ("cleaning up multifdsink");
413 g_signal_emit_by_name (sink, "remove", pfd1[1]);
414 g_signal_emit_by_name (sink, "remove", pfd2[1]);
415 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
417 /* setting to NULL should have cleared the streamheader */
418 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
419 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
420 gst_buffer_unref (hbuf1);
421 gst_buffer_unref (hbuf2);
422 cleanup_multifdsink (sink);
424 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
425 gst_caps_unref (caps);
430 /* keep 100 bytes and burst 80 bytes to clients */
431 GST_START_TEST (test_burst_client_bytes)
441 guint buffers_queued;
443 sink = setup_multifdsink ();
444 /* make sure we keep at least 100 bytes at all times */
445 g_object_set (sink, "bytes-min", 100, NULL);
446 g_object_set (sink, "sync-method", 3, NULL); /* 3 = burst */
447 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
448 g_object_set (sink, "burst-value", (guint64) 80, NULL);
450 fail_if (pipe (pfd1) == -1);
451 fail_if (pipe (pfd2) == -1);
452 fail_if (pipe (pfd3) == -1);
454 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
456 caps = gst_caps_from_string ("application/x-gst-check");
457 gst_pad_set_caps (mysrcpad, caps);
458 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
460 /* push buffers in, 9 * 16 bytes = 144 bytes */
461 for (i = 0; i < 9; i++) {
464 buffer = gst_buffer_new_and_alloc (16);
467 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
468 g_snprintf (data, 16, "deadbee%08x", i);
469 gst_buffer_unmap (buffer, data, 16);
471 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
474 /* check that at least 7 buffers (112 bytes) are in the queue */
475 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
476 fail_if (buffers_queued != 7);
478 /* now add the clients */
479 g_signal_emit_by_name (sink, "add", pfd1[1]);
480 g_signal_emit_by_name (sink, "add_full", pfd2[1], 3,
481 3, (guint64) 50, 3, (guint64) 200);
482 g_signal_emit_by_name (sink, "add_full", pfd3[1], 3,
483 3, (guint64) 50, 3, (guint64) 50);
485 /* push last buffer to make client fds ready for reading */
486 for (i = 9; i < 10; i++) {
489 buffer = gst_buffer_new_and_alloc (16);
492 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
493 g_snprintf (data, 16, "deadbee%08x", i);
494 gst_buffer_unmap (buffer, data, 16);
496 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
499 /* now we should only read the last 5 buffers (5 * 16 = 80 bytes) */
500 GST_DEBUG ("Reading from client 1");
501 fail_if (read (pfd1[0], data, 16) < 16);
502 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
503 fail_if (read (pfd1[0], data, 16) < 16);
504 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
505 fail_if (read (pfd1[0], data, 16) < 16);
506 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
507 fail_if (read (pfd1[0], data, 16) < 16);
508 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
509 fail_if (read (pfd1[0], data, 16) < 16);
510 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
512 /* second client only bursts 50 bytes = 4 buffers (we get 4 buffers since
513 * the max alows it) */
514 GST_DEBUG ("Reading from client 2");
515 fail_if (read (pfd2[0], data, 16) < 16);
516 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
517 fail_if (read (pfd2[0], data, 16) < 16);
518 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
519 fail_if (read (pfd2[0], data, 16) < 16);
520 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
521 fail_if (read (pfd2[0], data, 16) < 16);
522 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
524 /* third client only bursts 50 bytes = 4 buffers, we can't send
525 * more than 50 bytes so we only get 3 buffers (48 bytes). */
526 GST_DEBUG ("Reading from client 3");
527 fail_if (read (pfd3[0], data, 16) < 16);
528 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
529 fail_if (read (pfd3[0], data, 16) < 16);
530 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
531 fail_if (read (pfd3[0], data, 16) < 16);
532 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
534 GST_DEBUG ("cleaning up multifdsink");
535 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
536 cleanup_multifdsink (sink);
538 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
539 gst_caps_unref (caps);
544 /* keep 100 bytes and burst 80 bytes to clients */
545 GST_START_TEST (test_burst_client_bytes_keyframe)
555 guint buffers_queued;
557 sink = setup_multifdsink ();
558 /* make sure we keep at least 100 bytes at all times */
559 g_object_set (sink, "bytes-min", 100, NULL);
560 g_object_set (sink, "sync-method", 4, NULL); /* 3 = burst_keyframe */
561 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
562 g_object_set (sink, "burst-value", (guint64) 80, NULL);
564 fail_if (pipe (pfd1) == -1);
565 fail_if (pipe (pfd2) == -1);
566 fail_if (pipe (pfd3) == -1);
568 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
570 caps = gst_caps_from_string ("application/x-gst-check");
571 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
572 gst_pad_set_caps (mysrcpad, caps);
574 /* push buffers in, 9 * 16 bytes = 144 bytes */
575 for (i = 0; i < 9; i++) {
578 buffer = gst_buffer_new_and_alloc (16);
580 /* mark most buffers as delta */
581 if (i != 0 && i != 4 && i != 8)
582 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
585 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
586 g_snprintf (data, 16, "deadbee%08x", i);
587 gst_buffer_unmap (buffer, data, 16);
589 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
592 /* check that at least 7 buffers (112 bytes) are in the queue */
593 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
594 fail_if (buffers_queued != 7);
596 /* now add the clients */
597 g_signal_emit_by_name (sink, "add", pfd1[1]);
598 g_signal_emit_by_name (sink, "add_full", pfd2[1], 4,
599 3, (guint64) 50, 3, (guint64) 90);
600 g_signal_emit_by_name (sink, "add_full", pfd3[1], 4,
601 3, (guint64) 50, 3, (guint64) 50);
603 /* push last buffer to make client fds ready for reading */
604 for (i = 9; i < 10; i++) {
607 buffer = gst_buffer_new_and_alloc (16);
608 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
611 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
612 g_snprintf (data, 16, "deadbee%08x", i);
613 gst_buffer_unmap (buffer, data, 16);
615 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
618 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
619 * keyframe at buffer 4 */
620 GST_DEBUG ("Reading from client 1");
621 fail_if (read (pfd1[0], data, 16) < 16);
622 fail_unless (strncmp (data, "deadbee00000004", 16) == 0);
623 fail_if (read (pfd1[0], data, 16) < 16);
624 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
625 fail_if (read (pfd1[0], data, 16) < 16);
626 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
627 fail_if (read (pfd1[0], data, 16) < 16);
628 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
629 fail_if (read (pfd1[0], data, 16) < 16);
630 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
631 fail_if (read (pfd1[0], data, 16) < 16);
632 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
634 /* second client only bursts 50 bytes = 4 buffers, there is
635 * no keyframe above min and below max, so get one below min */
636 GST_DEBUG ("Reading from client 2");
637 fail_if (read (pfd2[0], data, 16) < 16);
638 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
639 fail_if (read (pfd2[0], data, 16) < 16);
640 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
642 /* third client only bursts 50 bytes = 4 buffers, we can't send
643 * more than 50 bytes so we only get 2 buffers (32 bytes). */
644 GST_DEBUG ("Reading from client 3");
645 fail_if (read (pfd3[0], data, 16) < 16);
646 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
647 fail_if (read (pfd3[0], data, 16) < 16);
648 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
650 GST_DEBUG ("cleaning up multifdsink");
651 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
652 cleanup_multifdsink (sink);
654 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
655 gst_caps_unref (caps);
660 /* keep 100 bytes and burst 80 bytes to clients */
661 GST_START_TEST (test_burst_client_bytes_with_keyframe)
671 guint buffers_queued;
673 sink = setup_multifdsink ();
674 /* make sure we keep at least 100 bytes at all times */
675 g_object_set (sink, "bytes-min", 100, NULL);
676 g_object_set (sink, "sync-method", 5, NULL); /* 3 = burst_with_keyframe */
677 g_object_set (sink, "burst-unit", 3, NULL); /* 3 = bytes */
678 g_object_set (sink, "burst-value", (guint64) 80, NULL);
680 fail_if (pipe (pfd1) == -1);
681 fail_if (pipe (pfd2) == -1);
682 fail_if (pipe (pfd3) == -1);
684 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
686 caps = gst_caps_from_string ("application/x-gst-check");
687 gst_pad_set_caps (mysrcpad, caps);
688 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
690 /* push buffers in, 9 * 16 bytes = 144 bytes */
691 for (i = 0; i < 9; i++) {
694 buffer = gst_buffer_new_and_alloc (16);
696 /* mark most buffers as delta */
697 if (i != 0 && i != 4 && i != 8)
698 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
701 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
702 g_snprintf (data, 16, "deadbee%08x", i);
703 gst_buffer_unmap (buffer, data, 16);
705 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
708 /* check that at least 7 buffers (112 bytes) are in the queue */
709 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
710 fail_if (buffers_queued != 7);
712 /* now add the clients */
713 g_signal_emit_by_name (sink, "add", pfd1[1]);
714 g_signal_emit_by_name (sink, "add_full", pfd2[1], 5,
715 3, (guint64) 50, 3, (guint64) 90);
716 g_signal_emit_by_name (sink, "add_full", pfd3[1], 5,
717 3, (guint64) 50, 3, (guint64) 50);
719 /* push last buffer to make client fds ready for reading */
720 for (i = 9; i < 10; i++) {
723 buffer = gst_buffer_new_and_alloc (16);
724 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
727 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
728 g_snprintf (data, 16, "deadbee%08x", i);
729 gst_buffer_unmap (buffer, data, 16);
731 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
734 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
735 * keyframe at buffer 4 */
736 GST_DEBUG ("Reading from client 1");
737 fail_if (read (pfd1[0], data, 16) < 16);
738 fail_unless (strncmp (data, "deadbee00000004", 16) == 0);
739 fail_if (read (pfd1[0], data, 16) < 16);
740 fail_unless (strncmp (data, "deadbee00000005", 16) == 0);
741 fail_if (read (pfd1[0], data, 16) < 16);
742 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
743 fail_if (read (pfd1[0], data, 16) < 16);
744 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
745 fail_if (read (pfd1[0], data, 16) < 16);
746 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
747 fail_if (read (pfd1[0], data, 16) < 16);
748 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
750 /* second client only bursts 50 bytes = 4 buffers, there is
751 * no keyframe above min and below max, so send min */
752 GST_DEBUG ("Reading from client 2");
753 fail_if (read (pfd2[0], data, 16) < 16);
754 fail_unless (strncmp (data, "deadbee00000006", 16) == 0);
755 fail_if (read (pfd2[0], data, 16) < 16);
756 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
757 fail_if (read (pfd2[0], data, 16) < 16);
758 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
759 fail_if (read (pfd2[0], data, 16) < 16);
760 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
762 /* third client only bursts 50 bytes = 4 buffers, we can't send
763 * more than 50 bytes so we only get 3 buffers (48 bytes). */
764 GST_DEBUG ("Reading from client 3");
765 fail_if (read (pfd3[0], data, 16) < 16);
766 fail_unless (strncmp (data, "deadbee00000007", 16) == 0);
767 fail_if (read (pfd3[0], data, 16) < 16);
768 fail_unless (strncmp (data, "deadbee00000008", 16) == 0);
769 fail_if (read (pfd3[0], data, 16) < 16);
770 fail_unless (strncmp (data, "deadbee00000009", 16) == 0);
772 GST_DEBUG ("cleaning up multifdsink");
773 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
774 cleanup_multifdsink (sink);
776 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
777 gst_caps_unref (caps);
782 /* Check that we can get data when multifdsink is configured in next-keyframe
784 GST_START_TEST (test_client_next_keyframe)
793 sink = setup_multifdsink ();
794 g_object_set (sink, "sync-method", 1, NULL); /* 1 = next-keyframe */
796 fail_if (pipe (pfd1) == -1);
798 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
800 caps = gst_caps_from_string ("application/x-gst-check");
801 gst_pad_set_caps (mysrcpad, caps);
802 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
804 /* now add our client */
805 g_signal_emit_by_name (sink, "add", pfd1[1]);
807 /* push buffers in: keyframe, then non-keyframe */
808 for (i = 0; i < 2; i++) {
811 buffer = gst_buffer_new_and_alloc (16);
814 data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
815 g_snprintf (data, 16, "deadbee%08x", i);
816 gst_buffer_unmap (buffer, data, 16);
818 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
820 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
823 /* now we should be able to read some data */
824 GST_DEBUG ("Reading from client 1");
825 fail_if (read (pfd1[0], data, 16) < 16);
826 fail_unless (strncmp (data, "deadbee00000000", 16) == 0);
827 fail_if (read (pfd1[0], data, 16) < 16);
828 fail_unless (strncmp (data, "deadbee00000001", 16) == 0);
830 GST_DEBUG ("cleaning up multifdsink");
831 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
832 cleanup_multifdsink (sink);
834 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
835 gst_caps_unref (caps);
840 /* FIXME: add test simulating chained oggs where:
841 * sync-method is burst-on-connect
842 * (when multifdsink actually does burst-on-connect based on byte size, not
843 "last keyframe" which any frame for audio :))
844 * an old client still needs to read from before the new streamheaders
845 * a new client gets the new streamheaders
848 multifdsink_suite (void)
850 Suite *s = suite_create ("multifdsink");
851 TCase *tc_chain = tcase_create ("general");
853 suite_add_tcase (s, tc_chain);
854 tcase_add_test (tc_chain, test_no_clients);
855 tcase_add_test (tc_chain, test_add_client);
856 tcase_add_test (tc_chain, test_streamheader);
857 tcase_add_test (tc_chain, test_change_streamheader);
858 tcase_add_test (tc_chain, test_burst_client_bytes);
859 tcase_add_test (tc_chain, test_burst_client_bytes_keyframe);
860 tcase_add_test (tc_chain, test_burst_client_bytes_with_keyframe);
861 tcase_add_test (tc_chain, test_client_next_keyframe);
866 GST_CHECK_MAIN (multifdsink);