Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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, NULL);
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_buffer_set_caps (buffer, 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_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);
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   memcpy (GST_BUFFER_DATA (*hbuf1), data1, size1);
180   *hbuf2 = gst_buffer_new_and_alloc (size2);
181   GST_BUFFER_FLAG_SET (*hbuf2, GST_BUFFER_FLAG_IN_CAPS);
182   memcpy (GST_BUFFER_DATA (*hbuf2), data2, size2);
183
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   /* 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);
214
215   /* we want to keep them around for the tests */
216   gst_buffer_ref (*hbuf1);
217   gst_buffer_ref (*hbuf2);
218
219   GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
220 }
221
222
223 /* this test:
224  * - adds a first client
225  * - sets streamheader caps on the pad
226  * - pushes the IN_CAPS buffers
227  * - pushes a buffer
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
232  * - the new buffer
233  */
234 GST_START_TEST (test_streamheader)
235 {
236   GstElement *sink;
237   GstBuffer *hbuf1, *hbuf2, *buf;
238   GstCaps *caps;
239   int pfd1[2], pfd2[2];
240
241   sink = setup_multifdsink ();
242
243   fail_if (pipe (pfd1) == -1);
244   fail_if (pipe (pfd2) == -1);
245
246   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
247
248   /* add the first client */
249   g_signal_emit_by_name (sink, "add", pfd1[1]);
250
251   /* create caps with streamheader, set the caps, and push the IN_CAPS
252    * buffers */
253   gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
254       &caps);
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);
258
259   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
260   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
261
262   //FIXME:
263   //fail_if_can_read ("first client", pfd1[0]);
264
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);
270
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);
275
276   /* now add the second client */
277   g_signal_emit_by_name (sink, "add", pfd2[1]);
278   //FIXME:
279   //fail_if_can_read ("second client", pfd2[0]);
280
281   /* now push another buffer, which will trigger streamheader for second
282    * client */
283   buf = gst_buffer_new_and_alloc (4);
284   memcpy (GST_BUFFER_DATA (buf), "deaf", 4);
285   gst_pad_push (mysrcpad, buf);
286
287   fail_unless_read ("first client", pfd1[0], 4, "deaf");
288
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);
294
295   GST_DEBUG ("cleaning up multifdsink");
296
297   g_signal_emit_by_name (sink, "remove", pfd1[1]);
298   g_signal_emit_by_name (sink, "remove", pfd2[1]);
299
300   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
301   cleanup_multifdsink (sink);
302
303   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
304   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
305   gst_buffer_unref (hbuf1);
306   gst_buffer_unref (hbuf2);
307
308   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
309   gst_caps_unref (caps);
310 }
311
312 GST_END_TEST;
313
314 /* this tests changing of streamheaders
315  * - set streamheader caps on the pad
316  * - pushes the IN_CAPS buffers
317  * - pushes a buffer
318  * - add a first client
319  * - verifies that this first client receives the first streamheader caps,
320  *   plus a new buffer
321  * - change streamheader caps
322  * - verify that the first client receives the new streamheader buffers as well
323  */
324 GST_START_TEST (test_change_streamheader)
325 {
326   GstElement *sink;
327   GstBuffer *hbuf1, *hbuf2, *buf;
328   GstCaps *caps;
329   int pfd1[2], pfd2[2];
330
331   sink = setup_multifdsink ();
332
333   fail_if (pipe (pfd1) == -1);
334   fail_if (pipe (pfd2) == -1);
335
336   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
337
338   /* create caps with streamheader, set the caps, and push the IN_CAPS
339    * buffers */
340   gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
341       &caps);
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);
345
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);
349
350   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
351   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
352
353   /* add the first client */
354   g_signal_emit_by_name (sink, "add", pfd1[1]);
355
356   /* verify this hasn't triggered a write yet */
357   /* FIXME: possibly racy, since if it would write, we may not get it
358    * immediately ? */
359   //fail_if_can_read ("first client, no buffer", pfd1[0]);
360
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);
365
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);
370
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]);
374
375   /* change the streamheader */
376
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);
382
383   /* drop our ref to the previous caps */
384   gst_caps_unref (caps);
385
386   gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
387       &caps);
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);
392
393   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
394   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
395
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]);
399
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);
405
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");
409
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);
416
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);
421
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);
428
429   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
430   gst_caps_unref (caps);
431 }
432
433 GST_END_TEST;
434
435 /* keep 100 bytes and burst 80 bytes to clients */
436 GST_START_TEST (test_burst_client_bytes)
437 {
438   GstElement *sink;
439   GstBuffer *buffer;
440   GstCaps *caps;
441   int pfd1[2];
442   int pfd2[2];
443   int pfd3[2];
444   gchar data[16];
445   gint i;
446   guint buffers_queued;
447
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);
454
455   fail_if (pipe (pfd1) == -1);
456   fail_if (pipe (pfd2) == -1);
457   fail_if (pipe (pfd3) == -1);
458
459   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
460
461   caps = gst_caps_from_string ("application/x-gst-check");
462   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
463
464   /* push buffers in, 9 * 16 bytes = 144 bytes */
465   for (i = 0; i < 9; i++) {
466     gchar *data;
467
468     buffer = gst_buffer_new_and_alloc (16);
469     gst_buffer_set_caps (buffer, caps);
470
471     /* copy some id */
472     data = (gchar *) GST_BUFFER_DATA (buffer);
473     g_snprintf (data, 16, "deadbee%08x", i);
474
475     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
476   }
477
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);
481
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);
488
489   /* push last buffer to make client fds ready for reading */
490   for (i = 9; i < 10; i++) {
491     gchar *data;
492
493     buffer = gst_buffer_new_and_alloc (16);
494     gst_buffer_set_caps (buffer, caps);
495
496     /* copy some id */
497     data = (gchar *) GST_BUFFER_DATA (buffer);
498     g_snprintf (data, 16, "deadbee%08x", i);
499
500     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
501   }
502
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);
515
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);
527
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);
537
538   GST_DEBUG ("cleaning up multifdsink");
539   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
540   cleanup_multifdsink (sink);
541
542   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
543   gst_caps_unref (caps);
544 }
545
546 GST_END_TEST;
547
548 /* keep 100 bytes and burst 80 bytes to clients */
549 GST_START_TEST (test_burst_client_bytes_keyframe)
550 {
551   GstElement *sink;
552   GstBuffer *buffer;
553   GstCaps *caps;
554   int pfd1[2];
555   int pfd2[2];
556   int pfd3[2];
557   gchar data[16];
558   gint i;
559   guint buffers_queued;
560
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);
567
568   fail_if (pipe (pfd1) == -1);
569   fail_if (pipe (pfd2) == -1);
570   fail_if (pipe (pfd3) == -1);
571
572   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
573
574   caps = gst_caps_from_string ("application/x-gst-check");
575   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
576
577   /* push buffers in, 9 * 16 bytes = 144 bytes */
578   for (i = 0; i < 9; i++) {
579     gchar *data;
580
581     buffer = gst_buffer_new_and_alloc (16);
582     gst_buffer_set_caps (buffer, caps);
583
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);
587
588     /* copy some id */
589     data = (gchar *) GST_BUFFER_DATA (buffer);
590     g_snprintf (data, 16, "deadbee%08x", i);
591
592     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
593   }
594
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);
598
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);
605
606   /* push last buffer to make client fds ready for reading */
607   for (i = 9; i < 10; i++) {
608     gchar *data;
609
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);
613
614     /* copy some id */
615     data = (gchar *) GST_BUFFER_DATA (buffer);
616     g_snprintf (data, 16, "deadbee%08x", i);
617
618     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
619   }
620
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);
636
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);
644
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);
652
653   GST_DEBUG ("cleaning up multifdsink");
654   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
655   cleanup_multifdsink (sink);
656
657   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
658   gst_caps_unref (caps);
659 }
660
661 GST_END_TEST;
662
663 /* keep 100 bytes and burst 80 bytes to clients */
664 GST_START_TEST (test_burst_client_bytes_with_keyframe)
665 {
666   GstElement *sink;
667   GstBuffer *buffer;
668   GstCaps *caps;
669   int pfd1[2];
670   int pfd2[2];
671   int pfd3[2];
672   gchar data[16];
673   gint i;
674   guint buffers_queued;
675
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);
682
683   fail_if (pipe (pfd1) == -1);
684   fail_if (pipe (pfd2) == -1);
685   fail_if (pipe (pfd3) == -1);
686
687   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
688
689   caps = gst_caps_from_string ("application/x-gst-check");
690   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
691
692   /* push buffers in, 9 * 16 bytes = 144 bytes */
693   for (i = 0; i < 9; i++) {
694     gchar *data;
695
696     buffer = gst_buffer_new_and_alloc (16);
697     gst_buffer_set_caps (buffer, caps);
698
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);
702
703     /* copy some id */
704     data = (gchar *) GST_BUFFER_DATA (buffer);
705     g_snprintf (data, 16, "deadbee%08x", i);
706
707     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
708   }
709
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);
713
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);
720
721   /* push last buffer to make client fds ready for reading */
722   for (i = 9; i < 10; i++) {
723     gchar *data;
724
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);
728
729     /* copy some id */
730     data = (gchar *) GST_BUFFER_DATA (buffer);
731     g_snprintf (data, 16, "deadbee%08x", i);
732
733     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
734   }
735
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);
751
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);
763
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);
773
774   GST_DEBUG ("cleaning up multifdsink");
775   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
776   cleanup_multifdsink (sink);
777
778   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
779   gst_caps_unref (caps);
780 }
781
782 GST_END_TEST;
783
784 /* Check that we can get data when multifdsink is configured in next-keyframe
785  * mode */
786 GST_START_TEST (test_client_next_keyframe)
787 {
788   GstElement *sink;
789   GstBuffer *buffer;
790   GstCaps *caps;
791   int pfd1[2];
792   gchar data[16];
793   gint i;
794
795   sink = setup_multifdsink ();
796   g_object_set (sink, "sync-method", 1, NULL);  /* 1 = next-keyframe */
797
798   fail_if (pipe (pfd1) == -1);
799
800   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
801
802   caps = gst_caps_from_string ("application/x-gst-check");
803   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
804
805   /* now add our client */
806   g_signal_emit_by_name (sink, "add", pfd1[1]);
807
808   /* push buffers in: keyframe, then non-keyframe */
809   for (i = 0; i < 2; i++) {
810     gchar *data;
811
812     buffer = gst_buffer_new_and_alloc (16);
813     gst_buffer_set_caps (buffer, caps);
814
815     /* copy some id */
816     data = (gchar *) GST_BUFFER_DATA (buffer);
817     g_snprintf (data, 16, "deadbee%08x", i);
818     if (i > 0)
819       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
820
821     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
822   }
823
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);
830
831   GST_DEBUG ("cleaning up multifdsink");
832   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
833   cleanup_multifdsink (sink);
834
835   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
836   gst_caps_unref (caps);
837 }
838
839 GST_END_TEST;
840
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
847  */
848 static Suite *
849 multifdsink_suite (void)
850 {
851   Suite *s = suite_create ("multifdsink");
852   TCase *tc_chain = tcase_create ("general");
853
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);
863
864   return s;
865 }
866
867 GST_CHECK_MAIN (multifdsink);