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 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
30 /* For ease of programming we use globals to keep refs for our floating
31 * src and sink pads we create; otherwise we always have to do get_pad,
32 * get_peer, and then remove references in every test function */
33 GstPad *mysrcpad, *myshsrcpad, *mysinkpad;
35 #define AUDIO_CAPS_TEMPLATE_STRING \
37 "rate = (int) [ 1, MAX ], " \
38 "channels = (int) [ 1, 8 ], " \
39 "endianness = (int) BYTE_ORDER, " \
40 "width = (int) {8, 16}, " \
41 "depth = (int) {8, 16}, " \
42 "signed = (boolean) true"
44 #define AUDIO_CAPS_STRING \
46 "rate = (int) 1000, " \
47 "channels = (int) 2, " \
48 "endianness = (int) BYTE_ORDER, " \
49 "width = (int) 16, " \
50 "depth = (int) 16, " \
51 "signed = (boolean) true"
54 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS ("application/x-gdp")
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
65 /* takes over reference for outcaps */
71 GST_DEBUG ("setup_gdppay");
72 gdppay = gst_check_setup_element ("gdppay");
73 mysrcpad = gst_check_setup_src_pad (gdppay, &srctemplate, NULL);
74 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
80 cleanup_gdppay (GstElement * gdppay)
82 GST_DEBUG ("cleanup_gdppay");
84 gst_check_teardown_src_pad (gdppay);
85 gst_check_teardown_sink_pad (gdppay);
86 gst_check_teardown_element (gdppay);
90 GST_START_TEST (test_audio)
94 GstBuffer *inbuffer, *outbuffer;
99 gdppay = setup_gdppay ();
101 fail_unless (gst_element_set_state (gdppay,
102 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
103 "could not set to playing");
105 GST_DEBUG ("new segment");
107 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
108 fail_unless (gst_pad_push_event (mysrcpad, event));
110 /* no buffer should be pushed yet, waiting for caps */
111 fail_unless_equals_int (g_list_length (buffers), 0);
113 GST_DEBUG ("first buffer");
114 inbuffer = gst_buffer_new_and_alloc (4);
115 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
116 gst_buffer_set_caps (inbuffer, caps);
117 caps_string = gst_caps_to_string (caps);
119 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
121 /* pushing gives away my reference */
122 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
124 /* we should have three buffers now */
125 fail_unless_equals_int (g_list_length (buffers), 3);
127 /* first buffer is the serialized new_segment event;
128 * the element also holds a ref to it */
129 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
130 buffers = g_list_remove (buffers, outbuffer);
131 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
132 gst_buffer_unref (outbuffer);
134 /* second buffer is the serialized caps;
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 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
140 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
141 gst_buffer_unref (outbuffer);
143 /* the third buffer is the GDP buffer for our pushed buffer */
144 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
145 buffers = g_list_remove (buffers, outbuffer);
146 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
147 length = GST_DP_HEADER_LENGTH + 4;
148 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
149 gst_buffer_unref (outbuffer);
152 GST_DEBUG ("second buffer");
153 inbuffer = gst_buffer_new_and_alloc (4);
154 gst_buffer_set_caps (inbuffer, caps);
156 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
158 /* pushing gives away my reference */
159 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
161 fail_unless_equals_int (g_list_length (buffers), 1);
162 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
163 buffers = g_list_remove (buffers, outbuffer);
164 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
166 /* the third output buffer is data */
167 length = GST_DP_HEADER_LENGTH + 4;
168 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
169 gst_buffer_unref (outbuffer);
171 /* a third buffer without caps set explicitly; should work */
172 GST_DEBUG ("Creating third buffer, no caps set");
173 inbuffer = gst_buffer_new_and_alloc (4);
175 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
177 /* pushing gives away my reference */
178 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
180 fail_unless_equals_int (g_list_length (buffers), 1);
181 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
182 buffers = g_list_remove (buffers, outbuffer);
183 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
185 /* the fourth output buffer is data */
186 length = GST_DP_HEADER_LENGTH + 4;
187 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
188 gst_buffer_unref (outbuffer);
191 fail_unless (gst_element_set_state (gdppay,
192 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
194 gst_caps_unref (caps);
195 g_free (caps_string);
196 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
197 g_list_free (buffers);
199 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
200 gst_object_unref (gdppay);
205 static GstStaticPadTemplate shsrctemplate = GST_STATIC_PAD_TEMPLATE ("src",
208 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
213 setup_gdppay_streamheader ()
217 GST_DEBUG ("setup_gdppay");
218 gdppay = gst_check_setup_element ("gdppay");
219 myshsrcpad = gst_check_setup_src_pad (gdppay, &shsrctemplate, NULL);
220 mysinkpad = gst_check_setup_sink_pad (gdppay, &sinktemplate, NULL);
225 /* this test serializes a stream that already has a streamheader of its own.
226 * the streamheader should then be serialized and put on the GDP stream's
228 GST_START_TEST (test_streamheader)
230 GstCaps *caps, *sinkcaps;
232 GstBuffer *inbuffer, *outbuffer, *shbuffer;
236 GstStructure *structure;
237 GValue array = { 0 };
238 GValue value = { 0 };
243 gdppay = setup_gdppay_streamheader ();
245 fail_unless (gst_element_set_state (gdppay,
246 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
247 "could not set to playing");
249 GST_DEBUG ("new segment");
251 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME, 0, GST_SECOND, 0);
252 fail_unless (gst_pad_push_event (myshsrcpad, event));
254 /* no buffer should be pushed yet, still waiting for caps */
255 fail_unless_equals_int (g_list_length (buffers), 0);
257 GST_DEBUG ("first buffer");
258 inbuffer = gst_buffer_new_and_alloc (4);
259 memcpy (GST_BUFFER_DATA (inbuffer), "head", 4);
260 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
261 structure = gst_caps_get_structure (caps, 0);
262 GST_BUFFER_FLAG_SET (inbuffer, GST_BUFFER_FLAG_IN_CAPS);
263 g_value_init (&array, GST_TYPE_ARRAY);
264 g_value_init (&value, GST_TYPE_BUFFER);
265 shbuffer = gst_buffer_copy (inbuffer);
266 gst_value_set_buffer (&value, shbuffer);
267 gst_buffer_unref (shbuffer);
268 gst_value_array_append_value (&array, &value);
269 g_value_unset (&value);
270 gst_structure_set_value (structure, "streamheader", &array);
271 g_value_unset (&array);
272 caps_string = gst_caps_to_string (caps);
274 gst_buffer_set_caps (inbuffer, caps);
275 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
277 /* pushing gives away my reference */
278 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
280 /* we should have three buffers now */
281 fail_unless_equals_int (g_list_length (buffers), 3);
283 /* our sink pad should now have GDP caps with a streamheader that includes
284 * GDP wrappings of our streamheader */
285 sinkcaps = gst_pad_get_negotiated_caps (mysinkpad);
286 structure = gst_caps_get_structure (sinkcaps, 0);
287 fail_unless_equals_string ((gchar *) gst_structure_get_name (structure),
288 "application/x-gdp");
289 fail_unless (gst_structure_has_field (structure, "streamheader"));
290 sh = gst_structure_get_value (structure, "streamheader");
291 fail_unless (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
292 shbuffers = g_value_peek_pointer (sh);
293 /* a serialized new_segment, a serialized caps, and serialization of our
294 * incoming streamheader */
295 fail_unless_equals_int (shbuffers->len, 3);
297 gst_caps_unref (sinkcaps);
299 /* first buffer is the serialized new_segment event;
300 * the element also holds a ref to it */
301 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
302 buffers = g_list_remove (buffers, outbuffer);
303 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 2);
304 gst_buffer_unref (outbuffer);
306 /* second buffer is the serialized caps;
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 length = GST_DP_HEADER_LENGTH + (strlen (caps_string) + 1);
312 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
313 gst_buffer_unref (outbuffer);
315 /* the third buffer is the GDP buffer for our pushed buffer */
316 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
317 buffers = g_list_remove (buffers, outbuffer);
318 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
319 length = GST_DP_HEADER_LENGTH + 4;
320 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
321 gst_buffer_unref (outbuffer);
324 GST_DEBUG ("second buffer");
325 inbuffer = gst_buffer_new_and_alloc (4);
326 gst_buffer_set_caps (inbuffer, caps);
328 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
330 /* pushing gives away my reference */
331 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
333 fail_unless_equals_int (g_list_length (buffers), 1);
334 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
335 buffers = g_list_remove (buffers, outbuffer);
336 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
338 /* the third output buffer is data */
339 length = GST_DP_HEADER_LENGTH + 4;
340 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
341 gst_buffer_unref (outbuffer);
343 /* a third buffer without caps set explicitly; should work */
344 GST_DEBUG ("Creating third buffer, no caps set");
345 inbuffer = gst_buffer_new_and_alloc (4);
347 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
349 /* pushing gives away my reference */
350 fail_unless (gst_pad_push (myshsrcpad, inbuffer) == GST_FLOW_OK);
352 fail_unless_equals_int (g_list_length (buffers), 1);
353 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
354 buffers = g_list_remove (buffers, outbuffer);
355 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
357 /* the fourth output buffer is data */
358 length = GST_DP_HEADER_LENGTH + 4;
359 fail_unless_equals_int (GST_BUFFER_SIZE (outbuffer), length);
360 gst_buffer_unref (outbuffer);
363 fail_unless (gst_element_set_state (gdppay,
364 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
366 gst_caps_unref (caps);
367 g_free (caps_string);
368 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
369 g_list_free (buffers);
371 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
372 gst_object_unref (gdppay);
378 GST_START_TEST (test_first_no_caps)
383 gdppay = setup_gdppay ();
385 fail_unless (gst_element_set_state (gdppay,
386 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
387 "could not set to playing");
389 GST_DEBUG ("first buffer");
390 inbuffer = gst_buffer_new_and_alloc (4);
391 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
393 /* pushing should trigger an error */
394 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_NOT_NEGOTIATED);
396 fail_unless_equals_int (g_list_length (buffers), 0);
398 fail_unless (gst_element_set_state (gdppay,
399 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
401 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
402 g_list_free (buffers);
404 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
405 gst_object_unref (gdppay);
410 /* element should still work if no new_segment is sent before the first
412 GST_START_TEST (test_first_no_new_segment)
418 gdppay = setup_gdppay ();
420 fail_unless (gst_element_set_state (gdppay,
421 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
422 "could not set to playing");
424 GST_DEBUG ("first buffer");
425 inbuffer = gst_buffer_new_and_alloc (4);
426 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
427 gst_buffer_set_caps (inbuffer, caps);
429 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
431 /* pushing gives away my reference */
432 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
434 /* we should have three buffers now;
435 * one for an "invented" new segment, one for GDP caps, and one with our
437 fail_unless_equals_int (g_list_length (buffers), 3);
439 fail_unless (gst_element_set_state (gdppay,
440 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
442 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
443 g_list_free (buffers);
445 ASSERT_OBJECT_REFCOUNT (gdppay, "gdppay", 1);
446 gst_object_unref (gdppay);
455 Suite *s = suite_create ("gdppay");
456 TCase *tc_chain = tcase_create ("general");
458 suite_add_tcase (s, tc_chain);
459 tcase_add_test (tc_chain, test_audio);
460 tcase_add_test (tc_chain, test_first_no_caps);
461 tcase_add_test (tc_chain, test_first_no_new_segment);
462 tcase_add_test (tc_chain, test_streamheader);
468 main (int argc, char **argv)
472 Suite *s = gdppay_suite ();
473 SRunner *sr = srunner_create (s);
475 gst_check_init (&argc, &argv);
477 srunner_run_all (sr, CK_NORMAL);
478 nf = srunner_ntests_failed (sr);