3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
24 #include <gst/check/gstcheck.h>
25 #include <gst/dataprotocol/dataprotocol.h>
27 gboolean have_eos = FALSE;
29 /* For ease of programming we use globals to keep refs for our floating
30 * src and sink pads we create; otherwise we always have to do get_pad,
31 * get_peer, and then remove references in every test function */
32 GstPad *mysrcpad, *myshsrcpad, *mysinkpad;
34 #define AUDIO_CAPS_TEMPLATE_STRING \
36 "rate = (int) [ 1, MAX ], " \
37 "channels = (int) [ 1, 8 ], " \
38 "endianness = (int) BYTE_ORDER, " \
39 "width = (int) {8, 16}, " \
40 "depth = (int) {8, 16}, " \
41 "signed = (boolean) true"
43 #define AUDIO_CAPS_STRING \
45 "rate = (int) 1000, " \
46 "channels = (int) 2, " \
47 "endianness = (int) BYTE_ORDER, " \
48 "width = (int) 16, " \
49 "depth = (int) 16, " \
50 "signed = (boolean) true"
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-gdp")
58 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
64 /* takes over reference for outcaps */
70 GST_DEBUG ("setup_gdppay");
71 gdppay = gst_check_setup_element ("gdppay");
72 mysrcpad = gst_check_setup_src_pad (gdppay, &srctemplate, NULL);
73 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
74 gst_pad_set_active (mysrcpad, TRUE);
75 gst_pad_set_active (mysinkpad, TRUE);
81 cleanup_gdppay (GstElement * gdppay)
83 GST_DEBUG ("cleanup_gdppay");
86 gst_pad_set_active (mysrcpad, FALSE);
88 gst_pad_set_active (myshsrcpad, FALSE);
89 gst_pad_set_active (mysinkpad, FALSE);
90 gst_check_teardown_src_pad (gdppay);
91 gst_check_teardown_sink_pad (gdppay);
92 gst_check_teardown_element (gdppay);
97 GST_START_TEST (test_audio)
101 GstBuffer *inbuffer, *outbuffer;
106 gdppay = setup_gdppay ();
108 fail_unless (gst_element_set_state (gdppay,
109 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
110 "could not set to playing");
112 GST_DEBUG ("new segment");
114 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
115 fail_unless (gst_pad_push_event (mysrcpad, event));
117 /* no buffer should be pushed yet, waiting for caps */
118 fail_unless_equals_int (g_list_length (buffers), 0);
120 GST_DEBUG ("first buffer");
121 inbuffer = gst_buffer_new_and_alloc (4);
122 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
123 gst_buffer_set_caps (inbuffer, caps);
124 caps_string = gst_caps_to_string (caps);
126 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
128 /* pushing gives away my reference */
129 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
131 /* we should have three buffers now */
132 fail_unless_equals_int (g_list_length (buffers), 3);
134 /* first buffer is the serialized new_segment event;
135 * the element also holds a ref to it */
136 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
137 buffers = g_list_remove (buffers, outbuffer);
138 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
139 gst_buffer_unref (outbuffer);
141 /* second buffer is the serialized caps;
142 * the element also holds a ref to it */
143 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
144 buffers = g_list_remove (buffers, outbuffer);
145 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
146 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
147 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
148 gst_buffer_unref (outbuffer);
150 /* the third buffer is the GDP buffer for our pushed buffer */
151 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
152 buffers = g_list_remove (buffers, outbuffer);
153 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
154 length = GST_DP_HEADER_LENGTH + 4;
155 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
156 gst_buffer_unref (outbuffer);
159 GST_DEBUG ("second buffer");
160 inbuffer = gst_buffer_new_and_alloc (4);
161 gst_buffer_set_caps (inbuffer, caps);
163 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
165 /* pushing gives away my reference */
166 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
168 fail_unless_equals_int (g_list_length (buffers), 1);
169 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
170 buffers = g_list_remove (buffers, outbuffer);
171 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
173 /* the third output buffer is data */
174 length = GST_DP_HEADER_LENGTH + 4;
175 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
176 gst_buffer_unref (outbuffer);
178 /* a third buffer without caps set explicitly; should work */
179 GST_DEBUG ("Creating third buffer, no caps set");
180 inbuffer = gst_buffer_new_and_alloc (4);
182 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
184 /* pushing gives away my reference */
185 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
187 fail_unless_equals_int (g_list_length (buffers), 1);
188 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
189 buffers = g_list_remove (buffers, outbuffer);
190 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
192 /* the fourth output buffer is data */
193 length = GST_DP_HEADER_LENGTH + 4;
194 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
195 gst_buffer_unref (outbuffer);
198 fail_unless (gst_element_set_state (gdppay,
199 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
201 gst_caps_unref (caps);
202 g_free (caps_string);
203 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
204 g_list_free (buffers);
206 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
207 gst_object_unref (gdppay);
212 static GstStaticPadTemplate shsrctemplate = GST_STATIC_PAD_TEMPLATE ("src",
215 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
220 setup_gdppay_streamheader ()
224 GST_DEBUG ("setup_gdppay");
225 gdppay = gst_check_setup_element ("gdppay");
226 myshsrcpad = gst_check_setup_src_pad (gdppay, &shsrctemplate, NULL);
227 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
228 gst_pad_set_active (myshsrcpad, TRUE);
229 gst_pad_set_active (mysinkpad, TRUE);
234 /* this test serializes a stream that already has a streamheader of its own.
235 * the streamheader should then be serialized and put on the GDP stream's
237 GST_START_TEST (test_streamheader)
239 GstCaps *caps, *sinkcaps;
241 GstBuffer *inbuffer, *outbuffer, *shbuffer;
245 GstStructure *structure;
246 GValue array = { 0 };
247 GValue value = { 0 };
252 gdppay = setup_gdppay_streamheader ();
254 fail_unless (gst_element_set_state (gdppay,
255 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
256 "could not set to playing");
258 GST_DEBUG ("new segment");
260 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
261 fail_unless (gst_pad_push_event (myshsrcpad, event));
263 /* no buffer should be pushed yet, still waiting for caps */
264 fail_unless_equals_int (g_list_length (buffers), 0);
266 GST_DEBUG ("first buffer");
267 inbuffer = gst_buffer_new_and_alloc (4);
268 memcpy (GST_BUFFER_DATA (inbuffer), "head", 4);
269 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
270 structure = gst_caps_get_structure (caps, 0);
271 GST_BUFFER_FLAG_SET (inbuffer, GST_BUFFER_FLAG_IN_CAPS);
272 g_value_init (&array, GST_TYPE_ARRAY);
273 g_value_init (&value, GST_TYPE_BUFFER);
274 shbuffer = gst_buffer_copy (inbuffer);
275 gst_value_set_buffer (&value, shbuffer);
276 gst_buffer_unref (shbuffer);
277 gst_value_array_append_value (&array, &value);
278 g_value_unset (&value);
279 gst_structure_set_value (structure, "streamheader", &array);
280 g_value_unset (&array);
281 caps_string = gst_caps_to_string (caps);
283 gst_buffer_set_caps (inbuffer, caps);
284 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
286 /* pushing gives away my reference */
287 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
289 /* we should have three buffers now */
290 fail_unless_equals_int (g_list_length (buffers), 3);
292 /* our sink pad should now have GDP caps with a streamheader that includes
293 * GDP wrappings of our streamheader */
294 sinkcaps = gst_pad_get_negotiated_caps (mysinkpad);
295 structure = gst_caps_get_structure (sinkcaps, 0);
296 fail_unless_equals_string ((gchar *) gst_structure_get_name (structure),
297 "application/x-gdp");
298 fail_unless (gst_structure_has_field (structure, "streamheader"));
299 sh = gst_structure_get_value (structure, "streamheader");
300 fail_unless (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
301 shbuffers = g_value_peek_pointer (sh);
302 /* a serialized new_segment, a serialized caps, and serialization of our
303 * incoming streamheader */
304 fail_unless_equals_int (shbuffers->len, 3);
306 gst_caps_unref (sinkcaps);
308 /* first buffer is the serialized new_segment event;
309 * the element also holds a ref to it */
310 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
311 buffers = g_list_remove (buffers, outbuffer);
312 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
313 gst_buffer_unref (outbuffer);
315 /* second buffer is the serialized caps;
316 * the element also holds a ref to it */
317 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
318 buffers = g_list_remove (buffers, outbuffer);
319 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
320 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
321 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
322 gst_buffer_unref (outbuffer);
324 /* the third buffer is the GDP buffer for our pushed buffer */
325 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
326 buffers = g_list_remove (buffers, outbuffer);
327 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
328 length = GST_DP_HEADER_LENGTH + 4;
329 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
330 gst_buffer_unref (outbuffer);
333 GST_DEBUG ("second buffer");
334 inbuffer = gst_buffer_new_and_alloc (4);
335 gst_buffer_set_caps (inbuffer, caps);
337 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
339 /* pushing gives away my reference */
340 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
342 fail_unless_equals_int (g_list_length (buffers), 1);
343 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
344 buffers = g_list_remove (buffers, outbuffer);
345 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
347 /* the third output buffer is data */
348 length = GST_DP_HEADER_LENGTH + 4;
349 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
350 gst_buffer_unref (outbuffer);
352 /* a third buffer without caps set explicitly; should work */
353 GST_DEBUG ("Creating third buffer, no caps set");
354 inbuffer = gst_buffer_new_and_alloc (4);
356 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
358 /* pushing gives away my reference */
359 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
361 fail_unless_equals_int (g_list_length (buffers), 1);
362 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
363 buffers = g_list_remove (buffers, outbuffer);
364 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
366 /* the fourth output buffer is data */
367 length = GST_DP_HEADER_LENGTH + 4;
368 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
369 gst_buffer_unref (outbuffer);
372 fail_unless (gst_element_set_state (gdppay,
373 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
375 gst_caps_unref (caps);
376 g_free (caps_string);
377 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
378 g_list_free (buffers);
380 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
381 gst_object_unref (gdppay);
387 GST_START_TEST (test_first_no_caps)
392 gdppay = setup_gdppay ();
394 fail_unless (gst_element_set_state (gdppay,
395 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
396 "could not set to playing");
398 GST_DEBUG ("first buffer");
399 inbuffer = gst_buffer_new_and_alloc (4);
400 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
402 /* pushing should trigger an error */
403 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_NOT_NEGOTIATED);
405 fail_unless_equals_int (g_list_length (buffers), 0);
407 fail_unless (gst_element_set_state (gdppay,
408 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
410 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
411 g_list_free (buffers);
413 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
414 gst_object_unref (gdppay);
419 /* element should still work if no new_segment is sent before the first
421 GST_START_TEST (test_first_no_new_segment)
427 gdppay = setup_gdppay ();
429 fail_unless (gst_element_set_state (gdppay,
430 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
431 "could not set to playing");
433 GST_DEBUG ("first buffer");
434 inbuffer = gst_buffer_new_and_alloc (4);
435 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
436 gst_buffer_set_caps (inbuffer, caps);
438 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
440 /* pushing gives away my reference */
441 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
443 /* we should have three buffers now;
444 * one for an "invented" new segment, one for GDP caps, and one with our
446 fail_unless_equals_int (g_list_length (buffers), 3);
448 fail_unless (gst_element_set_state (gdppay,
449 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
451 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
452 g_list_free (buffers);
454 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
455 gst_object_unref (gdppay);
460 GST_START_TEST (test_crc)
464 GstBuffer *inbuffer, *outbuffer;
468 guint16 crc_calculated, crc_read;
470 gdppay = setup_gdppay ();
471 g_object_set (gdppay, "crc-header", TRUE, NULL);
473 fail_unless (gst_element_set_state (gdppay,
474 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
475 "could not set to playing");
477 GST_DEBUG ("new segment");
479 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
480 fail_unless (gst_pad_push_event (mysrcpad, event));
482 /* no buffer should be pushed yet, waiting for caps */
483 fail_unless_equals_int (g_list_length (buffers), 0);
485 GST_DEBUG ("first buffer");
486 inbuffer = gst_buffer_new_and_alloc (4);
487 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
488 gst_buffer_set_caps (inbuffer, caps);
489 caps_string = gst_caps_to_string (caps);
491 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
493 /* pushing gives away my reference */
494 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
496 /* we should have three buffers now */
497 fail_unless_equals_int (g_list_length (buffers), 3);
499 /* first buffer is the serialized new_segment event;
500 * the element also holds a ref to it */
501 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
502 buffers = g_list_remove (buffers, outbuffer);
503 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
505 /* verify the header checksum */
506 /* CRC's start at 58 in the header */
507 crc_calculated = gst_dp_crc (GST_BUFFER_DATA (outbuffer), 58);
508 crc_read = GST_READ_UINT16_BE (GST_BUFFER_DATA (outbuffer) + 58);
509 fail_unless_equals_int (crc_calculated, crc_read);
511 /* change a byte in the header and verify that the checksum now fails */
512 GST_BUFFER_DATA (outbuffer)[0] = 0xff;
513 crc_calculated = gst_dp_crc (GST_BUFFER_DATA (outbuffer), 58);
514 fail_if (crc_calculated == crc_read,
515 "Introducing a byte error in the header should make the checksum fail");
517 gst_buffer_unref (outbuffer);
519 /* second buffer is the serialized caps;
520 * the element also holds a ref to it */
521 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
522 buffers = g_list_remove (buffers, outbuffer);
523 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
524 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
525 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
526 gst_buffer_unref (outbuffer);
528 /* the third buffer is the GDP buffer for our pushed buffer */
529 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
530 buffers = g_list_remove (buffers, outbuffer);
531 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
532 length = GST_DP_HEADER_LENGTH + 4;
533 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
534 gst_buffer_unref (outbuffer);
536 fail_unless (gst_element_set_state (gdppay,
537 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
539 gst_caps_unref (caps);
540 g_free (caps_string);
541 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
542 g_list_free (buffers);
544 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
545 gst_object_unref (gdppay);
554 Suite *s = suite_create ("gdppay");
555 TCase *tc_chain = tcase_create ("general");
557 suite_add_tcase (s, tc_chain);
558 tcase_add_test (tc_chain, test_audio);
559 tcase_add_test (tc_chain, test_first_no_caps);
560 tcase_add_test (tc_chain, test_first_no_new_segment);
561 tcase_add_test (tc_chain, test_streamheader);
562 tcase_add_test (tc_chain, test_crc);
568 main (int argc, char **argv)
572 Suite *s = gdppay_suite ();
573 SRunner *sr = srunner_create (s);
575 gst_check_init (&argc, &argv);
577 srunner_run_all (sr, CK_NORMAL);
578 nf = srunner_ntests_failed (sr);