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