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