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