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