tests multihandle: verify number of handles
[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 /* FIXME: remove this header once formats are refactored */
30 #include "gst/tcp/gstmultifdsink.h"
31
32 static GstPad *mysrcpad;
33
34 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("application/x-gst-check")
38     );
39
40 static GstElement *
41 setup_multifdsink (void)
42 {
43   GstElement *multifdsink;
44
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);
49
50   return multifdsink;
51 }
52
53 static void
54 cleanup_multifdsink (GstElement * multifdsink)
55 {
56   GST_DEBUG ("cleanup_multifdsink");
57
58   gst_check_teardown_src_pad (multifdsink);
59   gst_check_teardown_element (multifdsink);
60 }
61
62 static void
63 wait_bytes_served (GstElement * sink, guint64 bytes)
64 {
65   guint64 bytes_served = 0;
66
67   while (bytes_served != bytes) {
68     g_object_get (sink, "bytes-served", &bytes_served, NULL);
69   }
70 }
71
72 /* FIXME: possibly racy, since if it would write, we may not get it
73  * immediately ? */
74 #define fail_if_can_read(msg,fd) \
75 G_STMT_START { \
76   long avail; \
77 \
78   fail_if (ioctl (fd, FIONREAD, &avail) < 0, "%s: could not ioctl", msg); \
79   fail_if (avail > 0, "%s: has bytes available to read"); \
80 } G_STMT_END;
81
82
83 GST_START_TEST (test_no_clients)
84 {
85   GstElement *sink;
86   GstBuffer *buffer;
87   GstCaps *caps;
88
89   sink = setup_multifdsink ();
90
91   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
92
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);
98
99   GST_DEBUG ("cleaning up multifdsink");
100   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
101   cleanup_multifdsink (sink);
102 }
103
104 GST_END_TEST;
105
106 GST_START_TEST (test_add_client)
107 {
108   GstElement *sink;
109   GstBuffer *buffer;
110   GstCaps *caps;
111   int pfd[2];
112   gchar data[4];
113
114   sink = setup_multifdsink ();
115
116   fail_if (pipe (pfd) == -1);
117
118   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
119
120   /* add the client */
121   g_signal_emit_by_name (sink, "add", pfd[1]);
122
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);
131
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);
136
137   GST_DEBUG ("cleaning up multifdsink");
138   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
139   cleanup_multifdsink (sink);
140
141   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
142   gst_caps_unref (caps);
143 }
144
145 GST_END_TEST;
146
147 #define fail_unless_read(msg,fd,size,ref) \
148 G_STMT_START { \
149   char data[size + 1]; \
150   int nbytes; \
151 \
152   GST_DEBUG ("%s: reading %d bytes", msg, size); \
153   nbytes = read (fd, data, size); \
154   data[size] = 0; \
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); \
159 } G_STMT_END;
160
161 #define fail_unless_num_handles(sink,num) \
162 G_STMT_START { \
163   gint handles; \
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); \
167 } G_STMT_END;
168
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 */
172 static void
173 gst_multifdsink_create_streamheader (const gchar * data1,
174     const gchar * data2, GstBuffer ** hbuf1, GstBuffer ** hbuf2,
175     GstCaps ** caps)
176 {
177   GstBuffer *buf;
178   GValue array = { 0 };
179   GValue value = { 0 };
180   GstStructure *structure;
181   guint size1 = strlen (data1);
182   guint size2 = strlen (data2);
183
184   fail_if (hbuf1 == NULL);
185   fail_if (hbuf2 == NULL);
186   fail_if (caps == NULL);
187
188   /* create caps with streamheader, set the caps, and push the IN_CAPS
189    * buffers */
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);
196
197   g_value_init (&array, GST_TYPE_ARRAY);
198
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);
207
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);
215
216   *caps = gst_caps_from_string ("application/x-gst-check");
217   structure = gst_caps_get_structure (*caps, 0);
218
219   gst_structure_set_value (structure, "streamheader", &array);
220   g_value_unset (&array);
221   ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
222
223   /* we want to keep them around for the tests */
224   gst_buffer_ref (*hbuf1);
225   gst_buffer_ref (*hbuf2);
226
227   GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
228 }
229
230
231 /* this test:
232  * - adds a first client
233  * - sets streamheader caps on the pad
234  * - pushes the IN_CAPS buffers
235  * - pushes a buffer
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
240  * - the new buffer
241  */
242 GST_START_TEST (test_streamheader)
243 {
244   GstElement *sink;
245   GstBuffer *hbuf1, *hbuf2, *buf;
246   GstCaps *caps;
247   int pfd1[2], pfd2[2];
248
249   sink = setup_multifdsink ();
250
251   fail_if (pipe (pfd1) == -1);
252   fail_if (pipe (pfd2) == -1);
253
254   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
255
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);
260
261   /* create caps with streamheader, set the caps, and push the IN_CAPS
262    * buffers */
263   gst_multifdsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
264       &caps);
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);
269
270   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
271   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
272
273   //FIXME:
274   //fail_if_can_read ("first client", pfd1[0]);
275
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);
281
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);
286
287   /* now add the second client */
288   g_signal_emit_by_name (sink, "add", pfd2[1]);
289   fail_unless_num_handles (sink, 2);
290   //FIXME:
291   //fail_if_can_read ("second client", pfd2[0]);
292
293   /* now push another buffer, which will trigger streamheader for second
294    * client */
295   buf = gst_buffer_new_and_alloc (4);
296   gst_buffer_fill (buf, 0, "deaf", 4);
297   gst_pad_push (mysrcpad, buf);
298
299   fail_unless_read ("first client", pfd1[0], 4, "deaf");
300
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);
306
307   GST_DEBUG ("cleaning up multifdsink");
308
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);
314
315   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
316   cleanup_multifdsink (sink);
317
318   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
319   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
320   gst_buffer_unref (hbuf1);
321   gst_buffer_unref (hbuf2);
322
323   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
324   gst_caps_unref (caps);
325 }
326
327 GST_END_TEST;
328
329 /* this tests changing of streamheaders
330  * - set streamheader caps on the pad
331  * - pushes the IN_CAPS buffers
332  * - pushes a buffer
333  * - add a first client
334  * - verifies that this first client receives the first streamheader caps,
335  *   plus a new buffer
336  * - change streamheader caps
337  * - verify that the first client receives the new streamheader buffers as well
338  */
339 GST_START_TEST (test_change_streamheader)
340 {
341   GstElement *sink;
342   GstBuffer *hbuf1, *hbuf2, *buf;
343   GstCaps *caps;
344   int pfd1[2], pfd2[2];
345
346   sink = setup_multifdsink ();
347
348   fail_if (pipe (pfd1) == -1);
349   fail_if (pipe (pfd2) == -1);
350
351   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
352
353   /* create caps with streamheader, set the caps, and push the IN_CAPS
354    * buffers */
355   gst_multifdsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
356       &caps);
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);
361
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);
365
366   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
367   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
368
369   /* add the first client */
370   g_signal_emit_by_name (sink, "add", pfd1[1]);
371
372   /* verify this hasn't triggered a write yet */
373   /* FIXME: possibly racy, since if it would write, we may not get it
374    * immediately ? */
375   //fail_if_can_read ("first client, no buffer", pfd1[0]);
376
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);
381
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);
386
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]);
390
391   /* change the streamheader */
392
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);
398
399   /* drop our ref to the previous caps */
400   gst_caps_unref (caps);
401
402   gst_multifdsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
403       &caps);
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);
408
409   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
410   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
411
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]);
415
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);
421
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");
425
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);
432
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);
437
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);
444
445   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
446   gst_caps_unref (caps);
447 }
448
449 GST_END_TEST;
450
451 static GstBuffer *
452 gst_new_buffer (int i)
453 {
454   GstMapInfo info;
455   gchar *data;
456
457   GstBuffer *buffer = gst_buffer_new_and_alloc (16);
458
459   /* copy some id */
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);
464
465   return buffer;
466 }
467
468
469 /* keep 100 bytes and burst 80 bytes to clients */
470 GST_START_TEST (test_burst_client_bytes)
471 {
472   GstElement *sink;
473   GstCaps *caps;
474   int pfd1[2];
475   int pfd2[2];
476   int pfd3[2];
477   gint i;
478   guint buffers_queued;
479
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);
486
487   fail_if (pipe (pfd1) == -1);
488   fail_if (pipe (pfd2) == -1);
489   fail_if (pipe (pfd3) == -1);
490
491   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
492
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);
496
497   /* push buffers in, 9 * 16 bytes = 144 bytes */
498   for (i = 0; i < 9; i++) {
499     GstBuffer *buffer = gst_new_buffer (i);
500
501     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
502   }
503
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);
507
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);
517
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);
521
522     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
523   }
524
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");
532
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");
540
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");
547
548   GST_DEBUG ("cleaning up multifdsink");
549   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
550   cleanup_multifdsink (sink);
551
552   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
553   gst_caps_unref (caps);
554 }
555
556 GST_END_TEST;
557
558 /* keep 100 bytes and burst 80 bytes to clients */
559 GST_START_TEST (test_burst_client_bytes_keyframe)
560 {
561   GstElement *sink;
562   GstCaps *caps;
563   int pfd1[2];
564   int pfd2[2];
565   int pfd3[2];
566   gint i;
567   guint buffers_queued;
568
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);
575
576   fail_if (pipe (pfd1) == -1);
577   fail_if (pipe (pfd2) == -1);
578   fail_if (pipe (pfd3) == -1);
579
580   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
581
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);
585
586   /* push buffers in, 9 * 16 bytes = 144 bytes */
587   for (i = 0; i < 9; i++) {
588     GstBuffer *buffer = gst_new_buffer (i);
589
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);
593
594     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
595   }
596
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);
600
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);
609
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);
613
614     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
615
616     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
617   }
618
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");
628
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");
634
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");
640
641   GST_DEBUG ("cleaning up multifdsink");
642   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
643   cleanup_multifdsink (sink);
644
645   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
646   gst_caps_unref (caps);
647 }
648
649 GST_END_TEST;
650
651 /* keep 100 bytes and burst 80 bytes to clients */
652 GST_START_TEST (test_burst_client_bytes_with_keyframe)
653 {
654   GstElement *sink;
655   GstCaps *caps;
656   int pfd1[2];
657   int pfd2[2];
658   int pfd3[2];
659   gint i;
660   guint buffers_queued;
661
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);
668
669   fail_if (pipe (pfd1) == -1);
670   fail_if (pipe (pfd2) == -1);
671   fail_if (pipe (pfd3) == -1);
672
673   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
674
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);
678
679   /* push buffers in, 9 * 16 bytes = 144 bytes */
680   for (i = 0; i < 9; i++) {
681     GstBuffer *buffer = gst_new_buffer (i);
682
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);
686
687     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
688   }
689
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);
693
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);
702
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);
706
707     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
708
709     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
710   }
711
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");
721
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");
729
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");
736
737   GST_DEBUG ("cleaning up multifdsink");
738   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
739   cleanup_multifdsink (sink);
740
741   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
742   gst_caps_unref (caps);
743 }
744
745 GST_END_TEST;
746
747 /* Check that we can get data when multifdsink is configured in next-keyframe
748  * mode */
749 GST_START_TEST (test_client_next_keyframe)
750 {
751   GstElement *sink;
752   GstCaps *caps;
753   int pfd1[2];
754   gint i;
755
756   sink = setup_multifdsink ();
757   g_object_set (sink, "sync-method", 1, NULL);  /* 1 = next-keyframe */
758
759   fail_if (pipe (pfd1) == -1);
760
761   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
762
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);
766
767   /* now add our client */
768   g_signal_emit_by_name (sink, "add", pfd1[1]);
769
770   /* push buffers in: keyframe, then non-keyframe */
771   for (i = 0; i < 2; i++) {
772     GstBuffer *buffer = gst_new_buffer (i);
773     if (i > 0)
774       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
775
776     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
777   }
778
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");
783
784   GST_DEBUG ("cleaning up multifdsink");
785   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
786   cleanup_multifdsink (sink);
787
788   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
789   gst_caps_unref (caps);
790 }
791
792 GST_END_TEST;
793
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
800  */
801 static Suite *
802 multifdsink_suite (void)
803 {
804   Suite *s = suite_create ("multifdsink");
805   TCase *tc_chain = tcase_create ("general");
806
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);
816
817   return s;
818 }
819
820 GST_CHECK_MAIN (multifdsink);