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.
25 #include <gst/check/gstcheck.h>
26 #include <gst/dataprotocol/dataprotocol.h>
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, *mysinkpad, *myshsinkpad;
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 (AUDIO_CAPS_TEMPLATE_STRING)
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-gdp")
65 /* takes over reference for outcaps */
71 GST_DEBUG ("setup_gdpdepay");
72 gdpdepay = gst_check_setup_element ("gdpdepay");
73 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
74 mysinkpad = gst_check_setup_sink_pad (gdpdepay, &sinktemplate, NULL);
75 gst_pad_set_active (mysrcpad, TRUE);
76 gst_pad_set_active (mysinkpad, TRUE);
82 cleanup_gdpdepay (GstElement * gdpdepay)
84 GST_DEBUG ("cleanup_gdpdepay");
86 gst_pad_set_active (mysrcpad, FALSE);
88 gst_pad_set_active (mysinkpad, FALSE);
90 gst_pad_set_active (myshsinkpad, FALSE);
91 gst_check_teardown_src_pad (gdpdepay);
92 gst_check_teardown_sink_pad (gdpdepay);
93 gst_check_teardown_element (gdpdepay);
99 gdpdepay_push_per_byte (gchar * reason, guint8 * bytes, guint length)
104 for (i = 0; i < length; ++i) {
105 inbuffer = gst_buffer_new_and_alloc (1);
106 GST_BUFFER_DATA (inbuffer)[0] = bytes[i];
107 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
108 "%s: failed pushing byte buffer", reason);
112 GST_START_TEST (test_audio_per_byte)
116 GstElement *gdpdepay;
117 GstBuffer *buffer, *outbuffer;
118 guint8 *header, *payload;
122 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
124 gdpdepay = setup_gdpdepay ();
125 srcpad = gst_element_get_pad (gdpdepay, "src");
127 fail_unless (gst_element_set_state (gdpdepay,
128 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
129 "could not set to playing");
131 caps = gst_pad_get_caps (srcpad);
132 fail_unless (gst_caps_is_any (caps));
133 gst_caps_unref (caps);
134 fail_if (gst_pad_get_negotiated_caps (srcpad));
136 /* create caps and buffer packets and push them */
137 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
138 fail_unless (pk->packet_from_caps (caps, 0, &len, &header, &payload));
139 gst_caps_unref (caps);
140 gdpdepay_push_per_byte ("caps header", header, len);
141 fail_unless_equals_int (g_list_length (buffers), 0);
142 gdpdepay_push_per_byte ("caps payload", payload,
143 gst_dp_header_payload_length (header));
144 fail_unless_equals_int (g_list_length (buffers), 0);
145 caps = gst_pad_get_caps (srcpad);
146 fail_if (gst_caps_is_any (caps));
147 gst_caps_unref (caps);
152 buffer = gst_buffer_new_and_alloc (4);
153 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
154 GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
155 GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
156 fail_unless (pk->header_from_buffer (buffer, 0, &len, &header));
157 gdpdepay_push_per_byte ("buffer header", header, len);
158 fail_unless_equals_int (g_list_length (buffers), 0);
159 gdpdepay_push_per_byte ("buffer payload", GST_BUFFER_DATA (buffer),
160 gst_dp_header_payload_length (header));
162 gst_buffer_unref (buffer);
164 fail_unless_equals_int (g_list_length (buffers), 1);
165 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
166 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
167 fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
169 buffers = g_list_remove (buffers, outbuffer);
170 gst_buffer_unref (outbuffer);
172 fail_unless (gst_element_set_state (gdpdepay,
173 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
175 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
176 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
177 g_list_free (buffers);
179 gst_object_unref (srcpad);
180 cleanup_gdpdepay (gdpdepay);
182 gst_dp_packetizer_free (pk);
187 GST_START_TEST (test_audio_in_one_buffer)
191 GstElement *gdpdepay;
192 GstBuffer *buffer, *inbuffer;
193 guint8 *caps_header, *caps_payload, *buf_header;
194 guint header_len, payload_len;
198 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
200 gdpdepay = setup_gdpdepay ();
201 srcpad = gst_element_get_pad (gdpdepay, "src");
203 fail_unless (gst_element_set_state (gdpdepay,
204 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
205 "could not set to playing");
207 /* make sure no caps are set yet */
208 caps = gst_pad_get_caps (srcpad);
209 fail_unless (gst_caps_is_any (caps));
210 gst_caps_unref (caps);
211 fail_if (gst_pad_get_negotiated_caps (srcpad));
213 /* create caps and buffer packets and push them as one buffer */
214 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
215 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
218 buffer = gst_buffer_new_and_alloc (4);
219 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
220 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
222 payload_len = gst_dp_header_payload_length (caps_header);
224 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
225 payload_len + GST_BUFFER_SIZE (buffer));
226 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
227 i = GST_DP_HEADER_LENGTH;
228 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
230 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
231 i += GST_DP_HEADER_LENGTH;
232 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
233 GST_BUFFER_SIZE (buffer));
235 gst_caps_unref (caps);
236 gst_buffer_unref (buffer);
238 g_free (caps_header);
239 g_free (caps_payload);
243 gst_pad_push (mysrcpad, inbuffer);
245 /* the buffer is still queued */
246 fail_unless_equals_int (g_list_length (buffers), 1);
248 fail_unless (gst_element_set_state (gdpdepay,
249 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
251 gst_object_unref (srcpad);
252 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
253 g_list_free (buffers);
255 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
256 cleanup_gdpdepay (gdpdepay);
258 gst_dp_packetizer_free (pk);
263 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
266 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
270 setup_gdpdepay_streamheader ()
272 GstElement *gdpdepay;
274 GST_DEBUG ("setup_gdpdepay");
275 gdpdepay = gst_check_setup_element ("gdpdepay");
276 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
277 myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate, NULL);
278 gst_pad_set_active (mysrcpad, TRUE);
279 gst_pad_set_active (myshsinkpad, TRUE);
284 #ifndef HAVE_CPU_PPC64 /* Test known to fail on PPC64. See #348114 */
286 /* this tests deserialization of a GDP stream where the serialized caps
287 * have a streamheader set */
288 GST_START_TEST (test_streamheader)
290 GstCaps *caps, *padcaps;
292 GstElement *gdpdepay;
293 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
294 guint8 *caps_header, *caps_payload, *buf_header;
295 guint header_len, payload_len;
297 GstStructure *structure;
298 GValue array = { 0 };
299 GValue value = { 0 };
302 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
304 gdpdepay = setup_gdpdepay_streamheader ();
305 srcpad = gst_element_get_pad (gdpdepay, "src");
306 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
308 fail_unless (gst_element_set_state (gdpdepay,
309 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
310 "could not set to playing");
312 /* make sure no caps are set yet */
313 caps = gst_pad_get_caps (srcpad);
314 fail_unless (gst_caps_is_any (caps));
315 gst_caps_unref (caps);
316 fail_if (gst_pad_get_negotiated_caps (srcpad));
318 /* create a streamheader buffer and the caps containing it */
319 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
320 structure = gst_caps_get_structure (caps, 0);
321 buffer = gst_buffer_new_and_alloc (4);
322 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
323 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
324 g_value_init (&array, GST_TYPE_ARRAY);
325 g_value_init (&value, GST_TYPE_BUFFER);
326 shbuffer = gst_buffer_copy (buffer);
327 gst_value_set_buffer (&value, shbuffer);
328 gst_buffer_unref (shbuffer);
329 gst_value_array_append_value (&array, &value);
330 g_value_unset (&value);
331 gst_structure_set_value (structure, "streamheader", &array);
332 g_value_unset (&array);
334 gst_buffer_set_caps (buffer, caps);
336 /* create GDP packets for the caps and the buffer, and put them in one
338 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
341 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
343 payload_len = gst_dp_header_payload_length (caps_header);
345 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
346 payload_len + GST_BUFFER_SIZE (buffer));
347 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
348 i = GST_DP_HEADER_LENGTH;
349 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
351 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
352 i += GST_DP_HEADER_LENGTH;
353 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
354 GST_BUFFER_SIZE (buffer));
356 gst_caps_unref (caps);
357 gst_buffer_unref (buffer);
359 g_free (caps_header);
360 g_free (caps_payload);
364 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
365 gst_pad_push (mysrcpad, inbuffer);
367 /* our only output buffer is the streamheader buffer */
368 fail_unless_equals_int (g_list_length (buffers), 1);
369 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
370 buffers = g_list_remove (buffers, outbuffer);
371 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
372 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
374 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
375 caps = gst_buffer_get_caps (outbuffer);
376 fail_if (caps == NULL);
377 fail_if (padcaps == NULL);
378 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
380 fail_unless (gst_caps_is_equal (padcaps, caps));
382 /* FIXME: get streamheader, compare data with buffer */
383 gst_buffer_unref (outbuffer);
384 gst_caps_unref (padcaps);
385 gst_caps_unref (caps);
388 fail_unless (gst_element_set_state (gdpdepay,
389 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
391 gst_object_unref (srcpad);
392 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
393 g_list_free (buffers);
395 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
396 cleanup_gdpdepay (gdpdepay);
398 gst_dp_packetizer_free (pk);
403 #endif /* ifndef HAVE_CPU_PPC64 */
406 gdpdepay_suite (void)
408 Suite *s = suite_create ("gdpdepay");
409 TCase *tc_chain = tcase_create ("general");
411 suite_add_tcase (s, tc_chain);
412 tcase_add_test (tc_chain, test_audio_per_byte);
413 tcase_add_test (tc_chain, test_audio_in_one_buffer);
414 #ifndef HAVE_CPU_PPC64 /* Test known to fail on PPC64. See #348114 */
415 tcase_add_test (tc_chain, test_streamheader);
421 main (int argc, char **argv)
425 Suite *s = gdpdepay_suite ();
426 SRunner *sr = srunner_create (s);
428 gst_check_init (&argc, &argv);
430 srunner_run_all (sr, CK_NORMAL);
431 nf = srunner_ntests_failed (sr);