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 /* For ease of programming we use globals to keep refs for our floating
28 * src and sink pads we create; otherwise we always have to do get_pad,
29 * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *myshsrcpad, *mysinkpad;
32 #define AUDIO_CAPS_TEMPLATE_STRING \
34 "rate = (int) [ 1, MAX ], " \
35 "channels = (int) [ 1, 8 ], " \
36 "endianness = (int) BYTE_ORDER, " \
37 "width = (int) {8, 16}, " \
38 "depth = (int) {8, 16}, " \
39 "signed = (boolean) true"
41 #define AUDIO_CAPS_STRING \
43 "rate = (int) 1000, " \
44 "channels = (int) 2, " \
45 "endianness = (int) BYTE_ORDER, " \
46 "width = (int) 16, " \
47 "depth = (int) 16, " \
48 "signed = (boolean) true"
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("application/x-gdp")
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
62 /* takes over reference for outcaps */
68 GST_DEBUG ("setup_gdppay");
69 gdppay = gst_check_setup_element ("gdppay");
70 mysrcpad = gst_check_setup_src_pad (gdppay, &srctemplate, NULL);
71 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
72 gst_pad_set_active (mysrcpad, TRUE);
73 gst_pad_set_active (mysinkpad, TRUE);
79 cleanup_gdppay (GstElement * gdppay)
81 GST_DEBUG ("cleanup_gdppay");
84 gst_pad_set_active (mysrcpad, FALSE);
86 gst_pad_set_active (myshsrcpad, FALSE);
87 gst_pad_set_active (mysinkpad, FALSE);
88 gst_check_teardown_src_pad (gdppay);
89 gst_check_teardown_sink_pad (gdppay);
90 gst_check_teardown_element (gdppay);
95 GST_START_TEST (test_audio)
99 GstBuffer *inbuffer, *outbuffer;
104 gdppay = setup_gdppay ();
106 fail_unless (gst_element_set_state (gdppay,
107 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
108 "could not set to playing");
110 GST_DEBUG ("new segment");
112 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
113 fail_unless (gst_pad_push_event (mysrcpad, event));
115 /* no buffer should be pushed yet, waiting for caps */
116 fail_unless_equals_int (g_list_length (buffers), 0);
118 GST_DEBUG ("first buffer");
119 inbuffer = gst_buffer_new_and_alloc (4);
120 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
121 gst_buffer_set_caps (inbuffer, caps);
122 caps_string = gst_caps_to_string (caps);
124 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
126 /* pushing gives away my reference */
127 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
129 /* we should have three buffers now */
130 fail_unless_equals_int (g_list_length (buffers), 3);
132 /* first buffer is the serialized new_segment event;
133 * the element also holds a ref to it */
134 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
135 buffers = g_list_remove (buffers, outbuffer);
136 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
137 gst_buffer_unref (outbuffer);
139 /* second buffer is the serialized caps;
140 * the element also holds a ref to it */
141 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
142 buffers = g_list_remove (buffers, outbuffer);
143 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
144 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
145 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
146 gst_buffer_unref (outbuffer);
148 /* the third buffer is the GDP buffer for our pushed buffer */
149 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
150 buffers = g_list_remove (buffers, outbuffer);
151 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
152 length = GST_DP_HEADER_LENGTH + 4;
153 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
154 gst_buffer_unref (outbuffer);
157 GST_DEBUG ("second buffer");
158 inbuffer = gst_buffer_new_and_alloc (4);
159 gst_buffer_set_caps (inbuffer, caps);
161 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
163 /* pushing gives away my reference */
164 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
166 fail_unless_equals_int (g_list_length (buffers), 1);
167 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
168 buffers = g_list_remove (buffers, outbuffer);
169 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
171 /* the third output buffer is data */
172 length = GST_DP_HEADER_LENGTH + 4;
173 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
174 gst_buffer_unref (outbuffer);
176 /* a third buffer without caps set explicitly; should work */
177 GST_DEBUG ("Creating third buffer, no caps set");
178 inbuffer = gst_buffer_new_and_alloc (4);
180 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
182 /* pushing gives away my reference */
183 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
185 fail_unless_equals_int (g_list_length (buffers), 1);
186 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
187 buffers = g_list_remove (buffers, outbuffer);
188 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
190 /* the fourth output buffer is data */
191 length = GST_DP_HEADER_LENGTH + 4;
192 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
193 gst_buffer_unref (outbuffer);
196 fail_unless (gst_element_set_state (gdppay,
197 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
199 gst_caps_unref (caps);
200 g_free (caps_string);
201 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
202 g_list_free (buffers);
204 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
205 cleanup_gdppay (gdppay);
210 static GstStaticPadTemplate shsrctemplate = GST_STATIC_PAD_TEMPLATE ("src",
213 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
218 setup_gdppay_streamheader (void)
222 GST_DEBUG ("setup_gdppay");
223 gdppay = gst_check_setup_element ("gdppay");
224 myshsrcpad = gst_check_setup_src_pad (gdppay, &shsrctemplate, NULL);
225 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
226 gst_pad_set_active (myshsrcpad, TRUE);
227 gst_pad_set_active (mysinkpad, TRUE);
232 /* this test serializes a stream that already has a streamheader of its own.
233 * the streamheader should then be serialized and put on the GDP stream's
235 GST_START_TEST (test_streamheader)
237 GstCaps *caps, *sinkcaps;
239 GstBuffer *inbuffer, *outbuffer, *shbuffer;
243 GstStructure *structure;
244 GValue array = { 0 };
245 GValue value = { 0 };
250 gdppay = setup_gdppay_streamheader ();
252 fail_unless (gst_element_set_state (gdppay,
253 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
254 "could not set to playing");
256 GST_DEBUG ("new segment");
258 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
259 fail_unless (gst_pad_push_event (myshsrcpad, event));
261 /* no buffer should be pushed yet, still waiting for caps */
262 fail_unless_equals_int (g_list_length (buffers), 0);
264 GST_DEBUG ("first buffer");
265 inbuffer = gst_buffer_new_and_alloc (4);
266 memcpy (GST_BUFFER_DATA (inbuffer), "head", 4);
267 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
268 structure = gst_caps_get_structure (caps, 0);
269 GST_BUFFER_FLAG_SET (inbuffer, GST_BUFFER_FLAG_IN_CAPS);
270 g_value_init (&array, GST_TYPE_ARRAY);
271 g_value_init (&value, GST_TYPE_BUFFER);
272 shbuffer = gst_buffer_copy (inbuffer);
273 gst_value_set_buffer (&value, shbuffer);
274 gst_buffer_unref (shbuffer);
275 gst_value_array_append_value (&array, &value);
276 g_value_unset (&value);
277 gst_structure_set_value (structure, "streamheader", &array);
278 g_value_unset (&array);
279 caps_string = gst_caps_to_string (caps);
281 gst_buffer_set_caps (inbuffer, caps);
282 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
284 /* pushing gives away my reference */
285 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
287 /* we should have three buffers now */
288 fail_unless_equals_int (g_list_length (buffers), 3);
290 /* our sink pad should now have GDP caps with a streamheader that includes
291 * GDP wrappings of our streamheader */
292 sinkcaps = gst_pad_get_negotiated_caps (mysinkpad);
293 structure = gst_caps_get_structure (sinkcaps, 0);
294 fail_unless_equals_string ((gchar *) gst_structure_get_name (structure),
295 "application/x-gdp");
296 fail_unless (gst_structure_has_field (structure, "streamheader"));
297 sh = gst_structure_get_value (structure, "streamheader");
298 fail_unless (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
299 shbuffers = g_value_peek_pointer (sh);
300 /* a serialized new_segment, a serialized caps, and serialization of our
301 * incoming streamheader */
302 fail_unless_equals_int (shbuffers->len, 3);
304 gst_caps_unref (sinkcaps);
306 /* first buffer is the serialized new_segment event;
307 * the element also holds a ref to it */
308 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
309 buffers = g_list_remove (buffers, outbuffer);
310 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
311 gst_buffer_unref (outbuffer);
313 /* second buffer is the serialized caps;
314 * the element also holds a ref to it */
315 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
316 buffers = g_list_remove (buffers, outbuffer);
317 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
318 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
319 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
320 gst_buffer_unref (outbuffer);
322 /* the third buffer is the GDP buffer for our pushed buffer */
323 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
324 buffers = g_list_remove (buffers, outbuffer);
325 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
326 length = GST_DP_HEADER_LENGTH + 4;
327 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
328 gst_buffer_unref (outbuffer);
331 GST_DEBUG ("second buffer");
332 inbuffer = gst_buffer_new_and_alloc (4);
333 gst_buffer_set_caps (inbuffer, caps);
335 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
337 /* pushing gives away my reference */
338 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
340 fail_unless_equals_int (g_list_length (buffers), 1);
341 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
342 buffers = g_list_remove (buffers, outbuffer);
343 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
345 /* the third output buffer is data */
346 length = GST_DP_HEADER_LENGTH + 4;
347 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
348 gst_buffer_unref (outbuffer);
350 /* a third buffer without caps set explicitly; should work */
351 GST_DEBUG ("Creating third buffer, no caps set");
352 inbuffer = gst_buffer_new_and_alloc (4);
354 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
356 /* pushing gives away my reference */
357 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
359 fail_unless_equals_int (g_list_length (buffers), 1);
360 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
361 buffers = g_list_remove (buffers, outbuffer);
362 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
364 /* the fourth output buffer is data */
365 length = GST_DP_HEADER_LENGTH + 4;
366 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
367 gst_buffer_unref (outbuffer);
370 fail_unless (gst_element_set_state (gdppay,
371 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
373 gst_caps_unref (caps);
374 g_free (caps_string);
375 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
376 g_list_free (buffers);
378 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
379 cleanup_gdppay (gdppay);
385 GST_START_TEST (test_first_no_caps)
390 gdppay = setup_gdppay ();
392 fail_unless (gst_element_set_state (gdppay,
393 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
394 "could not set to playing");
396 GST_DEBUG ("first buffer");
397 inbuffer = gst_buffer_new_and_alloc (4);
398 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
400 /* pushing should trigger an error */
401 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_NOT_NEGOTIATED);
403 fail_unless_equals_int (g_list_length (buffers), 0);
405 fail_unless (gst_element_set_state (gdppay,
406 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
408 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
409 g_list_free (buffers);
411 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
412 cleanup_gdppay (gdppay);
417 /* element should still work if no new_segment is sent before the first
419 GST_START_TEST (test_first_no_new_segment)
425 gdppay = setup_gdppay ();
427 fail_unless (gst_element_set_state (gdppay,
428 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
429 "could not set to playing");
431 GST_DEBUG ("first buffer");
432 inbuffer = gst_buffer_new_and_alloc (4);
433 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
434 gst_buffer_set_caps (inbuffer, caps);
435 gst_caps_unref (caps);
437 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
439 /* pushing gives away my reference */
440 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
442 /* we should have three buffers now;
443 * one for an "invented" new segment, one for GDP caps, and one with our
445 fail_unless_equals_int (g_list_length (buffers), 3);
447 fail_unless (gst_element_set_state (gdppay,
448 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
450 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
451 g_list_free (buffers);
453 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
454 cleanup_gdppay (gdppay);
459 GST_START_TEST (test_crc)
463 GstBuffer *inbuffer, *outbuffer;
467 guint16 crc_calculated, crc_read;
469 gdppay = setup_gdppay ();
470 g_object_set (gdppay, "crc-header", TRUE, NULL);
472 fail_unless (gst_element_set_state (gdppay,
473 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
474 "could not set to playing");
476 GST_DEBUG ("new segment");
478 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
479 fail_unless (gst_pad_push_event (mysrcpad, event));
481 /* no buffer should be pushed yet, waiting for caps */
482 fail_unless_equals_int (g_list_length (buffers), 0);
484 GST_DEBUG ("first buffer");
485 inbuffer = gst_buffer_new_and_alloc (4);
486 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
487 gst_buffer_set_caps (inbuffer, caps);
488 caps_string = gst_caps_to_string (caps);
490 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
492 /* pushing gives away my reference */
493 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
495 /* we should have three buffers now */
496 fail_unless_equals_int (g_list_length (buffers), 3);
498 /* first buffer is the serialized new_segment event;
499 * the element also holds a ref to it */
500 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
501 buffers = g_list_remove (buffers, outbuffer);
502 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
504 /* verify the header checksum */
505 /* CRC's start at 58 in the header */
506 crc_calculated = gst_dp_crc (GST_BUFFER_DATA (outbuffer), 58);
507 crc_read = GST_READ_UINT16_BE (GST_BUFFER_DATA (outbuffer) + 58);
508 fail_unless_equals_int (crc_calculated, crc_read);
510 /* change a byte in the header and verify that the checksum now fails */
511 GST_BUFFER_DATA (outbuffer)[0] = 0xff;
512 crc_calculated = gst_dp_crc (GST_BUFFER_DATA (outbuffer), 58);
513 fail_if (crc_calculated == crc_read,
514 "Introducing a byte error in the header should make the checksum fail");
516 gst_buffer_unref (outbuffer);
518 /* second buffer is the serialized caps;
519 * the element also holds a ref to it */
520 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
521 buffers = g_list_remove (buffers, outbuffer);
522 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
523 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
524 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
525 gst_buffer_unref (outbuffer);
527 /* the third buffer is the GDP buffer for our pushed buffer */
528 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
529 buffers = g_list_remove (buffers, outbuffer);
530 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
531 length = GST_DP_HEADER_LENGTH + 4;
532 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
533 gst_buffer_unref (outbuffer);
535 fail_unless (gst_element_set_state (gdppay,
536 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
538 gst_caps_unref (caps);
539 g_free (caps_string);
540 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
541 g_list_free (buffers);
543 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
544 cleanup_gdppay (gdppay);
553 Suite *s = suite_create ("gdppay");
554 TCase *tc_chain = tcase_create ("general");
556 suite_add_tcase (s, tc_chain);
557 tcase_add_test (tc_chain, test_audio);
558 tcase_add_test (tc_chain, test_first_no_caps);
559 tcase_add_test (tc_chain, test_first_no_new_segment);
560 tcase_add_test (tc_chain, test_streamheader);
561 tcase_add_test (tc_chain, test_crc);
566 GST_CHECK_MAIN (gdppay);