multifdsink: put back multifdsink before refactoring
[platform/upstream/gstreamer.git] / tests / check / elements / multifdsink.c
1 /* GStreamer
2  *
3  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
24 #include <sys/filio.h>
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 static GstPad *mysrcpad;
30
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
32     GST_PAD_SRC,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("application/x-gst-check")
35     );
36
37 static GstElement *
38 setup_multifdsink (void)
39 {
40   GstElement *multifdsink;
41
42   GST_DEBUG ("setup_multifdsink");
43   multifdsink = gst_check_setup_element ("multifdsink");
44   mysrcpad = gst_check_setup_src_pad (multifdsink, &srctemplate);
45
46   return multifdsink;
47 }
48
49 static void
50 cleanup_multifdsink (GstElement * multifdsink)
51 {
52   GST_DEBUG ("cleanup_multifdsink");
53
54   gst_check_teardown_src_pad (multifdsink);
55   gst_check_teardown_element (multifdsink);
56 }
57
58 static void
59 wait_bytes_served (GstElement * sink, guint64 bytes)
60 {
61   guint64 bytes_served = 0;
62
63   while (bytes_served != bytes) {
64     g_object_get (sink, "bytes-served", &bytes_served, NULL);
65   }
66 }
67
68 /* FIXME: possibly racy, since if it would write, we may not get it
69  * immediately ? */
70 #define fail_if_can_read(msg,fd) \
71 G_STMT_START { \
72   long avail; \
73 \
74   fail_if (ioctl (fd, FIONREAD, &avail) < 0, "%s: could not ioctl", msg); \
75   fail_if (avail > 0, "%s: has bytes available to read"); \
76 } G_STMT_END;
77
78
79 GST_START_TEST (test_no_clients)
80 {
81   GstElement *sink;
82   GstBuffer *buffer;
83   GstCaps *caps;
84
85   sink = setup_multifdsink ();
86
87   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
88
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);
94
95   GST_DEBUG ("cleaning up multifdsink");
96   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
97   cleanup_multifdsink (sink);
98 }
99
100 GST_END_TEST;
101
102 GST_START_TEST (test_add_client)
103 {
104   GstElement *sink;
105   GstBuffer *buffer;
106   GstCaps *caps;
107   int pfd[2];
108   gchar data[4];
109
110   sink = setup_multifdsink ();
111
112   fail_if (pipe (pfd) == -1);
113
114   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
115
116   /* add the client */
117   g_signal_emit_by_name (sink, "add", pfd[1]);
118
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);
126
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);
131
132   GST_DEBUG ("cleaning up multifdsink");
133   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
134   cleanup_multifdsink (sink);
135
136   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
137   gst_caps_unref (caps);
138 }
139
140 GST_END_TEST;
141
142 #define fail_unless_read(msg,fd,size,ref) \
143 G_STMT_START { \
144   char data[size + 1]; \
145   int nbytes; \
146 \
147   GST_DEBUG ("%s: reading %d bytes", msg, size); \
148   nbytes = read (fd, data, size); \
149   data[size] = 0; \
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); \
154 } G_STMT_END;
155
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 */
159 static void
160 gst_multifdsink_create_streamheader (const gchar * data1,
161     const gchar * data2, GstBuffer ** hbuf1, GstBuffer ** hbuf2,
162     GstCaps ** caps)
163 {
164   GstBuffer *buf;
165   GValue array = { 0 };
166   GValue value = { 0 };
167   GstStructure *structure;
168   guint size1 = strlen (data1);
169   guint size2 = strlen (data2);
170
171   fail_if (hbuf1 == NULL);
172   fail_if (hbuf2 == NULL);
173   fail_if (caps == NULL);
174
175   /* create caps with streamheader, set the caps, and push the IN_CAPS
176    * buffers */
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);
183
184   g_value_init (&array, GST_TYPE_ARRAY);
185
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);
194
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);
202
203   *caps = gst_caps_from_string ("application/x-gst-check");
204   structure = gst_caps_get_structure (*caps, 0);
205
206   gst_structure_set_value (structure, "streamheader", &array);
207   g_value_unset (&array);
208   ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
209
210   /* we want to keep them around for the tests */
211   gst_buffer_ref (*hbuf1);
212   gst_buffer_ref (*hbuf2);
213
214   GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
215 }
216
217
218 /* this test:
219  * - adds a first client
220  * - sets streamheader caps on the pad
221  * - pushes the IN_CAPS buffers
222  * - pushes a buffer
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
227  * - the new buffer
228  */
229 GST_START_TEST (test_streamheader)
230 {
231   GstElement *sink;
232   GstBuffer *hbuf1, *hbuf2, *buf;
233   GstCaps *caps;
234   int pfd1[2], pfd2[2];
235
236   sink = setup_multifdsink ();
237
238   fail_if (pipe (pfd1) == -1);
239   fail_if (pipe (pfd2) == -1);
240
241   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
242
243   /* add the first client */
244   g_signal_emit_by_name (sink, "add", pfd1[1]);
245
246   /* create caps with streamheader, set the caps, and push the IN_CAPS
247    * buffers */
248   gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
249       &caps);
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);
253
254   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
255   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
256
257   //FIXME:
258   //fail_if_can_read ("first client", pfd1[0]);
259
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);
265
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);
270
271   /* now add the second client */
272   g_signal_emit_by_name (sink, "add", pfd2[1]);
273   //FIXME:
274   //fail_if_can_read ("second client", pfd2[0]);
275
276   /* now push another buffer, which will trigger streamheader for second
277    * client */
278   buf = gst_buffer_new_and_alloc (4);
279   gst_buffer_fill (buf, 0, "deaf", 4);
280   gst_pad_push (mysrcpad, buf);
281
282   fail_unless_read ("first client", pfd1[0], 4, "deaf");
283
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);
289
290   GST_DEBUG ("cleaning up multifdsink");
291
292   g_signal_emit_by_name (sink, "remove", pfd1[1]);
293   g_signal_emit_by_name (sink, "remove", pfd2[1]);
294
295   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
296   cleanup_multifdsink (sink);
297
298   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
299   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
300   gst_buffer_unref (hbuf1);
301   gst_buffer_unref (hbuf2);
302
303   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
304   gst_caps_unref (caps);
305 }
306
307 GST_END_TEST;
308
309 /* this tests changing of streamheaders
310  * - set streamheader caps on the pad
311  * - pushes the IN_CAPS buffers
312  * - pushes a buffer
313  * - add a first client
314  * - verifies that this first client receives the first streamheader caps,
315  *   plus a new buffer
316  * - change streamheader caps
317  * - verify that the first client receives the new streamheader buffers as well
318  */
319 GST_START_TEST (test_change_streamheader)
320 {
321   GstElement *sink;
322   GstBuffer *hbuf1, *hbuf2, *buf;
323   GstCaps *caps;
324   int pfd1[2], pfd2[2];
325
326   sink = setup_multifdsink ();
327
328   fail_if (pipe (pfd1) == -1);
329   fail_if (pipe (pfd2) == -1);
330
331   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
332
333   /* create caps with streamheader, set the caps, and push the IN_CAPS
334    * buffers */
335   gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
336       &caps);
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);
340
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);
344
345   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
346   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
347
348   /* add the first client */
349   g_signal_emit_by_name (sink, "add", pfd1[1]);
350
351   /* verify this hasn't triggered a write yet */
352   /* FIXME: possibly racy, since if it would write, we may not get it
353    * immediately ? */
354   //fail_if_can_read ("first client, no buffer", pfd1[0]);
355
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);
360
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);
365
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]);
369
370   /* change the streamheader */
371
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);
377
378   /* drop our ref to the previous caps */
379   gst_caps_unref (caps);
380
381   gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
382       &caps);
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);
387
388   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
389   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
390
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]);
394
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);
400
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");
404
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);
411
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);
416
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);
423
424   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
425   gst_caps_unref (caps);
426 }
427
428 GST_END_TEST;
429
430 /* keep 100 bytes and burst 80 bytes to clients */
431 GST_START_TEST (test_burst_client_bytes)
432 {
433   GstElement *sink;
434   GstBuffer *buffer;
435   GstCaps *caps;
436   int pfd1[2];
437   int pfd2[2];
438   int pfd3[2];
439   gchar data[16];
440   gint i;
441   guint buffers_queued;
442
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);
449
450   fail_if (pipe (pfd1) == -1);
451   fail_if (pipe (pfd2) == -1);
452   fail_if (pipe (pfd3) == -1);
453
454   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
455
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);
459
460   /* push buffers in, 9 * 16 bytes = 144 bytes */
461   for (i = 0; i < 9; i++) {
462     gchar *data;
463
464     buffer = gst_buffer_new_and_alloc (16);
465
466     /* copy some id */
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);
470
471     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
472   }
473
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);
477
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);
484
485   /* push last buffer to make client fds ready for reading */
486   for (i = 9; i < 10; i++) {
487     gchar *data;
488
489     buffer = gst_buffer_new_and_alloc (16);
490
491     /* copy some id */
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);
495
496     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
497   }
498
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);
511
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);
523
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);
533
534   GST_DEBUG ("cleaning up multifdsink");
535   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
536   cleanup_multifdsink (sink);
537
538   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
539   gst_caps_unref (caps);
540 }
541
542 GST_END_TEST;
543
544 /* keep 100 bytes and burst 80 bytes to clients */
545 GST_START_TEST (test_burst_client_bytes_keyframe)
546 {
547   GstElement *sink;
548   GstBuffer *buffer;
549   GstCaps *caps;
550   int pfd1[2];
551   int pfd2[2];
552   int pfd3[2];
553   gchar data[16];
554   gint i;
555   guint buffers_queued;
556
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);
563
564   fail_if (pipe (pfd1) == -1);
565   fail_if (pipe (pfd2) == -1);
566   fail_if (pipe (pfd3) == -1);
567
568   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
569
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);
573
574   /* push buffers in, 9 * 16 bytes = 144 bytes */
575   for (i = 0; i < 9; i++) {
576     gchar *data;
577
578     buffer = gst_buffer_new_and_alloc (16);
579
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);
583
584     /* copy some id */
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);
588
589     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
590   }
591
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);
595
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);
602
603   /* push last buffer to make client fds ready for reading */
604   for (i = 9; i < 10; i++) {
605     gchar *data;
606
607     buffer = gst_buffer_new_and_alloc (16);
608     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
609
610     /* copy some id */
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);
614
615     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
616   }
617
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);
633
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);
641
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);
649
650   GST_DEBUG ("cleaning up multifdsink");
651   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
652   cleanup_multifdsink (sink);
653
654   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
655   gst_caps_unref (caps);
656 }
657
658 GST_END_TEST;
659
660 /* keep 100 bytes and burst 80 bytes to clients */
661 GST_START_TEST (test_burst_client_bytes_with_keyframe)
662 {
663   GstElement *sink;
664   GstBuffer *buffer;
665   GstCaps *caps;
666   int pfd1[2];
667   int pfd2[2];
668   int pfd3[2];
669   gchar data[16];
670   gint i;
671   guint buffers_queued;
672
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);
679
680   fail_if (pipe (pfd1) == -1);
681   fail_if (pipe (pfd2) == -1);
682   fail_if (pipe (pfd3) == -1);
683
684   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
685
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);
689
690   /* push buffers in, 9 * 16 bytes = 144 bytes */
691   for (i = 0; i < 9; i++) {
692     gchar *data;
693
694     buffer = gst_buffer_new_and_alloc (16);
695
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);
699
700     /* copy some id */
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);
704
705     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
706   }
707
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);
711
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);
718
719   /* push last buffer to make client fds ready for reading */
720   for (i = 9; i < 10; i++) {
721     gchar *data;
722
723     buffer = gst_buffer_new_and_alloc (16);
724     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
725
726     /* copy some id */
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);
730
731     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
732   }
733
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);
749
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);
761
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);
771
772   GST_DEBUG ("cleaning up multifdsink");
773   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
774   cleanup_multifdsink (sink);
775
776   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
777   gst_caps_unref (caps);
778 }
779
780 GST_END_TEST;
781
782 /* Check that we can get data when multifdsink is configured in next-keyframe
783  * mode */
784 GST_START_TEST (test_client_next_keyframe)
785 {
786   GstElement *sink;
787   GstBuffer *buffer;
788   GstCaps *caps;
789   int pfd1[2];
790   gchar data[16];
791   gint i;
792
793   sink = setup_multifdsink ();
794   g_object_set (sink, "sync-method", 1, NULL);  /* 1 = next-keyframe */
795
796   fail_if (pipe (pfd1) == -1);
797
798   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
799
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);
803
804   /* now add our client */
805   g_signal_emit_by_name (sink, "add", pfd1[1]);
806
807   /* push buffers in: keyframe, then non-keyframe */
808   for (i = 0; i < 2; i++) {
809     gchar *data;
810
811     buffer = gst_buffer_new_and_alloc (16);
812
813     /* copy some id */
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);
817     if (i > 0)
818       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
819
820     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
821   }
822
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);
829
830   GST_DEBUG ("cleaning up multifdsink");
831   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
832   cleanup_multifdsink (sink);
833
834   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
835   gst_caps_unref (caps);
836 }
837
838 GST_END_TEST;
839
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
846  */
847 static Suite *
848 multifdsink_suite (void)
849 {
850   Suite *s = suite_create ("multifdsink");
851   TCase *tc_chain = tcase_create ("general");
852
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);
862
863   return s;
864 }
865
866 GST_CHECK_MAIN (multifdsink);