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 /* FIXME: remove this header once formats are refactored */
30 #include "gst/tcp/gstmultifdsink.h"
32 static GstPad *mysrcpad;
34 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
37 GST_STATIC_CAPS ("application/x-gst-check")
41 setup_multifdsink (void)
43 GstElement *multifdsink;
45 GST_DEBUG ("setup_multifdsink");
46 multifdsink = gst_check_setup_element ("multifdsink");
47 mysrcpad = gst_check_setup_src_pad (multifdsink, &srctemplate);
48 GST_PAD_UNSET_FLUSHING (mysrcpad);
54 cleanup_multifdsink (GstElement * multifdsink)
56 GST_DEBUG ("cleanup_multifdsink");
58 gst_check_teardown_src_pad (multifdsink);
59 gst_check_teardown_element (multifdsink);
63 wait_bytes_served (GstElement * sink, guint64 bytes)
65 guint64 bytes_served = 0;
67 while (bytes_served != bytes) {
68 g_object_get (sink, "bytes-served", &bytes_served, NULL);
72 /* FIXME: possibly racy, since if it would write, we may not get it
74 #define fail_if_can_read(msg,fd) \
78 fail_if (ioctl (fd, FIONREAD, &avail) < 0, "%s: could not ioctl", msg); \
79 fail_if (avail > 0, "%s: has bytes available to read"); \
83 GST_START_TEST (test_no_clients)
89 sink = setup_multifdsink ();
91 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
93 caps = gst_caps_from_string ("application/x-gst-check");
94 buffer = gst_buffer_new_and_alloc (4);
95 gst_pad_set_caps (mysrcpad, caps);
96 gst_caps_unref (caps);
97 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
99 GST_DEBUG ("cleaning up multifdsink");
100 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
101 cleanup_multifdsink (sink);
106 GST_START_TEST (test_add_client)
114 sink = setup_multifdsink ();
116 fail_if (pipe (pfd) == -1);
118 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
121 g_signal_emit_by_name (sink, "add", pfd[1]);
123 caps = gst_caps_from_string ("application/x-gst-check");
124 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
125 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
126 buffer = gst_buffer_new_and_alloc (4);
127 gst_pad_set_caps (mysrcpad, caps);
128 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
129 gst_buffer_fill (buffer, 0, "dead", 4);
130 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
132 GST_DEBUG ("reading");
133 fail_if (read (pfd[0], data, 4) < 4);
134 fail_unless (strncmp (data, "dead", 4) == 0);
135 wait_bytes_served (sink, 4);
137 GST_DEBUG ("cleaning up multifdsink");
138 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
139 cleanup_multifdsink (sink);
141 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
142 gst_caps_unref (caps);
147 #define fail_unless_read(msg,fd,size,ref) \
149 char data[size + 1]; \
152 GST_DEBUG ("%s: reading %d bytes", msg, size); \
153 nbytes = read (fd, data, size); \
155 GST_DEBUG ("%s: read %d bytes", msg, nbytes); \
156 fail_if (nbytes < size); \
157 fail_unless (memcmp (data, ref, size) == 0, \
158 "data read '%s' differs from '%s'", data, ref); \
161 #define fail_unless_num_handles(sink,num) \
164 g_object_get (sink, "num-fds", &handles, NULL); \
165 fail_unless (handles == num, \
166 "sink has %d handles instead of expected %d", handles, num); \
169 /* from the given two data buffers, create two streamheader buffers and
170 * some caps that match it, and store them in the given pointers
171 * returns one ref to each of the buffers and the caps */
173 gst_multifdsink_create_streamheader (const gchar * data1,
174 const gchar * data2, GstBuffer ** hbuf1, GstBuffer ** hbuf2,
178 GValue array = { 0 };
179 GValue value = { 0 };
180 GstStructure *structure;
181 guint size1 = strlen (data1);
182 guint size2 = strlen (data2);
184 fail_if (hbuf1 == NULL);
185 fail_if (hbuf2 == NULL);
186 fail_if (caps == NULL);
188 /* create caps with streamheader, set the caps, and push the IN_CAPS
190 *hbuf1 = gst_buffer_new_and_alloc (size1);
191 GST_BUFFER_FLAG_SET (*hbuf1, GST_BUFFER_FLAG_IN_CAPS);
192 gst_buffer_fill (*hbuf1, 0, data1, size1);
193 *hbuf2 = gst_buffer_new_and_alloc (size2);
194 GST_BUFFER_FLAG_SET (*hbuf2, GST_BUFFER_FLAG_IN_CAPS);
195 gst_buffer_fill (*hbuf2, 0, data2, size2);
197 g_value_init (&array, GST_TYPE_ARRAY);
199 g_value_init (&value, GST_TYPE_BUFFER);
200 /* we take a copy, set it on the array (which refs it), then unref our copy */
201 buf = gst_buffer_copy (*hbuf1);
202 gst_value_set_buffer (&value, buf);
203 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
204 gst_buffer_unref (buf);
205 gst_value_array_append_value (&array, &value);
206 g_value_unset (&value);
208 g_value_init (&value, GST_TYPE_BUFFER);
209 buf = gst_buffer_copy (*hbuf2);
210 gst_value_set_buffer (&value, buf);
211 ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
212 gst_buffer_unref (buf);
213 gst_value_array_append_value (&array, &value);
214 g_value_unset (&value);
216 *caps = gst_caps_from_string ("application/x-gst-check");
217 structure = gst_caps_get_structure (*caps, 0);
219 gst_structure_set_value (structure, "streamheader", &array);
220 g_value_unset (&array);
221 ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
223 /* we want to keep them around for the tests */
224 gst_buffer_ref (*hbuf1);
225 gst_buffer_ref (*hbuf2);
227 GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
232 * - adds a first client
233 * - sets streamheader caps on the pad
234 * - pushes the IN_CAPS buffers
236 * - verifies that the client received all the data correctly, and did not
237 * get multiple copies of the streamheader
238 * - adds a second client
239 * - verifies that this second client receives the streamheader caps too, plus
242 GST_START_TEST (test_streamheader)
245 GstBuffer *hbuf1, *hbuf2, *buf;
247 int pfd1[2], pfd2[2];
249 sink = setup_multifdsink ();
251 fail_if (pipe (pfd1) == -1);
252 fail_if (pipe (pfd2) == -1);
254 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
256 /* add the first client */
257 fail_unless_num_handles (sink, 0);
258 g_signal_emit_by_name (sink, "add", pfd1[1]);
259 fail_unless_num_handles (sink, 1);
261 /* create caps with streamheader, set the caps, and push the IN_CAPS
263 gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
265 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
266 fail_unless (gst_pad_set_caps (mysrcpad, caps));
267 /* one is ours, two from set_caps */
268 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
270 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
271 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
274 //fail_if_can_read ("first client", pfd1[0]);
276 /* push a non-IN_CAPS buffer, this should trigger the client receiving the
277 * first three buffers */
278 buf = gst_buffer_new_and_alloc (4);
279 gst_buffer_fill (buf, 0, "f00d", 4);
280 gst_pad_push (mysrcpad, buf);
282 fail_unless_read ("first client", pfd1[0], 4, "babe");
283 fail_unless_read ("first client", pfd1[0], 8, "deadbeef");
284 fail_unless_read ("first client", pfd1[0], 4, "f00d");
285 wait_bytes_served (sink, 16);
287 /* now add the second client */
288 g_signal_emit_by_name (sink, "add", pfd2[1]);
289 fail_unless_num_handles (sink, 2);
291 //fail_if_can_read ("second client", pfd2[0]);
293 /* now push another buffer, which will trigger streamheader for second
295 buf = gst_buffer_new_and_alloc (4);
296 gst_buffer_fill (buf, 0, "deaf", 4);
297 gst_pad_push (mysrcpad, buf);
299 fail_unless_read ("first client", pfd1[0], 4, "deaf");
301 fail_unless_read ("second client", pfd2[0], 4, "babe");
302 fail_unless_read ("second client", pfd2[0], 8, "deadbeef");
303 /* we missed the f00d buffer */
304 fail_unless_read ("second client", pfd2[0], 4, "deaf");
305 wait_bytes_served (sink, 36);
307 GST_DEBUG ("cleaning up multifdsink");
309 fail_unless_num_handles (sink, 2);
310 g_signal_emit_by_name (sink, "remove", pfd1[1]);
311 fail_unless_num_handles (sink, 1);
312 g_signal_emit_by_name (sink, "remove", pfd2[1]);
313 fail_unless_num_handles (sink, 0);
315 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
316 cleanup_multifdsink (sink);
318 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
319 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
320 gst_buffer_unref (hbuf1);
321 gst_buffer_unref (hbuf2);
323 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
324 gst_caps_unref (caps);
329 /* this tests changing of streamheaders
330 * - set streamheader caps on the pad
331 * - pushes the IN_CAPS buffers
333 * - add a first client
334 * - verifies that this first client receives the first streamheader caps,
336 * - change streamheader caps
337 * - verify that the first client receives the new streamheader buffers as well
339 GST_START_TEST (test_change_streamheader)
342 GstBuffer *hbuf1, *hbuf2, *buf;
344 int pfd1[2], pfd2[2];
346 sink = setup_multifdsink ();
348 fail_if (pipe (pfd1) == -1);
349 fail_if (pipe (pfd2) == -1);
351 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
353 /* create caps with streamheader, set the caps, and push the IN_CAPS
355 gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
357 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
358 fail_unless (gst_pad_set_caps (mysrcpad, caps));
359 /* one is ours, two from set_caps */
360 ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
362 /* one to hold for the test and one to give away */
363 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
364 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
366 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
367 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
369 /* add the first client */
370 g_signal_emit_by_name (sink, "add", pfd1[1]);
372 /* verify this hasn't triggered a write yet */
373 /* FIXME: possibly racy, since if it would write, we may not get it
375 //fail_if_can_read ("first client, no buffer", pfd1[0]);
377 /* now push a buffer and read */
378 buf = gst_buffer_new_and_alloc (4);
379 gst_buffer_fill (buf, 0, "f00d", 4);
380 gst_pad_push (mysrcpad, buf);
382 fail_unless_read ("change: first client", pfd1[0], 5, "first");
383 fail_unless_read ("change: first client", pfd1[0], 6, "header");
384 fail_unless_read ("change: first client", pfd1[0], 4, "f00d");
385 //wait_bytes_served (sink, 16);
387 /* now add the second client */
388 g_signal_emit_by_name (sink, "add", pfd2[1]);
389 //fail_if_can_read ("second client, no buffer", pfd2[0]);
391 /* change the streamheader */
393 /* before we change, multifdsink still has a list of the old streamheaders */
394 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
395 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
396 gst_buffer_unref (hbuf1);
397 gst_buffer_unref (hbuf2);
399 /* drop our ref to the previous caps */
400 gst_caps_unref (caps);
402 gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
404 fail_unless (gst_pad_set_caps (mysrcpad, caps));
405 /* one to hold for the test and one to give away */
406 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
407 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
409 fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
410 fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
412 /* verify neither client has new data available to read */
413 //fail_if_can_read ("first client, changed streamheader", pfd1[0]);
414 //fail_if_can_read ("second client, changed streamheader", pfd2[0]);
416 /* now push another buffer, which will trigger streamheader for second
417 * client, but should also send new streamheaders to first client */
418 buf = gst_buffer_new_and_alloc (8);
419 gst_buffer_fill (buf, 0, "deadbabe", 8);
420 gst_pad_push (mysrcpad, buf);
422 fail_unless_read ("first client", pfd1[0], 6, "second");
423 fail_unless_read ("first client", pfd1[0], 6, "header");
424 fail_unless_read ("first client", pfd1[0], 8, "deadbabe");
426 /* new streamheader data */
427 fail_unless_read ("second client", pfd2[0], 6, "second");
428 fail_unless_read ("second client", pfd2[0], 6, "header");
429 /* we missed the f00d buffer */
430 fail_unless_read ("second client", pfd2[0], 8, "deadbabe");
431 //wait_bytes_served (sink, 36);
433 GST_DEBUG ("cleaning up multifdsink");
434 g_signal_emit_by_name (sink, "remove", pfd1[1]);
435 g_signal_emit_by_name (sink, "remove", pfd2[1]);
436 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
438 /* setting to NULL should have cleared the streamheader */
439 ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
440 ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
441 gst_buffer_unref (hbuf1);
442 gst_buffer_unref (hbuf2);
443 cleanup_multifdsink (sink);
445 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
446 gst_caps_unref (caps);
452 gst_new_buffer (int i)
457 GstBuffer *buffer = gst_buffer_new_and_alloc (16);
460 g_assert (gst_buffer_map (buffer, &info, GST_MAP_WRITE));
461 data = (gchar *) info.data;
462 g_snprintf (data, 16, "deadbee%08x", i);
463 gst_buffer_unmap (buffer, &info);
469 /* keep 100 bytes and burst 80 bytes to clients */
470 GST_START_TEST (test_burst_client_bytes)
478 guint buffers_queued;
480 sink = setup_multifdsink ();
481 /* make sure we keep at least 100 bytes at all times */
482 g_object_set (sink, "bytes-min", 100, NULL);
483 g_object_set (sink, "sync-method", 3, NULL); /* 3 = burst */
484 g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
485 g_object_set (sink, "burst-value", (guint64) 80, NULL);
487 fail_if (pipe (pfd1) == -1);
488 fail_if (pipe (pfd2) == -1);
489 fail_if (pipe (pfd3) == -1);
491 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
493 caps = gst_caps_from_string ("application/x-gst-check");
494 gst_pad_set_caps (mysrcpad, caps);
495 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
497 /* push buffers in, 9 * 16 bytes = 144 bytes */
498 for (i = 0; i < 9; i++) {
499 GstBuffer *buffer = gst_new_buffer (i);
501 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
504 /* check that at least 7 buffers (112 bytes) are in the queue */
505 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
506 fail_if (buffers_queued != 7);
508 /* now add the clients */
509 fail_unless_num_handles (sink, 0);
510 g_signal_emit_by_name (sink, "add", pfd1[1]);
511 fail_unless_num_handles (sink, 1);
512 g_signal_emit_by_name (sink, "add_full", pfd2[1], GST_SYNC_METHOD_BURST,
513 GST_FORMAT_BYTES, (guint64) 50, GST_FORMAT_BYTES, (guint64) 200);
514 g_signal_emit_by_name (sink, "add_full", pfd3[1], GST_SYNC_METHOD_BURST,
515 GST_FORMAT_BYTES, (guint64) 50, GST_FORMAT_BYTES, (guint64) 50);
516 fail_unless_num_handles (sink, 3);
518 /* push last buffer to make client fds ready for reading */
519 for (i = 9; i < 10; i++) {
520 GstBuffer *buffer = gst_new_buffer (i);
522 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
525 /* now we should only read the last 5 buffers (5 * 16 = 80 bytes) */
526 GST_DEBUG ("Reading from client 1");
527 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000005");
528 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000006");
529 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000007");
530 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000008");
531 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000009");
533 /* second client only bursts 50 bytes = 4 buffers (we get 4 buffers since
534 * the max alows it) */
535 GST_DEBUG ("Reading from client 2");
536 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000006");
537 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000007");
538 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000008");
539 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000009");
541 /* third client only bursts 50 bytes = 4 buffers, we can't send
542 * more than 50 bytes so we only get 3 buffers (48 bytes). */
543 GST_DEBUG ("Reading from client 3");
544 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000007");
545 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000008");
546 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000009");
548 GST_DEBUG ("cleaning up multifdsink");
549 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
550 cleanup_multifdsink (sink);
552 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
553 gst_caps_unref (caps);
558 /* keep 100 bytes and burst 80 bytes to clients */
559 GST_START_TEST (test_burst_client_bytes_keyframe)
567 guint buffers_queued;
569 sink = setup_multifdsink ();
570 /* make sure we keep at least 100 bytes at all times */
571 g_object_set (sink, "bytes-min", 100, NULL);
572 g_object_set (sink, "sync-method", 4, NULL); /* 4 = burst_keyframe */
573 g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
574 g_object_set (sink, "burst-value", (guint64) 80, NULL);
576 fail_if (pipe (pfd1) == -1);
577 fail_if (pipe (pfd2) == -1);
578 fail_if (pipe (pfd3) == -1);
580 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
582 caps = gst_caps_from_string ("application/x-gst-check");
583 gst_pad_set_caps (mysrcpad, caps);
584 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
586 /* push buffers in, 9 * 16 bytes = 144 bytes */
587 for (i = 0; i < 9; i++) {
588 GstBuffer *buffer = gst_new_buffer (i);
590 /* mark most buffers as delta */
591 if (i != 0 && i != 4 && i != 8)
592 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
594 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
597 /* check that at least 7 buffers (112 bytes) are in the queue */
598 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
599 fail_if (buffers_queued != 7);
601 /* now add the clients */
602 g_signal_emit_by_name (sink, "add", pfd1[1]);
603 g_signal_emit_by_name (sink, "add_full", pfd2[1],
604 GST_SYNC_METHOD_BURST_KEYFRAME, GST_FORMAT_BYTES, (guint64) 50,
605 GST_FORMAT_BYTES, (guint64) 90);
606 g_signal_emit_by_name (sink, "add_full", pfd3[1],
607 GST_SYNC_METHOD_BURST_KEYFRAME, GST_FORMAT_BYTES, (guint64) 50,
608 GST_FORMAT_BYTES, (guint64) 50);
610 /* push last buffer to make client fds ready for reading */
611 for (i = 9; i < 10; i++) {
612 GstBuffer *buffer = gst_new_buffer (i);
614 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
616 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
619 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
620 * keyframe at buffer 4 */
621 GST_DEBUG ("Reading from client 1");
622 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000004");
623 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000005");
624 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000006");
625 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000007");
626 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000008");
627 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000009");
629 /* second client only bursts 50 bytes = 4 buffers, there is
630 * no keyframe above min and below max, so get one below min */
631 GST_DEBUG ("Reading from client 2");
632 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000008");
633 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000009");
635 /* third client only bursts 50 bytes = 4 buffers, we can't send
636 * more than 50 bytes so we only get 2 buffers (32 bytes). */
637 GST_DEBUG ("Reading from client 3");
638 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000008");
639 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000009");
641 GST_DEBUG ("cleaning up multifdsink");
642 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
643 cleanup_multifdsink (sink);
645 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
646 gst_caps_unref (caps);
651 /* keep 100 bytes and burst 80 bytes to clients */
652 GST_START_TEST (test_burst_client_bytes_with_keyframe)
660 guint buffers_queued;
662 sink = setup_multifdsink ();
663 /* make sure we keep at least 100 bytes at all times */
664 g_object_set (sink, "bytes-min", 100, NULL);
665 g_object_set (sink, "sync-method", 5, NULL); /* 5 = burst_with_keyframe */
666 g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
667 g_object_set (sink, "burst-value", (guint64) 80, NULL);
669 fail_if (pipe (pfd1) == -1);
670 fail_if (pipe (pfd2) == -1);
671 fail_if (pipe (pfd3) == -1);
673 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
675 caps = gst_caps_from_string ("application/x-gst-check");
676 gst_pad_set_caps (mysrcpad, caps);
677 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
679 /* push buffers in, 9 * 16 bytes = 144 bytes */
680 for (i = 0; i < 9; i++) {
681 GstBuffer *buffer = gst_new_buffer (i);
683 /* mark most buffers as delta */
684 if (i != 0 && i != 4 && i != 8)
685 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
687 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
690 /* check that at least 7 buffers (112 bytes) are in the queue */
691 g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
692 fail_if (buffers_queued != 7);
694 /* now add the clients */
695 g_signal_emit_by_name (sink, "add", pfd1[1]);
696 g_signal_emit_by_name (sink, "add_full", pfd2[1],
697 GST_SYNC_METHOD_BURST_WITH_KEYFRAME, GST_FORMAT_BYTES,
698 (guint64) 50, GST_FORMAT_BYTES, (guint64) 90);
699 g_signal_emit_by_name (sink, "add_full", pfd3[1],
700 GST_SYNC_METHOD_BURST_WITH_KEYFRAME, GST_FORMAT_BYTES,
701 (guint64) 50, GST_FORMAT_BYTES, (guint64) 50);
703 /* push last buffer to make client fds ready for reading */
704 for (i = 9; i < 10; i++) {
705 GstBuffer *buffer = gst_new_buffer (i);
707 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
709 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
712 /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
713 * keyframe at buffer 4 */
714 GST_DEBUG ("Reading from client 1");
715 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000004");
716 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000005");
717 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000006");
718 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000007");
719 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000008");
720 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000009");
722 /* second client only bursts 50 bytes = 4 buffers, there is
723 * no keyframe above min and below max, so send min */
724 GST_DEBUG ("Reading from client 2");
725 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000006");
726 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000007");
727 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000008");
728 fail_unless_read ("client 2", pfd2[0], 16, "deadbee00000009");
730 /* third client only bursts 50 bytes = 4 buffers, we can't send
731 * more than 50 bytes so we only get 3 buffers (48 bytes). */
732 GST_DEBUG ("Reading from client 3");
733 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000007");
734 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000008");
735 fail_unless_read ("client 3", pfd3[0], 16, "deadbee00000009");
737 GST_DEBUG ("cleaning up multifdsink");
738 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
739 cleanup_multifdsink (sink);
741 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
742 gst_caps_unref (caps);
747 /* Check that we can get data when multifdsink is configured in next-keyframe
749 GST_START_TEST (test_client_next_keyframe)
756 sink = setup_multifdsink ();
757 g_object_set (sink, "sync-method", 1, NULL); /* 1 = next-keyframe */
759 fail_if (pipe (pfd1) == -1);
761 ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
763 caps = gst_caps_from_string ("application/x-gst-check");
764 gst_pad_set_caps (mysrcpad, caps);
765 GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
767 /* now add our client */
768 g_signal_emit_by_name (sink, "add", pfd1[1]);
770 /* push buffers in: keyframe, then non-keyframe */
771 for (i = 0; i < 2; i++) {
772 GstBuffer *buffer = gst_new_buffer (i);
774 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
776 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
779 /* now we should be able to read some data */
780 GST_DEBUG ("Reading from client 1");
781 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000000");
782 fail_unless_read ("client 1", pfd1[0], 16, "deadbee00000001");
784 GST_DEBUG ("cleaning up multifdsink");
785 ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
786 cleanup_multifdsink (sink);
788 ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
789 gst_caps_unref (caps);
794 /* FIXME: add test simulating chained oggs where:
795 * sync-method is burst-on-connect
796 * (when multifdsink actually does burst-on-connect based on byte size, not
797 "last keyframe" which any frame for audio :))
798 * an old client still needs to read from before the new streamheaders
799 * a new client gets the new streamheaders
802 multifdsink_suite (void)
804 Suite *s = suite_create ("multifdsink");
805 TCase *tc_chain = tcase_create ("general");
807 suite_add_tcase (s, tc_chain);
808 tcase_add_test (tc_chain, test_no_clients);
809 tcase_add_test (tc_chain, test_add_client);
810 tcase_add_test (tc_chain, test_streamheader);
811 tcase_add_test (tc_chain, test_change_streamheader);
812 tcase_add_test (tc_chain, test_burst_client_bytes);
813 tcase_add_test (tc_chain, test_burst_client_bytes_keyframe);
814 tcase_add_test (tc_chain, test_burst_client_bytes_with_keyframe);
815 tcase_add_test (tc_chain, test_client_next_keyframe);
820 GST_CHECK_MAIN (multifdsink);