multisocketsink: copy over multifdsink unit tests, with FIXME
[platform/upstream/gstreamer.git] / tests / check / elements / multisocketsink.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 #include <sys/socket.h>
24 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
25 #include <sys/filio.h>
26 #endif
27
28 #include <gio/gio.h>
29 #include <gst/check/gstcheck.h>
30
31 static GstPad *mysrcpad;
32
33 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("application/x-gst-check")
37     );
38
39 static GstElement *
40 setup_multisocketsink (void)
41 {
42   GstElement *multisocketsink;
43
44   GST_DEBUG ("setup_multisocketsink");
45   multisocketsink = gst_check_setup_element ("multisocketsink");
46   mysrcpad = gst_check_setup_src_pad (multisocketsink, &srctemplate);
47   GST_PAD_UNSET_FLUSHING (mysrcpad);
48
49   return multisocketsink;
50 }
51
52 static void
53 cleanup_multisocketsink (GstElement * multisocketsink)
54 {
55   GST_DEBUG ("cleanup_multisocketsink");
56
57   gst_check_teardown_src_pad (multisocketsink);
58   gst_check_teardown_element (multisocketsink);
59 }
60
61 static void
62 wait_bytes_served (GstElement * sink, guint64 bytes)
63 {
64   guint64 bytes_served = 0;
65
66   while (bytes_served != bytes) {
67     g_object_get (sink, "bytes-served", &bytes_served, NULL);
68   }
69 }
70
71 /* FIXME: possibly racy, since if it would write, we may not get it
72  * immediately ? */
73 #define fail_if_can_read(msg,fd) \
74 G_STMT_START { \
75   long avail; \
76 \
77   fail_if (ioctl (fd, FIONREAD, &avail) < 0, "%s: could not ioctl", msg); \
78   fail_if (avail > 0, "%s: has bytes available to read"); \
79 } G_STMT_END;
80
81
82 GST_START_TEST (test_no_clients)
83 {
84   GstElement *sink;
85   GstBuffer *buffer;
86   GstCaps *caps;
87
88   sink = setup_multisocketsink ();
89
90   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
91
92   caps = gst_caps_from_string ("application/x-gst-check");
93   buffer = gst_buffer_new_and_alloc (4);
94   gst_pad_set_caps (mysrcpad, caps);
95   gst_caps_unref (caps);
96   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
97
98   GST_DEBUG ("cleaning up multisocketsink");
99   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
100   cleanup_multisocketsink (sink);
101 }
102
103 GST_END_TEST;
104
105 static gboolean
106 setup_handles (GSocket ** sinkhandle, GSocket ** srchandle)
107 {
108   GError *error = NULL;
109   gint sv[3];
110
111
112 //  g_assert (*sinkhandle);
113 //  g_assert (*srchandle);
114
115   fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, sv));
116
117   *sinkhandle = g_socket_new_from_fd (sv[1], &error);
118   fail_if (error);
119   fail_if (*sinkhandle == NULL);
120   *srchandle = g_socket_new_from_fd (sv[0], &error);
121   fail_if (error);
122   fail_if (*srchandle == NULL);
123
124   return TRUE;
125 }
126
127 static ssize_t
128 read_handle (GSocket * srchandle, void *buf, size_t count)
129 {
130   gssize ret;
131
132   ret = g_socket_receive (srchandle, buf, count, NULL, NULL);
133
134   return ret;
135 }
136
137 #define fail_unless_read(msg,handle,size,ref) \
138 G_STMT_START { \
139   char data[size + 1]; \
140   int nbytes; \
141 \
142   GST_DEBUG ("%s: reading %d bytes", msg, size); \
143   nbytes = read_handle (handle, data, size); \
144   data[size] = 0; \
145   GST_DEBUG ("%s: read %d bytes", msg, nbytes); \
146   fail_if (nbytes < size); \
147   fail_unless (memcmp (data, ref, size) == 0, \
148       "data read '%s' differs from '%s'", data, ref); \
149 } G_STMT_END;
150
151 GST_START_TEST (test_add_client)
152 {
153   GstElement *sink;
154   GstBuffer *buffer;
155   GstCaps *caps;
156   gchar data[4];
157   GSocket *sinksocket, *srcsocket;
158
159   sink = setup_multisocketsink ();
160   fail_unless (setup_handles (&sinksocket, &srcsocket));
161
162
163   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
164
165   /* add the client */
166   g_signal_emit_by_name (sink, "add", sinksocket);
167
168   caps = gst_caps_from_string ("application/x-gst-check");
169   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
170   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
171   buffer = gst_buffer_new_and_alloc (4);
172   gst_pad_set_caps (mysrcpad, caps);
173   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
174   gst_buffer_fill (buffer, 0, "dead", 4);
175   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
176
177   GST_DEBUG ("reading");
178   fail_if (read_handle (srcsocket, data, 4) < 4);
179   fail_unless (strncmp (data, "dead", 4) == 0);
180   wait_bytes_served (sink, 4);
181
182   GST_DEBUG ("cleaning up multisocketsink");
183   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
184   cleanup_multisocketsink (sink);
185
186   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
187   gst_caps_unref (caps);
188 }
189
190 GST_END_TEST;
191
192 /* from the given two data buffers, create two streamheader buffers and
193  * some caps that match it, and store them in the given pointers
194  * returns  one ref to each of the buffers and the caps */
195 static void
196 gst_multisocketsink_create_streamheader (const gchar * data1,
197     const gchar * data2, GstBuffer ** hbuf1, GstBuffer ** hbuf2,
198     GstCaps ** caps)
199 {
200   GstBuffer *buf;
201   GValue array = { 0 };
202   GValue value = { 0 };
203   GstStructure *structure;
204   guint size1 = strlen (data1);
205   guint size2 = strlen (data2);
206
207   fail_if (hbuf1 == NULL);
208   fail_if (hbuf2 == NULL);
209   fail_if (caps == NULL);
210
211   /* create caps with streamheader, set the caps, and push the IN_CAPS
212    * buffers */
213   *hbuf1 = gst_buffer_new_and_alloc (size1);
214   GST_BUFFER_FLAG_SET (*hbuf1, GST_BUFFER_FLAG_IN_CAPS);
215   gst_buffer_fill (*hbuf1, 0, data1, size1);
216   *hbuf2 = gst_buffer_new_and_alloc (size2);
217   GST_BUFFER_FLAG_SET (*hbuf2, GST_BUFFER_FLAG_IN_CAPS);
218   gst_buffer_fill (*hbuf2, 0, data2, size2);
219
220   g_value_init (&array, GST_TYPE_ARRAY);
221
222   g_value_init (&value, GST_TYPE_BUFFER);
223   /* we take a copy, set it on the array (which refs it), then unref our copy */
224   buf = gst_buffer_copy (*hbuf1);
225   gst_value_set_buffer (&value, buf);
226   ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
227   gst_buffer_unref (buf);
228   gst_value_array_append_value (&array, &value);
229   g_value_unset (&value);
230
231   g_value_init (&value, GST_TYPE_BUFFER);
232   buf = gst_buffer_copy (*hbuf2);
233   gst_value_set_buffer (&value, buf);
234   ASSERT_BUFFER_REFCOUNT (buf, "copied buffer", 2);
235   gst_buffer_unref (buf);
236   gst_value_array_append_value (&array, &value);
237   g_value_unset (&value);
238
239   *caps = gst_caps_from_string ("application/x-gst-check");
240   structure = gst_caps_get_structure (*caps, 0);
241
242   gst_structure_set_value (structure, "streamheader", &array);
243   g_value_unset (&array);
244   ASSERT_CAPS_REFCOUNT (*caps, "streamheader caps", 1);
245
246   /* we want to keep them around for the tests */
247   gst_buffer_ref (*hbuf1);
248   gst_buffer_ref (*hbuf2);
249
250   GST_DEBUG ("created streamheader caps %p %" GST_PTR_FORMAT, *caps, *caps);
251 }
252
253
254 /* this test:
255  * - adds a first client
256  * - sets streamheader caps on the pad
257  * - pushes the IN_CAPS buffers
258  * - pushes a buffer
259  * - verifies that the client received all the data correctly, and did not
260  *   get multiple copies of the streamheader
261  * - adds a second client
262  * - verifies that this second client receives the streamheader caps too, plus
263  * - the new buffer
264  */
265 GST_START_TEST (test_streamheader)
266 {
267   GstElement *sink;
268   GstBuffer *hbuf1, *hbuf2, *buf;
269   GstCaps *caps;
270   GSocket *socket[4];
271
272   sink = setup_multisocketsink ();
273
274   fail_unless (setup_handles (&socket[0], &socket[1]));
275   fail_unless (setup_handles (&socket[2], &socket[3]));
276
277   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
278
279   /* add the first client */
280   g_signal_emit_by_name (sink, "add", socket[0]);
281
282   /* create caps with streamheader, set the caps, and push the IN_CAPS
283    * buffers */
284   gst_multisocketsink_create_streamheader ("babe", "deadbeef", &hbuf1, &hbuf2,
285       &caps);
286   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
287   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
288   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
289   fail_unless (gst_pad_set_caps (mysrcpad, caps));
290   /* one is ours, two from set_caps */
291   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
292
293   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
294   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
295   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
296   // FIXME: we can't assert on the refcount because giving away the ref
297   //        doesn't mean the refcount decreases
298   // ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
299
300   //FIXME:
301   //fail_if_can_read ("first client", socket[1]);
302
303   /* push a non-IN_CAPS buffer, this should trigger the client receiving the
304    * first three buffers */
305   buf = gst_buffer_new_and_alloc (4);
306   gst_buffer_fill (buf, 0, "f00d", 4);
307   gst_pad_push (mysrcpad, buf);
308
309   fail_unless_read ("first client", socket[1], 4, "babe");
310   fail_unless_read ("first client", socket[1], 8, "deadbeef");
311   fail_unless_read ("first client", socket[1], 4, "f00d");
312   wait_bytes_served (sink, 16);
313
314   /* now add the second client */
315   g_signal_emit_by_name (sink, "add", socket[2]);
316   //FIXME:
317   //fail_if_can_read ("second client", socket[3]);
318
319   /* now push another buffer, which will trigger streamheader for second
320    * client */
321   buf = gst_buffer_new_and_alloc (4);
322   gst_buffer_fill (buf, 0, "deaf", 4);
323   gst_pad_push (mysrcpad, buf);
324
325   fail_unless_read ("first client", socket[1], 4, "deaf");
326
327   fail_unless_read ("second client", socket[3], 4, "babe");
328   fail_unless_read ("second client", socket[3], 8, "deadbeef");
329   /* we missed the f00d buffer */
330   fail_unless_read ("second client", socket[3], 4, "deaf");
331   wait_bytes_served (sink, 36);
332
333   GST_DEBUG ("cleaning up multisocketsink");
334
335   g_signal_emit_by_name (sink, "remove", socket[0]);
336   g_signal_emit_by_name (sink, "remove", socket[2]);
337
338   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
339   cleanup_multisocketsink (sink);
340
341   // FIXME for slomo: these refcounts end up as 0 instead of 1
342   //ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
343   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 0);
344   //ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
345   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 0);
346   //gst_buffer_unref (hbuf1);
347   //gst_buffer_unref (hbuf2);
348
349   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
350   gst_caps_unref (caps);
351 }
352
353 GST_END_TEST;
354
355 /* this tests changing of streamheaders
356  * - set streamheader caps on the pad
357  * - pushes the IN_CAPS buffers
358  * - pushes a buffer
359  * - add a first client
360  * - verifies that this first client receives the first streamheader caps,
361  *   plus a new buffer
362  * - change streamheader caps
363  * - verify that the first client receives the new streamheader buffers as well
364  */
365 GST_START_TEST (test_change_streamheader)
366 {
367   GstElement *sink;
368   GstBuffer *hbuf1, *hbuf2, *buf;
369   GstCaps *caps;
370   GSocket *socket[4];
371
372   sink = setup_multisocketsink ();
373
374   fail_unless (setup_handles (&socket[0], &socket[1]));
375   fail_unless (setup_handles (&socket[2], &socket[3]));
376
377   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
378
379   /* create caps with streamheader, set the caps, and push the IN_CAPS
380    * buffers */
381   gst_multisocketsink_create_streamheader ("first", "header", &hbuf1, &hbuf2,
382       &caps);
383   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
384   fail_unless (gst_pad_set_caps (mysrcpad, caps));
385   /* one is ours, two from set_caps */
386   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
387
388   /* one to hold for the test and one to give away */
389   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
390   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
391
392   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
393   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
394
395   /* add the first client */
396   g_signal_emit_by_name (sink, "add", socket[0]);
397
398   /* verify this hasn't triggered a write yet */
399   /* FIXME: possibly racy, since if it would write, we may not get it
400    * immediately ? */
401   //fail_if_can_read ("first client, no buffer", socket[1]);
402
403   /* now push a buffer and read */
404   buf = gst_buffer_new_and_alloc (4);
405   gst_buffer_fill (buf, 0, "f00d", 4);
406   gst_pad_push (mysrcpad, buf);
407
408   fail_unless_read ("change: first client", socket[1], 5, "first");
409   fail_unless_read ("change: first client", socket[1], 6, "header");
410   fail_unless_read ("change: first client", socket[1], 4, "f00d");
411   //wait_bytes_served (sink, 16);
412
413   /* now add the second client */
414   g_signal_emit_by_name (sink, "add", socket[2]);
415   //fail_if_can_read ("second client, no buffer", socket[3]);
416
417   /* change the streamheader */
418
419   /* before we change, multisocketsink still has a list of the old streamheaders */
420   // FIXME for slomo: it actually has refcount 1
421   // ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
422   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
423   //ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
424   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
425   // FIXME: which means we can't drop our refs
426   // gst_buffer_unref (hbuf1);
427   // gst_buffer_unref (hbuf2);
428
429   /* drop our ref to the previous caps */
430   gst_caps_unref (caps);
431
432   gst_multisocketsink_create_streamheader ("second", "header", &hbuf1, &hbuf2,
433       &caps);
434   fail_unless (gst_pad_set_caps (mysrcpad, caps));
435   /* one to hold for the test and one to give away */
436   ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 2);
437   ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 2);
438
439   fail_unless (gst_pad_push (mysrcpad, hbuf1) == GST_FLOW_OK);
440   fail_unless (gst_pad_push (mysrcpad, hbuf2) == GST_FLOW_OK);
441
442   /* verify neither client has new data available to read */
443   //fail_if_can_read ("first client, changed streamheader", socket[1]);
444   //fail_if_can_read ("second client, changed streamheader", socket[3]);
445
446   /* now push another buffer, which will trigger streamheader for second
447    * client, but should also send new streamheaders to first client */
448   buf = gst_buffer_new_and_alloc (8);
449   gst_buffer_fill (buf, 0, "deadbabe", 8);
450   gst_pad_push (mysrcpad, buf);
451
452   fail_unless_read ("first client", socket[1], 6, "second");
453   fail_unless_read ("first client", socket[1], 6, "header");
454   fail_unless_read ("first client", socket[1], 8, "deadbabe");
455
456   /* new streamheader data */
457   fail_unless_read ("second client", socket[3], 6, "second");
458   fail_unless_read ("second client", socket[3], 6, "header");
459   /* we missed the f00d buffer */
460   fail_unless_read ("second client", socket[3], 8, "deadbabe");
461   //wait_bytes_served (sink, 36);
462
463   GST_DEBUG ("cleaning up multisocketsink");
464   g_signal_emit_by_name (sink, "remove", socket[0]);
465   g_signal_emit_by_name (sink, "remove", socket[2]);
466   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
467
468   /* setting to NULL should have cleared the streamheader */
469   /* FIXME for slomo: again, these buffers end up with rc 0
470      ASSERT_BUFFER_REFCOUNT (hbuf1, "hbuf1", 1);
471      ASSERT_BUFFER_REFCOUNT (hbuf2, "hbuf2", 1);
472      gst_buffer_unref (hbuf1);
473      gst_buffer_unref (hbuf2);
474    */
475   cleanup_multisocketsink (sink);
476
477   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
478   gst_caps_unref (caps);
479 }
480
481 GST_END_TEST;
482
483 static GstBuffer *
484 gst_new_buffer (int i)
485 {
486   GstMapInfo info;
487   gchar *data;
488
489   GstBuffer *buffer = gst_buffer_new_and_alloc (16);
490
491   /* copy some id */
492   g_assert (gst_buffer_map (buffer, &info, GST_MAP_WRITE));
493   data = (gchar *) info.data;
494   g_snprintf (data, 16, "deadbee%08x", i);
495   gst_buffer_unmap (buffer, &info);
496
497   return buffer;
498 }
499
500
501 /* keep 100 bytes and burst 80 bytes to clients */
502 GST_START_TEST (test_burst_client_bytes)
503 {
504   GstElement *sink;
505   GstCaps *caps;
506   GSocket *socket[6];
507   gint i;
508   guint buffers_queued;
509
510   sink = setup_multisocketsink ();
511   /* make sure we keep at least 100 bytes at all times */
512   g_object_set (sink, "bytes-min", 100, NULL);
513   g_object_set (sink, "sync-method", 3, NULL);  /* 3 = burst */
514   g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
515   g_object_set (sink, "burst-value", (guint64) 80, NULL);
516
517   fail_unless (setup_handles (&socket[0], &socket[1]));
518   fail_unless (setup_handles (&socket[2], &socket[3]));
519   fail_unless (setup_handles (&socket[4], &socket[5]));
520
521   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
522
523   caps = gst_caps_from_string ("application/x-gst-check");
524   gst_pad_set_caps (mysrcpad, caps);
525   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
526
527   /* push buffers in, 9 * 16 bytes = 144 bytes */
528   for (i = 0; i < 9; i++) {
529     GstBuffer *buffer = gst_new_buffer (i);
530
531     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
532   }
533
534   /* check that at least 7 buffers (112 bytes) are in the queue */
535   g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
536   fail_if (buffers_queued != 7);
537
538   /* now add the clients */
539   g_signal_emit_by_name (sink, "add", socket[0]);
540   g_signal_emit_by_name (sink, "add_full", socket[2], 3,
541       3, (guint64) 50, 3, (guint64) 200);
542   g_signal_emit_by_name (sink, "add_full", socket[4], 3,
543       3, (guint64) 50, 3, (guint64) 50);
544
545   /* push last buffer to make client fds ready for reading */
546   for (i = 9; i < 10; i++) {
547     GstBuffer *buffer = gst_new_buffer (i);
548
549     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
550   }
551
552   /* now we should only read the last 5 buffers (5 * 16 = 80 bytes) */
553   GST_DEBUG ("Reading from client 1");
554   fail_unless_read ("client 1", socket[1], 16, "deadbee00000005");
555   fail_unless_read ("client 1", socket[1], 16, "deadbee00000006");
556   fail_unless_read ("client 1", socket[1], 16, "deadbee00000007");
557   fail_unless_read ("client 1", socket[1], 16, "deadbee00000008");
558   fail_unless_read ("client 1", socket[1], 16, "deadbee00000009");
559
560   /* second client only bursts 50 bytes = 4 buffers (we get 4 buffers since
561    * the max allows it) */
562   GST_DEBUG ("Reading from client 2");
563   // FIXME for slomo: this client receives starting from 3 when it should
564   // start from 6!
565   fail_unless_read ("client 2", socket[3], 16, "deadbee00000003");
566   fail_unless_read ("client 2", socket[3], 16, "deadbee00000004");
567   fail_unless_read ("client 2", socket[3], 16, "deadbee00000005");
568   fail_unless_read ("client 2", socket[3], 16, "deadbee00000006");
569   fail_unless_read ("client 2", socket[3], 16, "deadbee00000007");
570   fail_unless_read ("client 2", socket[3], 16, "deadbee00000008");
571   fail_unless_read ("client 2", socket[3], 16, "deadbee00000009");
572
573   /* third client only bursts 50 bytes = 4 buffers, we can't send
574    * more than 50 bytes so we only get 3 buffers (48 bytes). */
575   // FIXME for slomo: this client receives starting from 3 when it should
576   // start from 7!
577   GST_DEBUG ("Reading from client 3");
578   fail_unless_read ("client 3", socket[5], 16, "deadbee00000003");
579   fail_unless_read ("client 3", socket[5], 16, "deadbee00000004");
580   fail_unless_read ("client 3", socket[5], 16, "deadbee00000005");
581   fail_unless_read ("client 3", socket[5], 16, "deadbee00000006");
582   fail_unless_read ("client 3", socket[5], 16, "deadbee00000007");
583   fail_unless_read ("client 3", socket[5], 16, "deadbee00000008");
584   fail_unless_read ("client 3", socket[5], 16, "deadbee00000009");
585
586   GST_DEBUG ("cleaning up multisocketsink");
587   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
588   cleanup_multisocketsink (sink);
589
590   // FIXME for slomo: the refcount is now 3 instead of 1 ?
591   // ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
592   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
593   gst_caps_unref (caps);
594 }
595
596 GST_END_TEST;
597
598 /* keep 100 bytes and burst 80 bytes to clients */
599 GST_START_TEST (test_burst_client_bytes_keyframe)
600 {
601   GstElement *sink;
602   GstCaps *caps;
603   GSocket *socket[6];
604   gint i;
605   guint buffers_queued;
606
607   sink = setup_multisocketsink ();
608   /* make sure we keep at least 100 bytes at all times */
609   g_object_set (sink, "bytes-min", 100, NULL);
610   g_object_set (sink, "sync-method", 4, NULL);  /* 3 = burst_keyframe */
611   g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
612   g_object_set (sink, "burst-value", (guint64) 80, NULL);
613
614   fail_unless (setup_handles (&socket[0], &socket[1]));
615   fail_unless (setup_handles (&socket[2], &socket[3]));
616   fail_unless (setup_handles (&socket[4], &socket[5]));
617
618   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
619
620   caps = gst_caps_from_string ("application/x-gst-check");
621   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
622   gst_pad_set_caps (mysrcpad, caps);
623
624   /* push buffers in, 9 * 16 bytes = 144 bytes */
625   for (i = 0; i < 9; i++) {
626     GstBuffer *buffer = gst_new_buffer (i);
627
628     /* mark most buffers as delta */
629     if (i != 0 && i != 4 && i != 8)
630       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
631
632     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
633   }
634
635   /* check that at least 7 buffers (112 bytes) are in the queue */
636   g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
637   fail_if (buffers_queued != 7);
638
639   /* now add the clients */
640   g_signal_emit_by_name (sink, "add", socket[0]);
641   g_signal_emit_by_name (sink, "add_full", socket[2], 4,
642       3, (guint64) 50, 3, (guint64) 90);
643   g_signal_emit_by_name (sink, "add_full", socket[4], 4,
644       3, (guint64) 50, 3, (guint64) 50);
645
646   /* push last buffer to make client fds ready for reading */
647   for (i = 9; i < 10; i++) {
648     GstBuffer *buffer = gst_new_buffer (i);
649
650     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
651
652     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
653   }
654
655   /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
656    * keyframe at buffer 4 */
657   GST_DEBUG ("Reading from client 1");
658   fail_unless_read ("client 1", socket[1], 16, "deadbee00000004");
659   fail_unless_read ("client 1", socket[1], 16, "deadbee00000005");
660   fail_unless_read ("client 1", socket[1], 16, "deadbee00000006");
661   fail_unless_read ("client 1", socket[1], 16, "deadbee00000007");
662   fail_unless_read ("client 1", socket[1], 16, "deadbee00000008");
663   fail_unless_read ("client 1", socket[1], 16, "deadbee00000009");
664
665   /* second client only bursts 50 bytes = 4 buffers, there is
666    * no keyframe above min and below max, so get one below min */
667   GST_DEBUG ("Reading from client 2");
668   // FIXME for slomo: should start from 8, not from 4
669   fail_unless_read ("client 2", socket[3], 16, "deadbee00000004");
670   fail_unless_read ("client 2", socket[3], 16, "deadbee00000005");
671   fail_unless_read ("client 2", socket[3], 16, "deadbee00000006");
672   fail_unless_read ("client 2", socket[3], 16, "deadbee00000007");
673
674   fail_unless_read ("client 2", socket[3], 16, "deadbee00000008");
675   fail_unless_read ("client 2", socket[3], 16, "deadbee00000009");
676
677   /* third client only bursts 50 bytes = 4 buffers, we can't send
678    * more than 50 bytes so we only get 2 buffers (32 bytes). */
679   GST_DEBUG ("Reading from client 3");
680   // FIXME for slomo: should start from 8, not from 4
681   fail_unless_read ("client 3", socket[5], 16, "deadbee00000004");
682   fail_unless_read ("client 3", socket[5], 16, "deadbee00000005");
683   fail_unless_read ("client 3", socket[5], 16, "deadbee00000006");
684   fail_unless_read ("client 3", socket[5], 16, "deadbee00000007");
685
686   fail_unless_read ("client 3", socket[5], 16, "deadbee00000008");
687   fail_unless_read ("client 3", socket[5], 16, "deadbee00000009");
688
689   GST_DEBUG ("cleaning up multisocketsink");
690   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
691   cleanup_multisocketsink (sink);
692
693   // FIXME for slomo: the refcount is now 3 instead of 1 ?
694   // ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
695   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
696   gst_caps_unref (caps);
697 }
698
699 GST_END_TEST;
700
701
702
703 /* keep 100 bytes and burst 80 bytes to clients */
704 GST_START_TEST (test_burst_client_bytes_with_keyframe)
705 {
706   GstElement *sink;
707   GstCaps *caps;
708   GSocket *socket[6];
709   gint i;
710   guint buffers_queued;
711
712   sink = setup_multisocketsink ();
713
714   /* make sure we keep at least 100 bytes at all times */
715   g_object_set (sink, "bytes-min", 100, NULL);
716   g_object_set (sink, "sync-method", 5, NULL);  /* 3 = burst_with_keyframe */
717   g_object_set (sink, "burst-format", GST_FORMAT_BYTES, NULL);
718   g_object_set (sink, "burst-value", (guint64) 80, NULL);
719
720   fail_unless (setup_handles (&socket[0], &socket[1]));
721   fail_unless (setup_handles (&socket[2], &socket[3]));
722   fail_unless (setup_handles (&socket[4], &socket[5]));
723
724   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
725
726   caps = gst_caps_from_string ("application/x-gst-check");
727   gst_pad_set_caps (mysrcpad, caps);
728   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
729
730   /* push buffers in, 9 * 16 bytes = 144 bytes */
731   for (i = 0; i < 9; i++) {
732     GstBuffer *buffer = gst_new_buffer (i);
733
734     /* mark most buffers as delta */
735     if (i != 0 && i != 4 && i != 8)
736       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
737
738     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
739   }
740
741   /* check that at least 7 buffers (112 bytes) are in the queue */
742   g_object_get (sink, "buffers-queued", &buffers_queued, NULL);
743   fail_if (buffers_queued != 7);
744
745   /* now add the clients */
746   g_signal_emit_by_name (sink, "add", socket[0]);
747   g_signal_emit_by_name (sink, "add_full", socket[2], 5,
748       3, (guint64) 50, 3, (guint64) 90);
749   g_signal_emit_by_name (sink, "add_full", socket[4], 5,
750       3, (guint64) 50, 3, (guint64) 50);
751
752   /* push last buffer to make client fds ready for reading */
753   for (i = 9; i < 10; i++) {
754     GstBuffer *buffer = gst_new_buffer (i);
755
756     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
757
758     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
759   }
760
761   /* now we should only read the last 6 buffers (min 5 * 16 = 80 bytes),
762    * keyframe at buffer 4 */
763   GST_DEBUG ("Reading from client 1");
764   fail_unless_read ("client 1", socket[1], 16, "deadbee00000004");
765   fail_unless_read ("client 1", socket[1], 16, "deadbee00000005");
766   fail_unless_read ("client 1", socket[1], 16, "deadbee00000006");
767   fail_unless_read ("client 1", socket[1], 16, "deadbee00000007");
768   fail_unless_read ("client 1", socket[1], 16, "deadbee00000008");
769   fail_unless_read ("client 1", socket[1], 16, "deadbee00000009");
770
771   /* second client only bursts 50 bytes = 4 buffers, there is
772    * no keyframe above min and below max, so send min */
773   GST_DEBUG ("Reading from client 2");
774   // FIXME for slomo: should start from 6, not from 3
775   fail_unless_read ("client 2", socket[3], 16, "deadbee00000003");
776   fail_unless_read ("client 2", socket[3], 16, "deadbee00000004");
777   fail_unless_read ("client 2", socket[3], 16, "deadbee00000005");
778
779   fail_unless_read ("client 2", socket[3], 16, "deadbee00000006");
780   fail_unless_read ("client 2", socket[3], 16, "deadbee00000007");
781   fail_unless_read ("client 2", socket[3], 16, "deadbee00000008");
782   fail_unless_read ("client 2", socket[3], 16, "deadbee00000009");
783
784   /* third client only bursts 50 bytes = 4 buffers, we can't send
785    * more than 50 bytes so we only get 3 buffers (48 bytes). */
786   GST_DEBUG ("Reading from client 3");
787   // FIXME for slomo: should start from 7, not from 3
788   fail_unless_read ("client 3", socket[5], 16, "deadbee00000003");
789   fail_unless_read ("client 3", socket[5], 16, "deadbee00000004");
790   fail_unless_read ("client 3", socket[5], 16, "deadbee00000005");
791   fail_unless_read ("client 3", socket[5], 16, "deadbee00000006");
792
793   fail_unless_read ("client 3", socket[5], 16, "deadbee00000007");
794   fail_unless_read ("client 3", socket[5], 16, "deadbee00000008");
795   fail_unless_read ("client 3", socket[5], 16, "deadbee00000009");
796
797   GST_DEBUG ("cleaning up multisocketsink");
798   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
799   cleanup_multisocketsink (sink);
800
801   // FIXME for slomo: the refcount is now 3 instead of 1 ?
802   // ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
803   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
804   gst_caps_unref (caps);
805 }
806
807 GST_END_TEST;
808
809 /* Check that we can get data when multisocketsink is configured in next-keyframe
810  * mode */
811 GST_START_TEST (test_client_next_keyframe)
812 {
813   GstElement *sink;
814   GstCaps *caps;
815   GSocket *socket[2];
816   gint i;
817
818   sink = setup_multisocketsink ();
819   g_object_set (sink, "sync-method", 1, NULL);  /* 1 = next-keyframe */
820
821   fail_unless (setup_handles (&socket[0], &socket[1]));
822
823   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
824
825   caps = gst_caps_from_string ("application/x-gst-check");
826   gst_pad_set_caps (mysrcpad, caps);
827   GST_DEBUG ("Created test caps %p %" GST_PTR_FORMAT, caps, caps);
828
829   /* now add our client */
830   g_signal_emit_by_name (sink, "add", socket[0]);
831
832   /* push buffers in: keyframe, then non-keyframe */
833   for (i = 0; i < 2; i++) {
834     GstBuffer *buffer = gst_new_buffer (i);
835     if (i > 0)
836       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
837
838     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
839   }
840
841   /* now we should be able to read some data */
842   GST_DEBUG ("Reading from client 1");
843   fail_unless_read ("client 1", socket[1], 16, "deadbee00000000");
844   fail_unless_read ("client 1", socket[1], 16, "deadbee00000001");
845
846   GST_DEBUG ("cleaning up multisocketsink");
847   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
848   cleanup_multisocketsink (sink);
849
850   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
851   gst_caps_unref (caps);
852 }
853
854 GST_END_TEST;
855
856 /* FIXME: add test simulating chained oggs where:
857  * sync-method is burst-on-connect
858  * (when multisocketsink actually does burst-on-connect based on byte size, not
859    "last keyframe" which any frame for audio :))
860  * an old client still needs to read from before the new streamheaders
861  * a new client gets the new streamheaders
862  */
863 static Suite *
864 multisocketsink_suite (void)
865 {
866   Suite *s = suite_create ("multisocketsink");
867   TCase *tc_chain = tcase_create ("general");
868
869   suite_add_tcase (s, tc_chain);
870   tcase_add_test (tc_chain, test_no_clients);
871   tcase_add_test (tc_chain, test_add_client);
872   tcase_add_test (tc_chain, test_streamheader);
873   tcase_add_test (tc_chain, test_change_streamheader);
874   tcase_add_test (tc_chain, test_burst_client_bytes);
875   tcase_add_test (tc_chain, test_burst_client_bytes_keyframe);
876   tcase_add_test (tc_chain, test_burst_client_bytes_with_keyframe);
877   tcase_add_test (tc_chain, test_client_next_keyframe);
878
879   return s;
880 }
881
882 GST_CHECK_MAIN (multisocketsink);